dom/filesystem/FileSystemPermissionRequest.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial