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..a36bd4732ed9351b05935210748bb7108b2dbe20 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/feedback_private/log_source_access_manager.h |
| @@ -0,0 +1,144 @@ |
| +// 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 <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/browser/feedback/system_logs/system_logs_fetcher_base.h" |
| +#include "chrome/common/extensions/api/feedback_private.h" |
| +#include "content/public/browser/browser_context.h" |
| + |
| +namespace extensions { |
| + |
| +// Provides bookkeepping for the rules of surrounding the use of |
|
tbarzic
2017/06/06 20:27:26
"Provides bookkeeping for SingleLogSource usage. I
Simon Que
2017/06/06 22:29:15
Done.
|
| +// 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: |
| + using ReadLogSourceCallback = |
| + base::Callback<void(api::feedback_private::ReadLogSourceResult&)>; |
| + |
| + explicit LogSourceAccessManager(content::BrowserContext* context); |
| + ~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); |
| + |
| + void set_tick_clock(std::unique_ptr<base::TickClock> clock) { |
|
tbarzic
2017/06/06 20:27:26
is this supposed to be used in tests only? If so,
Simon Que
2017/06/06 22:29:15
Done.
|
| + tick_clock_ = std::move(clock); |
| + } |
| + |
| + // Initiates a fetch from a log source, as specified in |params|. See |
| + // feedback_private.idl for more info about the actual parameters. |
| + bool FetchFromSource(api::feedback_private::ReadLogSourceParams& params, |
|
tbarzic
2017/06/06 20:27:26
nit: const &
Simon Que
2017/06/06 22:29:14
Done.
|
| + const std::string& extension_id, |
| + const ReadLogSourceCallback& callback); |
| + |
| + private: |
| + // Every source/extension_id pair is linked to a unique SingleLogSource. |
|
tbarzic
2017/06/06 20:27:26
nit: this comment seems out of place.
Simon Que
2017/06/06 22:29:14
Done.
|
| + 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; |
| + }; |
| + |
| + static SourceAndExtension MakeKey(api::feedback_private::LogSource source, |
| + const std::string& extension_id) { |
| + return SourceAndExtension(source, extension_id); |
|
tbarzic
2017/06/06 20:27:26
nit: you can probably do without this method.
Simon Que
2017/06/06 22:29:14
Done.
|
| + } |
| + |
| + // Creates a new LogSourceResource for the source and extension indicated by |
| + // |key|. Stores the new resource in the API Resource Manager and stores the |
| + // resource ID in |sources_| as a new entry. Returns the nonzero ID of the |
| + // newly created resource, or 0 if there was already an existing resource for |
| + // |key|. |
| + int CreateResource(const SourceAndExtension& key); |
| + |
| + // Callback that is passed to the log source from FetchFromSource. |
| + // Arguments: |
| + // - resource_id: the ID of the API resource containing the log source that |
| + // was used. |
| + // - response_callback: callback for sending the response as a |
| + // ReadLogSourceResult struct. |
| + // - cleanup_callback: callback for removing the source after a fetch. |
| + // Can be null if the source needs to be kept. |
| + void OnFetchComplete(int resource_id, |
| + const ReadLogSourceCallback& response_callback, |
| + const base::Closure& cleanup_callback, |
| + system_logs::SystemLogsResponse* response); |
| + |
| + // Removes an existing log source indicated by |key| from both the API |
| + // Resource Manager and |sources_|. |
| + void RemoveSource(const SourceAndExtension& key); |
| + |
| + // Returns a callback to call RemoveSource(key). |
| + base::Closure CreateRemoveCallback(const SourceAndExtension& key); |
| + |
| + // 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 UpdateSourceAccessTime(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 |
|
tbarzic
2017/06/06 20:27:26
nit: base::TimeTicks
Simon Que
2017/06/06 22:29:15
Done.
|
| + // object. |
| + base::TimeTicks GetLastExtensionAccessTime( |
| + const SourceAndExtension& key) const; |
| + |
| + // Keys: source/extension pairs for which a SingleLogSource has been created |
| + // and not yet destroyed. (i.e. currently in use). |
| + // Values: ID of the API Resource containing the SingleLogSource. |
| + std::map<SourceAndExtension, int> sources_; |
| + |
| + // 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 |
|
tbarzic
2017/06/06 20:27:26
update the comment :)
Simon Que
2017/06/06 22:29:15
Done.
|
| + // SingleLogSource. |
| + // |
| + // This intentionally kept separate from |sources_| because entries can be |
| + // removed from and re-added to |sources_|, but that should not erase the |
| + // recorded access times. |
| + std::map<SourceAndExtension, base::TimeTicks> last_access_times_; |
| + |
| + // For fetching browser resources like ApiResourceManager. |
| + content::BrowserContext* context_; |
| + |
| + // 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_ |