Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef MOZILLA_IMAGELIB_IMAGEURL_H_ |
michael@0 | 7 | #define MOZILLA_IMAGELIB_IMAGEURL_H_ |
michael@0 | 8 | |
michael@0 | 9 | #include "nsIURI.h" |
michael@0 | 10 | #include "MainThreadUtils.h" |
michael@0 | 11 | #include "nsNetUtil.h" |
michael@0 | 12 | |
michael@0 | 13 | namespace mozilla { |
michael@0 | 14 | namespace image { |
michael@0 | 15 | |
michael@0 | 16 | /** ImageURL |
michael@0 | 17 | * |
michael@0 | 18 | * nsStandardURL is not threadsafe, so this class is created to hold only the |
michael@0 | 19 | * necessary URL data required for image loading and decoding. |
michael@0 | 20 | * |
michael@0 | 21 | * Note: Although several APIs have the same or similar prototypes as those |
michael@0 | 22 | * found in nsIURI/nsStandardURL, the class does not implement nsIURI. This is |
michael@0 | 23 | * intentional; functionality is limited, and is only useful for imagelib code. |
michael@0 | 24 | * By not implementing nsIURI, external code cannot unintentionally be given an |
michael@0 | 25 | * nsIURI pointer with this limited class behind it; instead, conversion to a |
michael@0 | 26 | * fully implemented nsIURI is required (e.g. through NS_NewURI). |
michael@0 | 27 | */ |
michael@0 | 28 | class ImageURL |
michael@0 | 29 | { |
michael@0 | 30 | public: |
michael@0 | 31 | explicit ImageURL(nsIURI* aURI) { |
michael@0 | 32 | MOZ_ASSERT(NS_IsMainThread(), "Cannot use nsIURI off main thread!"); |
michael@0 | 33 | aURI->GetSpec(mSpec); |
michael@0 | 34 | aURI->GetScheme(mScheme); |
michael@0 | 35 | aURI->GetRef(mRef); |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | NS_INLINE_DECL_THREADSAFE_REFCOUNTING(mozilla::image::ImageURL) |
michael@0 | 39 | |
michael@0 | 40 | nsresult GetSpec(nsACString &result) { |
michael@0 | 41 | result = mSpec; |
michael@0 | 42 | return NS_OK; |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | nsresult GetScheme(nsACString &result) { |
michael@0 | 46 | result = mScheme; |
michael@0 | 47 | return NS_OK; |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | nsresult SchemeIs(const char *scheme, bool *result) |
michael@0 | 51 | { |
michael@0 | 52 | NS_PRECONDITION(scheme, "scheme is null"); |
michael@0 | 53 | NS_PRECONDITION(result, "result is null"); |
michael@0 | 54 | |
michael@0 | 55 | *result = mScheme.Equals(scheme); |
michael@0 | 56 | return NS_OK; |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | nsresult GetRef(nsACString &result) { |
michael@0 | 60 | result = mRef; |
michael@0 | 61 | return NS_OK; |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | already_AddRefed<nsIURI> ToIURI() { |
michael@0 | 65 | MOZ_ASSERT(NS_IsMainThread(), |
michael@0 | 66 | "Convert to nsIURI on main thread only; it is not threadsafe."); |
michael@0 | 67 | nsCOMPtr<nsIURI> newURI; |
michael@0 | 68 | NS_NewURI(getter_AddRefs(newURI), mSpec); |
michael@0 | 69 | return newURI.forget(); |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | nsresult Equals(nsIURI *otherURI, bool *result) { |
michael@0 | 73 | nsAutoCString otherSpec; |
michael@0 | 74 | otherURI->GetSpec(otherSpec); |
michael@0 | 75 | *result = mSpec.Equals(otherSpec); |
michael@0 | 76 | return NS_OK; |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | private: |
michael@0 | 80 | // Since this is a basic storage class, no duplication of spec parsing is |
michael@0 | 81 | // included in the functionality. Instead, the class depends upon the |
michael@0 | 82 | // parsing implementation in the nsIURI class used in object construction. |
michael@0 | 83 | // This means each field is stored separately, but since only a few are |
michael@0 | 84 | // required, this small memory tradeoff for threadsafe usage should be ok. |
michael@0 | 85 | nsAutoCString mSpec; |
michael@0 | 86 | nsAutoCString mScheme; |
michael@0 | 87 | nsAutoCString mRef; |
michael@0 | 88 | }; |
michael@0 | 89 | |
michael@0 | 90 | } // namespace image |
michael@0 | 91 | } // namespace mozilla |
michael@0 | 92 | |
michael@0 | 93 | #endif // MOZILLA_IMAGELIB_IMAGEURL_H_ |