michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef MOZILLA_IMAGELIB_IMAGEURL_H_ michael@0: #define MOZILLA_IMAGELIB_IMAGEURL_H_ michael@0: michael@0: #include "nsIURI.h" michael@0: #include "MainThreadUtils.h" michael@0: #include "nsNetUtil.h" michael@0: michael@0: namespace mozilla { michael@0: namespace image { michael@0: michael@0: /** ImageURL michael@0: * michael@0: * nsStandardURL is not threadsafe, so this class is created to hold only the michael@0: * necessary URL data required for image loading and decoding. michael@0: * michael@0: * Note: Although several APIs have the same or similar prototypes as those michael@0: * found in nsIURI/nsStandardURL, the class does not implement nsIURI. This is michael@0: * intentional; functionality is limited, and is only useful for imagelib code. michael@0: * By not implementing nsIURI, external code cannot unintentionally be given an michael@0: * nsIURI pointer with this limited class behind it; instead, conversion to a michael@0: * fully implemented nsIURI is required (e.g. through NS_NewURI). michael@0: */ michael@0: class ImageURL michael@0: { michael@0: public: michael@0: explicit ImageURL(nsIURI* aURI) { michael@0: MOZ_ASSERT(NS_IsMainThread(), "Cannot use nsIURI off main thread!"); michael@0: aURI->GetSpec(mSpec); michael@0: aURI->GetScheme(mScheme); michael@0: aURI->GetRef(mRef); michael@0: } michael@0: michael@0: NS_INLINE_DECL_THREADSAFE_REFCOUNTING(mozilla::image::ImageURL) michael@0: michael@0: nsresult GetSpec(nsACString &result) { michael@0: result = mSpec; michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult GetScheme(nsACString &result) { michael@0: result = mScheme; michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult SchemeIs(const char *scheme, bool *result) michael@0: { michael@0: NS_PRECONDITION(scheme, "scheme is null"); michael@0: NS_PRECONDITION(result, "result is null"); michael@0: michael@0: *result = mScheme.Equals(scheme); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult GetRef(nsACString &result) { michael@0: result = mRef; michael@0: return NS_OK; michael@0: } michael@0: michael@0: already_AddRefed ToIURI() { michael@0: MOZ_ASSERT(NS_IsMainThread(), michael@0: "Convert to nsIURI on main thread only; it is not threadsafe."); michael@0: nsCOMPtr newURI; michael@0: NS_NewURI(getter_AddRefs(newURI), mSpec); michael@0: return newURI.forget(); michael@0: } michael@0: michael@0: nsresult Equals(nsIURI *otherURI, bool *result) { michael@0: nsAutoCString otherSpec; michael@0: otherURI->GetSpec(otherSpec); michael@0: *result = mSpec.Equals(otherSpec); michael@0: return NS_OK; michael@0: } michael@0: michael@0: private: michael@0: // Since this is a basic storage class, no duplication of spec parsing is michael@0: // included in the functionality. Instead, the class depends upon the michael@0: // parsing implementation in the nsIURI class used in object construction. michael@0: // This means each field is stored separately, but since only a few are michael@0: // required, this small memory tradeoff for threadsafe usage should be ok. michael@0: nsAutoCString mSpec; michael@0: nsAutoCString mScheme; michael@0: nsAutoCString mRef; michael@0: }; michael@0: michael@0: } // namespace image michael@0: } // namespace mozilla michael@0: michael@0: #endif // MOZILLA_IMAGELIB_IMAGEURL_H_