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

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

Powered by Google App Engine
This is Rietveld 408576698