Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(634)

Unified Diff: components/sync/driver/non_ui_data_type_controller.cc

Issue 2257523003: [Sync] Move StartAssociation from NonUIDataTypeController to SharedChangeProcessor. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Move local_service_ WeakPtr to SharedChangeProcessor. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/sync/driver/non_ui_data_type_controller.cc
diff --git a/components/sync/driver/non_ui_data_type_controller.cc b/components/sync/driver/non_ui_data_type_controller.cc
index a3e4f6f5cc0a088b40007bd0687eb5901ecfe389..c20e9f5965d87572b4c026d3bb6753dc972b5246 100644
--- a/components/sync/driver/non_ui_data_type_controller.cc
+++ b/components/sync/driver/non_ui_data_type_controller.cc
@@ -10,10 +10,10 @@
#include "components/sync/api/sync_error.h"
#include "components/sync/api/sync_merge_result.h"
#include "components/sync/api/syncable_service.h"
+#include "components/sync/base/bind_to_task_runner.h"
#include "components/sync/base/data_type_histogram.h"
#include "components/sync/base/model_type.h"
#include "components/sync/driver/generic_change_processor_factory.h"
-#include "components/sync/driver/shared_change_processor_ref.h"
#include "components/sync/driver/sync_api_component_factory.h"
#include "components/sync/driver/sync_client.h"
#include "components/sync/driver/sync_service.h"
@@ -21,7 +21,7 @@
namespace sync_driver {
SharedChangeProcessor* NonUIDataTypeController::CreateSharedChangeProcessor() {
- return new SharedChangeProcessor();
+ return new SharedChangeProcessor(type());
}
NonUIDataTypeController::NonUIDataTypeController(
@@ -95,9 +95,9 @@ void NonUIDataTypeController::StartAssociating(
"Failed to post StartAssociation", type());
syncer::SyncMergeResult local_merge_result(type());
local_merge_result.set_error(error);
- StartDoneImpl(ASSOCIATION_FAILED, NOT_RUNNING, local_merge_result,
- syncer::SyncMergeResult(type()));
- // StartDoneImpl should have called ClearSharedChangeProcessor();
+ StartDone(ASSOCIATION_FAILED, local_merge_result,
+ syncer::SyncMergeResult(type()));
+ // StartDone should have cleared the SharedChangeProcessor.
DCHECK(!shared_change_processor_.get());
return;
}
@@ -112,7 +112,7 @@ void NonUIDataTypeController::Stop() {
// Disconnect the change processor. At this point, the
// syncer::SyncableService can no longer interact with the Syncer, even if
// it hasn't finished MergeDataAndStartSyncing.
- ClearSharedChangeProcessor();
+ DisconnectSharedChangeProcessor();
// If we haven't finished starting, we need to abort the start.
switch (state()) {
@@ -129,7 +129,7 @@ void NonUIDataTypeController::Stop() {
case DISABLED:
// If DTC is loaded or disabled, we never attempted or succeeded
// associating and never activated the datatype. We would have already
- // stopped the local service in StartDoneImpl(..).
+ // stopped the local service in StartDone(..).
state_ = NOT_RUNNING;
StopModels();
return;
@@ -144,7 +144,7 @@ void NonUIDataTypeController::Stop() {
// Stop the local service and release our references to it and the
// shared change processor (posts a task to the datatype's thread).
- StopLocalServiceAsync();
+ StopServiceAndClearProcessor();
state_ = NOT_RUNNING;
}
@@ -180,7 +180,7 @@ void NonUIDataTypeController::StartDone(
DataTypeController::ConfigureResult start_result,
const syncer::SyncMergeResult& local_merge_result,
const syncer::SyncMergeResult& syncer_merge_result) {
- DCHECK(!ui_thread_->BelongsToCurrentThread());
+ DCHECK(ui_thread_->BelongsToCurrentThread());
DataTypeController::State new_state;
if (IsSuccessfulResult(start_result)) {
@@ -189,27 +189,14 @@ void NonUIDataTypeController::StartDone(
new_state = (start_result == ASSOCIATION_FAILED ? DISABLED : NOT_RUNNING);
}
- ui_thread_->PostTask(
- FROM_HERE,
- base::Bind(&NonUIDataTypeController::StartDoneImpl, this, start_result,
- new_state, local_merge_result, syncer_merge_result));
-}
-
-void NonUIDataTypeController::StartDoneImpl(
- DataTypeController::ConfigureResult start_result,
- DataTypeController::State new_state,
- const syncer::SyncMergeResult& local_merge_result,
- const syncer::SyncMergeResult& syncer_merge_result) {
- DCHECK(ui_thread_->BelongsToCurrentThread());
-
// If we failed to start up, and we haven't been stopped yet, we need to
// ensure we clean up the local service and shared change processor properly.
if (new_state != RUNNING && state() != NOT_RUNNING && state() != STOPPING) {
- ClearSharedChangeProcessor();
- StopLocalServiceAsync();
+ DisconnectSharedChangeProcessor();
+ StopServiceAndClearProcessor();
}
- // It's possible to have StartDoneImpl called first from the UI thread
+ // It's possible to have StartDone called first from the UI thread
// (due to Stop being called) and then posted from the non-UI thread. In
// this case, we drop the second call because we've already been stopped.
if (state_ == NOT_RUNNING) {
@@ -226,14 +213,6 @@ void NonUIDataTypeController::StartDoneImpl(
start_callback_.Run(start_result, local_merge_result, syncer_merge_result);
}
-void NonUIDataTypeController::RecordAssociationTime(base::TimeDelta time) {
- DCHECK(!ui_thread_->BelongsToCurrentThread());
-#define PER_DATA_TYPE_MACRO(type_str) \
- UMA_HISTOGRAM_TIMES("Sync." type_str "AssociationTime", time);
- SYNC_DATA_TYPE_HISTOGRAM(type());
-#undef PER_DATA_TYPE_MACRO
-}
-
void NonUIDataTypeController::RecordStartFailure(ConfigureResult result) {
DCHECK(ui_thread_->BelongsToCurrentThread());
UMA_HISTOGRAM_ENUMERATION("Sync.DataTypeStartFailures",
@@ -266,9 +245,12 @@ bool NonUIDataTypeController::StartAssociationAsync() {
DCHECK_EQ(state(), ASSOCIATING);
return PostTaskOnBackendThread(
FROM_HERE,
- base::Bind(
- &NonUIDataTypeController::StartAssociationWithSharedChangeProcessor,
- this, shared_change_processor_));
+ base::Bind(&SharedChangeProcessor::StartAssociation,
+ shared_change_processor_,
+ syncer::BindToTaskRunner(
+ ui_thread_,
+ base::Bind(&NonUIDataTypeController::StartDone, this)),
+ sync_client_, user_share_, base::Unretained(this)));
}
ChangeProcessor* NonUIDataTypeController::GetChangeProcessor() const {
@@ -276,120 +258,23 @@ ChangeProcessor* NonUIDataTypeController::GetChangeProcessor() const {
return shared_change_processor_->generic_change_processor();
}
-// This method can execute after we've already stopped (and possibly even
-// destroyed) both the Syncer and the SyncableService. As a result, all actions
-// must either have no side effects outside of the DTC or must be protected
-// by |shared_change_processor|, which is guaranteed to have been Disconnected
-// if the syncer shut down.
-void NonUIDataTypeController::StartAssociationWithSharedChangeProcessor(
- const scoped_refptr<SharedChangeProcessor>& shared_change_processor) {
- DCHECK(!ui_thread_->BelongsToCurrentThread());
- DCHECK(shared_change_processor.get());
- DCHECK(user_share_);
- syncer::SyncMergeResult local_merge_result(type());
- syncer::SyncMergeResult syncer_merge_result(type());
- base::WeakPtrFactory<syncer::SyncMergeResult> weak_ptr_factory(
- &syncer_merge_result);
-
- // Connect |shared_change_processor| to the syncer and get the
- // syncer::SyncableService associated with type().
- // Note that it's possible the shared_change_processor has already been
- // disconnected at this point, so all our accesses to the syncer from this
- // point on are through it.
- GenericChangeProcessorFactory factory;
- local_service_ = shared_change_processor->Connect(
- sync_client_, &factory, user_share_, this, type(),
- weak_ptr_factory.GetWeakPtr());
- if (!local_service_.get()) {
- syncer::SyncError error(FROM_HERE, syncer::SyncError::DATATYPE_ERROR,
- "Failed to connect to syncer.", type());
- local_merge_result.set_error(error);
- StartDone(ASSOCIATION_FAILED, local_merge_result, syncer_merge_result);
- return;
- }
-
- if (!shared_change_processor->CryptoReadyIfNecessary()) {
- syncer::SyncError error(FROM_HERE, syncer::SyncError::CRYPTO_ERROR, "",
- type());
- local_merge_result.set_error(error);
- StartDone(NEEDS_CRYPTO, local_merge_result, syncer_merge_result);
- return;
- }
-
- bool sync_has_nodes = false;
- if (!shared_change_processor->SyncModelHasUserCreatedNodes(&sync_has_nodes)) {
- syncer::SyncError error(FROM_HERE, syncer::SyncError::UNRECOVERABLE_ERROR,
- "Failed to load sync nodes", type());
- local_merge_result.set_error(error);
- StartDone(UNRECOVERABLE_ERROR, local_merge_result, syncer_merge_result);
- return;
- }
-
- // Scope for |initial_sync_data| which might be expensive, so we don't want
- // to keep it in memory longer than necessary.
- {
- syncer::SyncDataList initial_sync_data;
-
- base::TimeTicks start_time = base::TimeTicks::Now();
- syncer::SyncError error =
- shared_change_processor->GetAllSyncDataReturnError(type(),
- &initial_sync_data);
- if (error.IsSet()) {
- local_merge_result.set_error(error);
- StartDone(ASSOCIATION_FAILED, local_merge_result, syncer_merge_result);
- return;
- }
-
- std::string datatype_context;
- if (shared_change_processor->GetDataTypeContext(&datatype_context)) {
- local_service_->UpdateDataTypeContext(
- type(), syncer::SyncChangeProcessor::NO_REFRESH, datatype_context);
- }
-
- syncer_merge_result.set_num_items_before_association(
- initial_sync_data.size());
- // Passes a reference to |shared_change_processor|.
- local_merge_result = local_service_->MergeDataAndStartSyncing(
- type(), initial_sync_data,
- std::unique_ptr<syncer::SyncChangeProcessor>(
- new SharedChangeProcessorRef(shared_change_processor)),
- std::unique_ptr<syncer::SyncErrorFactory>(
- new SharedChangeProcessorRef(shared_change_processor)));
- RecordAssociationTime(base::TimeTicks::Now() - start_time);
- if (local_merge_result.error().IsSet()) {
- StartDone(ASSOCIATION_FAILED, local_merge_result, syncer_merge_result);
- return;
- }
- }
-
- syncer_merge_result.set_num_items_after_association(
- shared_change_processor->GetSyncCount());
-
- StartDone(!sync_has_nodes ? OK_FIRST_RUN : OK, local_merge_result,
- syncer_merge_result);
-}
-
-void NonUIDataTypeController::ClearSharedChangeProcessor() {
+void NonUIDataTypeController::DisconnectSharedChangeProcessor() {
DCHECK(ui_thread_->BelongsToCurrentThread());
// |shared_change_processor_| can already be NULL if Stop() is
- // called after StartDoneImpl(_, DISABLED, _).
+ // called after StartDone(_, DISABLED, _).
if (shared_change_processor_.get()) {
shared_change_processor_->Disconnect();
- shared_change_processor_ = NULL;
}
}
-void NonUIDataTypeController::StopLocalServiceAsync() {
+void NonUIDataTypeController::StopServiceAndClearProcessor() {
DCHECK(ui_thread_->BelongsToCurrentThread());
- PostTaskOnBackendThread(
- FROM_HERE, base::Bind(&NonUIDataTypeController::StopLocalService, this));
-}
-
-void NonUIDataTypeController::StopLocalService() {
- DCHECK(!ui_thread_->BelongsToCurrentThread());
- if (local_service_.get())
- local_service_->StopSyncing(type());
- local_service_.reset();
+ if (shared_change_processor_.get()) {
+ PostTaskOnBackendThread(FROM_HERE,
+ base::Bind(&SharedChangeProcessor::StopLocalService,
+ shared_change_processor_));
+ shared_change_processor_ = nullptr;
+ }
}
} // namespace sync_driver
« no previous file with comments | « components/sync/driver/non_ui_data_type_controller.h ('k') | components/sync/driver/non_ui_data_type_controller_mock.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698