OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/feedback_private/log_source_access_manag
er.h" | 5 #include "chrome/browser/extensions/api/feedback_private/log_source_access_manag
er.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
12 #include "base/strings/string_split.h" | 12 #include "base/strings/string_split.h" |
13 #include "base/time/default_tick_clock.h" | 13 #include "base/time/default_tick_clock.h" |
14 #include "chrome/browser/extensions/api/feedback_private/log_source_resource.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" | 15 #include "chrome/browser/extensions/api/feedback_private/single_log_source_facto
ry.h" |
16 #include "extensions/browser/api/api_resource_manager.h" | 16 #include "extensions/browser/api/api_resource_manager.h" |
17 | 17 |
18 namespace extensions { | 18 namespace extensions { |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 namespace feedback_private = api::feedback_private; | 22 namespace feedback_private = api::feedback_private; |
23 using feedback_private::LogSource; | 23 |
24 using SingleLogSource = system_logs::SingleLogSource; | |
25 using SupportedSource = system_logs::SingleLogSource::SupportedSource; | |
26 using SystemLogsResponse = system_logs::SystemLogsResponse; | 24 using SystemLogsResponse = system_logs::SystemLogsResponse; |
27 | 25 |
28 const int kMaxReadersPerSource = 10; | 26 const int kMaxReadersPerSource = 10; |
29 | 27 |
30 // The minimum time between consecutive reads of a log source by a particular | 28 // The minimum time between consecutive reads of a log source by a particular |
31 // extension. | 29 // extension. |
32 const int kDefaultRateLimitingTimeoutMs = 1000; | 30 const int kDefaultRateLimitingTimeoutMs = 1000; |
33 | 31 |
34 // If this is null, then |kDefaultRateLimitingTimeoutMs| is used as the timeout. | 32 // If this is null, then |kDefaultRateLimitingTimeoutMs| is used as the timeout. |
35 const base::TimeDelta* g_rate_limiting_timeout = nullptr; | 33 const base::TimeDelta* g_rate_limiting_timeout = nullptr; |
36 | 34 |
37 base::TimeDelta GetMinTimeBetweenReads() { | 35 base::TimeDelta GetMinTimeBetweenReads() { |
38 return g_rate_limiting_timeout | 36 return g_rate_limiting_timeout |
39 ? *g_rate_limiting_timeout | 37 ? *g_rate_limiting_timeout |
40 : base::TimeDelta::FromMilliseconds(kDefaultRateLimitingTimeoutMs); | 38 : base::TimeDelta::FromMilliseconds(kDefaultRateLimitingTimeoutMs); |
41 } | 39 } |
42 | 40 |
43 // Converts from feedback_private::LogSource to SupportedSource. | |
44 SupportedSource GetSupportedSourceType(LogSource source) { | |
45 switch (source) { | |
46 case feedback_private::LOG_SOURCE_MESSAGES: | |
47 return SupportedSource::kMessages; | |
48 case feedback_private::LOG_SOURCE_UILATEST: | |
49 return SupportedSource::kUiLatest; | |
50 case feedback_private::LOG_SOURCE_NONE: | |
51 default: | |
52 NOTREACHED() << "Unknown log source type."; | |
53 return SingleLogSource::SupportedSource::kMessages; | |
54 } | |
55 NOTREACHED(); | |
56 return SingleLogSource::SupportedSource::kMessages; | |
57 } | |
58 | |
59 // SystemLogsResponse is a map of strings -> strings. The map value has the | 41 // SystemLogsResponse is a map of strings -> strings. The map value has the |
60 // actual log contents, a string containing all lines, separated by newlines. | 42 // actual log contents, a string containing all lines, separated by newlines. |
61 // This function extracts the individual lines and converts them into a vector | 43 // This function extracts the individual lines and converts them into a vector |
62 // of strings, each string containing a single line. | 44 // of strings, each string containing a single line. |
63 void GetLogLinesFromSystemLogsResponse(const SystemLogsResponse& response, | 45 void GetLogLinesFromSystemLogsResponse(const SystemLogsResponse& response, |
64 std::vector<std::string>* log_lines) { | 46 std::vector<std::string>* log_lines) { |
65 for (const std::pair<std::string, std::string>& pair : response) { | 47 for (const std::pair<std::string, std::string>& pair : response) { |
66 std::vector<std::string> new_lines = base::SplitString( | 48 std::vector<std::string> new_lines = base::SplitString( |
67 pair.second, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | 49 pair.second, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
68 log_lines->reserve(log_lines->size() + new_lines.size()); | 50 log_lines->reserve(log_lines->size() + new_lines.size()); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 } | 130 } |
149 | 131 |
150 callback.Run(result); | 132 callback.Run(result); |
151 } | 133 } |
152 | 134 |
153 void LogSourceAccessManager::RemoveSource(const SourceAndExtension& key) { | 135 void LogSourceAccessManager::RemoveSource(const SourceAndExtension& key) { |
154 sources_.erase(key); | 136 sources_.erase(key); |
155 } | 137 } |
156 | 138 |
157 LogSourceAccessManager::SourceAndExtension::SourceAndExtension( | 139 LogSourceAccessManager::SourceAndExtension::SourceAndExtension( |
158 api::feedback_private::LogSource source, | 140 feedback_private::LogSource source, |
159 const std::string& extension_id) | 141 const std::string& extension_id) |
160 : source(source), extension_id(extension_id) {} | 142 : source(source), extension_id(extension_id) {} |
161 | 143 |
162 int LogSourceAccessManager::CreateResource(const SourceAndExtension& key) { | 144 int LogSourceAccessManager::CreateResource(const SourceAndExtension& key) { |
163 // Enforce the rules: Do not create a new SingleLogSource if there was already | 145 // Enforce the rules: Do not create a new SingleLogSource if there was already |
164 // one created for |key|. | 146 // one created for |key|. |
165 if (sources_.find(key) != sources_.end()) | 147 if (sources_.find(key) != sources_.end()) |
166 return 0; | 148 return 0; |
167 | 149 |
168 // Enforce the rules: Do not create too many SingleLogSource objects to read | 150 // Enforce the rules: Do not create too many SingleLogSource objects to read |
169 // from a source, even if they are from different extensions. | 151 // from a source, even if they are from different extensions. |
170 if (GetNumActiveResourcesForSource(key.source) >= kMaxReadersPerSource) | 152 if (GetNumActiveResourcesForSource(key.source) >= kMaxReadersPerSource) |
171 return 0; | 153 return 0; |
172 | 154 |
173 std::unique_ptr<LogSourceResource> new_resource = | 155 std::unique_ptr<LogSourceResource> new_resource = |
174 base::MakeUnique<LogSourceResource>( | 156 base::MakeUnique<LogSourceResource>( |
175 key.extension_id, | 157 key.extension_id, |
176 SingleLogSourceFactory::CreateSingleLogSource( | 158 SingleLogSourceFactory::CreateSingleLogSource(key.source), |
177 GetSupportedSourceType(key.source)), | |
178 base::Bind(&LogSourceAccessManager::RemoveSource, | 159 base::Bind(&LogSourceAccessManager::RemoveSource, |
179 weak_factory_.GetWeakPtr(), key)); | 160 weak_factory_.GetWeakPtr(), key)); |
180 | 161 |
181 int id = ApiResourceManager<LogSourceResource>::Get(context_)->Add( | 162 int id = ApiResourceManager<LogSourceResource>::Get(context_)->Add( |
182 new_resource.release()); | 163 new_resource.release()); |
183 sources_[key] = id; | 164 sources_[key] = id; |
184 | 165 |
185 return id; | 166 return id; |
186 } | 167 } |
187 | 168 |
(...skipping 11 matching lines...) Expand all Loading... |
199 base::TimeTicks LogSourceAccessManager::GetLastExtensionAccessTime( | 180 base::TimeTicks LogSourceAccessManager::GetLastExtensionAccessTime( |
200 const SourceAndExtension& key) const { | 181 const SourceAndExtension& key) const { |
201 const auto iter = last_access_times_.find(key); | 182 const auto iter = last_access_times_.find(key); |
202 if (iter == last_access_times_.end()) | 183 if (iter == last_access_times_.end()) |
203 return base::TimeTicks(); | 184 return base::TimeTicks(); |
204 | 185 |
205 return iter->second; | 186 return iter->second; |
206 } | 187 } |
207 | 188 |
208 size_t LogSourceAccessManager::GetNumActiveResourcesForSource( | 189 size_t LogSourceAccessManager::GetNumActiveResourcesForSource( |
209 api::feedback_private::LogSource source) const { | 190 feedback_private::LogSource source) const { |
210 size_t count = 0; | 191 size_t count = 0; |
211 // The stored entries are sorted first by source type, then by extension ID. | 192 // The stored entries are sorted first by source type, then by extension ID. |
212 // We can take advantage of this fact to avoid iterating over all elements. | 193 // We can take advantage of this fact to avoid iterating over all elements. |
213 // Instead start from the first element that matches |source|, and end at the | 194 // Instead start from the first element that matches |source|, and end at the |
214 // first element that does not match |source| anymore. | 195 // first element that does not match |source| anymore. |
215 for (auto iter = sources_.lower_bound(SourceAndExtension(source, "")); | 196 for (auto iter = sources_.lower_bound(SourceAndExtension(source, "")); |
216 iter != sources_.end() && iter->first.source == source; ++iter) { | 197 iter != sources_.end() && iter->first.source == source; ++iter) { |
217 ++count; | 198 ++count; |
218 } | 199 } |
219 return count; | 200 return count; |
220 } | 201 } |
221 | 202 |
222 } // namespace extensions | 203 } // namespace extensions |
OLD | NEW |