Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync_file_system / drive_backend / sync_engine.h
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 "base/memory/scoped_ptr.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/observer_list.h"
11 #include "chrome/browser/drive/drive_notification_observer.h"
12 #include "chrome/browser/drive/drive_service_interface.h"
13 #include "chrome/browser/sync_file_system/drive_backend/sync_engine_context.h"
14 #include "chrome/browser/sync_file_system/local_change_processor.h"
15 #include "chrome/browser/sync_file_system/remote_file_sync_service.h"
16 #include "chrome/browser/sync_file_system/sync_task_manager.h"
17 #include "net/base/network_change_notifier.h"
18
19 class ExtensionServiceInterface;
20 class ProfileOAuth2TokenService;
21
22 namespace base {
23 class SequencedTaskRunner;
24 }
25
26 namespace drive {
27 class DriveServiceInterface;
28 class DriveNotificationManager;
29 }
30
31 namespace leveldb {
32 class Env;
33 }
34
35 namespace sync_file_system {
36 namespace drive_backend {
37
38 class LocalToRemoteSyncer;
39 class MetadataDatabase;
40 class RemoteToLocalSyncer;
41 class SyncEngineInitializer;
42
43 class SyncEngine : public RemoteFileSyncService,
44                    public LocalChangeProcessor,
45                    public SyncTaskManager::Client,
46                    public drive::DriveNotificationObserver,
47                    public drive::DriveServiceObserver,
48                    public net::NetworkChangeNotifier::NetworkChangeObserver,
49                    public SyncEngineContext {
50  public:
51   typedef Observer SyncServiceObserver;
52
53   static scoped_ptr<SyncEngine> CreateForBrowserContext(
54       content::BrowserContext* context);
55   static void AppendDependsOnFactories(
56       std::set<BrowserContextKeyedServiceFactory*>* factories);
57
58   virtual ~SyncEngine();
59
60   void Initialize();
61
62   // RemoteFileSyncService overrides.
63   virtual void AddServiceObserver(SyncServiceObserver* observer) OVERRIDE;
64   virtual void AddFileStatusObserver(FileStatusObserver* observer) OVERRIDE;
65   virtual void RegisterOrigin(
66       const GURL& origin,
67       const SyncStatusCallback& callback) OVERRIDE;
68   virtual void EnableOrigin(
69       const GURL& origin,
70       const SyncStatusCallback& callback) OVERRIDE;
71   virtual void DisableOrigin(
72       const GURL& origin,
73       const SyncStatusCallback& callback) OVERRIDE;
74   virtual void UninstallOrigin(
75       const GURL& origin,
76       UninstallFlag flag,
77       const SyncStatusCallback& callback) OVERRIDE;
78   virtual void ProcessRemoteChange(const SyncFileCallback& callback) OVERRIDE;
79   virtual void SetRemoteChangeProcessor(
80       RemoteChangeProcessor* processor) OVERRIDE;
81   virtual LocalChangeProcessor* GetLocalChangeProcessor() OVERRIDE;
82   virtual bool IsConflicting(const fileapi::FileSystemURL& url) OVERRIDE;
83   virtual RemoteServiceState GetCurrentState() const OVERRIDE;
84   virtual void GetOriginStatusMap(OriginStatusMap* status_map) OVERRIDE;
85   virtual scoped_ptr<base::ListValue> DumpFiles(const GURL& origin) OVERRIDE;
86   virtual scoped_ptr<base::ListValue> DumpDatabase() OVERRIDE;
87   virtual void SetSyncEnabled(bool enabled) OVERRIDE;
88   virtual SyncStatusCode SetConflictResolutionPolicy(
89       ConflictResolutionPolicy policy) OVERRIDE;
90   virtual ConflictResolutionPolicy GetConflictResolutionPolicy() const OVERRIDE;
91   virtual void GetRemoteVersions(
92       const fileapi::FileSystemURL& url,
93       const RemoteVersionsCallback& callback) OVERRIDE;
94   virtual void DownloadRemoteVersion(
95       const fileapi::FileSystemURL& url,
96       const std::string& version_id,
97       const DownloadVersionCallback& callback) OVERRIDE;
98   virtual void PromoteDemotedChanges() OVERRIDE;
99
100   // LocalChangeProcessor overrides.
101   virtual void ApplyLocalChange(
102       const FileChange& local_change,
103       const base::FilePath& local_path,
104       const SyncFileMetadata& local_metadata,
105       const fileapi::FileSystemURL& url,
106       const SyncStatusCallback& callback) OVERRIDE;
107
108   // SyncTaskManager::Client overrides.
109   virtual void MaybeScheduleNextTask() OVERRIDE;
110   virtual void NotifyLastOperationStatus(SyncStatusCode sync_status,
111                                          bool used_network) OVERRIDE;
112
113   // drive::DriveNotificationObserver overrides.
114   virtual void OnNotificationReceived() OVERRIDE;
115   virtual void OnPushNotificationEnabled(bool enabled) OVERRIDE;
116
117   // drive::DriveServiceObserver overrides.
118   virtual void OnReadyToSendRequests() OVERRIDE;
119   virtual void OnRefreshTokenInvalid() OVERRIDE;
120
121   // net::NetworkChangeNotifier::NetworkChangeObserver overrides.
122   virtual void OnNetworkChanged(
123       net::NetworkChangeNotifier::ConnectionType type) OVERRIDE;
124
125   // SyncEngineContext overrides.
126   virtual drive::DriveServiceInterface* GetDriveService() OVERRIDE;
127   virtual drive::DriveUploaderInterface* GetDriveUploader() OVERRIDE;
128   virtual MetadataDatabase* GetMetadataDatabase() OVERRIDE;
129   virtual RemoteChangeProcessor* GetRemoteChangeProcessor() OVERRIDE;
130   virtual base::SequencedTaskRunner* GetBlockingTaskRunner() OVERRIDE;
131
132  private:
133   friend class DriveBackendSyncTest;
134   friend class SyncEngineTest;
135
136   SyncEngine(const base::FilePath& base_dir,
137              base::SequencedTaskRunner* task_runner,
138              scoped_ptr<drive::DriveServiceInterface> drive_service,
139              scoped_ptr<drive::DriveUploaderInterface> drive_uploader,
140              drive::DriveNotificationManager* notification_manager,
141              ExtensionServiceInterface* extension_service,
142              ProfileOAuth2TokenService* auth_token_service,
143              leveldb::Env* env_override);
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 DidProcessRemoteChange(RemoteToLocalSyncer* syncer,
154                               const SyncFileCallback& callback,
155                               SyncStatusCode status);
156   void DidApplyLocalChange(LocalToRemoteSyncer* syncer,
157                            const SyncStatusCallback& callback,
158                            SyncStatusCode status);
159
160   void MaybeStartFetchChanges();
161   void DidResolveConflict(SyncStatusCode status);
162   void DidFetchChanges(SyncStatusCode status);
163
164   void UpdateServiceStateFromSyncStatusCode(SyncStatusCode state,
165                                             bool used_network);
166   void UpdateServiceState(RemoteServiceState state,
167                           const std::string& description);
168   void UpdateRegisteredApps();
169
170   base::FilePath base_dir_;
171   base::FilePath temporary_file_dir_;
172
173   scoped_refptr<base::SequencedTaskRunner> task_runner_;
174   leveldb::Env* env_override_;
175
176   scoped_ptr<drive::DriveServiceInterface> drive_service_;
177   scoped_ptr<drive::DriveUploaderInterface> drive_uploader_;
178   scoped_ptr<MetadataDatabase> metadata_database_;
179
180   // These external services are not owned by SyncEngine.
181   // The owner of the SyncEngine is responsible for their lifetime.
182   // I.e. the owner should declare the dependency explicitly by calling
183   // BrowserContextKeyedService::DependsOn().
184   drive::DriveNotificationManager* notification_manager_;
185   ExtensionServiceInterface* extension_service_;
186   ProfileOAuth2TokenService* auth_token_service_;
187
188   ObserverList<SyncServiceObserver> service_observers_;
189   ObserverList<FileStatusObserver> file_status_observers_;
190   RemoteChangeProcessor* remote_change_processor_;
191
192   RemoteServiceState service_state_;
193
194   bool should_check_conflict_;
195   bool should_check_remote_change_;
196   bool listing_remote_changes_;
197   base::TimeTicks time_to_check_changes_;
198
199   bool sync_enabled_;
200   ConflictResolutionPolicy conflict_resolution_policy_;
201   bool network_available_;
202
203   scoped_ptr<SyncTaskManager> task_manager_;
204
205   base::WeakPtrFactory<SyncEngine> weak_ptr_factory_;
206
207   DISALLOW_COPY_AND_ASSIGN(SyncEngine);
208 };
209
210 }  // namespace drive_backend
211 }  // namespace sync_file_system
212
213 #endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_SYNC_ENGINE_H_