Skip to content

Commit

Permalink
[Sync] Componentize AutofillWalletDataTypeController.
Browse files Browse the repository at this point in the history
BUG=511283

Review URL: https://codereview.chromium.org/1373573003

Cr-Commit-Position: refs/heads/master@{#352680}
  • Loading branch information
maxbogue authored and Commit bot committed Oct 6, 2015
1 parent 21eb426 commit 6aeb907
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 42 deletions.
12 changes: 9 additions & 3 deletions chrome/browser/sync/profile_sync_components_factory_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sync/glue/autofill_data_type_controller.h"
#include "chrome/browser/sync/glue/autofill_profile_data_type_controller.h"
#include "chrome/browser/sync/glue/autofill_wallet_data_type_controller.h"
#include "chrome/browser/sync/glue/bookmark_change_processor.h"
#include "chrome/browser/sync/glue/bookmark_data_type_controller.h"
#include "chrome/browser/sync/glue/bookmark_model_associator.h"
Expand All @@ -29,6 +28,7 @@
#include "chrome/browser/sync/sessions/session_data_type_controller.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "components/autofill/core/browser/autofill_wallet_data_type_controller.h"
#include "components/autofill/core/common/autofill_pref_names.h"
#include "components/autofill/core/common/autofill_switches.h"
#include "components/dom_distiller/core/dom_distiller_features.h"
Expand Down Expand Up @@ -187,7 +187,10 @@ void ProfileSyncComponentsFactoryImpl::RegisterCommonDataTypes(
if (!wallet_disabled) {
sync_service->RegisterDataTypeController(
new browser_sync::AutofillWalletDataTypeController(
sync_client, syncer::AUTOFILL_WALLET_DATA));
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB),
base::Bind(&ChromeReportUnrecoverableError), sync_client,
syncer::AUTOFILL_WALLET_DATA));
}

// Wallet metadata sync depends on Wallet data sync and is disabled by
Expand All @@ -196,7 +199,10 @@ void ProfileSyncComponentsFactoryImpl::RegisterCommonDataTypes(
if (!wallet_disabled && enabled_types.Has(syncer::AUTOFILL_WALLET_METADATA)) {
sync_service->RegisterDataTypeController(
new browser_sync::AutofillWalletDataTypeController(
sync_client, syncer::AUTOFILL_WALLET_METADATA));
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB),
base::Bind(&ChromeReportUnrecoverableError), sync_client,
syncer::AUTOFILL_WALLET_DATA));
}

// Bookmark sync is enabled by default. Register unless explicitly
Expand Down
2 changes: 0 additions & 2 deletions chrome/chrome_browser.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -2849,8 +2849,6 @@
'browser/sync/glue/autofill_data_type_controller.h',
'browser/sync/glue/autofill_profile_data_type_controller.cc',
'browser/sync/glue/autofill_profile_data_type_controller.h',
'browser/sync/glue/autofill_wallet_data_type_controller.cc',
'browser/sync/glue/autofill_wallet_data_type_controller.h',
'browser/sync/glue/bookmark_change_processor.cc',
'browser/sync/glue/bookmark_change_processor.h',
'browser/sync/glue/bookmark_data_type_controller.cc',
Expand Down
3 changes: 3 additions & 0 deletions components/autofill.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
'rappor',
'signin_core_browser',
'signin_core_common',
'sync_driver',
'variations_http_provider',
'webdata_common',
],
Expand Down Expand Up @@ -143,6 +144,8 @@
'autofill/core/browser/autofill_sync_constants.h',
'autofill/core/browser/autofill_type.cc',
'autofill/core/browser/autofill_type.h',
'autofill/core/browser/autofill_wallet_data_type_controller.cc',
'autofill/core/browser/autofill_wallet_data_type_controller.h',
'autofill/core/browser/autofill_xml_parser.cc',
'autofill/core/browser/autofill_xml_parser.h',
'autofill/core/browser/card_unmask_delegate.cc',
Expand Down
2 changes: 2 additions & 0 deletions components/autofill/core/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ source_set("browser") {
"autofill_sync_constants.h",
"autofill_type.cc",
"autofill_type.h",
"autofill_wallet_data_type_controller.cc",
"autofill_wallet_data_type_controller.h",
"autofill_xml_parser.cc",
"autofill_xml_parser.h",
"card_unmask_delegate.cc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,34 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/sync/glue/autofill_wallet_data_type_controller.h"
#include "components/autofill/core/browser/autofill_wallet_data_type_controller.h"

#include "base/bind.h"
#include "base/prefs/pref_service.h"
#include "chrome/browser/sync/glue/chrome_report_unrecoverable_error.h"
#include "chrome/common/pref_names.h"
#include "components/autofill/core/browser/personal_data_manager.h"
#include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
#include "components/autofill/core/common/autofill_pref_names.h"
#include "components/sync_driver/sync_client.h"
#include "components/sync_driver/sync_service.h"
#include "content/public/browser/browser_thread.h"
#include "sync/api/sync_error.h"
#include "sync/api/syncable_service.h"

using content::BrowserThread;

namespace browser_sync {

AutofillWalletDataTypeController::AutofillWalletDataTypeController(
const scoped_refptr<base::SingleThreadTaskRunner>& ui_thread,
const scoped_refptr<base::SingleThreadTaskRunner>& db_thread,
const base::Closure& error_callback,
sync_driver::SyncClient* sync_client,
syncer::ModelType model_type)
: NonUIDataTypeController(
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
base::Bind(&ChromeReportUnrecoverableError),
sync_client),
: NonUIDataTypeController(ui_thread, error_callback, sync_client),
ui_thread_(ui_thread),
db_thread_(db_thread),
sync_client_(sync_client),
callback_registered_(false),
model_type_(model_type),
currently_enabled_(IsEnabled()) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(ui_thread_->BelongsToCurrentThread());
DCHECK(model_type_ == syncer::AUTOFILL_WALLET_DATA ||
model_type_ == syncer::AUTOFILL_WALLET_METADATA);
pref_registrar_.Init(sync_client_->GetPrefService());
Expand All @@ -45,27 +43,26 @@ AutofillWalletDataTypeController::AutofillWalletDataTypeController(
base::Unretained(this)));
}

AutofillWalletDataTypeController::~AutofillWalletDataTypeController() {
}
AutofillWalletDataTypeController::~AutofillWalletDataTypeController() {}

syncer::ModelType AutofillWalletDataTypeController::type() const {
return model_type_;
}

syncer::ModelSafeGroup
AutofillWalletDataTypeController::model_safe_group() const {
syncer::ModelSafeGroup AutofillWalletDataTypeController::model_safe_group()
const {
return syncer::GROUP_DB;
}

bool AutofillWalletDataTypeController::PostTaskOnBackendThread(
const tracked_objects::Location& from_here,
const base::Closure& task) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
return BrowserThread::PostTask(BrowserThread::DB, from_here, task);
DCHECK(ui_thread_->BelongsToCurrentThread());
return db_thread_->PostTask(from_here, task);
}

bool AutofillWalletDataTypeController::StartModels() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(ui_thread_->BelongsToCurrentThread());
DCHECK_EQ(state(), MODEL_STARTING);

scoped_refptr<autofill::AutofillWebDataService> web_data_service =
Expand All @@ -78,16 +75,16 @@ bool AutofillWalletDataTypeController::StartModels() {
return true;

if (!callback_registered_) {
web_data_service->RegisterDBLoadedCallback(base::Bind(
&AutofillWalletDataTypeController::OnModelLoaded, this));
callback_registered_ = true;
web_data_service->RegisterDBLoadedCallback(
base::Bind(&AutofillWalletDataTypeController::OnModelLoaded, this));
callback_registered_ = true;
}

return false;
}

void AutofillWalletDataTypeController::StopModels() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(ui_thread_->BelongsToCurrentThread());

// This function is called when shutting down (nothing is changing), when
// sync is disabled completely, or when wallet sync is disabled. In the
Expand All @@ -102,21 +99,20 @@ void AutofillWalletDataTypeController::StopModels() {
// currently_enabled_ indicates if the other prefs are enabled. All of these
// have to be enabled to sync wallet data/metadata.
if (!service->HasSyncSetupCompleted() ||
!service->GetPreferredDataTypes().Has(type()) ||
!currently_enabled_) {
!service->GetPreferredDataTypes().Has(type()) || !currently_enabled_) {
autofill::PersonalDataManager* pdm = sync_client_->GetPersonalDataManager();
if (pdm)
pdm->ClearAllServerData();
}
}

bool AutofillWalletDataTypeController::ReadyForStart() const {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(ui_thread_->BelongsToCurrentThread());
return currently_enabled_;
}

void AutofillWalletDataTypeController::OnSyncPrefChanged() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(ui_thread_->BelongsToCurrentThread());

bool new_enabled = IsEnabled();
if (currently_enabled_ == new_enabled)
Expand All @@ -136,23 +132,21 @@ void AutofillWalletDataTypeController::OnSyncPrefChanged() {
base::Bind(&DataTypeController::OnSingleDataTypeUnrecoverableError,
this,
syncer::SyncError(
FROM_HERE,
syncer::SyncError::DATATYPE_POLICY_ERROR,
"Wallet syncing is disabled by policy.",
type())));
FROM_HERE, syncer::SyncError::DATATYPE_POLICY_ERROR,
"Wallet syncing is disabled by policy.", type())));
}
}
}

bool AutofillWalletDataTypeController::IsEnabled() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(ui_thread_->BelongsToCurrentThread());

// Require both the sync experiment and the user-visible pref to be
// enabled to sync Wallet data/metadata.
PrefService* ps = sync_client_->GetPrefService();
return
ps->GetBoolean(autofill::prefs::kAutofillWalletSyncExperimentEnabled) &&
ps->GetBoolean(autofill::prefs::kAutofillWalletImportEnabled);
return ps->GetBoolean(
autofill::prefs::kAutofillWalletSyncExperimentEnabled) &&
ps->GetBoolean(autofill::prefs::kAutofillWalletImportEnabled);
}

} // namespace browser_sync
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_SYNC_GLUE_AUTOFILL_WALLET_DATA_TYPE_CONTROLLER_H_
#define CHROME_BROWSER_SYNC_GLUE_AUTOFILL_WALLET_DATA_TYPE_CONTROLLER_H_
#ifndef COMPONENTS_SYNC_DRIVER_GLUE_AUTOFILL_WALLET_DATA_TYPE_CONTROLLER_H_
#define COMPONENTS_SYNC_DRIVER_GLUE_AUTOFILL_WALLET_DATA_TYPE_CONTROLLER_H_

#include "base/basictypes.h"
#include "base/prefs/pref_change_registrar.h"
Expand All @@ -17,6 +17,9 @@ class AutofillWalletDataTypeController
public:
// |model_type| should be either AUTOFILL_WALLET or AUTOFILL_WALLET_METADATA.
AutofillWalletDataTypeController(
const scoped_refptr<base::SingleThreadTaskRunner>& ui_thread,
const scoped_refptr<base::SingleThreadTaskRunner>& db_thread,
const base::Closure& error_callback,
sync_driver::SyncClient* sync_client,
syncer::ModelType model_type);

Expand All @@ -41,6 +44,12 @@ class AutofillWalletDataTypeController
// Returns true if the prefs are set such that wallet sync should be enabled.
bool IsEnabled();

// A reference to the UI thread's task runner.
const scoped_refptr<base::SingleThreadTaskRunner> ui_thread_;

// A reference to the DB thread's task runner.
const scoped_refptr<base::SingleThreadTaskRunner> db_thread_;

sync_driver::SyncClient* const sync_client_;
bool callback_registered_;
syncer::ModelType model_type_;
Expand All @@ -57,4 +66,4 @@ class AutofillWalletDataTypeController

} // namespace browser_sync

#endif // CHROME_BROWSER_SYNC_GLUE_AUTOFILL_WALLET_DATA_TYPE_CONTROLLER_H_
#endif // COMPONENTS_SYNC_DRIVER_GLUE_AUTOFILL_WALLET_DATA_TYPE_CONTROLLER_H_

0 comments on commit 6aeb907

Please sign in to comment.