Commit ae417733 authored by dullweber's avatar dullweber Committed by Commit bot

Show autofill sync status in CBD

This changes the AutofillCounter to indicate that autofill data is synced.

BUG=681523

Review-Url: https://codereview.chromium.org/2828083003
Cr-Commit-Position: refs/heads/master@{#468606}
parent 172ca56f
......@@ -11,6 +11,7 @@
#include "base/strings/utf_string_conversions.h"
#include "base/threading/platform_thread.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sync/test/integration/sync_test.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/web_data_service_factory.h"
#include "chrome/test/base/in_process_browser_test.h"
......@@ -19,6 +20,7 @@
#include "components/autofill/core/browser/autofill_type.h"
#include "components/autofill/core/browser/credit_card.h"
#include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
#include "components/browser_sync/profile_sync_service.h"
#include "components/browsing_data/core/browsing_data_utils.h"
#include "components/browsing_data/core/pref_names.h"
#include "components/prefs/pref_service.h"
......@@ -26,9 +28,11 @@
namespace {
class AutofillCounterTest : public InProcessBrowserTest {
// TODO(crbug.com/553421): Only RestartOnSyncChange is a SyncTest.
// Extract it together with HistoryCounterTest.RestartOnSyncChange.
class AutofillCounterTest : public SyncTest {
public:
AutofillCounterTest() {}
AutofillCounterTest() : SyncTest(SINGLE_CLIENT) {}
~AutofillCounterTest() override {}
void SetUpOnMainThread() override {
......@@ -152,6 +156,20 @@ class AutofillCounterTest : public InProcessBrowserTest {
run_loop_->Run();
}
bool CountingFinishedSinceLastAsked() {
bool result = finished_;
finished_ = false;
return result;
}
void WaitForCountingOrConfirmFinished() {
if (CountingFinishedSinceLastAsked())
return;
WaitForCounting();
CountingFinishedSinceLastAsked();
}
browsing_data::BrowsingDataCounter::ResultInt GetNumSuggestions() {
DCHECK(finished_);
return num_suggestions_;
......@@ -167,6 +185,8 @@ class AutofillCounterTest : public InProcessBrowserTest {
return num_addresses_;
}
bool AutofillSyncEnabled() { return autofill_sync_enabled_; }
void Callback(
std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) {
finished_ = result->Finished();
......@@ -179,6 +199,7 @@ class AutofillCounterTest : public InProcessBrowserTest {
num_suggestions_ = autofill_result->Value();
num_credit_cards_ = autofill_result->num_credit_cards();
num_addresses_ = autofill_result->num_addresses();
autofill_sync_enabled_ = autofill_result->autofill_sync_enabled();
}
if (run_loop_ && finished_)
......@@ -197,6 +218,7 @@ class AutofillCounterTest : public InProcessBrowserTest {
browsing_data::BrowsingDataCounter::ResultInt num_suggestions_;
browsing_data::BrowsingDataCounter::ResultInt num_credit_cards_;
browsing_data::BrowsingDataCounter::ResultInt num_addresses_;
bool autofill_sync_enabled_;
DISALLOW_COPY_AND_ASSIGN(AutofillCounterTest);
};
......@@ -204,7 +226,7 @@ class AutofillCounterTest : public InProcessBrowserTest {
// Tests that we count the correct number of autocomplete suggestions.
IN_PROC_BROWSER_TEST_F(AutofillCounterTest, AutocompleteSuggestions) {
Profile* profile = browser()->profile();
browsing_data::AutofillCounter counter(GetWebDataService());
browsing_data::AutofillCounter counter(GetWebDataService(), nullptr);
counter.Init(
profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED,
base::Bind(&AutofillCounterTest::Callback, base::Unretained(this)));
......@@ -241,7 +263,7 @@ IN_PROC_BROWSER_TEST_F(AutofillCounterTest, AutocompleteSuggestions) {
// Tests that we count the correct number of credit cards.
IN_PROC_BROWSER_TEST_F(AutofillCounterTest, CreditCards) {
Profile* profile = browser()->profile();
browsing_data::AutofillCounter counter(GetWebDataService());
browsing_data::AutofillCounter counter(GetWebDataService(), nullptr);
counter.Init(
profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED,
base::Bind(&AutofillCounterTest::Callback, base::Unretained(this)));
......@@ -278,7 +300,7 @@ IN_PROC_BROWSER_TEST_F(AutofillCounterTest, CreditCards) {
// Tests that we count the correct number of addresses.
IN_PROC_BROWSER_TEST_F(AutofillCounterTest, Addresses) {
Profile* profile = browser()->profile();
browsing_data::AutofillCounter counter(GetWebDataService());
browsing_data::AutofillCounter counter(GetWebDataService(), nullptr);
counter.Init(
profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED,
base::Bind(&AutofillCounterTest::Callback, base::Unretained(this)));
......@@ -329,7 +351,7 @@ IN_PROC_BROWSER_TEST_F(AutofillCounterTest, ComplexResult) {
AddAddress("John", "Smith", "Side Street 47");
Profile* profile = browser()->profile();
browsing_data::AutofillCounter counter(GetWebDataService());
browsing_data::AutofillCounter counter(GetWebDataService(), nullptr);
counter.Init(
profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED,
base::Bind(&AutofillCounterTest::Callback, base::Unretained(this)));
......@@ -385,7 +407,7 @@ IN_PROC_BROWSER_TEST_F(AutofillCounterTest, TimeRanges) {
};
Profile* profile = browser()->profile();
browsing_data::AutofillCounter counter(GetWebDataService());
browsing_data::AutofillCounter counter(GetWebDataService(), nullptr);
counter.Init(
profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED,
base::Bind(&AutofillCounterTest::Callback, base::Unretained(this)));
......@@ -400,4 +422,71 @@ IN_PROC_BROWSER_TEST_F(AutofillCounterTest, TimeRanges) {
}
}
// Test that the counting restarts when autofill sync state changes.
// TODO(crbug.com/553421): Move this to the sync/test/integration directory?
IN_PROC_BROWSER_TEST_F(AutofillCounterTest, RestartOnSyncChange) {
// Set up the Sync client.
ASSERT_TRUE(SetupClients());
static const int kFirstProfileIndex = 0;
browser_sync::ProfileSyncService* sync_service =
GetSyncService(kFirstProfileIndex);
Profile* profile = GetProfile(kFirstProfileIndex);
// Set up the counter.
browsing_data::AutofillCounter counter(
WebDataServiceFactory::GetAutofillWebDataForProfile(
profile, ServiceAccessType::IMPLICIT_ACCESS),
sync_service);
counter.Init(
profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED,
base::Bind(&AutofillCounterTest::Callback, base::Unretained(this)));
// Note that some Sync operations notify observers immediately (and thus there
// is no need to call |WaitForCounting()|; in fact, it would block the test),
// while other operations only post the task on UI thread's message loop
// (which requires calling |WaitForCounting()| for them to run). Therefore,
// this test always checks if the callback has already run and only waits
// if it has not.
// We sync all datatypes by default, so starting Sync means that we start
// syncing autofill, and this should restart the counter.
ASSERT_TRUE(SetupSync());
ASSERT_TRUE(sync_service->IsSyncActive());
ASSERT_TRUE(sync_service->GetActiveDataTypes().Has(syncer::AUTOFILL));
WaitForCountingOrConfirmFinished();
EXPECT_TRUE(AutofillSyncEnabled());
// We stop syncing autofill in particular. This restarts the counter.
syncer::ModelTypeSet everything_except_autofill =
syncer::UserSelectableTypes();
everything_except_autofill.Remove(syncer::AUTOFILL);
auto sync_blocker = sync_service->GetSetupInProgressHandle();
sync_service->OnUserChoseDatatypes(/*sync_everything=*/false,
everything_except_autofill);
ASSERT_FALSE(sync_service->GetPreferredDataTypes().Has(syncer::AUTOFILL));
sync_blocker.reset();
WaitForCountingOrConfirmFinished();
ASSERT_FALSE(sync_service->GetActiveDataTypes().Has(syncer::AUTOFILL));
EXPECT_FALSE(AutofillSyncEnabled());
// If autofill sync is not affected, the counter is not restarted.
syncer::ModelTypeSet only_history(syncer::HISTORY_DELETE_DIRECTIVES);
sync_blocker = sync_service->GetSetupInProgressHandle();
sync_service->ChangePreferredDataTypes(only_history);
sync_blocker.reset();
EXPECT_FALSE(CountingFinishedSinceLastAsked());
// We start syncing autofill again. This restarts the counter.
sync_blocker = sync_service->GetSetupInProgressHandle();
sync_service->ChangePreferredDataTypes(syncer::ModelTypeSet::All());
sync_blocker.reset();
WaitForCountingOrConfirmFinished();
EXPECT_TRUE(AutofillSyncEnabled());
// Stopping the Sync service triggers a restart.
sync_service->RequestStop(syncer::SyncService::CLEAR_DATA);
WaitForCountingOrConfirmFinished();
EXPECT_FALSE(AutofillSyncEnabled());
}
} // namespace
......@@ -84,7 +84,8 @@ BrowsingDataCounterFactory::GetForProfileAndPref(Profile* profile,
if (pref_name == browsing_data::prefs::kDeleteFormData) {
return base::MakeUnique<browsing_data::AutofillCounter>(
WebDataServiceFactory::GetAutofillWebDataForProfile(
profile, ServiceAccessType::EXPLICIT_ACCESS));
profile, ServiceAccessType::EXPLICIT_ACCESS),
ProfileSyncServiceFactory::GetForProfile(profile));
}
if (pref_name == browsing_data::prefs::kDeleteDownloadHistory)
......
......@@ -398,10 +398,12 @@ IN_PROC_BROWSER_TEST_F(HistoryCounterTest, RestartOnSyncChange) {
WaitForCountingOrConfirmFinished();
// We stop syncing history deletion in particular. This restarts the counter.
syncer::ModelTypeSet everything_except_history = syncer::ModelTypeSet::All();
syncer::ModelTypeSet everything_except_history =
syncer::UserSelectableTypes();
everything_except_history.Remove(syncer::HISTORY_DELETE_DIRECTIVES);
auto sync_blocker = sync_service->GetSetupInProgressHandle();
sync_service->ChangePreferredDataTypes(everything_except_history);
sync_service->OnUserChoseDatatypes(/*sync_everything=*/false,
everything_except_history);
sync_blocker.reset();
WaitForCountingOrConfirmFinished();
......
......@@ -332,12 +332,15 @@ IN_PROC_BROWSER_TEST_F(PasswordsCounterTest, RestartOnSyncChange) {
// We stop syncing passwords in particular. This restarts the counter.
syncer::ModelTypeSet everything_except_passwords =
syncer::ModelTypeSet::All();
syncer::UserSelectableTypes();
everything_except_passwords.Remove(syncer::PASSWORDS);
auto sync_blocker = sync_service->GetSetupInProgressHandle();
sync_service->ChangePreferredDataTypes(everything_except_passwords);
sync_service->OnUserChoseDatatypes(/*sync_everything=*/false,
everything_except_passwords);
ASSERT_FALSE(sync_service->GetPreferredDataTypes().Has(syncer::PASSWORDS));
sync_blocker.reset();
WaitForCountingOrConfirmFinished();
ASSERT_FALSE(sync_service->GetPreferredDataTypes().Has(syncer::PASSWORDS));
EXPECT_FALSE(PasswordSyncEnabled());
// If password sync is not affected, the counter is not restarted.
......
......@@ -145,23 +145,30 @@ base::string16 GetCounterTextFromResult(
}
}
bool synced = autofill_result->autofill_sync_enabled();
// Construct the resulting string from the sections in |displayed_strings|.
switch (displayed_strings.size()) {
case 0:
text = l10n_util::GetStringUTF16(IDS_DEL_AUTOFILL_COUNTER_EMPTY);
break;
case 1:
text = displayed_strings[0];
text = synced ? l10n_util::GetStringFUTF16(
IDS_DEL_AUTOFILL_COUNTER_ONE_TYPE_SYNCED,
displayed_strings[0])
: displayed_strings[0];
break;
case 2:
text = l10n_util::GetStringFUTF16(IDS_DEL_AUTOFILL_COUNTER_TWO_TYPES,
displayed_strings[0],
displayed_strings[1]);
text = l10n_util::GetStringFUTF16(
synced ? IDS_DEL_AUTOFILL_COUNTER_TWO_TYPES_SYNCED
: IDS_DEL_AUTOFILL_COUNTER_TWO_TYPES,
displayed_strings[0], displayed_strings[1]);
break;
case 3:
text = l10n_util::GetStringFUTF16(
IDS_DEL_AUTOFILL_COUNTER_THREE_TYPES, displayed_strings[0],
displayed_strings[1], displayed_strings[2]);
synced ? IDS_DEL_AUTOFILL_COUNTER_THREE_TYPES_SYNCED
: IDS_DEL_AUTOFILL_COUNTER_THREE_TYPES,
displayed_strings[0], displayed_strings[1], displayed_strings[2]);
break;
default:
NOTREACHED();
......
......@@ -52,7 +52,7 @@ class BrowsingDataUtilsTest : public testing::Test {
// Tests the complex output of the Autofill counter.
TEST_F(BrowsingDataUtilsTest, AutofillCounterResult) {
browsing_data::AutofillCounter counter(
scoped_refptr<FakeWebDataService>(new FakeWebDataService()));
scoped_refptr<FakeWebDataService>(new FakeWebDataService()), nullptr);
// Test all configurations of zero and nonzero partial results for datatypes.
// Test singular and plural for each datatype.
......@@ -60,24 +60,29 @@ TEST_F(BrowsingDataUtilsTest, AutofillCounterResult) {
int num_credit_cards;
int num_addresses;
int num_suggestions;
bool sync_enabled;
std::string expected_output;
} kTestCases[] = {
{0, 0, 0, "none"},
{1, 0, 0, "1 credit card"},
{0, 5, 0, "5 addresses"},
{0, 0, 1, "1 suggestion"},
{0, 0, 2, "2 suggestions"},
{4, 7, 0, "4 credit cards, 7 addresses"},
{3, 0, 9, "3 credit cards, 9 other suggestions"},
{0, 1, 1, "1 address, 1 other suggestion"},
{9, 6, 3, "9 credit cards, 6 addresses, 3 others"},
{4, 2, 1, "4 credit cards, 2 addresses, 1 other"},
{0, 0, 0, false, "none"},
{0, 0, 0, true, "none"},
{1, 0, 0, false, "1 credit card"},
{0, 5, 0, false, "5 addresses"},
{0, 0, 1, false, "1 suggestion"},
{0, 0, 2, false, "2 suggestions"},
{0, 0, 2, true, "2 suggestions (synced)"},
{4, 7, 0, false, "4 credit cards, 7 addresses"},
{4, 7, 0, true, "4 credit cards, 7 addresses (synced)"},
{3, 0, 9, false, "3 credit cards, 9 other suggestions"},
{0, 1, 1, false, "1 address, 1 other suggestion"},
{9, 6, 3, false, "9 credit cards, 6 addresses, 3 others"},
{4, 2, 1, false, "4 credit cards, 2 addresses, 1 other"},
{4, 2, 1, true, "4 credit cards, 2 addresses, 1 other (synced)"},
};
for (const TestCase& test_case : kTestCases) {
browsing_data::AutofillCounter::AutofillResult result(
&counter, test_case.num_suggestions, test_case.num_credit_cards,
test_case.num_addresses);
test_case.num_addresses, test_case.sync_enabled);
SCOPED_TRACE(
base::StringPrintf("Test params: %d credit card(s), "
......
......@@ -8,29 +8,49 @@
#include <utility>
#include <vector>
#include "base/memory/ptr_util.h"
#include "components/autofill/core/browser/autofill_profile.h"
#include "components/autofill/core/browser/credit_card.h"
#include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
#include "components/browsing_data/core/pref_names.h"
#include "components/sync/driver/sync_service.h"
namespace {
bool IsAutofillSyncEnabled(const syncer::SyncService* sync_service) {
return sync_service && sync_service->IsFirstSetupComplete() &&
sync_service->IsSyncActive() &&
sync_service->GetActiveDataTypes().Has(syncer::AUTOFILL);
}
} // namespace
namespace browsing_data {
AutofillCounter::AutofillCounter(
scoped_refptr<autofill::AutofillWebDataService> web_data_service)
scoped_refptr<autofill::AutofillWebDataService> web_data_service,
syncer::SyncService* sync_service)
: web_data_service_(web_data_service),
sync_service_(sync_service),
suggestions_query_(0),
credit_cards_query_(0),
addresses_query_(0),
num_suggestions_(0),
num_credit_cards_(0),
num_addresses_(0) {}
num_addresses_(0),
autofill_sync_enabled_() {}
AutofillCounter::~AutofillCounter() {
CancelAllRequests();
if (sync_service_)
sync_service_->RemoveObserver(this);
}
void AutofillCounter::OnInitialized() {
DCHECK(web_data_service_);
if (sync_service_)
sync_service_->AddObserver(this);
autofill_sync_enabled_ = IsAutofillSyncEnabled(sync_service_);
}
const char* AutofillCounter::GetPrefName() const {
......@@ -150,8 +170,9 @@ void AutofillCounter::OnWebDataServiceRequestDone(
if (suggestions_query_ || credit_cards_query_ || addresses_query_)
return;
std::unique_ptr<Result> reported_result(new AutofillResult(
this, num_suggestions_, num_credit_cards_, num_addresses_));
auto reported_result = base::MakeUnique<AutofillResult>(
this, num_suggestions_, num_credit_cards_, num_addresses_,
autofill_sync_enabled_);
ReportResult(std::move(reported_result));
}
......@@ -164,15 +185,25 @@ void AutofillCounter::CancelAllRequests() {
web_data_service_->CancelRequest(addresses_query_);
}
void AutofillCounter::OnStateChanged(syncer::SyncService* sync) {
bool sync_enabled_new = IsAutofillSyncEnabled(sync);
if (autofill_sync_enabled_ != sync_enabled_new) {
autofill_sync_enabled_ = sync_enabled_new;
Restart();
}
}
// AutofillCounter::AutofillResult ---------------------------------------------
AutofillCounter::AutofillResult::AutofillResult(const AutofillCounter* source,
ResultInt num_suggestions,
ResultInt num_credit_cards,
ResultInt num_addresses)
ResultInt num_addresses,
bool autofill_sync_enabled_)
: FinishedResult(source, num_suggestions),
num_credit_cards_(num_credit_cards),
num_addresses_(num_addresses) {}
num_addresses_(num_addresses),
autofill_sync_enabled_(autofill_sync_enabled_) {}
AutofillCounter::AutofillResult::~AutofillResult() {}
......
......@@ -10,6 +10,7 @@
#include "base/time/time.h"
#include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
#include "components/browsing_data/core/counters/browsing_data_counter.h"
#include "components/sync/driver/sync_service_observer.h"
#include "components/webdata/common/web_data_service_consumer.h"
namespace autofill {
......@@ -19,28 +20,33 @@ class AutofillWebDataService;
namespace browsing_data {
class AutofillCounter : public browsing_data::BrowsingDataCounter,
public WebDataServiceConsumer {
public WebDataServiceConsumer,
public syncer::SyncServiceObserver {
public:
class AutofillResult : public FinishedResult {
public:
AutofillResult(const AutofillCounter* source,
ResultInt num_suggestions,
ResultInt num_credit_cards,
ResultInt num_addresses);
ResultInt num_addresses,
bool autofill_sync_enabled_);
~AutofillResult() override;
ResultInt num_credit_cards() const { return num_credit_cards_; }
ResultInt num_addresses() const { return num_addresses_; }
bool autofill_sync_enabled() const { return autofill_sync_enabled_; }
private:
ResultInt num_credit_cards_;
ResultInt num_addresses_;
bool autofill_sync_enabled_;
DISALLOW_COPY_AND_ASSIGN(AutofillResult);
};
explicit AutofillCounter(
scoped_refptr<autofill::AutofillWebDataService> web_data_service);
scoped_refptr<autofill::AutofillWebDataService> web_data_service,
syncer::SyncService* sync_service);
~AutofillCounter() override;
// BrowsingDataCounter implementation.
......@@ -64,12 +70,16 @@ class AutofillCounter : public browsing_data::BrowsingDataCounter,
WebDataServiceBase::Handle handle,
std::unique_ptr<WDTypedResult> result) override;
// SyncServiceObserver implementation.
void OnStateChanged(syncer::SyncService* sync) override;
// Cancel all pending requests to AutofillWebdataService.
void CancelAllRequests();
base::ThreadChecker thread_checker_;
scoped_refptr<autofill::AutofillWebDataService> web_data_service_;
syncer::SyncService* sync_service_;
WebDataServiceBase::Handle suggestions_query_;
WebDataServiceBase::Handle credit_cards_query_;
......@@ -78,6 +88,7 @@ class AutofillCounter : public browsing_data::BrowsingDataCounter,
ResultInt num_suggestions_;
ResultInt num_credit_cards_;
ResultInt num_addresses_;
bool autofill_sync_enabled_;
base::Time period_start_for_testing_;
......
......@@ -65,12 +65,21 @@
=1 {1 other}
other {# others}}
</message>
<message name="IDS_DEL_AUTOFILL_COUNTER_ONE_TYPE_SYNCED" desc="A counter showing that the user has one type of form data and that it is synced. The type will be substituted, this message only provides the (synced) part.">
<ph name="TYPE_1">$1<ex>2 credit cards</ex></ph> (synced)
</message>
<message name="IDS_DEL_AUTOFILL_COUNTER_TWO_TYPES" desc="A counter showing that the user has two types of form data. The types and their counts will be substituted, this message only provides the comma separator.">
<ph name="TYPE_1">$1<ex>2 credit cards</ex></ph>, <ph name="TYPE_2">$2<ex>1 address</ex></ph>
</message>
<message name="IDS_DEL_AUTOFILL_COUNTER_TWO_TYPES_SYNCED" desc="A counter showing that the user has two types of form data and that they are synced. The types and their counts will be substituted, this message only provides the comma separator.">
<ph name="TYPE_1">$1<ex>2 credit cards</ex></ph>, <ph name="TYPE_2">$2<ex>1 address</ex></ph> (synced)
</message>
<message name="IDS_DEL_AUTOFILL_COUNTER_THREE_TYPES" desc="A counter showing that the user has three types of form data. The types and their counts will be substituted, this message only provides the comma separators.">
<ph name="TYPE_1">$1<ex>2 credit cards</ex></ph>, <ph name="TYPE_2">$2<ex>1 address</ex></ph>, <ph name="TYPE_3">$3<ex>5 others</ex></ph>
</message>
<message name="IDS_DEL_AUTOFILL_COUNTER_THREE_TYPES_SYNCED" desc="A counter showing that the user has three types of form data and that they are synced. The types and their counts will be substituted, this message only provides the comma separators.">
<ph name="TYPE_1">$1<ex>2 credit cards</ex></ph>, <ph name="TYPE_2">$2<ex>1 address</ex></ph>, <ph name="TYPE_3">$3<ex>5 others</ex></ph> (synced)
</message>
<message name="IDS_DEL_COOKIES_COUNTER" desc="A static message shown in the Clear Browsing Data dialog explaining to the user that deleting cookies and site data will result in the user being signed out of most websites.">
This will sign you out of most websites.
</message>
......
......@@ -53,7 +53,8 @@ IOSBrowsingDataCounterFactory::GetForBrowserStateAndPref(
if (pref_name == browsing_data::prefs::kDeleteFormData) {
return base::MakeUnique<browsing_data::AutofillCounter>(
ios::WebDataServiceFactory::GetAutofillWebDataForBrowserState(
browser_state, ServiceAccessType::EXPLICIT_ACCESS));
browser_state, ServiceAccessType::EXPLICIT_ACCESS),
IOSChromeProfileSyncServiceFactory::GetForBrowserState(browser_state));
}
return nullptr;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment