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

Side by Side Diff: chrome/browser/extensions/api/feedback_private/log_source_access_manager.h

Issue 2840103002: Add new API function: feedbackPrivate.readLogSource (Closed)
Patch Set: Rebased; updated histogram enum Created 3 years, 6 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 2017 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_EXTENSIONS_API_FEEDBACK_PRIVATE_LOG_SOURCE_ACCESS_MANAGER _H_
6 #define CHROME_BROWSER_EXTENSIONS_API_FEEDBACK_PRIVATE_LOG_SOURCE_ACCESS_MANAGER _H_
7
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <utility>
12
13 #include "base/callback.h"
14 #include "base/macros.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/time/tick_clock.h"
17 #include "base/time/time.h"
18 #include "chrome/common/extensions/api/feedback_private.h"
19
20 namespace extensions {
21
22 // Provides bookkeepping for the rules of surrounding the use of
23 // SingleLogSources:
24 // - Each extension can have only one SingleLogSource for a particular source.
25 // - A source may not be accessed too frequently by an extension.
26 class LogSourceAccessManager {
27 public:
28 // Every source/extension_id pair is linked to a unique SingleLogSource.
29 struct SourceAndExtension {
30 explicit SourceAndExtension(api::feedback_private::LogSource source,
31 const std::string& extension_id);
32
33 bool operator<(const SourceAndExtension& other) const {
34 return std::make_pair(source, extension_id) <
35 std::make_pair(other.source, other.extension_id);
36 }
37
38 api::feedback_private::LogSource source;
39 std::string extension_id;
40 };
41
42 LogSourceAccessManager();
43 ~LogSourceAccessManager();
44
45 // To override the default rate-limiting mechanism of this function, pass in
46 // a TimeDelta representing the desired minimum time between consecutive reads
47 // of a source from an extension. Does not take ownership of |timeout|. When
48 // done testing, call this function again with |timeout|=nullptr to reset to
49 // the default behavior.
50 static void SetRateLimitingTimeoutForTesting(const base::TimeDelta* timeout);
51
52 // Add a source/extension pair to |active_keys_|. Returns true if there was no
53 // existing entry, or false otherwise.
54 bool AddExtension(const SourceAndExtension& key);
55
56 // Removes a source/extension pair from |active_keys_|. Returns true if
57 // there an existing entry, or false otherwise.
58 bool RemoveExtension(const SourceAndExtension& key);
59
60 // Returns the number of entries in |active_keys_| with source=|source|.
61 size_t GetNumActiveExtensionsForSource(
62 api::feedback_private::LogSource source) const;
63
64 // Attempts to update the entry for |key| in |last_access_times_| to the
65 // current time, to record that the source is being accessed by the extension
66 // right now. If less than |min_time_between_reads_| has elapsed since the
67 // last successful read, do not update the timestamp in |last_access_times_|,
68 // and instead return false. Otherwise returns true.
69 //
70 // Creates a new entry in |last_access_times_| if it doesn't exist. Will not
71 // delete from |last_access_times_|.
72 bool AccessSourceFromExtension(const SourceAndExtension& key);
73
74 // Returns the last time that |key.source| was accessed by |key.extension|.
75 // If it was never accessed by the extension, returns an empty base::Time
76 // object.
77 base::TimeTicks GetLastExtensionAccessTime(
78 const SourceAndExtension& key) const;
79
80 // Returns a closure (callback with no args) that removes |key| from this
tbarzic 2017/06/01 19:23:46 nit: remove (callback with no args)
Simon Que 2017/06/01 21:49:09 Done.
81 // LogSourceAccessManager. This so that when ApiResourceManager automatically
82 // deletes its entries that track SingleLogSources, it can update the
83 // bookkeeping entries in LogSourceAccessManager.
84 base::Closure GetUnregisterCallback(const SourceAndExtension& key);
85
86 // Takes ownership of |clock|.
tbarzic 2017/06/01 19:23:46 no need for the comment, it's evident from argumen
Simon Que 2017/06/01 21:49:10 Done.
87 void set_tick_clock(std::unique_ptr<base::TickClock> clock) {
88 tick_clock_.reset(clock.release());
tbarzic 2017/06/01 19:23:46 nit: tick_clock_ = std::move(clock);
Simon Que 2017/06/01 21:49:10 Done.
89 }
90
91 private:
92 // Calls RemoveExtension() but discards the return value. Used to generate the
93 // closure that is returned by GetUnregisterCallback.
94 void RemoveExtensionNoReturn(const SourceAndExtension& key);
95
96 // Contains all source/extension pairs for which a SingleLogSource has been
97 // created and not yet destroyed (i.e. still in use).
98 std::set<SourceAndExtension> active_keys_;
99
100 // Keeps track of the last time each source was accessed by each extension.
101 // Does not get updated when the actual SingleLogSource is accessed. Instead,
102 // explicitly call AccessSourceFromExtension() at the time of accessing the
103 // SingleLogSource.
104 //
105 // This intentionally kept separate from |active_keys_| because entries can be
106 // removed from and re-added to |active_keys_|, but that should not erase the
107 // recorded access times.
108 std::map<SourceAndExtension, base::TimeTicks> last_access_times_;
109
110 // Provides a timer clock implementation for keeping track of access times.
111 // Can override the default clock for testing.
112 std::unique_ptr<base::TickClock> tick_clock_;
113
114 base::WeakPtrFactory<LogSourceAccessManager> weak_factory_;
115
116 DISALLOW_COPY_AND_ASSIGN(LogSourceAccessManager);
117 };
118
119 } // namespace extensions
120
121 #endif // CHROME_BROWSER_EXTENSIONS_API_FEEDBACK_PRIVATE_LOG_SOURCE_ACCESS_MANA GER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698