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