sync_file_system_service.h revision 116680a4aac90f2aa7413d9095a592090648e557
1// Copyright (c) 2012 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_SYNC_FILE_SYSTEM_SERVICE_H_
6#define CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNC_FILE_SYSTEM_SERVICE_H_
7
8#include <map>
9#include <string>
10
11#include "base/basictypes.h"
12#include "base/callback_forward.h"
13#include "base/memory/scoped_ptr.h"
14#include "base/memory/scoped_vector.h"
15#include "base/memory/weak_ptr.h"
16#include "base/observer_list.h"
17#include "base/timer/timer.h"
18#include "chrome/browser/sync/profile_sync_service_observer.h"
19#include "chrome/browser/sync_file_system/conflict_resolution_policy.h"
20#include "chrome/browser/sync_file_system/file_status_observer.h"
21#include "chrome/browser/sync_file_system/remote_file_sync_service.h"
22#include "chrome/browser/sync_file_system/sync_callbacks.h"
23#include "chrome/browser/sync_file_system/sync_process_runner.h"
24#include "chrome/browser/sync_file_system/sync_service_state.h"
25#include "chrome/browser/sync_file_system/task_logger.h"
26#include "components/keyed_service/core/keyed_service.h"
27#include "extensions/browser/extension_registry_observer.h"
28#include "url/gurl.h"
29
30class Profile;
31class ProfileSyncServiceBase;
32
33namespace fileapi {
34class FileSystemContext;
35}
36
37namespace sync_file_system {
38
39class LocalFileSyncService;
40class LocalSyncRunner;
41class RemoteSyncRunner;
42class SyncEventObserver;
43
44class SyncFileSystemService
45    : public KeyedService,
46      public SyncProcessRunner::Client,
47      public ProfileSyncServiceObserver,
48      public FileStatusObserver,
49      public extensions::ExtensionRegistryObserver,
50      public base::SupportsWeakPtr<SyncFileSystemService> {
51 public:
52  typedef base::Callback<void(const base::ListValue&)> DumpFilesCallback;
53  typedef base::Callback<void(const RemoteFileSyncService::OriginStatusMap&)>
54      ExtensionStatusMapCallback;
55
56  // KeyedService implementation.
57  virtual void Shutdown() OVERRIDE;
58
59  void InitializeForApp(
60      fileapi::FileSystemContext* file_system_context,
61      const GURL& app_origin,
62      const SyncStatusCallback& callback);
63
64  void GetExtensionStatusMap(const ExtensionStatusMapCallback& callback);
65  void DumpFiles(const GURL& origin, const DumpFilesCallback& callback);
66  void DumpDatabase(const DumpFilesCallback& callback);
67
68  // Returns the file |url|'s sync status.
69  void GetFileSyncStatus(
70      const fileapi::FileSystemURL& url,
71      const SyncFileStatusCallback& callback);
72
73  void AddSyncEventObserver(SyncEventObserver* observer);
74  void RemoveSyncEventObserver(SyncEventObserver* observer);
75
76  LocalChangeProcessor* GetLocalChangeProcessor(const GURL& origin);
77
78  // SyncProcessRunner::Client implementations.
79  virtual void OnSyncIdle() OVERRIDE;
80  virtual SyncServiceState GetSyncServiceState() OVERRIDE;
81  virtual SyncFileSystemService* GetSyncService() OVERRIDE;
82
83  TaskLogger* task_logger() { return &task_logger_; }
84
85 private:
86  friend class SyncFileSystemServiceFactory;
87  friend class SyncFileSystemServiceTest;
88  friend struct base::DefaultDeleter<SyncFileSystemService>;
89  friend class LocalSyncRunner;
90  friend class RemoteSyncRunner;
91
92  explicit SyncFileSystemService(Profile* profile);
93  virtual ~SyncFileSystemService();
94
95  void Initialize(scoped_ptr<LocalFileSyncService> local_file_service,
96                  scoped_ptr<RemoteFileSyncService> remote_file_service);
97
98  // Callbacks for InitializeForApp.
99  void DidInitializeFileSystem(const GURL& app_origin,
100                               const SyncStatusCallback& callback,
101                               SyncStatusCode status);
102  void DidRegisterOrigin(const GURL& app_origin,
103                         const SyncStatusCallback& callback,
104                         SyncStatusCode status);
105
106  void DidInitializeFileSystemForDump(const GURL& app_origin,
107                                      const DumpFilesCallback& callback,
108                                      SyncStatusCode status);
109  void DidDumpFiles(const GURL& app_origin,
110                    const DumpFilesCallback& callback,
111                    scoped_ptr<base::ListValue> files);
112
113  void DidDumpDatabase(const DumpFilesCallback& callback,
114                       scoped_ptr<base::ListValue> list);
115  void DidDumpV2Database(const DumpFilesCallback& callback,
116                         scoped_ptr<base::ListValue> v1list,
117                         scoped_ptr<base::ListValue> v2list);
118
119  void DidGetExtensionStatusMap(
120      const ExtensionStatusMapCallback& callback,
121      scoped_ptr<RemoteFileSyncService::OriginStatusMap> status_map);
122  void DidGetV2ExtensionStatusMap(
123      const ExtensionStatusMapCallback& callback,
124      scoped_ptr<RemoteFileSyncService::OriginStatusMap> status_map_v1,
125      scoped_ptr<RemoteFileSyncService::OriginStatusMap> status_map_v2);
126
127  // Overrides sync_enabled_ setting. This should be called only by tests.
128  void SetSyncEnabledForTesting(bool enabled);
129
130  void DidGetLocalChangeStatus(const SyncFileStatusCallback& callback,
131                               SyncStatusCode status,
132                               bool has_pending_local_changes);
133
134  void OnRemoteServiceStateUpdated(RemoteServiceState state,
135                                   const std::string& description);
136
137  // extensions::ExtensionRegistryObserver implementations.
138  virtual void OnExtensionInstalled(
139      content::BrowserContext* browser_context,
140      const extensions::Extension* extension,
141      bool is_update) OVERRIDE;
142  virtual void OnExtensionUnloaded(
143      content::BrowserContext* browser_context,
144      const extensions::Extension* extension,
145      extensions::UnloadedExtensionInfo::Reason reason) OVERRIDE;
146  virtual void OnExtensionUninstalled(
147      content::BrowserContext* browser_context,
148      const extensions::Extension* extension) OVERRIDE;
149  virtual void OnExtensionLoaded(
150      content::BrowserContext* browser_context,
151      const extensions::Extension* extension) OVERRIDE;
152
153  // ProfileSyncServiceObserver implementation.
154  virtual void OnStateChanged() OVERRIDE;
155
156  // SyncFileStatusObserver implementation.
157  virtual void OnFileStatusChanged(
158      const fileapi::FileSystemURL& url,
159      SyncFileStatus sync_status,
160      SyncAction action_taken,
161      SyncDirection direction) OVERRIDE;
162
163  // Check the profile's sync preference settings and call
164  // remote_file_service_->SetSyncEnabled() to update the status.
165  // |profile_sync_service| must be non-null.
166  void UpdateSyncEnabledStatus(ProfileSyncServiceBase* profile_sync_service);
167
168  // Runs the SyncProcessRunner method of all sync runners (e.g. for Local sync
169  // and Remote sync).
170  void RunForEachSyncRunners(void(SyncProcessRunner::*method)());
171
172  // Returns the appropriate RemoteFileSyncService for the given origin/app.
173  // (crbug.com/324215)
174  RemoteFileSyncService* GetRemoteService(const GURL& origin);
175
176  Profile* profile_;
177
178  scoped_ptr<LocalFileSyncService> local_service_;
179  scoped_ptr<RemoteFileSyncService> remote_service_;
180
181  // Holds v2 RemoteFileSyncService, gets created lazily
182  // in case we need to run multiple remote services depending on origin/app.
183  // (crbug.com/324215)
184  scoped_ptr<RemoteFileSyncService> v2_remote_service_;
185
186  // Holds all SyncProcessRunners.
187  ScopedVector<SyncProcessRunner> local_sync_runners_;
188  ScopedVector<SyncProcessRunner> remote_sync_runners_;
189
190  // Indicates if sync is currently enabled or not.
191  bool sync_enabled_;
192
193  TaskLogger task_logger_;
194  ObserverList<SyncEventObserver> observers_;
195
196  DISALLOW_COPY_AND_ASSIGN(SyncFileSystemService);
197};
198
199}  // namespace sync_file_system
200
201#endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNC_FILE_SYSTEM_SERVICE_H_
202