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 "chrome/browser/sync/profile_sync_service_observer.h"
18 #include "chrome/browser/sync_file_system/conflict_resolution_policy.h"
19 #include "chrome/browser/sync_file_system/file_status_observer.h"
20 #include "chrome/browser/sync_file_system/remote_file_sync_service.h"
21 #include "chrome/browser/sync_file_system/sync_callbacks.h"
22 #include "chrome/browser/sync_file_system/sync_process_runner.h"
23 #include "chrome/browser/sync_file_system/sync_service_state.h"
24 #include "chrome/browser/sync_file_system/task_logger.h"
25 #include "components/keyed_service/core/keyed_service.h"
26 #include "extensions/browser/extension_registry_observer.h"
30 class ProfileSyncServiceBase
;
33 class FileSystemContext
;
36 namespace sync_file_system
{
38 class LocalFileSyncService
;
39 class LocalSyncRunner
;
40 class RemoteSyncRunner
;
41 class SyncEventObserver
;
43 class SyncFileSystemService
44 : public KeyedService
,
45 public SyncProcessRunner::Client
,
46 public ProfileSyncServiceObserver
,
47 public FileStatusObserver
,
48 public extensions::ExtensionRegistryObserver
,
49 public base::SupportsWeakPtr
<SyncFileSystemService
> {
51 typedef base::Callback
<void(const base::ListValue
&)> DumpFilesCallback
;
52 typedef base::Callback
<void(const RemoteFileSyncService::OriginStatusMap
&)>
53 ExtensionStatusMapCallback
;
55 // KeyedService implementation.
56 void Shutdown() override
;
58 void InitializeForApp(storage::FileSystemContext
* file_system_context
,
59 const GURL
& app_origin
,
60 const SyncStatusCallback
& callback
);
62 void GetExtensionStatusMap(const ExtensionStatusMapCallback
& callback
);
63 void DumpFiles(const GURL
& origin
, const DumpFilesCallback
& callback
);
64 void DumpDatabase(const DumpFilesCallback
& callback
);
66 // Returns the file |url|'s sync status.
67 void GetFileSyncStatus(const storage::FileSystemURL
& url
,
68 const SyncFileStatusCallback
& callback
);
70 void AddSyncEventObserver(SyncEventObserver
* observer
);
71 void RemoveSyncEventObserver(SyncEventObserver
* observer
);
73 LocalChangeProcessor
* GetLocalChangeProcessor(const GURL
& origin
);
75 // SyncProcessRunner::Client implementations.
76 void OnSyncIdle() override
;
77 SyncServiceState
GetSyncServiceState() override
;
78 SyncFileSystemService
* GetSyncService() override
;
80 void OnPromotionCompleted(int* num_running_jobs
);
83 TaskLogger
* task_logger() { return &task_logger_
; }
85 void CallOnIdleForTesting(const base::Closure
& callback
);
88 friend class SyncFileSystemServiceFactory
;
89 friend class SyncFileSystemServiceTest
;
90 friend class SyncFileSystemTest
;
91 friend struct base::DefaultDeleter
<SyncFileSystemService
>;
92 friend class LocalSyncRunner
;
93 friend class RemoteSyncRunner
;
95 explicit SyncFileSystemService(Profile
* profile
);
96 ~SyncFileSystemService() override
;
98 void Initialize(scoped_ptr
<LocalFileSyncService
> local_file_service
,
99 scoped_ptr
<RemoteFileSyncService
> remote_file_service
);
101 // Callbacks for InitializeForApp.
102 void DidInitializeFileSystem(const GURL
& app_origin
,
103 const SyncStatusCallback
& callback
,
104 SyncStatusCode status
);
105 void DidRegisterOrigin(const GURL
& app_origin
,
106 const SyncStatusCallback
& callback
,
107 SyncStatusCode status
);
109 void DidInitializeFileSystemForDump(const GURL
& app_origin
,
110 const DumpFilesCallback
& callback
,
111 SyncStatusCode status
);
112 void DidDumpFiles(const GURL
& app_origin
,
113 const DumpFilesCallback
& callback
,
114 scoped_ptr
<base::ListValue
> files
);
116 void DidDumpDatabase(const DumpFilesCallback
& callback
,
117 scoped_ptr
<base::ListValue
> list
);
119 void DidGetExtensionStatusMap(
120 const ExtensionStatusMapCallback
& callback
,
121 scoped_ptr
<RemoteFileSyncService::OriginStatusMap
> status_map
);
123 // Overrides sync_enabled_ setting. This should be called only by tests.
124 void SetSyncEnabledForTesting(bool enabled
);
126 void DidGetLocalChangeStatus(const SyncFileStatusCallback
& callback
,
127 SyncStatusCode status
,
128 bool has_pending_local_changes
);
130 void OnRemoteServiceStateUpdated(RemoteServiceState state
,
131 const std::string
& description
);
133 // extensions::ExtensionRegistryObserver implementations.
134 void OnExtensionInstalled(content::BrowserContext
* browser_context
,
135 const extensions::Extension
* extension
,
136 bool is_update
) override
;
137 void OnExtensionUnloaded(
138 content::BrowserContext
* browser_context
,
139 const extensions::Extension
* extension
,
140 extensions::UnloadedExtensionInfo::Reason reason
) override
;
141 void OnExtensionUninstalled(content::BrowserContext
* browser_context
,
142 const extensions::Extension
* extension
,
143 extensions::UninstallReason reason
) override
;
144 void OnExtensionLoaded(content::BrowserContext
* browser_context
,
145 const extensions::Extension
* extension
) override
;
147 // ProfileSyncServiceObserver implementation.
148 void OnStateChanged() override
;
150 // SyncFileStatusObserver implementation.
151 void OnFileStatusChanged(const storage::FileSystemURL
& url
,
152 SyncFileType file_type
,
153 SyncFileStatus sync_status
,
154 SyncAction action_taken
,
155 SyncDirection direction
) override
;
157 // Check the profile's sync preference settings and call
158 // remote_file_service_->SetSyncEnabled() to update the status.
159 // |profile_sync_service| must be non-null.
160 void UpdateSyncEnabledStatus(ProfileSyncServiceBase
* profile_sync_service
);
162 // Runs the SyncProcessRunner method of all sync runners (e.g. for Local sync
164 void RunForEachSyncRunners(void(SyncProcessRunner::*method
)());
166 // Returns the appropriate RemoteFileSyncService for the given origin/app.
167 // (crbug.com/324215)
168 RemoteFileSyncService
* GetRemoteService(const GURL
& origin
);
172 scoped_ptr
<LocalFileSyncService
> local_service_
;
173 scoped_ptr
<RemoteFileSyncService
> remote_service_
;
175 // Holds all SyncProcessRunners.
176 ScopedVector
<SyncProcessRunner
> local_sync_runners_
;
177 ScopedVector
<SyncProcessRunner
> remote_sync_runners_
;
179 // Indicates if sync is currently enabled or not.
182 TaskLogger task_logger_
;
183 ObserverList
<SyncEventObserver
> observers_
;
185 bool promoting_demoted_changes_
;
186 base::Closure idle_callback_
;
188 DISALLOW_COPY_AND_ASSIGN(SyncFileSystemService
);
191 } // namespace sync_file_system
193 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNC_FILE_SYSTEM_SERVICE_H_