Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync_file_system / sync_file_system_service.h
1 // Copyright (c) 2012 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_SYNC_FILE_SYSTEM_SERVICE_H_
6 #define CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNC_FILE_SYSTEM_SERVICE_H_
7
8 #include <map>
9 #include <string>
10
11 #include "base/basictypes.h"
12 #include "base/callback_forward.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/scoped_vector.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/observer_list.h"
17 #include "base/timer/timer.h"
18 #include "chrome/browser/sync/profile_sync_service_observer.h"
19 #include "chrome/browser/sync_file_system/conflict_resolution_policy.h"
20 #include "chrome/browser/sync_file_system/file_status_observer.h"
21 #include "chrome/browser/sync_file_system/remote_file_sync_service.h"
22 #include "chrome/browser/sync_file_system/sync_callbacks.h"
23 #include "chrome/browser/sync_file_system/sync_process_runner.h"
24 #include "chrome/browser/sync_file_system/sync_service_state.h"
25 #include "chrome/browser/sync_file_system/task_logger.h"
26 #include "components/keyed_service/core/keyed_service.h"
27 #include "extensions/browser/extension_registry_observer.h"
28 #include "url/gurl.h"
29
30 class Profile;
31 class ProfileSyncServiceBase;
32
33 namespace storage {
34 class FileSystemContext;
35 }
36
37 namespace sync_file_system {
38
39 class LocalFileSyncService;
40 class LocalSyncRunner;
41 class RemoteSyncRunner;
42 class SyncEventObserver;
43
44 class SyncFileSystemService
45     : public KeyedService,
46       public SyncProcessRunner::Client,
47       public ProfileSyncServiceObserver,
48       public FileStatusObserver,
49       public extensions::ExtensionRegistryObserver,
50       public base::SupportsWeakPtr<SyncFileSystemService> {
51  public:
52   typedef base::Callback<void(const base::ListValue&)> DumpFilesCallback;
53   typedef base::Callback<void(const RemoteFileSyncService::OriginStatusMap&)>
54       ExtensionStatusMapCallback;
55
56   // KeyedService implementation.
57   virtual void Shutdown() OVERRIDE;
58
59   void InitializeForApp(storage::FileSystemContext* file_system_context,
60                         const GURL& app_origin,
61                         const SyncStatusCallback& callback);
62
63   void GetExtensionStatusMap(const ExtensionStatusMapCallback& callback);
64   void DumpFiles(const GURL& origin, const DumpFilesCallback& callback);
65   void DumpDatabase(const DumpFilesCallback& callback);
66
67   // Returns the file |url|'s sync status.
68   void GetFileSyncStatus(const storage::FileSystemURL& url,
69                          const SyncFileStatusCallback& callback);
70
71   void AddSyncEventObserver(SyncEventObserver* observer);
72   void RemoveSyncEventObserver(SyncEventObserver* observer);
73
74   LocalChangeProcessor* GetLocalChangeProcessor(const GURL& origin);
75
76   // SyncProcessRunner::Client implementations.
77   virtual void OnSyncIdle() OVERRIDE;
78   virtual SyncServiceState GetSyncServiceState() OVERRIDE;
79   virtual SyncFileSystemService* GetSyncService() OVERRIDE;
80
81   void OnPromotionCompleted(int* num_running_jobs);
82   void CheckIfIdle();
83
84   TaskLogger* task_logger() { return &task_logger_; }
85
86   void CallOnIdleForTesting(const base::Closure& callback);
87
88  private:
89   friend class SyncFileSystemServiceFactory;
90   friend class SyncFileSystemServiceTest;
91   friend class SyncFileSystemTest;
92   friend struct base::DefaultDeleter<SyncFileSystemService>;
93   friend class LocalSyncRunner;
94   friend class RemoteSyncRunner;
95
96   explicit SyncFileSystemService(Profile* profile);
97   virtual ~SyncFileSystemService();
98
99   void Initialize(scoped_ptr<LocalFileSyncService> local_file_service,
100                   scoped_ptr<RemoteFileSyncService> remote_file_service);
101
102   // Callbacks for InitializeForApp.
103   void DidInitializeFileSystem(const GURL& app_origin,
104                                const SyncStatusCallback& callback,
105                                SyncStatusCode status);
106   void DidRegisterOrigin(const GURL& app_origin,
107                          const SyncStatusCallback& callback,
108                          SyncStatusCode status);
109
110   void DidInitializeFileSystemForDump(const GURL& app_origin,
111                                       const DumpFilesCallback& callback,
112                                       SyncStatusCode status);
113   void DidDumpFiles(const GURL& app_origin,
114                     const DumpFilesCallback& callback,
115                     scoped_ptr<base::ListValue> files);
116
117   void DidDumpDatabase(const DumpFilesCallback& callback,
118                        scoped_ptr<base::ListValue> list);
119
120   void DidGetExtensionStatusMap(
121       const ExtensionStatusMapCallback& callback,
122       scoped_ptr<RemoteFileSyncService::OriginStatusMap> status_map);
123
124   // Overrides sync_enabled_ setting. This should be called only by tests.
125   void SetSyncEnabledForTesting(bool enabled);
126
127   void DidGetLocalChangeStatus(const SyncFileStatusCallback& callback,
128                                SyncStatusCode status,
129                                bool has_pending_local_changes);
130
131   void OnRemoteServiceStateUpdated(RemoteServiceState state,
132                                    const std::string& description);
133
134   // extensions::ExtensionRegistryObserver implementations.
135   virtual void OnExtensionInstalled(
136       content::BrowserContext* browser_context,
137       const extensions::Extension* extension,
138       bool is_update) OVERRIDE;
139   virtual void OnExtensionUnloaded(
140       content::BrowserContext* browser_context,
141       const extensions::Extension* extension,
142       extensions::UnloadedExtensionInfo::Reason reason) OVERRIDE;
143   virtual void OnExtensionUninstalled(
144       content::BrowserContext* browser_context,
145       const extensions::Extension* extension,
146       extensions::UninstallReason reason) OVERRIDE;
147   virtual void OnExtensionLoaded(
148       content::BrowserContext* browser_context,
149       const extensions::Extension* extension) OVERRIDE;
150
151   // ProfileSyncServiceObserver implementation.
152   virtual void OnStateChanged() OVERRIDE;
153
154   // SyncFileStatusObserver implementation.
155   virtual void OnFileStatusChanged(const storage::FileSystemURL& url,
156                                    SyncFileStatus sync_status,
157                                    SyncAction action_taken,
158                                    SyncDirection direction) OVERRIDE;
159
160   // Check the profile's sync preference settings and call
161   // remote_file_service_->SetSyncEnabled() to update the status.
162   // |profile_sync_service| must be non-null.
163   void UpdateSyncEnabledStatus(ProfileSyncServiceBase* profile_sync_service);
164
165   // Runs the SyncProcessRunner method of all sync runners (e.g. for Local sync
166   // and Remote sync).
167   void RunForEachSyncRunners(void(SyncProcessRunner::*method)());
168
169   // Returns the appropriate RemoteFileSyncService for the given origin/app.
170   // (crbug.com/324215)
171   RemoteFileSyncService* GetRemoteService(const GURL& origin);
172
173   Profile* profile_;
174
175   scoped_ptr<LocalFileSyncService> local_service_;
176   scoped_ptr<RemoteFileSyncService> remote_service_;
177
178   // Holds all SyncProcessRunners.
179   ScopedVector<SyncProcessRunner> local_sync_runners_;
180   ScopedVector<SyncProcessRunner> remote_sync_runners_;
181
182   // Indicates if sync is currently enabled or not.
183   bool sync_enabled_;
184
185   TaskLogger task_logger_;
186   ObserverList<SyncEventObserver> observers_;
187
188   bool promoting_demoted_changes_;
189   base::Closure idle_callback_;
190
191   DISALLOW_COPY_AND_ASSIGN(SyncFileSystemService);
192 };
193
194 }  // namespace sync_file_system
195
196 #endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNC_FILE_SYSTEM_SERVICE_H_