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_service_state.h" 24 #include "chrome/browser/sync_file_system/task_logger.h" 25 #include "components/keyed_service/core/keyed_service.h" 26 #include "content/public/browser/notification_observer.h" 27 #include "content/public/browser/notification_registrar.h" 28 #include "url/gurl.h" 29 30 class Profile; 31 class ProfileSyncServiceBase; 32 33 namespace fileapi { 34 class FileSystemContext; 35 } 36 37 namespace sync_file_system { 38 39 class LocalFileSyncService; 40 class LocalSyncRunner; 41 class RemoteSyncRunner; 42 class SyncEventObserver; 43 class SyncProcessRunner; 44 45 class SyncFileSystemService 46 : public KeyedService, 47 public ProfileSyncServiceObserver, 48 public FileStatusObserver, 49 public content::NotificationObserver, 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 overrides. 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 SyncServiceState GetSyncServiceState(); 65 void GetExtensionStatusMap(const ExtensionStatusMapCallback& callback); 66 void DumpFiles(const GURL& origin, const DumpFilesCallback& callback); 67 void DumpDatabase(const DumpFilesCallback& callback); 68 69 // Returns the file |url|'s sync status. 70 void GetFileSyncStatus( 71 const fileapi::FileSystemURL& url, 72 const SyncFileStatusCallback& callback); 73 74 void AddSyncEventObserver(SyncEventObserver* observer); 75 void RemoveSyncEventObserver(SyncEventObserver* observer); 76 77 LocalChangeProcessor* GetLocalChangeProcessor(const GURL& origin); 78 79 void OnSyncIdle(); 80 81 TaskLogger* task_logger() { return &task_logger_; } 82 83 private: 84 friend class SyncFileSystemServiceFactory; 85 friend class SyncFileSystemServiceTest; 86 friend struct base::DefaultDeleter<SyncFileSystemService>; 87 friend class LocalSyncRunner; 88 friend class RemoteSyncRunner; 89 90 explicit SyncFileSystemService(Profile* profile); 91 virtual ~SyncFileSystemService(); 92 93 void Initialize(scoped_ptr<LocalFileSyncService> local_file_service, 94 scoped_ptr<RemoteFileSyncService> remote_file_service); 95 96 // Callbacks for InitializeForApp. 97 void DidInitializeFileSystem(const GURL& app_origin, 98 const SyncStatusCallback& callback, 99 SyncStatusCode status); 100 void DidRegisterOrigin(const GURL& app_origin, 101 const SyncStatusCallback& callback, 102 SyncStatusCode status); 103 104 void DidInitializeFileSystemForDump(const GURL& app_origin, 105 const DumpFilesCallback& callback, 106 SyncStatusCode status); 107 void DidDumpFiles(const GURL& app_origin, 108 const DumpFilesCallback& callback, 109 scoped_ptr<base::ListValue> files); 110 111 void DidDumpDatabase(const DumpFilesCallback& callback, 112 scoped_ptr<base::ListValue> list); 113 void DidDumpV2Database(const DumpFilesCallback& callback, 114 scoped_ptr<base::ListValue> v1list, 115 scoped_ptr<base::ListValue> v2list); 116 117 void DidGetExtensionStatusMap( 118 const ExtensionStatusMapCallback& callback, 119 scoped_ptr<RemoteFileSyncService::OriginStatusMap> status_map); 120 void DidGetV2ExtensionStatusMap( 121 const ExtensionStatusMapCallback& callback, 122 scoped_ptr<RemoteFileSyncService::OriginStatusMap> status_map_v1, 123 scoped_ptr<RemoteFileSyncService::OriginStatusMap> status_map_v2); 124 125 // Overrides sync_enabled_ setting. This should be called only by tests. 126 void SetSyncEnabledForTesting(bool enabled); 127 128 void DidGetLocalChangeStatus(const SyncFileStatusCallback& callback, 129 SyncStatusCode status, 130 bool has_pending_local_changes); 131 132 void OnRemoteServiceStateUpdated(RemoteServiceState state, 133 const std::string& description); 134 135 // content::NotificationObserver implementation. 136 virtual void Observe(int type, 137 const content::NotificationSource& source, 138 const content::NotificationDetails& details) OVERRIDE; 139 140 void HandleExtensionInstalled(const content::NotificationDetails& details); 141 void HandleExtensionUnloaded(int type, 142 const content::NotificationDetails& details); 143 void HandleExtensionUninstalled(int type, 144 const content::NotificationDetails& details); 145 void HandleExtensionEnabled(int type, 146 const content::NotificationDetails& details); 147 148 // ProfileSyncServiceObserver: 149 virtual void OnStateChanged() OVERRIDE; 150 151 // SyncFileStatusObserver: 152 virtual void OnFileStatusChanged( 153 const fileapi::FileSystemURL& url, 154 SyncFileStatus sync_status, 155 SyncAction action_taken, 156 SyncDirection direction) OVERRIDE; 157 158 // Check the profile's sync preference settings and call 159 // remote_file_service_->SetSyncEnabled() to update the status. 160 // |profile_sync_service| must be non-null. 161 void UpdateSyncEnabledStatus(ProfileSyncServiceBase* profile_sync_service); 162 163 // Runs the SyncProcessRunner method of all sync runners (e.g. for Local sync 164 // and Remote sync). 165 void RunForEachSyncRunners(void(SyncProcessRunner::*method)()); 166 167 // Returns the appropriate RemoteFileSyncService for the given origin/app. 168 // (crbug.com/324215) 169 RemoteFileSyncService* GetRemoteService(const GURL& origin); 170 171 Profile* profile_; 172 content::NotificationRegistrar registrar_; 173 174 scoped_ptr<LocalFileSyncService> local_service_; 175 scoped_ptr<RemoteFileSyncService> remote_service_; 176 177 // Holds v2 RemoteFileSyncService, gets created lazily 178 // in case we need to run multiple remote services depending on origin/app. 179 // (crbug.com/324215) 180 scoped_ptr<RemoteFileSyncService> v2_remote_service_; 181 182 // Holds all SyncProcessRunners. 183 ScopedVector<SyncProcessRunner> local_sync_runners_; 184 ScopedVector<SyncProcessRunner> remote_sync_runners_; 185 186 // Indicates if sync is currently enabled or not. 187 bool sync_enabled_; 188 189 TaskLogger task_logger_; 190 ObserverList<SyncEventObserver> observers_; 191 192 DISALLOW_COPY_AND_ASSIGN(SyncFileSystemService); 193 }; 194 195 } // namespace sync_file_system 196 197 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNC_FILE_SYSTEM_SERVICE_H_ 198