| 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 <string> |
| 10 #include <utility> |
| 11 |
| 12 #include "base/callback.h" |
| 13 #include "base/gtest_prod_util.h" |
| 14 #include "base/macros.h" |
| 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/time/tick_clock.h" |
| 17 #include "base/time/time.h" |
| 18 #include "chrome/browser/feedback/system_logs/system_logs_fetcher.h" |
| 19 #include "chrome/common/extensions/api/feedback_private.h" |
| 20 #include "content/public/browser/browser_context.h" |
| 21 |
| 22 namespace extensions { |
| 23 |
| 24 // Provides bookkeepping for SingleLogSource usage. It ensures that: |
| 25 // - Each extension can have only one SingleLogSource for a particular source. |
| 26 // - A source may not be accessed too frequently by an extension. |
| 27 class LogSourceAccessManager { |
| 28 public: |
| 29 using ReadLogSourceCallback = |
| 30 base::Callback<void(const api::feedback_private::ReadLogSourceResult&)>; |
| 31 |
| 32 explicit LogSourceAccessManager(content::BrowserContext* context); |
| 33 ~LogSourceAccessManager(); |
| 34 |
| 35 // To override the default rate-limiting mechanism of this function, pass in |
| 36 // a TimeDelta representing the desired minimum time between consecutive reads |
| 37 // of a source from an extension. Does not take ownership of |timeout|. When |
| 38 // done testing, call this function again with |timeout|=nullptr to reset to |
| 39 // the default behavior. |
| 40 static void SetRateLimitingTimeoutForTesting(const base::TimeDelta* timeout); |
| 41 |
| 42 // Override the default base::Time clock with a custom clock for testing. |
| 43 // Pass in |clock|=nullptr to revert to default behavior. |
| 44 void SetTickClockForTesting(std::unique_ptr<base::TickClock> clock) { |
| 45 tick_clock_ = std::move(clock); |
| 46 } |
| 47 |
| 48 // Initiates a fetch from a log source, as specified in |params|. See |
| 49 // feedback_private.idl for more info about the actual parameters. |
| 50 bool FetchFromSource(const api::feedback_private::ReadLogSourceParams& params, |
| 51 const std::string& extension_id, |
| 52 const ReadLogSourceCallback& callback); |
| 53 |
| 54 private: |
| 55 FRIEND_TEST_ALL_PREFIXES(LogSourceAccessManagerTest, |
| 56 MaxNumberOfOpenLogSources); |
| 57 |
| 58 // Contains a source/extension pair. |
| 59 struct SourceAndExtension { |
| 60 explicit SourceAndExtension(api::feedback_private::LogSource source, |
| 61 const std::string& extension_id); |
| 62 |
| 63 bool operator<(const SourceAndExtension& other) const { |
| 64 return std::make_pair(source, extension_id) < |
| 65 std::make_pair(other.source, other.extension_id); |
| 66 } |
| 67 |
| 68 api::feedback_private::LogSource source; |
| 69 std::string extension_id; |
| 70 }; |
| 71 |
| 72 // Creates a new LogSourceResource for the source and extension indicated by |
| 73 // |key|. Stores the new resource in the API Resource Manager and stores the |
| 74 // resource ID in |sources_| as a new entry. Returns the nonzero ID of the |
| 75 // newly created resource, or 0 if there was already an existing resource for |
| 76 // |key|. |
| 77 int CreateResource(const SourceAndExtension& key); |
| 78 |
| 79 // Callback that is passed to the log source from FetchFromSource. |
| 80 // Arguments: |
| 81 // - key: The source that was read, and the extension requesting the read. |
| 82 // - delete_source: Set this if the source indicated by |key| should be |
| 83 // removed from both the API Resource Manager and from |sources_|. |
| 84 // - response_callback: Callback for sending the response as a |
| 85 // ReadLogSourceResult struct. |
| 86 void OnFetchComplete(const SourceAndExtension& key, |
| 87 bool delete_source, |
| 88 const ReadLogSourceCallback& callback, |
| 89 system_logs::SystemLogsResponse* response); |
| 90 |
| 91 // Removes an existing log source indicated by |key| from both the API |
| 92 // Resource Manager and |sources_|. |
| 93 void RemoveSource(const SourceAndExtension& key); |
| 94 |
| 95 // Attempts to update the entry for |key| in |last_access_times_| to the |
| 96 // current time, to record that the source is being accessed by the extension |
| 97 // right now. If less than |min_time_between_reads_| has elapsed since the |
| 98 // last successful read, do not update the timestamp in |last_access_times_|, |
| 99 // and instead return false. Otherwise returns true. |
| 100 // |
| 101 // Creates a new entry in |last_access_times_| if it doesn't exist. Will not |
| 102 // delete from |last_access_times_|. |
| 103 bool UpdateSourceAccessTime(const SourceAndExtension& key); |
| 104 |
| 105 // Returns the last time that |key.source| was accessed by |key.extension|. |
| 106 // If it was never accessed by the extension, returns an empty base::TimeTicks |
| 107 // object. |
| 108 base::TimeTicks GetLastExtensionAccessTime( |
| 109 const SourceAndExtension& key) const; |
| 110 |
| 111 // Returns the number of entries in |sources_| with source=|source|. |
| 112 size_t GetNumActiveResourcesForSource( |
| 113 api::feedback_private::LogSource source) const; |
| 114 |
| 115 // Every SourceAndExtension is linked to a unique SingleLogSource. |
| 116 // |
| 117 // Keys: SourceAndExtension for which a SingleLogSource has been created |
| 118 // and not yet destroyed. (i.e. currently in use). |
| 119 // Values: ID of the API Resource containing the SingleLogSource. |
| 120 std::map<SourceAndExtension, int> sources_; |
| 121 |
| 122 // Keeps track of the last time each source was accessed by each extension. |
| 123 // Each time FetchFromSource() is called, the timestamp gets updated. |
| 124 // |
| 125 // This intentionally kept separate from |sources_| because entries can be |
| 126 // removed from and re-added to |sources_|, but that should not erase the |
| 127 // recorded access times. |
| 128 std::map<SourceAndExtension, base::TimeTicks> last_access_times_; |
| 129 |
| 130 // For fetching browser resources like ApiResourceManager. |
| 131 content::BrowserContext* context_; |
| 132 |
| 133 // Provides a timer clock implementation for keeping track of access times. |
| 134 // Can override the default clock for testing. |
| 135 std::unique_ptr<base::TickClock> tick_clock_; |
| 136 |
| 137 base::WeakPtrFactory<LogSourceAccessManager> weak_factory_; |
| 138 |
| 139 DISALLOW_COPY_AND_ASSIGN(LogSourceAccessManager); |
| 140 }; |
| 141 |
| 142 } // namespace extensions |
| 143 |
| 144 #endif // CHROME_BROWSER_EXTENSIONS_API_FEEDBACK_PRIVATE_LOG_SOURCE_ACCESS_MANA
GER_H_ |
| OLD | NEW |