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