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