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.
5 #ifndef CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNC_FILE_SYSTEM_SERVICE_H_
6 #define CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNC_FILE_SYSTEM_SERVICE_H_
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"
29 class ProfileSyncServiceBase
;
32 class FileSystemContext
;
35 namespace sync_file_system
{
37 class SyncEventObserver
;
39 class 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
> {
48 typedef base::Callback
<void(const base::ListValue
* files
)> DumpFilesCallback
;
50 // BrowserContextKeyedService overrides.
51 virtual void Shutdown() OVERRIDE
;
53 void InitializeForApp(
54 fileapi::FileSystemContext
* file_system_context
,
55 const GURL
& app_origin
,
56 const SyncStatusCallback
& callback
);
58 SyncServiceState
GetSyncServiceState();
59 void GetExtensionStatusMap(std::map
<GURL
, std::string
>* status_map
);
60 void DumpFiles(const GURL
& origin
, const DumpFilesCallback
& callback
);
62 // Returns the file |url|'s sync status.
63 void GetFileSyncStatus(
64 const fileapi::FileSystemURL
& url
,
65 const SyncFileStatusCallback
& callback
);
67 void AddSyncEventObserver(SyncEventObserver
* observer
);
68 void RemoveSyncEventObserver(SyncEventObserver
* observer
);
70 ConflictResolutionPolicy
GetConflictResolutionPolicy() const;
71 SyncStatusCode
SetConflictResolutionPolicy(ConflictResolutionPolicy policy
);
74 friend class SyncFileSystemServiceFactory
;
75 friend class SyncFileSystemServiceTest
;
76 friend struct base::DefaultDeleter
<SyncFileSystemService
>;
78 explicit SyncFileSystemService(Profile
* profile
);
79 virtual ~SyncFileSystemService();
81 void Initialize(scoped_ptr
<LocalFileSyncService
> local_file_service
,
82 scoped_ptr
<RemoteFileSyncService
> remote_file_service
);
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
);
92 void DidInitializeFileSystemForDump(const GURL
& app_origin
,
93 const DumpFilesCallback
& callback
,
94 SyncStatusCode status
);
96 // Overrides sync_enabled_ setting. This should be called only by tests.
97 void SetSyncEnabledForTesting(bool enabled
);
99 // Called when following observer methods are called:
100 // - OnLocalChangeAvailable()
101 // - OnRemoteChangeAvailable()
102 // - OnRemoteServiceStateUpdated()
103 void MaybeStartSync();
105 // Called from MaybeStartSync(). (Should not be called from others)
106 void StartRemoteSync();
107 void StartLocalSync();
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
);
115 void DidGetLocalChangeStatus(const SyncFileStatusCallback
& callback
,
116 SyncStatusCode status
,
117 bool has_pending_local_changes
);
119 void OnSyncEnabledForRemoteSync();
121 // RemoteFileSyncService::Observer overrides.
122 virtual void OnLocalChangeAvailable(int64 pending_changes
) OVERRIDE
;
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
;
130 // content::NotificationObserver implementation.
131 virtual void Observe(int type
,
132 const content::NotificationSource
& source
,
133 const content::NotificationDetails
& details
) OVERRIDE
;
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
);
143 // ProfileSyncServiceObserver:
144 virtual void OnStateChanged() OVERRIDE
;
146 // SyncFileStatusObserver:
147 virtual void OnFileStatusChanged(
148 const fileapi::FileSystemURL
& url
,
149 SyncFileStatus sync_status
,
150 SyncAction action_taken
,
151 SyncDirection direction
) OVERRIDE
;
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
);
159 content::NotificationRegistrar registrar_
;
161 int64 pending_local_changes_
;
162 int64 pending_remote_changes_
;
164 scoped_ptr
<LocalFileSyncService
> local_file_service_
;
165 scoped_ptr
<RemoteFileSyncService
> remote_file_service_
;
167 bool local_sync_running_
;
168 bool remote_sync_running_
;
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_
;
176 // Indicates if sync is currently enabled or not.
179 base::OneShotTimer
<SyncFileSystemService
> sync_retry_timer_
;
181 ObserverList
<SyncEventObserver
> observers_
;
183 DISALLOW_COPY_AND_ASSIGN(SyncFileSystemService
);
186 } // namespace sync_file_system
188 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_SYNC_FILE_SYSTEM_SERVICE_H_