| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #ifndef CHROME_BROWSER_PROFILES_PROFILE_STATISTICS_AGGREGATOR_H_ | 5 #ifndef CHROME_BROWSER_PROFILES_PROFILE_STATISTICS_AGGREGATOR_H_ |
| 6 #define CHROME_BROWSER_PROFILES_PROFILE_STATISTICS_AGGREGATOR_H_ | 6 #define CHROME_BROWSER_PROFILES_PROFILE_STATISTICS_AGGREGATOR_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | |
| 9 | |
| 10 #include <memory> | 8 #include <memory> |
| 11 #include <set> | |
| 12 #include <vector> | 9 #include <vector> |
| 13 | 10 |
| 14 #include "base/callback_forward.h" | 11 #include "base/callback_forward.h" |
| 15 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 16 #include "base/memory/ref_counted.h" | |
| 17 #include "base/task/cancelable_task_tracker.h" | |
| 18 #include "base/task_runner.h" | |
| 19 #include "chrome/browser/profiles/profile_statistics_common.h" | 13 #include "chrome/browser/profiles/profile_statistics_common.h" |
| 20 #include "components/browsing_data/core/counters/browsing_data_counter.h" | 14 #include "components/browsing_data/core/counters/browsing_data_counter.h" |
| 21 | 15 |
| 22 class Profile; | 16 class Profile; |
| 23 | 17 |
| 24 class ProfileStatisticsAggregator | 18 class ProfileStatisticsAggregator { |
| 25 : public base::RefCountedThreadSafe<ProfileStatisticsAggregator> { | 19 // This class is used internally by ProfileStatistics |
| 26 // This class is used internally by GetProfileStatistics and | |
| 27 // StoreProfileStatisticsToCache. | |
| 28 // | 20 // |
| 29 // The class collects statistical information about the profile and returns | 21 // The class collects statistical information about the profile and returns |
| 30 // the information via a callback function. Currently bookmarks, history, | 22 // the information via a callback function. Currently bookmarks, history, |
| 31 // logins and preferences are counted. | 23 // logins and sites with autofill forms are counted. |
| 32 // | |
| 33 // The class is RefCounted because this is needed for CancelableTaskTracker | |
| 34 // to function properly. | |
| 35 | 24 |
| 36 public: | 25 public: |
| 37 ProfileStatisticsAggregator(Profile* profile, | 26 ProfileStatisticsAggregator(Profile* profile, |
| 38 const base::Closure& done_callback); | 27 const base::Closure& done_callback); |
| 39 | 28 |
| 29 ~ProfileStatisticsAggregator(); |
| 30 |
| 40 void AddCallbackAndStartAggregator( | 31 void AddCallbackAndStartAggregator( |
| 41 const profiles::ProfileStatisticsCallback& stats_callback); | 32 const profiles::ProfileStatisticsCallback& stats_callback); |
| 42 | 33 |
| 43 // The method below is used for testing. | |
| 44 size_t GetCallbackCount(); | |
| 45 | |
| 46 private: | 34 private: |
| 47 friend class base::RefCountedThreadSafe<ProfileStatisticsAggregator>; | |
| 48 | |
| 49 struct ProfileStatValue { | |
| 50 int count = 0; | |
| 51 bool success = false; // False means the statistics failed to load. | |
| 52 }; | |
| 53 | |
| 54 ~ProfileStatisticsAggregator(); | |
| 55 | |
| 56 // Start gathering statistics. Also cancels existing statistics tasks. | 35 // Start gathering statistics. Also cancels existing statistics tasks. |
| 57 void StartAggregator(); | 36 void StartAggregator(); |
| 58 | 37 |
| 59 // Callback functions | 38 // Callback functions |
| 60 // Normal callback. Appends result to |profile_category_stats_|, and then call | 39 // Appends result to |profile_category_stats_|, and then calls |
| 61 // the external callback. All other callbacks call this function. | 40 // the external callback. |
| 62 void StatisticsCallback(const char* category, ProfileStatValue result); | 41 void StatisticsCallback(const char* category, int count); |
| 63 // Callback for reporting success. | |
| 64 void StatisticsCallbackSuccess(const char* category, int count); | |
| 65 // Callback for reporting failure. | |
| 66 void StatisticsCallbackFailure(const char* category); | |
| 67 | 42 |
| 68 // Callback for counters. | 43 // Callback for counters. |
| 69 void OnCounterResult( | 44 void OnCounterResult( |
| 70 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result); | 45 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result); |
| 71 | 46 |
| 72 // Registers, initializes and starts a BrowsingDataCounter. | 47 // Registers, initializes and starts a BrowsingDataCounter. |
| 73 void AddCounter(std::unique_ptr<browsing_data::BrowsingDataCounter> counter); | 48 void AddCounter(std::unique_ptr<browsing_data::BrowsingDataCounter> counter); |
| 74 | 49 |
| 75 // Preference counting. | |
| 76 ProfileStatValue CountPrefs() const; | |
| 77 | |
| 78 Profile* profile_; | 50 Profile* profile_; |
| 79 base::FilePath profile_path_; | 51 base::FilePath profile_path_; |
| 80 profiles::ProfileCategoryStats profile_category_stats_; | 52 profiles::ProfileCategoryStats profile_category_stats_; |
| 81 | 53 |
| 82 // Callback function to be called when results arrive. Will be called | 54 // Callback function to be called when results arrive. Will be called |
| 83 // multiple times (once for each statistics). | 55 // multiple times (once for each statistics). |
| 84 std::vector<profiles::ProfileStatisticsCallback> stats_callbacks_; | 56 std::vector<profiles::ProfileStatisticsCallback> stats_callbacks_; |
| 85 | 57 |
| 86 // Callback function to be called when all statistics are calculated. | 58 // Callback function to be called when all statistics are calculated. |
| 87 base::Closure done_callback_; | 59 base::Closure done_callback_; |
| 88 | 60 |
| 89 base::CancelableTaskTracker tracker_; | |
| 90 | |
| 91 std::vector<std::unique_ptr<browsing_data::BrowsingDataCounter>> counters_; | 61 std::vector<std::unique_ptr<browsing_data::BrowsingDataCounter>> counters_; |
| 92 | 62 |
| 93 DISALLOW_COPY_AND_ASSIGN(ProfileStatisticsAggregator); | 63 DISALLOW_COPY_AND_ASSIGN(ProfileStatisticsAggregator); |
| 94 }; | 64 }; |
| 95 | 65 |
| 96 #endif // CHROME_BROWSER_PROFILES_PROFILE_STATISTICS_AGGREGATOR_H_ | 66 #endif // CHROME_BROWSER_PROFILES_PROFILE_STATISTICS_AGGREGATOR_H_ |
| OLD | NEW |