2013-09-28 22:28:42 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2015-05-15 06:52:05 +03:00
|
|
|
#ifndef mozilla_image_ImageURL_h
|
|
|
|
#define mozilla_image_ImageURL_h
|
2013-09-28 22:28:42 +04:00
|
|
|
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "MainThreadUtils.h"
|
|
|
|
#include "nsNetUtil.h"
|
2016-11-25 15:28:27 +03:00
|
|
|
#include "mozilla/HashFunctions.h"
|
|
|
|
#include "nsHashKeys.h"
|
2016-11-28 16:40:42 +03:00
|
|
|
#include "nsProxyRelease.h"
|
2013-09-28 22:28:42 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace image {
|
|
|
|
|
2016-11-25 15:28:27 +03:00
|
|
|
class ImageCacheKey;
|
|
|
|
|
2013-09-28 22:28:42 +04:00
|
|
|
/** ImageURL
|
|
|
|
*
|
|
|
|
* nsStandardURL is not threadsafe, so this class is created to hold only the
|
|
|
|
* necessary URL data required for image loading and decoding.
|
|
|
|
*
|
|
|
|
* Note: Although several APIs have the same or similar prototypes as those
|
|
|
|
* found in nsIURI/nsStandardURL, the class does not implement nsIURI. This is
|
|
|
|
* intentional; functionality is limited, and is only useful for imagelib code.
|
|
|
|
* By not implementing nsIURI, external code cannot unintentionally be given an
|
|
|
|
* nsIURI pointer with this limited class behind it; instead, conversion to a
|
|
|
|
* fully implemented nsIURI is required (e.g. through NS_NewURI).
|
|
|
|
*/
|
|
|
|
class ImageURL
|
|
|
|
{
|
|
|
|
public:
|
2016-08-29 08:34:32 +03:00
|
|
|
explicit ImageURL(nsIURI* aURI, nsresult& aRv)
|
2016-11-28 16:40:42 +03:00
|
|
|
: mURI(new nsMainThreadPtrHolder<nsIURI>(aURI))
|
2014-12-04 22:43:00 +03:00
|
|
|
{
|
2013-09-28 22:28:42 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Cannot use nsIURI off main thread!");
|
2016-08-29 08:34:32 +03:00
|
|
|
|
|
|
|
aRv = aURI->GetSpec(mSpec);
|
|
|
|
NS_ENSURE_SUCCESS_VOID(aRv);
|
|
|
|
|
|
|
|
aRv = aURI->GetScheme(mScheme);
|
|
|
|
NS_ENSURE_SUCCESS_VOID(aRv);
|
|
|
|
|
|
|
|
aRv = aURI->GetRef(mRef);
|
|
|
|
NS_ENSURE_SUCCESS_VOID(aRv);
|
2013-09-28 22:28:42 +04:00
|
|
|
}
|
|
|
|
|
2014-06-19 06:29:00 +04:00
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ImageURL)
|
2013-09-28 22:28:42 +04:00
|
|
|
|
2014-12-04 22:43:00 +03:00
|
|
|
nsresult GetSpec(nsACString& result)
|
|
|
|
{
|
2013-09-28 22:28:42 +04:00
|
|
|
result = mSpec;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-05-20 20:21:11 +03:00
|
|
|
/// A weak pointer to the URI spec for this ImageURL. For logging only.
|
|
|
|
const char* Spec() const { return mSpec.get(); }
|
|
|
|
|
2015-05-21 23:23:42 +03:00
|
|
|
enum TruncatedSpecStatus {
|
|
|
|
FitsInto1k,
|
|
|
|
TruncatedTo1k
|
|
|
|
};
|
|
|
|
TruncatedSpecStatus GetSpecTruncatedTo1k(nsACString& result)
|
|
|
|
{
|
|
|
|
static const size_t sMaxTruncatedLength = 1024;
|
|
|
|
|
|
|
|
if (sMaxTruncatedLength >= mSpec.Length()) {
|
|
|
|
result = mSpec;
|
|
|
|
return FitsInto1k;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = Substring(mSpec, 0, sMaxTruncatedLength);
|
|
|
|
return TruncatedTo1k;
|
|
|
|
}
|
|
|
|
|
2014-12-04 22:43:00 +03:00
|
|
|
nsresult GetScheme(nsACString& result)
|
|
|
|
{
|
2013-09-28 22:28:42 +04:00
|
|
|
result = mScheme;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-12-04 22:43:00 +03:00
|
|
|
nsresult SchemeIs(const char* scheme, bool* result)
|
2013-09-28 22:28:42 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(scheme, "scheme is null");
|
|
|
|
NS_PRECONDITION(result, "result is null");
|
|
|
|
|
|
|
|
*result = mScheme.Equals(scheme);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-12-04 22:43:00 +03:00
|
|
|
nsresult GetRef(nsACString& result)
|
|
|
|
{
|
2013-09-28 22:28:42 +04:00
|
|
|
result = mRef;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-12-04 22:43:00 +03:00
|
|
|
already_AddRefed<nsIURI> ToIURI()
|
|
|
|
{
|
2016-11-28 16:40:42 +03:00
|
|
|
nsCOMPtr<nsIURI> newURI = mURI.get();
|
2013-09-28 22:28:42 +04:00
|
|
|
return newURI.forget();
|
|
|
|
}
|
|
|
|
|
2015-05-20 20:21:11 +03:00
|
|
|
bool operator==(const ImageURL& aOther) const
|
|
|
|
{
|
|
|
|
// Note that we don't need to consider mScheme and mRef, because they're
|
|
|
|
// already represented in mSpec.
|
|
|
|
return mSpec == aOther.mSpec;
|
|
|
|
}
|
|
|
|
|
2015-06-05 11:52:06 +03:00
|
|
|
bool HasSameRef(const ImageURL& aOther) const
|
|
|
|
{
|
|
|
|
return mRef == aOther.mRef;
|
|
|
|
}
|
|
|
|
|
2013-09-28 22:28:42 +04:00
|
|
|
private:
|
2016-11-25 15:28:27 +03:00
|
|
|
friend class ImageCacheKey;
|
|
|
|
|
|
|
|
uint32_t ComputeHash(const Maybe<uint64_t>& aBlobSerial) const
|
|
|
|
{
|
|
|
|
if (aBlobSerial) {
|
|
|
|
// For blob URIs, we hash the serial number of the underlying blob, so that
|
|
|
|
// different blob URIs which point to the same blob share a cache entry. We
|
|
|
|
// also include the ref portion of the URI to support media fragments which
|
|
|
|
// requires us to create different Image objects even if the source data is
|
|
|
|
// the same.
|
|
|
|
return HashGeneric(*aBlobSerial, HashString(mRef));
|
|
|
|
}
|
|
|
|
// For non-blob URIs, we hash the URI spec.
|
|
|
|
return HashString(mSpec);
|
|
|
|
}
|
|
|
|
|
2016-11-28 16:40:42 +03:00
|
|
|
nsMainThreadPtrHandle<nsIURI> mURI;
|
|
|
|
|
2013-09-28 22:28:42 +04:00
|
|
|
// Since this is a basic storage class, no duplication of spec parsing is
|
|
|
|
// included in the functionality. Instead, the class depends upon the
|
|
|
|
// parsing implementation in the nsIURI class used in object construction.
|
|
|
|
// This means each field is stored separately, but since only a few are
|
|
|
|
// required, this small memory tradeoff for threadsafe usage should be ok.
|
|
|
|
nsAutoCString mSpec;
|
|
|
|
nsAutoCString mScheme;
|
|
|
|
nsAutoCString mRef;
|
2014-06-19 06:29:00 +04:00
|
|
|
|
2014-12-04 22:43:00 +03:00
|
|
|
~ImageURL() { }
|
2013-09-28 22:28:42 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace image
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2015-05-15 06:52:05 +03:00
|
|
|
#endif // mozilla_image_ImageURL_h
|