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/single_thread_task_runner.h"
12 #include "base/strings/string_util.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "storage/browser/fileapi/external_mount_points.h"
15 #include "storage/browser/fileapi/file_observers.h"
16 #include "storage/browser/fileapi/file_system_context.h"
17 #include "storage/common/fileapi/file_system_util.h"
19 using storage::ExternalMountPoints
;
20 using storage::FileSystemContext
;
21 using storage::FileSystemURL
;
23 namespace sync_file_system
{
27 const char kSyncableMountName
[] = "syncfs";
28 const char kSyncableMountNameForInternalSync
[] = "syncfs-internal";
30 const base::FilePath::CharType kSyncFileSystemDir
[] =
31 FILE_PATH_LITERAL("Sync FileSystem");
37 void RegisterSyncableFileSystem() {
38 ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
40 storage::kFileSystemTypeSyncable
,
41 storage::FileSystemMountOption(),
43 ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
44 kSyncableMountNameForInternalSync
,
45 storage::kFileSystemTypeSyncableForInternalSync
,
46 storage::FileSystemMountOption(),
50 void RevokeSyncableFileSystem() {
51 ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(
53 ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(
54 kSyncableMountNameForInternalSync
);
57 GURL
GetSyncableFileSystemRootURI(const GURL
& origin
) {
59 storage::GetExternalFileSystemRootURIString(origin
, kSyncableMountName
));
62 FileSystemURL
CreateSyncableFileSystemURL(const GURL
& origin
,
63 const base::FilePath
& path
) {
64 base::FilePath path_for_url
= path
;
65 if (storage::VirtualPath::IsAbsolute(path
.value()))
66 path_for_url
= base::FilePath(path
.value().substr(1));
68 return ExternalMountPoints::GetSystemInstance()->CreateExternalFileSystemURL(
69 origin
, kSyncableMountName
, path_for_url
);
72 FileSystemURL
CreateSyncableFileSystemURLForSync(
73 storage::FileSystemContext
* file_system_context
,
74 const FileSystemURL
& syncable_url
) {
75 return ExternalMountPoints::GetSystemInstance()->CreateExternalFileSystemURL(
76 syncable_url
.origin(),
77 kSyncableMountNameForInternalSync
,
81 bool SerializeSyncableFileSystemURL(const FileSystemURL
& url
,
82 std::string
* serialized_url
) {
83 if (!url
.is_valid() || url
.type() != storage::kFileSystemTypeSyncable
)
86 GetSyncableFileSystemRootURI(url
.origin()).spec() +
87 url
.path().AsUTF8Unsafe();
91 bool DeserializeSyncableFileSystemURL(
92 const std::string
& serialized_url
, FileSystemURL
* url
) {
93 #if !defined(FILE_PATH_USES_WIN_SEPARATORS)
94 DCHECK(serialized_url
.find('\\') == std::string::npos
);
95 #endif // FILE_PATH_USES_WIN_SEPARATORS
97 FileSystemURL deserialized
=
98 ExternalMountPoints::GetSystemInstance()->CrackURL(GURL(serialized_url
));
99 if (!deserialized
.is_valid() ||
100 deserialized
.type() != storage::kFileSystemTypeSyncable
) {
108 base::FilePath
GetSyncFileSystemDir(const base::FilePath
& profile_base_dir
) {
109 return profile_base_dir
.Append(kSyncFileSystemDir
);
112 void RunSoon(const tracked_objects::Location
& from_here
,
113 const base::Closure
& callback
) {
114 base::ThreadTaskRunnerHandle::Get()->PostTask(from_here
, callback
);
117 base::Closure
NoopClosure() {
118 return base::Bind(&Noop
);
121 } // namespace sync_file_system