| 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 "chrome/browser/browsing_data/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" | 12 #include "chrome/browser/history/history_service_factory.h" |
| 13 #include "chrome/browser/history/web_history_service_factory.h" | 13 #include "chrome/browser/history/web_history_service_factory.h" |
| 14 #include "chrome/browser/sync/profile_sync_service_factory.h" | 14 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 15 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
| 16 #include "components/browser_sync/browser/profile_sync_service.h" | 16 #include "components/browser_sync/browser/profile_sync_service.h" |
| 17 #include "components/history/core/browser/history_service.h" | 17 #include "components/history/core/browser/history_service.h" |
| 18 #include "components/history/core/browser/web_history_service.h" | 18 #include "components/history/core/browser/web_history_service.h" |
| 19 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 static const int64_t kWebHistoryTimeoutSeconds = 10; | 22 static const int64_t kWebHistoryTimeoutSeconds = 10; |
| 23 } | 23 } |
| 24 | 24 |
| 25 HistoryCounter::HistoryCounter() : pref_name_(prefs::kDeleteBrowsingHistory), | 25 HistoryCounter::HistoryCounter(Profile* profile) |
| 26 has_synced_visits_(false), | 26 : BrowsingDataCounter(prefs::kDeleteBrowsingHistory), |
| 27 local_counting_finished_(false), | 27 profile_(profile), |
| 28 web_counting_finished_(false), | 28 has_synced_visits_(false), |
| 29 testing_web_history_service_(nullptr), | 29 local_counting_finished_(false), |
| 30 sync_service_(nullptr), | 30 web_counting_finished_(false), |
| 31 history_sync_enabled_(false), | 31 testing_web_history_service_(nullptr), |
| 32 weak_ptr_factory_(this) { | 32 sync_service_(nullptr), |
| 33 } | 33 history_sync_enabled_(false), |
| 34 weak_ptr_factory_(this) {} |
| 34 | 35 |
| 35 HistoryCounter::~HistoryCounter() { | 36 HistoryCounter::~HistoryCounter() { |
| 36 if (sync_service_) | 37 if (sync_service_) |
| 37 sync_service_->RemoveObserver(this); | 38 sync_service_->RemoveObserver(this); |
| 38 } | 39 } |
| 39 | 40 |
| 40 void HistoryCounter::OnInitialized() { | 41 void HistoryCounter::OnInitialized() { |
| 41 sync_service_ = ProfileSyncServiceFactory::GetForProfile(GetProfile()); | 42 sync_service_ = ProfileSyncServiceFactory::GetForProfile(profile_); |
| 42 if (sync_service_) | 43 if (sync_service_) |
| 43 sync_service_->AddObserver(this); | 44 sync_service_->AddObserver(this); |
| 44 history_sync_enabled_ = | 45 history_sync_enabled_ = !!WebHistoryServiceFactory::GetForProfile(profile_); |
| 45 !!WebHistoryServiceFactory::GetForProfile(GetProfile()); | |
| 46 } | |
| 47 | |
| 48 const std::string& HistoryCounter::GetPrefName() const { | |
| 49 return pref_name_; | |
| 50 } | 46 } |
| 51 | 47 |
| 52 bool HistoryCounter::HasTrackedTasks() { | 48 bool HistoryCounter::HasTrackedTasks() { |
| 53 return cancelable_task_tracker_.HasTrackedTasks(); | 49 return cancelable_task_tracker_.HasTrackedTasks(); |
| 54 } | 50 } |
| 55 | 51 |
| 56 void HistoryCounter::SetWebHistoryServiceForTesting( | 52 void HistoryCounter::SetWebHistoryServiceForTesting( |
| 57 history::WebHistoryService* service) { | 53 history::WebHistoryService* service) { |
| 58 testing_web_history_service_ = service; | 54 testing_web_history_service_ = service; |
| 59 } | 55 } |
| 60 | 56 |
| 61 void HistoryCounter::Count() { | 57 void HistoryCounter::Count() { |
| 62 // Reset the state. | 58 // Reset the state. |
| 63 cancelable_task_tracker_.TryCancelAll(); | 59 cancelable_task_tracker_.TryCancelAll(); |
| 64 web_history_request_.reset(); | 60 web_history_request_.reset(); |
| 65 has_synced_visits_ = false; | 61 has_synced_visits_ = false; |
| 66 | 62 |
| 67 // Count the locally stored items. | 63 // Count the locally stored items. |
| 68 local_counting_finished_ = false; | 64 local_counting_finished_ = false; |
| 69 | 65 |
| 70 history::HistoryService* service = | 66 history::HistoryService* service = HistoryServiceFactory::GetForProfile( |
| 71 HistoryServiceFactory::GetForProfile( | 67 profile_, ServiceAccessType::EXPLICIT_ACCESS); |
| 72 GetProfile(), ServiceAccessType::EXPLICIT_ACCESS); | |
| 73 | 68 |
| 74 service->GetHistoryCount( | 69 service->GetHistoryCount( |
| 75 GetPeriodStart(), | 70 GetPeriodStart(), |
| 76 base::Time::Max(), | 71 base::Time::Max(), |
| 77 base::Bind(&HistoryCounter::OnGetLocalHistoryCount, | 72 base::Bind(&HistoryCounter::OnGetLocalHistoryCount, |
| 78 weak_ptr_factory_.GetWeakPtr()), | 73 weak_ptr_factory_.GetWeakPtr()), |
| 79 &cancelable_task_tracker_); | 74 &cancelable_task_tracker_); |
| 80 | 75 |
| 81 // If the history sync is enabled, test if there is at least one synced item. | 76 // If the history sync is enabled, test if there is at least one synced item. |
| 82 // If the testing web history service is present, use that one instead. | 77 // If the testing web history service is present, use that one instead. |
| 83 history::WebHistoryService* web_history = testing_web_history_service_ | 78 history::WebHistoryService* web_history = |
| 84 ? testing_web_history_service_ | 79 testing_web_history_service_ |
| 85 : WebHistoryServiceFactory::GetForProfile(GetProfile()); | 80 ? testing_web_history_service_ |
| 81 : WebHistoryServiceFactory::GetForProfile(profile_); |
| 86 | 82 |
| 87 if (!web_history) { | 83 if (!web_history) { |
| 88 web_counting_finished_ = true; | 84 web_counting_finished_ = true; |
| 89 return; | 85 return; |
| 90 } | 86 } |
| 91 | 87 |
| 92 web_counting_finished_ = false; | 88 web_counting_finished_ = false; |
| 93 | 89 |
| 94 web_history_timeout_.Start( | 90 web_history_timeout_.Start( |
| 95 FROM_HERE, | 91 FROM_HERE, |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 bool has_synced_visits) | 173 bool has_synced_visits) |
| 178 : FinishedResult(source, value), | 174 : FinishedResult(source, value), |
| 179 has_synced_visits_(has_synced_visits) { | 175 has_synced_visits_(has_synced_visits) { |
| 180 } | 176 } |
| 181 | 177 |
| 182 HistoryCounter::HistoryResult::~HistoryResult() { | 178 HistoryCounter::HistoryResult::~HistoryResult() { |
| 183 } | 179 } |
| 184 | 180 |
| 185 void HistoryCounter::OnStateChanged() { | 181 void HistoryCounter::OnStateChanged() { |
| 186 bool history_sync_enabled_new_state = | 182 bool history_sync_enabled_new_state = |
| 187 !!WebHistoryServiceFactory::GetForProfile(GetProfile()); | 183 !!WebHistoryServiceFactory::GetForProfile(profile_); |
| 188 | 184 |
| 189 // If the history sync was just enabled or disabled, restart the counter | 185 // If the history sync was just enabled or disabled, restart the counter |
| 190 // so that we update the result accordingly. | 186 // so that we update the result accordingly. |
| 191 if (history_sync_enabled_ != history_sync_enabled_new_state) { | 187 if (history_sync_enabled_ != history_sync_enabled_new_state) { |
| 192 history_sync_enabled_ = history_sync_enabled_new_state; | 188 history_sync_enabled_ = history_sync_enabled_new_state; |
| 193 Restart(); | 189 Restart(); |
| 194 } | 190 } |
| 195 } | 191 } |
| OLD | NEW |