2013-10-24 23:05:08 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-09-25 07:24:44 +04:00
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "nsHostObjectProtocolHandler.h"
|
2015-08-20 01:10:28 +03:00
|
|
|
|
2014-10-03 12:18:00 +04:00
|
|
|
#include "DOMMediaStream.h"
|
2014-10-08 20:15:23 +04:00
|
|
|
#include "mozilla/dom/File.h"
|
2015-08-20 01:10:28 +03:00
|
|
|
#include "mozilla/dom/MediaSource.h"
|
2014-07-10 10:56:37 +04:00
|
|
|
#include "mozilla/LoadInfo.h"
|
2015-08-20 01:10:28 +03:00
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
#include "nsClassHashtable.h"
|
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsHostObjectURI.h"
|
|
|
|
#include "nsIMemoryReporter.h"
|
|
|
|
#include "nsIPrincipal.h"
|
|
|
|
#include "nsIUUIDGenerator.h"
|
|
|
|
#include "nsNetUtil.h"
|
2012-09-25 07:24:44 +04:00
|
|
|
|
2015-05-12 15:11:03 +03:00
|
|
|
using mozilla::dom::BlobImpl;
|
2014-10-08 20:15:22 +04:00
|
|
|
using mozilla::ErrorResult;
|
2014-07-10 10:56:37 +04:00
|
|
|
using mozilla::LoadInfo;
|
2014-06-27 07:46:34 +04:00
|
|
|
|
2012-09-25 07:24:44 +04:00
|
|
|
// -----------------------------------------------------------------------
|
|
|
|
// Hash table
|
|
|
|
struct DataInfo
|
|
|
|
{
|
2014-10-03 12:18:00 +04:00
|
|
|
// mObject is expected to be an nsIDOMBlob, DOMMediaStream, or MediaSource
|
2012-09-25 07:24:44 +04:00
|
|
|
nsCOMPtr<nsISupports> mObject;
|
|
|
|
nsCOMPtr<nsIPrincipal> mPrincipal;
|
2014-01-16 17:22:26 +04:00
|
|
|
nsCString mStack;
|
2012-09-25 07:24:44 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
static nsClassHashtable<nsCStringHashKey, DataInfo>* gDataTable;
|
|
|
|
|
2013-10-24 23:05:08 +04:00
|
|
|
// Memory reporting for the hash table.
|
|
|
|
namespace mozilla {
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class HostObjectURLsReporter final : public nsIMemoryReporter
|
2013-10-24 23:05:08 +04:00
|
|
|
{
|
2014-06-25 06:09:15 +04:00
|
|
|
~HostObjectURLsReporter() {}
|
|
|
|
|
2013-10-24 23:05:08 +04:00
|
|
|
public:
|
2013-12-08 10:09:10 +04:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
NS_IMETHOD CollectReports(nsIHandleReportCallback* aHandleReport,
|
2015-03-21 19:28:04 +03:00
|
|
|
nsISupports* aData, bool aAnonymize) override
|
2013-10-24 23:05:08 +04:00
|
|
|
{
|
2013-12-08 10:09:10 +04:00
|
|
|
return MOZ_COLLECT_REPORT(
|
|
|
|
"host-object-urls", KIND_OTHER, UNITS_COUNT,
|
|
|
|
gDataTable ? gDataTable->Count() : 0,
|
|
|
|
"The number of host objects stored for access via URLs "
|
|
|
|
"(e.g. blobs passed to URL.createObjectURL).");
|
2013-10-24 23:05:08 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(HostObjectURLsReporter, nsIMemoryReporter)
|
2013-12-08 10:09:10 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class BlobURLsReporter final : public nsIMemoryReporter
|
2014-01-16 17:22:26 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
NS_IMETHOD CollectReports(nsIHandleReportCallback* aCallback,
|
2015-03-21 19:28:04 +03:00
|
|
|
nsISupports* aData, bool aAnonymize) override
|
2014-01-16 17:22:26 +04:00
|
|
|
{
|
|
|
|
EnumArg env;
|
|
|
|
env.mCallback = aCallback;
|
|
|
|
env.mData = aData;
|
2014-05-21 10:06:54 +04:00
|
|
|
env.mAnonymize = aAnonymize;
|
2014-01-16 17:22:26 +04:00
|
|
|
|
|
|
|
if (gDataTable) {
|
|
|
|
gDataTable->EnumerateRead(CountCallback, &env);
|
|
|
|
gDataTable->EnumerateRead(ReportCallback, &env);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize info->mStack to record JS stack info, if enabled.
|
|
|
|
// The string generated here is used in ReportCallback, below.
|
|
|
|
static void GetJSStackForBlob(DataInfo* aInfo)
|
|
|
|
{
|
|
|
|
nsCString& stack = aInfo->mStack;
|
|
|
|
MOZ_ASSERT(stack.IsEmpty());
|
|
|
|
const uint32_t maxFrames = Preferences::GetUint("memory.blob_report.stack_frames");
|
|
|
|
|
|
|
|
if (maxFrames == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsIXPConnect* xpc = nsContentUtils::XPConnect();
|
|
|
|
nsCOMPtr<nsIStackFrame> frame;
|
|
|
|
rv = xpc->GetCurrentJSStack(getter_AddRefs(frame));
|
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
|
|
|
|
|
|
|
nsAutoCString origin;
|
|
|
|
nsCOMPtr<nsIURI> principalURI;
|
|
|
|
if (NS_SUCCEEDED(aInfo->mPrincipal->GetURI(getter_AddRefs(principalURI)))
|
|
|
|
&& principalURI) {
|
|
|
|
principalURI->GetPrePath(origin);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < maxFrames && frame; ++i) {
|
2014-04-12 06:20:40 +04:00
|
|
|
nsString fileNameUTF16;
|
2014-01-16 17:22:26 +04:00
|
|
|
int32_t lineNumber = 0;
|
|
|
|
|
2014-04-12 06:20:40 +04:00
|
|
|
frame->GetFilename(fileNameUTF16);
|
2014-01-16 17:22:26 +04:00
|
|
|
frame->GetLineNumber(&lineNumber);
|
|
|
|
|
2014-04-12 06:20:40 +04:00
|
|
|
if (!fileNameUTF16.IsEmpty()) {
|
|
|
|
NS_ConvertUTF16toUTF8 fileName(fileNameUTF16);
|
2014-01-16 17:22:26 +04:00
|
|
|
stack += "js(";
|
|
|
|
if (!origin.IsEmpty()) {
|
|
|
|
// Make the file name root-relative for conciseness if possible.
|
|
|
|
const char* originData;
|
|
|
|
uint32_t originLen;
|
|
|
|
|
|
|
|
originLen = origin.GetData(&originData);
|
|
|
|
// If fileName starts with origin + "/", cut up to that "/".
|
2014-01-20 20:51:41 +04:00
|
|
|
if (fileName.Length() >= originLen + 1 &&
|
|
|
|
memcmp(fileName.get(), originData, originLen) == 0 &&
|
2014-01-16 17:22:26 +04:00
|
|
|
fileName[originLen] == '/') {
|
2014-01-20 20:51:41 +04:00
|
|
|
fileName.Cut(0, originLen);
|
2014-01-16 17:22:26 +04:00
|
|
|
}
|
|
|
|
}
|
2014-01-20 20:51:41 +04:00
|
|
|
fileName.ReplaceChar('/', '\\');
|
|
|
|
stack += fileName;
|
2014-01-16 17:22:26 +04:00
|
|
|
if (lineNumber > 0) {
|
|
|
|
stack += ", line=";
|
|
|
|
stack.AppendInt(lineNumber);
|
|
|
|
}
|
|
|
|
stack += ")/";
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = frame->GetCaller(getter_AddRefs(frame));
|
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-06-25 06:09:15 +04:00
|
|
|
~BlobURLsReporter() {}
|
|
|
|
|
2014-01-16 17:22:26 +04:00
|
|
|
struct EnumArg {
|
|
|
|
nsIHandleReportCallback* mCallback;
|
|
|
|
nsISupports* mData;
|
2014-05-21 10:06:54 +04:00
|
|
|
bool mAnonymize;
|
2014-01-16 17:22:26 +04:00
|
|
|
nsDataHashtable<nsPtrHashKey<nsIDOMBlob>, uint32_t> mRefCounts;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Determine number of URLs per blob, to handle the case where it's > 1.
|
|
|
|
static PLDHashOperator CountCallback(nsCStringHashKey::KeyType aKey,
|
|
|
|
DataInfo* aInfo,
|
|
|
|
void* aUserArg)
|
|
|
|
{
|
|
|
|
EnumArg* envp = static_cast<EnumArg*>(aUserArg);
|
|
|
|
nsCOMPtr<nsIDOMBlob> blob;
|
|
|
|
|
|
|
|
blob = do_QueryInterface(aInfo->mObject);
|
|
|
|
if (blob) {
|
|
|
|
envp->mRefCounts.Put(blob, envp->mRefCounts.Get(blob) + 1);
|
|
|
|
}
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2015-04-17 02:20:03 +03:00
|
|
|
static void BuildPath(nsAutoCString& path,
|
|
|
|
nsCStringHashKey::KeyType aKey,
|
|
|
|
DataInfo* aInfo,
|
|
|
|
bool anonymize)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> principalURI;
|
|
|
|
nsAutoCString url, owner;
|
|
|
|
if (NS_SUCCEEDED(aInfo->mPrincipal->GetURI(getter_AddRefs(principalURI))) &&
|
|
|
|
principalURI != nullptr &&
|
|
|
|
NS_SUCCEEDED(principalURI->GetSpec(owner)) &&
|
|
|
|
!owner.IsEmpty()) {
|
|
|
|
owner.ReplaceChar('/', '\\');
|
|
|
|
path += "owner(";
|
|
|
|
if (anonymize) {
|
|
|
|
path += "<anonymized>";
|
|
|
|
} else {
|
|
|
|
path += owner;
|
|
|
|
}
|
|
|
|
path += ")";
|
|
|
|
} else {
|
|
|
|
path += "owner unknown";
|
|
|
|
}
|
|
|
|
path += "/";
|
|
|
|
if (anonymize) {
|
|
|
|
path += "<anonymized-stack>";
|
|
|
|
} else {
|
|
|
|
path += aInfo->mStack;
|
|
|
|
}
|
|
|
|
url = aKey;
|
|
|
|
url.ReplaceChar('/', '\\');
|
|
|
|
if (anonymize) {
|
|
|
|
path += "<anonymized-url>";
|
|
|
|
} else {
|
|
|
|
path += url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-16 17:22:26 +04:00
|
|
|
static PLDHashOperator ReportCallback(nsCStringHashKey::KeyType aKey,
|
|
|
|
DataInfo* aInfo,
|
|
|
|
void* aUserArg)
|
|
|
|
{
|
|
|
|
EnumArg* envp = static_cast<EnumArg*>(aUserArg);
|
2015-05-19 17:36:37 +03:00
|
|
|
nsCOMPtr<nsIDOMBlob> tmp = do_QueryInterface(aInfo->mObject);
|
2015-07-01 16:56:00 +03:00
|
|
|
nsRefPtr<mozilla::dom::Blob> blob = static_cast<mozilla::dom::Blob*>(tmp.get());
|
2014-01-16 17:22:26 +04:00
|
|
|
|
|
|
|
if (blob) {
|
|
|
|
NS_NAMED_LITERAL_CSTRING
|
|
|
|
(desc, "A blob URL allocated with URL.createObjectURL; the referenced "
|
|
|
|
"blob cannot be freed until all URLs for it have been explicitly "
|
|
|
|
"invalidated with URL.revokeObjectURL.");
|
|
|
|
nsAutoCString path, url, owner, specialDesc;
|
|
|
|
nsCOMPtr<nsIURI> principalURI;
|
2014-03-03 23:35:00 +04:00
|
|
|
uint64_t size = 0;
|
2014-01-16 17:22:26 +04:00
|
|
|
uint32_t refCount = 1;
|
|
|
|
DebugOnly<bool> blobWasCounted;
|
|
|
|
|
|
|
|
blobWasCounted = envp->mRefCounts.Get(blob, &refCount);
|
|
|
|
MOZ_ASSERT(blobWasCounted);
|
|
|
|
MOZ_ASSERT(refCount > 0);
|
|
|
|
|
2014-03-03 23:35:00 +04:00
|
|
|
bool isMemoryFile = blob->IsMemoryFile();
|
|
|
|
|
|
|
|
if (isMemoryFile) {
|
2015-05-19 17:36:37 +03:00
|
|
|
ErrorResult rv;
|
|
|
|
size = blob->GetSize(rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
rv.SuppressException();
|
2014-03-03 23:35:00 +04:00
|
|
|
size = 0;
|
|
|
|
}
|
2014-01-16 17:22:26 +04:00
|
|
|
}
|
|
|
|
|
2014-03-03 23:35:00 +04:00
|
|
|
path = isMemoryFile ? "memory-blob-urls/" : "file-blob-urls/";
|
2015-04-17 02:20:03 +03:00
|
|
|
BuildPath(path, aKey, aInfo, envp->mAnonymize);
|
|
|
|
|
2014-01-16 17:22:26 +04:00
|
|
|
if (refCount > 1) {
|
|
|
|
nsAutoCString addrStr;
|
|
|
|
|
|
|
|
addrStr = "0x";
|
|
|
|
addrStr.AppendInt((uint64_t)(nsIDOMBlob*)blob, 16);
|
|
|
|
|
|
|
|
path += " ";
|
|
|
|
path.AppendInt(refCount);
|
|
|
|
path += "@";
|
|
|
|
path += addrStr;
|
|
|
|
|
|
|
|
specialDesc = desc;
|
|
|
|
specialDesc += "\n\nNOTE: This blob (address ";
|
|
|
|
specialDesc += addrStr;
|
|
|
|
specialDesc += ") has ";
|
|
|
|
specialDesc.AppendInt(refCount);
|
2014-03-03 23:35:00 +04:00
|
|
|
specialDesc += " URLs.";
|
|
|
|
if (isMemoryFile) {
|
|
|
|
specialDesc += " Its size is divided ";
|
|
|
|
specialDesc += refCount > 2 ? "among" : "between";
|
|
|
|
specialDesc += " them in this report.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsACString& descString = specialDesc.IsEmpty()
|
|
|
|
? static_cast<const nsACString&>(desc)
|
|
|
|
: static_cast<const nsACString&>(specialDesc);
|
|
|
|
if (isMemoryFile) {
|
|
|
|
envp->mCallback->Callback(EmptyCString(),
|
|
|
|
path,
|
|
|
|
KIND_OTHER,
|
|
|
|
UNITS_BYTES,
|
|
|
|
size / refCount,
|
|
|
|
descString,
|
|
|
|
envp->mData);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
envp->mCallback->Callback(EmptyCString(),
|
|
|
|
path,
|
|
|
|
KIND_OTHER,
|
|
|
|
UNITS_COUNT,
|
|
|
|
1,
|
|
|
|
descString,
|
|
|
|
envp->mData);
|
2014-01-16 17:22:26 +04:00
|
|
|
}
|
2015-04-17 02:20:03 +03:00
|
|
|
} else {
|
|
|
|
// Just report the path for the DOMMediaStream or MediaSource.
|
|
|
|
nsCOMPtr<mozilla::dom::MediaSource> ms(do_QueryInterface(aInfo->mObject));
|
|
|
|
nsAutoCString path;
|
|
|
|
path = ms ? "media-source-urls/" : "dom-media-stream-urls/";
|
|
|
|
BuildPath(path, aKey, aInfo, envp->mAnonymize);
|
|
|
|
|
|
|
|
NS_NAMED_LITERAL_CSTRING
|
|
|
|
(desc, "An object URL allocated with URL.createObjectURL; the referenced "
|
|
|
|
"data cannot be freed until all URLs for it have been explicitly "
|
|
|
|
"invalidated with URL.revokeObjectURL.");
|
|
|
|
|
|
|
|
envp->mCallback->Callback(EmptyCString(),
|
|
|
|
path,
|
|
|
|
KIND_OTHER,
|
|
|
|
UNITS_COUNT,
|
|
|
|
1,
|
|
|
|
desc,
|
|
|
|
envp->mData);
|
2014-01-16 17:22:26 +04:00
|
|
|
}
|
2015-04-17 02:20:03 +03:00
|
|
|
|
2014-01-16 17:22:26 +04:00
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(BlobURLsReporter, nsIMemoryReporter)
|
2014-01-16 17:22:26 +04:00
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace mozilla
|
2013-10-24 23:05:08 +04:00
|
|
|
|
2014-03-03 23:35:00 +04:00
|
|
|
void
|
|
|
|
nsHostObjectProtocolHandler::Init(void)
|
2013-10-24 23:05:08 +04:00
|
|
|
{
|
|
|
|
static bool initialized = false;
|
|
|
|
|
|
|
|
if (!initialized) {
|
|
|
|
initialized = true;
|
2013-11-07 09:35:30 +04:00
|
|
|
RegisterStrongMemoryReporter(new mozilla::HostObjectURLsReporter());
|
2014-01-16 17:22:26 +04:00
|
|
|
RegisterStrongMemoryReporter(new mozilla::BlobURLsReporter());
|
2013-10-24 23:05:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-03 23:35:00 +04:00
|
|
|
nsHostObjectProtocolHandler::nsHostObjectProtocolHandler()
|
|
|
|
{
|
|
|
|
Init();
|
|
|
|
}
|
2014-01-16 17:22:26 +04:00
|
|
|
|
2012-09-25 07:24:44 +04:00
|
|
|
nsresult
|
|
|
|
nsHostObjectProtocolHandler::AddDataEntry(const nsACString& aScheme,
|
|
|
|
nsISupports* aObject,
|
|
|
|
nsIPrincipal* aPrincipal,
|
|
|
|
nsACString& aUri)
|
|
|
|
{
|
2014-03-03 23:35:00 +04:00
|
|
|
Init();
|
|
|
|
|
2014-08-31 14:40:11 +04:00
|
|
|
nsresult rv = GenerateURIString(aScheme, aPrincipal, aUri);
|
2012-09-25 07:24:44 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!gDataTable) {
|
|
|
|
gDataTable = new nsClassHashtable<nsCStringHashKey, DataInfo>;
|
|
|
|
}
|
|
|
|
|
|
|
|
DataInfo* info = new DataInfo;
|
|
|
|
|
|
|
|
info->mObject = aObject;
|
|
|
|
info->mPrincipal = aPrincipal;
|
2014-01-16 17:22:26 +04:00
|
|
|
mozilla::BlobURLsReporter::GetJSStackForBlob(info);
|
2012-09-25 07:24:44 +04:00
|
|
|
|
|
|
|
gDataTable->Put(aUri, info);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHostObjectProtocolHandler::RemoveDataEntry(const nsACString& aUri)
|
|
|
|
{
|
|
|
|
if (gDataTable) {
|
2013-10-17 22:37:57 +04:00
|
|
|
nsCString uriIgnoringRef;
|
|
|
|
int32_t hashPos = aUri.FindChar('#');
|
|
|
|
if (hashPos < 0) {
|
|
|
|
uriIgnoringRef = aUri;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
uriIgnoringRef = StringHead(aUri, hashPos);
|
|
|
|
}
|
|
|
|
gDataTable->Remove(uriIgnoringRef);
|
2012-09-25 07:24:44 +04:00
|
|
|
if (gDataTable->Count() == 0) {
|
|
|
|
delete gDataTable;
|
|
|
|
gDataTable = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-10 16:25:12 +04:00
|
|
|
nsresult
|
|
|
|
nsHostObjectProtocolHandler::GenerateURIString(const nsACString &aScheme,
|
2014-08-31 14:40:11 +04:00
|
|
|
nsIPrincipal* aPrincipal,
|
2013-06-10 16:25:12 +04:00
|
|
|
nsACString& aUri)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIUUIDGenerator> uuidgen =
|
|
|
|
do_GetService("@mozilla.org/uuid-generator;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsID id;
|
|
|
|
rv = uuidgen->GenerateUUIDInPlace(&id);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
char chars[NSID_LENGTH];
|
|
|
|
id.ToProvidedString(chars);
|
|
|
|
|
2014-08-31 14:40:11 +04:00
|
|
|
aUri = aScheme;
|
|
|
|
aUri.Append(':');
|
|
|
|
|
|
|
|
if (aPrincipal) {
|
2015-05-07 21:20:33 +03:00
|
|
|
nsAutoCString origin;
|
|
|
|
rv = nsContentUtils::GetASCIIOrigin(aPrincipal, origin);
|
2014-08-31 14:40:11 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-05-07 21:20:33 +03:00
|
|
|
aUri.Append(origin);
|
2014-08-31 14:40:11 +04:00
|
|
|
aUri.Append('/');
|
|
|
|
}
|
|
|
|
|
2013-06-10 16:25:12 +04:00
|
|
|
aUri += Substring(chars + 1, chars + NSID_LENGTH - 2);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-10-17 22:37:57 +04:00
|
|
|
static DataInfo*
|
|
|
|
GetDataInfo(const nsACString& aUri)
|
|
|
|
{
|
|
|
|
if (!gDataTable) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
DataInfo* res;
|
|
|
|
nsCString uriIgnoringRef;
|
|
|
|
int32_t hashPos = aUri.FindChar('#');
|
|
|
|
if (hashPos < 0) {
|
|
|
|
uriIgnoringRef = aUri;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
uriIgnoringRef = StringHead(aUri, hashPos);
|
|
|
|
}
|
|
|
|
gDataTable->Get(uriIgnoringRef, &res);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2012-09-25 07:24:44 +04:00
|
|
|
nsIPrincipal*
|
|
|
|
nsHostObjectProtocolHandler::GetDataEntryPrincipal(const nsACString& aUri)
|
|
|
|
{
|
|
|
|
if (!gDataTable) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-10-17 22:37:57 +04:00
|
|
|
DataInfo* res = GetDataInfo(aUri);
|
|
|
|
|
2012-09-25 07:24:44 +04:00
|
|
|
if (!res) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res->mPrincipal;
|
|
|
|
}
|
|
|
|
|
2013-05-03 04:54:05 +04:00
|
|
|
void
|
|
|
|
nsHostObjectProtocolHandler::Traverse(const nsACString& aUri,
|
|
|
|
nsCycleCollectionTraversalCallback& aCallback)
|
|
|
|
{
|
|
|
|
if (!gDataTable) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DataInfo* res;
|
|
|
|
gDataTable->Get(aUri, &res);
|
|
|
|
if (!res) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(aCallback, "HostObjectProtocolHandler DataInfo.mObject");
|
|
|
|
aCallback.NoteXPCOMChild(res->mObject);
|
|
|
|
}
|
|
|
|
|
2015-05-21 04:49:50 +03:00
|
|
|
static nsISupports*
|
|
|
|
GetDataObjectForSpec(const nsACString& aSpec)
|
|
|
|
{
|
|
|
|
DataInfo* info = GetDataInfo(aSpec);
|
|
|
|
return info ? info->mObject : nullptr;
|
|
|
|
}
|
|
|
|
|
2012-09-25 07:24:44 +04:00
|
|
|
static nsISupports*
|
|
|
|
GetDataObject(nsIURI* aURI)
|
|
|
|
{
|
|
|
|
nsCString spec;
|
|
|
|
aURI->GetSpec(spec);
|
2015-05-21 04:49:50 +03:00
|
|
|
return GetDataObjectForSpec(spec);
|
2012-09-25 07:24:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------
|
|
|
|
// Protocol handler
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsHostObjectProtocolHandler, nsIProtocolHandler)
|
2012-09-25 07:24:44 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHostObjectProtocolHandler::GetDefaultPort(int32_t *result)
|
|
|
|
{
|
|
|
|
*result = -1;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHostObjectProtocolHandler::GetProtocolFlags(uint32_t *result)
|
|
|
|
{
|
|
|
|
*result = URI_NORELATIVE | URI_NOAUTH | URI_LOADABLE_BY_SUBSUMERS |
|
|
|
|
URI_IS_LOCAL_RESOURCE | URI_NON_PERSISTABLE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHostObjectProtocolHandler::NewURI(const nsACString& aSpec,
|
|
|
|
const char *aCharset,
|
|
|
|
nsIURI *aBaseURI,
|
|
|
|
nsIURI **aResult)
|
|
|
|
{
|
|
|
|
*aResult = nullptr;
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
DataInfo* info = GetDataInfo(aSpec);
|
|
|
|
|
|
|
|
nsRefPtr<nsHostObjectURI> uri =
|
|
|
|
new nsHostObjectURI(info ? info->mPrincipal.get() : nullptr);
|
|
|
|
|
|
|
|
rv = uri->SetSpec(aSpec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
NS_TryToSetImmutable(uri);
|
|
|
|
uri.forget(aResult);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-10-23 04:16:57 +04:00
|
|
|
nsHostObjectProtocolHandler::NewChannel2(nsIURI* uri,
|
2014-12-12 20:06:09 +03:00
|
|
|
nsILoadInfo* aLoadInfo,
|
2014-10-23 04:16:57 +04:00
|
|
|
nsIChannel** result)
|
2012-09-25 07:24:44 +04:00
|
|
|
{
|
|
|
|
*result = nullptr;
|
|
|
|
|
|
|
|
nsCString spec;
|
|
|
|
uri->GetSpec(spec);
|
|
|
|
|
|
|
|
DataInfo* info = GetDataInfo(spec);
|
|
|
|
|
|
|
|
if (!info) {
|
|
|
|
return NS_ERROR_DOM_BAD_URI;
|
|
|
|
}
|
2014-06-27 07:46:34 +04:00
|
|
|
|
2015-05-12 15:11:03 +03:00
|
|
|
nsCOMPtr<BlobImpl> blob = do_QueryInterface(info->mObject);
|
2015-01-12 00:34:31 +03:00
|
|
|
if (!blob) {
|
2012-09-25 07:24:44 +04:00
|
|
|
return NS_ERROR_DOM_BAD_URI;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURIWithPrincipal> uriPrinc = do_QueryInterface(uri);
|
|
|
|
nsCOMPtr<nsIPrincipal> principal;
|
|
|
|
uriPrinc->GetPrincipal(getter_AddRefs(principal));
|
|
|
|
NS_ASSERTION(info->mPrincipal == principal, "Wrong principal!");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-05-19 17:36:37 +03:00
|
|
|
ErrorResult rv;
|
2012-09-25 07:24:44 +04:00
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2015-05-19 17:36:37 +03:00
|
|
|
blob->GetInternalStream(getter_AddRefs(stream), rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
return rv.StealNSResult();
|
|
|
|
}
|
2012-09-25 07:24:44 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIChannel> channel;
|
2015-02-19 22:47:59 +03:00
|
|
|
rv = NS_NewInputStreamChannelInternal(getter_AddRefs(channel),
|
|
|
|
uri,
|
|
|
|
stream,
|
|
|
|
EmptyCString(), // aContentType
|
|
|
|
EmptyCString(), // aContentCharset
|
|
|
|
aLoadInfo);
|
2015-05-19 17:36:37 +03:00
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
return rv.StealNSResult();
|
|
|
|
}
|
2012-09-25 07:24:44 +04:00
|
|
|
|
2013-03-22 02:23:48 +04:00
|
|
|
nsString type;
|
2014-10-08 20:15:22 +04:00
|
|
|
blob->GetType(type);
|
2012-09-25 07:24:44 +04:00
|
|
|
|
2014-06-27 07:46:34 +04:00
|
|
|
if (blob->IsFile()) {
|
2013-09-27 09:22:37 +04:00
|
|
|
nsString filename;
|
2014-10-08 20:15:22 +04:00
|
|
|
blob->GetName(filename);
|
2013-09-27 09:22:37 +04:00
|
|
|
channel->SetContentDispositionFilename(filename);
|
2013-06-21 07:15:15 +04:00
|
|
|
}
|
2013-07-02 08:23:37 +04:00
|
|
|
|
2015-05-19 17:36:37 +03:00
|
|
|
uint64_t size = blob->GetSize(rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
return rv.StealNSResult();
|
2014-10-08 20:15:22 +04:00
|
|
|
}
|
2013-09-27 09:22:37 +04:00
|
|
|
|
2012-09-25 07:24:44 +04:00
|
|
|
channel->SetOriginalURI(uri);
|
|
|
|
channel->SetContentType(NS_ConvertUTF16toUTF8(type));
|
2013-09-27 09:22:37 +04:00
|
|
|
channel->SetContentLength(size);
|
2012-09-25 07:24:44 +04:00
|
|
|
|
|
|
|
channel.forget(result);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-23 04:16:57 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHostObjectProtocolHandler::NewChannel(nsIURI* uri, nsIChannel* *result)
|
|
|
|
{
|
|
|
|
return NewChannel2(uri, nullptr, result);
|
|
|
|
}
|
|
|
|
|
2012-09-25 07:24:44 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHostObjectProtocolHandler::AllowPort(int32_t port, const char *scheme,
|
|
|
|
bool *_retval)
|
|
|
|
{
|
|
|
|
// don't override anything.
|
|
|
|
*_retval = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsBlobProtocolHandler::GetScheme(nsACString &result)
|
|
|
|
{
|
|
|
|
result.AssignLiteral(BLOBURI_SCHEME);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-25 07:24:44 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMediaStreamProtocolHandler::GetScheme(nsACString &result)
|
|
|
|
{
|
|
|
|
result.AssignLiteral(MEDIASTREAMURI_SCHEME);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-06-21 07:15:15 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMediaSourceProtocolHandler::GetScheme(nsACString &result)
|
|
|
|
{
|
|
|
|
result.AssignLiteral(MEDIASOURCEURI_SCHEME);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-06-10 16:25:12 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFontTableProtocolHandler::GetScheme(nsACString &result)
|
|
|
|
{
|
|
|
|
result.AssignLiteral(FONTTABLEURI_SCHEME);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-25 07:24:44 +04:00
|
|
|
nsresult
|
2015-05-12 15:11:03 +03:00
|
|
|
NS_GetBlobForBlobURI(nsIURI* aURI, BlobImpl** aBlob)
|
2012-09-25 07:24:44 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(IsBlobURI(aURI), "Only call this with blob URIs");
|
|
|
|
|
2014-10-06 22:01:20 +04:00
|
|
|
*aBlob = nullptr;
|
2012-09-25 07:24:44 +04:00
|
|
|
|
2015-05-12 15:11:03 +03:00
|
|
|
nsCOMPtr<BlobImpl> blob = do_QueryInterface(GetDataObject(aURI));
|
2015-01-12 00:34:31 +03:00
|
|
|
if (!blob) {
|
2012-09-25 07:24:44 +04:00
|
|
|
return NS_ERROR_DOM_BAD_URI;
|
|
|
|
}
|
|
|
|
|
2014-10-06 22:01:20 +04:00
|
|
|
blob.forget(aBlob);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-05-21 04:49:50 +03:00
|
|
|
nsresult
|
|
|
|
NS_GetBlobForBlobURISpec(const nsACString& aSpec, BlobImpl** aBlob)
|
|
|
|
{
|
|
|
|
*aBlob = nullptr;
|
|
|
|
|
|
|
|
nsCOMPtr<BlobImpl> blob = do_QueryInterface(GetDataObjectForSpec(aSpec));
|
|
|
|
if (!blob) {
|
|
|
|
return NS_ERROR_DOM_BAD_URI;
|
|
|
|
}
|
|
|
|
|
|
|
|
blob.forget(aBlob);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-06 22:01:20 +04:00
|
|
|
nsresult
|
|
|
|
NS_GetStreamForBlobURI(nsIURI* aURI, nsIInputStream** aStream)
|
|
|
|
{
|
2015-05-12 15:11:03 +03:00
|
|
|
nsRefPtr<BlobImpl> blobImpl;
|
2015-05-19 17:36:37 +03:00
|
|
|
ErrorResult rv;
|
|
|
|
rv = NS_GetBlobForBlobURI(aURI, getter_AddRefs(blobImpl));
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
return rv.StealNSResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
blobImpl->GetInternalStream(aStream, rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
return rv.StealNSResult();
|
2014-10-06 22:01:20 +04:00
|
|
|
}
|
|
|
|
|
2015-05-19 17:36:37 +03:00
|
|
|
return NS_OK;
|
2012-09-25 07:24:44 +04:00
|
|
|
}
|
2012-09-25 07:24:44 +04:00
|
|
|
|
|
|
|
nsresult
|
2014-10-03 12:18:00 +04:00
|
|
|
NS_GetStreamForMediaStreamURI(nsIURI* aURI, mozilla::DOMMediaStream** aStream)
|
2012-09-25 07:24:44 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(IsMediaStreamURI(aURI), "Only call this with mediastream URIs");
|
|
|
|
|
2014-12-23 21:43:29 +03:00
|
|
|
nsISupports* dataObject = GetDataObject(aURI);
|
|
|
|
if (!dataObject) {
|
|
|
|
return NS_ERROR_DOM_BAD_URI;
|
|
|
|
}
|
|
|
|
|
2012-09-25 07:24:44 +04:00
|
|
|
*aStream = nullptr;
|
2014-12-23 21:43:29 +03:00
|
|
|
return CallQueryInterface(dataObject, aStream);
|
2012-09-25 07:24:44 +04:00
|
|
|
}
|
2013-06-21 07:15:15 +04:00
|
|
|
|
2013-06-10 16:25:12 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFontTableProtocolHandler::NewURI(const nsACString& aSpec,
|
|
|
|
const char *aCharset,
|
|
|
|
nsIURI *aBaseURI,
|
|
|
|
nsIURI **aResult)
|
|
|
|
{
|
|
|
|
nsRefPtr<nsIURI> uri;
|
|
|
|
|
|
|
|
// Either you got here via a ref or a fonttable: uri
|
|
|
|
if (aSpec.Length() && aSpec.CharAt(0) == '#') {
|
|
|
|
nsresult rv = aBaseURI->CloneIgnoringRef(getter_AddRefs(uri));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
uri->SetRef(aSpec);
|
|
|
|
} else {
|
|
|
|
// Relative URIs (other than #ref) are not meaningful within the
|
|
|
|
// fonttable: scheme.
|
|
|
|
// If aSpec is a relative URI -other- than a bare #ref,
|
|
|
|
// this will leave uri empty, and we'll return a failure code below.
|
|
|
|
uri = new nsSimpleURI();
|
|
|
|
uri->SetSpec(aSpec);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool schemeIs;
|
|
|
|
if (NS_FAILED(uri->SchemeIs(FONTTABLEURI_SCHEME, &schemeIs)) || !schemeIs) {
|
|
|
|
NS_WARNING("Non-fonttable spec in nsFontTableProtocolHander");
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
uri.forget(aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-06-21 07:15:15 +04:00
|
|
|
nsresult
|
|
|
|
NS_GetSourceForMediaSourceURI(nsIURI* aURI, mozilla::dom::MediaSource** aSource)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(IsMediaSourceURI(aURI), "Only call this with mediasource URIs");
|
|
|
|
|
|
|
|
*aSource = nullptr;
|
|
|
|
|
|
|
|
nsCOMPtr<mozilla::dom::MediaSource> source = do_QueryInterface(GetDataObject(aURI));
|
|
|
|
if (!source) {
|
|
|
|
return NS_ERROR_DOM_BAD_URI;
|
|
|
|
}
|
|
|
|
|
|
|
|
source.forget(aSource);
|
|
|
|
return NS_OK;
|
|
|
|
}
|