blob: 3c0209691f66ae98529f20172ad4193ebacf9b3c [file] [log] [blame]
msramek8be33282015-10-06 14:33:211// 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
3// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_BROWSING_DATA_HISTORY_COUNTER_H_
6#define CHROME_BROWSER_BROWSING_DATA_HISTORY_COUNTER_H_
7
msrameke63c7822015-11-20 15:49:418#include "base/memory/weak_ptr.h"
msramek8be33282015-10-06 14:33:219#include "base/task/cancelable_task_tracker.h"
msramekae197fb2015-10-29 21:14:1910#include "base/timer/timer.h"
msramek8be33282015-10-06 14:33:2111#include "chrome/browser/browsing_data/browsing_data_counter.h"
12#include "components/history/core/browser/history_service.h"
msramekae197fb2015-10-29 21:14:1913#include "components/history/core/browser/web_history_service.h"
msramek5d06f7c2015-11-09 17:58:1514#include "components/sync_driver/sync_service_observer.h"
msramek8be33282015-10-06 14:33:2115
msramek5d06f7c2015-11-09 17:58:1516class ProfileSyncService;
17
18class HistoryCounter: public BrowsingDataCounter,
19 public sync_driver::SyncServiceObserver {
msramek8be33282015-10-06 14:33:2120 public:
msramek6bb4dffa2015-11-03 18:20:5421 class HistoryResult : public FinishedResult {
22 public:
23 HistoryResult(const HistoryCounter* source,
24 ResultInt value,
25 bool has_synced_visits);
26 ~HistoryResult() override;
27
28 bool has_synced_visits() const { return has_synced_visits_; }
29
30 private:
31 bool has_synced_visits_;
32 };
msramekae197fb2015-10-29 21:14:1933
msramek8be33282015-10-06 14:33:2134 HistoryCounter();
35 ~HistoryCounter() override;
36
msramek5d06f7c2015-11-09 17:58:1537 void OnInitialized() override;
msramek8be33282015-10-06 14:33:2138 const std::string& GetPrefName() const override;
39
40 // Whether there are counting tasks in progress. Only used for testing.
41 bool HasTrackedTasks();
42
msramekae197fb2015-10-29 21:14:1943 // Make the history counter use a custom WebHistoryService instance. Only
44 // used for testing.
45 void SetWebHistoryServiceForTesting(history::WebHistoryService* service);
46
msramek8be33282015-10-06 14:33:2147 private:
48 const std::string pref_name_;
msramekae197fb2015-10-29 21:14:1949
50 BrowsingDataCounter::ResultInt local_result_;
51 bool has_synced_visits_;
52
53 bool local_counting_finished_;
54 bool web_counting_finished_;
55
56 history::WebHistoryService* testing_web_history_service_;
57
msramek8be33282015-10-06 14:33:2158 base::CancelableTaskTracker cancelable_task_tracker_;
msramekae197fb2015-10-29 21:14:1959 scoped_ptr<history::WebHistoryService::Request> web_history_request_;
60 base::OneShotTimer web_history_timeout_;
61
62 base::ThreadChecker thread_checker_;
msramek8be33282015-10-06 14:33:2163
msramek5d06f7c2015-11-09 17:58:1564 ProfileSyncService* sync_service_;
65 bool history_sync_enabled_;
66
msrameke63c7822015-11-20 15:49:4167 base::WeakPtrFactory<HistoryCounter> weak_ptr_factory_;
68
msramek8be33282015-10-06 14:33:2169 void Count() override;
70
71 void OnGetLocalHistoryCount(history::HistoryCountResult result);
msramekae197fb2015-10-29 21:14:1972 void OnGetWebHistoryCount(history::WebHistoryService::Request* request,
73 const base::DictionaryValue* result);
74 void OnWebHistoryTimeout();
75 void MergeResults();
msramek5d06f7c2015-11-09 17:58:1576
77 // SyncServiceObserver implementation.
78 void OnStateChanged() override;
msramek8be33282015-10-06 14:33:2179};
80
81#endif // CHROME_BROWSER_BROWSING_DATA_HISTORY_COUNTER_H_