sync_file_system_service.h revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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_service_state.h"
24#include "components/keyed_service/core/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 Profile;
30class ProfileSyncServiceBase;
31
32namespace fileapi {
33class FileSystemContext;
34}
35
36namespace sync_file_system {
37
38class LocalFileSyncService;
39class LocalSyncRunner;
40class RemoteSyncRunner;
41class SyncEventObserver;
42class SyncProcessRunner;
43
44class SyncFileSystemService
45    : public KeyedService,
46      public ProfileSyncServiceObserver,
47      public FileStatusObserver,
48      public content::NotificationObserver,
49      public base::SupportsWeakPtr<SyncFileSystemService> {
50 public:
51  typedef base::Callback<void(const base::ListValue* files)> DumpFilesCallback;
52
53  // KeyedService overrides.
54  virtual void Shutdown() OVERRIDE;
55
56  void InitializeForApp(
57      fileapi::FileSystemContext* file_system_context,
58      const GURL& app_origin,
59      const SyncStatusCallback& callback);
60
61  SyncServiceState GetSyncServiceState();
62  void GetExtensionStatusMap(std::map<GURL, std::string>* status_map);
63  void DumpFiles(const GURL& origin, const DumpFilesCallback& callback);
64  scoped_ptr<base::ListValue> DumpDatabase();
65
66  // Returns the file |url|'s sync status.
67  void GetFileSyncStatus(
68      const fileapi::FileSystemURL& url,
69      const SyncFileStatusCallback& callback);
70
71  void AddSyncEventObserver(SyncEventObserver* observer);
72  void RemoveSyncEventObserver(SyncEventObserver* observer);
73
74  ConflictResolutionPolicy GetConflictResolutionPolicy(const GURL& origin);
75  SyncStatusCode SetConflictResolutionPolicy(const GURL& origin,
76                                             ConflictResolutionPolicy policy);
77
78  LocalChangeProcessor* GetLocalChangeProcessor(const GURL& origin);
79
80  void OnSyncIdle();
81
82 private:
83  friend class SyncFileSystemServiceFactory;
84  friend class SyncFileSystemServiceTest;
85  friend struct base::DefaultDeleter<SyncFileSystemService>;
86  friend class LocalSyncRunner;
87  friend class RemoteSyncRunner;
88
89  explicit SyncFileSystemService(Profile* profile);
90  virtual ~SyncFileSystemService();
91
92  void Initialize(scoped_ptr<LocalFileSyncService> local_file_service,
93                  scoped_ptr<RemoteFileSyncService> remote_file_service);
94
95  // Callbacks for InitializeForApp.
96  void DidInitializeFileSystem(const GURL& app_origin,
97                               const SyncStatusCallback& callback,
98                               SyncStatusCode status);
99  void DidRegisterOrigin(const GURL& app_origin,
100                         const SyncStatusCallback& callback,
101                         SyncStatusCode status);
102
103  void DidInitializeFileSystemForDump(const GURL& app_origin,
104                                      const DumpFilesCallback& callback,
105                                      SyncStatusCode status);
106
107  // Overrides sync_enabled_ setting. This should be called only by tests.
108  void SetSyncEnabledForTesting(bool enabled);
109
110  void DidGetLocalChangeStatus(const SyncFileStatusCallback& callback,
111                               SyncStatusCode status,
112                               bool has_pending_local_changes);
113
114  void OnRemoteServiceStateUpdated(RemoteServiceState state,
115                                   const std::string& description);
116
117  // content::NotificationObserver implementation.
118  virtual void Observe(int type,
119                       const content::NotificationSource& source,
120                       const content::NotificationDetails& details) OVERRIDE;
121
122  void HandleExtensionInstalled(const content::NotificationDetails& details);
123  void HandleExtensionUnloaded(int type,
124                               const content::NotificationDetails& details);
125  void HandleExtensionUninstalled(int type,
126                                  const content::NotificationDetails& details);
127  void HandleExtensionEnabled(int type,
128                              const content::NotificationDetails& details);
129
130  // ProfileSyncServiceObserver:
131  virtual void OnStateChanged() OVERRIDE;
132
133  // SyncFileStatusObserver:
134  virtual void OnFileStatusChanged(
135      const fileapi::FileSystemURL& url,
136      SyncFileStatus sync_status,
137      SyncAction action_taken,
138      SyncDirection direction) OVERRIDE;
139
140  // Check the profile's sync preference settings and call
141  // remote_file_service_->SetSyncEnabled() to update the status.
142  // |profile_sync_service| must be non-null.
143  void UpdateSyncEnabledStatus(ProfileSyncServiceBase* profile_sync_service);
144
145  // Runs the SyncProcessRunner method of all sync runners (e.g. for Local sync
146  // and Remote sync).
147  void RunForEachSyncRunners(void(SyncProcessRunner::*method)());
148
149  // Returns the appropriate RemoteFileSyncService for the given origin/app.
150  // (crbug.com/324215)
151  RemoteFileSyncService* GetRemoteService(const GURL& origin);
152
153  Profile* profile_;
154  content::NotificationRegistrar registrar_;
155
156  scoped_ptr<LocalFileSyncService> local_service_;
157  scoped_ptr<RemoteFileSyncService> remote_service_;
158
159  // Holds v2 RemoteFileSyncService, gets created lazily
160  // in case we need to run multiple remote services depending on origin/app.
161  // (crbug.com/324215)
162  scoped_ptr<RemoteFileSyncService> v2_remote_service_;
163
164  // Holds all SyncProcessRunners.
165  ScopedVector<SyncProcessRunner> local_sync_runners_;
166  ScopedVector<SyncProcessRunner> remote_sync_runners_;
167
168  // Indicates if sync is currently enabled or not.
169  bool sync_enabled_;
170
171  ObserverList<SyncEventObserver> observers_;
172
173  DISALLOW_COPY_AND_ASSIGN(SyncFileSystemService);
174};
175
176}  // namespace sync_file_system
177
178#endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNC_FILE_SYSTEM_SERVICE_H_
179