ExtensionSyncService: listen for relevant changes instead of being explicitly called...
[chromium-blink-merge.git] / chrome / browser / sync_file_system / sync_file_system_service.h
blob0d642e6c4f85db9acaf212ceaa7cf25c5e2fe167
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.
5 #ifndef CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNC_FILE_SYSTEM_SERVICE_H_
6 #define CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNC_FILE_SYSTEM_SERVICE_H_
8 #include <map>
9 #include <string>
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 "chrome/browser/sync_file_system/conflict_resolution_policy.h"
18 #include "chrome/browser/sync_file_system/file_status_observer.h"
19 #include "chrome/browser/sync_file_system/remote_file_sync_service.h"
20 #include "chrome/browser/sync_file_system/sync_callbacks.h"
21 #include "chrome/browser/sync_file_system/sync_process_runner.h"
22 #include "chrome/browser/sync_file_system/sync_service_state.h"
23 #include "chrome/browser/sync_file_system/task_logger.h"
24 #include "components/keyed_service/core/keyed_service.h"
25 #include "components/sync_driver/sync_service_observer.h"
26 #include "extensions/browser/extension_registry_observer.h"
27 #include "url/gurl.h"
29 class Profile;
31 namespace storage {
32 class FileSystemContext;
35 namespace sync_driver {
36 class SyncService;
39 namespace sync_file_system {
41 class LocalFileSyncService;
42 class LocalSyncRunner;
43 class RemoteSyncRunner;
44 class SyncEventObserver;
46 class SyncFileSystemService
47 : public KeyedService,
48 public SyncProcessRunner::Client,
49 public sync_driver::SyncServiceObserver,
50 public FileStatusObserver,
51 public extensions::ExtensionRegistryObserver,
52 public base::SupportsWeakPtr<SyncFileSystemService> {
53 public:
54 typedef base::Callback<void(const base::ListValue&)> DumpFilesCallback;
55 typedef base::Callback<void(const RemoteFileSyncService::OriginStatusMap&)>
56 ExtensionStatusMapCallback;
58 // KeyedService implementation.
59 void Shutdown() override;
61 void InitializeForApp(storage::FileSystemContext* file_system_context,
62 const GURL& app_origin,
63 const SyncStatusCallback& callback);
65 void GetExtensionStatusMap(const ExtensionStatusMapCallback& callback);
66 void DumpFiles(const GURL& origin, const DumpFilesCallback& callback);
67 void DumpDatabase(const DumpFilesCallback& callback);
69 // Returns the file |url|'s sync status.
70 void GetFileSyncStatus(const storage::FileSystemURL& url,
71 const SyncFileStatusCallback& callback);
73 void AddSyncEventObserver(SyncEventObserver* observer);
74 void RemoveSyncEventObserver(SyncEventObserver* observer);
76 LocalChangeProcessor* GetLocalChangeProcessor(const GURL& origin);
78 // SyncProcessRunner::Client implementations.
79 void OnSyncIdle() override;
80 SyncServiceState GetSyncServiceState() override;
81 SyncFileSystemService* GetSyncService() override;
83 void OnPromotionCompleted(int* num_running_jobs);
84 void CheckIfIdle();
86 TaskLogger* task_logger() { return &task_logger_; }
88 void CallOnIdleForTesting(const base::Closure& callback);
90 private:
91 friend class SyncFileSystemServiceFactory;
92 friend class SyncFileSystemServiceTest;
93 friend class SyncFileSystemTest;
94 friend struct base::DefaultDeleter<SyncFileSystemService>;
95 friend class LocalSyncRunner;
96 friend class RemoteSyncRunner;
98 explicit SyncFileSystemService(Profile* profile);
99 ~SyncFileSystemService() override;
101 void Initialize(scoped_ptr<LocalFileSyncService> local_file_service,
102 scoped_ptr<RemoteFileSyncService> remote_file_service);
104 // Callbacks for InitializeForApp.
105 void DidInitializeFileSystem(const GURL& app_origin,
106 const SyncStatusCallback& callback,
107 SyncStatusCode status);
108 void DidRegisterOrigin(const GURL& app_origin,
109 const SyncStatusCallback& callback,
110 SyncStatusCode status);
112 void DidInitializeFileSystemForDump(const GURL& app_origin,
113 const DumpFilesCallback& callback,
114 SyncStatusCode status);
115 void DidDumpFiles(const GURL& app_origin,
116 const DumpFilesCallback& callback,
117 scoped_ptr<base::ListValue> files);
119 void DidDumpDatabase(const DumpFilesCallback& callback,
120 scoped_ptr<base::ListValue> list);
122 void DidGetExtensionStatusMap(
123 const ExtensionStatusMapCallback& callback,
124 scoped_ptr<RemoteFileSyncService::OriginStatusMap> status_map);
126 // Overrides sync_enabled_ setting. This should be called only by tests.
127 void SetSyncEnabledForTesting(bool enabled);
129 void DidGetLocalChangeStatus(const SyncFileStatusCallback& callback,
130 SyncStatusCode status,
131 bool has_pending_local_changes);
133 void OnRemoteServiceStateUpdated(RemoteServiceState state,
134 const std::string& description);
136 // extensions::ExtensionRegistryObserver implementations.
137 void OnExtensionInstalled(content::BrowserContext* browser_context,
138 const extensions::Extension* extension,
139 bool is_update) override;
140 void OnExtensionUnloaded(
141 content::BrowserContext* browser_context,
142 const extensions::Extension* extension,
143 extensions::UnloadedExtensionInfo::Reason reason) override;
144 void OnExtensionUninstalled(content::BrowserContext* browser_context,
145 const extensions::Extension* extension,
146 extensions::UninstallReason reason) override;
147 void OnExtensionLoaded(content::BrowserContext* browser_context,
148 const extensions::Extension* extension) override;
150 // sync_driver::SyncServiceObserver implementation.
151 void OnStateChanged() override;
153 // SyncFileStatusObserver implementation.
154 void OnFileStatusChanged(const storage::FileSystemURL& url,
155 SyncFileType file_type,
156 SyncFileStatus sync_status,
157 SyncAction action_taken,
158 SyncDirection direction) override;
160 // Check the profile's sync preference settings and call
161 // remote_file_service_->SetSyncEnabled() to update the status.
162 // |profile_sync_service| must be non-null.
163 void UpdateSyncEnabledStatus(sync_driver::SyncService* profile_sync_service);
165 // Runs the SyncProcessRunner method of all sync runners (e.g. for Local sync
166 // and Remote sync).
167 void RunForEachSyncRunners(void(SyncProcessRunner::*method)());
169 Profile* profile_;
171 scoped_ptr<LocalFileSyncService> local_service_;
172 scoped_ptr<RemoteFileSyncService> remote_service_;
174 // Holds all SyncProcessRunners.
175 ScopedVector<SyncProcessRunner> local_sync_runners_;
176 ScopedVector<SyncProcessRunner> remote_sync_runners_;
178 // Indicates if sync is currently enabled or not.
179 bool sync_enabled_;
181 TaskLogger task_logger_;
182 base::ObserverList<SyncEventObserver> observers_;
184 bool promoting_demoted_changes_;
185 base::Closure idle_callback_;
187 DISALLOW_COPY_AND_ASSIGN(SyncFileSystemService);
190 } // namespace sync_file_system
192 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNC_FILE_SYSTEM_SERVICE_H_