OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "components/sync/driver/glue/sync_backend_host_core.h" | 5 #include "components/sync/driver/glue/sync_backend_host_core.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 #include "components/sync/engine/cycle/commit_counters.h" | 21 #include "components/sync/engine/cycle/commit_counters.h" |
22 #include "components/sync/engine/cycle/status_counters.h" | 22 #include "components/sync/engine/cycle/status_counters.h" |
23 #include "components/sync/engine/cycle/sync_cycle_snapshot.h" | 23 #include "components/sync/engine/cycle/sync_cycle_snapshot.h" |
24 #include "components/sync/engine/cycle/update_counters.h" | 24 #include "components/sync/engine/cycle/update_counters.h" |
25 #include "components/sync/engine/engine_components_factory.h" | 25 #include "components/sync/engine/engine_components_factory.h" |
26 #include "components/sync/engine/events/protocol_event.h" | 26 #include "components/sync/engine/events/protocol_event.h" |
27 #include "components/sync/engine/net/http_post_provider_factory.h" | 27 #include "components/sync/engine/net/http_post_provider_factory.h" |
28 #include "components/sync/engine/sync_backend_registrar.h" | 28 #include "components/sync/engine/sync_backend_registrar.h" |
29 #include "components/sync/engine/sync_manager.h" | 29 #include "components/sync/engine/sync_manager.h" |
30 #include "components/sync/engine/sync_manager_factory.h" | 30 #include "components/sync/engine/sync_manager_factory.h" |
| 31 #include "components/sync/syncable/directory.h" |
31 | 32 |
32 // Helper macros to log with the syncer thread name; useful when there | 33 // Helper macros to log with the syncer thread name; useful when there |
33 // are multiple syncers involved. | 34 // are multiple syncers involved. |
34 | 35 |
35 #define SLOG(severity) LOG(severity) << name_ << ": " | 36 #define SLOG(severity) LOG(severity) << name_ << ": " |
36 | 37 |
37 #define SDVLOG(verbose_level) DVLOG(verbose_level) << name_ << ": " | 38 #define SDVLOG(verbose_level) DVLOG(verbose_level) << name_ << ": " |
38 | 39 |
39 static const int kSaveChangesIntervalSeconds = 10; | 40 static const int kSaveChangesIntervalSeconds = 10; |
40 | 41 |
41 namespace net { | 42 namespace net { |
42 class URLFetcher; | 43 class URLFetcher; |
43 } | 44 } |
44 | 45 |
45 namespace { | 46 namespace { |
46 | 47 |
47 void BindFetcherToDataTracker(net::URLFetcher* fetcher) { | 48 void BindFetcherToDataTracker(net::URLFetcher* fetcher) { |
48 data_use_measurement::DataUseUserData::AttachToFetcher( | 49 data_use_measurement::DataUseUserData::AttachToFetcher( |
49 fetcher, data_use_measurement::DataUseUserData::SYNC); | 50 fetcher, data_use_measurement::DataUseUserData::SYNC); |
50 } | 51 } |
51 | 52 |
52 } // namespace | 53 } // namespace |
53 | 54 |
54 namespace syncer { | 55 namespace syncer { |
55 | 56 |
56 class EngineComponentsFactory; | 57 class EngineComponentsFactory; |
57 | 58 |
58 SyncBackendHostCore::SyncBackendHostCore( | 59 SyncBackendHostCore::SyncBackendHostCore( |
59 const std::string& name, | 60 const std::string& name, |
60 const base::FilePath& sync_data_folder_path, | 61 const base::FilePath& sync_data_folder, |
61 const base::WeakPtr<SyncBackendHostImpl>& backend) | 62 const base::WeakPtr<SyncBackendHostImpl>& backend) |
62 : name_(name), | 63 : name_(name), |
63 sync_data_folder_path_(sync_data_folder_path), | 64 sync_data_folder_(sync_data_folder), |
64 host_(backend), | 65 host_(backend), |
65 weak_ptr_factory_(this) { | 66 weak_ptr_factory_(this) { |
66 DCHECK(backend.get()); | 67 DCHECK(backend.get()); |
67 // This is constructed on the UI thread but used from the sync thread. | 68 // This is constructed on the UI thread but used from the sync thread. |
68 thread_checker_.DetachFromThread(); | 69 thread_checker_.DetachFromThread(); |
69 } | 70 } |
70 | 71 |
71 SyncBackendHostCore::~SyncBackendHostCore() { | 72 SyncBackendHostCore::~SyncBackendHostCore() { |
72 DCHECK(!sync_manager_.get()); | 73 DCHECK(!sync_manager_.get()); |
73 } | 74 } |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 host_.Call(FROM_HERE, &SyncBackendHostImpl::UpdateInvalidationVersions, | 319 host_.Call(FROM_HERE, &SyncBackendHostImpl::UpdateInvalidationVersions, |
319 last_invalidation_versions_); | 320 last_invalidation_versions_); |
320 } | 321 } |
321 | 322 |
322 void SyncBackendHostCore::DoInitialize(SyncEngine::InitParams params) { | 323 void SyncBackendHostCore::DoInitialize(SyncEngine::InitParams params) { |
323 DCHECK(thread_checker_.CalledOnValidThread()); | 324 DCHECK(thread_checker_.CalledOnValidThread()); |
324 | 325 |
325 // Blow away the partial or corrupt sync data folder before doing any more | 326 // Blow away the partial or corrupt sync data folder before doing any more |
326 // initialization, if necessary. | 327 // initialization, if necessary. |
327 if (params.delete_sync_data_folder) { | 328 if (params.delete_sync_data_folder) { |
328 DeleteSyncDataFolder(); | 329 syncable::Directory::DeleteDirectoryFiles(sync_data_folder_); |
329 } | 330 } |
330 | 331 |
331 // Make sure that the directory exists before initializing the backend. | 332 // Make sure that the directory exists before initializing the backend. |
332 // If it already exists, this will do no harm. | 333 // If it already exists, this will do no harm. |
333 if (!base::CreateDirectory(sync_data_folder_path_)) { | 334 if (!base::CreateDirectory(sync_data_folder_)) { |
334 DLOG(FATAL) << "Sync Data directory creation failed."; | 335 DLOG(FATAL) << "Sync Data directory creation failed."; |
335 } | 336 } |
336 | 337 |
337 // Load the previously persisted set of invalidation versions into memory. | 338 // Load the previously persisted set of invalidation versions into memory. |
338 last_invalidation_versions_ = params.invalidation_versions; | 339 last_invalidation_versions_ = params.invalidation_versions; |
339 | 340 |
340 DCHECK(!registrar_); | 341 DCHECK(!registrar_); |
341 registrar_ = std::move(params.registrar); | 342 registrar_ = std::move(params.registrar); |
342 DCHECK(registrar_); | 343 DCHECK(registrar_); |
343 | 344 |
344 sync_manager_ = params.sync_manager_factory->CreateSyncManager(name_); | 345 sync_manager_ = params.sync_manager_factory->CreateSyncManager(name_); |
345 sync_manager_->AddObserver(this); | 346 sync_manager_->AddObserver(this); |
346 | 347 |
347 SyncManager::InitArgs args; | 348 SyncManager::InitArgs args; |
348 args.database_location = sync_data_folder_path_; | 349 args.database_location = sync_data_folder_; |
349 args.event_handler = params.event_handler; | 350 args.event_handler = params.event_handler; |
350 args.service_url = params.service_url; | 351 args.service_url = params.service_url; |
351 args.enable_local_sync_backend = params.enable_local_sync_backend; | 352 args.enable_local_sync_backend = params.enable_local_sync_backend; |
352 args.local_sync_backend_folder = params.local_sync_backend_folder; | 353 args.local_sync_backend_folder = params.local_sync_backend_folder; |
353 args.post_factory = | 354 args.post_factory = |
354 params.http_factory_getter.Run(&release_request_context_signal_); | 355 params.http_factory_getter.Run(&release_request_context_signal_); |
355 // Finish initializing the HttpBridgeFactory. We do this here because | 356 // Finish initializing the HttpBridgeFactory. We do this here because |
356 // building the user agent may block on some platforms. | 357 // building the user agent may block on some platforms. |
357 args.post_factory->Init(params.sync_user_agent, | 358 args.post_factory->Init(params.sync_user_agent, |
358 base::Bind(&BindFetcherToDataTracker)); | 359 base::Bind(&BindFetcherToDataTracker)); |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
473 } | 474 } |
474 | 475 |
475 void SyncBackendHostCore::DoShutdown(ShutdownReason reason) { | 476 void SyncBackendHostCore::DoShutdown(ShutdownReason reason) { |
476 DCHECK(thread_checker_.CalledOnValidThread()); | 477 DCHECK(thread_checker_.CalledOnValidThread()); |
477 | 478 |
478 DoDestroySyncManager(reason); | 479 DoDestroySyncManager(reason); |
479 | 480 |
480 registrar_ = nullptr; | 481 registrar_ = nullptr; |
481 | 482 |
482 if (reason == DISABLE_SYNC) | 483 if (reason == DISABLE_SYNC) |
483 DeleteSyncDataFolder(); | 484 syncable::Directory::DeleteDirectoryFiles(sync_data_folder_); |
484 | 485 |
485 host_.Reset(); | 486 host_.Reset(); |
486 weak_ptr_factory_.InvalidateWeakPtrs(); | 487 weak_ptr_factory_.InvalidateWeakPtrs(); |
487 } | 488 } |
488 | 489 |
489 void SyncBackendHostCore::DoDestroySyncManager(ShutdownReason reason) { | 490 void SyncBackendHostCore::DoDestroySyncManager(ShutdownReason reason) { |
490 DCHECK(thread_checker_.CalledOnValidThread()); | 491 DCHECK(thread_checker_.CalledOnValidThread()); |
491 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider( | 492 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider( |
492 this); | 493 this); |
493 if (sync_manager_) { | 494 if (sync_manager_) { |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
594 | 595 |
595 if (!forward_type_info_) | 596 if (!forward_type_info_) |
596 return; | 597 return; |
597 | 598 |
598 forward_type_info_ = false; | 599 forward_type_info_ = false; |
599 | 600 |
600 if (sync_manager_->HasDirectoryTypeDebugInfoObserver(this)) | 601 if (sync_manager_->HasDirectoryTypeDebugInfoObserver(this)) |
601 sync_manager_->UnregisterDirectoryTypeDebugInfoObserver(this); | 602 sync_manager_->UnregisterDirectoryTypeDebugInfoObserver(this); |
602 } | 603 } |
603 | 604 |
604 void SyncBackendHostCore::DeleteSyncDataFolder() { | |
605 DCHECK(thread_checker_.CalledOnValidThread()); | |
606 if (base::DirectoryExists(sync_data_folder_path_)) { | |
607 if (!base::DeleteFile(sync_data_folder_path_, true)) | |
608 SLOG(DFATAL) << "Could not delete the Sync Data folder."; | |
609 } | |
610 } | |
611 | |
612 void SyncBackendHostCore::StartSavingChanges() { | 605 void SyncBackendHostCore::StartSavingChanges() { |
613 DCHECK(thread_checker_.CalledOnValidThread()); | 606 DCHECK(thread_checker_.CalledOnValidThread()); |
614 DCHECK(!save_changes_timer_.get()); | 607 DCHECK(!save_changes_timer_.get()); |
615 save_changes_timer_ = base::MakeUnique<base::RepeatingTimer>(); | 608 save_changes_timer_ = base::MakeUnique<base::RepeatingTimer>(); |
616 save_changes_timer_->Start( | 609 save_changes_timer_->Start( |
617 FROM_HERE, base::TimeDelta::FromSeconds(kSaveChangesIntervalSeconds), | 610 FROM_HERE, base::TimeDelta::FromSeconds(kSaveChangesIntervalSeconds), |
618 this, &SyncBackendHostCore::SaveChanges); | 611 this, &SyncBackendHostCore::SaveChanges); |
619 } | 612 } |
620 | 613 |
621 void SyncBackendHostCore::SaveChanges() { | 614 void SyncBackendHostCore::SaveChanges() { |
(...skipping 17 matching lines...) Expand all Loading... |
639 } | 632 } |
640 | 633 |
641 void SyncBackendHostCore::ClearServerDataDone( | 634 void SyncBackendHostCore::ClearServerDataDone( |
642 const base::Closure& frontend_callback) { | 635 const base::Closure& frontend_callback) { |
643 DCHECK(thread_checker_.CalledOnValidThread()); | 636 DCHECK(thread_checker_.CalledOnValidThread()); |
644 host_.Call(FROM_HERE, &SyncBackendHostImpl::ClearServerDataDoneOnFrontendLoop, | 637 host_.Call(FROM_HERE, &SyncBackendHostImpl::ClearServerDataDoneOnFrontendLoop, |
645 frontend_callback); | 638 frontend_callback); |
646 } | 639 } |
647 | 640 |
648 } // namespace syncer | 641 } // namespace syncer |
OLD | NEW |