|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */ |
|
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 #include "FileSystemPermissionRequest.h" |
|
7 |
|
8 #include "mozilla/dom/FileSystemBase.h" |
|
9 #include "mozilla/dom/FileSystemTaskBase.h" |
|
10 #include "mozilla/dom/FileSystemUtils.h" |
|
11 #include "mozilla/dom/TabChild.h" |
|
12 #include "nsIDocument.h" |
|
13 #include "nsPIDOMWindow.h" |
|
14 #include "nsString.h" |
|
15 |
|
16 namespace mozilla { |
|
17 namespace dom { |
|
18 |
|
19 NS_IMPL_ISUPPORTS(FileSystemPermissionRequest, nsIRunnable, nsIContentPermissionRequest) |
|
20 |
|
21 // static |
|
22 void |
|
23 FileSystemPermissionRequest::RequestForTask(FileSystemTaskBase* aTask) |
|
24 { |
|
25 MOZ_ASSERT(aTask, "aTask should not be null!"); |
|
26 MOZ_ASSERT(NS_IsMainThread()); |
|
27 nsRefPtr<FileSystemPermissionRequest> request = |
|
28 new FileSystemPermissionRequest(aTask); |
|
29 NS_DispatchToCurrentThread(request); |
|
30 } |
|
31 |
|
32 FileSystemPermissionRequest::FileSystemPermissionRequest( |
|
33 FileSystemTaskBase* aTask) |
|
34 : mTask(aTask) |
|
35 { |
|
36 MOZ_ASSERT(mTask, "aTask should not be null!"); |
|
37 MOZ_ASSERT(NS_IsMainThread()); |
|
38 |
|
39 mTask->GetPermissionAccessType(mPermissionAccess); |
|
40 |
|
41 nsRefPtr<FileSystemBase> filesystem = mTask->GetFileSystem(); |
|
42 if (!filesystem) { |
|
43 return; |
|
44 } |
|
45 |
|
46 mPermissionType = filesystem->GetPermission(); |
|
47 |
|
48 mWindow = filesystem->GetWindow(); |
|
49 if (!mWindow) { |
|
50 return; |
|
51 } |
|
52 |
|
53 nsCOMPtr<nsIDocument> doc = mWindow->GetDoc(); |
|
54 if (!doc) { |
|
55 return; |
|
56 } |
|
57 |
|
58 mPrincipal = doc->NodePrincipal(); |
|
59 } |
|
60 |
|
61 FileSystemPermissionRequest::~FileSystemPermissionRequest() |
|
62 { |
|
63 } |
|
64 |
|
65 bool |
|
66 FileSystemPermissionRequest::Recv__delete__(const bool& aAllow, |
|
67 const InfallibleTArray<PermissionChoice>& aChoices) |
|
68 { |
|
69 MOZ_ASSERT(aChoices.IsEmpty(), |
|
70 "FileSystemPermissionRequest doesn't support permission choice"); |
|
71 if (aAllow) { |
|
72 Allow(JS::UndefinedHandleValue); |
|
73 } else { |
|
74 Cancel(); |
|
75 } |
|
76 return true; |
|
77 } |
|
78 |
|
79 void |
|
80 FileSystemPermissionRequest::IPDLRelease() |
|
81 { |
|
82 Release(); |
|
83 } |
|
84 |
|
85 NS_IMETHODIMP |
|
86 FileSystemPermissionRequest::GetTypes(nsIArray** aTypes) |
|
87 { |
|
88 nsTArray<nsString> emptyOptions; |
|
89 return CreatePermissionArray(mPermissionType, |
|
90 mPermissionAccess, |
|
91 emptyOptions, |
|
92 aTypes); |
|
93 } |
|
94 |
|
95 NS_IMETHODIMP |
|
96 FileSystemPermissionRequest::GetPrincipal(nsIPrincipal** aRequestingPrincipal) |
|
97 { |
|
98 NS_IF_ADDREF(*aRequestingPrincipal = mPrincipal); |
|
99 return NS_OK; |
|
100 } |
|
101 |
|
102 NS_IMETHODIMP |
|
103 FileSystemPermissionRequest::GetWindow(nsIDOMWindow** aRequestingWindow) |
|
104 { |
|
105 NS_IF_ADDREF(*aRequestingWindow = mWindow); |
|
106 return NS_OK; |
|
107 } |
|
108 |
|
109 NS_IMETHODIMP |
|
110 FileSystemPermissionRequest::GetElement(nsIDOMElement** aRequestingElement) |
|
111 { |
|
112 *aRequestingElement = nullptr; |
|
113 return NS_OK; |
|
114 } |
|
115 |
|
116 NS_IMETHODIMP |
|
117 FileSystemPermissionRequest::Cancel() |
|
118 { |
|
119 MOZ_ASSERT(NS_IsMainThread()); |
|
120 mTask->SetError(NS_ERROR_DOM_SECURITY_ERR); |
|
121 mTask->Start(); |
|
122 return NS_OK; |
|
123 } |
|
124 |
|
125 NS_IMETHODIMP |
|
126 FileSystemPermissionRequest::Allow(JS::HandleValue aChoices) |
|
127 { |
|
128 MOZ_ASSERT(NS_IsMainThread()); |
|
129 MOZ_ASSERT(aChoices.isUndefined()); |
|
130 mTask->Start(); |
|
131 return NS_OK; |
|
132 } |
|
133 |
|
134 NS_IMETHODIMP |
|
135 FileSystemPermissionRequest::Run() |
|
136 { |
|
137 MOZ_ASSERT(NS_IsMainThread()); |
|
138 |
|
139 nsRefPtr<FileSystemBase> filesystem = mTask->GetFileSystem(); |
|
140 if (!filesystem) { |
|
141 Cancel(); |
|
142 return NS_OK; |
|
143 } |
|
144 |
|
145 if (filesystem->IsTesting()) { |
|
146 Allow(JS::UndefinedHandleValue); |
|
147 return NS_OK; |
|
148 } |
|
149 |
|
150 if (FileSystemUtils::IsParentProcess()) { |
|
151 nsCOMPtr<nsIContentPermissionPrompt> prompt |
|
152 = do_CreateInstance(NS_CONTENT_PERMISSION_PROMPT_CONTRACTID); |
|
153 if (!prompt || NS_FAILED(prompt->Prompt(this))) { |
|
154 Cancel(); |
|
155 } |
|
156 return NS_OK; |
|
157 } |
|
158 |
|
159 if (!mWindow) { |
|
160 Cancel(); |
|
161 return NS_OK; |
|
162 } |
|
163 |
|
164 // because owner implements nsITabChild, we can assume that it is |
|
165 // the one and only TabChild. |
|
166 TabChild* child = TabChild::GetFrom(mWindow->GetDocShell()); |
|
167 if (!child) { |
|
168 Cancel(); |
|
169 return NS_OK; |
|
170 } |
|
171 |
|
172 // Retain a reference so the object isn't deleted without IPDL's |
|
173 // knowledge. Corresponding release occurs in |
|
174 // DeallocPContentPermissionRequest. |
|
175 AddRef(); |
|
176 |
|
177 nsTArray<PermissionRequest> permArray; |
|
178 nsTArray<nsString> emptyOptions; |
|
179 permArray.AppendElement(PermissionRequest(mPermissionType, |
|
180 mPermissionAccess, |
|
181 emptyOptions)); |
|
182 child->SendPContentPermissionRequestConstructor( |
|
183 this, permArray, IPC::Principal(mPrincipal)); |
|
184 |
|
185 Sendprompt(); |
|
186 return NS_OK; |
|
187 } |
|
188 |
|
189 } /* namespace dom */ |
|
190 } /* namespace mozilla */ |