OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/sync_file_system/sync_file_system_service.h" | 5 #include "chrome/browser/sync_file_system/sync_file_system_service.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/format_macros.h" | 10 #include "base/format_macros.h" |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 class LocalSyncRunner : public SyncProcessRunner, | 140 class LocalSyncRunner : public SyncProcessRunner, |
141 public LocalFileSyncService::Observer { | 141 public LocalFileSyncService::Observer { |
142 public: | 142 public: |
143 LocalSyncRunner(const std::string& name, | 143 LocalSyncRunner(const std::string& name, |
144 SyncFileSystemService* sync_service) | 144 SyncFileSystemService* sync_service) |
145 : SyncProcessRunner(name, sync_service, | 145 : SyncProcessRunner(name, sync_service, |
146 scoped_ptr<SyncProcessRunner::TimerHelper>(), 1), | 146 scoped_ptr<SyncProcessRunner::TimerHelper>(), 1), |
147 factory_(this) {} | 147 factory_(this) {} |
148 | 148 |
149 virtual void StartSync(const SyncStatusCallback& callback) OVERRIDE { | 149 virtual void StartSync(const SyncStatusCallback& callback) OVERRIDE { |
150 sync_service()->local_service_->ProcessLocalChange( | 150 GetSyncService()->local_service_->ProcessLocalChange( |
151 base::Bind(&LocalSyncRunner::DidProcessLocalChange, | 151 base::Bind(&LocalSyncRunner::DidProcessLocalChange, |
152 factory_.GetWeakPtr(), callback)); | 152 factory_.GetWeakPtr(), callback)); |
153 } | 153 } |
154 | 154 |
155 // LocalFileSyncService::Observer overrides. | 155 // LocalFileSyncService::Observer overrides. |
156 virtual void OnLocalChangeAvailable(int64 pending_changes) OVERRIDE { | 156 virtual void OnLocalChangeAvailable(int64 pending_changes) OVERRIDE { |
157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
158 | 158 |
159 OnChangesUpdated(pending_changes); | 159 OnChangesUpdated(pending_changes); |
160 | 160 |
161 // Kick other sync runners just in case they're not running. | 161 // Kick other sync runners just in case they're not running. |
162 sync_service()->RunForEachSyncRunners( | 162 GetSyncService()->RunForEachSyncRunners( |
163 &SyncProcessRunner::ScheduleIfNotRunning); | 163 &SyncProcessRunner::ScheduleIfNotRunning); |
164 } | 164 } |
165 | 165 |
166 private: | 166 private: |
167 void DidProcessLocalChange( | 167 void DidProcessLocalChange( |
168 const SyncStatusCallback& callback, | 168 const SyncStatusCallback& callback, |
169 SyncStatusCode status, | 169 SyncStatusCode status, |
170 const FileSystemURL& url) { | 170 const FileSystemURL& url) { |
171 util::Log(logging::LOG_VERBOSE, FROM_HERE, | 171 util::Log(logging::LOG_VERBOSE, FROM_HERE, |
172 "ProcessLocalChange finished with status=%d (%s) for url=%s", | 172 "ProcessLocalChange finished with status=%d (%s) for url=%s", |
(...skipping 29 matching lines...) Expand all Loading... |
202 return RemoteStateToSyncServiceState(last_state_); | 202 return RemoteStateToSyncServiceState(last_state_); |
203 } | 203 } |
204 | 204 |
205 // RemoteFileSyncService::Observer overrides. | 205 // RemoteFileSyncService::Observer overrides. |
206 virtual void OnRemoteChangeQueueUpdated(int64 pending_changes) OVERRIDE { | 206 virtual void OnRemoteChangeQueueUpdated(int64 pending_changes) OVERRIDE { |
207 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 207 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
208 | 208 |
209 OnChangesUpdated(pending_changes); | 209 OnChangesUpdated(pending_changes); |
210 | 210 |
211 // Kick other sync runners just in case they're not running. | 211 // Kick other sync runners just in case they're not running. |
212 sync_service()->RunForEachSyncRunners( | 212 GetSyncService()->RunForEachSyncRunners( |
213 &SyncProcessRunner::ScheduleIfNotRunning); | 213 &SyncProcessRunner::ScheduleIfNotRunning); |
214 } | 214 } |
215 | 215 |
216 virtual void OnRemoteServiceStateUpdated( | 216 virtual void OnRemoteServiceStateUpdated( |
217 RemoteServiceState state, | 217 RemoteServiceState state, |
218 const std::string& description) OVERRIDE { | 218 const std::string& description) OVERRIDE { |
219 // Just forward to SyncFileSystemService. | 219 // Just forward to SyncFileSystemService. |
220 sync_service()->OnRemoteServiceStateUpdated(state, description); | 220 GetSyncService()->OnRemoteServiceStateUpdated(state, description); |
221 last_state_ = state; | 221 last_state_ = state; |
222 } | 222 } |
223 | 223 |
224 private: | 224 private: |
225 void DidProcessRemoteChange( | 225 void DidProcessRemoteChange( |
226 const SyncStatusCallback& callback, | 226 const SyncStatusCallback& callback, |
227 SyncStatusCode status, | 227 SyncStatusCode status, |
228 const FileSystemURL& url) { | 228 const FileSystemURL& url) { |
229 util::Log(logging::LOG_VERBOSE, FROM_HERE, | 229 util::Log(logging::LOG_VERBOSE, FROM_HERE, |
230 "ProcessRemoteChange finished with status=%d (%s) for url=%s", | 230 "ProcessRemoteChange finished with status=%d (%s) for url=%s", |
231 status, SyncStatusCodeToString(status), | 231 status, SyncStatusCodeToString(status), |
232 url.DebugString().c_str()); | 232 url.DebugString().c_str()); |
233 | 233 |
234 if (status == SYNC_STATUS_FILE_BUSY) { | 234 if (status == SYNC_STATUS_FILE_BUSY) { |
235 sync_service()->local_service_->RegisterURLForWaitingSync( | 235 GetSyncService()->local_service_->RegisterURLForWaitingSync( |
236 url, base::Bind(&RemoteSyncRunner::Schedule, | 236 url, base::Bind(&RemoteSyncRunner::Schedule, |
237 factory_.GetWeakPtr())); | 237 factory_.GetWeakPtr())); |
238 } | 238 } |
239 callback.Run(status); | 239 callback.Run(status); |
240 } | 240 } |
241 | 241 |
242 RemoteFileSyncService* remote_service_; | 242 RemoteFileSyncService* remote_service_; |
243 RemoteServiceState last_state_; | 243 RemoteServiceState last_state_; |
244 base::WeakPtrFactory<RemoteSyncRunner> factory_; | 244 base::WeakPtrFactory<RemoteSyncRunner> factory_; |
245 DISALLOW_COPY_AND_ASSIGN(RemoteSyncRunner); | 245 DISALLOW_COPY_AND_ASSIGN(RemoteSyncRunner); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 | 282 |
283 util::Log(logging::LOG_VERBOSE, FROM_HERE, | 283 util::Log(logging::LOG_VERBOSE, FROM_HERE, |
284 "Initializing for App: %s", app_origin.spec().c_str()); | 284 "Initializing for App: %s", app_origin.spec().c_str()); |
285 | 285 |
286 local_service_->MaybeInitializeFileSystemContext( | 286 local_service_->MaybeInitializeFileSystemContext( |
287 app_origin, file_system_context, | 287 app_origin, file_system_context, |
288 base::Bind(&SyncFileSystemService::DidInitializeFileSystem, | 288 base::Bind(&SyncFileSystemService::DidInitializeFileSystem, |
289 AsWeakPtr(), app_origin, callback)); | 289 AsWeakPtr(), app_origin, callback)); |
290 } | 290 } |
291 | 291 |
292 SyncServiceState SyncFileSystemService::GetSyncServiceState() { | |
293 // For now we always query the state from the main RemoteFileSyncService. | |
294 return RemoteStateToSyncServiceState(remote_service_->GetCurrentState()); | |
295 } | |
296 | |
297 void SyncFileSystemService::GetExtensionStatusMap( | 292 void SyncFileSystemService::GetExtensionStatusMap( |
298 const ExtensionStatusMapCallback& callback) { | 293 const ExtensionStatusMapCallback& callback) { |
299 remote_service_->GetOriginStatusMap( | 294 remote_service_->GetOriginStatusMap( |
300 base::Bind(&SyncFileSystemService::DidGetExtensionStatusMap, | 295 base::Bind(&SyncFileSystemService::DidGetExtensionStatusMap, |
301 AsWeakPtr(), callback)); | 296 AsWeakPtr(), callback)); |
302 } | 297 } |
303 | 298 |
304 void SyncFileSystemService::DumpFiles(const GURL& origin, | 299 void SyncFileSystemService::DumpFiles(const GURL& origin, |
305 const DumpFilesCallback& callback) { | 300 const DumpFilesCallback& callback) { |
306 DCHECK(!origin.is_empty()); | 301 DCHECK(!origin.is_empty()); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 void SyncFileSystemService::RemoveSyncEventObserver( | 344 void SyncFileSystemService::RemoveSyncEventObserver( |
350 SyncEventObserver* observer) { | 345 SyncEventObserver* observer) { |
351 observers_.RemoveObserver(observer); | 346 observers_.RemoveObserver(observer); |
352 } | 347 } |
353 | 348 |
354 LocalChangeProcessor* SyncFileSystemService::GetLocalChangeProcessor( | 349 LocalChangeProcessor* SyncFileSystemService::GetLocalChangeProcessor( |
355 const GURL& origin) { | 350 const GURL& origin) { |
356 return GetRemoteService(origin)->GetLocalChangeProcessor(); | 351 return GetRemoteService(origin)->GetLocalChangeProcessor(); |
357 } | 352 } |
358 | 353 |
| 354 void SyncFileSystemService::OnSyncIdle() { |
| 355 int64 remote_changes = 0; |
| 356 for (ScopedVector<SyncProcessRunner>::iterator iter = |
| 357 remote_sync_runners_.begin(); |
| 358 iter != remote_sync_runners_.end(); ++iter) |
| 359 remote_changes += (*iter)->pending_changes(); |
| 360 if (remote_changes == 0) |
| 361 local_service_->PromoteDemotedChanges(); |
| 362 |
| 363 int64 local_changes = 0; |
| 364 for (ScopedVector<SyncProcessRunner>::iterator iter = |
| 365 local_sync_runners_.begin(); |
| 366 iter != local_sync_runners_.end(); ++iter) |
| 367 local_changes += (*iter)->pending_changes(); |
| 368 if (local_changes == 0 && v2_remote_service_) |
| 369 v2_remote_service_->PromoteDemotedChanges(); |
| 370 } |
| 371 |
| 372 SyncServiceState SyncFileSystemService::GetSyncServiceState() { |
| 373 // For now we always query the state from the main RemoteFileSyncService. |
| 374 return RemoteStateToSyncServiceState(remote_service_->GetCurrentState()); |
| 375 } |
| 376 |
| 377 SyncFileSystemService* SyncFileSystemService::GetSyncService() { |
| 378 return this; |
| 379 } |
| 380 |
359 SyncFileSystemService::SyncFileSystemService(Profile* profile) | 381 SyncFileSystemService::SyncFileSystemService(Profile* profile) |
360 : profile_(profile), | 382 : profile_(profile), |
361 sync_enabled_(true) { | 383 sync_enabled_(true) { |
362 } | 384 } |
363 | 385 |
364 void SyncFileSystemService::Initialize( | 386 void SyncFileSystemService::Initialize( |
365 scoped_ptr<LocalFileSyncService> local_service, | 387 scoped_ptr<LocalFileSyncService> local_service, |
366 scoped_ptr<RemoteFileSyncService> remote_service) { | 388 scoped_ptr<RemoteFileSyncService> remote_service) { |
367 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 389 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
368 DCHECK(local_service); | 390 DCHECK(local_service); |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
572 void SyncFileSystemService::DidGetLocalChangeStatus( | 594 void SyncFileSystemService::DidGetLocalChangeStatus( |
573 const SyncFileStatusCallback& callback, | 595 const SyncFileStatusCallback& callback, |
574 SyncStatusCode status, | 596 SyncStatusCode status, |
575 bool has_pending_local_changes) { | 597 bool has_pending_local_changes) { |
576 callback.Run( | 598 callback.Run( |
577 status, | 599 status, |
578 has_pending_local_changes ? | 600 has_pending_local_changes ? |
579 SYNC_FILE_STATUS_HAS_PENDING_CHANGES : SYNC_FILE_STATUS_SYNCED); | 601 SYNC_FILE_STATUS_HAS_PENDING_CHANGES : SYNC_FILE_STATUS_SYNCED); |
580 } | 602 } |
581 | 603 |
582 void SyncFileSystemService::OnSyncIdle() { | |
583 int64 remote_changes = 0; | |
584 for (ScopedVector<SyncProcessRunner>::iterator iter = | |
585 remote_sync_runners_.begin(); | |
586 iter != remote_sync_runners_.end(); ++iter) | |
587 remote_changes += (*iter)->pending_changes(); | |
588 if (remote_changes == 0) | |
589 local_service_->PromoteDemotedChanges(); | |
590 | |
591 int64 local_changes = 0; | |
592 for (ScopedVector<SyncProcessRunner>::iterator iter = | |
593 local_sync_runners_.begin(); | |
594 iter != local_sync_runners_.end(); ++iter) | |
595 local_changes += (*iter)->pending_changes(); | |
596 if (local_changes == 0 && v2_remote_service_) | |
597 v2_remote_service_->PromoteDemotedChanges(); | |
598 } | |
599 | |
600 void SyncFileSystemService::OnRemoteServiceStateUpdated( | 604 void SyncFileSystemService::OnRemoteServiceStateUpdated( |
601 RemoteServiceState state, | 605 RemoteServiceState state, |
602 const std::string& description) { | 606 const std::string& description) { |
603 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 607 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
604 util::Log(logging::LOG_VERBOSE, FROM_HERE, | 608 util::Log(logging::LOG_VERBOSE, FROM_HERE, |
605 "OnRemoteServiceStateChanged: %d %s", state, description.c_str()); | 609 "OnRemoteServiceStateChanged: %d %s", state, description.c_str()); |
606 | 610 |
607 FOR_EACH_OBSERVER( | 611 FOR_EACH_OBSERVER( |
608 SyncEventObserver, observers_, | 612 SyncEventObserver, observers_, |
609 OnSyncStateUpdated(GURL(), | 613 OnSyncStateUpdated(GURL(), |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
744 v2_remote_service_->AddServiceObserver(v2_remote_syncer.get()); | 748 v2_remote_service_->AddServiceObserver(v2_remote_syncer.get()); |
745 v2_remote_service_->AddFileStatusObserver(this); | 749 v2_remote_service_->AddFileStatusObserver(this); |
746 v2_remote_service_->SetRemoteChangeProcessor(local_service_.get()); | 750 v2_remote_service_->SetRemoteChangeProcessor(local_service_.get()); |
747 v2_remote_service_->SetSyncEnabled(sync_enabled_); | 751 v2_remote_service_->SetSyncEnabled(sync_enabled_); |
748 remote_sync_runners_.push_back(v2_remote_syncer.release()); | 752 remote_sync_runners_.push_back(v2_remote_syncer.release()); |
749 } | 753 } |
750 return v2_remote_service_.get(); | 754 return v2_remote_service_.get(); |
751 } | 755 } |
752 | 756 |
753 } // namespace sync_file_system | 757 } // namespace sync_file_system |
OLD | NEW |