|
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/. */ |
|
5 |
|
6 #ifndef MOZILLA_IMAGELIB_IMAGEURL_H_ |
|
7 #define MOZILLA_IMAGELIB_IMAGEURL_H_ |
|
8 |
|
9 #include "nsIURI.h" |
|
10 #include "MainThreadUtils.h" |
|
11 #include "nsNetUtil.h" |
|
12 |
|
13 namespace mozilla { |
|
14 namespace image { |
|
15 |
|
16 /** ImageURL |
|
17 * |
|
18 * nsStandardURL is not threadsafe, so this class is created to hold only the |
|
19 * necessary URL data required for image loading and decoding. |
|
20 * |
|
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). |
|
27 */ |
|
28 class ImageURL |
|
29 { |
|
30 public: |
|
31 explicit ImageURL(nsIURI* aURI) { |
|
32 MOZ_ASSERT(NS_IsMainThread(), "Cannot use nsIURI off main thread!"); |
|
33 aURI->GetSpec(mSpec); |
|
34 aURI->GetScheme(mScheme); |
|
35 aURI->GetRef(mRef); |
|
36 } |
|
37 |
|
38 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(mozilla::image::ImageURL) |
|
39 |
|
40 nsresult GetSpec(nsACString &result) { |
|
41 result = mSpec; |
|
42 return NS_OK; |
|
43 } |
|
44 |
|
45 nsresult GetScheme(nsACString &result) { |
|
46 result = mScheme; |
|
47 return NS_OK; |
|
48 } |
|
49 |
|
50 nsresult SchemeIs(const char *scheme, bool *result) |
|
51 { |
|
52 NS_PRECONDITION(scheme, "scheme is null"); |
|
53 NS_PRECONDITION(result, "result is null"); |
|
54 |
|
55 *result = mScheme.Equals(scheme); |
|
56 return NS_OK; |
|
57 } |
|
58 |
|
59 nsresult GetRef(nsACString &result) { |
|
60 result = mRef; |
|
61 return NS_OK; |
|
62 } |
|
63 |
|
64 already_AddRefed<nsIURI> ToIURI() { |
|
65 MOZ_ASSERT(NS_IsMainThread(), |
|
66 "Convert to nsIURI on main thread only; it is not threadsafe."); |
|
67 nsCOMPtr<nsIURI> newURI; |
|
68 NS_NewURI(getter_AddRefs(newURI), mSpec); |
|
69 return newURI.forget(); |
|
70 } |
|
71 |
|
72 nsresult Equals(nsIURI *otherURI, bool *result) { |
|
73 nsAutoCString otherSpec; |
|
74 otherURI->GetSpec(otherSpec); |
|
75 *result = mSpec.Equals(otherSpec); |
|
76 return NS_OK; |
|
77 } |
|
78 |
|
79 private: |
|
80 // Since this is a basic storage class, no duplication of spec parsing is |
|
81 // included in the functionality. Instead, the class depends upon the |
|
82 // parsing implementation in the nsIURI class used in object construction. |
|
83 // This means each field is stored separately, but since only a few are |
|
84 // required, this small memory tradeoff for threadsafe usage should be ok. |
|
85 nsAutoCString mSpec; |
|
86 nsAutoCString mScheme; |
|
87 nsAutoCString mRef; |
|
88 }; |
|
89 |
|
90 } // namespace image |
|
91 } // namespace mozilla |
|
92 |
|
93 #endif // MOZILLA_IMAGELIB_IMAGEURL_H_ |