| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/fileapi/cros_mount_point_provider.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/message_loop.h" | |
| 10 #include "base/path_service.h" | |
| 11 #include "base/strings/stringprintf.h" | |
| 12 #include "base/strings/utf_string_conversions.h" | |
| 13 #include "base/synchronization/lock.h" | |
| 14 #include "chrome/browser/chromeos/fileapi/cros_mount_point_provider_delegate.h" | |
| 15 #include "chrome/browser/chromeos/fileapi/file_access_permissions.h" | |
| 16 #include "chromeos/dbus/cros_disks_client.h" | |
| 17 #include "webkit/browser/blob/file_stream_reader.h" | |
| 18 #include "webkit/browser/fileapi/async_file_util_adapter.h" | |
| 19 #include "webkit/browser/fileapi/copy_or_move_file_validator.h" | |
| 20 #include "webkit/browser/fileapi/external_mount_points.h" | |
| 21 #include "webkit/browser/fileapi/file_system_context.h" | |
| 22 #include "webkit/browser/fileapi/file_system_file_stream_reader.h" | |
| 23 #include "webkit/browser/fileapi/file_system_operation_context.h" | |
| 24 #include "webkit/browser/fileapi/file_system_task_runners.h" | |
| 25 #include "webkit/browser/fileapi/file_system_url.h" | |
| 26 #include "webkit/browser/fileapi/isolated_context.h" | |
| 27 #include "webkit/browser/fileapi/isolated_file_util.h" | |
| 28 #include "webkit/browser/fileapi/local_file_stream_writer.h" | |
| 29 #include "webkit/browser/fileapi/local_file_system_operation.h" | |
| 30 | |
| 31 namespace { | |
| 32 | |
| 33 const char kChromeUIScheme[] = "chrome"; | |
| 34 | |
| 35 } // namespace | |
| 36 | |
| 37 namespace chromeos { | |
| 38 | |
| 39 // static | |
| 40 bool CrosMountPointProvider::CanHandleURL(const fileapi::FileSystemURL& url) { | |
| 41 if (!url.is_valid()) | |
| 42 return false; | |
| 43 return url.type() == fileapi::kFileSystemTypeNativeLocal || | |
| 44 url.type() == fileapi::kFileSystemTypeRestrictedNativeLocal || | |
| 45 url.type() == fileapi::kFileSystemTypeDrive; | |
| 46 } | |
| 47 | |
| 48 CrosMountPointProvider::CrosMountPointProvider( | |
| 49 CrosMountPointProviderDelegate* drive_delegate, | |
| 50 scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy, | |
| 51 scoped_refptr<fileapi::ExternalMountPoints> mount_points, | |
| 52 fileapi::ExternalMountPoints* system_mount_points) | |
| 53 : special_storage_policy_(special_storage_policy), | |
| 54 file_access_permissions_(new FileAccessPermissions()), | |
| 55 local_file_util_(new fileapi::AsyncFileUtilAdapter( | |
| 56 new fileapi::IsolatedFileUtil())), | |
| 57 drive_delegate_(drive_delegate), | |
| 58 mount_points_(mount_points), | |
| 59 system_mount_points_(system_mount_points) { | |
| 60 } | |
| 61 | |
| 62 CrosMountPointProvider::~CrosMountPointProvider() { | |
| 63 } | |
| 64 | |
| 65 void CrosMountPointProvider::AddSystemMountPoints() { | |
| 66 // RegisterFileSystem() is no-op if the mount point with the same name | |
| 67 // already exists, hence it's safe to call without checking if a mount | |
| 68 // point already exists or not. | |
| 69 | |
| 70 // TODO(satorux): "Downloads" directory should probably be per-profile. For | |
| 71 // this to be per-profile, a unique directory path should be chosen per | |
| 72 // profile, and the mount point should be added to | |
| 73 // mount_points_. crbug.com/247236 | |
| 74 base::FilePath home_path; | |
| 75 if (PathService::Get(base::DIR_HOME, &home_path)) { | |
| 76 system_mount_points_->RegisterFileSystem( | |
| 77 "Downloads", | |
| 78 fileapi::kFileSystemTypeNativeLocal, | |
| 79 home_path.AppendASCII("Downloads")); | |
| 80 } | |
| 81 | |
| 82 system_mount_points_->RegisterFileSystem( | |
| 83 "archive", | |
| 84 fileapi::kFileSystemTypeNativeLocal, | |
| 85 chromeos::CrosDisksClient::GetArchiveMountPoint()); | |
| 86 system_mount_points_->RegisterFileSystem( | |
| 87 "removable", | |
| 88 fileapi::kFileSystemTypeNativeLocal, | |
| 89 chromeos::CrosDisksClient::GetRemovableDiskMountPoint()); | |
| 90 system_mount_points_->RegisterFileSystem( | |
| 91 "oem", | |
| 92 fileapi::kFileSystemTypeRestrictedNativeLocal, | |
| 93 base::FilePath(FILE_PATH_LITERAL("/usr/share/oem"))); | |
| 94 } | |
| 95 | |
| 96 bool CrosMountPointProvider::CanHandleType(fileapi::FileSystemType type) const { | |
| 97 switch (type) { | |
| 98 case fileapi::kFileSystemTypeExternal: | |
| 99 case fileapi::kFileSystemTypeDrive: | |
| 100 case fileapi::kFileSystemTypeRestrictedNativeLocal: | |
| 101 case fileapi::kFileSystemTypeNativeLocal: | |
| 102 case fileapi::kFileSystemTypeNativeForPlatformApp: | |
| 103 return true; | |
| 104 default: | |
| 105 return false; | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 void CrosMountPointProvider::OpenFileSystem( | |
| 110 const GURL& origin_url, | |
| 111 fileapi::FileSystemType type, | |
| 112 fileapi::OpenFileSystemMode mode, | |
| 113 const OpenFileSystemCallback& callback) { | |
| 114 DCHECK(fileapi::IsolatedContext::IsIsolatedType(type)); | |
| 115 // Nothing to validate for external filesystem. | |
| 116 callback.Run(base::PLATFORM_FILE_OK); | |
| 117 } | |
| 118 | |
| 119 fileapi::FileSystemQuotaUtil* CrosMountPointProvider::GetQuotaUtil() { | |
| 120 // No quota support. | |
| 121 return NULL; | |
| 122 } | |
| 123 | |
| 124 bool CrosMountPointProvider::IsAccessAllowed( | |
| 125 const fileapi::FileSystemURL& url) const { | |
| 126 if (!url.is_valid()) | |
| 127 return false; | |
| 128 | |
| 129 // Permit access to mount points from internal WebUI. | |
| 130 const GURL& origin_url = url.origin(); | |
| 131 if (origin_url.SchemeIs(kChromeUIScheme)) | |
| 132 return true; | |
| 133 | |
| 134 // No extra check is needed for isolated file systems. | |
| 135 if (url.mount_type() == fileapi::kFileSystemTypeIsolated) | |
| 136 return true; | |
| 137 | |
| 138 if (!CanHandleURL(url)) | |
| 139 return false; | |
| 140 | |
| 141 std::string extension_id = origin_url.host(); | |
| 142 // Check first to make sure this extension has fileBrowserHander permissions. | |
| 143 if (!special_storage_policy_->IsFileHandler(extension_id)) | |
| 144 return false; | |
| 145 | |
| 146 return file_access_permissions_->HasAccessPermission(extension_id, | |
| 147 url.virtual_path()); | |
| 148 } | |
| 149 | |
| 150 void CrosMountPointProvider::GrantFullAccessToExtension( | |
| 151 const std::string& extension_id) { | |
| 152 DCHECK(special_storage_policy_->IsFileHandler(extension_id)); | |
| 153 if (!special_storage_policy_->IsFileHandler(extension_id)) | |
| 154 return; | |
| 155 | |
| 156 std::vector<fileapi::MountPoints::MountPointInfo> files; | |
| 157 mount_points_->AddMountPointInfosTo(&files); | |
| 158 system_mount_points_->AddMountPointInfosTo(&files); | |
| 159 | |
| 160 for (size_t i = 0; i < files.size(); ++i) { | |
| 161 file_access_permissions_->GrantAccessPermission( | |
| 162 extension_id, | |
| 163 base::FilePath::FromUTF8Unsafe(files[i].name)); | |
| 164 } | |
| 165 } | |
| 166 | |
| 167 void CrosMountPointProvider::GrantFileAccessToExtension( | |
| 168 const std::string& extension_id, const base::FilePath& virtual_path) { | |
| 169 // All we care about here is access from extensions for now. | |
| 170 DCHECK(special_storage_policy_->IsFileHandler(extension_id)); | |
| 171 if (!special_storage_policy_->IsFileHandler(extension_id)) | |
| 172 return; | |
| 173 | |
| 174 std::string id; | |
| 175 fileapi::FileSystemType type; | |
| 176 base::FilePath path; | |
| 177 if (!mount_points_->CrackVirtualPath(virtual_path, &id, &type, &path) && | |
| 178 !system_mount_points_->CrackVirtualPath(virtual_path, | |
| 179 &id, &type, &path)) { | |
| 180 return; | |
| 181 } | |
| 182 | |
| 183 if (type == fileapi::kFileSystemTypeRestrictedNativeLocal) { | |
| 184 LOG(ERROR) << "Can't grant access for restricted mount point"; | |
| 185 return; | |
| 186 } | |
| 187 | |
| 188 file_access_permissions_->GrantAccessPermission(extension_id, virtual_path); | |
| 189 } | |
| 190 | |
| 191 void CrosMountPointProvider::RevokeAccessForExtension( | |
| 192 const std::string& extension_id) { | |
| 193 file_access_permissions_->RevokePermissions(extension_id); | |
| 194 } | |
| 195 | |
| 196 std::vector<base::FilePath> CrosMountPointProvider::GetRootDirectories() const { | |
| 197 std::vector<fileapi::MountPoints::MountPointInfo> mount_points; | |
| 198 mount_points_->AddMountPointInfosTo(&mount_points); | |
| 199 system_mount_points_->AddMountPointInfosTo(&mount_points); | |
| 200 | |
| 201 std::vector<base::FilePath> root_dirs; | |
| 202 for (size_t i = 0; i < mount_points.size(); ++i) | |
| 203 root_dirs.push_back(mount_points[i].path); | |
| 204 return root_dirs; | |
| 205 } | |
| 206 | |
| 207 fileapi::FileSystemFileUtil* CrosMountPointProvider::GetFileUtil( | |
| 208 fileapi::FileSystemType type) { | |
| 209 DCHECK(type == fileapi::kFileSystemTypeNativeLocal || | |
| 210 type == fileapi::kFileSystemTypeRestrictedNativeLocal); | |
| 211 return local_file_util_->sync_file_util(); | |
| 212 } | |
| 213 | |
| 214 fileapi::AsyncFileUtil* CrosMountPointProvider::GetAsyncFileUtil( | |
| 215 fileapi::FileSystemType type) { | |
| 216 if (type == fileapi::kFileSystemTypeDrive) | |
| 217 return drive_delegate_->GetAsyncFileUtil(type); | |
| 218 | |
| 219 DCHECK(type == fileapi::kFileSystemTypeNativeLocal || | |
| 220 type == fileapi::kFileSystemTypeRestrictedNativeLocal); | |
| 221 return local_file_util_.get(); | |
| 222 } | |
| 223 | |
| 224 fileapi::CopyOrMoveFileValidatorFactory* | |
| 225 CrosMountPointProvider::GetCopyOrMoveFileValidatorFactory( | |
| 226 fileapi::FileSystemType type, base::PlatformFileError* error_code) { | |
| 227 DCHECK(error_code); | |
| 228 *error_code = base::PLATFORM_FILE_OK; | |
| 229 return NULL; | |
| 230 } | |
| 231 | |
| 232 fileapi::FileSystemOperation* CrosMountPointProvider::CreateFileSystemOperation( | |
| 233 const fileapi::FileSystemURL& url, | |
| 234 fileapi::FileSystemContext* context, | |
| 235 base::PlatformFileError* error_code) const { | |
| 236 DCHECK(url.is_valid()); | |
| 237 | |
| 238 if (!IsAccessAllowed(url)) { | |
| 239 *error_code = base::PLATFORM_FILE_ERROR_SECURITY; | |
| 240 return NULL; | |
| 241 } | |
| 242 | |
| 243 if (url.type() == fileapi::kFileSystemTypeDrive) | |
| 244 return drive_delegate_->CreateFileSystemOperation(url, context, error_code); | |
| 245 | |
| 246 DCHECK(url.type() == fileapi::kFileSystemTypeNativeLocal || | |
| 247 url.type() == fileapi::kFileSystemTypeRestrictedNativeLocal); | |
| 248 scoped_ptr<fileapi::FileSystemOperationContext> operation_context( | |
| 249 new fileapi::FileSystemOperationContext(context)); | |
| 250 operation_context->set_root_path(GetFileSystemRootPath(url)); | |
| 251 return new fileapi::LocalFileSystemOperation(url, context, | |
| 252 operation_context.Pass()); | |
| 253 } | |
| 254 | |
| 255 scoped_ptr<webkit_blob::FileStreamReader> | |
| 256 CrosMountPointProvider::CreateFileStreamReader( | |
| 257 const fileapi::FileSystemURL& url, | |
| 258 int64 offset, | |
| 259 const base::Time& expected_modification_time, | |
| 260 fileapi::FileSystemContext* context) const { | |
| 261 DCHECK(url.is_valid()); | |
| 262 | |
| 263 if (!IsAccessAllowed(url)) | |
| 264 return scoped_ptr<webkit_blob::FileStreamReader>(); | |
| 265 | |
| 266 if (url.type() == fileapi::kFileSystemTypeDrive) { | |
| 267 return drive_delegate_->CreateFileStreamReader( | |
| 268 url, offset, expected_modification_time, context); | |
| 269 } | |
| 270 | |
| 271 return scoped_ptr<webkit_blob::FileStreamReader>( | |
| 272 new fileapi::FileSystemFileStreamReader( | |
| 273 context, url, offset, expected_modification_time)); | |
| 274 } | |
| 275 | |
| 276 scoped_ptr<fileapi::FileStreamWriter> | |
| 277 CrosMountPointProvider::CreateFileStreamWriter( | |
| 278 const fileapi::FileSystemURL& url, | |
| 279 int64 offset, | |
| 280 fileapi::FileSystemContext* context) const { | |
| 281 DCHECK(url.is_valid()); | |
| 282 | |
| 283 if (!IsAccessAllowed(url)) | |
| 284 return scoped_ptr<fileapi::FileStreamWriter>(); | |
| 285 | |
| 286 if (url.type() == fileapi::kFileSystemTypeDrive) | |
| 287 return drive_delegate_->CreateFileStreamWriter(url, offset, context); | |
| 288 | |
| 289 if (url.type() == fileapi::kFileSystemTypeRestrictedNativeLocal) | |
| 290 return scoped_ptr<fileapi::FileStreamWriter>(); | |
| 291 | |
| 292 DCHECK(url.type() == fileapi::kFileSystemTypeNativeLocal); | |
| 293 return scoped_ptr<fileapi::FileStreamWriter>( | |
| 294 new fileapi::LocalFileStreamWriter( | |
| 295 context->task_runners()->file_task_runner(), url.path(), offset)); | |
| 296 } | |
| 297 | |
| 298 bool CrosMountPointProvider::GetVirtualPath( | |
| 299 const base::FilePath& filesystem_path, | |
| 300 base::FilePath* virtual_path) { | |
| 301 return mount_points_->GetVirtualPath(filesystem_path, virtual_path) || | |
| 302 system_mount_points_->GetVirtualPath(filesystem_path, virtual_path); | |
| 303 } | |
| 304 | |
| 305 base::FilePath CrosMountPointProvider::GetFileSystemRootPath( | |
| 306 const fileapi::FileSystemURL& url) const { | |
| 307 DCHECK(fileapi::IsolatedContext::IsIsolatedType(url.mount_type())); | |
| 308 if (!url.is_valid()) | |
| 309 return base::FilePath(); | |
| 310 | |
| 311 base::FilePath root_path; | |
| 312 std::string mount_name = url.filesystem_id(); | |
| 313 if (!mount_points_->GetRegisteredPath(mount_name, &root_path) && | |
| 314 !system_mount_points_->GetRegisteredPath(mount_name, &root_path)) { | |
| 315 return base::FilePath(); | |
| 316 } | |
| 317 | |
| 318 return root_path.DirName(); | |
| 319 } | |
| 320 | |
| 321 } // namespace chromeos | |
| OLD | NEW |