Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(131)

Side by Side Diff: chrome/browser/extensions/api/feedback_private/log_source_access_manager.h

Issue 2840103002: Add new API function: feedbackPrivate.readLogSource (Closed)
Patch Set: Refactor passing of params from API into Log Source Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 the rules of surrounding the use of
tbarzic 2017/06/06 20:27:26 "Provides bookkeeping for SingleLogSource usage. I
Simon Que 2017/06/06 22:29:15 Done.
24 // SingleLogSources:
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(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 void set_tick_clock(std::unique_ptr<base::TickClock> clock) {
tbarzic 2017/06/06 20:27:26 is this supposed to be used in tests only? If so,
Simon Que 2017/06/06 22:29:15 Done.
43 tick_clock_ = std::move(clock);
44 }
45
46 // Initiates a fetch from a log source, as specified in |params|. See
47 // feedback_private.idl for more info about the actual parameters.
48 bool FetchFromSource(api::feedback_private::ReadLogSourceParams& params,
tbarzic 2017/06/06 20:27:26 nit: const &
Simon Que 2017/06/06 22:29:14 Done.
49 const std::string& extension_id,
50 const ReadLogSourceCallback& callback);
51
52 private:
53 // Every source/extension_id pair is linked to a unique SingleLogSource.
tbarzic 2017/06/06 20:27:26 nit: this comment seems out of place.
Simon Que 2017/06/06 22:29:14 Done.
54 struct SourceAndExtension {
55 explicit SourceAndExtension(api::feedback_private::LogSource source,
56 const std::string& extension_id);
57
58 bool operator<(const SourceAndExtension& other) const {
59 return std::make_pair(source, extension_id) <
60 std::make_pair(other.source, other.extension_id);
61 }
62
63 api::feedback_private::LogSource source;
64 std::string extension_id;
65 };
66
67 static SourceAndExtension MakeKey(api::feedback_private::LogSource source,
68 const std::string& extension_id) {
69 return SourceAndExtension(source, extension_id);
tbarzic 2017/06/06 20:27:26 nit: you can probably do without this method.
Simon Que 2017/06/06 22:29:14 Done.
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 // - resource_id: the ID of the API resource containing the log source that
82 // was used.
83 // - response_callback: callback for sending the response as a
84 // ReadLogSourceResult struct.
85 // - cleanup_callback: callback for removing the source after a fetch.
86 // Can be null if the source needs to be kept.
87 void OnFetchComplete(int resource_id,
88 const ReadLogSourceCallback& response_callback,
89 const base::Closure& cleanup_callback,
90 system_logs::SystemLogsResponse* response);
91
92 // Removes an existing log source indicated by |key| from both the API
93 // Resource Manager and |sources_|.
94 void RemoveSource(const SourceAndExtension& key);
95
96 // Returns a callback to call RemoveSource(key).
97 base::Closure CreateRemoveCallback(const SourceAndExtension& key);
98
99 // Attempts to update the entry for |key| in |last_access_times_| to the
100 // current time, to record that the source is being accessed by the extension
101 // right now. If less than |min_time_between_reads_| has elapsed since the
102 // last successful read, do not update the timestamp in |last_access_times_|,
103 // and instead return false. Otherwise returns true.
104 //
105 // Creates a new entry in |last_access_times_| if it doesn't exist. Will not
106 // delete from |last_access_times_|.
107 bool UpdateSourceAccessTime(const SourceAndExtension& key);
108
109 // Returns the last time that |key.source| was accessed by |key.extension|.
110 // If it was never accessed by the extension, returns an empty base::Time
tbarzic 2017/06/06 20:27:26 nit: base::TimeTicks
Simon Que 2017/06/06 22:29:15 Done.
111 // object.
112 base::TimeTicks GetLastExtensionAccessTime(
113 const SourceAndExtension& key) const;
114
115 // Keys: source/extension pairs for which a SingleLogSource has been created
116 // and not yet destroyed. (i.e. currently in use).
117 // Values: ID of the API Resource containing the SingleLogSource.
118 std::map<SourceAndExtension, int> sources_;
119
120 // Keeps track of the last time each source was accessed by each extension.
121 // Does not get updated when the actual SingleLogSource is accessed. Instead,
122 // explicitly call AccessSourceFromExtension() at the time of accessing the
tbarzic 2017/06/06 20:27:26 update the comment :)
Simon Que 2017/06/06 22:29:15 Done.
123 // SingleLogSource.
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_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698