Fix typo in 9b54bd30006c008b4a951331b273613d5bac3abf
[pm.git] / image / src / ImageURL.h
blob9a5049f25293cb5a3184db6b8e76b3dead1fdd4d
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_IMAGELIB_IMAGEURL_H_
7 #define MOZILLA_IMAGELIB_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 nsresult GetScheme(nsACString& result)
49 result = mScheme;
50 return NS_OK;
53 nsresult SchemeIs(const char* scheme, bool* result)
55 NS_PRECONDITION(scheme, "scheme is null");
56 NS_PRECONDITION(result, "result is null");
58 *result = mScheme.Equals(scheme);
59 return NS_OK;
62 nsresult GetRef(nsACString& result)
64 result = mRef;
65 return NS_OK;
68 already_AddRefed<nsIURI> ToIURI()
70 MOZ_ASSERT(NS_IsMainThread(),
71 "Convert to nsIURI on main thread only; it is not threadsafe.");
72 nsCOMPtr<nsIURI> newURI;
73 NS_NewURI(getter_AddRefs(newURI), mSpec);
74 return newURI.forget();
77 private:
78 // Since this is a basic storage class, no duplication of spec parsing is
79 // included in the functionality. Instead, the class depends upon the
80 // parsing implementation in the nsIURI class used in object construction.
81 // This means each field is stored separately, but since only a few are
82 // required, this small memory tradeoff for threadsafe usage should be ok.
83 nsAutoCString mSpec;
84 nsAutoCString mScheme;
85 nsAutoCString mRef;
87 ~ImageURL() { }
90 } // namespace image
91 } // namespace mozilla
93 #endif // MOZILLA_IMAGELIB_IMAGEURL_H_