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.
5 #include "chrome/browser/sync_file_system/syncable_file_system_util.h"
9 #include "base/command_line.h"
10 #include "base/location.h"
11 #include "base/strings/string_util.h"
12 #include "webkit/browser/fileapi/external_mount_points.h"
13 #include "webkit/browser/fileapi/file_observers.h"
14 #include "webkit/browser/fileapi/file_system_context.h"
15 #include "webkit/common/fileapi/file_system_util.h"
17 using fileapi::ExternalMountPoints
;
18 using fileapi::FileSystemContext
;
19 using fileapi::FileSystemURL
;
21 namespace sync_file_system
{
25 // A command switch to enable syncing directory operations in Sync FileSystem
26 // API. (http://crbug.com/161442)
27 // TODO(kinuko): this command-line switch should be temporary.
28 const char kEnableSyncFSDirectoryOperation
[] =
29 "enable-syncfs-directory-operation";
31 // A command switch to enable V2 Sync FileSystem.
32 const char kEnableSyncFileSystemV2
[] = "enable-syncfs-v2";
34 // A command switch to specify comma-separated app IDs to enable V2 Sync
36 const char kSyncFileSystemV2Whitelist
[] = "syncfs-v2-whitelist";
38 const char kSyncableMountName
[] = "syncfs";
39 const char kSyncableMountNameForInternalSync
[] = "syncfs-internal";
41 const base::FilePath::CharType kSyncFileSystemDir
[] =
42 FILE_PATH_LITERAL("Sync FileSystem");
43 const base::FilePath::CharType kSyncFileSystemDirDev
[] =
44 FILE_PATH_LITERAL("Sync FileSystem Dev");
46 bool is_directory_operation_enabled
= false;
50 void RegisterSyncableFileSystem() {
51 ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
53 fileapi::kFileSystemTypeSyncable
,
54 fileapi::FileSystemMountOption(),
56 ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
57 kSyncableMountNameForInternalSync
,
58 fileapi::kFileSystemTypeSyncableForInternalSync
,
59 fileapi::FileSystemMountOption(),
63 void RevokeSyncableFileSystem() {
64 ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(
66 ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(
67 kSyncableMountNameForInternalSync
);
70 GURL
GetSyncableFileSystemRootURI(const GURL
& origin
) {
71 return GURL(fileapi::GetExternalFileSystemRootURIString(
72 origin
, kSyncableMountName
));
75 FileSystemURL
CreateSyncableFileSystemURL(const GURL
& origin
,
76 const base::FilePath
& path
) {
77 base::FilePath path_for_url
= path
;
78 if (fileapi::VirtualPath::IsAbsolute(path
.value()))
79 path_for_url
= base::FilePath(path
.value().substr(1));
81 return ExternalMountPoints::GetSystemInstance()->CreateExternalFileSystemURL(
82 origin
, kSyncableMountName
, path_for_url
);
85 FileSystemURL
CreateSyncableFileSystemURLForSync(
86 fileapi::FileSystemContext
* file_system_context
,
87 const FileSystemURL
& syncable_url
) {
88 return ExternalMountPoints::GetSystemInstance()->CreateExternalFileSystemURL(
89 syncable_url
.origin(),
90 kSyncableMountNameForInternalSync
,
94 bool SerializeSyncableFileSystemURL(const FileSystemURL
& url
,
95 std::string
* serialized_url
) {
96 if (!url
.is_valid() || url
.type() != fileapi::kFileSystemTypeSyncable
)
99 GetSyncableFileSystemRootURI(url
.origin()).spec() +
100 url
.path().AsUTF8Unsafe();
104 bool DeserializeSyncableFileSystemURL(
105 const std::string
& serialized_url
, FileSystemURL
* url
) {
106 #if !defined(FILE_PATH_USES_WIN_SEPARATORS)
107 DCHECK(serialized_url
.find('\\') == std::string::npos
);
108 #endif // FILE_PATH_USES_WIN_SEPARATORS
110 FileSystemURL deserialized
=
111 ExternalMountPoints::GetSystemInstance()->CrackURL(GURL(serialized_url
));
112 if (!deserialized
.is_valid() ||
113 deserialized
.type() != fileapi::kFileSystemTypeSyncable
) {
121 void SetEnableSyncFSDirectoryOperation(bool flag
) {
122 is_directory_operation_enabled
= flag
;
125 bool IsSyncFSDirectoryOperationEnabled() {
126 return IsSyncFSDirectoryOperationEnabled(GURL());
129 bool IsSyncFSDirectoryOperationEnabled(const GURL
& origin
) {
130 return is_directory_operation_enabled
||
131 CommandLine::ForCurrentProcess()->HasSwitch(
132 kEnableSyncFSDirectoryOperation
) ||
133 IsV2EnabledForOrigin(origin
);
137 return CommandLine::ForCurrentProcess()->HasSwitch(kEnableSyncFileSystemV2
);
140 bool IsV2EnabledForOrigin(const GURL
& origin
) {
144 // Spark release channel.
145 if (origin
.host() == "kcjgcakhgelcejampmijgkjkadfcncjl")
147 // Spark dev channel.
148 if (origin
.host() == "pnoffddplpippgcfjdhbmhkofpnaalpg")
151 CommandLine command_line
= *CommandLine::ForCurrentProcess();
152 if (command_line
.HasSwitch(kSyncFileSystemV2Whitelist
)) {
153 std::string app_ids_string
=
154 command_line
.GetSwitchValueASCII(kSyncFileSystemV2Whitelist
);
155 if (app_ids_string
.find(origin
.host()) == std::string::npos
)
157 std::vector
<std::string
> app_ids
;
158 Tokenize(app_ids_string
, ",", &app_ids
);
159 for (size_t i
= 0; i
< app_ids
.size(); ++i
) {
160 if (origin
.host() == app_ids
[i
])
168 base::FilePath
GetSyncFileSystemDir(const base::FilePath
& profile_base_dir
) {
170 return profile_base_dir
.Append(kSyncFileSystemDir
);
171 if (IsSyncFSDirectoryOperationEnabled())
172 return profile_base_dir
.Append(kSyncFileSystemDirDev
);
173 return profile_base_dir
.Append(kSyncFileSystemDir
);
176 ScopedEnableSyncFSDirectoryOperation::ScopedEnableSyncFSDirectoryOperation() {
177 was_enabled_
= IsSyncFSDirectoryOperationEnabled(GURL());
178 SetEnableSyncFSDirectoryOperation(true);
181 ScopedEnableSyncFSDirectoryOperation::~ScopedEnableSyncFSDirectoryOperation() {
182 DCHECK(IsSyncFSDirectoryOperationEnabled(GURL()));
183 SetEnableSyncFSDirectoryOperation(was_enabled_
);
186 void RunSoon(const tracked_objects::Location
& from_here
,
187 const base::Closure
& callback
) {
188 base::MessageLoop::current()->PostTask(from_here
, callback
);
191 } // namespace sync_file_system