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

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

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 #include "chrome/browser/extensions/api/feedback_private/log_source_access_manag er.h"
6
7 #include <algorithm>
8 #include <utility>
9
10 #include "base/bind.h"
11 #include "base/memory/ptr_util.h"
12 #include "base/strings/string_split.h"
13 #include "base/time/default_tick_clock.h"
14 #include "chrome/browser/extensions/api/feedback_private/log_source_resource.h"
15 #include "chrome/browser/extensions/api/feedback_private/single_log_source_facto ry.h"
16 #include "extensions/browser/api/api_resource_manager.h"
17
18 namespace extensions {
19
20 namespace {
21
22 namespace feedback_private = api::feedback_private;
23 using feedback_private::LogSource;
24 using SingleLogSource = system_logs::SingleLogSource;
25 using SupportedSource = system_logs::SingleLogSource::SupportedSource;
26 using SystemLogsResponse = system_logs::SystemLogsResponse;
27
28 // The minimum time between consecutive reads of a log source by a particular
29 // extension.
30 const int kDefaultRateLimitingTimeoutMs = 1000;
31
32 // If this is null, then |kDefaultRateLimitingTimeoutMs| is used as the timeout.
33 const base::TimeDelta* g_rate_limiting_timeout = nullptr;
34
35 base::TimeDelta GetMinTimeBetweenReads() {
36 return g_rate_limiting_timeout
37 ? *g_rate_limiting_timeout
38 : base::TimeDelta::FromMilliseconds(kDefaultRateLimitingTimeoutMs);
39 }
40
41 // Converts from feedback_private::LogSource to SupportedSource.
42 SupportedSource GetSupportedSourceType(LogSource source) {
43 switch (source) {
44 case feedback_private::LOG_SOURCE_MESSAGES:
45 return SupportedSource::kMessages;
46 case feedback_private::LOG_SOURCE_UILATEST:
47 return SupportedSource::kUiLatest;
48 case feedback_private::LOG_SOURCE_NONE:
49 NOTREACHED() << "Unknown log source type.";
50 return SingleLogSource::SupportedSource::kMessages;
51 }
tbarzic 2017/06/06 20:27:26 NOTREACHED(); return SingleLogSource::SupportedSou
Simon Que 2017/06/06 22:29:14 Done.
52 }
53
54 // SystemLogsResponse is a map of strings -> strings. The map value has the
55 // actual log contents, a string containing all lines, separated by newlines.
56 // This function extracts the individual lines and converts them into a vector
57 // of strings, each string containing a single line.
58 void GetLogLinesFromSystemLogsResponse(const SystemLogsResponse& response,
59 std::vector<std::string>* log_lines) {
60 for (const std::pair<std::string, std::string>& pair : response) {
61 // TODO(sque): Use std::move?
62 std::vector<std::string> new_lines = base::SplitString(
63 pair.second, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
64 log_lines->reserve(log_lines->size() + new_lines.size());
65 log_lines->insert(log_lines->end(), new_lines.begin(), new_lines.end());
66 }
67 }
68
69 } // namespace
70
71 LogSourceAccessManager::LogSourceAccessManager(content::BrowserContext* context)
72 : context_(context),
73 tick_clock_(new base::DefaultTickClock),
74 weak_factory_(this) {}
75
76 LogSourceAccessManager::~LogSourceAccessManager() {}
77
78 // static
79 void LogSourceAccessManager::SetRateLimitingTimeoutForTesting(
80 const base::TimeDelta* timeout) {
81 g_rate_limiting_timeout = timeout;
82 }
83
84 bool LogSourceAccessManager::FetchFromSource(
85 api::feedback_private::ReadLogSourceParams& params,
86 const std::string& extension_id,
87 const ReadLogSourceCallback& callback) {
88 SourceAndExtension key = MakeKey(params.source, extension_id);
89 int requested_resource_id = params.reader_id ? *params.reader_id : 0;
90 int resource_id =
91 requested_resource_id > 0 ? requested_resource_id : CreateResource(key);
92 if (resource_id <= 0)
93 return false;
94
95 ApiResourceManager<LogSourceResource>* resource_manager =
96 ApiResourceManager<LogSourceResource>::Get(context_);
97 LogSourceResource* resource =
98 resource_manager->Get(extension_id, resource_id);
99 if (!resource)
100 return false;
101
102 // Enforce the rules: rate-limit access to the source from the current
103 // extension. If not enough time has elapsed since the last access, do not
104 // read from the source, but instead return an empty response. From the
105 // caller's perspective, there is no new data. There is no need for the caller
106 // to keep track of the time since last access.
107 if (!UpdateSourceAccessTime(key)) {
108 feedback_private::ReadLogSourceResult empty_result;
109 callback.Run(empty_result);
110 return true;
111 }
112
113 // If the API call requested a non-incremental access, clean up the
114 // SingleLogSource by removing its API resource. Even if the existing source
115 // were originally created as incremental, passing in incremental=false on a
116 // later access indicates that the source should be closed afterwards.
117 base::Closure cleanup_callback;
118 if (!params.incremental)
119 cleanup_callback = CreateRemoveCallback(key);
120
121 resource->GetLogSource()->Fetch(base::Bind(
122 &LogSourceAccessManager::OnFetchComplete, weak_factory_.GetWeakPtr(),
123 resource_id, callback, cleanup_callback));
124 return true;
125 }
126
127 void LogSourceAccessManager::OnFetchComplete(
128 int resource_id,
129 const ReadLogSourceCallback& response_callback,
130 const base::Closure& cleanup_callback,
tbarzic 2017/06/06 20:27:26 instead of passing in cleanup_callback and testing
Simon Que 2017/06/06 22:29:14 Done.
131 SystemLogsResponse* response) {
132 feedback_private::ReadLogSourceResult result;
133 // Always return reader_id=0 if there is a cleanup.
134 result.reader_id = cleanup_callback.is_null() ? resource_id : 0;
135
136 GetLogLinesFromSystemLogsResponse(*response, &result.log_lines);
137 if (!cleanup_callback.is_null())
138 cleanup_callback.Run();
139
140 response_callback.Run(result);
141 }
142
143 void LogSourceAccessManager::RemoveSource(const SourceAndExtension& key) {
144 auto iter = sources_.find(key);
145 if (iter != sources_.end()) {
146 int resource_id = iter->second;
147 sources_.erase(iter);
148 // It's very important to call ApiResourceManager:Remove() after erasing the
149 // entry from sources_. Otherwise ApiResourceManager:Remove() will call this
tbarzic 2017/06/06 20:27:26 this seems very fragile.. Could we avoid having Re
Simon Que 2017/06/06 22:29:14 Done.
150 // function again, ad infinitum.
151 ApiResourceManager<LogSourceResource>::Get(context_)->Remove(
152 key.extension_id, resource_id);
153 }
154 }
155
156 LogSourceAccessManager::SourceAndExtension::SourceAndExtension(
157 api::feedback_private::LogSource source,
158 const std::string& extension_id)
159 : source(source), extension_id(extension_id) {}
160
161 int LogSourceAccessManager::CreateResource(const SourceAndExtension& key) {
162 // Enforce the rules: Do not create a new SingleLogSource if there was already
163 // one created for |key|.
164 if (sources_.find(key) != sources_.end())
165 return 0;
166
167 std::unique_ptr<LogSourceResource> new_resource =
168 base::MakeUnique<LogSourceResource>(
169 key.extension_id,
170 SingleLogSourceFactory::CreateSingleLogSource(
171 GetSupportedSourceType(key.source)),
172 CreateRemoveCallback(key));
173
174 int id = ApiResourceManager<LogSourceResource>::Get(context_)->Add(
175 new_resource.release());
176 sources_[key] = id;
177
178 return id;
179 }
180
181 base::Closure LogSourceAccessManager::CreateRemoveCallback(
182 const SourceAndExtension& key) {
183 return base::Bind(&LogSourceAccessManager::RemoveSource,
184 weak_factory_.GetWeakPtr(), key);
tbarzic 2017/06/06 20:27:26 I'd inline this
Simon Que 2017/06/06 22:29:14 Done.
185 }
186
187 bool LogSourceAccessManager::UpdateSourceAccessTime(
188 const SourceAndExtension& key) {
189 base::TimeTicks last = GetLastExtensionAccessTime(key);
190 base::TimeTicks now = tick_clock_->NowTicks();
191 if (!last.is_null() && now < last + GetMinTimeBetweenReads()) {
192 return false;
193 }
194 last_access_times_[key] = now;
195 return true;
196 }
197
198 base::TimeTicks LogSourceAccessManager::GetLastExtensionAccessTime(
199 const SourceAndExtension& key) const {
200 auto iter = last_access_times_.find(key);
tbarzic 2017/06/06 20:27:26 const auto?
Simon Que 2017/06/06 22:29:14 Done.
201 if (iter == last_access_times_.end())
202 return base::TimeTicks();
203
204 return iter->second;
205 }
206
207 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698