Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(39)

Side by Side Diff: chrome/browser/profiles/profile_statistics_aggregator.h

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

Powered by Google App Engine
This is Rietveld 408576698