sync_worker.h revision 46d4c2bc3267f3f028f39e7e311b0f89aba2e4fd
1// Copyright 2014 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_DRIVE_BACKEND_SYNC_WORKER_H_ 6#define CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_SYNC_WORKER_H_ 7 8#include <set> 9#include <string> 10 11#include "base/memory/scoped_ptr.h" 12#include "base/memory/weak_ptr.h" 13#include "base/observer_list.h" 14#include "chrome/browser/drive/drive_notification_observer.h" 15#include "chrome/browser/drive/drive_service_interface.h" 16#include "chrome/browser/sync_file_system/drive_backend/sync_task_manager.h" 17#include "chrome/browser/sync_file_system/local_change_processor.h" 18#include "chrome/browser/sync_file_system/remote_file_sync_service.h" 19#include "chrome/browser/sync_file_system/sync_action.h" 20#include "chrome/browser/sync_file_system/sync_direction.h" 21#include "chrome/browser/sync_file_system/task_logger.h" 22#include "net/base/network_change_notifier.h" 23 24class ExtensionServiceInterface; 25 26namespace base { 27class SequencedTaskRunner; 28} 29 30namespace drive { 31class DriveServiceInterface; 32class DriveNotificationManager; 33class DriveUploaderInterface; 34} 35 36namespace leveldb { 37class Env; 38} 39 40namespace sync_file_system { 41 42class RemoteChangeProcessor; 43 44namespace drive_backend { 45 46class LocalToRemoteSyncer; 47class MetadataDatabase; 48class RemoteChangeProcessorOnWorker; 49class RemoteToLocalSyncer; 50class SyncEngineContext; 51class SyncEngineInitializer; 52 53class SyncWorker : public SyncTaskManager::Client { 54 public: 55 enum AppStatus { 56 APP_STATUS_ENABLED, 57 APP_STATUS_DISABLED, 58 APP_STATUS_UNINSTALLED, 59 }; 60 61 typedef base::hash_map<std::string, AppStatus> AppStatusMap; 62 63 class Observer { 64 public: 65 virtual void OnPendingFileListUpdated(int item_count) = 0; 66 virtual void OnFileStatusChanged(const fileapi::FileSystemURL& url, 67 SyncFileStatus file_status, 68 SyncAction sync_action, 69 SyncDirection direction) = 0; 70 virtual void UpdateServiceState(RemoteServiceState state, 71 const std::string& description) = 0; 72 73 protected: 74 virtual ~Observer() {} 75 }; 76 77 SyncWorker(const base::FilePath& base_dir, 78 const base::WeakPtr<ExtensionServiceInterface>& extension_service, 79 scoped_ptr<SyncEngineContext> sync_engine_context, 80 leveldb::Env* env_override); 81 82 virtual ~SyncWorker(); 83 84 void Initialize(); 85 86 // SyncTaskManager::Client overrides 87 virtual void MaybeScheduleNextTask() OVERRIDE; 88 virtual void NotifyLastOperationStatus( 89 SyncStatusCode sync_status, bool used_network) OVERRIDE; 90 virtual void RecordTaskLog(scoped_ptr<TaskLogger::TaskLog> task_log) OVERRIDE; 91 92 void RegisterOrigin(const GURL& origin, const SyncStatusCallback& callback); 93 void EnableOrigin(const GURL& origin, const SyncStatusCallback& callback); 94 void DisableOrigin(const GURL& origin, const SyncStatusCallback& callback); 95 void UninstallOrigin( 96 const GURL& origin, 97 RemoteFileSyncService::UninstallFlag flag, 98 const SyncStatusCallback& callback); 99 void ProcessRemoteChange(const SyncFileCallback& callback); 100 void SetRemoteChangeProcessor( 101 RemoteChangeProcessorOnWorker* remote_change_processor_on_worker); 102 RemoteServiceState GetCurrentState() const; 103 void GetOriginStatusMap( 104 const RemoteFileSyncService::StatusMapCallback& callback); 105 scoped_ptr<base::ListValue> DumpFiles(const GURL& origin); 106 scoped_ptr<base::ListValue> DumpDatabase(); 107 void SetSyncEnabled(bool enabled); 108 void PromoteDemotedChanges(); 109 SyncStatusCode SetDefaultConflictResolutionPolicy( 110 ConflictResolutionPolicy policy); 111 SyncStatusCode SetConflictResolutionPolicy( 112 const GURL& origin, 113 ConflictResolutionPolicy policy); 114 ConflictResolutionPolicy GetDefaultConflictResolutionPolicy() 115 const; 116 ConflictResolutionPolicy GetConflictResolutionPolicy( 117 const GURL& origin) const; 118 119 void ApplyLocalChange( 120 const FileChange& local_change, 121 const base::FilePath& local_path, 122 const SyncFileMetadata& local_metadata, 123 const fileapi::FileSystemURL& url, 124 const SyncStatusCallback& callback); 125 126 void OnNotificationReceived(); 127 128 void OnReadyToSendRequests(const std::string& account_id); 129 void OnRefreshTokenInvalid(); 130 131 void OnNetworkChanged(net::NetworkChangeNotifier::ConnectionType type); 132 133 drive::DriveServiceInterface* GetDriveService(); 134 drive::DriveUploaderInterface* GetDriveUploader(); 135 MetadataDatabase* GetMetadataDatabase(); 136 SyncTaskManager* GetSyncTaskManager(); 137 138 void AddObserver(Observer* observer); 139 140 void DetachFromSequence(); 141 142 private: 143 friend class SyncEngineTest; 144 145 void DoDisableApp(const std::string& app_id, 146 const SyncStatusCallback& callback); 147 void DoEnableApp(const std::string& app_id, 148 const SyncStatusCallback& callback); 149 150 void PostInitializeTask(); 151 void DidInitialize(SyncEngineInitializer* initializer, 152 SyncStatusCode status); 153 void UpdateRegisteredApp(); 154 void DidQueryAppStatus(const AppStatusMap* app_status); 155 void DidProcessRemoteChange(RemoteToLocalSyncer* syncer, 156 const SyncFileCallback& callback, 157 SyncStatusCode status); 158 void DidApplyLocalChange(LocalToRemoteSyncer* syncer, 159 const SyncStatusCallback& callback, 160 SyncStatusCode status); 161 162 void MaybeStartFetchChanges(); 163 void DidResolveConflict(SyncStatusCode status); 164 void DidFetchChanges(SyncStatusCode status); 165 166 void UpdateServiceStateFromSyncStatusCode(SyncStatusCode state, 167 bool used_network); 168 void UpdateServiceState(RemoteServiceState state, 169 const std::string& description); 170 void UpdateRegisteredApps(); 171 172 base::FilePath base_dir_; 173 174 leveldb::Env* env_override_; 175 176 // Sync with SyncEngine. 177 RemoteServiceState service_state_; 178 179 bool should_check_conflict_; 180 bool should_check_remote_change_; 181 bool listing_remote_changes_; 182 base::TimeTicks time_to_check_changes_; 183 184 bool sync_enabled_; 185 ConflictResolutionPolicy default_conflict_resolution_policy_; 186 bool network_available_; 187 188 scoped_ptr<SyncTaskManager> task_manager_; 189 190 base::WeakPtr<ExtensionServiceInterface> extension_service_; 191 192 scoped_ptr<SyncEngineContext> context_; 193 ObserverList<Observer> observers_; 194 195 bool has_refresh_token_; 196 197 base::SequenceChecker sequence_checker_; 198 199 base::WeakPtrFactory<SyncWorker> weak_ptr_factory_; 200 DISALLOW_COPY_AND_ASSIGN(SyncWorker); 201}; 202 203} // namespace drive_backend 204} // namespace sync_file_system 205 206#endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_SYNC_WORKER_H_ 207