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