Chromium Code Reviews| Index: chrome/browser/extensions/api/feedback_private/log_source_access_manager.h |
| diff --git a/chrome/browser/extensions/api/feedback_private/log_source_access_manager.h b/chrome/browser/extensions/api/feedback_private/log_source_access_manager.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..939aa20f985db19b1f1346cd5854f223b0921421 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/feedback_private/log_source_access_manager.h |
| @@ -0,0 +1,121 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CHROME_BROWSER_EXTENSIONS_API_FEEDBACK_PRIVATE_LOG_SOURCE_ACCESS_MANAGER_H_ |
| +#define CHROME_BROWSER_EXTENSIONS_API_FEEDBACK_PRIVATE_LOG_SOURCE_ACCESS_MANAGER_H_ |
| + |
| +#include <map> |
| +#include <set> |
| +#include <string> |
| +#include <utility> |
| + |
| +#include "base/callback.h" |
| +#include "base/macros.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "base/time/tick_clock.h" |
| +#include "base/time/time.h" |
| +#include "chrome/common/extensions/api/feedback_private.h" |
| + |
| +namespace extensions { |
| + |
| +// Provides bookkeepping for the rules of surrounding the use of |
| +// SingleLogSources: |
| +// - Each extension can have only one SingleLogSource for a particular source. |
| +// - A source may not be accessed too frequently by an extension. |
| +class LogSourceAccessManager { |
| + public: |
| + // Every source/extension_id pair is linked to a unique SingleLogSource. |
| + struct SourceAndExtension { |
| + explicit SourceAndExtension(api::feedback_private::LogSource source, |
| + const std::string& extension_id); |
| + |
| + bool operator<(const SourceAndExtension& other) const { |
| + return std::make_pair(source, extension_id) < |
| + std::make_pair(other.source, other.extension_id); |
| + } |
| + |
| + api::feedback_private::LogSource source; |
| + std::string extension_id; |
| + }; |
| + |
| + LogSourceAccessManager(); |
| + ~LogSourceAccessManager(); |
| + |
| + // To override the default rate-limiting mechanism of this function, pass in |
| + // a TimeDelta representing the desired minimum time between consecutive reads |
| + // of a source from an extension. Does not take ownership of |timeout|. When |
| + // done testing, call this function again with |timeout|=nullptr to reset to |
| + // the default behavior. |
| + static void SetRateLimitingTimeoutForTesting(const base::TimeDelta* timeout); |
| + |
| + // Add a source/extension pair to |active_keys_|. Returns true if there was no |
| + // existing entry, or false otherwise. |
| + bool AddExtension(const SourceAndExtension& key); |
| + |
| + // Removes a source/extension pair from |active_keys_|. Returns true if |
| + // there an existing entry, or false otherwise. |
| + bool RemoveExtension(const SourceAndExtension& key); |
| + |
| + // Returns the number of entries in |active_keys_| with source=|source|. |
| + size_t GetNumActiveExtensionsForSource( |
| + api::feedback_private::LogSource source) const; |
| + |
| + // Attempts to update the entry for |key| in |last_access_times_| to the |
| + // current time, to record that the source is being accessed by the extension |
| + // right now. If less than |min_time_between_reads_| has elapsed since the |
| + // last successful read, do not update the timestamp in |last_access_times_|, |
| + // and instead return false. Otherwise returns true. |
| + // |
| + // Creates a new entry in |last_access_times_| if it doesn't exist. Will not |
| + // delete from |last_access_times_|. |
| + bool AccessSourceFromExtension(const SourceAndExtension& key); |
| + |
| + // Returns the last time that |key.source| was accessed by |key.extension|. |
| + // If it was never accessed by the extension, returns an empty base::Time |
| + // object. |
| + base::TimeTicks GetLastExtensionAccessTime( |
| + const SourceAndExtension& key) const; |
| + |
| + // 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.
|
| + // LogSourceAccessManager. This so that when ApiResourceManager automatically |
| + // deletes its entries that track SingleLogSources, it can update the |
| + // bookkeeping entries in LogSourceAccessManager. |
| + base::Closure GetUnregisterCallback(const SourceAndExtension& key); |
| + |
| + // 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.
|
| + void set_tick_clock(std::unique_ptr<base::TickClock> clock) { |
| + 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.
|
| + } |
| + |
| + private: |
| + // Calls RemoveExtension() but discards the return value. Used to generate the |
| + // closure that is returned by GetUnregisterCallback. |
| + void RemoveExtensionNoReturn(const SourceAndExtension& key); |
| + |
| + // Contains all source/extension pairs for which a SingleLogSource has been |
| + // created and not yet destroyed (i.e. still in use). |
| + std::set<SourceAndExtension> active_keys_; |
| + |
| + // Keeps track of the last time each source was accessed by each extension. |
| + // Does not get updated when the actual SingleLogSource is accessed. Instead, |
| + // explicitly call AccessSourceFromExtension() at the time of accessing the |
| + // SingleLogSource. |
| + // |
| + // This intentionally kept separate from |active_keys_| because entries can be |
| + // removed from and re-added to |active_keys_|, but that should not erase the |
| + // recorded access times. |
| + std::map<SourceAndExtension, base::TimeTicks> last_access_times_; |
| + |
| + // Provides a timer clock implementation for keeping track of access times. |
| + // Can override the default clock for testing. |
| + std::unique_ptr<base::TickClock> tick_clock_; |
| + |
| + base::WeakPtrFactory<LogSourceAccessManager> weak_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(LogSourceAccessManager); |
| +}; |
| + |
| +} // namespace extensions |
| + |
| +#endif // CHROME_BROWSER_EXTENSIONS_API_FEEDBACK_PRIVATE_LOG_SOURCE_ACCESS_MANAGER_H_ |