Add a function to create a bookmark app from a WebApplicationInfo.
[chromium-blink-merge.git] / components / sync_driver / model_association_manager.cc
blob0b89f81945ea8fd55b03376ecd5a01091c8a8135
1 // Copyright 2014 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.
5 #include "components/sync_driver/model_association_manager.h"
7 #include <algorithm>
8 #include <functional>
10 #include "base/debug/trace_event.h"
11 #include "base/logging.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/metrics/histogram.h"
14 #include "sync/internal_api/public/base/model_type.h"
16 using syncer::ModelTypeSet;
18 namespace browser_sync {
20 namespace {
22 static const syncer::ModelType kStartOrder[] = {
23 syncer::NIGORI, // Listed for completeness.
24 syncer::DEVICE_INFO, // Listed for completeness.
25 syncer::EXPERIMENTS, // Listed for completeness.
26 syncer::PROXY_TABS, // Listed for completeness.
28 // Kick off the association of the non-UI types first so they can associate
29 // in parallel with the UI types.
30 syncer::PASSWORDS,
31 syncer::AUTOFILL,
32 syncer::AUTOFILL_PROFILE,
33 syncer::EXTENSION_SETTINGS,
34 syncer::APP_SETTINGS,
35 syncer::TYPED_URLS,
36 syncer::HISTORY_DELETE_DIRECTIVES,
37 syncer::SYNCED_NOTIFICATIONS,
38 syncer::SYNCED_NOTIFICATION_APP_INFO,
40 // UI thread data types.
41 syncer::BOOKMARKS,
42 syncer::MANAGED_USERS, // Syncing managed users on initial login might
43 // block creating a new managed user, so we
44 // want to do it early.
45 syncer::PREFERENCES,
46 syncer::PRIORITY_PREFERENCES,
47 syncer::EXTENSIONS,
48 syncer::APPS,
49 syncer::APP_LIST,
50 syncer::THEMES,
51 syncer::SEARCH_ENGINES,
52 syncer::SESSIONS,
53 syncer::APP_NOTIFICATIONS,
54 syncer::DICTIONARY,
55 syncer::FAVICON_IMAGES,
56 syncer::FAVICON_TRACKING,
57 syncer::MANAGED_USER_SETTINGS,
58 syncer::MANAGED_USER_SHARED_SETTINGS,
59 syncer::ARTICLES,
62 COMPILE_ASSERT(arraysize(kStartOrder) ==
63 syncer::MODEL_TYPE_COUNT - syncer::FIRST_REAL_MODEL_TYPE,
64 kStartOrder_IncorrectSize);
66 // The amount of time we wait for association to finish. If some types haven't
67 // finished association by the time, configuration result will be
68 // PARTIAL_SUCCESS and DataTypeManager is notified of the unfinished types.
69 const int64 kAssociationTimeOutInSeconds = 600;
71 syncer::DataTypeAssociationStats BuildAssociationStatsFromMergeResults(
72 const syncer::SyncMergeResult& local_merge_result,
73 const syncer::SyncMergeResult& syncer_merge_result,
74 const base::TimeDelta& association_wait_time,
75 const base::TimeDelta& association_time) {
76 DCHECK_EQ(local_merge_result.model_type(), syncer_merge_result.model_type());
77 syncer::DataTypeAssociationStats stats;
78 stats.had_error = local_merge_result.error().IsSet() ||
79 syncer_merge_result.error().IsSet();
80 stats.num_local_items_before_association =
81 local_merge_result.num_items_before_association();
82 stats.num_sync_items_before_association =
83 syncer_merge_result.num_items_before_association();
84 stats.num_local_items_after_association =
85 local_merge_result.num_items_after_association();
86 stats.num_sync_items_after_association =
87 syncer_merge_result.num_items_after_association();
88 stats.num_local_items_added =
89 local_merge_result.num_items_added();
90 stats.num_local_items_deleted =
91 local_merge_result.num_items_deleted();
92 stats.num_local_items_modified =
93 local_merge_result.num_items_modified();
94 stats.local_version_pre_association =
95 local_merge_result.pre_association_version();
96 stats.num_sync_items_added =
97 syncer_merge_result.num_items_added();
98 stats.num_sync_items_deleted =
99 syncer_merge_result.num_items_deleted();
100 stats.num_sync_items_modified =
101 syncer_merge_result.num_items_modified();
102 stats.sync_version_pre_association =
103 syncer_merge_result.pre_association_version();
104 stats.association_wait_time = association_wait_time;
105 stats.association_time = association_time;
106 return stats;
109 } // namespace
111 ModelAssociationManager::ModelAssociationManager(
112 const DataTypeController::TypeMap* controllers,
113 ModelAssociationResultProcessor* processor)
114 : state_(IDLE),
115 controllers_(controllers),
116 result_processor_(processor),
117 weak_ptr_factory_(this),
118 configure_status_(DataTypeManager::UNKNOWN) {
119 // Ensure all data type controllers are stopped.
120 for (DataTypeController::TypeMap::const_iterator it = controllers_->begin();
121 it != controllers_->end(); ++it) {
122 DCHECK_EQ(DataTypeController::NOT_RUNNING, (*it).second->state());
126 ModelAssociationManager::~ModelAssociationManager() {
129 void ModelAssociationManager::Initialize(syncer::ModelTypeSet desired_types) {
130 // state_ can be INITIALIZED_TO_CONFIGURE if types are reconfigured when
131 // data is being downloaded, so StartAssociationAsync() is never called for
132 // the first configuration.
133 DCHECK_NE(CONFIGURING, state_);
135 // Only keep types that have controllers.
136 desired_types_.Clear();
137 slow_types_.Clear();
138 for (syncer::ModelTypeSet::Iterator it = desired_types.First();
139 it.Good(); it.Inc()) {
140 if (controllers_->find(it.Get()) != controllers_->end())
141 desired_types_.Put(it.Get());
144 DVLOG(1) << "ModelAssociationManager: Initializing for "
145 << syncer::ModelTypeSetToString(desired_types_);
147 state_ = INITIALIZED_TO_CONFIGURE;
149 StopDisabledTypes();
150 LoadEnabledTypes();
153 void ModelAssociationManager::StopDisabledTypes() {
154 DVLOG(1) << "ModelAssociationManager: Stopping disabled types.";
155 for (DataTypeController::TypeMap::const_iterator it = controllers_->begin();
156 it != controllers_->end(); ++it) {
157 DataTypeController* dtc = (*it).second.get();
158 if (dtc->state() != DataTypeController::NOT_RUNNING &&
159 (!desired_types_.Has(dtc->type()) ||
160 failed_data_types_info_.count(dtc->type()) > 0)) {
161 DVLOG(1) << "ModelTypeToString: stop " << dtc->name();
162 dtc->Stop();
164 loaded_types_.Remove(dtc->type());
165 associated_types_.Remove(dtc->type());
170 void ModelAssociationManager::LoadEnabledTypes() {
171 // Load in kStartOrder.
172 for (size_t i = 0; i < arraysize(kStartOrder); i++) {
173 syncer::ModelType type = kStartOrder[i];
174 if (!desired_types_.Has(type))
175 continue;
177 DCHECK(controllers_->find(type) != controllers_->end());
178 DataTypeController* dtc = controllers_->find(type)->second.get();
179 if (dtc->state() == DataTypeController::NOT_RUNNING) {
180 DCHECK(!loaded_types_.Has(dtc->type()));
181 DCHECK(!associated_types_.Has(dtc->type()));
182 dtc->LoadModels(base::Bind(&ModelAssociationManager::ModelLoadCallback,
183 weak_ptr_factory_.GetWeakPtr()));
188 void ModelAssociationManager::StartAssociationAsync(
189 const syncer::ModelTypeSet& types_to_associate) {
190 DCHECK_NE(CONFIGURING, state_);
191 state_ = CONFIGURING;
193 association_start_time_ = base::TimeTicks::Now();
195 requested_types_ = types_to_associate;
197 associating_types_ = types_to_associate;
198 associating_types_.RetainAll(desired_types_);
199 associating_types_.RemoveAll(associated_types_);
201 // Assume success.
202 configure_status_ = DataTypeManager::OK;
204 // Remove types that already failed.
205 for (std::map<syncer::ModelType, syncer::SyncError>::const_iterator it =
206 failed_data_types_info_.begin();
207 it != failed_data_types_info_.end(); ++it) {
208 associating_types_.Remove(it->first);
211 // Done if no types to associate.
212 if (associating_types_.Empty()) {
213 ModelAssociationDone();
214 return;
217 timer_.Start(FROM_HERE,
218 base::TimeDelta::FromSeconds(kAssociationTimeOutInSeconds),
219 this,
220 &ModelAssociationManager::ModelAssociationDone);
222 // Start association of types that are loaded in specified order.
223 for (size_t i = 0; i < arraysize(kStartOrder); i++) {
224 syncer::ModelType type = kStartOrder[i];
225 if (!associating_types_.Has(type) || !loaded_types_.Has(type))
226 continue;
228 DataTypeController* dtc = controllers_->find(type)->second.get();
229 DCHECK(DataTypeController::MODEL_LOADED == dtc->state() ||
230 DataTypeController::ASSOCIATING == dtc->state());
231 if (dtc->state() == DataTypeController::MODEL_LOADED) {
232 TRACE_EVENT_ASYNC_BEGIN1("sync", "ModelAssociation",
233 dtc,
234 "DataType",
235 ModelTypeToString(type));
237 dtc->StartAssociating(
238 base::Bind(&ModelAssociationManager::TypeStartCallback,
239 weak_ptr_factory_.GetWeakPtr(),
240 type, base::TimeTicks::Now()));
245 void ModelAssociationManager::ResetForNextAssociation() {
246 DVLOG(1) << "ModelAssociationManager: Reseting for next configuration";
247 // |loaded_types_| and |associated_types_| are not cleared. So
248 // reconfiguration won't restart types that are already started.
249 requested_types_.Clear();
250 failed_data_types_info_.clear();
251 associating_types_.Clear();
252 needs_crypto_types_.Clear();
255 void ModelAssociationManager::Stop() {
256 // Ignore callbacks from controllers.
257 weak_ptr_factory_.InvalidateWeakPtrs();
259 // Stop started data types.
260 for (DataTypeController::TypeMap::const_iterator it = controllers_->begin();
261 it != controllers_->end(); ++it) {
262 DataTypeController* dtc = (*it).second.get();
263 if (dtc->state() != DataTypeController::NOT_RUNNING) {
264 dtc->Stop();
265 DVLOG(1) << "ModelAssociationManager: Stopped " << dtc->name();
269 desired_types_.Clear();
270 loaded_types_.Clear();
271 associated_types_.Clear();
272 slow_types_.Clear();
274 if (state_ == CONFIGURING) {
275 if (configure_status_ == DataTypeManager::OK)
276 configure_status_ = DataTypeManager::ABORTED;
277 DVLOG(1) << "ModelAssociationManager: Calling OnModelAssociationDone";
278 ModelAssociationDone();
281 ResetForNextAssociation();
283 state_ = IDLE;
286 void ModelAssociationManager::AppendToFailedDatatypesAndLogError(
287 const syncer::SyncError& error) {
288 failed_data_types_info_[error.model_type()] = error;
289 LOG(ERROR) << "Failed to associate models for "
290 << syncer::ModelTypeToString(error.model_type());
291 UMA_HISTOGRAM_ENUMERATION("Sync.ConfigureFailed",
292 ModelTypeToHistogramInt(error.model_type()),
293 syncer::MODEL_TYPE_COUNT);
296 void ModelAssociationManager::ModelLoadCallback(syncer::ModelType type,
297 syncer::SyncError error) {
298 DVLOG(1) << "ModelAssociationManager: ModelLoadCallback for "
299 << syncer::ModelTypeToString(type);
301 // TODO(haitaol): temporary fix for 335606.
302 if (slow_types_.Has(type))
303 return;
305 // This happens when slow loading type is disabled by new configuration.
306 if (!desired_types_.Has(type))
307 return;
309 DCHECK(!loaded_types_.Has(type));
310 if (error.IsSet()) {
311 syncer::SyncMergeResult local_merge_result(type);
312 local_merge_result.set_error(error);
313 TypeStartCallback(type,
314 base::TimeTicks::Now(),
315 DataTypeController::ASSOCIATION_FAILED,
316 local_merge_result,
317 syncer::SyncMergeResult(type));
318 return;
321 loaded_types_.Put(type);
322 if (associating_types_.Has(type) || slow_types_.Has(type)) {
323 DataTypeController* dtc = controllers_->find(type)->second.get();
324 dtc->StartAssociating(
325 base::Bind(&ModelAssociationManager::TypeStartCallback,
326 weak_ptr_factory_.GetWeakPtr(),
327 type, base::TimeTicks::Now()));
331 void ModelAssociationManager::TypeStartCallback(
332 syncer::ModelType type,
333 base::TimeTicks type_start_time,
334 DataTypeController::StartResult start_result,
335 const syncer::SyncMergeResult& local_merge_result,
336 const syncer::SyncMergeResult& syncer_merge_result) {
337 // TODO(haitaol): temporary fix for 335606.
338 if (slow_types_.Has(type))
339 return;
341 // This happens when slow associating type is disabled by new configuration.
342 if (!desired_types_.Has(type))
343 return;
345 slow_types_.Remove(type);
347 DCHECK(!associated_types_.Has(type));
348 if (DataTypeController::IsSuccessfulResult(start_result)) {
349 associated_types_.Put(type);
350 } else if (state_ == IDLE) {
351 // For type that failed in IDLE mode, simply stop the controller. Next
352 // configuration will try to restart from scratch if the type is still
353 // enabled.
354 DataTypeController* dtc = controllers_->find(type)->second.get();
355 if (dtc->state() != DataTypeController::NOT_RUNNING)
356 dtc->Stop();
357 loaded_types_.Remove(type);
358 } else {
359 // Record error in CONFIGURING or INITIALIZED_TO_CONFIGURE mode. The error
360 // will be reported when data types association finishes.
361 if (start_result == DataTypeController::NEEDS_CRYPTO) {
362 DVLOG(1) << "ModelAssociationManager: Encountered an undecryptable type";
363 needs_crypto_types_.Put(type);
364 } else {
365 DVLOG(1) << "ModelAssociationManager: Encountered a failed type";
366 AppendToFailedDatatypesAndLogError(local_merge_result.error());
370 if (state_ != CONFIGURING)
371 return;
373 TRACE_EVENT_ASYNC_END1("sync", "ModelAssociation",
374 controllers_->find(type)->second.get(),
375 "DataType",
376 ModelTypeToString(type));
378 // Track the merge results if we succeeded or an association failure
379 // occurred.
380 if ((DataTypeController::IsSuccessfulResult(start_result) ||
381 start_result == DataTypeController::ASSOCIATION_FAILED) &&
382 syncer::ProtocolTypes().Has(type)) {
383 base::TimeDelta association_wait_time =
384 std::max(base::TimeDelta(), type_start_time - association_start_time_);
385 base::TimeDelta association_time =
386 base::TimeTicks::Now() - type_start_time;;
387 syncer::DataTypeAssociationStats stats =
388 BuildAssociationStatsFromMergeResults(local_merge_result,
389 syncer_merge_result,
390 association_wait_time,
391 association_time);
392 result_processor_->OnSingleDataTypeAssociationDone(type, stats);
395 // Update configuration result.
396 if (configure_status_ == DataTypeManager::OK &&
397 start_result == DataTypeController::ASSOCIATION_FAILED) {
398 configure_status_ = DataTypeManager::PARTIAL_SUCCESS;
400 if (start_result == DataTypeController::UNRECOVERABLE_ERROR)
401 configure_status_ = DataTypeManager::UNRECOVERABLE_ERROR;
403 associating_types_.Remove(type);
405 if (associating_types_.Empty())
406 ModelAssociationDone();
409 void ModelAssociationManager::ModelAssociationDone() {
410 CHECK_EQ(CONFIGURING, state_);
412 timer_.Stop();
414 slow_types_.PutAll(associating_types_);
416 // TODO(haitaol): temporary fix for 335606.
417 for (syncer::ModelTypeSet::Iterator it = associating_types_.First();
418 it.Good(); it.Inc()) {
419 AppendToFailedDatatypesAndLogError(
420 syncer::SyncError(FROM_HERE, syncer::SyncError::DATATYPE_ERROR,
421 "Association timed out.", it.Get()));
424 // Stop controllers of failed types.
425 StopDisabledTypes();
427 if (configure_status_ == DataTypeManager::OK &&
428 (!associating_types_.Empty() || !failed_data_types_info_.empty() ||
429 !needs_crypto_types_.Empty())) {
430 // We have not configured all types that we have been asked to configure.
431 // Either we have failed types or types that have not completed loading
432 // yet.
433 DVLOG(1) << "ModelAssociationManager: setting partial success";
434 configure_status_ = DataTypeManager::PARTIAL_SUCCESS;
437 DataTypeManager::ConfigureResult result(configure_status_,
438 requested_types_,
439 failed_data_types_info_,
440 associating_types_,
441 needs_crypto_types_);
443 // Reset state before notifying |result_processor_| because that might
444 // trigger a new round of configuration.
445 ResetForNextAssociation();
446 state_ = IDLE;
448 result_processor_->OnModelAssociationDone(result);
451 base::OneShotTimer<ModelAssociationManager>*
452 ModelAssociationManager::GetTimerForTesting() {
453 return &timer_;
456 } // namespace browser_sync