dom/filesystem/DeviceStorageFileSystem.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/filesystem/DeviceStorageFileSystem.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,188 @@
     1.4 +/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
     1.5 +/* vim: set ts=2 et sw=2 tw=80: */
     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 file,
     1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "mozilla/dom/DeviceStorageFileSystem.h"
    1.11 +
    1.12 +#include "DeviceStorage.h"
    1.13 +#include "mozilla/Preferences.h"
    1.14 +#include "mozilla/dom/Directory.h"
    1.15 +#include "mozilla/dom/FileSystemUtils.h"
    1.16 +#include "nsCOMPtr.h"
    1.17 +#include "nsDebug.h"
    1.18 +#include "nsDeviceStorage.h"
    1.19 +#include "nsIDOMFile.h"
    1.20 +#include "nsIFile.h"
    1.21 +#include "nsPIDOMWindow.h"
    1.22 +
    1.23 +namespace mozilla {
    1.24 +namespace dom {
    1.25 +
    1.26 +DeviceStorageFileSystem::DeviceStorageFileSystem(
    1.27 +  const nsAString& aStorageType,
    1.28 +  const nsAString& aStorageName)
    1.29 +  : mDeviceStorage(nullptr)
    1.30 +{
    1.31 +  MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
    1.32 +
    1.33 +  mStorageType = aStorageType;
    1.34 +  mStorageName = aStorageName;
    1.35 +
    1.36 +  // Generate the string representation of the file system.
    1.37 +  mString.AppendLiteral("devicestorage-");
    1.38 +  mString.Append(mStorageType);
    1.39 +  mString.AppendLiteral("-");
    1.40 +  mString.Append(mStorageName);
    1.41 +
    1.42 +  mIsTesting =
    1.43 +    mozilla::Preferences::GetBool("device.storage.prompt.testing", false);
    1.44 +
    1.45 +  // Get the permission name required to access the file system.
    1.46 +  nsresult rv =
    1.47 +    DeviceStorageTypeChecker::GetPermissionForType(mStorageType, mPermission);
    1.48 +  NS_WARN_IF(NS_FAILED(rv));
    1.49 +
    1.50 +  // Get the local path of the file system root.
    1.51 +  // Since the child process is not allowed to access the file system, we only
    1.52 +  // do this from the parent process.
    1.53 +  if (!FileSystemUtils::IsParentProcess()) {
    1.54 +    return;
    1.55 +  }
    1.56 +  nsCOMPtr<nsIFile> rootFile;
    1.57 +  DeviceStorageFile::GetRootDirectoryForType(aStorageType,
    1.58 +                                             aStorageName,
    1.59 +                                             getter_AddRefs(rootFile));
    1.60 +
    1.61 +  NS_WARN_IF(!rootFile || NS_FAILED(rootFile->GetPath(mLocalRootPath)));
    1.62 +  FileSystemUtils::LocalPathToNormalizedPath(mLocalRootPath,
    1.63 +    mNormalizedLocalRootPath);
    1.64 +
    1.65 +  // DeviceStorageTypeChecker is a singleton object and must be initialized on
    1.66 +  // the main thread. We initialize it here so that we can use it on the worker
    1.67 +  // thread.
    1.68 +  DebugOnly<DeviceStorageTypeChecker*> typeChecker
    1.69 +    = DeviceStorageTypeChecker::CreateOrGet();
    1.70 +  MOZ_ASSERT(typeChecker);
    1.71 +}
    1.72 +
    1.73 +DeviceStorageFileSystem::~DeviceStorageFileSystem()
    1.74 +{
    1.75 +}
    1.76 +
    1.77 +void
    1.78 +DeviceStorageFileSystem::Init(nsDOMDeviceStorage* aDeviceStorage)
    1.79 +{
    1.80 +  MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
    1.81 +  MOZ_ASSERT(aDeviceStorage);
    1.82 +  mDeviceStorage = aDeviceStorage;
    1.83 +}
    1.84 +
    1.85 +void
    1.86 +DeviceStorageFileSystem::Shutdown()
    1.87 +{
    1.88 +  MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
    1.89 +  mDeviceStorage = nullptr;
    1.90 +  mShutdown = true;
    1.91 +}
    1.92 +
    1.93 +nsPIDOMWindow*
    1.94 +DeviceStorageFileSystem::GetWindow() const
    1.95 +{
    1.96 +  MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
    1.97 +  if (!mDeviceStorage) {
    1.98 +    return nullptr;
    1.99 +  }
   1.100 +  return mDeviceStorage->GetOwner();
   1.101 +}
   1.102 +
   1.103 +already_AddRefed<nsIFile>
   1.104 +DeviceStorageFileSystem::GetLocalFile(const nsAString& aRealPath) const
   1.105 +{
   1.106 +  MOZ_ASSERT(FileSystemUtils::IsParentProcess(),
   1.107 +             "Should be on parent process!");
   1.108 +  nsAutoString localPath;
   1.109 +  FileSystemUtils::NormalizedPathToLocalPath(aRealPath, localPath);
   1.110 +  localPath = mLocalRootPath + localPath;
   1.111 +  nsCOMPtr<nsIFile> file;
   1.112 +  nsresult rv = NS_NewLocalFile(localPath, false, getter_AddRefs(file));
   1.113 +  if (NS_WARN_IF(NS_FAILED(rv))) {
   1.114 +    return nullptr;
   1.115 +  }
   1.116 +  return file.forget();
   1.117 +}
   1.118 +
   1.119 +bool
   1.120 +DeviceStorageFileSystem::GetRealPath(nsIDOMFile* aFile, nsAString& aRealPath) const
   1.121 +{
   1.122 +  MOZ_ASSERT(FileSystemUtils::IsParentProcess(),
   1.123 +             "Should be on parent process!");
   1.124 +  MOZ_ASSERT(aFile, "aFile Should not be null.");
   1.125 +
   1.126 +  aRealPath.Truncate();
   1.127 +
   1.128 +  nsAutoString filePath;
   1.129 +  if (NS_FAILED(aFile->GetMozFullPathInternal(filePath))) {
   1.130 +    return false;
   1.131 +  }
   1.132 +
   1.133 +  return LocalPathToRealPath(filePath, aRealPath);
   1.134 +}
   1.135 +
   1.136 +const nsAString&
   1.137 +DeviceStorageFileSystem::GetRootName() const
   1.138 +{
   1.139 +  return mStorageName;
   1.140 +}
   1.141 +
   1.142 +bool
   1.143 +DeviceStorageFileSystem::IsSafeFile(nsIFile* aFile) const
   1.144 +{
   1.145 +  MOZ_ASSERT(FileSystemUtils::IsParentProcess(),
   1.146 +             "Should be on parent process!");
   1.147 +  MOZ_ASSERT(aFile);
   1.148 +
   1.149 +  // Check if this file belongs to this storage.
   1.150 +  nsAutoString path;
   1.151 +  if (NS_FAILED(aFile->GetPath(path))) {
   1.152 +    return false;
   1.153 +  }
   1.154 +  if (!LocalPathToRealPath(path, path)) {
   1.155 +    return false;
   1.156 +  }
   1.157 +
   1.158 +  // Check if the file type is compatible with the storage type.
   1.159 +  DeviceStorageTypeChecker* typeChecker
   1.160 +    = DeviceStorageTypeChecker::CreateOrGet();
   1.161 +  MOZ_ASSERT(typeChecker);
   1.162 +  return typeChecker->Check(mStorageType, aFile);
   1.163 +}
   1.164 +
   1.165 +bool
   1.166 +DeviceStorageFileSystem::IsSafeDirectory(Directory* aDir) const
   1.167 +{
   1.168 +  MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!");
   1.169 +  MOZ_ASSERT(aDir);
   1.170 +  nsRefPtr<FileSystemBase> fs = aDir->GetFileSystem();
   1.171 +  MOZ_ASSERT(fs);
   1.172 +  // Check if the given directory is from this storage.
   1.173 +  return fs->ToString() == mString;
   1.174 +}
   1.175 +
   1.176 +bool
   1.177 +DeviceStorageFileSystem::LocalPathToRealPath(const nsAString& aLocalPath,
   1.178 +                                             nsAString& aRealPath) const
   1.179 +{
   1.180 +  nsAutoString path;
   1.181 +  FileSystemUtils::LocalPathToNormalizedPath(aLocalPath, path);
   1.182 +  if (!FileSystemUtils::IsDescendantPath(mNormalizedLocalRootPath, path)) {
   1.183 +    aRealPath.Truncate();
   1.184 +    return false;
   1.185 +  }
   1.186 +  aRealPath = Substring(path, mNormalizedLocalRootPath.Length());
   1.187 +  return true;
   1.188 +}
   1.189 +
   1.190 +} // namespace dom
   1.191 +} // namespace mozilla

mercurial