Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(329)

Side by Side Diff: components/browsing_data/core/counters/history_counter.cc

Issue 2153863002: Move counters for passwords, history and autofill to components (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@separate_build_targets_in_components_bd
Patch Set: Addressed comments Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2015 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/browsing_data/history_counter.h" 5 #include "components/browsing_data/core/counters/history_counter.h"
6 6
7 #include <limits.h> 7 #include <limits.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
11 #include "base/timer/timer.h" 11 #include "base/timer/timer.h"
12 #include "chrome/browser/history/history_service_factory.h"
13 #include "chrome/browser/history/web_history_service_factory.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/sync/profile_sync_service_factory.h"
16 #include "components/browser_sync/browser/profile_sync_service.h"
17 #include "components/browsing_data/core/pref_names.h" 12 #include "components/browsing_data/core/pref_names.h"
18 #include "components/history/core/browser/history_service.h"
19 #include "components/history/core/browser/web_history_service.h"
20 #include "content/public/browser/browser_thread.h"
21 13
22 namespace { 14 namespace {
23 static const int64_t kWebHistoryTimeoutSeconds = 10; 15 static const int64_t kWebHistoryTimeoutSeconds = 10;
24 } 16 }
25 17
26 HistoryCounter::HistoryCounter(Profile* profile) 18 namespace browsing_data {
27 : BrowsingDataCounter(browsing_data::prefs::kDeleteBrowsingHistory), 19
28 profile_(profile), 20 HistoryCounter::HistoryCounter(
21 history::HistoryService* history_service,
22 const GetUpdatedWebHistoryServiceCallback& callback,
23 sync_driver::SyncService* sync_service)
24 : history_service_(history_service),
25 web_history_service_callback_(callback),
26 sync_service_(sync_service),
29 has_synced_visits_(false), 27 has_synced_visits_(false),
30 local_counting_finished_(false), 28 local_counting_finished_(false),
31 web_counting_finished_(false), 29 web_counting_finished_(false),
32 testing_web_history_service_(nullptr),
33 sync_service_(nullptr),
34 history_sync_enabled_(false), 30 history_sync_enabled_(false),
35 weak_ptr_factory_(this) {} 31 weak_ptr_factory_(this) {}
36 32
37 HistoryCounter::~HistoryCounter() { 33 HistoryCounter::~HistoryCounter() {
38 if (sync_service_) 34 if (sync_service_)
39 sync_service_->RemoveObserver(this); 35 sync_service_->RemoveObserver(this);
40 } 36 }
41 37
42 void HistoryCounter::OnInitialized() { 38 void HistoryCounter::OnInitialized() {
43 sync_service_ = ProfileSyncServiceFactory::GetForProfile(profile_);
44 if (sync_service_) 39 if (sync_service_)
45 sync_service_->AddObserver(this); 40 sync_service_->AddObserver(this);
46 history_sync_enabled_ = !!WebHistoryServiceFactory::GetForProfile(profile_); 41 history_sync_enabled_ = !!web_history_service_callback_.Run();
47 } 42 }
48 43
49 bool HistoryCounter::HasTrackedTasks() { 44 bool HistoryCounter::HasTrackedTasks() {
50 return cancelable_task_tracker_.HasTrackedTasks(); 45 return cancelable_task_tracker_.HasTrackedTasks();
51 } 46 }
52 47
53 void HistoryCounter::SetWebHistoryServiceForTesting( 48 const char* HistoryCounter::GetPrefName() const {
54 history::WebHistoryService* service) { 49 return browsing_data::prefs::kDeleteBrowsingHistory;
55 testing_web_history_service_ = service;
56 } 50 }
57 51
58 void HistoryCounter::Count() { 52 void HistoryCounter::Count() {
59 // Reset the state. 53 // Reset the state.
60 cancelable_task_tracker_.TryCancelAll(); 54 cancelable_task_tracker_.TryCancelAll();
61 web_history_request_.reset(); 55 web_history_request_.reset();
62 has_synced_visits_ = false; 56 has_synced_visits_ = false;
63 57
64 // Count the locally stored items. 58 // Count the locally stored items.
65 local_counting_finished_ = false; 59 local_counting_finished_ = false;
66 60
67 history::HistoryService* service = HistoryServiceFactory::GetForProfile( 61 history_service_->GetHistoryCount(
68 profile_, ServiceAccessType::EXPLICIT_ACCESS); 62 GetPeriodStart(), base::Time::Max(),
69
70 service->GetHistoryCount(
71 GetPeriodStart(),
72 base::Time::Max(),
73 base::Bind(&HistoryCounter::OnGetLocalHistoryCount, 63 base::Bind(&HistoryCounter::OnGetLocalHistoryCount,
74 weak_ptr_factory_.GetWeakPtr()), 64 weak_ptr_factory_.GetWeakPtr()),
75 &cancelable_task_tracker_); 65 &cancelable_task_tracker_);
76 66
77 // If the history sync is enabled, test if there is at least one synced item. 67 // If the history sync is enabled, test if there is at least one synced item.
78 // If the testing web history service is present, use that one instead.
79 history::WebHistoryService* web_history = 68 history::WebHistoryService* web_history =
80 testing_web_history_service_ 69 web_history_service_callback_.Run();
81 ? testing_web_history_service_
82 : WebHistoryServiceFactory::GetForProfile(profile_);
83 70
84 if (!web_history) { 71 if (!web_history) {
85 web_counting_finished_ = true; 72 web_counting_finished_ = true;
86 return; 73 return;
87 } 74 }
88 75
89 web_counting_finished_ = false; 76 web_counting_finished_ = false;
90 77
91 web_history_timeout_.Start( 78 web_history_timeout_.Start(
92 FROM_HERE, 79 FROM_HERE, base::TimeDelta::FromSeconds(kWebHistoryTimeoutSeconds), this,
93 base::TimeDelta::FromSeconds(kWebHistoryTimeoutSeconds),
94 this,
95 &HistoryCounter::OnWebHistoryTimeout); 80 &HistoryCounter::OnWebHistoryTimeout);
96 81
97 history::QueryOptions options; 82 history::QueryOptions options;
98 options.max_count = 1; 83 options.max_count = 1;
99 options.begin_time = GetPeriodStart(); 84 options.begin_time = GetPeriodStart();
100 options.end_time = base::Time::Max(); 85 options.end_time = base::Time::Max();
101 web_history_request_ = web_history->QueryHistory( 86 web_history_request_ = web_history->QueryHistory(
102 base::string16(), 87 base::string16(), options,
103 options,
104 base::Bind(&HistoryCounter::OnGetWebHistoryCount, 88 base::Bind(&HistoryCounter::OnGetWebHistoryCount,
105 weak_ptr_factory_.GetWeakPtr())); 89 weak_ptr_factory_.GetWeakPtr()));
106 90
107 // TODO(msramek): Include web history count when there is an API for it. 91 // TODO(msramek): Include web history count when there is an API for it.
108 } 92 }
109 93
110 void HistoryCounter::OnGetLocalHistoryCount( 94 void HistoryCounter::OnGetLocalHistoryCount(
111 history::HistoryCountResult result) { 95 history::HistoryCountResult result) {
112 // Ensure that all callbacks are on the same thread, so that we do not need 96 // Ensure that all callbacks are on the same thread, so that we do not need
113 // a mutex for |MergeResults|. 97 // a mutex for |MergeResults|.
(...skipping 20 matching lines...) Expand all
134 if (!web_history_timeout_.IsRunning()) 118 if (!web_history_timeout_.IsRunning())
135 return; 119 return;
136 120
137 web_history_timeout_.Stop(); 121 web_history_timeout_.Stop();
138 122
139 // If the query failed, err on the safe side and inform the user that they 123 // If the query failed, err on the safe side and inform the user that they
140 // may have history items stored in Sync. Otherwise, we expect at least one 124 // may have history items stored in Sync. Otherwise, we expect at least one
141 // entry in the "event" list. 125 // entry in the "event" list.
142 const base::ListValue* events; 126 const base::ListValue* events;
143 has_synced_visits_ = 127 has_synced_visits_ =
144 !result || 128 !result || (result->GetList("event", &events) && !events->empty());
145 (result->GetList("event", &events) && !events->empty());
146 web_counting_finished_ = true; 129 web_counting_finished_ = true;
147 MergeResults(); 130 MergeResults();
148 } 131 }
149 132
150 void HistoryCounter::OnWebHistoryTimeout() { 133 void HistoryCounter::OnWebHistoryTimeout() {
151 // Ensure that all callbacks are on the same thread, so that we do not need 134 // Ensure that all callbacks are on the same thread, so that we do not need
152 // a mutex for |MergeResults|. 135 // a mutex for |MergeResults|.
153 DCHECK(thread_checker_.CalledOnValidThread()); 136 DCHECK(thread_checker_.CalledOnValidThread());
154 137
155 // If the query timed out, err on the safe side and inform the user that they 138 // If the query timed out, err on the safe side and inform the user that they
156 // may have history items stored in Sync. 139 // may have history items stored in Sync.
157 web_history_request_.reset(); 140 web_history_request_.reset();
158 has_synced_visits_ = true; 141 has_synced_visits_ = true;
159 web_counting_finished_ = true; 142 web_counting_finished_ = true;
160 MergeResults(); 143 MergeResults();
161 } 144 }
162 145
163 void HistoryCounter::MergeResults() { 146 void HistoryCounter::MergeResults() {
164 if (!local_counting_finished_ || !web_counting_finished_) 147 if (!local_counting_finished_ || !web_counting_finished_)
165 return; 148 return;
166 149
167 ReportResult(base::WrapUnique( 150 ReportResult(base::WrapUnique(
168 new HistoryResult(this, local_result_, has_synced_visits_))); 151 new HistoryResult(this, local_result_, has_synced_visits_)));
169 } 152 }
170 153
171 HistoryCounter::HistoryResult::HistoryResult( 154 HistoryCounter::HistoryResult::HistoryResult(const HistoryCounter* source,
172 const HistoryCounter* source, 155 ResultInt value,
173 ResultInt value, 156 bool has_synced_visits)
174 bool has_synced_visits) 157 : FinishedResult(source, value), has_synced_visits_(has_synced_visits) {}
175 : FinishedResult(source, value),
176 has_synced_visits_(has_synced_visits) {
177 }
178 158
179 HistoryCounter::HistoryResult::~HistoryResult() { 159 HistoryCounter::HistoryResult::~HistoryResult() {}
180 }
181 160
182 void HistoryCounter::OnStateChanged() { 161 void HistoryCounter::OnStateChanged() {
183 bool history_sync_enabled_new_state = 162 bool history_sync_enabled_new_state = !!web_history_service_callback_.Run();
184 !!WebHistoryServiceFactory::GetForProfile(profile_);
185 163
186 // If the history sync was just enabled or disabled, restart the counter 164 // If the history sync was just enabled or disabled, restart the counter
187 // so that we update the result accordingly. 165 // so that we update the result accordingly.
188 if (history_sync_enabled_ != history_sync_enabled_new_state) { 166 if (history_sync_enabled_ != history_sync_enabled_new_state) {
189 history_sync_enabled_ = history_sync_enabled_new_state; 167 history_sync_enabled_ = history_sync_enabled_new_state;
190 Restart(); 168 Restart();
191 } 169 }
192 } 170 }
171
172 } // namespace browsing_data
OLDNEW
« no previous file with comments | « components/browsing_data/core/counters/history_counter.h ('k') | components/browsing_data/core/counters/passwords_counter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698