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 // 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 default: | |
| 50 NOTREACHED() << "Unknown log source type."; | |
| 51 return SingleLogSource::SupportedSource::kMessages; | |
| 52 } | |
| 53 NOTREACHED(); | |
| 54 return SingleLogSource::SupportedSource::kMessages; | |
| 55 } | |
| 56 | |
| 57 // SystemLogsResponse is a map of strings -> strings. The map value has the | |
| 58 // actual log contents, a string containing all lines, separated by newlines. | |
| 59 // This function extracts the individual lines and converts them into a vector | |
| 60 // of strings, each string containing a single line. | |
| 61 void GetLogLinesFromSystemLogsResponse(const SystemLogsResponse& response, | |
| 62 std::vector<std::string>* log_lines) { | |
| 63 for (const std::pair<std::string, std::string>& pair : response) { | |
| 64 // TODO(sque): Use std::move? | |
| 65 std::vector<std::string> new_lines = base::SplitString( | |
| 66 pair.second, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | |
| 67 log_lines->reserve(log_lines->size() + new_lines.size()); | |
| 68 log_lines->insert(log_lines->end(), new_lines.begin(), new_lines.end()); | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 } // namespace | |
| 73 | |
| 74 LogSourceAccessManager::LogSourceAccessManager(content::BrowserContext* context) | |
| 75 : context_(context), | |
| 76 tick_clock_(new base::DefaultTickClock), | |
| 77 weak_factory_(this) {} | |
| 78 | |
| 79 LogSourceAccessManager::~LogSourceAccessManager() {} | |
| 80 | |
| 81 // static | |
| 82 void LogSourceAccessManager::SetRateLimitingTimeoutForTesting( | |
| 83 const base::TimeDelta* timeout) { | |
| 84 g_rate_limiting_timeout = timeout; | |
| 85 } | |
| 86 | |
| 87 bool LogSourceAccessManager::FetchFromSource( | |
| 88 const feedback_private::ReadLogSourceParams& params, | |
| 89 const std::string& extension_id, | |
| 90 const ReadLogSourceCallback& callback) { | |
| 91 SourceAndExtension key = SourceAndExtension(params.source, extension_id); | |
| 92 int requested_resource_id = params.reader_id ? *params.reader_id : 0; | |
| 93 int resource_id = | |
| 94 requested_resource_id > 0 ? requested_resource_id : CreateResource(key); | |
| 95 if (resource_id <= 0) | |
| 96 return false; | |
| 97 | |
| 98 ApiResourceManager<LogSourceResource>* resource_manager = | |
| 99 ApiResourceManager<LogSourceResource>::Get(context_); | |
| 100 LogSourceResource* resource = | |
| 101 resource_manager->Get(extension_id, resource_id); | |
| 102 if (!resource) | |
| 103 return false; | |
| 104 | |
| 105 // Enforce the rules: rate-limit access to the source from the current | |
| 106 // extension. If not enough time has elapsed since the last access, do not | |
| 107 // read from the source, but instead return an empty response. From the | |
| 108 // caller's perspective, there is no new data. There is no need for the caller | |
| 109 // to keep track of the time since last access. | |
| 110 if (!UpdateSourceAccessTime(key)) { | |
| 111 feedback_private::ReadLogSourceResult empty_result; | |
| 112 callback.Run(empty_result); | |
| 113 return true; | |
| 114 } | |
| 115 | |
| 116 // If the API call requested a non-incremental access, clean up the | |
| 117 // SingleLogSource by removing its API resource. Even if the existing source | |
| 118 // were originally created as incremental, passing in incremental=false on a | |
| 119 // later access indicates that the source should be closed afterwards. | |
| 120 bool delete_source_when_done = !params.incremental; | |
| 121 | |
| 122 resource->GetLogSource()->Fetch(base::Bind( | |
| 123 &LogSourceAccessManager::OnFetchComplete, weak_factory_.GetWeakPtr(), key, | |
| 124 delete_source_when_done, callback)); | |
| 125 return true; | |
| 126 } | |
| 127 | |
| 128 void LogSourceAccessManager::OnFetchComplete( | |
| 129 const SourceAndExtension& key, | |
| 130 bool delete_source, | |
|
tbarzic
2017/06/07 17:58:38
nit: rename to delete_resource
Simon Que
2017/06/07 18:25:59
Done.
| |
| 131 const ReadLogSourceCallback& callback, | |
| 132 SystemLogsResponse* response) { | |
| 133 int resource_id = 0; | |
| 134 const auto iter = sources_.find(key); | |
| 135 if (iter != sources_.end()) | |
| 136 resource_id = iter->second; | |
| 137 | |
| 138 feedback_private::ReadLogSourceResult result; | |
| 139 // Always return reader_id=0 if there is a cleanup. | |
| 140 result.reader_id = delete_source ? 0 : resource_id; | |
| 141 | |
| 142 GetLogLinesFromSystemLogsResponse(*response, &result.log_lines); | |
| 143 if (delete_source) { | |
| 144 // This should also remove the entry from |sources_|. | |
| 145 ApiResourceManager<LogSourceResource>::Get(context_)->Remove( | |
| 146 key.extension_id, resource_id); | |
| 147 } | |
| 148 | |
| 149 callback.Run(result); | |
| 150 } | |
| 151 | |
| 152 void LogSourceAccessManager::RemoveSource(const SourceAndExtension& key) { | |
| 153 auto iter = sources_.find(key); | |
|
tbarzic
2017/06/07 17:58:38
this can be just:
sources_.erase(key);
Simon Que
2017/06/07 18:25:58
Done.
| |
| 154 if (iter != sources_.end()) | |
| 155 sources_.erase(iter); | |
| 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 std::unique_ptr<LogSourceResource> new_resource = | |
| 170 base::MakeUnique<LogSourceResource>( | |
| 171 key.extension_id, | |
| 172 SingleLogSourceFactory::CreateSingleLogSource( | |
| 173 GetSupportedSourceType(key.source)), | |
| 174 base::Bind(&LogSourceAccessManager::RemoveSource, | |
| 175 weak_factory_.GetWeakPtr(), key)); | |
| 176 | |
| 177 int id = ApiResourceManager<LogSourceResource>::Get(context_)->Add( | |
| 178 new_resource.release()); | |
| 179 sources_[key] = id; | |
| 180 | |
| 181 return id; | |
| 182 } | |
| 183 | |
| 184 bool LogSourceAccessManager::UpdateSourceAccessTime( | |
| 185 const SourceAndExtension& key) { | |
| 186 base::TimeTicks last = GetLastExtensionAccessTime(key); | |
| 187 base::TimeTicks now = tick_clock_->NowTicks(); | |
| 188 if (!last.is_null() && now < last + GetMinTimeBetweenReads()) { | |
| 189 return false; | |
| 190 } | |
| 191 last_access_times_[key] = now; | |
| 192 return true; | |
| 193 } | |
| 194 | |
| 195 base::TimeTicks LogSourceAccessManager::GetLastExtensionAccessTime( | |
| 196 const SourceAndExtension& key) const { | |
| 197 const auto iter = last_access_times_.find(key); | |
| 198 if (iter == last_access_times_.end()) | |
| 199 return base::TimeTicks(); | |
| 200 | |
| 201 return iter->second; | |
| 202 } | |
| 203 | |
| 204 } // namespace extensions | |
| OLD | NEW |