Bug 891986 - Keep the source ArrayBuffer to a decodeAudioData call alive until the...
[gecko.git] / dom / base / URL.cpp
blobabd7d1cd46c9ccae0f047a76f4556926f75f6a31
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 #include "URL.h"
8 #include "nsGlobalWindow.h"
9 #include "nsIDOMFile.h"
10 #include "DOMMediaStream.h"
11 #include "mozilla/dom/MediaSource.h"
12 #include "nsIDocument.h"
13 #include "nsIPrincipal.h"
14 #include "nsContentUtils.h"
15 #include "nsHostObjectProtocolHandler.h"
17 namespace mozilla {
18 namespace dom {
20 void
21 URL::CreateObjectURL(const GlobalObject& aGlobal, nsIDOMBlob* aBlob,
22 const objectURLOptions& aOptions,
23 nsString& aResult,
24 ErrorResult& aError)
26 CreateObjectURLInternal(aGlobal.Get(), aBlob,
27 NS_LITERAL_CSTRING(BLOBURI_SCHEME), aOptions, aResult,
28 aError);
31 void
32 URL::CreateObjectURL(const GlobalObject& aGlobal, DOMMediaStream& aStream,
33 const mozilla::dom::objectURLOptions& aOptions,
34 nsString& aResult,
35 ErrorResult& aError)
37 CreateObjectURLInternal(aGlobal.Get(), &aStream,
38 NS_LITERAL_CSTRING(MEDIASTREAMURI_SCHEME), aOptions,
39 aResult, aError);
42 void
43 URL::CreateObjectURL(const GlobalObject& aGlobal, MediaSource& aSource,
44 const objectURLOptions& aOptions,
45 nsString& aResult,
46 ErrorResult& aError)
48 CreateObjectURLInternal(aGlobal.Get(), &aSource,
49 NS_LITERAL_CSTRING(MEDIASOURCEURI_SCHEME), aOptions,
50 aResult, aError);
53 void
54 URL::CreateObjectURLInternal(nsISupports* aGlobal, nsISupports* aObject,
55 const nsACString& aScheme,
56 const mozilla::dom::objectURLOptions& aOptions,
57 nsString& aResult,
58 ErrorResult& aError)
60 nsCOMPtr<nsPIDOMWindow> w = do_QueryInterface(aGlobal);
61 nsGlobalWindow* window = static_cast<nsGlobalWindow*>(w.get());
62 NS_PRECONDITION(!window || window->IsInnerWindow(),
63 "Should be inner window");
65 if (!window || !window->GetExtantDoc()) {
66 aError.Throw(NS_ERROR_INVALID_POINTER);
67 return;
70 nsIDocument* doc = window->GetExtantDoc();
72 nsCString url;
73 nsresult rv = nsHostObjectProtocolHandler::AddDataEntry(aScheme, aObject,
74 doc->NodePrincipal(), url);
75 if (NS_FAILED(rv)) {
76 aError.Throw(rv);
77 return;
80 doc->RegisterHostObjectUri(url);
81 CopyASCIItoUTF16(url, aResult);
84 void
85 URL::RevokeObjectURL(const GlobalObject& aGlobal, const nsAString& aURL)
87 nsCOMPtr<nsPIDOMWindow> w = do_QueryInterface(aGlobal.Get());
88 nsGlobalWindow* window = static_cast<nsGlobalWindow*>(w.get());
89 NS_PRECONDITION(!window || window->IsInnerWindow(),
90 "Should be inner window");
91 if (!window)
92 return;
94 NS_LossyConvertUTF16toASCII asciiurl(aURL);
96 nsIPrincipal* winPrincipal = window->GetPrincipal();
97 if (!winPrincipal) {
98 return;
101 nsIPrincipal* principal =
102 nsHostObjectProtocolHandler::GetDataEntryPrincipal(asciiurl);
103 bool subsumes;
104 if (principal && winPrincipal &&
105 NS_SUCCEEDED(winPrincipal->Subsumes(principal, &subsumes)) &&
106 subsumes) {
107 if (window->GetExtantDoc()) {
108 window->GetExtantDoc()->UnregisterHostObjectUri(asciiurl);
110 nsHostObjectProtocolHandler::RemoveDataEntry(asciiurl);