syncable_file_system_util.cc revision d0247b1b59f9c528cb6df88b4f2b9afaf80d181e
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 "webkit/browser/fileapi/external_mount_points.h"
9#include "webkit/browser/fileapi/file_observers.h"
10#include "webkit/browser/fileapi/file_system_context.h"
11#include "webkit/browser/fileapi/sandbox_file_system_backend.h"
12#include "webkit/common/fileapi/file_system_util.h"
13
14using fileapi::ExternalMountPoints;
15using fileapi::FileSystemContext;
16using fileapi::FileSystemURL;
17
18namespace sync_file_system {
19
20namespace {
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.
25const char kEnableSyncFSDirectoryOperation[] =
26    "enable-syncfs-directory-operation";
27
28const char kSyncableMountName[] = "syncfs";
29const char kSyncableMountNameForInternalSync[] = "syncfs-internal";
30
31const base::FilePath::CharType kSyncFileSystemDir[] =
32    FILE_PATH_LITERAL("Sync FileSystem");
33const base::FilePath::CharType kSyncFileSystemDirDev[] =
34    FILE_PATH_LITERAL("Sync FileSystem Dev");
35
36bool is_directory_operation_enabled = false;
37
38}  // namespace
39
40void 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
51void RevokeSyncableFileSystem() {
52  ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(
53      kSyncableMountName);
54  ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(
55      kSyncableMountNameForInternalSync);
56}
57
58GURL GetSyncableFileSystemRootURI(const GURL& origin) {
59  return GURL(fileapi::GetExternalFileSystemRootURIString(
60      origin, kSyncableMountName));
61}
62
63FileSystemURL CreateSyncableFileSystemURL(const GURL& origin,
64                                          const base::FilePath& path) {
65  return ExternalMountPoints::GetSystemInstance()->CreateExternalFileSystemURL(
66      origin, kSyncableMountName, path);
67}
68
69FileSystemURL 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
78bool 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
88bool 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
105void SetEnableSyncFSDirectoryOperation(bool flag) {
106  is_directory_operation_enabled = flag;
107}
108
109bool IsSyncFSDirectoryOperationEnabled() {
110  return is_directory_operation_enabled ||
111      CommandLine::ForCurrentProcess()->HasSwitch(
112          kEnableSyncFSDirectoryOperation);
113}
114
115base::FilePath GetSyncFileSystemDir(const base::FilePath& profile_base_dir) {
116  return profile_base_dir.Append(
117      IsSyncFSDirectoryOperationEnabled() ? kSyncFileSystemDirDev
118                                          : kSyncFileSystemDir);
119}
120
121ScopedEnableSyncFSDirectoryOperation::ScopedEnableSyncFSDirectoryOperation() {
122  was_enabled_ = IsSyncFSDirectoryOperationEnabled();
123  SetEnableSyncFSDirectoryOperation(true);
124}
125
126ScopedEnableSyncFSDirectoryOperation::~ScopedEnableSyncFSDirectoryOperation() {
127  DCHECK(IsSyncFSDirectoryOperationEnabled());
128  SetEnableSyncFSDirectoryOperation(was_enabled_);
129}
130
131}  // namespace sync_file_system
132