Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Side by Side Diff: chrome/browser/extensions/api/feedback_private/feedback_private_api.cc

Issue 2840103002: Add new API function: feedbackPrivate.readLogSource (Closed)
Patch Set: Add API unit test; create CrOS-specific test module; minor fixes/refactors Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/feedback_private_api.h" 5 #include "chrome/browser/extensions/api/feedback_private/feedback_private_api.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 #include <vector> 10 #include <vector>
(...skipping 18 matching lines...) Expand all
29 #include "chrome/grit/generated_resources.h" 29 #include "chrome/grit/generated_resources.h"
30 #include "components/feedback/tracing_manager.h" 30 #include "components/feedback/tracing_manager.h"
31 #include "components/signin/core/browser/signin_manager.h" 31 #include "components/signin/core/browser/signin_manager.h"
32 #include "components/strings/grit/components_strings.h" 32 #include "components/strings/grit/components_strings.h"
33 #include "extensions/browser/event_router.h" 33 #include "extensions/browser/event_router.h"
34 #include "ui/base/l10n/l10n_util.h" 34 #include "ui/base/l10n/l10n_util.h"
35 #include "ui/base/webui/web_ui_util.h" 35 #include "ui/base/webui/web_ui_util.h"
36 #include "url/url_util.h" 36 #include "url/url_util.h"
37 37
38 #if defined(OS_CHROMEOS) 38 #if defined(OS_CHROMEOS)
39 #include "base/strings/string_split.h"
39 #include "chrome/browser/chromeos/arc/arc_util.h" 40 #include "chrome/browser/chromeos/arc/arc_util.h"
41 #include "chrome/browser/extensions/api/feedback_private/log_source_resource.h"
42 #include "chrome/browser/extensions/api/feedback_private/single_log_source_facto ry.h"
43 #include "extensions/browser/api/api_resource_manager.h"
40 #endif // defined(OS_CHROMEOS) 44 #endif // defined(OS_CHROMEOS)
41 45
42 #if defined(OS_WIN) 46 #if defined(OS_WIN)
43 #include "base/feature_list.h" 47 #include "base/feature_list.h"
44 #include "chrome/browser/safe_browsing/chrome_cleaner/reporter_runner_win.h" 48 #include "chrome/browser/safe_browsing/chrome_cleaner/reporter_runner_win.h"
45 #endif 49 #endif
46 50
47 using extensions::api::feedback_private::SystemInformation; 51 using extensions::api::feedback_private::SystemInformation;
48 using feedback::FeedbackData; 52 using feedback::FeedbackData;
49 53
(...skipping 25 matching lines...) Expand all
75 using feedback_private::SystemInformation; 79 using feedback_private::SystemInformation;
76 using feedback_private::FeedbackInfo; 80 using feedback_private::FeedbackInfo;
77 using feedback_private::FeedbackFlow; 81 using feedback_private::FeedbackFlow;
78 82
79 using SystemInformationList = 83 using SystemInformationList =
80 std::vector<api::feedback_private::SystemInformation>; 84 std::vector<api::feedback_private::SystemInformation>;
81 85
82 static base::LazyInstance<BrowserContextKeyedAPIFactory<FeedbackPrivateAPI>>:: 86 static base::LazyInstance<BrowserContextKeyedAPIFactory<FeedbackPrivateAPI>>::
83 DestructorAtExit g_factory = LAZY_INSTANCE_INITIALIZER; 87 DestructorAtExit g_factory = LAZY_INSTANCE_INITIALIZER;
84 88
89 #if defined(OS_CHROMEOS)
90 using feedback_private::LogSource;
91 using SingleLogSource = system_logs::SingleLogSource;
92 using SupportedSource = system_logs::SingleLogSource::SupportedSource;
93
94 // Converts from feedback_private::LogSource to SupportedSource.
95 SupportedSource GetSupportedSourceType(LogSource source) {
96 switch (source) {
97 case feedback_private::LOG_SOURCE_MESSAGES:
98 return SupportedSource::kMessages;
99 case feedback_private::LOG_SOURCE_UI_LATEST:
100 return SupportedSource::kUiLatest;
101 case feedback_private::LOG_SOURCE_NONE:
102 DLOG(FATAL) << "Invalid log source type requested.";
103 return SingleLogSource::SupportedSource::kMessages;
104 }
105 }
106 #endif // defined(OS_CHROMEOS)
107
85 // static 108 // static
86 BrowserContextKeyedAPIFactory<FeedbackPrivateAPI>* 109 BrowserContextKeyedAPIFactory<FeedbackPrivateAPI>*
87 FeedbackPrivateAPI::GetFactoryInstance() { 110 FeedbackPrivateAPI::GetFactoryInstance() {
88 return g_factory.Pointer(); 111 return g_factory.Pointer();
89 } 112 }
90 113
91 FeedbackPrivateAPI::FeedbackPrivateAPI(content::BrowserContext* context) 114 FeedbackPrivateAPI::FeedbackPrivateAPI(content::BrowserContext* context)
92 : browser_context_(context), service_(new FeedbackService()) { 115 : browser_context_(context),
93 } 116 service_(new FeedbackService()),
117 log_source_access_manager_(new LogSourceAccessManager) {}
94 118
95 FeedbackPrivateAPI::~FeedbackPrivateAPI() { 119 FeedbackPrivateAPI::~FeedbackPrivateAPI() {
96 delete service_; 120 delete service_;
97 service_ = NULL; 121 service_ = NULL;
98 } 122 }
99 123
100 FeedbackService* FeedbackPrivateAPI::GetService() const { 124 FeedbackService* FeedbackPrivateAPI::GetService() const {
101 return service_; 125 return service_;
102 } 126 }
103 127
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
252 sys_info_entry.key = std::move(itr.first); 276 sys_info_entry.key = std::move(itr.first);
253 sys_info_entry.value = std::move(itr.second); 277 sys_info_entry.value = std::move(itr.second);
254 sys_info_list.emplace_back(std::move(sys_info_entry)); 278 sys_info_list.emplace_back(std::move(sys_info_entry));
255 } 279 }
256 } 280 }
257 281
258 Respond(ArgumentList( 282 Respond(ArgumentList(
259 feedback_private::GetSystemInformation::Results::Create(sys_info_list))); 283 feedback_private::GetSystemInformation::Results::Create(sys_info_list)));
260 } 284 }
261 285
286 ExtensionFunction::ResponseAction FeedbackPrivateReadLogSourceFunction::Run() {
287 #if defined(OS_CHROMEOS)
288 using Params = feedback_private::ReadLogSource::Params;
289 std::unique_ptr<Params> api_params = Params::Create(*args_);
290
291 int reader_id =
292 api_params->params.reader_id ? *api_params->params.reader_id : 0;
293 LogSource source = api_params->params.source;
294
295 SingleLogSource* log_source = GetOrCreateSingleLogSource(&reader_id, source);
296 if (!log_source) {
297 return RespondNow(
298 Error("Unable to get existing or create new log source."));
299 }
300
301 // Enforce the rules: rate-limit access to the source from the current
302 // extension. If not enough time has elapsed since the last access, do not
303 // read from the source, but instead return an empty response. From the
304 // caller's perspective, there is no new data. There is no need for the caller
305 // to keep track of the time since last access.
306 LogSourceAccessManager* log_source_manager =
307 FeedbackPrivateAPI::GetFactoryInstance()
308 ->Get(browser_context())
309 ->log_source_access_manager();
310 DCHECK(log_source_manager);
311 if (!log_source_manager->AccessSourceFromExtension(
312 LogSourceAccessManager::SourceAndExtension(source, extension_id()))) {
313 return RespondNow(
314 ArgumentList(feedback_private::ReadLogSource::Results::Create(
315 0, std::vector<std::string>())));
316 }
317
318 // Now we are ready to access the log source.
319 log_source->Fetch(
320 base::Bind(&FeedbackPrivateReadLogSourceFunction::OnCompleted, this,
321 source, reader_id, api_params->params.incremental));
322
323 return RespondLater();
324 #else
325 return RespondNow(Error("API function is not supported."));
326 #endif // defined(OS_CHROMEOS)
327 }
328
329 #if defined(OS_CHROMEOS)
330 SingleLogSource*
331 FeedbackPrivateReadLogSourceFunction::GetOrCreateSingleLogSource(
332 int* reader_id,
333 LogSource source) {
334 ApiResourceManager<LogSourceResource>* resource_manager =
335 ApiResourceManager<LogSourceResource>::Get(browser_context());
336 DCHECK(resource_manager);
337
338 LogSourceResource* resource = nullptr;
339 // If the caller passed in a valid ID, look up the ID in the resource manager.
340 if (*reader_id > 0) {
341 resource = resource_manager->Get(extension_id(), *reader_id);
342 return resource ? resource->GetLogSource() : nullptr;
343 }
344
345 LogSourceAccessManager* log_source_manager =
346 FeedbackPrivateAPI::GetFactoryInstance()
347 ->Get(browser_context())
348 ->log_source_access_manager();
349 DCHECK(log_source_manager);
350
tbarzic 2017/06/01 19:23:45 I'd pass SourceAndExtension as a parameter (you'll
351 LogSourceAccessManager::SourceAndExtension source_and_extension(
352 source, extension_id());
353
354 // If caller passed in |reader_id| == 0, create a new SingleLogSource.
355
356 // Enforce the rules: Do not create a new SingleLogSource if there was
357 // already one created for |source_and_extension|.
358 if (!log_source_manager->AddExtension(source_and_extension))
359 return nullptr;
360
361 SupportedSource source_type = GetSupportedSourceType(source);
362 std::unique_ptr<LogSourceResource> new_resource =
363 base::MakeUnique<LogSourceResource>(
364 extension_id(),
365 SingleLogSourceFactory::CreateSingleLogSource(source_type).release(),
366 log_source_manager->GetUnregisterCallback(source_and_extension));
367 resource = new_resource.get();
368 *reader_id = resource_manager->Add(new_resource.release());
369
370 return resource->GetLogSource();
371 }
372
373 void FeedbackPrivateReadLogSourceFunction::OnCompleted(
374 LogSource source,
375 int reader_id,
376 bool incremental,
377 system_logs::SystemLogsResponse* response) {
378 std::vector<std::string> result;
379 for (const std::pair<std::string, std::string>& pair : *response) {
380 // TODO(sque): Use std::move?
381 std::vector<std::string> new_lines = base::SplitString(
382 pair.second, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
383 result.reserve(result.size() + new_lines.size());
384 result.insert(result.end(), new_lines.begin(), new_lines.end());
385 }
386
387 // If the API call requested a non-incremental access, clean up the
388 // SingleLogSource by removing its API resource.
389 if (!incremental) {
390 ApiResourceManager<LogSourceResource>::Get(browser_context())
391 ->Remove(extension_id(), reader_id);
392 // Must return reader_id=0 when the resource has been deleted.
393 reader_id = 0;
394 }
395
396 Respond(ArgumentList(
397 feedback_private::ReadLogSource::Results::Create(reader_id, result)));
398 }
399
400 #endif // defined(OS_CHROMEOS)
401
262 bool FeedbackPrivateSendFeedbackFunction::RunAsync() { 402 bool FeedbackPrivateSendFeedbackFunction::RunAsync() {
263 std::unique_ptr<feedback_private::SendFeedback::Params> params( 403 std::unique_ptr<feedback_private::SendFeedback::Params> params(
264 feedback_private::SendFeedback::Params::Create(*args_)); 404 feedback_private::SendFeedback::Params::Create(*args_));
265 EXTENSION_FUNCTION_VALIDATE(params); 405 EXTENSION_FUNCTION_VALIDATE(params);
266 406
267 const FeedbackInfo &feedback_info = params->feedback; 407 const FeedbackInfo &feedback_info = params->feedback;
268 408
269 // Populate feedback data. 409 // Populate feedback data.
270 scoped_refptr<FeedbackData> feedback_data(new FeedbackData()); 410 scoped_refptr<FeedbackData> feedback_data(new FeedbackData());
271 feedback_data->set_context(GetProfile()); 411 feedback_data->set_context(GetProfile());
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
358 case feedback_private::SRT_PROMPT_RESULT_CLOSED: 498 case feedback_private::SRT_PROMPT_RESULT_CLOSED:
359 base::RecordAction(base::UserMetricsAction("Feedback.SrtPromptClosed")); 499 base::RecordAction(base::UserMetricsAction("Feedback.SrtPromptClosed"));
360 break; 500 break;
361 default: 501 default:
362 return RespondNow(Error("Invalid arugment.")); 502 return RespondNow(Error("Invalid arugment."));
363 } 503 }
364 return RespondNow(NoArguments()); 504 return RespondNow(NoArguments());
365 } 505 }
366 506
367 } // namespace extensions 507 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698