Upstream version 11.40.277.0
[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 <vector>
8
9 #include "base/command_line.h"
10 #include "base/location.h"
11 #include "base/strings/string_util.h"
12 #include "storage/browser/fileapi/external_mount_points.h"
13 #include "storage/browser/fileapi/file_observers.h"
14 #include "storage/browser/fileapi/file_system_context.h"
15 #include "storage/common/fileapi/file_system_util.h"
16
17 using storage::ExternalMountPoints;
18 using storage::FileSystemContext;
19 using storage::FileSystemURL;
20
21 namespace sync_file_system {
22
23 namespace {
24
25 const char kSyncableMountName[] = "syncfs";
26 const char kSyncableMountNameForInternalSync[] = "syncfs-internal";
27
28 const base::FilePath::CharType kSyncFileSystemDir[] =
29     FILE_PATH_LITERAL("Sync FileSystem");
30
31 void Noop() {}
32
33 }  // namespace
34
35 void RegisterSyncableFileSystem() {
36   ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
37       kSyncableMountName,
38       storage::kFileSystemTypeSyncable,
39       storage::FileSystemMountOption(),
40       base::FilePath());
41   ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
42       kSyncableMountNameForInternalSync,
43       storage::kFileSystemTypeSyncableForInternalSync,
44       storage::FileSystemMountOption(),
45       base::FilePath());
46 }
47
48 void RevokeSyncableFileSystem() {
49   ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(
50       kSyncableMountName);
51   ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(
52       kSyncableMountNameForInternalSync);
53 }
54
55 GURL GetSyncableFileSystemRootURI(const GURL& origin) {
56   return GURL(
57       storage::GetExternalFileSystemRootURIString(origin, kSyncableMountName));
58 }
59
60 FileSystemURL CreateSyncableFileSystemURL(const GURL& origin,
61                                           const base::FilePath& path) {
62   base::FilePath path_for_url = path;
63   if (storage::VirtualPath::IsAbsolute(path.value()))
64     path_for_url = base::FilePath(path.value().substr(1));
65
66   return ExternalMountPoints::GetSystemInstance()->CreateExternalFileSystemURL(
67       origin, kSyncableMountName, path_for_url);
68 }
69
70 FileSystemURL CreateSyncableFileSystemURLForSync(
71     storage::FileSystemContext* file_system_context,
72     const FileSystemURL& syncable_url) {
73   return ExternalMountPoints::GetSystemInstance()->CreateExternalFileSystemURL(
74       syncable_url.origin(),
75       kSyncableMountNameForInternalSync,
76       syncable_url.path());
77 }
78
79 bool SerializeSyncableFileSystemURL(const FileSystemURL& url,
80                                     std::string* serialized_url) {
81   if (!url.is_valid() || url.type() != storage::kFileSystemTypeSyncable)
82     return false;
83   *serialized_url =
84       GetSyncableFileSystemRootURI(url.origin()).spec() +
85       url.path().AsUTF8Unsafe();
86   return true;
87 }
88
89 bool DeserializeSyncableFileSystemURL(
90     const std::string& serialized_url, FileSystemURL* url) {
91 #if !defined(FILE_PATH_USES_WIN_SEPARATORS)
92   DCHECK(serialized_url.find('\\') == std::string::npos);
93 #endif  // FILE_PATH_USES_WIN_SEPARATORS
94
95   FileSystemURL deserialized =
96       ExternalMountPoints::GetSystemInstance()->CrackURL(GURL(serialized_url));
97   if (!deserialized.is_valid() ||
98       deserialized.type() != storage::kFileSystemTypeSyncable) {
99     return false;
100   }
101
102   *url = deserialized;
103   return true;
104 }
105
106 base::FilePath GetSyncFileSystemDir(const base::FilePath& profile_base_dir) {
107   return profile_base_dir.Append(kSyncFileSystemDir);
108 }
109
110 void RunSoon(const tracked_objects::Location& from_here,
111              const base::Closure& callback) {
112   base::MessageLoop::current()->PostTask(from_here, callback);
113 }
114
115 base::Closure NoopClosure() {
116   return base::Bind(&Noop);
117 }
118
119 }  // namespace sync_file_system