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