1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "URLMainThread.h"
9 #include "mozilla/dom/BindingUtils.h"
10 #include "mozilla/dom/Blob.h"
11 #include "mozilla/dom/BlobURLProtocolHandler.h"
12 #include "mozilla/Unused.h"
13 #include "nsContentUtils.h"
14 #include "nsNetUtil.h"
15 #include "nsThreadUtils.h"
16 #include "mozilla/dom/Document.h"
18 namespace mozilla::dom
{
21 void URLMainThread::CreateObjectURL(const GlobalObject
& aGlobal
, Blob
& aBlob
,
22 nsACString
& aResult
, ErrorResult
& aRv
) {
23 MOZ_ASSERT(NS_IsMainThread());
25 nsCOMPtr
<nsIGlobalObject
> global
= do_QueryInterface(aGlobal
.GetAsSupports());
26 if (NS_WARN_IF(!global
)) {
27 aRv
.Throw(NS_ERROR_FAILURE
);
32 if (nsCOMPtr
<nsPIDOMWindowInner
> owner
= do_QueryInterface(global
)) {
33 if (Document
* doc
= owner
->GetExtantDoc()) {
34 nsCOMPtr
<nsICookieJarSettings
> cookieJarSettings
=
35 doc
->CookieJarSettings();
37 cookieJarSettings
->GetPartitionKey(partKey
);
41 nsCOMPtr
<nsIPrincipal
> principal
=
42 nsContentUtils::ObjectPrincipal(aGlobal
.Get());
44 aRv
= BlobURLProtocolHandler::AddDataEntry(
45 aBlob
.Impl(), principal
, NS_ConvertUTF16toUTF8(partKey
), aResult
);
46 if (NS_WARN_IF(aRv
.Failed())) {
50 global
->RegisterHostObjectURI(aResult
);
54 void URLMainThread::CreateObjectURL(const GlobalObject
& aGlobal
,
55 MediaSource
& aSource
, nsACString
& aResult
,
57 MOZ_ASSERT(NS_IsMainThread());
59 nsCOMPtr
<nsIGlobalObject
> global
= do_QueryInterface(aGlobal
.GetAsSupports());
60 if (NS_WARN_IF(!global
)) {
61 aRv
.Throw(NS_ERROR_FAILURE
);
66 if (nsCOMPtr
<nsPIDOMWindowInner
> owner
= do_QueryInterface(global
)) {
67 if (Document
* doc
= owner
->GetExtantDoc()) {
68 nsCOMPtr
<nsICookieJarSettings
> cookieJarSettings
=
69 doc
->CookieJarSettings();
71 cookieJarSettings
->GetPartitionKey(partKey
);
75 nsCOMPtr
<nsIPrincipal
> principal
=
76 nsContentUtils::ObjectPrincipal(aGlobal
.Get());
78 aRv
= BlobURLProtocolHandler::AddDataEntry(
79 &aSource
, principal
, NS_ConvertUTF16toUTF8(partKey
), aResult
);
80 if (NS_WARN_IF(aRv
.Failed())) {
84 nsCOMPtr
<nsIRunnable
> revocation
= NS_NewRunnableFunction(
85 "dom::URLMainThread::CreateObjectURL", [result
= nsCString(aResult
)] {
86 BlobURLProtocolHandler::RemoveDataEntry(result
);
89 nsContentUtils::RunInStableState(revocation
.forget());
93 void URLMainThread::RevokeObjectURL(const GlobalObject
& aGlobal
,
94 const nsACString
& aURL
, ErrorResult
& aRv
) {
95 MOZ_ASSERT(NS_IsMainThread());
96 nsCOMPtr
<nsIGlobalObject
> global
= do_QueryInterface(aGlobal
.GetAsSupports());
98 aRv
.Throw(NS_ERROR_FAILURE
);
102 nsAutoString partKey
;
103 if (nsCOMPtr
<nsPIDOMWindowInner
> owner
= do_QueryInterface(global
)) {
104 if (Document
* doc
= owner
->GetExtantDoc()) {
105 nsCOMPtr
<nsICookieJarSettings
> cookieJarSettings
=
106 doc
->CookieJarSettings();
108 cookieJarSettings
->GetPartitionKey(partKey
);
112 if (BlobURLProtocolHandler::RemoveDataEntry(
113 aURL
, nsContentUtils::ObjectPrincipal(aGlobal
.Get()),
114 NS_ConvertUTF16toUTF8(partKey
))) {
115 global
->UnregisterHostObjectURI(aURL
);
120 bool URLMainThread::IsValidObjectURL(const GlobalObject
& aGlobal
,
121 const nsACString
& aURL
, ErrorResult
& aRv
) {
122 MOZ_ASSERT(NS_IsMainThread());
123 return BlobURLProtocolHandler::HasDataEntry(aURL
);
126 } // namespace mozilla::dom