Bumping manifests a=b2g-bump
[gecko.git] / dom / filesystem / FileSystemPermissionRequest.cpp
blobe3e9001e32861fbde170b37076d46c2d6dae7882
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"
8 #include "mozilla/dom/FileSystemBase.h"
9 #include "mozilla/dom/FileSystemTaskBase.h"
10 #include "mozilla/dom/FileSystemUtils.h"
11 #include "nsIDocument.h"
12 #include "nsPIDOMWindow.h"
13 #include "nsContentPermissionHelper.h"
15 namespace mozilla {
16 namespace dom {
18 NS_IMPL_ISUPPORTS(FileSystemPermissionRequest, nsIRunnable, nsIContentPermissionRequest)
20 // static
21 void
22 FileSystemPermissionRequest::RequestForTask(FileSystemTaskBase* aTask)
24 MOZ_ASSERT(aTask, "aTask should not be null!");
25 MOZ_ASSERT(NS_IsMainThread());
26 nsRefPtr<FileSystemPermissionRequest> request =
27 new FileSystemPermissionRequest(aTask);
28 NS_DispatchToCurrentThread(request);
31 FileSystemPermissionRequest::FileSystemPermissionRequest(
32 FileSystemTaskBase* aTask)
33 : mTask(aTask)
35 MOZ_ASSERT(mTask, "aTask should not be null!");
36 MOZ_ASSERT(NS_IsMainThread());
38 mTask->GetPermissionAccessType(mPermissionAccess);
40 nsRefPtr<FileSystemBase> filesystem = mTask->GetFileSystem();
41 if (!filesystem) {
42 return;
45 mPermissionType = filesystem->GetPermission();
47 mWindow = filesystem->GetWindow();
48 if (!mWindow) {
49 return;
52 nsCOMPtr<nsIDocument> doc = mWindow->GetDoc();
53 if (!doc) {
54 return;
57 mPrincipal = doc->NodePrincipal();
60 FileSystemPermissionRequest::~FileSystemPermissionRequest()
64 NS_IMETHODIMP
65 FileSystemPermissionRequest::GetTypes(nsIArray** aTypes)
67 nsTArray<nsString> emptyOptions;
68 return nsContentPermissionUtils::CreatePermissionArray(mPermissionType,
69 mPermissionAccess,
70 emptyOptions,
71 aTypes);
74 NS_IMETHODIMP
75 FileSystemPermissionRequest::GetPrincipal(nsIPrincipal** aRequestingPrincipal)
77 NS_IF_ADDREF(*aRequestingPrincipal = mPrincipal);
78 return NS_OK;
81 NS_IMETHODIMP
82 FileSystemPermissionRequest::GetWindow(nsIDOMWindow** aRequestingWindow)
84 NS_IF_ADDREF(*aRequestingWindow = mWindow);
85 return NS_OK;
88 NS_IMETHODIMP
89 FileSystemPermissionRequest::GetElement(nsIDOMElement** aRequestingElement)
91 *aRequestingElement = nullptr;
92 return NS_OK;
95 NS_IMETHODIMP
96 FileSystemPermissionRequest::Cancel()
98 MOZ_ASSERT(NS_IsMainThread());
99 mTask->SetError(NS_ERROR_DOM_SECURITY_ERR);
100 mTask->Start();
101 return NS_OK;
104 NS_IMETHODIMP
105 FileSystemPermissionRequest::Allow(JS::HandleValue aChoices)
107 MOZ_ASSERT(NS_IsMainThread());
108 MOZ_ASSERT(aChoices.isUndefined());
109 mTask->Start();
110 return NS_OK;
113 NS_IMETHODIMP
114 FileSystemPermissionRequest::Run()
116 MOZ_ASSERT(NS_IsMainThread());
118 nsRefPtr<FileSystemBase> filesystem = mTask->GetFileSystem();
119 if (!filesystem) {
120 Cancel();
121 return NS_OK;
124 if (filesystem->IsTesting()) {
125 Allow(JS::UndefinedHandleValue);
126 return NS_OK;
129 if (!mWindow) {
130 Cancel();
131 return NS_OK;
134 nsContentPermissionUtils::AskPermission(this, mWindow);
135 return NS_OK;
138 } /* namespace dom */
139 } /* namespace mozilla */