syncable_file_system_util.h revision f2477e01787aa58f445919b809d89e252beef54f
190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
52385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch#ifndef CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNCABLE_FILE_SYSTEM_UTIL_H_
62385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch#define CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNCABLE_FILE_SYSTEM_UTIL_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "base/callback_forward.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/file_path.h"
1290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "webkit/browser/fileapi/file_system_url.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace fileapi {
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class FileSystemContext;
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class FileSystemURL;
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)namespace tracked_objects {
204e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)class Location;
214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace sync_file_system {
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
25868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Registers a syncable filesystem.
262385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdochvoid RegisterSyncableFileSystem();
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
28868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Revokes the syncable filesystem.
292385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdochvoid RevokeSyncableFileSystem();
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
31868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Returns the root URI of the syncable filesystem for |origin|.
322385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen MurdochGURL GetSyncableFileSystemRootURI(const GURL& origin);
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
34868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Creates a FileSystem URL for the |path| in a syncable filesystem for
35868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// |origin|.
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Example: Assume following arguments are given:
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   origin: 'http://www.example.com/',
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   path: '/foo/bar',
40868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// returns 'filesystem:http://www.example.com/external/syncfs/foo/bar'
412385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdochfileapi::FileSystemURL
427d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)CreateSyncableFileSystemURL(const GURL& origin, const base::FilePath& path);
43868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
44868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Creates a special filesystem URL for synchronizing |syncable_url|.
452385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdochfileapi::FileSystemURL CreateSyncableFileSystemURLForSync(
46868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    fileapi::FileSystemContext* file_system_context,
47868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    const fileapi::FileSystemURL& syncable_url);
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Serializes a given FileSystemURL |url| and sets the serialized string to
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// |serialized_url|. If the URL does not represent a syncable filesystem,
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// |serialized_url| is not filled in, and returns false. Separators of the
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// path will be normalized depending on its platform.
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Example: Assume a following FileSystemURL object is given:
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   origin() returns 'http://www.example.com/',
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   type() returns the kFileSystemTypeSyncable,
57868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//   filesystem_id() returns 'syncfs',
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   path() returns '/foo/bar',
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// this URL will be serialized to
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (on Windows)
61868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//   'filesystem:http://www.example.com/external/syncfs/foo\\bar'
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (on others)
63868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//   'filesystem:http://www.example.com/external/syncfs/foo/bar'
642385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdochbool SerializeSyncableFileSystemURL(
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const fileapi::FileSystemURL& url, std::string* serialized_url);
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Deserializes a serialized FileSystem URL string |serialized_url| and sets the
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// deserialized value to |url|. If the reconstructed object is invalid or does
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// not represent a syncable filesystem, returns false.
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// NOTE: On any platform other than Windows, this function assumes that
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// |serialized_url| does not contain '\\'. If it contains '\\' on such
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// platforms, '\\' may be replaced with '/' (It would not be an expected
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// behavior).
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// See the comment of SerializeSyncableFileSystemURL() for more details.
772385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdochbool DeserializeSyncableFileSystemURL(
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& serialized_url, fileapi::FileSystemURL* url);
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Enables or disables directory operations in Sync FileSystem API.
81c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// TODO(nhiroki): This method should be used only for testing and should go
82c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// away when we fully support directory operations. (http://crbug.com/161442)
832385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdochvoid SetEnableSyncFSDirectoryOperation(bool flag);
84c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
8590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Returns true if we allow directory operations in Sync FileSystem API.
86c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// It is disabled by default but can be overridden by a command-line switch
8790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// (--enable-syncfs-directory-operations) or by calling
8890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// SetEnableSyncFSDirectoryOperation().
89c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// TODO(nhiroki): This method should be used only for testing and should go
90c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// away when we fully support directory operations. (http://crbug.com/161442)
912385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdochbool IsSyncFSDirectoryOperationEnabled();
92c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
93f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Checks the same as above, but takes |origin| and sees if directory operation
94f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// is enabled specifically for this |origin|.
95f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)bool IsSyncFSDirectoryOperationEnabled(const GURL& origin);
96f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
97f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Returns true if V2 is enabled.
98f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)bool IsV2Enabled();
99f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
100f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Returns true if the given |origin| is supposed to run in V2 mode.
101f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)bool IsV2EnabledForOrigin(const GURL& origin);
102f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
103d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// Returns SyncFileSystem sub-directory path.
104d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)base::FilePath GetSyncFileSystemDir(const base::FilePath& profile_base_dir);
105d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
106eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Enables directory operation for syncable filesystems temporarily for testing.
1072385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdochclass ScopedEnableSyncFSDirectoryOperation {
108eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch public:
109eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  ScopedEnableSyncFSDirectoryOperation();
110eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  ~ScopedEnableSyncFSDirectoryOperation();
111eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
112eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch private:
113eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  bool was_enabled_;
114eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
115eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  DISALLOW_COPY_AND_ASSIGN(ScopedEnableSyncFSDirectoryOperation);
116eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch};
117eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1184e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// Posts |callback| to the current thread.
1194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)void RunSoon(const tracked_objects::Location& from_here,
1204e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)             const base::Closure& callback);
1214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace sync_file_system
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1242385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch#endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNCABLE_FILE_SYSTEM_UTIL_H_
125