Home | History | Annotate | Download | only in drive_backend
      1 // Copyright 2013 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_ENGINE_H_
      6 #define CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_SYNC_ENGINE_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/callback_tracker.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 "components/signin/core/browser/signin_manager_base.h"
     22 #include "net/base/network_change_notifier.h"
     23 
     24 class ExtensionServiceInterface;
     25 class ProfileOAuth2TokenService;
     26 
     27 namespace base {
     28 class SequencedTaskRunner;
     29 }
     30 
     31 namespace drive {
     32 class DriveServiceInterface;
     33 class DriveNotificationManager;
     34 class DriveUploaderInterface;
     35 }
     36 
     37 namespace leveldb {
     38 class Env;
     39 }
     40 
     41 namespace net {
     42 class URLRequestContextGetter;
     43 }
     44 
     45 namespace sync_file_system {
     46 
     47 class RemoteChangeProcessor;
     48 
     49 namespace drive_backend {
     50 
     51 class DriveServiceWrapper;
     52 class DriveUploaderWrapper;
     53 class MetadataDatabase;
     54 class RemoteChangeProcessorOnWorker;
     55 class RemoteChangeProcessorWrapper;
     56 class SyncTaskManager;
     57 class SyncWorkerInterface;
     58 
     59 class SyncEngine : public RemoteFileSyncService,
     60                    public LocalChangeProcessor,
     61                    public drive::DriveNotificationObserver,
     62                    public drive::DriveServiceObserver,
     63                    public net::NetworkChangeNotifier::NetworkChangeObserver,
     64                    public SigninManagerBase::Observer {
     65  public:
     66   typedef RemoteFileSyncService::Observer SyncServiceObserver;
     67 
     68   static scoped_ptr<SyncEngine> CreateForBrowserContext(
     69       content::BrowserContext* context,
     70       TaskLogger* task_logger);
     71   static void AppendDependsOnFactories(
     72       std::set<BrowserContextKeyedServiceFactory*>* factories);
     73 
     74   virtual ~SyncEngine();
     75   void Reset();
     76 
     77   // Can be called more than once.
     78   void Initialize();
     79 
     80   void InitializeForTesting(
     81       scoped_ptr<drive::DriveServiceInterface> drive_service,
     82       scoped_ptr<drive::DriveUploaderInterface> drive_uploader,
     83       scoped_ptr<SyncWorkerInterface> sync_worker);
     84   void InitializeInternal(
     85       scoped_ptr<drive::DriveServiceInterface> drive_service,
     86       scoped_ptr<drive::DriveUploaderInterface> drive_uploader,
     87       scoped_ptr<SyncWorkerInterface> sync_worker);
     88 
     89   // RemoteFileSyncService overrides.
     90   virtual void AddServiceObserver(SyncServiceObserver* observer) OVERRIDE;
     91   virtual void AddFileStatusObserver(FileStatusObserver* observer) OVERRIDE;
     92   virtual void RegisterOrigin(
     93       const GURL& origin,
     94       const SyncStatusCallback& callback) OVERRIDE;
     95   virtual void EnableOrigin(
     96       const GURL& origin,
     97       const SyncStatusCallback& callback) OVERRIDE;
     98   virtual void DisableOrigin(
     99       const GURL& origin,
    100       const SyncStatusCallback& callback) OVERRIDE;
    101   virtual void UninstallOrigin(
    102       const GURL& origin,
    103       UninstallFlag flag,
    104       const SyncStatusCallback& callback) OVERRIDE;
    105   virtual void ProcessRemoteChange(const SyncFileCallback& callback) OVERRIDE;
    106   virtual void SetRemoteChangeProcessor(
    107       RemoteChangeProcessor* processor) OVERRIDE;
    108   virtual LocalChangeProcessor* GetLocalChangeProcessor() OVERRIDE;
    109   virtual RemoteServiceState GetCurrentState() const OVERRIDE;
    110   virtual void GetOriginStatusMap(const StatusMapCallback& callback) OVERRIDE;
    111   virtual void DumpFiles(const GURL& origin,
    112                          const ListCallback& callback) OVERRIDE;
    113   virtual void DumpDatabase(const ListCallback& callback) OVERRIDE;
    114   virtual void SetSyncEnabled(bool enabled) OVERRIDE;
    115   virtual void PromoteDemotedChanges() OVERRIDE;
    116 
    117   // LocalChangeProcessor overrides.
    118   virtual void ApplyLocalChange(
    119       const FileChange& local_change,
    120       const base::FilePath& local_path,
    121       const SyncFileMetadata& local_metadata,
    122       const fileapi::FileSystemURL& url,
    123       const SyncStatusCallback& callback) OVERRIDE;
    124 
    125   // drive::DriveNotificationObserver overrides.
    126   virtual void OnNotificationReceived() OVERRIDE;
    127   virtual void OnPushNotificationEnabled(bool enabled) OVERRIDE;
    128 
    129   // drive::DriveServiceObserver overrides.
    130   virtual void OnReadyToSendRequests() OVERRIDE;
    131   virtual void OnRefreshTokenInvalid() OVERRIDE;
    132 
    133   // net::NetworkChangeNotifier::NetworkChangeObserver overrides.
    134   virtual void OnNetworkChanged(
    135       net::NetworkChangeNotifier::ConnectionType type) OVERRIDE;
    136 
    137   // SigninManagerBase::Observer overrides.
    138   virtual void GoogleSigninFailed(const GoogleServiceAuthError& error) OVERRIDE;
    139   virtual void GoogleSigninSucceeded(const std::string& username,
    140                                      const std::string& password) OVERRIDE;
    141   virtual void GoogleSignedOut(const std::string& username) OVERRIDE;
    142 
    143  private:
    144   class WorkerObserver;
    145 
    146   friend class DriveBackendSyncTest;
    147   friend class SyncEngineTest;
    148 
    149   SyncEngine(base::SingleThreadTaskRunner* ui_task_runner,
    150              base::SequencedTaskRunner* worker_task_runner,
    151              base::SequencedTaskRunner* file_task_runner,
    152              base::SequencedTaskRunner* drive_task_runner,
    153              const base::FilePath& sync_file_system_dir,
    154              TaskLogger* task_logger,
    155              drive::DriveNotificationManager* notification_manager,
    156              ExtensionServiceInterface* extension_service,
    157              SigninManagerBase* signin_manager,
    158              ProfileOAuth2TokenService* token_service,
    159              net::URLRequestContextGetter* request_context,
    160              leveldb::Env* env_override);
    161 
    162   // Called by WorkerObserver.
    163   void OnPendingFileListUpdated(int item_count);
    164   void OnFileStatusChanged(const fileapi::FileSystemURL& url,
    165                            SyncFileStatus file_status,
    166                            SyncAction sync_action,
    167                            SyncDirection direction);
    168   void UpdateServiceState(RemoteServiceState state,
    169                           const std::string& description);
    170 
    171   SyncStatusCallback TrackCallback(const SyncStatusCallback& callback);
    172 
    173   scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
    174   scoped_refptr<base::SequencedTaskRunner> worker_task_runner_;
    175   scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
    176   scoped_refptr<base::SequencedTaskRunner> drive_task_runner_;
    177 
    178   const base::FilePath sync_file_system_dir_;
    179   TaskLogger* task_logger_;
    180 
    181   // These external services are not owned by SyncEngine.
    182   // The owner of the SyncEngine is responsible for their lifetime.
    183   // I.e. the owner should declare the dependency explicitly by calling
    184   // KeyedService::DependsOn().
    185   drive::DriveNotificationManager* notification_manager_;
    186   ExtensionServiceInterface* extension_service_;
    187   SigninManagerBase* signin_manager_;
    188   ProfileOAuth2TokenService* token_service_;
    189 
    190   scoped_refptr<net::URLRequestContextGetter> request_context_;
    191 
    192   scoped_ptr<drive::DriveServiceInterface> drive_service_;
    193   scoped_ptr<DriveServiceWrapper> drive_service_wrapper_;
    194   scoped_ptr<drive::DriveUploaderInterface> drive_uploader_;
    195   scoped_ptr<DriveUploaderWrapper> drive_uploader_wrapper_;
    196 
    197   RemoteChangeProcessor* remote_change_processor_;  // Not owned.
    198   scoped_ptr<RemoteChangeProcessorWrapper> remote_change_processor_wrapper_;
    199   // Delete this on worker.
    200   scoped_ptr<RemoteChangeProcessorOnWorker> remote_change_processor_on_worker_;
    201 
    202   RemoteServiceState service_state_;
    203   bool sync_enabled_;
    204 
    205   // Delete them on worker.
    206   scoped_ptr<WorkerObserver> worker_observer_;
    207   scoped_ptr<SyncWorkerInterface> sync_worker_;
    208 
    209   ObserverList<SyncServiceObserver> service_observers_;
    210   ObserverList<FileStatusObserver> file_status_observers_;
    211   leveldb::Env* env_override_;
    212 
    213   CallbackTracker callback_tracker_;
    214 
    215   base::WeakPtrFactory<SyncEngine> weak_ptr_factory_;
    216   DISALLOW_COPY_AND_ASSIGN(SyncEngine);
    217 };
    218 
    219 }  // namespace drive_backend
    220 }  // namespace sync_file_system
    221 
    222 #endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_SYNC_ENGINE_H_
    223