- add sources.
[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 ExtensionService;
20
21 namespace base {
22 class SequencedTaskRunner;
23 }
24
25 namespace drive {
26 class DriveAPIService;
27 class DriveNotificationManager;
28 }
29
30 namespace sync_file_system {
31 namespace drive_backend {
32
33 class LocalToRemoteSyncer;
34 class MetadataDatabase;
35 class RemoteToLocalSyncer;
36 class SyncEngineInitializer;
37
38 class SyncEngine : public RemoteFileSyncService,
39                    public LocalChangeProcessor,
40                    public SyncTaskManager::Client,
41                    public drive::DriveNotificationObserver,
42                    public drive::DriveServiceObserver,
43                    public net::NetworkChangeNotifier::NetworkChangeObserver,
44                    public SyncEngineContext {
45  public:
46   typedef Observer SyncServiceObserver;
47
48   SyncEngine(const base::FilePath& base_dir,
49              base::SequencedTaskRunner* task_runner,
50              scoped_ptr<drive::DriveAPIService> drive_service,
51              drive::DriveNotificationManager* notification_manager,
52              ExtensionService* extension_service);
53   virtual ~SyncEngine();
54
55   void Initialize();
56
57   // RemoteFileSyncService overrides.
58   virtual void AddServiceObserver(SyncServiceObserver* observer) OVERRIDE;
59   virtual void AddFileStatusObserver(FileStatusObserver* observer) OVERRIDE;
60   virtual void RegisterOrigin(
61       const GURL& origin,
62       const SyncStatusCallback& callback) OVERRIDE;
63   virtual void EnableOrigin(
64       const GURL& origin,
65       const SyncStatusCallback& callback) OVERRIDE;
66   virtual void DisableOrigin(
67       const GURL& origin,
68       const SyncStatusCallback& callback) OVERRIDE;
69   virtual void UninstallOrigin(
70       const GURL& origin,
71       UninstallFlag flag,
72       const SyncStatusCallback& callback) OVERRIDE;
73   virtual void ProcessRemoteChange(const SyncFileCallback& callback) OVERRIDE;
74   virtual void SetRemoteChangeProcessor(
75       RemoteChangeProcessor* processor) OVERRIDE;
76   virtual LocalChangeProcessor* GetLocalChangeProcessor() OVERRIDE;
77   virtual bool IsConflicting(const fileapi::FileSystemURL& url) OVERRIDE;
78   virtual RemoteServiceState GetCurrentState() const OVERRIDE;
79   virtual void GetOriginStatusMap(OriginStatusMap* status_map) OVERRIDE;
80   virtual scoped_ptr<base::ListValue> DumpFiles(const GURL& origin) OVERRIDE;
81   virtual void SetSyncEnabled(bool enabled) OVERRIDE;
82   virtual SyncStatusCode SetConflictResolutionPolicy(
83       ConflictResolutionPolicy policy) OVERRIDE;
84   virtual ConflictResolutionPolicy GetConflictResolutionPolicy() const OVERRIDE;
85   virtual void GetRemoteVersions(
86       const fileapi::FileSystemURL& url,
87       const RemoteVersionsCallback& callback) OVERRIDE;
88   virtual void DownloadRemoteVersion(
89       const fileapi::FileSystemURL& url,
90       const std::string& version_id,
91       const DownloadVersionCallback& callback) OVERRIDE;
92
93   // LocalChangeProcessor overrides.
94   virtual void ApplyLocalChange(
95       const FileChange& change,
96       const base::FilePath& local_file_path,
97       const SyncFileMetadata& local_file_metadata,
98       const fileapi::FileSystemURL& url,
99       const SyncStatusCallback& callback) OVERRIDE;
100
101   // SyncTaskManager::Client overrides.
102   virtual void MaybeScheduleNextTask() OVERRIDE;
103   virtual void NotifyLastOperationStatus(SyncStatusCode sync_status) OVERRIDE;
104
105   // drive::DriveNotificationObserver overrides.
106   virtual void OnNotificationReceived() OVERRIDE;
107   virtual void OnPushNotificationEnabled(bool enabled) OVERRIDE;
108
109   // drive::DriveServiceObserver overrides.
110   virtual void OnReadyToSendRequests() OVERRIDE;
111   virtual void OnRefreshTokenInvalid() OVERRIDE;
112
113   // net::NetworkChangeNotifier::NetworkChangeObserver overrides.
114   virtual void OnNetworkChanged(
115       net::NetworkChangeNotifier::ConnectionType type) OVERRIDE;
116
117   // SyncEngineContext overrides.
118   virtual drive::DriveServiceInterface* GetDriveService() OVERRIDE;
119   virtual MetadataDatabase* GetMetadataDatabase() OVERRIDE;
120
121  private:
122   void DoDisableApp(const std::string& app_id,
123                     const SyncStatusCallback& callback);
124   void DoEnableApp(const std::string& app_id,
125                    const SyncStatusCallback& callback);
126
127   void DidInitialize(SyncEngineInitializer* initializer,
128                      SyncStatusCode status);
129   void DidProcessRemoteChange(RemoteToLocalSyncer* syncer,
130                               const SyncFileCallback& callback,
131                               SyncStatusCode status);
132   void DidApplyLocalChange(LocalToRemoteSyncer* syncer,
133                            const SyncStatusCallback& callback,
134                            SyncStatusCode status);
135   void DidFetchChangeList(SyncStatusCallback& callback);
136
137   void MaybeStartFetchChanges();
138   void UpdateServiceStateFromSyncStatusCode(
139       SyncStatusCode state,
140       const std::string& description);
141   void UpdateServiceState(RemoteServiceState state,
142                           const std::string& description);
143
144   base::FilePath base_dir_;
145   base::FilePath temporary_file_dir_;
146
147   scoped_refptr<base::SequencedTaskRunner> task_runner_;
148
149   scoped_ptr<drive::DriveAPIService> drive_service_;
150   scoped_ptr<MetadataDatabase> metadata_database_;
151
152   // These external services are not owned by SyncEngine.
153   // The owner of the SyncEngine is responsible for their lifetime.
154   // I.e. the owner should declare the dependency explicitly by calling
155   // BrowserContextKeyedService::DependsOn().
156   drive::DriveNotificationManager* notification_manager_;
157   ExtensionService* extension_service_;
158
159   ObserverList<SyncServiceObserver> service_observers_;
160   ObserverList<FileStatusObserver> file_status_observers_;
161   RemoteChangeProcessor* remote_change_processor_;
162
163   RemoteServiceState service_state_;
164
165   bool should_check_remote_change_;
166   base::TimeTicks time_to_check_changes_;
167
168   bool sync_enabled_;
169   ConflictResolutionPolicy conflict_resolution_policy_;
170   bool network_available_;
171
172   scoped_ptr<SyncTaskManager> task_manager_;
173
174   base::WeakPtrFactory<SyncEngine> weak_ptr_factory_;
175
176   DISALLOW_COPY_AND_ASSIGN(SyncEngine);
177 };
178
179 }  // namespace drive_backend
180 }  // namespace sync_file_system
181
182 #endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_SYNC_ENGINE_H_