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_
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"
31 class ProfileSyncServiceBase;
34 class FileSystemContext;
37 namespace sync_file_system {
39 class LocalFileSyncService;
40 class LocalSyncRunner;
41 class RemoteSyncRunner;
42 class SyncEventObserver;
44 class SyncFileSystemService
45 : public KeyedService,
46 public SyncProcessRunner::Client,
47 public ProfileSyncServiceObserver,
48 public FileStatusObserver,
49 public extensions::ExtensionRegistryObserver,
50 public base::SupportsWeakPtr<SyncFileSystemService> {
52 typedef base::Callback<void(const base::ListValue&)> DumpFilesCallback;
53 typedef base::Callback<void(const RemoteFileSyncService::OriginStatusMap&)>
54 ExtensionStatusMapCallback;
56 // KeyedService implementation.
57 virtual void Shutdown() OVERRIDE;
59 void InitializeForApp(storage::FileSystemContext* file_system_context,
60 const GURL& app_origin,
61 const SyncStatusCallback& callback);
63 void GetExtensionStatusMap(const ExtensionStatusMapCallback& callback);
64 void DumpFiles(const GURL& origin, const DumpFilesCallback& callback);
65 void DumpDatabase(const DumpFilesCallback& callback);
67 // Returns the file |url|'s sync status.
68 void GetFileSyncStatus(const storage::FileSystemURL& url,
69 const SyncFileStatusCallback& callback);
71 void AddSyncEventObserver(SyncEventObserver* observer);
72 void RemoveSyncEventObserver(SyncEventObserver* observer);
74 LocalChangeProcessor* GetLocalChangeProcessor(const GURL& origin);
76 // SyncProcessRunner::Client implementations.
77 virtual void OnSyncIdle() OVERRIDE;
78 virtual SyncServiceState GetSyncServiceState() OVERRIDE;
79 virtual SyncFileSystemService* GetSyncService() OVERRIDE;
81 void OnPromotionCompleted(int* num_running_jobs);
84 TaskLogger* task_logger() { return &task_logger_; }
86 void CallOnIdleForTesting(const base::Closure& callback);
89 friend class SyncFileSystemServiceFactory;
90 friend class SyncFileSystemServiceTest;
91 friend class SyncFileSystemTest;
92 friend struct base::DefaultDeleter<SyncFileSystemService>;
93 friend class LocalSyncRunner;
94 friend class RemoteSyncRunner;
96 explicit SyncFileSystemService(Profile* profile);
97 virtual ~SyncFileSystemService();
99 void Initialize(scoped_ptr<LocalFileSyncService> local_file_service,
100 scoped_ptr<RemoteFileSyncService> remote_file_service);
102 // Callbacks for InitializeForApp.
103 void DidInitializeFileSystem(const GURL& app_origin,
104 const SyncStatusCallback& callback,
105 SyncStatusCode status);
106 void DidRegisterOrigin(const GURL& app_origin,
107 const SyncStatusCallback& callback,
108 SyncStatusCode status);
110 void DidInitializeFileSystemForDump(const GURL& app_origin,
111 const DumpFilesCallback& callback,
112 SyncStatusCode status);
113 void DidDumpFiles(const GURL& app_origin,
114 const DumpFilesCallback& callback,
115 scoped_ptr<base::ListValue> files);
117 void DidDumpDatabase(const DumpFilesCallback& callback,
118 scoped_ptr<base::ListValue> list);
120 void DidGetExtensionStatusMap(
121 const ExtensionStatusMapCallback& callback,
122 scoped_ptr<RemoteFileSyncService::OriginStatusMap> status_map);
124 // Overrides sync_enabled_ setting. This should be called only by tests.
125 void SetSyncEnabledForTesting(bool enabled);
127 void DidGetLocalChangeStatus(const SyncFileStatusCallback& callback,
128 SyncStatusCode status,
129 bool has_pending_local_changes);
131 void OnRemoteServiceStateUpdated(RemoteServiceState state,
132 const std::string& description);
134 // extensions::ExtensionRegistryObserver implementations.
135 virtual void OnExtensionInstalled(
136 content::BrowserContext* browser_context,
137 const extensions::Extension* extension,
138 bool is_update) OVERRIDE;
139 virtual void OnExtensionUnloaded(
140 content::BrowserContext* browser_context,
141 const extensions::Extension* extension,
142 extensions::UnloadedExtensionInfo::Reason reason) OVERRIDE;
143 virtual void OnExtensionUninstalled(
144 content::BrowserContext* browser_context,
145 const extensions::Extension* extension,
146 extensions::UninstallReason reason) OVERRIDE;
147 virtual void OnExtensionLoaded(
148 content::BrowserContext* browser_context,
149 const extensions::Extension* extension) OVERRIDE;
151 // ProfileSyncServiceObserver implementation.
152 virtual void OnStateChanged() OVERRIDE;
154 // SyncFileStatusObserver implementation.
155 virtual void OnFileStatusChanged(const storage::FileSystemURL& url,
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(ProfileSyncServiceBase* profile_sync_service);
165 // Runs the SyncProcessRunner method of all sync runners (e.g. for Local sync
167 void RunForEachSyncRunners(void(SyncProcessRunner::*method)());
169 // Returns the appropriate RemoteFileSyncService for the given origin/app.
170 // (crbug.com/324215)
171 RemoteFileSyncService* GetRemoteService(const GURL& origin);
175 scoped_ptr<LocalFileSyncService> local_service_;
176 scoped_ptr<RemoteFileSyncService> remote_service_;
178 // Holds all SyncProcessRunners.
179 ScopedVector<SyncProcessRunner> local_sync_runners_;
180 ScopedVector<SyncProcessRunner> remote_sync_runners_;
182 // Indicates if sync is currently enabled or not.
185 TaskLogger task_logger_;
186 ObserverList<SyncEventObserver> observers_;
188 bool promoting_demoted_changes_;
189 base::Closure idle_callback_;
191 DISALLOW_COPY_AND_ASSIGN(SyncFileSystemService);
194 } // namespace sync_file_system
196 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNC_FILE_SYSTEM_SERVICE_H_