sync_file_system_service.h revision 424c4d7b64af9d0d8fd9624f381f469654d5e3d2
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/weak_ptr.h"
15#include "base/observer_list.h"
16#include "base/timer/timer.h"
17#include "chrome/browser/sync/profile_sync_service_observer.h"
18#include "chrome/browser/sync_file_system/conflict_resolution_policy.h"
19#include "chrome/browser/sync_file_system/file_status_observer.h"
20#include "chrome/browser/sync_file_system/local/local_file_sync_service.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_service_state.h"
24#include "components/browser_context_keyed_service/browser_context_keyed_service.h"
25#include "content/public/browser/notification_observer.h"
26#include "content/public/browser/notification_registrar.h"
27#include "url/gurl.h"
28
29class ProfileSyncServiceBase;
30
31namespace fileapi {
32class FileSystemContext;
33}
34
35namespace sync_file_system {
36
37class SyncEventObserver;
38
39class SyncFileSystemService
40    : public BrowserContextKeyedService,
41      public ProfileSyncServiceObserver,
42      public LocalFileSyncService::Observer,
43      public RemoteFileSyncService::Observer,
44      public FileStatusObserver,
45      public content::NotificationObserver,
46      public base::SupportsWeakPtr<SyncFileSystemService> {
47 public:
48  typedef base::Callback<void(const base::ListValue* files)> DumpFilesCallback;
49
50  // BrowserContextKeyedService overrides.
51  virtual void Shutdown() OVERRIDE;
52
53  void InitializeForApp(
54      fileapi::FileSystemContext* file_system_context,
55      const GURL& app_origin,
56      const SyncStatusCallback& callback);
57
58  SyncServiceState GetSyncServiceState();
59  void GetExtensionStatusMap(std::map<GURL, std::string>* status_map);
60  void DumpFiles(const GURL& origin, const DumpFilesCallback& callback);
61
62  // Returns the file |url|'s sync status.
63  void GetFileSyncStatus(
64      const fileapi::FileSystemURL& url,
65      const SyncFileStatusCallback& callback);
66
67  void AddSyncEventObserver(SyncEventObserver* observer);
68  void RemoveSyncEventObserver(SyncEventObserver* observer);
69
70  ConflictResolutionPolicy GetConflictResolutionPolicy() const;
71  SyncStatusCode SetConflictResolutionPolicy(ConflictResolutionPolicy policy);
72
73 private:
74  friend class SyncFileSystemServiceFactory;
75  friend class SyncFileSystemServiceTest;
76  friend struct base::DefaultDeleter<SyncFileSystemService>;
77
78  explicit SyncFileSystemService(Profile* profile);
79  virtual ~SyncFileSystemService();
80
81  void Initialize(scoped_ptr<LocalFileSyncService> local_file_service,
82                  scoped_ptr<RemoteFileSyncService> remote_file_service);
83
84  // Callbacks for InitializeForApp.
85  void DidInitializeFileSystem(const GURL& app_origin,
86                               const SyncStatusCallback& callback,
87                               SyncStatusCode status);
88  void DidRegisterOrigin(const GURL& app_origin,
89                         const SyncStatusCallback& callback,
90                         SyncStatusCode status);
91
92  void DidInitializeFileSystemForDump(const GURL& app_origin,
93                                      const DumpFilesCallback& callback,
94                                      SyncStatusCode status);
95
96  // Overrides sync_enabled_ setting. This should be called only by tests.
97  void SetSyncEnabledForTesting(bool enabled);
98
99  // Called when following observer methods are called:
100  // - OnLocalChangeAvailable()
101  // - OnRemoteChangeAvailable()
102  // - OnRemoteServiceStateUpdated()
103  void MaybeStartSync();
104
105  // Called from MaybeStartSync(). (Should not be called from others)
106  void StartRemoteSync();
107  void StartLocalSync();
108
109  // Callbacks for remote/local sync.
110  void DidProcessRemoteChange(SyncStatusCode status,
111                              const fileapi::FileSystemURL& url);
112  void DidProcessLocalChange(SyncStatusCode status,
113                             const fileapi::FileSystemURL& url);
114
115  void DidGetLocalChangeStatus(const SyncFileStatusCallback& callback,
116                               SyncStatusCode status,
117                               bool has_pending_local_changes);
118
119  void OnSyncEnabledForRemoteSync();
120
121  // RemoteFileSyncService::Observer overrides.
122  virtual void OnLocalChangeAvailable(int64 pending_changes) OVERRIDE;
123
124  // LocalFileSyncService::Observer overrides.
125  virtual void OnRemoteChangeQueueUpdated(int64 pending_changes) OVERRIDE;
126  virtual void OnRemoteServiceStateUpdated(
127      RemoteServiceState state,
128      const std::string& description) OVERRIDE;
129
130  // content::NotificationObserver implementation.
131  virtual void Observe(int type,
132                       const content::NotificationSource& source,
133                       const content::NotificationDetails& details) OVERRIDE;
134
135  void HandleExtensionInstalled(const content::NotificationDetails& details);
136  void HandleExtensionUnloaded(int type,
137                               const content::NotificationDetails& details);
138  void HandleExtensionUninstalled(int type,
139                                  const content::NotificationDetails& details);
140  void HandleExtensionEnabled(int type,
141                              const content::NotificationDetails& details);
142
143  // ProfileSyncServiceObserver:
144  virtual void OnStateChanged() OVERRIDE;
145
146  // SyncFileStatusObserver:
147  virtual void OnFileStatusChanged(
148      const fileapi::FileSystemURL& url,
149      SyncFileStatus sync_status,
150      SyncAction action_taken,
151      SyncDirection direction) OVERRIDE;
152
153  // Check the profile's sync preference settings and call
154  // remote_file_service_->SetSyncEnabled() to update the status.
155  // |profile_sync_service| must be non-null.
156  void UpdateSyncEnabledStatus(ProfileSyncServiceBase* profile_sync_service);
157
158  Profile* profile_;
159  content::NotificationRegistrar registrar_;
160
161  int64 pending_local_changes_;
162  int64 pending_remote_changes_;
163
164  scoped_ptr<LocalFileSyncService> local_file_service_;
165  scoped_ptr<RemoteFileSyncService> remote_file_service_;
166
167  bool local_sync_running_;
168  bool remote_sync_running_;
169
170  // If a remote sync is returned with SYNC_STATUS_FILE_BUSY we mark this
171  // true and register the busy file URL to wait for a sync enabled event
172  // for the URL. When this flag is set to true it won't be worth trying
173  // another remote sync.
174  bool is_waiting_remote_sync_enabled_;
175
176  // Indicates if sync is currently enabled or not.
177  bool sync_enabled_;
178
179  base::OneShotTimer<SyncFileSystemService> sync_retry_timer_;
180
181  ObserverList<SyncEventObserver> observers_;
182
183  DISALLOW_COPY_AND_ASSIGN(SyncFileSystemService);
184};
185
186}  // namespace sync_file_system
187
188#endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNC_FILE_SYSTEM_SERVICE_H_
189