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/. */
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"
21 URL::CreateObjectURL(const GlobalObject
& aGlobal
, nsIDOMBlob
* aBlob
,
22 const objectURLOptions
& aOptions
,
26 CreateObjectURLInternal(aGlobal
.Get(), aBlob
,
27 NS_LITERAL_CSTRING(BLOBURI_SCHEME
), aOptions
, aResult
,
32 URL::CreateObjectURL(const GlobalObject
& aGlobal
, DOMMediaStream
& aStream
,
33 const mozilla::dom::objectURLOptions
& aOptions
,
37 CreateObjectURLInternal(aGlobal
.Get(), &aStream
,
38 NS_LITERAL_CSTRING(MEDIASTREAMURI_SCHEME
), aOptions
,
43 URL::CreateObjectURL(const GlobalObject
& aGlobal
, MediaSource
& aSource
,
44 const objectURLOptions
& aOptions
,
48 CreateObjectURLInternal(aGlobal
.Get(), &aSource
,
49 NS_LITERAL_CSTRING(MEDIASOURCEURI_SCHEME
), aOptions
,
54 URL::CreateObjectURLInternal(nsISupports
* aGlobal
, nsISupports
* aObject
,
55 const nsACString
& aScheme
,
56 const mozilla::dom::objectURLOptions
& aOptions
,
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
);
70 nsIDocument
* doc
= window
->GetExtantDoc();
73 nsresult rv
= nsHostObjectProtocolHandler::AddDataEntry(aScheme
, aObject
,
74 doc
->NodePrincipal(), url
);
80 doc
->RegisterHostObjectUri(url
);
81 CopyASCIItoUTF16(url
, aResult
);
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");
94 NS_LossyConvertUTF16toASCII
asciiurl(aURL
);
96 nsIPrincipal
* winPrincipal
= window
->GetPrincipal();
101 nsIPrincipal
* principal
=
102 nsHostObjectProtocolHandler::GetDataEntryPrincipal(asciiurl
);
104 if (principal
&& winPrincipal
&&
105 NS_SUCCEEDED(winPrincipal
->Subsumes(principal
, &subsumes
)) &&
107 if (window
->GetExtantDoc()) {
108 window
->GetExtantDoc()->UnregisterHostObjectUri(asciiurl
);
110 nsHostObjectProtocolHandler::RemoveDataEntry(asciiurl
);