sync_file_system_backend.h revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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_LOCAL_SYNC_FILE_SYSTEM_BACKEND_H_
6#define CHROME_BROWSER_SYNC_FILE_SYSTEM_LOCAL_SYNC_FILE_SYSTEM_BACKEND_H_
7
8#include "chrome/browser/profiles/profile.h"
9#include "chrome/browser/sync_file_system/sync_callbacks.h"
10#include "chrome/browser/sync_file_system/sync_status_code.h"
11#include "content/public/browser/notification_observer.h"
12#include "content/public/browser/notification_registrar.h"
13#include "storage/browser/fileapi/file_system_backend.h"
14#include "storage/browser/fileapi/file_system_quota_util.h"
15#include "storage/browser/fileapi/sandbox_file_system_backend_delegate.h"
16#include "storage/browser/fileapi/task_runner_bound_observer_list.h"
17
18namespace sync_file_system {
19
20class LocalFileChangeTracker;
21class LocalFileSyncContext;
22
23class SyncFileSystemBackend : public storage::FileSystemBackend {
24 public:
25  explicit SyncFileSystemBackend(Profile* profile);
26  virtual ~SyncFileSystemBackend();
27
28  static SyncFileSystemBackend* CreateForTesting();
29
30  // FileSystemBackend overrides.
31  virtual bool CanHandleType(storage::FileSystemType type) const OVERRIDE;
32  virtual void Initialize(storage::FileSystemContext* context) OVERRIDE;
33  virtual void ResolveURL(const storage::FileSystemURL& url,
34                          storage::OpenFileSystemMode mode,
35                          const OpenFileSystemCallback& callback) OVERRIDE;
36  virtual storage::AsyncFileUtil* GetAsyncFileUtil(
37      storage::FileSystemType type) OVERRIDE;
38  virtual storage::WatcherManager* GetWatcherManager(
39      storage::FileSystemType type) OVERRIDE;
40  virtual storage::CopyOrMoveFileValidatorFactory*
41      GetCopyOrMoveFileValidatorFactory(storage::FileSystemType type,
42                                        base::File::Error* error_code) OVERRIDE;
43  virtual storage::FileSystemOperation* CreateFileSystemOperation(
44      const storage::FileSystemURL& url,
45      storage::FileSystemContext* context,
46      base::File::Error* error_code) const OVERRIDE;
47  virtual bool SupportsStreaming(
48      const storage::FileSystemURL& url) const OVERRIDE;
49  virtual bool HasInplaceCopyImplementation(
50      storage::FileSystemType type) const OVERRIDE;
51  virtual scoped_ptr<storage::FileStreamReader> CreateFileStreamReader(
52      const storage::FileSystemURL& url,
53      int64 offset,
54      int64 max_bytes_to_read,
55      const base::Time& expected_modification_time,
56      storage::FileSystemContext* context) const OVERRIDE;
57  virtual scoped_ptr<storage::FileStreamWriter> CreateFileStreamWriter(
58      const storage::FileSystemURL& url,
59      int64 offset,
60      storage::FileSystemContext* context) const OVERRIDE;
61  virtual storage::FileSystemQuotaUtil* GetQuotaUtil() OVERRIDE;
62  virtual const storage::UpdateObserverList* GetUpdateObservers(
63      storage::FileSystemType type) const OVERRIDE;
64  virtual const storage::ChangeObserverList* GetChangeObservers(
65      storage::FileSystemType type) const OVERRIDE;
66  virtual const storage::AccessObserverList* GetAccessObservers(
67      storage::FileSystemType type) const OVERRIDE;
68
69  static SyncFileSystemBackend* GetBackend(
70      const storage::FileSystemContext* context);
71
72  LocalFileChangeTracker* change_tracker() { return change_tracker_.get(); }
73  void SetLocalFileChangeTracker(scoped_ptr<LocalFileChangeTracker> tracker);
74
75  LocalFileSyncContext* sync_context() { return sync_context_.get(); }
76  void set_sync_context(LocalFileSyncContext* sync_context);
77
78 private:
79  class ProfileHolder : public content::NotificationObserver {
80   public:
81    explicit ProfileHolder(Profile* profile);
82
83    // NotificationObserver override.
84    virtual void Observe(int type,
85                         const content::NotificationSource& source,
86                         const content::NotificationDetails& details) OVERRIDE;
87
88    Profile* GetProfile();
89
90   private:
91    content::NotificationRegistrar registrar_;
92    Profile* profile_;
93  };
94
95  // Not owned.
96  storage::FileSystemContext* context_;
97
98  scoped_ptr<LocalFileChangeTracker> change_tracker_;
99  scoped_refptr<LocalFileSyncContext> sync_context_;
100
101  // Should be accessed on the UI thread.
102  scoped_ptr<ProfileHolder> profile_holder_;
103
104  // A flag to skip the initialization sequence of SyncFileSystemService for
105  // testing.
106  bool skip_initialize_syncfs_service_for_testing_;
107
108  storage::SandboxFileSystemBackendDelegate* GetDelegate() const;
109
110  void InitializeSyncFileSystemService(
111      const GURL& origin_url,
112      const SyncStatusCallback& callback);
113  void DidInitializeSyncFileSystemService(
114      storage::FileSystemContext* context,
115      const GURL& origin_url,
116      storage::FileSystemType type,
117      storage::OpenFileSystemMode mode,
118      const OpenFileSystemCallback& callback,
119      SyncStatusCode status);
120
121  DISALLOW_COPY_AND_ASSIGN(SyncFileSystemBackend);
122};
123
124}  // namespace sync_file_system
125
126#endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_LOCAL_SYNC_FILE_SYSTEM_BACKEND_H_
127