Chromium Code Reviews| 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 #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 const int kMaxReadersPerSource = 10; | |
| 29 | |
| 30 // The minimum time between consecutive reads of a log source by a particular | |
| 31 // extension. | |
| 32 const int kDefaultRateLimitingTimeoutMs = 1000; | |
| 33 | |
| 34 // If this is null, then |kDefaultRateLimitingTimeoutMs| is used as the timeout. | |
| 35 const base::TimeDelta* g_rate_limiting_timeout = nullptr; | |
| 36 | |
| 37 base::TimeDelta GetMinTimeBetweenReads() { | |
| 38 return g_rate_limiting_timeout | |
| 39 ? *g_rate_limiting_timeout | |
| 40 : base::TimeDelta::FromMilliseconds(kDefaultRateLimitingTimeoutMs); | |
| 41 } | |
| 42 | |
| 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 | |
| 60 // actual log contents, a string containing all lines, separated by newlines. | |
| 61 // This function extracts the individual lines and converts them into a vector | |
| 62 // of strings, each string containing a single line. | |
| 63 void GetLogLinesFromSystemLogsResponse(const SystemLogsResponse& response, | |
| 64 std::vector<std::string>* log_lines) { | |
| 65 for (const std::pair<std::string, std::string>& pair : response) { | |
| 66 // TODO(sque): Use std::move? | |
|
Simon Que
2017/06/08 22:32:13
I measured this -- the difference was about 160 ms
| |
| 67 std::vector<std::string> new_lines = base::SplitString( | |
| 68 pair.second, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | |
| 69 log_lines->reserve(log_lines->size() + new_lines.size()); | |
| 70 log_lines->insert(log_lines->end(), new_lines.begin(), new_lines.end()); | |
| 71 } | |
| 72 } | |
| 73 | |
| 74 } // namespace | |
| 75 | |
| 76 LogSourceAccessManager::LogSourceAccessManager(content::BrowserContext* context) | |
| 77 : context_(context), | |
| 78 tick_clock_(new base::DefaultTickClock), | |
| 79 weak_factory_(this) {} | |
| 80 | |
| 81 LogSourceAccessManager::~LogSourceAccessManager() {} | |
| 82 | |
| 83 // static | |
| 84 void LogSourceAccessManager::SetRateLimitingTimeoutForTesting( | |
| 85 const base::TimeDelta* timeout) { | |
| 86 g_rate_limiting_timeout = timeout; | |
| 87 } | |
| 88 | |
| 89 bool LogSourceAccessManager::FetchFromSource( | |
| 90 const feedback_private::ReadLogSourceParams& params, | |
| 91 const std::string& extension_id, | |
| 92 const ReadLogSourceCallback& callback) { | |
| 93 SourceAndExtension key = SourceAndExtension(params.source, extension_id); | |
| 94 int requested_resource_id = params.reader_id ? *params.reader_id : 0; | |
| 95 int resource_id = | |
| 96 requested_resource_id > 0 ? requested_resource_id : CreateResource(key); | |
| 97 if (resource_id <= 0) | |
| 98 return false; | |
| 99 | |
| 100 ApiResourceManager<LogSourceResource>* resource_manager = | |
| 101 ApiResourceManager<LogSourceResource>::Get(context_); | |
| 102 LogSourceResource* resource = | |
| 103 resource_manager->Get(extension_id, resource_id); | |
| 104 if (!resource) | |
| 105 return false; | |
| 106 | |
| 107 // Enforce the rules: rate-limit access to the source from the current | |
| 108 // extension. If not enough time has elapsed since the last access, do not | |
| 109 // read from the source, but instead return an empty response. From the | |
| 110 // caller's perspective, there is no new data. There is no need for the caller | |
| 111 // to keep track of the time since last access. | |
| 112 if (!UpdateSourceAccessTime(key)) { | |
| 113 feedback_private::ReadLogSourceResult empty_result; | |
| 114 callback.Run(empty_result); | |
| 115 return true; | |
| 116 } | |
| 117 | |
| 118 // If the API call requested a non-incremental access, clean up the | |
| 119 // SingleLogSource by removing its API resource. Even if the existing source | |
| 120 // were originally created as incremental, passing in incremental=false on a | |
| 121 // later access indicates that the source should be closed afterwards. | |
| 122 bool delete_resource_when_done = !params.incremental; | |
| 123 | |
| 124 resource->GetLogSource()->Fetch(base::Bind( | |
| 125 &LogSourceAccessManager::OnFetchComplete, weak_factory_.GetWeakPtr(), key, | |
| 126 delete_resource_when_done, callback)); | |
| 127 return true; | |
| 128 } | |
| 129 | |
| 130 void LogSourceAccessManager::OnFetchComplete( | |
| 131 const SourceAndExtension& key, | |
| 132 bool delete_resource, | |
| 133 const ReadLogSourceCallback& callback, | |
| 134 SystemLogsResponse* response) { | |
| 135 int resource_id = 0; | |
| 136 const auto iter = sources_.find(key); | |
| 137 if (iter != sources_.end()) | |
| 138 resource_id = iter->second; | |
| 139 | |
| 140 feedback_private::ReadLogSourceResult result; | |
| 141 // Always return reader_id=0 if there is a cleanup. | |
| 142 result.reader_id = delete_resource ? 0 : resource_id; | |
| 143 | |
| 144 GetLogLinesFromSystemLogsResponse(*response, &result.log_lines); | |
| 145 if (delete_resource) { | |
| 146 // This should also remove the entry from |sources_|. | |
| 147 ApiResourceManager<LogSourceResource>::Get(context_)->Remove( | |
| 148 key.extension_id, resource_id); | |
| 149 } | |
| 150 | |
| 151 callback.Run(result); | |
| 152 } | |
| 153 | |
| 154 void LogSourceAccessManager::RemoveSource(const SourceAndExtension& key) { | |
| 155 sources_.erase(key); | |
| 156 } | |
| 157 | |
| 158 LogSourceAccessManager::SourceAndExtension::SourceAndExtension( | |
| 159 api::feedback_private::LogSource source, | |
| 160 const std::string& extension_id) | |
| 161 : source(source), extension_id(extension_id) {} | |
| 162 | |
| 163 int LogSourceAccessManager::CreateResource(const SourceAndExtension& key) { | |
| 164 // Enforce the rules: Do not create a new SingleLogSource if there was already | |
| 165 // one created for |key|. | |
| 166 if (sources_.find(key) != sources_.end()) | |
| 167 return 0; | |
| 168 | |
| 169 // Enforce the rules: Do not create too many SingleLogSource objects to read | |
| 170 // from a source, even if they are from different extensions. | |
| 171 if (GetNumActiveResourcesForSource(key.source) >= kMaxReadersPerSource) | |
| 172 return 0; | |
| 173 | |
| 174 std::unique_ptr<LogSourceResource> new_resource = | |
| 175 base::MakeUnique<LogSourceResource>( | |
| 176 key.extension_id, | |
| 177 SingleLogSourceFactory::CreateSingleLogSource( | |
| 178 GetSupportedSourceType(key.source)), | |
| 179 base::Bind(&LogSourceAccessManager::RemoveSource, | |
| 180 weak_factory_.GetWeakPtr(), key)); | |
| 181 | |
| 182 int id = ApiResourceManager<LogSourceResource>::Get(context_)->Add( | |
| 183 new_resource.release()); | |
| 184 sources_[key] = id; | |
| 185 | |
| 186 return id; | |
| 187 } | |
| 188 | |
| 189 bool LogSourceAccessManager::UpdateSourceAccessTime( | |
| 190 const SourceAndExtension& key) { | |
| 191 base::TimeTicks last = GetLastExtensionAccessTime(key); | |
| 192 base::TimeTicks now = tick_clock_->NowTicks(); | |
| 193 if (!last.is_null() && now < last + GetMinTimeBetweenReads()) { | |
| 194 return false; | |
| 195 } | |
| 196 last_access_times_[key] = now; | |
| 197 return true; | |
| 198 } | |
| 199 | |
| 200 base::TimeTicks LogSourceAccessManager::GetLastExtensionAccessTime( | |
| 201 const SourceAndExtension& key) const { | |
| 202 const auto iter = last_access_times_.find(key); | |
| 203 if (iter == last_access_times_.end()) | |
| 204 return base::TimeTicks(); | |
| 205 | |
| 206 return iter->second; | |
| 207 } | |
| 208 | |
| 209 size_t LogSourceAccessManager::GetNumActiveResourcesForSource( | |
| 210 api::feedback_private::LogSource source) const { | |
| 211 size_t count = 0; | |
| 212 // The stored entries are sorted first by source type, then by extension ID. | |
| 213 // We can take advantage of this fact to avoid iterating over all elements. | |
| 214 // Instead start from the first element that matches |source|, and end at the | |
| 215 // first element that does not match |source| anymore. | |
| 216 for (auto iter = sources_.lower_bound(SourceAndExtension(source, "")); | |
| 217 iter != sources_.end() && iter->first.source == source; ++iter) { | |
| 218 ++count; | |
| 219 } | |
| 220 return count; | |
| 221 } | |
| 222 | |
| 223 } // namespace extensions | |
| OLD | NEW |