1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/browser/api/feedback_private/log_source_access_manager.h"
#include <algorithm>
#include <utility>
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/strings/string_split.h"
#include "base/strings/stringprintf.h"
#include "base/task/thread_pool.h"
#include "base/time/default_tick_clock.h"
#include "extensions/browser/api/api_resource_manager.h"
#include "extensions/browser/api/extensions_api_client.h"
#include "extensions/browser/api/feedback_private/feedback_private_delegate.h"
#include "extensions/browser/api/feedback_private/log_source_resource.h"
#include "extensions/common/extension_id.h"
namespace extensions {
namespace {
using LogSource = api::feedback_private::LogSource;
using ReadLogSourceParams = api::feedback_private::ReadLogSourceParams;
using ReadLogSourceResult = api::feedback_private::ReadLogSourceResult;
using SystemLogsResponse = system_logs::SystemLogsResponse;
// Default value of |g_max_num_burst_accesses|.
constexpr int kDefaultMaxNumBurstAccesses = 10;
// The minimum time between consecutive reads of a log source by a particular
// extension.
constexpr base::TimeDelta kDefaultRateLimitingTimeout =
base::Milliseconds(1000);
// Maximum length of a single log line. Longer lines will be truncated.
constexpr int kMaxLogLineLength = 8100;
// Maximum length of a single (multiline) fetch. Larger fetches will be dropped.
// Hotrod polls the logs every 30 seconds, which results in logs up to a few
// hundred kB on startup.
constexpr int kMaxLogFetchLength = 2000000;
// The maximum number of accesses on a single log source that can be allowed
// before the next recharge increment. See access_rate_limiter.h for more info.
int g_max_num_burst_accesses = kDefaultMaxNumBurstAccesses;
// If this is null, then |kDefaultRateLimitingTimeoutMs| is used as the timeout.
const base::TimeDelta* g_rate_limiting_timeout = nullptr;
base::TimeDelta GetMinTimeBetweenReads() {
return g_rate_limiting_timeout ? *g_rate_limiting_timeout
: kDefaultRateLimitingTimeout;
}
// SystemLogsResponse is a map of strings -> strings. The map value has the
// actual log contents, a string containing all lines, separated by newlines.
// This function extracts the individual lines and converts them into a vector
// of strings, each string containing a single line.
void GetLogLinesFromSystemLogsResponse(const SystemLogsResponse& response,
std::vector<std::string>* log_lines) {
const int kTimestampPrefixLength = 27; // yyyy-mm-ddTHH:MM:SS.uuuuuu_
for (const std::pair<const std::string, std::string>& pair : response) {
if (pair.second.size() > kMaxLogFetchLength) {
const std::string error_message =
pair.second.substr(0, kTimestampPrefixLength) +
base::StringPrintf(
"WARNING extensions::LogSourceAccessManager::OnFetchComplete: "
"Dropped lines from log source %s totaling %d chars.",
pair.first.c_str(), pair.second.size());
log_lines->emplace_back(error_message);
continue;
}
std::vector<std::string_view> new_lines = base::SplitStringPiece(
pair.second, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
log_lines->reserve(log_lines->size() + new_lines.size());
for (std::string_view& line : new_lines) {
if (line.size() < kMaxLogLineLength) {
log_lines->emplace_back(line);
} else {
log_lines->emplace_back(std::string(line.substr(0, kMaxLogLineLength)) +
" (Truncated)");
}
}
}
}
// Redacts the strings in |result|.
void RedactResults(
scoped_refptr<redaction::RedactionToolContainer> redactor_container,
ReadLogSourceResult* result) {
redaction::RedactionTool* redactor = redactor_container->Get();
for (std::string& line : result->log_lines)
line = redactor->Redact(line);
}
} // namespace
LogSourceAccessManager::LogSourceAccessManager(content::BrowserContext* context)
: context_(context),
tick_clock_(base::DefaultTickClock::GetInstance()),
task_runner_for_redactor_(base::ThreadPool::CreateSequencedTaskRunner(
// User visible as the feedback_api is used by the Chrome (OS)
// feedback extension while the user may be looking at a spinner.
{base::TaskPriority::USER_VISIBLE,
base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN})),
redactor_container_(
base::MakeRefCounted<redaction::RedactionToolContainer>(
task_runner_for_redactor_,
/* first_party_extension_ids= */ nullptr)) {}
LogSourceAccessManager::~LogSourceAccessManager() = default;
// static
void LogSourceAccessManager::SetMaxNumBurstAccessesForTesting(
int num_accesses) {
g_max_num_burst_accesses = num_accesses;
}
// static
void LogSourceAccessManager::SetRateLimitingTimeoutForTesting(
const base::TimeDelta* timeout) {
g_rate_limiting_timeout = timeout;
}
bool LogSourceAccessManager::FetchFromSource(const ReadLogSourceParams& params,
const ExtensionId& extension_id,
ReadLogSourceCallback callback) {
int requested_resource_id =
params.reader_id ? *params.reader_id : kInvalidResourceId;
ApiResourceManager<LogSourceResource>* resource_manager =
ApiResourceManager<LogSourceResource>::Get(context_);
const ResourceId resource_id =
requested_resource_id != kInvalidResourceId
? requested_resource_id
: CreateResource(params.source, extension_id);
if (resource_id == kInvalidResourceId)
return false;
LogSourceResource* resource =
resource_manager->Get(extension_id, resource_id);
if (!resource)
return false;
// Enforce the rules: rate-limit access to the source from the current reader
// handle. If not enough time has elapsed since the last access, do not read
// from the source, but instead return an empty response. From the caller's
// perspective, there is no new data. There is no need for the caller to keep
// track of the time since last access.
if (!UpdateSourceAccessTime(resource_id)) {
std::move(callback).Run(
std::make_unique<api::feedback_private::ReadLogSourceResult>());
return true;
}
// If the API call requested a non-incremental access, clean up the
// SingleLogSource by removing its API resource. Even if the existing source
// were originally created as incremental, passing in incremental=false on a
// later access indicates that the source should be closed afterwards.
const bool delete_resource_when_done = !params.incremental;
resource->GetLogSource()->Fetch(
base::BindOnce(&LogSourceAccessManager::OnFetchComplete,
weak_factory_.GetWeakPtr(), extension_id, resource_id,
delete_resource_when_done, std::move(callback)));
return true;
}
void LogSourceAccessManager::OnFetchComplete(
const ExtensionId& extension_id,
ResourceId resource_id,
bool delete_resource,
ReadLogSourceCallback callback,
std::unique_ptr<SystemLogsResponse> response) {
auto result = std::make_unique<ReadLogSourceResult>();
// Always return invalid resource ID if there is a cleanup.
result->reader_id = delete_resource ? kInvalidResourceId : resource_id;
GetLogLinesFromSystemLogsResponse(*response, &result->log_lines);
// Retrieve result pointer before the PostTaskAndReply to fix issues with
// an undefined execution order of arguments in a function call
// (std::move(result) being executed before result.get()).
ReadLogSourceResult* result_ptr = result.get();
task_runner_for_redactor_->PostTaskAndReply(
FROM_HERE,
base::BindOnce(RedactResults, redactor_container_,
base::Unretained(result_ptr)),
base::BindOnce(std::move(callback), std::move(result)));
if (delete_resource) {
// This should also remove the entry from |sources_|.
ApiResourceManager<LogSourceResource>::Get(context_)->Remove(extension_id,
resource_id);
}
}
void LogSourceAccessManager::RemoveHandle(ResourceId id) {
auto iter = open_handles_.find(id);
if (iter == open_handles_.end())
return;
const SourceAndExtension& source_and_extension = *iter->second;
const LogSource source = source_and_extension.source;
if (num_readers_per_source_.find(source) != num_readers_per_source_.end())
num_readers_per_source_[source]--;
open_handles_.erase(id);
}
LogSourceAccessManager::SourceAndExtension::SourceAndExtension(
LogSource source,
const ExtensionId& extension_id)
: source(source), extension_id(extension_id) {}
LogSourceAccessManager::ResourceId LogSourceAccessManager::CreateResource(
LogSource source,
const ExtensionId& extension_id) {
// Enforce the rules: Do not create too many SingleLogSource objects to read
// from a source, even if they are from different extensions.
if (GetNumActiveResourcesForSource(source) >= kMaxReadersPerSource)
return kInvalidResourceId;
auto new_resource = std::make_unique<LogSourceResource>(
extension_id, ExtensionsAPIClient::Get()
->GetFeedbackPrivateDelegate()
->CreateSingleLogSource(source));
auto* resource_manager = ApiResourceManager<LogSourceResource>::Get(context_);
// Create an ID for the resource using the API Resource Manager, but don't
// release ownership of it until a valid ID has been secured.
ResourceId resource_id = resource_manager->Add(new_resource.get());
if (resource_id == kInvalidResourceId)
return kInvalidResourceId;
if (base::Contains(open_handles_, resource_id)) {
return kInvalidResourceId;
}
// Now that |resource_id| has been determined to be valid, release ownership
// of the LogSourceResource, which is now owned by the API resource manager.
new_resource.release();
// The resource ID isn't known until |new_resource| is added to the API
// Resource Manager, but it needs to be passed into the resource afterward, so
// that the resource can unregister itself from LogSourceAccessManager. It is
// passed in as part of a callback.
resource_manager->Get(extension_id, resource_id)
->set_unregister_callback(
base::BindOnce(&LogSourceAccessManager::RemoveHandle,
weak_factory_.GetWeakPtr(), resource_id));
open_handles_.emplace(
resource_id, std::make_unique<SourceAndExtension>(source, extension_id));
num_readers_per_source_[source]++;
return resource_id;
}
bool LogSourceAccessManager::UpdateSourceAccessTime(ResourceId id) {
auto iter = open_handles_.find(id);
if (iter == open_handles_.end())
return false;
const SourceAndExtension& key = *iter->second;
if (rate_limiters_.find(key) == rate_limiters_.end()) {
rate_limiters_.emplace(key, std::make_unique<AccessRateLimiter>(
g_max_num_burst_accesses,
GetMinTimeBetweenReads(), tick_clock_));
}
return rate_limiters_[key]->AttemptAccess();
}
size_t LogSourceAccessManager::GetNumActiveResourcesForSource(
LogSource source) const {
auto iter = num_readers_per_source_.find(source);
if (iter == num_readers_per_source_.end())
return 0;
return iter->second;
}
} // namespace extensions
|