| 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/file_access_permissions.h" | |
| 15 #include "chrome/browser/chromeos/fileapi/remote_file_stream_writer.h" | |
| 16 #include "chrome/browser/chromeos/fileapi/remote_file_system_operation.h" | |
| 17 #include "chromeos/dbus/cros_disks_client.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 scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy, | |
| 50 scoped_refptr<fileapi::ExternalMountPoints> mount_points, | |
| 51 fileapi::ExternalMountPoints* system_mount_points) | |
| 52 : special_storage_policy_(special_storage_policy), | |
| 53 file_access_permissions_(new FileAccessPermissions()), | |
| 54 local_file_util_(new fileapi::AsyncFileUtilAdapter( | |
| 55 new fileapi::IsolatedFileUtil())), | |
| 56 mount_points_(mount_points), | |
| 57 system_mount_points_(system_mount_points) { | |
| 58 } | |
| 59 | |
| 60 CrosMountPointProvider::~CrosMountPointProvider() { | |
| 61 } | |
| 62 | |
| 63 void CrosMountPointProvider::AddSystemMountPoints() { | |
| 64 // RegisterFileSystem() is no-op if the mount point with the same name | |
| 65 // already exists, hence it's safe to call without checking if a mount | |
| 66 // point already exists or not. | |
| 67 | |
| 68 // TODO(satorux): "Downloads" directory should probably be per-profile. For | |
| 69 // this to be per-profile, a unique directory path should be chosen per | |
| 70 // profile, and the mount point should be added to | |
| 71 // mount_points_. crbug.com/247236 | |
| 72 base::FilePath home_path; | |
| 73 if (PathService::Get(base::DIR_HOME, &home_path)) { | |
| 74 system_mount_points_->RegisterFileSystem( | |
| 75 "Downloads", | |
| 76 fileapi::kFileSystemTypeNativeLocal, | |
| 77 home_path.AppendASCII("Downloads")); | |
| 78 } | |
| 79 | |
| 80 system_mount_points_->RegisterFileSystem( | |
| 81 "archive", | |
| 82 fileapi::kFileSystemTypeNativeLocal, | |
| 83 chromeos::CrosDisksClient::GetArchiveMountPoint()); | |
| 84 system_mount_points_->RegisterFileSystem( | |
| 85 "removable", | |
| 86 fileapi::kFileSystemTypeNativeLocal, | |
| 87 chromeos::CrosDisksClient::GetRemovableDiskMountPoint()); | |
| 88 system_mount_points_->RegisterFileSystem( | |
| 89 "oem", | |
| 90 fileapi::kFileSystemTypeRestrictedNativeLocal, | |
| 91 base::FilePath(FILE_PATH_LITERAL("/usr/share/oem"))); | |
| 92 } | |
| 93 | |
| 94 bool CrosMountPointProvider::CanHandleType(fileapi::FileSystemType type) const { | |
| 95 switch (type) { | |
| 96 case fileapi::kFileSystemTypeExternal: | |
| 97 case fileapi::kFileSystemTypeDrive: | |
| 98 case fileapi::kFileSystemTypeRestrictedNativeLocal: | |
| 99 case fileapi::kFileSystemTypeNativeLocal: | |
| 100 case fileapi::kFileSystemTypeNativeForPlatformApp: | |
| 101 return true; | |
| 102 default: | |
| 103 return false; | |
| 104 } | |
| 105 } | |
| 106 | |
| 107 void CrosMountPointProvider::OpenFileSystem( | |
| 108 const GURL& origin_url, | |
| 109 fileapi::FileSystemType type, | |
| 110 fileapi::OpenFileSystemMode mode, | |
| 111 const OpenFileSystemCallback& callback) { | |
| 112 DCHECK(fileapi::IsolatedContext::IsIsolatedType(type)); | |
| 113 // Nothing to validate for external filesystem. | |
| 114 callback.Run(base::PLATFORM_FILE_OK); | |
| 115 } | |
| 116 | |
| 117 fileapi::FileSystemQuotaUtil* CrosMountPointProvider::GetQuotaUtil() { | |
| 118 // No quota support. | |
| 119 return NULL; | |
| 120 } | |
| 121 | |
| 122 void CrosMountPointProvider::DeleteFileSystem( | |
| 123 const GURL& origin_url, | |
| 124 fileapi::FileSystemType type, | |
| 125 fileapi::FileSystemContext* context, | |
| 126 const DeleteFileSystemCallback& callback) { | |
| 127 NOTREACHED(); | |
| 128 callback.Run(base::PLATFORM_FILE_ERROR_INVALID_OPERATION); | |
| 129 } | |
| 130 | |
| 131 bool CrosMountPointProvider::IsAccessAllowed( | |
| 132 const fileapi::FileSystemURL& url) const { | |
| 133 if (!url.is_valid()) | |
| 134 return false; | |
| 135 | |
| 136 // Permit access to mount points from internal WebUI. | |
| 137 const GURL& origin_url = url.origin(); | |
| 138 if (origin_url.SchemeIs(kChromeUIScheme)) | |
| 139 return true; | |
| 140 | |
| 141 // No extra check is needed for isolated file systems. | |
| 142 if (url.mount_type() == fileapi::kFileSystemTypeIsolated) | |
| 143 return true; | |
| 144 | |
| 145 if (!CanHandleURL(url)) | |
| 146 return false; | |
| 147 | |
| 148 std::string extension_id = origin_url.host(); | |
| 149 // Check first to make sure this extension has fileBrowserHander permissions. | |
| 150 if (!special_storage_policy_->IsFileHandler(extension_id)) | |
| 151 return false; | |
| 152 | |
| 153 return file_access_permissions_->HasAccessPermission(extension_id, | |
| 154 url.virtual_path()); | |
| 155 } | |
| 156 | |
| 157 void CrosMountPointProvider::GrantFullAccessToExtension( | |
| 158 const std::string& extension_id) { | |
| 159 DCHECK(special_storage_policy_->IsFileHandler(extension_id)); | |
| 160 if (!special_storage_policy_->IsFileHandler(extension_id)) | |
| 161 return; | |
| 162 | |
| 163 std::vector<fileapi::MountPoints::MountPointInfo> files; | |
| 164 mount_points_->AddMountPointInfosTo(&files); | |
| 165 system_mount_points_->AddMountPointInfosTo(&files); | |
| 166 | |
| 167 for (size_t i = 0; i < files.size(); ++i) { | |
| 168 file_access_permissions_->GrantAccessPermission( | |
| 169 extension_id, | |
| 170 base::FilePath::FromUTF8Unsafe(files[i].name)); | |
| 171 } | |
| 172 } | |
| 173 | |
| 174 void CrosMountPointProvider::GrantFileAccessToExtension( | |
| 175 const std::string& extension_id, const base::FilePath& virtual_path) { | |
| 176 // All we care about here is access from extensions for now. | |
| 177 DCHECK(special_storage_policy_->IsFileHandler(extension_id)); | |
| 178 if (!special_storage_policy_->IsFileHandler(extension_id)) | |
| 179 return; | |
| 180 | |
| 181 std::string id; | |
| 182 fileapi::FileSystemType type; | |
| 183 base::FilePath path; | |
| 184 if (!mount_points_->CrackVirtualPath(virtual_path, &id, &type, &path) && | |
| 185 !system_mount_points_->CrackVirtualPath(virtual_path, | |
| 186 &id, &type, &path)) { | |
| 187 return; | |
| 188 } | |
| 189 | |
| 190 if (type == fileapi::kFileSystemTypeRestrictedNativeLocal) { | |
| 191 LOG(ERROR) << "Can't grant access for restricted mount point"; | |
| 192 return; | |
| 193 } | |
| 194 | |
| 195 file_access_permissions_->GrantAccessPermission(extension_id, virtual_path); | |
| 196 } | |
| 197 | |
| 198 void CrosMountPointProvider::RevokeAccessForExtension( | |
| 199 const std::string& extension_id) { | |
| 200 file_access_permissions_->RevokePermissions(extension_id); | |
| 201 } | |
| 202 | |
| 203 std::vector<base::FilePath> CrosMountPointProvider::GetRootDirectories() const { | |
| 204 std::vector<fileapi::MountPoints::MountPointInfo> mount_points; | |
| 205 mount_points_->AddMountPointInfosTo(&mount_points); | |
| 206 system_mount_points_->AddMountPointInfosTo(&mount_points); | |
| 207 | |
| 208 std::vector<base::FilePath> root_dirs; | |
| 209 for (size_t i = 0; i < mount_points.size(); ++i) | |
| 210 root_dirs.push_back(mount_points[i].path); | |
| 211 return root_dirs; | |
| 212 } | |
| 213 | |
| 214 fileapi::FileSystemFileUtil* CrosMountPointProvider::GetFileUtil( | |
| 215 fileapi::FileSystemType type) { | |
| 216 DCHECK(type == fileapi::kFileSystemTypeNativeLocal || | |
| 217 type == fileapi::kFileSystemTypeRestrictedNativeLocal); | |
| 218 return local_file_util_->sync_file_util(); | |
| 219 } | |
| 220 | |
| 221 fileapi::AsyncFileUtil* CrosMountPointProvider::GetAsyncFileUtil( | |
| 222 fileapi::FileSystemType type) { | |
| 223 DCHECK(type == fileapi::kFileSystemTypeNativeLocal || | |
| 224 type == fileapi::kFileSystemTypeRestrictedNativeLocal); | |
| 225 return local_file_util_.get(); | |
| 226 } | |
| 227 | |
| 228 fileapi::CopyOrMoveFileValidatorFactory* | |
| 229 CrosMountPointProvider::GetCopyOrMoveFileValidatorFactory( | |
| 230 fileapi::FileSystemType type, base::PlatformFileError* error_code) { | |
| 231 DCHECK(error_code); | |
| 232 *error_code = base::PLATFORM_FILE_OK; | |
| 233 return NULL; | |
| 234 } | |
| 235 | |
| 236 fileapi::FileSystemOperation* CrosMountPointProvider::CreateFileSystemOperation( | |
| 237 const fileapi::FileSystemURL& url, | |
| 238 fileapi::FileSystemContext* context, | |
| 239 base::PlatformFileError* error_code) const { | |
| 240 DCHECK(url.is_valid()); | |
| 241 | |
| 242 if (!IsAccessAllowed(url)) { | |
| 243 *error_code = base::PLATFORM_FILE_ERROR_SECURITY; | |
| 244 return NULL; | |
| 245 } | |
| 246 | |
| 247 if (url.type() == fileapi::kFileSystemTypeDrive) { | |
| 248 fileapi::RemoteFileSystemProxyInterface* remote_proxy = | |
| 249 GetRemoteProxy(url.filesystem_id()); | |
| 250 if (!remote_proxy) { | |
| 251 *error_code = base::PLATFORM_FILE_ERROR_NOT_FOUND; | |
| 252 return NULL; | |
| 253 } | |
| 254 return new RemoteFileSystemOperation(remote_proxy); | |
| 255 } | |
| 256 | |
| 257 DCHECK(url.type() == fileapi::kFileSystemTypeNativeLocal || | |
| 258 url.type() == fileapi::kFileSystemTypeRestrictedNativeLocal); | |
| 259 scoped_ptr<fileapi::FileSystemOperationContext> operation_context( | |
| 260 new fileapi::FileSystemOperationContext(context)); | |
| 261 operation_context->set_root_path(GetFileSystemRootPath(url)); | |
| 262 return new fileapi::LocalFileSystemOperation(url, context, | |
| 263 operation_context.Pass()); | |
| 264 } | |
| 265 | |
| 266 scoped_ptr<webkit_blob::FileStreamReader> | |
| 267 CrosMountPointProvider::CreateFileStreamReader( | |
| 268 const fileapi::FileSystemURL& url, | |
| 269 int64 offset, | |
| 270 const base::Time& expected_modification_time, | |
| 271 fileapi::FileSystemContext* context) const { | |
| 272 DCHECK(url.is_valid()); | |
| 273 | |
| 274 if (!IsAccessAllowed(url)) | |
| 275 return scoped_ptr<webkit_blob::FileStreamReader>(); | |
| 276 | |
| 277 if (url.type() == fileapi::kFileSystemTypeDrive) { | |
| 278 fileapi::RemoteFileSystemProxyInterface* remote_proxy = | |
| 279 GetRemoteProxy(url.filesystem_id()); | |
| 280 if (!remote_proxy) | |
| 281 return scoped_ptr<webkit_blob::FileStreamReader>(); | |
| 282 return remote_proxy->CreateFileStreamReader( | |
| 283 context->task_runners()->file_task_runner(), | |
| 284 url, offset, expected_modification_time); | |
| 285 } | |
| 286 | |
| 287 return scoped_ptr<webkit_blob::FileStreamReader>( | |
| 288 new fileapi::FileSystemFileStreamReader( | |
| 289 context, url, offset, expected_modification_time)); | |
| 290 } | |
| 291 | |
| 292 scoped_ptr<fileapi::FileStreamWriter> | |
| 293 CrosMountPointProvider::CreateFileStreamWriter( | |
| 294 const fileapi::FileSystemURL& url, | |
| 295 int64 offset, | |
| 296 fileapi::FileSystemContext* context) const { | |
| 297 DCHECK(url.is_valid()); | |
| 298 | |
| 299 if (!IsAccessAllowed(url)) | |
| 300 return scoped_ptr<fileapi::FileStreamWriter>(); | |
| 301 | |
| 302 if (url.type() == fileapi::kFileSystemTypeDrive) { | |
| 303 fileapi::RemoteFileSystemProxyInterface* remote_proxy = | |
| 304 GetRemoteProxy(url.filesystem_id()); | |
| 305 if (!remote_proxy) | |
| 306 return scoped_ptr<fileapi::FileStreamWriter>(); | |
| 307 return scoped_ptr<fileapi::FileStreamWriter>( | |
| 308 new RemoteFileStreamWriter( | |
| 309 remote_proxy, url, offset, | |
| 310 context->task_runners()->file_task_runner())); | |
| 311 } | |
| 312 | |
| 313 if (url.type() == fileapi::kFileSystemTypeRestrictedNativeLocal) | |
| 314 return scoped_ptr<fileapi::FileStreamWriter>(); | |
| 315 | |
| 316 DCHECK(url.type() == fileapi::kFileSystemTypeNativeLocal); | |
| 317 return scoped_ptr<fileapi::FileStreamWriter>( | |
| 318 new fileapi::LocalFileStreamWriter( | |
| 319 context->task_runners()->file_task_runner(), url.path(), offset)); | |
| 320 } | |
| 321 | |
| 322 bool CrosMountPointProvider::GetVirtualPath( | |
| 323 const base::FilePath& filesystem_path, | |
| 324 base::FilePath* virtual_path) { | |
| 325 return mount_points_->GetVirtualPath(filesystem_path, virtual_path) || | |
| 326 system_mount_points_->GetVirtualPath(filesystem_path, virtual_path); | |
| 327 } | |
| 328 | |
| 329 fileapi::RemoteFileSystemProxyInterface* CrosMountPointProvider::GetRemoteProxy( | |
| 330 const std::string& mount_name) const { | |
| 331 fileapi::RemoteFileSystemProxyInterface* proxy = | |
| 332 mount_points_->GetRemoteFileSystemProxy(mount_name); | |
| 333 if (proxy) | |
| 334 return proxy; | |
| 335 return system_mount_points_->GetRemoteFileSystemProxy(mount_name); | |
| 336 } | |
| 337 | |
| 338 base::FilePath CrosMountPointProvider::GetFileSystemRootPath( | |
| 339 const fileapi::FileSystemURL& url) const { | |
| 340 DCHECK(fileapi::IsolatedContext::IsIsolatedType(url.mount_type())); | |
| 341 if (!url.is_valid()) | |
| 342 return base::FilePath(); | |
| 343 | |
| 344 base::FilePath root_path; | |
| 345 std::string mount_name = url.filesystem_id(); | |
| 346 if (!mount_points_->GetRegisteredPath(mount_name, &root_path) && | |
| 347 !system_mount_points_->GetRegisteredPath(mount_name, &root_path)) { | |
| 348 return base::FilePath(); | |
| 349 } | |
| 350 | |
| 351 return root_path.DirName(); | |
| 352 } | |
| 353 | |
| 354 } // namespace chromeos | |
| OLD | NEW |