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

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: Addressed tbarzic's comments from Patch Set 4 and 5 Created 3 years, 7 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/chromeos/system_logs/single_log_source.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 LogSourceResource* resource =
296 GetOrCreateLogSourceResource(&reader_id, source);
297 if (!resource)
298 return RespondNow(
299 Error("Unable to get existing or create new log source."));
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 resource->GetLogSource()->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 LogSourceResource*
331 FeedbackPrivateReadLogSourceFunction::GetOrCreateLogSourceResource(
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 return resource_manager->Get(extension_id(), *reader_id);
342
343 LogSourceAccessManager* log_source_manager =
344 FeedbackPrivateAPI::GetFactoryInstance()
345 ->Get(browser_context())
346 ->log_source_access_manager();
347 DCHECK(log_source_manager);
348
349 LogSourceAccessManager::SourceAndExtension source_and_extension(
350 source, extension_id());
351
352 // If caller passed in |reader_id| == 0, create a new SingleLogSource.
353
354 // Enforce the rules: Do not create a new SingleLogSource if there was
355 // already one created for |source_and_extension|.
356 if (!log_source_manager->AddExtension(source_and_extension))
357 return nullptr;
358
359 SupportedSource source_type = GetSupportedSourceType(source);
360 std::unique_ptr<LogSourceResource> new_resource =
361 base::MakeUnique<LogSourceResource>(
362 extension_id(),
363 SingleLogSourceFactory::CreateSingleLogSource(source_type).release(),
364 log_source_manager->GetUnregisterCallback(source_and_extension));
365 resource = new_resource.get();
366
367 *reader_id = resource_manager->Add(new_resource.release());
368
369 return resource;
370 }
371
372 void FeedbackPrivateReadLogSourceFunction::OnCompleted(
373 LogSource source,
374 int reader_id,
375 bool incremental,
376 system_logs::SystemLogsResponse* response) {
377 std::vector<std::string> result;
378 for (const std::pair<std::string, std::string>& pair : *response) {
379 // TODO(sque): Use std::move?
380 std::vector<std::string> new_lines = base::SplitString(
381 pair.second, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
382 result.reserve(result.size() + new_lines.size());
383 result.insert(result.end(), new_lines.begin(), new_lines.end());
384 }
385
386 // If the API call requested a non-incremental access, clean up the
387 // SingleLogSource by removing its API resource.
388 if (!incremental) {
389 ApiResourceManager<LogSourceResource>::Get(browser_context())
390 ->Remove(extension_id(), reader_id);
391 // Must return reader_id=0 when the resource has been deleted.
392 reader_id = 0;
393 }
394
395 Respond(ArgumentList(
396 feedback_private::ReadLogSource::Results::Create(reader_id, result)));
397 }
398
399 #endif // defined(OS_CHROMEOS)
400
262 bool FeedbackPrivateSendFeedbackFunction::RunAsync() { 401 bool FeedbackPrivateSendFeedbackFunction::RunAsync() {
263 std::unique_ptr<feedback_private::SendFeedback::Params> params( 402 std::unique_ptr<feedback_private::SendFeedback::Params> params(
264 feedback_private::SendFeedback::Params::Create(*args_)); 403 feedback_private::SendFeedback::Params::Create(*args_));
265 EXTENSION_FUNCTION_VALIDATE(params); 404 EXTENSION_FUNCTION_VALIDATE(params);
266 405
267 const FeedbackInfo &feedback_info = params->feedback; 406 const FeedbackInfo &feedback_info = params->feedback;
268 407
269 // Populate feedback data. 408 // Populate feedback data.
270 scoped_refptr<FeedbackData> feedback_data(new FeedbackData()); 409 scoped_refptr<FeedbackData> feedback_data(new FeedbackData());
271 feedback_data->set_context(GetProfile()); 410 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: 497 case feedback_private::SRT_PROMPT_RESULT_CLOSED:
359 base::RecordAction(base::UserMetricsAction("Feedback.SrtPromptClosed")); 498 base::RecordAction(base::UserMetricsAction("Feedback.SrtPromptClosed"));
360 break; 499 break;
361 default: 500 default:
362 return RespondNow(Error("Invalid arugment.")); 501 return RespondNow(Error("Invalid arugment."));
363 } 502 }
364 return RespondNow(NoArguments()); 503 return RespondNow(NoArguments());
365 } 504 }
366 505
367 } // namespace extensions 506 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698