Commit 767e6fb1 authored by Christian Dullweber's avatar Christian Dullweber Committed by Commit Bot

Replace history and password statistics with counters

Remove history and password related code from 
PasswordStatisticsAggregator replace it with BrowsingDataCounters.

Bug: 716267
Change-Id: I35b3f4e312732306dcb4ff2a9906976f3f62223e
Reviewed-on: https://chromium-review.googlesource.com/503232Reviewed-by: default avatarMihai Sardarescu <msarda@chromium.org>
Reviewed-by: default avatarMartin Šrámek <msramek@chromium.org>
Commit-Queue: Christian Dullweber <dullweber@chromium.org>
Cr-Commit-Position: refs/heads/master@{#475860}
parent 092f21a0
......@@ -170,6 +170,28 @@ IN_PROC_BROWSER_TEST_F(HistoryCounterTest, DuplicateVisits) {
EXPECT_EQ(7u, GetLocalResult());
}
// Tests that the counter works without |web_history_service_callback| and
// |sync_service|.
IN_PROC_BROWSER_TEST_F(HistoryCounterTest, WithoutSyncService) {
AddVisit("https://www.google.com");
AddVisit("https://www.chrome.com");
Profile* profile = browser()->profile();
browsing_data::HistoryCounter counter(
GetHistoryService(),
browsing_data::HistoryCounter::GetUpdatedWebHistoryServiceCallback(),
nullptr /* sync_service */);
counter.Init(
profile->GetPrefs(), browsing_data::ClearBrowsingDataTab::ADVANCED,
base::Bind(&HistoryCounterTest::Callback, base::Unretained(this)));
counter.Restart();
WaitForCounting();
EXPECT_EQ(2u, GetLocalResult());
}
// Tests that the counter starts counting automatically when the deletion
// pref changes to true.
IN_PROC_BROWSER_TEST_F(HistoryCounterTest, PrefChanged) {
......
......@@ -18,8 +18,9 @@
#include "chrome/browser/profiles/profile_statistics.h"
#include "chrome/browser/profiles/profile_statistics_factory.h"
#include "components/browsing_data/core/counters/bookmark_counter.h"
#include "components/history/core/browser/history_service.h"
#include "components/password_manager/core/browser/password_store.h"
#include "components/browsing_data/core/counters/history_counter.h"
#include "components/browsing_data/core/counters/passwords_counter.h"
#include "components/browsing_data/core/pref_names.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/browser_thread.h"
......@@ -45,21 +46,12 @@ void AccumulatePrefStats(const PrefService* pref_service,
} // namespace
void ProfileStatisticsAggregator::PasswordStoreConsumerHelper::
OnGetPasswordStoreResults(
std::vector<std::unique_ptr<autofill::PasswordForm>> results) {
parent_->StatisticsCallbackSuccess(
profiles::kProfileStatisticsPasswords, results.size());
}
ProfileStatisticsAggregator::ProfileStatisticsAggregator(
Profile* profile,
const base::Closure& done_callback)
: profile_(profile),
profile_path_(profile_->GetPath()),
done_callback_(done_callback),
password_store_consumer_helper_(this) {}
done_callback_(done_callback) {}
ProfileStatisticsAggregator::~ProfileStatisticsAggregator() {}
......@@ -88,10 +80,9 @@ void ProfileStatisticsAggregator::StartAggregator() {
DCHECK(g_browser_process->profile_manager()->IsValidProfile(profile_));
profile_category_stats_.clear();
// Try to cancel tasks from task trackers.
// Try to cancel tasks.
tracker_.TryCancelAll();
counters_.clear();
password_store_consumer_helper_.cancelable_task_tracker()->TryCancelAll();
// Initiate bookmark counting.
bookmarks::BookmarkModel* bookmark_model =
......@@ -103,27 +94,26 @@ void ProfileStatisticsAggregator::StartAggregator() {
StatisticsCallbackFailure(profiles::kProfileStatisticsBookmarks);
}
// Initiate history counting (async).
// Initiate history counting.
history::HistoryService* history_service =
HistoryServiceFactory::GetForProfileWithoutCreating(profile_);
if (history_service) {
history_service->GetHistoryCount(
base::Time(),
base::Time::Max(),
base::Bind(&ProfileStatisticsAggregator::StatisticsCallbackHistory,
this),
&tracker_);
AddCounter(base::MakeUnique<browsing_data::HistoryCounter>(
history_service,
browsing_data::HistoryCounter::GetUpdatedWebHistoryServiceCallback(),
/*sync_service=*/nullptr));
} else {
StatisticsCallbackFailure(profiles::kProfileStatisticsBrowsingHistory);
}
// Initiate stored password counting (async).
// Initiate stored password counting.
scoped_refptr<password_manager::PasswordStore> password_store =
PasswordStoreFactory::GetForProfile(
profile_, ServiceAccessType::EXPLICIT_ACCESS);
if (password_store) {
password_store->GetAutofillableLogins(&password_store_consumer_helper_);
AddCounter(base::MakeUnique<browsing_data::PasswordsCounter>(
password_store, /*sync_service=*/nullptr));
} else {
StatisticsCallbackFailure(profiles::kProfileStatisticsPasswords);
}
......@@ -141,12 +131,17 @@ void ProfileStatisticsAggregator::OnCounterResult(
std::unique_ptr<BrowsingDataCounter::Result> result) {
if (!result->Finished())
return;
const std::string& pref = result->source()->GetPrefName();
const char* pref_name = result->source()->GetPrefName();
auto* finished_result =
static_cast<BrowsingDataCounter::FinishedResult*>(result.get());
int count = finished_result->Value();
if (pref == browsing_data::BookmarkCounter::kPrefName) {
if (pref_name == browsing_data::BookmarkCounter::kPrefName) {
StatisticsCallbackSuccess(profiles::kProfileStatisticsBookmarks, count);
} else if (pref_name == browsing_data::prefs::kDeleteBrowsingHistory) {
StatisticsCallbackSuccess(profiles::kProfileStatisticsBrowsingHistory,
count);
} else if (pref_name == browsing_data::prefs::kDeletePasswords) {
StatisticsCallbackSuccess(profiles::kProfileStatisticsPasswords, count);
} else {
// TODO(dullweber): Add more cases when the other statistics are replaced.
NOTREACHED();
......@@ -191,15 +186,6 @@ void ProfileStatisticsAggregator::StatisticsCallbackFailure(
StatisticsCallback(category, result);
}
void ProfileStatisticsAggregator::StatisticsCallbackHistory(
history::HistoryCountResult result) {
ProfileStatValue result_converted;
result_converted.count = result.count;
result_converted.success = result.success;
StatisticsCallback(profiles::kProfileStatisticsBrowsingHistory,
result_converted);
}
ProfileStatisticsAggregator::ProfileStatValue
ProfileStatisticsAggregator::CountPrefs() const {
const PrefService* pref_service = profile_->GetPrefs();
......
......@@ -14,11 +14,10 @@
#include "base/callback_forward.h"
#include "base/files/file_path.h"
#include "base/memory/ref_counted.h"
#include "base/task/cancelable_task_tracker.h"
#include "base/task_runner.h"
#include "chrome/browser/profiles/profile_statistics_common.h"
#include "components/browsing_data/core/counters/browsing_data_counter.h"
#include "components/history/core/browser/history_types.h"
#include "components/password_manager/core/browser/password_store_consumer.h"
class Profile;
......@@ -65,31 +64,14 @@ class ProfileStatisticsAggregator
void StatisticsCallbackSuccess(const char* category, int count);
// Callback for reporting failure.
void StatisticsCallbackFailure(const char* category);
// Callback for history.
void StatisticsCallbackHistory(history::HistoryCountResult result);
// Callback for counters.
void OnCounterResult(
std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result);
// Registers and starts a BrowsingDataCounter.
// Registers, initializes and starts a BrowsingDataCounter.
void AddCounter(std::unique_ptr<browsing_data::BrowsingDataCounter> counter);
// Password counting
class PasswordStoreConsumerHelper
: public password_manager::PasswordStoreConsumer {
public:
explicit PasswordStoreConsumerHelper(ProfileStatisticsAggregator* parent)
: parent_(parent) {}
void OnGetPasswordStoreResults(
std::vector<std::unique_ptr<autofill::PasswordForm>> results) override;
private:
ProfileStatisticsAggregator* parent_ = nullptr;
DISALLOW_COPY_AND_ASSIGN(PasswordStoreConsumerHelper);
};
// Preference counting.
ProfileStatValue CountPrefs() const;
......@@ -108,9 +90,6 @@ class ProfileStatisticsAggregator
std::vector<std::unique_ptr<browsing_data::BrowsingDataCounter>> counters_;
// Password counting.
PasswordStoreConsumerHelper password_store_consumer_helper_;
DISALLOW_COPY_AND_ASSIGN(ProfileStatisticsAggregator);
};
......
......@@ -38,7 +38,7 @@ HistoryCounter::~HistoryCounter() {
void HistoryCounter::OnInitialized() {
if (sync_service_)
sync_service_->AddObserver(this);
history_sync_enabled_ = !!web_history_service_callback_.Run();
history_sync_enabled_ = !!GetWebHistoryService();
}
bool HistoryCounter::HasTrackedTasks() {
......@@ -51,6 +51,12 @@ const char* HistoryCounter::GetPrefName() const {
: browsing_data::prefs::kDeleteBrowsingHistory;
}
history::WebHistoryService* HistoryCounter::GetWebHistoryService() {
if (web_history_service_callback_)
return web_history_service_callback_.Run();
return nullptr;
}
void HistoryCounter::Count() {
// Reset the state.
cancelable_task_tracker_.TryCancelAll();
......@@ -69,8 +75,7 @@ void HistoryCounter::Count() {
&cancelable_task_tracker_);
// If the history sync is enabled, test if there is at least one synced item.
history::WebHistoryService* web_history =
web_history_service_callback_.Run();
history::WebHistoryService* web_history = GetWebHistoryService();
if (!web_history) {
web_counting_finished_ = true;
......@@ -155,7 +160,7 @@ void HistoryCounter::MergeResults() {
}
void HistoryCounter::OnStateChanged(syncer::SyncService* sync) {
bool history_sync_enabled_new_state = !!web_history_service_callback_.Run();
bool history_sync_enabled_new_state = !!GetWebHistoryService();
// If the history sync was just enabled or disabled, restart the counter
// so that we update the result accordingly.
......
......@@ -58,6 +58,8 @@ class HistoryCounter : public browsing_data::BrowsingDataCounter,
void OnWebHistoryTimeout();
void MergeResults();
history::WebHistoryService* GetWebHistoryService();
// SyncServiceObserver implementation.
void OnStateChanged(syncer::SyncService* sync) override;
......
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