Chromium Code Reviews| OLD | NEW |
|---|---|
| (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); | |
|
tbarzic
2017/05/30 19:35:35
do you still need this, given that you can just a
Simon Que
2017/05/31 00:21:44
I'll deal with this once I figure out the larger p
Simon Que
2017/06/01 00:06:40
See the new API unit tests. If I got rid of this,
| |
| 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 | |
| 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|. | |
| 87 void set_tick_clock(base::TickClock* clock) { tick_clock_.reset(clock); } | |
|
tbarzic
2017/05/30 19:35:35
pass in std::unique_ptr<base::TickClock>, to make
Simon Que
2017/05/31 00:21:44
Done.
| |
| 88 | |
| 89 private: | |
| 90 // Calls RemoveExtension() but discards the return value. Used to generate the | |
| 91 // closure that is returned by GetUnregisterCallback. | |
| 92 void RemoveExtensionNoReturn(const SourceAndExtension& key); | |
| 93 | |
| 94 // Contains all source/extension pairs for which a SingleLogSource has been | |
| 95 // created and not yet destroyed (i.e. still in use). | |
| 96 std::set<SourceAndExtension> active_keys_; | |
| 97 | |
| 98 // Keeps track of the last time each source was accessed by each extension. | |
| 99 // Does not get updated when the actual SingleLogSource is accessed. Instead, | |
| 100 // explicitly call AccessSourceFromExtension() at the time of accessing the | |
| 101 // SingleLogSource. | |
| 102 // | |
| 103 // This intentionally kept separate from |active_keys_| because entries can be | |
| 104 // removed from and re-added to |active_keys_|, but that should not erase the | |
| 105 // recorded access times. | |
| 106 std::map<SourceAndExtension, base::TimeTicks> last_access_times_; | |
| 107 | |
| 108 // Provides a timer clock implementation for keeping track of access times. | |
| 109 // Can override the default clock for testing. | |
| 110 std::unique_ptr<base::TickClock> tick_clock_; | |
| 111 | |
| 112 base::WeakPtrFactory<LogSourceAccessManager> weak_factory_; | |
| 113 | |
| 114 DISALLOW_COPY_AND_ASSIGN(LogSourceAccessManager); | |
| 115 }; | |
| 116 | |
| 117 } // namespace extensions | |
| 118 | |
| 119 #endif // CHROME_BROWSER_EXTENSIONS_API_FEEDBACK_PRIVATE_LOG_SOURCE_ACCESS_MANA GER_H_ | |
| OLD | NEW |