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/sync_file_system/syncable_file_system_util.h" 6 7 #include "base/command_line.h" 8 #include "webkit/browser/fileapi/external_mount_points.h" 9 #include "webkit/browser/fileapi/file_observers.h" 10 #include "webkit/browser/fileapi/file_system_context.h" 11 #include "webkit/browser/fileapi/sandbox_file_system_backend.h" 12 #include "webkit/common/fileapi/file_system_util.h" 13 14 using fileapi::ExternalMountPoints; 15 using fileapi::FileSystemContext; 16 using fileapi::FileSystemURL; 17 18 namespace sync_file_system { 19 20 namespace { 21 22 // A command switch to enable syncing directory operations in Sync FileSystem 23 // API. (http://crbug.com/161442) 24 // TODO(kinuko): this command-line switch should be temporary. 25 const char kEnableSyncFSDirectoryOperation[] = 26 "enable-syncfs-directory-operation"; 27 28 const char kSyncableMountName[] = "syncfs"; 29 const char kSyncableMountNameForInternalSync[] = "syncfs-internal"; 30 31 bool is_directory_operation_enabled = false; 32 33 } 34 35 void RegisterSyncableFileSystem() { 36 ExternalMountPoints::GetSystemInstance()->RegisterFileSystem( 37 kSyncableMountName, 38 fileapi::kFileSystemTypeSyncable, 39 base::FilePath()); 40 ExternalMountPoints::GetSystemInstance()->RegisterFileSystem( 41 kSyncableMountNameForInternalSync, 42 fileapi::kFileSystemTypeSyncableForInternalSync, 43 base::FilePath()); 44 } 45 46 void RevokeSyncableFileSystem() { 47 ExternalMountPoints::GetSystemInstance()->RevokeFileSystem( 48 kSyncableMountName); 49 ExternalMountPoints::GetSystemInstance()->RevokeFileSystem( 50 kSyncableMountNameForInternalSync); 51 } 52 53 GURL GetSyncableFileSystemRootURI(const GURL& origin) { 54 return GURL(fileapi::GetExternalFileSystemRootURIString( 55 origin, kSyncableMountName)); 56 } 57 58 FileSystemURL CreateSyncableFileSystemURL(const GURL& origin, 59 const base::FilePath& path) { 60 return ExternalMountPoints::GetSystemInstance()->CreateExternalFileSystemURL( 61 origin, kSyncableMountName, path); 62 } 63 64 FileSystemURL CreateSyncableFileSystemURLForSync( 65 fileapi::FileSystemContext* file_system_context, 66 const FileSystemURL& syncable_url) { 67 return ExternalMountPoints::GetSystemInstance()->CreateExternalFileSystemURL( 68 syncable_url.origin(), 69 kSyncableMountNameForInternalSync, 70 syncable_url.path()); 71 } 72 73 bool SerializeSyncableFileSystemURL(const FileSystemURL& url, 74 std::string* serialized_url) { 75 if (!url.is_valid() || url.type() != fileapi::kFileSystemTypeSyncable) 76 return false; 77 *serialized_url = 78 GetSyncableFileSystemRootURI(url.origin()).spec() + 79 url.path().AsUTF8Unsafe(); 80 return true; 81 } 82 83 bool DeserializeSyncableFileSystemURL( 84 const std::string& serialized_url, FileSystemURL* url) { 85 #if !defined(FILE_PATH_USES_WIN_SEPARATORS) 86 DCHECK(serialized_url.find('\\') == std::string::npos); 87 #endif // FILE_PATH_USES_WIN_SEPARATORS 88 89 FileSystemURL deserialized = 90 ExternalMountPoints::GetSystemInstance()->CrackURL(GURL(serialized_url)); 91 if (!deserialized.is_valid() || 92 deserialized.type() != fileapi::kFileSystemTypeSyncable) { 93 return false; 94 } 95 96 *url = deserialized; 97 return true; 98 } 99 100 void SetEnableSyncFSDirectoryOperation(bool flag) { 101 is_directory_operation_enabled = flag; 102 } 103 104 bool IsSyncFSDirectoryOperationEnabled() { 105 return is_directory_operation_enabled || 106 CommandLine::ForCurrentProcess()->HasSwitch( 107 kEnableSyncFSDirectoryOperation); 108 } 109 110 ScopedEnableSyncFSDirectoryOperation::ScopedEnableSyncFSDirectoryOperation() { 111 was_enabled_ = IsSyncFSDirectoryOperationEnabled(); 112 SetEnableSyncFSDirectoryOperation(true); 113 } 114 115 ScopedEnableSyncFSDirectoryOperation::~ScopedEnableSyncFSDirectoryOperation() { 116 DCHECK(IsSyncFSDirectoryOperationEnabled()); 117 SetEnableSyncFSDirectoryOperation(was_enabled_); 118 } 119 120 } // namespace sync_file_system 121