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 "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"
17 using storage::ExternalMountPoints
;
18 using storage::FileSystemContext
;
19 using storage::FileSystemURL
;
21 namespace sync_file_system
{
25 const char kSyncableMountName
[] = "syncfs";
26 const char kSyncableMountNameForInternalSync
[] = "syncfs-internal";
28 const base::FilePath::CharType kSyncFileSystemDir
[] =
29 FILE_PATH_LITERAL("Sync FileSystem");
35 void RegisterSyncableFileSystem() {
36 ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
38 storage::kFileSystemTypeSyncable
,
39 storage::FileSystemMountOption(),
41 ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
42 kSyncableMountNameForInternalSync
,
43 storage::kFileSystemTypeSyncableForInternalSync
,
44 storage::FileSystemMountOption(),
48 void RevokeSyncableFileSystem() {
49 ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(
51 ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(
52 kSyncableMountNameForInternalSync
);
55 GURL
GetSyncableFileSystemRootURI(const GURL
& origin
) {
57 storage::GetExternalFileSystemRootURIString(origin
, kSyncableMountName
));
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));
66 return ExternalMountPoints::GetSystemInstance()->CreateExternalFileSystemURL(
67 origin
, kSyncableMountName
, path_for_url
);
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
,
79 bool SerializeSyncableFileSystemURL(const FileSystemURL
& url
,
80 std::string
* serialized_url
) {
81 if (!url
.is_valid() || url
.type() != storage::kFileSystemTypeSyncable
)
84 GetSyncableFileSystemRootURI(url
.origin()).spec() +
85 url
.path().AsUTF8Unsafe();
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
95 FileSystemURL deserialized
=
96 ExternalMountPoints::GetSystemInstance()->CrackURL(GURL(serialized_url
));
97 if (!deserialized
.is_valid() ||
98 deserialized
.type() != storage::kFileSystemTypeSyncable
) {
106 base::FilePath
GetSyncFileSystemDir(const base::FilePath
& profile_base_dir
) {
107 return profile_base_dir
.Append(kSyncFileSystemDir
);
110 void RunSoon(const tracked_objects::Location
& from_here
,
111 const base::Closure
& callback
) {
112 base::MessageLoop::current()->PostTask(from_here
, callback
);
115 base::Closure
NoopClosure() {
116 return base::Bind(&Noop
);
119 } // namespace sync_file_system