#include "base/observer_list.h"
#include "chrome/browser/drive/drive_notification_observer.h"
#include "chrome/browser/drive/drive_service_interface.h"
-#include "chrome/browser/sync_file_system/drive_backend/sync_engine_context.h"
-#include "chrome/browser/sync_file_system/drive_backend/sync_task_manager.h"
+#include "chrome/browser/sync_file_system/drive_backend/callback_tracker.h"
#include "chrome/browser/sync_file_system/local_change_processor.h"
#include "chrome/browser/sync_file_system/remote_file_sync_service.h"
+#include "chrome/browser/sync_file_system/sync_action.h"
+#include "chrome/browser/sync_file_system/sync_direction.h"
+#include "components/signin/core/browser/signin_manager_base.h"
#include "net/base/network_change_notifier.h"
class ExtensionServiceInterface;
-class SigninManagerBase;
+class OAuth2TokenService;
namespace base {
class SequencedTaskRunner;
namespace drive {
class DriveServiceInterface;
class DriveNotificationManager;
+class DriveUploaderInterface;
}
namespace leveldb {
class Env;
}
+namespace net {
+class URLRequestContextGetter;
+}
+
namespace sync_file_system {
+
+class RemoteChangeProcessor;
+
namespace drive_backend {
-class LocalToRemoteSyncer;
+class DriveServiceWrapper;
+class DriveUploaderWrapper;
class MetadataDatabase;
-class RemoteToLocalSyncer;
-class SyncEngineInitializer;
+class RemoteChangeProcessorOnWorker;
+class RemoteChangeProcessorWrapper;
+class SyncTaskManager;
+class SyncWorkerInterface;
class SyncEngine : public RemoteFileSyncService,
public LocalChangeProcessor,
- public SyncTaskManager::Client,
public drive::DriveNotificationObserver,
public drive::DriveServiceObserver,
- public net::NetworkChangeNotifier::NetworkChangeObserver {
+ public net::NetworkChangeNotifier::NetworkChangeObserver,
+ public SigninManagerBase::Observer {
public:
- typedef Observer SyncServiceObserver;
+ typedef RemoteFileSyncService::Observer SyncServiceObserver;
+
+ class DriveServiceFactory {
+ public:
+ DriveServiceFactory() {}
+ virtual ~DriveServiceFactory() {}
+ virtual scoped_ptr<drive::DriveServiceInterface> CreateDriveService(
+ OAuth2TokenService* oauth2_token_service,
+ net::URLRequestContextGetter* url_request_context_getter,
+ base::SequencedTaskRunner* blocking_task_runner);
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(DriveServiceFactory);
+ };
static scoped_ptr<SyncEngine> CreateForBrowserContext(
- content::BrowserContext* context);
+ content::BrowserContext* context,
+ TaskLogger* task_logger);
static void AppendDependsOnFactories(
std::set<BrowserContextKeyedServiceFactory*>* factories);
virtual ~SyncEngine();
+ void Reset();
+ // Can be called more than once.
void Initialize();
+ void InitializeForTesting(
+ scoped_ptr<drive::DriveServiceInterface> drive_service,
+ scoped_ptr<drive::DriveUploaderInterface> drive_uploader,
+ scoped_ptr<SyncWorkerInterface> sync_worker);
+ void InitializeInternal(
+ scoped_ptr<drive::DriveServiceInterface> drive_service,
+ scoped_ptr<drive::DriveUploaderInterface> drive_uploader,
+ scoped_ptr<SyncWorkerInterface> sync_worker);
+
// RemoteFileSyncService overrides.
virtual void AddServiceObserver(SyncServiceObserver* observer) OVERRIDE;
virtual void AddFileStatusObserver(FileStatusObserver* observer) OVERRIDE;
virtual void SetRemoteChangeProcessor(
RemoteChangeProcessor* processor) OVERRIDE;
virtual LocalChangeProcessor* GetLocalChangeProcessor() OVERRIDE;
- virtual bool IsConflicting(const fileapi::FileSystemURL& url) OVERRIDE;
virtual RemoteServiceState GetCurrentState() const OVERRIDE;
- virtual void GetOriginStatusMap(OriginStatusMap* status_map) OVERRIDE;
- virtual scoped_ptr<base::ListValue> DumpFiles(const GURL& origin) OVERRIDE;
- virtual scoped_ptr<base::ListValue> DumpDatabase() OVERRIDE;
+ virtual void GetOriginStatusMap(const StatusMapCallback& callback) OVERRIDE;
+ virtual void DumpFiles(const GURL& origin,
+ const ListCallback& callback) OVERRIDE;
+ virtual void DumpDatabase(const ListCallback& callback) OVERRIDE;
virtual void SetSyncEnabled(bool enabled) OVERRIDE;
- virtual SyncStatusCode SetDefaultConflictResolutionPolicy(
- ConflictResolutionPolicy policy) OVERRIDE;
- virtual SyncStatusCode SetConflictResolutionPolicy(
- const GURL& origin,
- ConflictResolutionPolicy policy) OVERRIDE;
- virtual ConflictResolutionPolicy GetDefaultConflictResolutionPolicy()
- const OVERRIDE;
- virtual ConflictResolutionPolicy GetConflictResolutionPolicy(
- const GURL& origin) const OVERRIDE;
- virtual void GetRemoteVersions(
- const fileapi::FileSystemURL& url,
- const RemoteVersionsCallback& callback) OVERRIDE;
- virtual void DownloadRemoteVersion(
- const fileapi::FileSystemURL& url,
- const std::string& version_id,
- const DownloadVersionCallback& callback) OVERRIDE;
- virtual void PromoteDemotedChanges() OVERRIDE;
+ virtual void PromoteDemotedChanges(const base::Closure& callback) OVERRIDE;
// LocalChangeProcessor overrides.
virtual void ApplyLocalChange(
const fileapi::FileSystemURL& url,
const SyncStatusCallback& callback) OVERRIDE;
- // SyncTaskManager::Client overrides.
- virtual void MaybeScheduleNextTask() OVERRIDE;
- virtual void NotifyLastOperationStatus(SyncStatusCode sync_status,
- bool used_network) OVERRIDE;
-
// drive::DriveNotificationObserver overrides.
virtual void OnNotificationReceived() OVERRIDE;
virtual void OnPushNotificationEnabled(bool enabled) OVERRIDE;
virtual void OnNetworkChanged(
net::NetworkChangeNotifier::ConnectionType type) OVERRIDE;
- drive::DriveServiceInterface* GetDriveService();
- drive::DriveUploaderInterface* GetDriveUploader();
- MetadataDatabase* GetMetadataDatabase();
- RemoteChangeProcessor* GetRemoteChangeProcessor();
- base::SequencedTaskRunner* GetBlockingTaskRunner();
+ // SigninManagerBase::Observer overrides.
+ virtual void GoogleSigninFailed(const GoogleServiceAuthError& error) OVERRIDE;
+ virtual void GoogleSigninSucceeded(const std::string& username,
+ const std::string& password) OVERRIDE;
+ virtual void GoogleSignedOut(const std::string& username) OVERRIDE;
private:
+ class WorkerObserver;
+
friend class DriveBackendSyncTest;
friend class SyncEngineTest;
- SyncEngine(const base::FilePath& base_dir,
- base::SequencedTaskRunner* task_runner,
- scoped_ptr<drive::DriveServiceInterface> drive_service,
- scoped_ptr<drive::DriveUploaderInterface> drive_uploader,
+ SyncEngine(base::SingleThreadTaskRunner* ui_task_runner,
+ base::SequencedTaskRunner* worker_task_runner,
+ base::SequencedTaskRunner* drive_task_runner,
+ const base::FilePath& sync_file_system_dir,
+ TaskLogger* task_logger,
drive::DriveNotificationManager* notification_manager,
ExtensionServiceInterface* extension_service,
SigninManagerBase* signin_manager,
+ OAuth2TokenService* token_service,
+ net::URLRequestContextGetter* request_context,
+ scoped_ptr<DriveServiceFactory> drive_service_factory,
leveldb::Env* env_override);
- void DoDisableApp(const std::string& app_id,
- const SyncStatusCallback& callback);
- void DoEnableApp(const std::string& app_id,
- const SyncStatusCallback& callback);
-
- void PostInitializeTask();
- void DidInitialize(SyncEngineInitializer* initializer,
- SyncStatusCode status);
- void DidProcessRemoteChange(RemoteToLocalSyncer* syncer,
- const SyncFileCallback& callback,
- SyncStatusCode status);
- void DidApplyLocalChange(LocalToRemoteSyncer* syncer,
- const SyncStatusCallback& callback,
- SyncStatusCode status);
-
- void MaybeStartFetchChanges();
- void DidResolveConflict(SyncStatusCode status);
- void DidFetchChanges(SyncStatusCode status);
-
- void UpdateServiceStateFromSyncStatusCode(SyncStatusCode state,
- bool used_network);
+ // Called by WorkerObserver.
+ void OnPendingFileListUpdated(int item_count);
+ void OnFileStatusChanged(const fileapi::FileSystemURL& url,
+ SyncFileStatus file_status,
+ SyncAction sync_action,
+ SyncDirection direction);
void UpdateServiceState(RemoteServiceState state,
const std::string& description);
- void UpdateRegisteredApps();
- base::FilePath base_dir_;
- base::FilePath temporary_file_dir_;
+ SyncStatusCallback TrackCallback(const SyncStatusCallback& callback);
- leveldb::Env* env_override_;
+ scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
+ scoped_refptr<base::SequencedTaskRunner> worker_task_runner_;
+ scoped_refptr<base::SequencedTaskRunner> drive_task_runner_;
+
+ const base::FilePath sync_file_system_dir_;
+ TaskLogger* task_logger_;
// These external services are not owned by SyncEngine.
// The owner of the SyncEngine is responsible for their lifetime.
drive::DriveNotificationManager* notification_manager_;
ExtensionServiceInterface* extension_service_;
SigninManagerBase* signin_manager_;
+ OAuth2TokenService* token_service_;
- ObserverList<SyncServiceObserver> service_observers_;
- ObserverList<FileStatusObserver> file_status_observers_;
+ scoped_refptr<net::URLRequestContextGetter> request_context_;
- RemoteServiceState service_state_;
+ scoped_ptr<DriveServiceFactory> drive_service_factory_;
- bool should_check_conflict_;
- bool should_check_remote_change_;
- bool listing_remote_changes_;
- base::TimeTicks time_to_check_changes_;
+ scoped_ptr<drive::DriveServiceInterface> drive_service_;
+ scoped_ptr<DriveServiceWrapper> drive_service_wrapper_;
+ scoped_ptr<drive::DriveUploaderInterface> drive_uploader_;
+ scoped_ptr<DriveUploaderWrapper> drive_uploader_wrapper_;
- bool sync_enabled_;
- ConflictResolutionPolicy default_conflict_resolution_policy_;
+ RemoteChangeProcessor* remote_change_processor_; // Not owned.
+ scoped_ptr<RemoteChangeProcessorWrapper> remote_change_processor_wrapper_;
+ // Delete this on worker.
+ scoped_ptr<RemoteChangeProcessorOnWorker> remote_change_processor_on_worker_;
+
+ RemoteServiceState service_state_;
+ bool has_refresh_token_;
bool network_available_;
+ bool sync_enabled_;
- scoped_ptr<SyncTaskManager> task_manager_;
+ // Delete them on worker.
+ scoped_ptr<WorkerObserver> worker_observer_;
+ scoped_ptr<SyncWorkerInterface> sync_worker_;
- scoped_ptr<SyncEngineContext> context_;
- base::WeakPtrFactory<SyncEngine> weak_ptr_factory_;
+ ObserverList<SyncServiceObserver> service_observers_;
+ ObserverList<FileStatusObserver> file_status_observers_;
+ leveldb::Env* env_override_;
+
+ CallbackTracker callback_tracker_;
+ base::WeakPtrFactory<SyncEngine> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(SyncEngine);
};