Backed out changeset 177eae915693 (bug 1206581) for bustage
[gecko.git] / image / ImageURL.h
blobdd72690fbd6800b6449f7edcea5a5ed065843837
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef mozilla_image_ImageURL_h
7 #define mozilla_image_ImageURL_h
9 #include "nsIURI.h"
10 #include "MainThreadUtils.h"
11 #include "nsNetUtil.h"
13 namespace mozilla {
14 namespace image {
16 /** ImageURL
18 * nsStandardURL is not threadsafe, so this class is created to hold only the
19 * necessary URL data required for image loading and decoding.
21 * Note: Although several APIs have the same or similar prototypes as those
22 * found in nsIURI/nsStandardURL, the class does not implement nsIURI. This is
23 * intentional; functionality is limited, and is only useful for imagelib code.
24 * By not implementing nsIURI, external code cannot unintentionally be given an
25 * nsIURI pointer with this limited class behind it; instead, conversion to a
26 * fully implemented nsIURI is required (e.g. through NS_NewURI).
28 class ImageURL
30 public:
31 explicit ImageURL(nsIURI* aURI)
33 MOZ_ASSERT(NS_IsMainThread(), "Cannot use nsIURI off main thread!");
34 aURI->GetSpec(mSpec);
35 aURI->GetScheme(mScheme);
36 aURI->GetRef(mRef);
39 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ImageURL)
41 nsresult GetSpec(nsACString& result)
43 result = mSpec;
44 return NS_OK;
47 /// A weak pointer to the URI spec for this ImageURL. For logging only.
48 const char* Spec() const { return mSpec.get(); }
50 enum TruncatedSpecStatus {
51 FitsInto1k,
52 TruncatedTo1k
54 TruncatedSpecStatus GetSpecTruncatedTo1k(nsACString& result)
56 static const size_t sMaxTruncatedLength = 1024;
58 if (sMaxTruncatedLength >= mSpec.Length()) {
59 result = mSpec;
60 return FitsInto1k;
63 result = Substring(mSpec, 0, sMaxTruncatedLength);
64 return TruncatedTo1k;
67 nsresult GetScheme(nsACString& result)
69 result = mScheme;
70 return NS_OK;
73 nsresult SchemeIs(const char* scheme, bool* result)
75 NS_PRECONDITION(scheme, "scheme is null");
76 NS_PRECONDITION(result, "result is null");
78 *result = mScheme.Equals(scheme);
79 return NS_OK;
82 nsresult GetRef(nsACString& result)
84 result = mRef;
85 return NS_OK;
88 already_AddRefed<nsIURI> ToIURI()
90 MOZ_ASSERT(NS_IsMainThread(),
91 "Convert to nsIURI on main thread only; it is not threadsafe.");
92 nsCOMPtr<nsIURI> newURI;
93 NS_NewURI(getter_AddRefs(newURI), mSpec);
94 return newURI.forget();
97 bool operator==(const ImageURL& aOther) const
99 // Note that we don't need to consider mScheme and mRef, because they're
100 // already represented in mSpec.
101 return mSpec == aOther.mSpec;
104 bool HasSameRef(const ImageURL& aOther) const
106 return mRef == aOther.mRef;
109 private:
110 // Since this is a basic storage class, no duplication of spec parsing is
111 // included in the functionality. Instead, the class depends upon the
112 // parsing implementation in the nsIURI class used in object construction.
113 // This means each field is stored separately, but since only a few are
114 // required, this small memory tradeoff for threadsafe usage should be ok.
115 nsAutoCString mSpec;
116 nsAutoCString mScheme;
117 nsAutoCString mRef;
119 ~ImageURL() { }
122 } // namespace image
123 } // namespace mozilla
125 #endif // mozilla_image_ImageURL_h