syncable_file_system_util.h revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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#ifndef CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNCABLE_FILE_SYSTEM_UTIL_H_
6#define CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNCABLE_FILE_SYSTEM_UTIL_H_
7
8#include <string>
9
10#include "base/callback_forward.h"
11#include "base/files/file_path.h"
12#include "webkit/browser/fileapi/file_system_url.h"
13
14namespace fileapi {
15class FileSystemContext;
16class FileSystemURL;
17}
18
19namespace tracked_objects {
20class Location;
21}
22
23namespace sync_file_system {
24
25// Registers a syncable filesystem.
26void RegisterSyncableFileSystem();
27
28// Revokes the syncable filesystem.
29void RevokeSyncableFileSystem();
30
31// Returns the root URI of the syncable filesystem for |origin|.
32GURL GetSyncableFileSystemRootURI(const GURL& origin);
33
34// Creates a FileSystem URL for the |path| in a syncable filesystem for
35// |origin|.
36//
37// Example: Assume following arguments are given:
38//   origin: 'http://www.example.com/',
39//   path: '/foo/bar',
40// returns 'filesystem:http://www.example.com/external/syncfs/foo/bar'
41fileapi::FileSystemURL
42CreateSyncableFileSystemURL(const GURL& origin, const base::FilePath& path);
43
44// Creates a special filesystem URL for synchronizing |syncable_url|.
45fileapi::FileSystemURL CreateSyncableFileSystemURLForSync(
46    fileapi::FileSystemContext* file_system_context,
47    const fileapi::FileSystemURL& syncable_url);
48
49// Serializes a given FileSystemURL |url| and sets the serialized string to
50// |serialized_url|. If the URL does not represent a syncable filesystem,
51// |serialized_url| is not filled in, and returns false. Separators of the
52// path will be normalized depending on its platform.
53//
54// Example: Assume a following FileSystemURL object is given:
55//   origin() returns 'http://www.example.com/',
56//   type() returns the kFileSystemTypeSyncable,
57//   filesystem_id() returns 'syncfs',
58//   path() returns '/foo/bar',
59// this URL will be serialized to
60// (on Windows)
61//   'filesystem:http://www.example.com/external/syncfs/foo\\bar'
62// (on others)
63//   'filesystem:http://www.example.com/external/syncfs/foo/bar'
64bool SerializeSyncableFileSystemURL(
65    const fileapi::FileSystemURL& url, std::string* serialized_url);
66
67// Deserializes a serialized FileSystem URL string |serialized_url| and sets the
68// deserialized value to |url|. If the reconstructed object is invalid or does
69// not represent a syncable filesystem, returns false.
70//
71// NOTE: On any platform other than Windows, this function assumes that
72// |serialized_url| does not contain '\\'. If it contains '\\' on such
73// platforms, '\\' may be replaced with '/' (It would not be an expected
74// behavior).
75//
76// See the comment of SerializeSyncableFileSystemURL() for more details.
77bool DeserializeSyncableFileSystemURL(
78    const std::string& serialized_url, fileapi::FileSystemURL* url);
79
80// Enables or disables directory operations in Sync FileSystem API.
81// TODO(nhiroki): This method should be used only for testing and should go
82// away when we fully support directory operations. (http://crbug.com/161442)
83void SetEnableSyncFSDirectoryOperation(bool flag);
84
85// Returns true if we allow directory operations in Sync FileSystem API.
86// It is disabled by default but can be overridden by a command-line switch
87// (--enable-syncfs-directory-operations) or by calling
88// SetEnableSyncFSDirectoryOperation().
89// TODO(nhiroki): This method should be used only for testing and should go
90// away when we fully support directory operations. (http://crbug.com/161442)
91bool IsSyncFSDirectoryOperationEnabled();
92
93// Checks the same as above, but takes |origin| and sees if directory operation
94// is enabled specifically for this |origin|.
95bool IsSyncFSDirectoryOperationEnabled(const GURL& origin);
96
97// Returns true if V2 is enabled.
98bool IsV2Enabled();
99
100// Returns true if the given |origin| is supposed to run in V2 mode.
101bool IsV2EnabledForOrigin(const GURL& origin);
102
103// Returns SyncFileSystem sub-directory path.
104base::FilePath GetSyncFileSystemDir(const base::FilePath& profile_base_dir);
105
106// Enables directory operation for syncable filesystems temporarily for testing.
107class ScopedEnableSyncFSDirectoryOperation {
108 public:
109  ScopedEnableSyncFSDirectoryOperation();
110  ~ScopedEnableSyncFSDirectoryOperation();
111
112 private:
113  bool was_enabled_;
114
115  DISALLOW_COPY_AND_ASSIGN(ScopedEnableSyncFSDirectoryOperation);
116};
117
118// Enables V2 backend for syncable filesystems temporarily for testing.
119class ScopedEnableSyncFSV2 {
120 public:
121  ScopedEnableSyncFSV2();
122  ~ScopedEnableSyncFSV2();
123
124 private:
125  bool was_enabled_;
126
127  DISALLOW_COPY_AND_ASSIGN(ScopedEnableSyncFSV2);
128};
129
130// Posts |callback| to the current thread.
131void RunSoon(const tracked_objects::Location& from_here,
132             const base::Closure& callback);
133
134}  // namespace sync_file_system
135
136#endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNCABLE_FILE_SYSTEM_UTIL_H_
137