| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_BROWSER_PROFILES_PROFILE_STATISTICS_AGGREGATOR_H_ |
| 6 #define CHROME_BROWSER_PROFILES_PROFILE_STATISTICS_AGGREGATOR_H_ |
| 7 |
| 8 #include <stddef.h> |
| 9 |
| 10 #include <set> |
| 11 #include <vector> |
| 12 |
| 13 #include "base/callback_forward.h" |
| 14 #include "base/files/file_path.h" |
| 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/task_runner.h" |
| 18 #include "chrome/browser/profiles/profile_statistics_common.h" |
| 19 #include "components/bookmarks/browser/bookmark_model_observer.h" |
| 20 #include "components/history/core/browser/history_types.h" |
| 21 #include "components/password_manager/core/browser/password_store_consumer.h" |
| 22 |
| 23 namespace bookmarks { |
| 24 class BookMarkModel; |
| 25 class BookMarkNode; |
| 26 } |
| 27 |
| 28 class Profile; |
| 29 |
| 30 class ProfileStatisticsAggregator |
| 31 : public base::RefCountedThreadSafe<ProfileStatisticsAggregator> { |
| 32 // This class is used internally by GetProfileStatistics and |
| 33 // StoreProfileStatisticsToCache. |
| 34 // |
| 35 // The class collects statistical information about the profile and returns |
| 36 // the information via a callback function. Currently bookmarks, history, |
| 37 // logins and preferences are counted. |
| 38 // |
| 39 // The class is RefCounted because this is needed for CancelableTaskTracker |
| 40 // to function properly. Once all tasks are run (or cancelled) the instance is |
| 41 // automatically destructed. |
| 42 |
| 43 public: |
| 44 ProfileStatisticsAggregator(Profile* profile, |
| 45 const profiles::ProfileStatisticsCallback& stats_callback, |
| 46 const base::Closure& destruct_callback); |
| 47 |
| 48 void AddCallbackAndStartAggregator( |
| 49 const profiles::ProfileStatisticsCallback& stats_callback); |
| 50 |
| 51 // The method below is used for testing. |
| 52 size_t GetCallbackCount(); |
| 53 |
| 54 private: |
| 55 friend class base::RefCountedThreadSafe<ProfileStatisticsAggregator>; |
| 56 |
| 57 struct ProfileStatValue { |
| 58 int count; |
| 59 bool success; // false means the statistics failed to load |
| 60 }; |
| 61 |
| 62 ~ProfileStatisticsAggregator(); |
| 63 |
| 64 // Start gathering statistics. Also cancels existing statistics tasks. |
| 65 void StartAggregator(); |
| 66 |
| 67 // Callback functions |
| 68 // Normal callback. Appends result to |profile_category_stats_|, and then call |
| 69 // the external callback. All other callbacks call this function. |
| 70 void StatisticsCallback(const char* category, ProfileStatValue result); |
| 71 // Callback for reporting success. |
| 72 void StatisticsCallbackSuccess(const char* category, int count); |
| 73 // Callback for reporting failure. |
| 74 void StatisticsCallbackFailure(const char* category); |
| 75 // Callback for history. |
| 76 void StatisticsCallbackHistory(history::HistoryCountResult result); |
| 77 |
| 78 // Bookmark counting. |
| 79 void WaitOrCountBookmarks(); |
| 80 void CountBookmarks(bookmarks::BookmarkModel* bookmark_model); |
| 81 |
| 82 class BookmarkModelHelper |
| 83 : public bookmarks::BookmarkModelObserver { |
| 84 public: |
| 85 explicit BookmarkModelHelper(ProfileStatisticsAggregator* parent) |
| 86 : parent_(parent) {} |
| 87 |
| 88 void BookmarkModelLoaded(bookmarks::BookmarkModel* model, |
| 89 bool ids_reassigned) override; |
| 90 |
| 91 void BookmarkNodeMoved(bookmarks::BookmarkModel* model, |
| 92 const bookmarks::BookmarkNode* old_parent, |
| 93 int old_index, |
| 94 const bookmarks::BookmarkNode* new_parent, |
| 95 int new_index) override {} |
| 96 |
| 97 void BookmarkNodeAdded(bookmarks::BookmarkModel* model, |
| 98 const bookmarks::BookmarkNode* parent, |
| 99 int index) override {} |
| 100 |
| 101 void BookmarkNodeRemoved(bookmarks::BookmarkModel* model, |
| 102 const bookmarks::BookmarkNode* parent, |
| 103 int old_index, const bookmarks::BookmarkNode* node, |
| 104 const std::set<GURL>& no_longer_bookmarked) override {} |
| 105 |
| 106 void BookmarkNodeChanged(bookmarks::BookmarkModel* model, |
| 107 const bookmarks::BookmarkNode* node) override {} |
| 108 |
| 109 void BookmarkNodeFaviconChanged(bookmarks::BookmarkModel* model, |
| 110 const bookmarks::BookmarkNode* node) override {} |
| 111 |
| 112 void BookmarkNodeChildrenReordered(bookmarks::BookmarkModel* model, |
| 113 const bookmarks::BookmarkNode* node) override {} |
| 114 |
| 115 void BookmarkAllUserNodesRemoved(bookmarks::BookmarkModel* model, |
| 116 const std::set<GURL>& removed_urls) override {} |
| 117 |
| 118 private: |
| 119 ProfileStatisticsAggregator* parent_ = nullptr; |
| 120 }; |
| 121 |
| 122 // Password counting |
| 123 class PasswordStoreConsumerHelper |
| 124 : public password_manager::PasswordStoreConsumer { |
| 125 public: |
| 126 explicit PasswordStoreConsumerHelper(ProfileStatisticsAggregator* parent) |
| 127 : parent_(parent) {} |
| 128 |
| 129 void OnGetPasswordStoreResults( |
| 130 ScopedVector<autofill::PasswordForm> results) override; |
| 131 |
| 132 private: |
| 133 ProfileStatisticsAggregator* parent_ = nullptr; |
| 134 |
| 135 DISALLOW_COPY_AND_ASSIGN(PasswordStoreConsumerHelper); |
| 136 }; |
| 137 |
| 138 // Preference counting. |
| 139 ProfileStatValue CountPrefs() const; |
| 140 |
| 141 Profile* profile_; |
| 142 base::FilePath profile_path_; |
| 143 profiles::ProfileCategoryStats profile_category_stats_; |
| 144 |
| 145 // Callback function to be called when results arrive. Will be called |
| 146 // multiple times (once for each statistics). |
| 147 std::vector<profiles::ProfileStatisticsCallback> stats_callbacks_; |
| 148 |
| 149 // Callback function to be called when the aggregator destructs. Used for |
| 150 // removing expiring references to this aggregator. |
| 151 base::Closure destruct_callback_; |
| 152 |
| 153 base::CancelableTaskTracker tracker_; |
| 154 |
| 155 // Bookmark counting |
| 156 scoped_ptr<BookmarkModelHelper> bookmark_model_helper_; |
| 157 |
| 158 // Password counting. |
| 159 PasswordStoreConsumerHelper password_store_consumer_helper_; |
| 160 |
| 161 DISALLOW_COPY_AND_ASSIGN(ProfileStatisticsAggregator); |
| 162 }; |
| 163 |
| 164 #endif // CHROME_BROWSER_PROFILES_PROFILE_STATISTICS_AGGREGATOR_H_ |
| OLD | NEW |