OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/glue/autofill_profile_data_type_controller.h" | 5 #include "chrome/browser/sync/glue/autofill_profile_data_type_controller.h" |
6 | 6 |
7 #include "base/metrics/histogram.h" | 7 #include "base/metrics/histogram.h" |
| 8 #include "base/task.h" |
| 9 #include "chrome/browser/autofill/personal_data_manager.h" |
| 10 #include "chrome/browser/autofill/personal_data_manager_factory.h" |
| 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/sync/api/sync_error.h" |
| 13 #include "chrome/browser/sync/api/syncable_service.h" |
8 #include "chrome/browser/sync/profile_sync_factory.h" | 14 #include "chrome/browser/sync/profile_sync_factory.h" |
| 15 #include "chrome/browser/sync/profile_sync_service.h" |
9 #include "chrome/browser/webdata/web_data_service.h" | 16 #include "chrome/browser/webdata/web_data_service.h" |
| 17 #include "chrome/common/chrome_notification_types.h" |
| 18 #include "content/browser/browser_thread.h" |
| 19 #include "content/common/notification_service.h" |
| 20 #include "content/common/notification_source.h" |
10 | 21 |
11 namespace browser_sync { | 22 namespace browser_sync { |
12 | 23 |
13 AutofillProfileDataTypeController::AutofillProfileDataTypeController( | 24 AutofillProfileDataTypeController::AutofillProfileDataTypeController( |
14 ProfileSyncFactory* profile_sync_factory, | 25 ProfileSyncFactory* profile_sync_factory, |
15 Profile* profile) | 26 Profile* profile) |
16 : AutofillDataTypeController(profile_sync_factory, profile) {} | 27 : NewNonFrontendDataTypeController(profile_sync_factory, profile), |
| 28 personal_data_(NULL) { |
| 29 } |
17 | 30 |
18 AutofillProfileDataTypeController::~AutofillProfileDataTypeController() {} | 31 AutofillProfileDataTypeController::~AutofillProfileDataTypeController() {} |
19 | 32 |
| 33 bool AutofillProfileDataTypeController::StartModels() { |
| 34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 35 DCHECK_EQ(state(), MODEL_STARTING); |
| 36 // Waiting for the personal data is subtle: we do this as the PDM resets |
| 37 // its cache of unique IDs once it gets loaded. If we were to proceed with |
| 38 // association, the local ids in the mappings would wind up colliding. |
| 39 personal_data_ = PersonalDataManagerFactory::GetForProfile(profile()); |
| 40 if (!personal_data_->IsDataLoaded()) { |
| 41 personal_data_->SetObserver(this); |
| 42 return false; |
| 43 } |
| 44 |
| 45 web_data_service_ = profile()->GetWebDataService(Profile::IMPLICIT_ACCESS); |
| 46 if (web_data_service_.get() && web_data_service_->IsDatabaseLoaded()) { |
| 47 return true; |
| 48 } else { |
| 49 notification_registrar_.Add(this, chrome::NOTIFICATION_WEB_DATABASE_LOADED, |
| 50 NotificationService::AllSources()); |
| 51 return false; |
| 52 } |
| 53 } |
| 54 |
| 55 void AutofillProfileDataTypeController::OnPersonalDataChanged() { |
| 56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 57 DCHECK_EQ(state(), MODEL_STARTING); |
| 58 personal_data_->RemoveObserver(this); |
| 59 web_data_service_ = profile()->GetWebDataService(Profile::IMPLICIT_ACCESS); |
| 60 if (web_data_service_.get() && web_data_service_->IsDatabaseLoaded()) { |
| 61 DoStartAssociationAsync(); |
| 62 } else { |
| 63 notification_registrar_.Add(this, chrome::NOTIFICATION_WEB_DATABASE_LOADED, |
| 64 NotificationService::AllSources()); |
| 65 } |
| 66 } |
| 67 |
| 68 void AutofillProfileDataTypeController::Observe( |
| 69 int notification_type, |
| 70 const NotificationSource& source, |
| 71 const NotificationDetails& details) { |
| 72 notification_registrar_.RemoveAll(); |
| 73 DoStartAssociationAsync(); |
| 74 } |
| 75 |
| 76 void AutofillProfileDataTypeController::DoStartAssociationAsync() { |
| 77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 78 DCHECK_EQ(state(), MODEL_STARTING); |
| 79 set_state(ASSOCIATING); |
| 80 if (!StartAssociationAsync()) { |
| 81 SyncError error(FROM_HERE, |
| 82 "Failed to post association task.", |
| 83 type()); |
| 84 StartDoneImpl(ASSOCIATION_FAILED, NOT_RUNNING, error); |
| 85 } |
| 86 } |
| 87 |
| 88 bool AutofillProfileDataTypeController::StartAssociationAsync() { |
| 89 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 90 DCHECK_EQ(state(), ASSOCIATING); |
| 91 return BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, |
| 92 base::Bind(&AutofillProfileDataTypeController::StartAssociation, |
| 93 this)); |
| 94 } |
| 95 |
| 96 base::WeakPtr<SyncableService> |
| 97 AutofillProfileDataTypeController::GetWeakPtrToSyncableService() const { |
| 98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 99 return profile_sync_factory()->GetAutofillProfileSyncableService( |
| 100 web_data_service_.get()); |
| 101 } |
| 102 |
| 103 void AutofillProfileDataTypeController::StopModels() { |
| 104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 105 DCHECK(state() == STOPPING || state() == NOT_RUNNING); |
| 106 notification_registrar_.RemoveAll(); |
| 107 personal_data_->RemoveObserver(this); |
| 108 } |
| 109 |
| 110 void AutofillProfileDataTypeController::StopLocalServiceAsync() { |
| 111 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, |
| 112 base::Bind(&AutofillProfileDataTypeController::StopLocalService, |
| 113 this)); |
| 114 } |
20 syncable::ModelType AutofillProfileDataTypeController::type() const { | 115 syncable::ModelType AutofillProfileDataTypeController::type() const { |
21 return syncable::AUTOFILL_PROFILE; | 116 return syncable::AUTOFILL_PROFILE; |
22 } | 117 } |
23 | 118 |
24 void AutofillProfileDataTypeController::CreateSyncComponents() { | 119 browser_sync::ModelSafeGroup |
25 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 120 AutofillProfileDataTypeController::model_safe_group() const { |
26 ProfileSyncFactory::SyncComponents sync_components = | 121 return browser_sync::GROUP_DB; |
27 profile_sync_factory()-> | |
28 CreateAutofillProfileSyncComponents(profile_sync_service(), | |
29 web_data_service(), | |
30 this); | |
31 set_model_associator(sync_components.model_associator); | |
32 set_change_processor(sync_components.change_processor); | |
33 } | 122 } |
34 | 123 |
35 void AutofillProfileDataTypeController::RecordUnrecoverableError( | 124 void AutofillProfileDataTypeController::RecordUnrecoverableError( |
36 const tracked_objects::Location& from_here, | 125 const tracked_objects::Location& from_here, |
37 const std::string& message) { | 126 const std::string& message) { |
38 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 127 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
39 UMA_HISTOGRAM_COUNTS("Sync.AutofillProfileRunFailures", 1); | 128 UMA_HISTOGRAM_COUNTS("Sync.AutofillProfileRunFailures", 1); |
40 } | 129 } |
41 | 130 |
42 void AutofillProfileDataTypeController::RecordAssociationTime( | 131 void AutofillProfileDataTypeController::RecordAssociationTime( |
43 base::TimeDelta time) { | 132 base::TimeDelta time) { |
44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 133 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
45 UMA_HISTOGRAM_TIMES("Sync.AutofillProfileAssociationTime", time); | 134 UMA_HISTOGRAM_TIMES("Sync.AutofillProfileAssociationTime", time); |
46 } | 135 } |
47 | 136 |
48 void AutofillProfileDataTypeController::RecordStartFailure(StartResult result) { | 137 void AutofillProfileDataTypeController::RecordStartFailure(StartResult result) { |
49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 138 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
50 UMA_HISTOGRAM_ENUMERATION("Sync.AutofillProfileStartFailures", | 139 UMA_HISTOGRAM_ENUMERATION("Sync.AutofillProfileStartFailures", |
51 result, | 140 result, |
52 MAX_START_RESULT); | 141 MAX_START_RESULT); |
53 } | 142 } |
54 | 143 |
55 } // namepsace browser_sync | 144 } // namepsace browser_sync |
OLD | NEW |