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 | |
| 12 #include "base/callback.h" | |
| 13 #include "base/macros.h" | |
| 14 #include "base/memory/weak_ptr.h" | |
| 15 #include "base/time/time.h" | |
| 16 #include "chrome/common/extensions/api/feedback_private.h" | |
| 17 | |
| 18 namespace extensions { | |
| 19 | |
| 20 // Provides bookkeepping for the rules of surrounding the use of | |
| 21 // SingleLogSources: | |
| 22 // - Each extension can have only one SingleLogSource for a particular source. | |
| 23 // - A source may not be accessed too frequently by an extension. | |
| 24 class LogSourceAccessManager { | |
| 25 public: | |
| 26 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.
| |
| 27 std::pair<api::feedback_private::LogSource, std::string>; | |
| 28 | |
| 29 LogSourceAccessManager(); | |
| 30 ~LogSourceAccessManager(); | |
| 31 | |
| 32 // Add a source/extension pair to |active_keys_|. Returns true if there was no | |
| 33 // existing entry, or false otherwise. | |
| 34 bool AddExtension(const SourceAndExtension& key); | |
| 35 | |
| 36 // Removes a source/extension pair from |active_keys_|. Returns true if | |
| 37 // there an existing entry, or false otherwise. | |
| 38 bool RemoveExtension(const SourceAndExtension& key); | |
| 39 | |
| 40 // Returns the number of entries in |active_keys_| with source=|source|. | |
| 41 size_t GetNumActiveExtensionsForSource( | |
| 42 api::feedback_private::LogSource source) const; | |
| 43 | |
| 44 // Updates the entry for |key| in |last_access_times_| to the current time, to | |
| 45 // record that the source is being accessed by the extension right now. | |
| 46 // Creates a new entry in |last_access_times_| if it doesn't exist. Will not | |
| 47 // delete from |last_access_times_|. | |
| 48 void AccessSourceFromExtension(const SourceAndExtension& key); | |
| 49 | |
| 50 // Returns the last time that |key.source| was accessed by |key.extension|. | |
| 51 // If it was never accessed by the extension, returns an empty base::Time | |
| 52 // object. | |
| 53 base::Time GetLastExtensionAccessTime(const SourceAndExtension& key) const; | |
| 54 | |
| 55 // Returns a closure (callback with no args) that removes |key| from this | |
| 56 // LogSourceAccessManager. This so that when ApiResourceManager automatically | |
| 57 // deletes its entries that track SingleLogSources, it can update the | |
| 58 // bookkeeping entries in LogSourceAccessManager. | |
| 59 base::Closure GetUnregisterCallback(const SourceAndExtension& key); | |
| 60 | |
| 61 private: | |
| 62 // Calls RemoveExtension() but discards the return value. Used to generate the | |
| 63 // closure that is returned by GetUnregisterCallback. | |
| 64 void RemoveExtensionNoReturn(const SourceAndExtension& key); | |
| 65 | |
| 66 // Contains all source/extension pairs for which a SingleLogSource has been | |
| 67 // created and not yet destroyed (i.e. still in use). | |
| 68 std::set<SourceAndExtension> active_keys_; | |
| 69 | |
| 70 // Keeps track of the last time each source was accessed by each extension. | |
| 71 // Does not get updated when the actual SingleLogSource is accessed. Instead, | |
| 72 // explicitly call AccessSourceFromExtension() at the time of accessing the | |
| 73 // SingleLogSource. | |
| 74 // | |
| 75 // This intentionally kept separate from |active_keys_| because entries can be | |
| 76 // removed from and re-added to |active_keys_|, but that should not erase the | |
| 77 // recorded access times. | |
| 78 std::map<SourceAndExtension, base::Time> last_access_times_; | |
| 79 | |
| 80 base::WeakPtrFactory<LogSourceAccessManager> weak_factory_; | |
| 81 | |
| 82 DISALLOW_COPY_AND_ASSIGN(LogSourceAccessManager); | |
| 83 }; | |
| 84 | |
| 85 } // namespace extensions | |
| 86 | |
| 87 #endif // CHROME_BROWSER_EXTENSIONS_API_FEEDBACK_PRIVATE_LOG_SOURCE_ACCESS_MANA GER_H_ | |
| OLD | NEW |