OLD | NEW |
---|---|
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" | 12 #include "components/browser_sync/browser/profile_sync_service.h" |
17 #include "components/browsing_data/core/pref_names.h" | 13 #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 | 14 |
22 namespace { | 15 namespace { |
23 static const int64_t kWebHistoryTimeoutSeconds = 10; | 16 static const int64_t kWebHistoryTimeoutSeconds = 10; |
24 } | 17 } |
25 | 18 |
26 HistoryCounter::HistoryCounter(Profile* profile) | 19 namespace browsing_data { |
27 : BrowsingDataCounter(browsing_data::prefs::kDeleteBrowsingHistory), | 20 |
28 profile_(profile), | 21 HistoryCounter::HistoryCounter( |
22 ProfileSyncService* profile_sync_service, | |
msramek
2016/07/20 13:41:16
nit: The ordering here is strange. In the order of
ioanap
2016/07/20 17:50:36
Done.
| |
23 const GetUpdatedWebHistoryServiceCallback& callback, | |
24 history::HistoryService* history_service) | |
25 : pref_name_(browsing_data::prefs::kDeleteBrowsingHistory), | |
26 profile_sync_service_(profile_sync_service), | |
27 web_history_service_callback_(callback), | |
28 history_service_(history_service), | |
29 has_synced_visits_(false), | 29 has_synced_visits_(false), |
30 local_counting_finished_(false), | 30 local_counting_finished_(false), |
31 web_counting_finished_(false), | 31 web_counting_finished_(false), |
32 testing_web_history_service_(nullptr), | |
33 sync_service_(nullptr), | |
34 history_sync_enabled_(false), | 32 history_sync_enabled_(false), |
35 weak_ptr_factory_(this) {} | 33 weak_ptr_factory_(this) {} |
36 | 34 |
37 HistoryCounter::~HistoryCounter() { | 35 HistoryCounter::~HistoryCounter() { |
38 if (sync_service_) | 36 if (profile_sync_service_) |
msramek
2016/07/20 13:41:16
This is an unnecessary change, since we don't need
ioanap
2016/07/20 17:50:36
Done.
| |
39 sync_service_->RemoveObserver(this); | 37 profile_sync_service_->RemoveObserver(this); |
40 } | 38 } |
41 | 39 |
42 void HistoryCounter::OnInitialized() { | 40 void HistoryCounter::OnInitialized() { |
43 sync_service_ = ProfileSyncServiceFactory::GetForProfile(profile_); | 41 if (profile_sync_service_) |
44 if (sync_service_) | 42 profile_sync_service_->AddObserver(this); |
45 sync_service_->AddObserver(this); | 43 history_sync_enabled_ = IsHistorySyncEnabled(); |
46 history_sync_enabled_ = !!WebHistoryServiceFactory::GetForProfile(profile_); | |
47 } | 44 } |
48 | 45 |
49 bool HistoryCounter::HasTrackedTasks() { | 46 bool HistoryCounter::HasTrackedTasks() { |
50 return cancelable_task_tracker_.HasTrackedTasks(); | 47 return cancelable_task_tracker_.HasTrackedTasks(); |
51 } | 48 } |
52 | 49 |
53 void HistoryCounter::SetWebHistoryServiceForTesting( | 50 const std::string& HistoryCounter::GetPrefName() const { |
54 history::WebHistoryService* service) { | 51 return pref_name_; |
55 testing_web_history_service_ = service; | 52 } |
53 | |
54 bool HistoryCounter::IsHistorySyncEnabled() { | |
55 return profile_sync_service_ && profile_sync_service_->IsSyncActive() && | |
msramek
2016/07/20 13:41:16
The semantics here are that history sync is enable
ioanap
2016/07/20 17:50:37
I see... I added this before I had the callback in
| |
56 profile_sync_service_->GetActiveDataTypes().Has( | |
57 syncer::HISTORY_DELETE_DIRECTIVES); | |
56 } | 58 } |
57 | 59 |
58 void HistoryCounter::Count() { | 60 void HistoryCounter::Count() { |
59 // Reset the state. | 61 // Reset the state. |
60 cancelable_task_tracker_.TryCancelAll(); | 62 cancelable_task_tracker_.TryCancelAll(); |
61 web_history_request_.reset(); | 63 web_history_request_.reset(); |
62 has_synced_visits_ = false; | 64 has_synced_visits_ = false; |
63 | 65 |
64 // Count the locally stored items. | 66 // Count the locally stored items. |
65 local_counting_finished_ = false; | 67 local_counting_finished_ = false; |
66 | 68 |
67 history::HistoryService* service = HistoryServiceFactory::GetForProfile( | 69 history_service_->GetHistoryCount( |
68 profile_, ServiceAccessType::EXPLICIT_ACCESS); | 70 GetPeriodStart(), base::Time::Max(), |
69 | |
70 service->GetHistoryCount( | |
71 GetPeriodStart(), | |
72 base::Time::Max(), | |
73 base::Bind(&HistoryCounter::OnGetLocalHistoryCount, | 71 base::Bind(&HistoryCounter::OnGetLocalHistoryCount, |
74 weak_ptr_factory_.GetWeakPtr()), | 72 weak_ptr_factory_.GetWeakPtr()), |
75 &cancelable_task_tracker_); | 73 &cancelable_task_tracker_); |
76 | 74 |
77 // If the history sync is enabled, test if there is at least one synced item. | 75 // 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. | 76 history::WebHistoryService* web_history = web_history_service_callback_.Run(); |
79 history::WebHistoryService* web_history = | |
80 testing_web_history_service_ | |
81 ? testing_web_history_service_ | |
82 : WebHistoryServiceFactory::GetForProfile(profile_); | |
83 | 77 |
84 if (!web_history) { | 78 if (!web_history) { |
85 web_counting_finished_ = true; | 79 web_counting_finished_ = true; |
86 return; | 80 return; |
87 } | 81 } |
88 | 82 |
89 web_counting_finished_ = false; | 83 web_counting_finished_ = false; |
90 | 84 |
91 web_history_timeout_.Start( | 85 web_history_timeout_.Start( |
92 FROM_HERE, | 86 FROM_HERE, base::TimeDelta::FromSeconds(kWebHistoryTimeoutSeconds), this, |
93 base::TimeDelta::FromSeconds(kWebHistoryTimeoutSeconds), | |
94 this, | |
95 &HistoryCounter::OnWebHistoryTimeout); | 87 &HistoryCounter::OnWebHistoryTimeout); |
96 | 88 |
97 history::QueryOptions options; | 89 history::QueryOptions options; |
98 options.max_count = 1; | 90 options.max_count = 1; |
99 options.begin_time = GetPeriodStart(); | 91 options.begin_time = GetPeriodStart(); |
100 options.end_time = base::Time::Max(); | 92 options.end_time = base::Time::Max(); |
101 web_history_request_ = web_history->QueryHistory( | 93 web_history_request_ = web_history->QueryHistory( |
102 base::string16(), | 94 base::string16(), options, |
103 options, | |
104 base::Bind(&HistoryCounter::OnGetWebHistoryCount, | 95 base::Bind(&HistoryCounter::OnGetWebHistoryCount, |
105 weak_ptr_factory_.GetWeakPtr())); | 96 weak_ptr_factory_.GetWeakPtr())); |
106 | 97 |
107 // TODO(msramek): Include web history count when there is an API for it. | 98 // TODO(msramek): Include web history count when there is an API for it. |
108 } | 99 } |
109 | 100 |
110 void HistoryCounter::OnGetLocalHistoryCount( | 101 void HistoryCounter::OnGetLocalHistoryCount( |
111 history::HistoryCountResult result) { | 102 history::HistoryCountResult result) { |
112 // Ensure that all callbacks are on the same thread, so that we do not need | 103 // Ensure that all callbacks are on the same thread, so that we do not need |
113 // a mutex for |MergeResults|. | 104 // a mutex for |MergeResults|. |
(...skipping 20 matching lines...) Expand all Loading... | |
134 if (!web_history_timeout_.IsRunning()) | 125 if (!web_history_timeout_.IsRunning()) |
135 return; | 126 return; |
136 | 127 |
137 web_history_timeout_.Stop(); | 128 web_history_timeout_.Stop(); |
138 | 129 |
139 // If the query failed, err on the safe side and inform the user that they | 130 // 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 | 131 // may have history items stored in Sync. Otherwise, we expect at least one |
141 // entry in the "event" list. | 132 // entry in the "event" list. |
142 const base::ListValue* events; | 133 const base::ListValue* events; |
143 has_synced_visits_ = | 134 has_synced_visits_ = |
144 !result || | 135 !result || (result->GetList("event", &events) && !events->empty()); |
145 (result->GetList("event", &events) && !events->empty()); | |
146 web_counting_finished_ = true; | 136 web_counting_finished_ = true; |
147 MergeResults(); | 137 MergeResults(); |
148 } | 138 } |
149 | 139 |
150 void HistoryCounter::OnWebHistoryTimeout() { | 140 void HistoryCounter::OnWebHistoryTimeout() { |
151 // Ensure that all callbacks are on the same thread, so that we do not need | 141 // Ensure that all callbacks are on the same thread, so that we do not need |
152 // a mutex for |MergeResults|. | 142 // a mutex for |MergeResults|. |
153 DCHECK(thread_checker_.CalledOnValidThread()); | 143 DCHECK(thread_checker_.CalledOnValidThread()); |
154 | 144 |
155 // If the query timed out, err on the safe side and inform the user that they | 145 // If the query timed out, err on the safe side and inform the user that they |
156 // may have history items stored in Sync. | 146 // may have history items stored in Sync. |
157 web_history_request_.reset(); | 147 web_history_request_.reset(); |
158 has_synced_visits_ = true; | 148 has_synced_visits_ = true; |
159 web_counting_finished_ = true; | 149 web_counting_finished_ = true; |
160 MergeResults(); | 150 MergeResults(); |
161 } | 151 } |
162 | 152 |
163 void HistoryCounter::MergeResults() { | 153 void HistoryCounter::MergeResults() { |
164 if (!local_counting_finished_ || !web_counting_finished_) | 154 if (!local_counting_finished_ || !web_counting_finished_) |
165 return; | 155 return; |
166 | 156 |
167 ReportResult(base::WrapUnique( | 157 ReportResult(base::WrapUnique( |
168 new HistoryResult(this, local_result_, has_synced_visits_))); | 158 new HistoryResult(this, local_result_, has_synced_visits_))); |
169 } | 159 } |
170 | 160 |
171 HistoryCounter::HistoryResult::HistoryResult( | 161 HistoryCounter::HistoryResult::HistoryResult(const HistoryCounter* source, |
172 const HistoryCounter* source, | 162 ResultInt value, |
173 ResultInt value, | 163 bool has_synced_visits) |
174 bool has_synced_visits) | 164 : FinishedResult(source, value), has_synced_visits_(has_synced_visits) {} |
175 : FinishedResult(source, value), | |
176 has_synced_visits_(has_synced_visits) { | |
177 } | |
178 | 165 |
179 HistoryCounter::HistoryResult::~HistoryResult() { | 166 HistoryCounter::HistoryResult::~HistoryResult() {} |
180 } | |
181 | 167 |
182 void HistoryCounter::OnStateChanged() { | 168 void HistoryCounter::OnStateChanged() { |
183 bool history_sync_enabled_new_state = | 169 bool history_sync_enabled_new_state = IsHistorySyncEnabled(); |
184 !!WebHistoryServiceFactory::GetForProfile(profile_); | |
185 | 170 |
186 // If the history sync was just enabled or disabled, restart the counter | 171 // If the history sync was just enabled or disabled, restart the counter |
187 // so that we update the result accordingly. | 172 // so that we update the result accordingly. |
188 if (history_sync_enabled_ != history_sync_enabled_new_state) { | 173 if (history_sync_enabled_ != history_sync_enabled_new_state) { |
189 history_sync_enabled_ = history_sync_enabled_new_state; | 174 history_sync_enabled_ = history_sync_enabled_new_state; |
190 Restart(); | 175 Restart(); |
191 } | 176 } |
192 } | 177 } |
178 | |
179 } // namespace browsing_data | |
OLD | NEW |