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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/sync/glue/autofill_data_type_controller.h"
6
7 #include "base/bind.h"
8 #include "base/metrics/histogram.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/sync/glue/chrome_report_unrecoverable_error.h"
11 #include "chrome/browser/sync/profile_sync_components_factory.h"
12 #include "chrome/browser/sync/profile_sync_service_factory.h"
13 #include "chrome/browser/webdata/autocomplete_syncable_service.h"
14 #include "chrome/browser/webdata/web_data_service_factory.h"
15 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "sync/api/sync_error.h"
18 #include "sync/internal_api/public/util/experiments.h"
19
20 using content::BrowserThread;
21
22 namespace browser_sync {
23
AutofillDataTypeController(ProfileSyncComponentsFactory * profile_sync_factory,Profile * profile)24 AutofillDataTypeController::AutofillDataTypeController(
25 ProfileSyncComponentsFactory* profile_sync_factory,
26 Profile* profile)
27 : NonUIDataTypeController(
28 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
29 base::Bind(&ChromeReportUnrecoverableError),
30 profile_sync_factory),
31 profile_(profile) {
32 }
33
type() const34 syncer::ModelType AutofillDataTypeController::type() const {
35 return syncer::AUTOFILL;
36 }
37
model_safe_group() const38 syncer::ModelSafeGroup AutofillDataTypeController::model_safe_group() const {
39 return syncer::GROUP_DB;
40 }
41
WebDatabaseLoaded()42 void AutofillDataTypeController::WebDatabaseLoaded() {
43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
44 DCHECK_EQ(MODEL_STARTING, state());
45
46 OnModelLoaded();
47 }
48
~AutofillDataTypeController()49 AutofillDataTypeController::~AutofillDataTypeController() {
50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
51 }
52
PostTaskOnBackendThread(const tracked_objects::Location & from_here,const base::Closure & task)53 bool AutofillDataTypeController::PostTaskOnBackendThread(
54 const tracked_objects::Location& from_here,
55 const base::Closure& task) {
56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
57 return BrowserThread::PostTask(BrowserThread::DB, from_here, task);
58 }
59
StartModels()60 bool AutofillDataTypeController::StartModels() {
61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
62 DCHECK_EQ(MODEL_STARTING, state());
63
64 autofill::AutofillWebDataService* web_data_service =
65 WebDataServiceFactory::GetAutofillWebDataForProfile(
66 profile_, Profile::EXPLICIT_ACCESS).get();
67
68 if (!web_data_service)
69 return false;
70
71 if (web_data_service->IsDatabaseLoaded()) {
72 return true;
73 } else {
74 web_data_service->RegisterDBLoadedCallback(base::Bind(
75 &AutofillDataTypeController::WebDatabaseLoaded, this));
76 return false;
77 }
78 }
79
StartAssociating(const StartCallback & start_callback)80 void AutofillDataTypeController::StartAssociating(
81 const StartCallback& start_callback) {
82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
83 DCHECK_EQ(state(), MODEL_LOADED);
84 ProfileSyncService* sync = ProfileSyncServiceFactory::GetForProfile(
85 profile_);
86 DCHECK(sync);
87 NonUIDataTypeController::StartAssociating(start_callback);
88 }
89
90 } // namespace browser_sync
91