image/src/ImageURL.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/image/src/ImageURL.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,93 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef MOZILLA_IMAGELIB_IMAGEURL_H_
    1.10 +#define MOZILLA_IMAGELIB_IMAGEURL_H_
    1.11 +
    1.12 +#include "nsIURI.h"
    1.13 +#include "MainThreadUtils.h"
    1.14 +#include "nsNetUtil.h"
    1.15 +
    1.16 +namespace mozilla {
    1.17 +namespace image {
    1.18 +
    1.19 +/** ImageURL
    1.20 + *
    1.21 + * nsStandardURL is not threadsafe, so this class is created to hold only the
    1.22 + * necessary URL data required for image loading and decoding.
    1.23 + *
    1.24 + * Note: Although several APIs have the same or similar prototypes as those
    1.25 + * found in nsIURI/nsStandardURL, the class does not implement nsIURI. This is
    1.26 + * intentional; functionality is limited, and is only useful for imagelib code.
    1.27 + * By not implementing nsIURI, external code cannot unintentionally be given an
    1.28 + * nsIURI pointer with this limited class behind it; instead, conversion to a
    1.29 + * fully implemented nsIURI is required (e.g. through NS_NewURI).
    1.30 + */
    1.31 +class ImageURL
    1.32 +{
    1.33 +public:
    1.34 +  explicit ImageURL(nsIURI* aURI) {
    1.35 +    MOZ_ASSERT(NS_IsMainThread(), "Cannot use nsIURI off main thread!");
    1.36 +    aURI->GetSpec(mSpec);
    1.37 +    aURI->GetScheme(mScheme);
    1.38 +    aURI->GetRef(mRef);
    1.39 +  }
    1.40 +
    1.41 +  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(mozilla::image::ImageURL)
    1.42 +
    1.43 +  nsresult GetSpec(nsACString &result) {
    1.44 +    result = mSpec;
    1.45 +    return NS_OK;
    1.46 +  }
    1.47 +
    1.48 +  nsresult GetScheme(nsACString &result) {
    1.49 +    result = mScheme;
    1.50 +    return NS_OK;
    1.51 +  }
    1.52 +
    1.53 +  nsresult SchemeIs(const char *scheme, bool *result)
    1.54 +  {
    1.55 +    NS_PRECONDITION(scheme, "scheme is null");
    1.56 +    NS_PRECONDITION(result, "result is null");
    1.57 +
    1.58 +    *result = mScheme.Equals(scheme);
    1.59 +    return NS_OK;
    1.60 +  }
    1.61 +
    1.62 +  nsresult GetRef(nsACString &result) {
    1.63 +    result = mRef;
    1.64 +    return NS_OK;
    1.65 +  }
    1.66 +
    1.67 +  already_AddRefed<nsIURI> ToIURI() {
    1.68 +    MOZ_ASSERT(NS_IsMainThread(),
    1.69 +               "Convert to nsIURI on main thread only; it is not threadsafe.");
    1.70 +    nsCOMPtr<nsIURI> newURI;
    1.71 +    NS_NewURI(getter_AddRefs(newURI), mSpec);
    1.72 +    return newURI.forget();
    1.73 +  }
    1.74 +
    1.75 +  nsresult Equals(nsIURI *otherURI, bool *result) {
    1.76 +    nsAutoCString otherSpec;    
    1.77 +    otherURI->GetSpec(otherSpec);
    1.78 +    *result = mSpec.Equals(otherSpec);
    1.79 +    return NS_OK;
    1.80 +  }
    1.81 +
    1.82 +private:
    1.83 +  // Since this is a basic storage class, no duplication of spec parsing is
    1.84 +  // included in the functionality. Instead, the class depends upon the
    1.85 +  // parsing implementation in the nsIURI class used in object construction.
    1.86 +  // This means each field is stored separately, but since only a few are
    1.87 +  // required, this small memory tradeoff for threadsafe usage should be ok.
    1.88 +  nsAutoCString mSpec;
    1.89 +  nsAutoCString mScheme;
    1.90 +  nsAutoCString mRef;
    1.91 +};
    1.92 +
    1.93 +} // namespace image
    1.94 +} // namespace mozilla
    1.95 +
    1.96 +#endif // MOZILLA_IMAGELIB_IMAGEURL_H_

mercurial