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..0dea0984bde18420f3bb4be31721d4452aa75c88 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/feedback_private/log_source_access_manager.h |
| @@ -0,0 +1,87 @@ |
| +// 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 "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: |
| + using SourceAndExtension = |
|
tbarzic
2017/05/22 23:02:55
I'd prefer to have this defined as a struct {LogSo
Simon Que
2017/05/23 20:01:20
Done.
|
| + std::pair<api::feedback_private::LogSource, std::string>; |
| + |
| + LogSourceAccessManager(); |
| + ~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; |
| + |
| + // Updates 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. |
| + // Creates a new entry in |last_access_times_| if it doesn't exist. Will not |
| + // delete from |last_access_times_|. |
| + void 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_; |
| + |
| + base::WeakPtrFactory<LogSourceAccessManager> weak_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(LogSourceAccessManager); |
| +}; |
| + |
| +} // namespace extensions |
| + |
| +#endif // CHROME_BROWSER_EXTENSIONS_API_FEEDBACK_PRIVATE_LOG_SOURCE_ACCESS_MANAGER_H_ |