dom/filesystem/FileSystemPermissionRequest.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/filesystem/FileSystemPermissionRequest.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,190 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:set ts=2 sw=2 sts=2 et cindent: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +#include "FileSystemPermissionRequest.h"
    1.10 +
    1.11 +#include "mozilla/dom/FileSystemBase.h"
    1.12 +#include "mozilla/dom/FileSystemTaskBase.h"
    1.13 +#include "mozilla/dom/FileSystemUtils.h"
    1.14 +#include "mozilla/dom/TabChild.h"
    1.15 +#include "nsIDocument.h"
    1.16 +#include "nsPIDOMWindow.h"
    1.17 +#include "nsString.h"
    1.18 +
    1.19 +namespace mozilla {
    1.20 +namespace dom {
    1.21 +
    1.22 +NS_IMPL_ISUPPORTS(FileSystemPermissionRequest, nsIRunnable, nsIContentPermissionRequest)
    1.23 +
    1.24 +// static
    1.25 +void
    1.26 +FileSystemPermissionRequest::RequestForTask(FileSystemTaskBase* aTask)
    1.27 +{
    1.28 +  MOZ_ASSERT(aTask, "aTask should not be null!");
    1.29 +  MOZ_ASSERT(NS_IsMainThread());
    1.30 +  nsRefPtr<FileSystemPermissionRequest> request =
    1.31 +    new FileSystemPermissionRequest(aTask);
    1.32 +  NS_DispatchToCurrentThread(request);
    1.33 +}
    1.34 +
    1.35 +FileSystemPermissionRequest::FileSystemPermissionRequest(
    1.36 +  FileSystemTaskBase* aTask)
    1.37 +  : mTask(aTask)
    1.38 +{
    1.39 +  MOZ_ASSERT(mTask, "aTask should not be null!");
    1.40 +  MOZ_ASSERT(NS_IsMainThread());
    1.41 +
    1.42 +  mTask->GetPermissionAccessType(mPermissionAccess);
    1.43 +
    1.44 +  nsRefPtr<FileSystemBase> filesystem = mTask->GetFileSystem();
    1.45 +  if (!filesystem) {
    1.46 +    return;
    1.47 +  }
    1.48 +
    1.49 +  mPermissionType = filesystem->GetPermission();
    1.50 +
    1.51 +  mWindow = filesystem->GetWindow();
    1.52 +  if (!mWindow) {
    1.53 +    return;
    1.54 +  }
    1.55 +
    1.56 +  nsCOMPtr<nsIDocument> doc = mWindow->GetDoc();
    1.57 +  if (!doc) {
    1.58 +    return;
    1.59 +  }
    1.60 +
    1.61 +  mPrincipal = doc->NodePrincipal();
    1.62 +}
    1.63 +
    1.64 +FileSystemPermissionRequest::~FileSystemPermissionRequest()
    1.65 +{
    1.66 +}
    1.67 +
    1.68 +bool
    1.69 +FileSystemPermissionRequest::Recv__delete__(const bool& aAllow,
    1.70 +               const InfallibleTArray<PermissionChoice>& aChoices)
    1.71 +{
    1.72 +  MOZ_ASSERT(aChoices.IsEmpty(),
    1.73 +             "FileSystemPermissionRequest doesn't support permission choice");
    1.74 +  if (aAllow) {
    1.75 +    Allow(JS::UndefinedHandleValue);
    1.76 +  } else {
    1.77 +    Cancel();
    1.78 +  }
    1.79 +  return true;
    1.80 +}
    1.81 +
    1.82 +void
    1.83 +FileSystemPermissionRequest::IPDLRelease()
    1.84 +{
    1.85 +  Release();
    1.86 +}
    1.87 +
    1.88 +NS_IMETHODIMP
    1.89 +FileSystemPermissionRequest::GetTypes(nsIArray** aTypes)
    1.90 +{
    1.91 +  nsTArray<nsString> emptyOptions;
    1.92 +  return CreatePermissionArray(mPermissionType,
    1.93 +                               mPermissionAccess,
    1.94 +                               emptyOptions,
    1.95 +                               aTypes);
    1.96 +}
    1.97 +
    1.98 +NS_IMETHODIMP
    1.99 +FileSystemPermissionRequest::GetPrincipal(nsIPrincipal** aRequestingPrincipal)
   1.100 +{
   1.101 +  NS_IF_ADDREF(*aRequestingPrincipal = mPrincipal);
   1.102 +  return NS_OK;
   1.103 +}
   1.104 +
   1.105 +NS_IMETHODIMP
   1.106 +FileSystemPermissionRequest::GetWindow(nsIDOMWindow** aRequestingWindow)
   1.107 +{
   1.108 +  NS_IF_ADDREF(*aRequestingWindow = mWindow);
   1.109 +  return NS_OK;
   1.110 +}
   1.111 +
   1.112 +NS_IMETHODIMP
   1.113 +FileSystemPermissionRequest::GetElement(nsIDOMElement** aRequestingElement)
   1.114 +{
   1.115 +  *aRequestingElement = nullptr;
   1.116 +  return NS_OK;
   1.117 +}
   1.118 +
   1.119 +NS_IMETHODIMP
   1.120 +FileSystemPermissionRequest::Cancel()
   1.121 +{
   1.122 +  MOZ_ASSERT(NS_IsMainThread());
   1.123 +  mTask->SetError(NS_ERROR_DOM_SECURITY_ERR);
   1.124 +  mTask->Start();
   1.125 +  return NS_OK;
   1.126 +}
   1.127 +
   1.128 +NS_IMETHODIMP
   1.129 +FileSystemPermissionRequest::Allow(JS::HandleValue aChoices)
   1.130 +{
   1.131 +  MOZ_ASSERT(NS_IsMainThread());
   1.132 +  MOZ_ASSERT(aChoices.isUndefined());
   1.133 +  mTask->Start();
   1.134 +  return NS_OK;
   1.135 +}
   1.136 +
   1.137 +NS_IMETHODIMP
   1.138 +FileSystemPermissionRequest::Run()
   1.139 +{
   1.140 +  MOZ_ASSERT(NS_IsMainThread());
   1.141 +
   1.142 +  nsRefPtr<FileSystemBase> filesystem = mTask->GetFileSystem();
   1.143 +  if (!filesystem) {
   1.144 +    Cancel();
   1.145 +    return NS_OK;
   1.146 +  }
   1.147 +
   1.148 +  if (filesystem->IsTesting()) {
   1.149 +    Allow(JS::UndefinedHandleValue);
   1.150 +    return NS_OK;
   1.151 +  }
   1.152 +
   1.153 +  if (FileSystemUtils::IsParentProcess()) {
   1.154 +    nsCOMPtr<nsIContentPermissionPrompt> prompt
   1.155 +      = do_CreateInstance(NS_CONTENT_PERMISSION_PROMPT_CONTRACTID);
   1.156 +    if (!prompt || NS_FAILED(prompt->Prompt(this))) {
   1.157 +      Cancel();
   1.158 +    }
   1.159 +    return NS_OK;
   1.160 +  }
   1.161 +
   1.162 +  if (!mWindow) {
   1.163 +    Cancel();
   1.164 +    return NS_OK;
   1.165 +  }
   1.166 +
   1.167 +  // because owner implements nsITabChild, we can assume that it is
   1.168 +  // the one and only TabChild.
   1.169 +  TabChild* child = TabChild::GetFrom(mWindow->GetDocShell());
   1.170 +  if (!child) {
   1.171 +    Cancel();
   1.172 +    return NS_OK;
   1.173 +  }
   1.174 +
   1.175 +  // Retain a reference so the object isn't deleted without IPDL's
   1.176 +  // knowledge. Corresponding release occurs in
   1.177 +  // DeallocPContentPermissionRequest.
   1.178 +  AddRef();
   1.179 +
   1.180 +  nsTArray<PermissionRequest> permArray;
   1.181 +  nsTArray<nsString> emptyOptions;
   1.182 +  permArray.AppendElement(PermissionRequest(mPermissionType,
   1.183 +                                            mPermissionAccess,
   1.184 +                                            emptyOptions));
   1.185 +  child->SendPContentPermissionRequestConstructor(
   1.186 +    this, permArray, IPC::Principal(mPrincipal));
   1.187 +
   1.188 +  Sendprompt();
   1.189 +  return NS_OK;
   1.190 +}
   1.191 +
   1.192 +} /* namespace dom */
   1.193 +} /* namespace mozilla */

mercurial