sync_file_system_service.h revision d0247b1b59f9c528cb6df88b4f2b9afaf80d181e
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  class SyncRunner;
75
76  friend class SyncFileSystemServiceFactory;
77  friend class SyncFileSystemServiceTest;
78  friend struct base::DefaultDeleter<SyncFileSystemService>;
79
80  explicit SyncFileSystemService(Profile* profile);
81  virtual ~SyncFileSystemService();
82
83  void Initialize(scoped_ptr<LocalFileSyncService> local_file_service,
84                  scoped_ptr<RemoteFileSyncService> remote_file_service);
85
86  // Callbacks for InitializeForApp.
87  void DidInitializeFileSystem(const GURL& app_origin,
88                               const SyncStatusCallback& callback,
89                               SyncStatusCode status);
90  void DidRegisterOrigin(const GURL& app_origin,
91                         const SyncStatusCallback& callback,
92                         SyncStatusCode status);
93
94  void DidInitializeFileSystemForDump(const GURL& app_origin,
95                                      const DumpFilesCallback& callback,
96                                      SyncStatusCode status);
97
98  // Overrides sync_enabled_ setting. This should be called only by tests.
99  void SetSyncEnabledForTesting(bool enabled);
100
101  void StartLocalSync(const SyncStatusCallback& callback);
102  void StartRemoteSync(const SyncStatusCallback& callback);
103
104  // Callbacks for remote/local sync.
105  void DidProcessRemoteChange(const SyncStatusCallback& callback,
106                              SyncStatusCode status,
107                              const fileapi::FileSystemURL& url);
108  void DidProcessLocalChange(const SyncStatusCallback& callback,
109                             SyncStatusCode status,
110                             const fileapi::FileSystemURL& url);
111
112  void DidGetLocalChangeStatus(const SyncFileStatusCallback& callback,
113                               SyncStatusCode status,
114                               bool has_pending_local_changes);
115
116  void OnSyncEnabledForRemoteSync();
117
118  // RemoteFileSyncService::Observer overrides.
119  virtual void OnLocalChangeAvailable(int64 pending_changes) OVERRIDE;
120
121  // LocalFileSyncService::Observer overrides.
122  virtual void OnRemoteChangeQueueUpdated(int64 pending_changes) OVERRIDE;
123  virtual void OnRemoteServiceStateUpdated(
124      RemoteServiceState state,
125      const std::string& description) OVERRIDE;
126
127  // content::NotificationObserver implementation.
128  virtual void Observe(int type,
129                       const content::NotificationSource& source,
130                       const content::NotificationDetails& details) OVERRIDE;
131
132  void HandleExtensionInstalled(const content::NotificationDetails& details);
133  void HandleExtensionUnloaded(int type,
134                               const content::NotificationDetails& details);
135  void HandleExtensionUninstalled(int type,
136                                  const content::NotificationDetails& details);
137  void HandleExtensionEnabled(int type,
138                              const content::NotificationDetails& details);
139
140  // ProfileSyncServiceObserver:
141  virtual void OnStateChanged() OVERRIDE;
142
143  // SyncFileStatusObserver:
144  virtual void OnFileStatusChanged(
145      const fileapi::FileSystemURL& url,
146      SyncFileStatus sync_status,
147      SyncAction action_taken,
148      SyncDirection direction) OVERRIDE;
149
150  // Check the profile's sync preference settings and call
151  // remote_file_service_->SetSyncEnabled() to update the status.
152  // |profile_sync_service| must be non-null.
153  void UpdateSyncEnabledStatus(ProfileSyncServiceBase* profile_sync_service);
154
155  Profile* profile_;
156  content::NotificationRegistrar registrar_;
157
158  scoped_ptr<LocalFileSyncService> local_file_service_;
159  scoped_ptr<RemoteFileSyncService> remote_file_service_;
160
161  scoped_ptr<SyncRunner> local_sync_;
162  scoped_ptr<SyncRunner> remote_sync_;
163
164  // Indicates if sync is currently enabled or not.
165  bool sync_enabled_;
166
167  ObserverList<SyncEventObserver> observers_;
168
169  DISALLOW_COPY_AND_ASSIGN(SyncFileSystemService);
170};
171
172}  // namespace sync_file_system
173
174#endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNC_FILE_SYSTEM_SERVICE_H_
175