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