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