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