| 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..199dc89d53621b277292734f0d66f6702d3173d8
|
| --- /dev/null
|
| +++ b/chrome/browser/extensions/api/feedback_private/log_source_access_manager.h
|
| @@ -0,0 +1,108 @@
|
| +// 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/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;
|
| + };
|
| +
|
| + explicit LogSourceAccessManager(
|
| + const base::TimeDelta& min_time_between_reads);
|
| + ~LogSourceAccessManager();
|
| +
|
| + // 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::Time GetLastExtensionAccessTime(const SourceAndExtension& key) const;
|
| +
|
| + // Returns a closure (callback with no args) that removes |key| from this
|
| + // 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);
|
| +
|
| + 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::Time> last_access_times_;
|
| +
|
| + // The minimum time between consecutive reads of a source from a particular
|
| + // extension that will result in a valid read. Used for rate-limiting.
|
| + base::TimeDelta min_time_between_reads_;
|
| +
|
| + base::WeakPtrFactory<LogSourceAccessManager> weak_factory_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(LogSourceAccessManager);
|
| +};
|
| +
|
| +} // namespace extensions
|
| +
|
| +#endif // CHROME_BROWSER_EXTENSIONS_API_FEEDBACK_PRIVATE_LOG_SOURCE_ACCESS_MANAGER_H_
|
|
|