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