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/. */
6 
7 #include "URLMainThread.h"
8 
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 
17 namespace mozilla {
18 namespace dom {
19 
20 /* static */
CreateObjectURL(const GlobalObject & aGlobal,Blob & aBlob,nsAString & aResult,ErrorResult & aRv)21 void URLMainThread::CreateObjectURL(const GlobalObject& aGlobal, Blob& aBlob,
22                                     nsAString& aResult, ErrorResult& aRv) {
23   MOZ_ASSERT(NS_IsMainThread());
24 
25   nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
26   if (NS_WARN_IF(!global)) {
27     aRv.Throw(NS_ERROR_FAILURE);
28     return;
29   }
30 
31   nsCOMPtr<nsIPrincipal> principal =
32       nsContentUtils::ObjectPrincipal(aGlobal.Get());
33 
34   nsAutoCString url;
35   aRv = BlobURLProtocolHandler::AddDataEntry(aBlob.Impl(), principal,
36                                              global->GetAgentClusterId(), url);
37   if (NS_WARN_IF(aRv.Failed())) {
38     return;
39   }
40 
41   global->RegisterHostObjectURI(url);
42   CopyASCIItoUTF16(url, aResult);
43 }
44 
45 /* static */
CreateObjectURL(const GlobalObject & aGlobal,MediaSource & aSource,nsAString & aResult,ErrorResult & aRv)46 void URLMainThread::CreateObjectURL(const GlobalObject& aGlobal,
47                                     MediaSource& aSource, nsAString& aResult,
48                                     ErrorResult& aRv) {
49   MOZ_ASSERT(NS_IsMainThread());
50 
51   nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
52   if (NS_WARN_IF(!global)) {
53     aRv.Throw(NS_ERROR_FAILURE);
54     return;
55   }
56 
57   nsCOMPtr<nsIPrincipal> principal =
58       nsContentUtils::ObjectPrincipal(aGlobal.Get());
59 
60   nsAutoCString url;
61   aRv = BlobURLProtocolHandler::AddDataEntry(&aSource, principal,
62                                              global->GetAgentClusterId(), url);
63   if (NS_WARN_IF(aRv.Failed())) {
64     return;
65   }
66 
67   nsCOMPtr<nsIRunnable> revocation = NS_NewRunnableFunction(
68       "dom::URLMainThread::CreateObjectURL",
69       [url] { BlobURLProtocolHandler::RemoveDataEntry(url); });
70 
71   nsContentUtils::RunInStableState(revocation.forget());
72 
73   CopyASCIItoUTF16(url, aResult);
74 }
75 
76 /* static */
RevokeObjectURL(const GlobalObject & aGlobal,const nsAString & aURL,ErrorResult & aRv)77 void URLMainThread::RevokeObjectURL(const GlobalObject& aGlobal,
78                                     const nsAString& aURL, ErrorResult& aRv) {
79   MOZ_ASSERT(NS_IsMainThread());
80   nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
81   if (!global) {
82     aRv.Throw(NS_ERROR_FAILURE);
83     return;
84   }
85 
86   NS_LossyConvertUTF16toASCII asciiurl(aURL);
87 
88   if (BlobURLProtocolHandler::RemoveDataEntry(
89           asciiurl, nsContentUtils::ObjectPrincipal(aGlobal.Get()),
90           global->GetAgentClusterId())) {
91     global->UnregisterHostObjectURI(asciiurl);
92   }
93 }
94 
95 /* static */
IsValidURL(const GlobalObject & aGlobal,const nsAString & aURL,ErrorResult & aRv)96 bool URLMainThread::IsValidURL(const GlobalObject& aGlobal,
97                                const nsAString& aURL, ErrorResult& aRv) {
98   MOZ_ASSERT(NS_IsMainThread());
99   NS_LossyConvertUTF16toASCII asciiurl(aURL);
100   return BlobURLProtocolHandler::HasDataEntry(asciiurl);
101 }
102 
103 }  // namespace dom
104 }  // namespace mozilla
105