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"
7 #include "base/command_line.h"
8 #include "base/location.h"
9 #include "webkit/browser/fileapi/external_mount_points.h"
10 #include "webkit/browser/fileapi/file_observers.h"
11 #include "webkit/browser/fileapi/file_system_context.h"
12 #include "webkit/common/fileapi/file_system_util.h"
14 using fileapi::ExternalMountPoints;
15 using fileapi::FileSystemContext;
16 using fileapi::FileSystemURL;
18 namespace sync_file_system {
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";
28 const char kSyncableMountName[] = "syncfs";
29 const char kSyncableMountNameForInternalSync[] = "syncfs-internal";
31 const base::FilePath::CharType kSyncFileSystemDir[] =
32 FILE_PATH_LITERAL("Sync FileSystem");
33 const base::FilePath::CharType kSyncFileSystemDirDev[] =
34 FILE_PATH_LITERAL("Sync FileSystem Dev");
36 bool is_directory_operation_enabled = false;
40 void RegisterSyncableFileSystem() {
41 ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
43 fileapi::kFileSystemTypeSyncable,
45 ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
46 kSyncableMountNameForInternalSync,
47 fileapi::kFileSystemTypeSyncableForInternalSync,
51 void RevokeSyncableFileSystem() {
52 ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(
54 ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(
55 kSyncableMountNameForInternalSync);
58 GURL GetSyncableFileSystemRootURI(const GURL& origin) {
59 return GURL(fileapi::GetExternalFileSystemRootURIString(
60 origin, kSyncableMountName));
63 FileSystemURL CreateSyncableFileSystemURL(const GURL& origin,
64 const base::FilePath& path) {
65 return ExternalMountPoints::GetSystemInstance()->CreateExternalFileSystemURL(
66 origin, kSyncableMountName, path);
69 FileSystemURL CreateSyncableFileSystemURLForSync(
70 fileapi::FileSystemContext* file_system_context,
71 const FileSystemURL& syncable_url) {
72 return ExternalMountPoints::GetSystemInstance()->CreateExternalFileSystemURL(
73 syncable_url.origin(),
74 kSyncableMountNameForInternalSync,
78 bool SerializeSyncableFileSystemURL(const FileSystemURL& url,
79 std::string* serialized_url) {
80 if (!url.is_valid() || url.type() != fileapi::kFileSystemTypeSyncable)
83 GetSyncableFileSystemRootURI(url.origin()).spec() +
84 url.path().AsUTF8Unsafe();
88 bool DeserializeSyncableFileSystemURL(
89 const std::string& serialized_url, FileSystemURL* url) {
90 #if !defined(FILE_PATH_USES_WIN_SEPARATORS)
91 DCHECK(serialized_url.find('\\') == std::string::npos);
92 #endif // FILE_PATH_USES_WIN_SEPARATORS
94 FileSystemURL deserialized =
95 ExternalMountPoints::GetSystemInstance()->CrackURL(GURL(serialized_url));
96 if (!deserialized.is_valid() ||
97 deserialized.type() != fileapi::kFileSystemTypeSyncable) {
105 void SetEnableSyncFSDirectoryOperation(bool flag) {
106 is_directory_operation_enabled = flag;
109 bool IsSyncFSDirectoryOperationEnabled() {
110 return is_directory_operation_enabled ||
111 CommandLine::ForCurrentProcess()->HasSwitch(
112 kEnableSyncFSDirectoryOperation);
115 base::FilePath GetSyncFileSystemDir(const base::FilePath& profile_base_dir) {
116 return profile_base_dir.Append(
117 IsSyncFSDirectoryOperationEnabled() ? kSyncFileSystemDirDev
118 : kSyncFileSystemDir);
121 ScopedEnableSyncFSDirectoryOperation::ScopedEnableSyncFSDirectoryOperation() {
122 was_enabled_ = IsSyncFSDirectoryOperationEnabled();
123 SetEnableSyncFSDirectoryOperation(true);
126 ScopedEnableSyncFSDirectoryOperation::~ScopedEnableSyncFSDirectoryOperation() {
127 DCHECK(IsSyncFSDirectoryOperationEnabled());
128 SetEnableSyncFSDirectoryOperation(was_enabled_);
131 void RunSoon(const tracked_objects::Location& from_here,
132 const base::Closure& callback) {
133 base::MessageLoop::current()->PostTask(from_here, callback);
136 } // namespace sync_file_system