Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ |
michael@0 | 2 | /* vim: set ts=2 et sw=2 tw=80: */ |
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 file, |
michael@0 | 5 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "mozilla/dom/DeviceStorageFileSystem.h" |
michael@0 | 8 | |
michael@0 | 9 | #include "DeviceStorage.h" |
michael@0 | 10 | #include "mozilla/Preferences.h" |
michael@0 | 11 | #include "mozilla/dom/Directory.h" |
michael@0 | 12 | #include "mozilla/dom/FileSystemUtils.h" |
michael@0 | 13 | #include "nsCOMPtr.h" |
michael@0 | 14 | #include "nsDebug.h" |
michael@0 | 15 | #include "nsDeviceStorage.h" |
michael@0 | 16 | #include "nsIDOMFile.h" |
michael@0 | 17 | #include "nsIFile.h" |
michael@0 | 18 | #include "nsPIDOMWindow.h" |
michael@0 | 19 | |
michael@0 | 20 | namespace mozilla { |
michael@0 | 21 | namespace dom { |
michael@0 | 22 | |
michael@0 | 23 | DeviceStorageFileSystem::DeviceStorageFileSystem( |
michael@0 | 24 | const nsAString& aStorageType, |
michael@0 | 25 | const nsAString& aStorageName) |
michael@0 | 26 | : mDeviceStorage(nullptr) |
michael@0 | 27 | { |
michael@0 | 28 | MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); |
michael@0 | 29 | |
michael@0 | 30 | mStorageType = aStorageType; |
michael@0 | 31 | mStorageName = aStorageName; |
michael@0 | 32 | |
michael@0 | 33 | // Generate the string representation of the file system. |
michael@0 | 34 | mString.AppendLiteral("devicestorage-"); |
michael@0 | 35 | mString.Append(mStorageType); |
michael@0 | 36 | mString.AppendLiteral("-"); |
michael@0 | 37 | mString.Append(mStorageName); |
michael@0 | 38 | |
michael@0 | 39 | mIsTesting = |
michael@0 | 40 | mozilla::Preferences::GetBool("device.storage.prompt.testing", false); |
michael@0 | 41 | |
michael@0 | 42 | // Get the permission name required to access the file system. |
michael@0 | 43 | nsresult rv = |
michael@0 | 44 | DeviceStorageTypeChecker::GetPermissionForType(mStorageType, mPermission); |
michael@0 | 45 | NS_WARN_IF(NS_FAILED(rv)); |
michael@0 | 46 | |
michael@0 | 47 | // Get the local path of the file system root. |
michael@0 | 48 | // Since the child process is not allowed to access the file system, we only |
michael@0 | 49 | // do this from the parent process. |
michael@0 | 50 | if (!FileSystemUtils::IsParentProcess()) { |
michael@0 | 51 | return; |
michael@0 | 52 | } |
michael@0 | 53 | nsCOMPtr<nsIFile> rootFile; |
michael@0 | 54 | DeviceStorageFile::GetRootDirectoryForType(aStorageType, |
michael@0 | 55 | aStorageName, |
michael@0 | 56 | getter_AddRefs(rootFile)); |
michael@0 | 57 | |
michael@0 | 58 | NS_WARN_IF(!rootFile || NS_FAILED(rootFile->GetPath(mLocalRootPath))); |
michael@0 | 59 | FileSystemUtils::LocalPathToNormalizedPath(mLocalRootPath, |
michael@0 | 60 | mNormalizedLocalRootPath); |
michael@0 | 61 | |
michael@0 | 62 | // DeviceStorageTypeChecker is a singleton object and must be initialized on |
michael@0 | 63 | // the main thread. We initialize it here so that we can use it on the worker |
michael@0 | 64 | // thread. |
michael@0 | 65 | DebugOnly<DeviceStorageTypeChecker*> typeChecker |
michael@0 | 66 | = DeviceStorageTypeChecker::CreateOrGet(); |
michael@0 | 67 | MOZ_ASSERT(typeChecker); |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | DeviceStorageFileSystem::~DeviceStorageFileSystem() |
michael@0 | 71 | { |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | void |
michael@0 | 75 | DeviceStorageFileSystem::Init(nsDOMDeviceStorage* aDeviceStorage) |
michael@0 | 76 | { |
michael@0 | 77 | MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); |
michael@0 | 78 | MOZ_ASSERT(aDeviceStorage); |
michael@0 | 79 | mDeviceStorage = aDeviceStorage; |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | void |
michael@0 | 83 | DeviceStorageFileSystem::Shutdown() |
michael@0 | 84 | { |
michael@0 | 85 | MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); |
michael@0 | 86 | mDeviceStorage = nullptr; |
michael@0 | 87 | mShutdown = true; |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | nsPIDOMWindow* |
michael@0 | 91 | DeviceStorageFileSystem::GetWindow() const |
michael@0 | 92 | { |
michael@0 | 93 | MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); |
michael@0 | 94 | if (!mDeviceStorage) { |
michael@0 | 95 | return nullptr; |
michael@0 | 96 | } |
michael@0 | 97 | return mDeviceStorage->GetOwner(); |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | already_AddRefed<nsIFile> |
michael@0 | 101 | DeviceStorageFileSystem::GetLocalFile(const nsAString& aRealPath) const |
michael@0 | 102 | { |
michael@0 | 103 | MOZ_ASSERT(FileSystemUtils::IsParentProcess(), |
michael@0 | 104 | "Should be on parent process!"); |
michael@0 | 105 | nsAutoString localPath; |
michael@0 | 106 | FileSystemUtils::NormalizedPathToLocalPath(aRealPath, localPath); |
michael@0 | 107 | localPath = mLocalRootPath + localPath; |
michael@0 | 108 | nsCOMPtr<nsIFile> file; |
michael@0 | 109 | nsresult rv = NS_NewLocalFile(localPath, false, getter_AddRefs(file)); |
michael@0 | 110 | if (NS_WARN_IF(NS_FAILED(rv))) { |
michael@0 | 111 | return nullptr; |
michael@0 | 112 | } |
michael@0 | 113 | return file.forget(); |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | bool |
michael@0 | 117 | DeviceStorageFileSystem::GetRealPath(nsIDOMFile* aFile, nsAString& aRealPath) const |
michael@0 | 118 | { |
michael@0 | 119 | MOZ_ASSERT(FileSystemUtils::IsParentProcess(), |
michael@0 | 120 | "Should be on parent process!"); |
michael@0 | 121 | MOZ_ASSERT(aFile, "aFile Should not be null."); |
michael@0 | 122 | |
michael@0 | 123 | aRealPath.Truncate(); |
michael@0 | 124 | |
michael@0 | 125 | nsAutoString filePath; |
michael@0 | 126 | if (NS_FAILED(aFile->GetMozFullPathInternal(filePath))) { |
michael@0 | 127 | return false; |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | return LocalPathToRealPath(filePath, aRealPath); |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | const nsAString& |
michael@0 | 134 | DeviceStorageFileSystem::GetRootName() const |
michael@0 | 135 | { |
michael@0 | 136 | return mStorageName; |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | bool |
michael@0 | 140 | DeviceStorageFileSystem::IsSafeFile(nsIFile* aFile) const |
michael@0 | 141 | { |
michael@0 | 142 | MOZ_ASSERT(FileSystemUtils::IsParentProcess(), |
michael@0 | 143 | "Should be on parent process!"); |
michael@0 | 144 | MOZ_ASSERT(aFile); |
michael@0 | 145 | |
michael@0 | 146 | // Check if this file belongs to this storage. |
michael@0 | 147 | nsAutoString path; |
michael@0 | 148 | if (NS_FAILED(aFile->GetPath(path))) { |
michael@0 | 149 | return false; |
michael@0 | 150 | } |
michael@0 | 151 | if (!LocalPathToRealPath(path, path)) { |
michael@0 | 152 | return false; |
michael@0 | 153 | } |
michael@0 | 154 | |
michael@0 | 155 | // Check if the file type is compatible with the storage type. |
michael@0 | 156 | DeviceStorageTypeChecker* typeChecker |
michael@0 | 157 | = DeviceStorageTypeChecker::CreateOrGet(); |
michael@0 | 158 | MOZ_ASSERT(typeChecker); |
michael@0 | 159 | return typeChecker->Check(mStorageType, aFile); |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | bool |
michael@0 | 163 | DeviceStorageFileSystem::IsSafeDirectory(Directory* aDir) const |
michael@0 | 164 | { |
michael@0 | 165 | MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); |
michael@0 | 166 | MOZ_ASSERT(aDir); |
michael@0 | 167 | nsRefPtr<FileSystemBase> fs = aDir->GetFileSystem(); |
michael@0 | 168 | MOZ_ASSERT(fs); |
michael@0 | 169 | // Check if the given directory is from this storage. |
michael@0 | 170 | return fs->ToString() == mString; |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | bool |
michael@0 | 174 | DeviceStorageFileSystem::LocalPathToRealPath(const nsAString& aLocalPath, |
michael@0 | 175 | nsAString& aRealPath) const |
michael@0 | 176 | { |
michael@0 | 177 | nsAutoString path; |
michael@0 | 178 | FileSystemUtils::LocalPathToNormalizedPath(aLocalPath, path); |
michael@0 | 179 | if (!FileSystemUtils::IsDescendantPath(mNormalizedLocalRootPath, path)) { |
michael@0 | 180 | aRealPath.Truncate(); |
michael@0 | 181 | return false; |
michael@0 | 182 | } |
michael@0 | 183 | aRealPath = Substring(path, mNormalizedLocalRootPath.Length()); |
michael@0 | 184 | return true; |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | } // namespace dom |
michael@0 | 188 | } // namespace mozilla |