- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync_file_system / syncable_file_system_util.cc
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 "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"
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 const base::FilePath::CharType kSyncFileSystemDir[] =
32     FILE_PATH_LITERAL("Sync FileSystem");
33 const base::FilePath::CharType kSyncFileSystemDirDev[] =
34     FILE_PATH_LITERAL("Sync FileSystem Dev");
35
36 bool is_directory_operation_enabled = false;
37
38 }  // namespace
39
40 void RegisterSyncableFileSystem() {
41   ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
42       kSyncableMountName,
43       fileapi::kFileSystemTypeSyncable,
44       base::FilePath());
45   ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
46       kSyncableMountNameForInternalSync,
47       fileapi::kFileSystemTypeSyncableForInternalSync,
48       base::FilePath());
49 }
50
51 void RevokeSyncableFileSystem() {
52   ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(
53       kSyncableMountName);
54   ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(
55       kSyncableMountNameForInternalSync);
56 }
57
58 GURL GetSyncableFileSystemRootURI(const GURL& origin) {
59   return GURL(fileapi::GetExternalFileSystemRootURIString(
60       origin, kSyncableMountName));
61 }
62
63 FileSystemURL CreateSyncableFileSystemURL(const GURL& origin,
64                                           const base::FilePath& path) {
65   return ExternalMountPoints::GetSystemInstance()->CreateExternalFileSystemURL(
66       origin, kSyncableMountName, path);
67 }
68
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,
75       syncable_url.path());
76 }
77
78 bool SerializeSyncableFileSystemURL(const FileSystemURL& url,
79                                     std::string* serialized_url) {
80   if (!url.is_valid() || url.type() != fileapi::kFileSystemTypeSyncable)
81     return false;
82   *serialized_url =
83       GetSyncableFileSystemRootURI(url.origin()).spec() +
84       url.path().AsUTF8Unsafe();
85   return true;
86 }
87
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
93
94   FileSystemURL deserialized =
95       ExternalMountPoints::GetSystemInstance()->CrackURL(GURL(serialized_url));
96   if (!deserialized.is_valid() ||
97       deserialized.type() != fileapi::kFileSystemTypeSyncable) {
98     return false;
99   }
100
101   *url = deserialized;
102   return true;
103 }
104
105 void SetEnableSyncFSDirectoryOperation(bool flag) {
106   is_directory_operation_enabled = flag;
107 }
108
109 bool IsSyncFSDirectoryOperationEnabled() {
110   return is_directory_operation_enabled ||
111       CommandLine::ForCurrentProcess()->HasSwitch(
112           kEnableSyncFSDirectoryOperation);
113 }
114
115 base::FilePath GetSyncFileSystemDir(const base::FilePath& profile_base_dir) {
116   return profile_base_dir.Append(
117       IsSyncFSDirectoryOperationEnabled() ? kSyncFileSystemDirDev
118                                           : kSyncFileSystemDir);
119 }
120
121 ScopedEnableSyncFSDirectoryOperation::ScopedEnableSyncFSDirectoryOperation() {
122   was_enabled_ = IsSyncFSDirectoryOperationEnabled();
123   SetEnableSyncFSDirectoryOperation(true);
124 }
125
126 ScopedEnableSyncFSDirectoryOperation::~ScopedEnableSyncFSDirectoryOperation() {
127   DCHECK(IsSyncFSDirectoryOperationEnabled());
128   SetEnableSyncFSDirectoryOperation(was_enabled_);
129 }
130
131 void RunSoon(const tracked_objects::Location& from_here,
132              const base::Closure& callback) {
133   base::MessageLoop::current()->PostTask(from_here, callback);
134 }
135
136 }  // namespace sync_file_system