Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/ui/webui/task_scheduler_internals/task_scheduler_intern als_ui.h" | 5 #include "chrome/browser/ui/webui/task_scheduler_internals/task_scheduler_intern als_ui.h" |
| 6 | 6 |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/memory/ptr_util.h" | |
| 11 #include "base/metrics/histogram_base.h" | |
| 12 #include "base/metrics/histogram_samples.h" | |
| 7 #include "base/task_scheduler/task_scheduler.h" | 13 #include "base/task_scheduler/task_scheduler.h" |
| 14 #include "base/values.h" | |
| 8 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/common/url_constants.h" | 16 #include "chrome/common/url_constants.h" |
| 10 #include "chrome/grit/task_scheduler_internals_resources.h" | 17 #include "chrome/grit/task_scheduler_internals_resources.h" |
| 11 #include "content/public/browser/web_ui_data_source.h" | 18 #include "content/public/browser/web_ui_data_source.h" |
| 19 #include "content/public/browser/web_ui_message_handler.h" | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 std::unique_ptr<base::Value> SnapshotHistogramToValue( | |
| 24 const base::HistogramBase* histogram) { | |
| 25 std::unique_ptr<base::ListValue> values = | |
| 26 base::MakeUnique<base::ListValue>(); | |
| 27 | |
| 28 std::unique_ptr<base::HistogramSamples> samples = | |
| 29 histogram->SnapshotSamples(); | |
| 30 std::unique_ptr<base::SampleCountIterator> iterator = samples->Iterator(); | |
| 31 while (!iterator->Done()) { | |
| 32 base::HistogramBase::Sample min; | |
| 33 base::HistogramBase::Sample max; | |
| 34 base::HistogramBase::Count count; | |
| 35 iterator->Get(&min, &max, &count); | |
| 36 | |
| 37 std::unique_ptr<base::DictionaryValue> bucket = | |
| 38 base::MakeUnique<base::DictionaryValue>(); | |
| 39 bucket->SetInteger("min", min); | |
| 40 bucket->SetInteger("max", max); | |
| 41 bucket->SetInteger("count", count); | |
| 42 | |
| 43 values->Append(std::move(bucket)); | |
| 44 iterator->Next(); | |
| 45 } | |
| 46 return values; | |
| 47 } | |
| 48 | |
| 49 class TaskSchedulerDataHandler : public content::WebUIMessageHandler { | |
| 50 public: | |
| 51 TaskSchedulerDataHandler() = default; | |
| 52 | |
| 53 // content::WebUIMessageHandler: | |
| 54 void RegisterMessages() override { | |
| 55 web_ui()->RegisterMessageCallback( | |
| 56 "getTaskSchedulerData", | |
| 57 base::Bind(&TaskSchedulerDataHandler::GetTaskSchedulerData, | |
| 58 base::Unretained(this))); | |
| 59 } | |
| 60 | |
| 61 private: | |
| 62 void GetTaskSchedulerData(const base::ListValue*) { | |
| 63 base::DictionaryValue data; | |
| 64 | |
| 65 base::TaskScheduler* task_scheduler = base::TaskScheduler::GetInstance(); | |
| 66 data.SetBoolean("instantiated", !!task_scheduler); | |
| 67 | |
| 68 if (task_scheduler) { | |
| 69 std::unique_ptr<base::ListValue> histogram_value = | |
| 70 base::MakeUnique<base::ListValue>(); | |
| 71 std::vector<const base::HistogramBase*> histograms = | |
| 72 task_scheduler->GetHistograms(); | |
| 73 | |
| 74 for (const base::HistogramBase* const histogram : histograms) { | |
| 75 std::unique_ptr<base::DictionaryValue> buckets = | |
| 76 base::MakeUnique<base::DictionaryValue>(); | |
| 77 buckets->SetString("name", histogram->histogram_name()); | |
| 78 buckets->Set("buckets", SnapshotHistogramToValue(histogram)); | |
| 79 histogram_value->Append(std::move(buckets)); | |
| 80 } | |
| 81 | |
| 82 data.Set("histograms", std::move(histogram_value)); | |
| 83 } | |
| 84 | |
| 85 web_ui()->CallJavascriptFunctionUnsafe( | |
|
Dan Beam
2016/10/29 01:13:24
nit: can you use the safe version? in your case i
robliao
2016/11/08 18:14:18
Done.
| |
| 86 "TaskSchedulerInternals.onGetTaskSchedulerData", data); | |
| 87 } | |
| 88 | |
| 89 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerDataHandler); | |
| 90 }; | |
| 91 | |
| 92 } // namespace | |
| 12 | 93 |
| 13 TaskSchedulerInternalsUI::TaskSchedulerInternalsUI(content::WebUI* web_ui) | 94 TaskSchedulerInternalsUI::TaskSchedulerInternalsUI(content::WebUI* web_ui) |
| 14 : content::WebUIController(web_ui) { | 95 : content::WebUIController(web_ui) { |
| 96 web_ui->AddMessageHandler(new TaskSchedulerDataHandler()); | |
| 97 | |
| 15 content::WebUIDataSource* html_source = | 98 content::WebUIDataSource* html_source = |
| 16 content::WebUIDataSource::Create( | 99 content::WebUIDataSource::Create( |
| 17 chrome::kChromeUITaskSchedulerInternalsHost); | 100 chrome::kChromeUITaskSchedulerInternalsHost); |
| 18 | 101 html_source->AddResourcePath( |
| 19 html_source->AddString("status", base::TaskScheduler::GetInstance() | 102 "index.css", IDR_TASK_SCHEDULER_INTERNALS_RESOURCES_INDEX_CSS); |
| 20 ? "Instantiated" | 103 html_source->AddResourcePath( |
| 21 : "Not Instantiated"); | 104 "index.js", IDR_TASK_SCHEDULER_INTERNALS_RESOURCES_INDEX_JS); |
| 22 | |
| 23 html_source->SetDefaultResource( | 105 html_source->SetDefaultResource( |
| 24 IDR_TASK_SCHEDULER_INTERNALS_RESOURCES_INDEX_HTML); | 106 IDR_TASK_SCHEDULER_INTERNALS_RESOURCES_INDEX_HTML); |
| 25 | 107 |
| 26 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), html_source); | 108 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), html_source); |
| 27 } | 109 } |
| 28 | 110 |
| 29 TaskSchedulerInternalsUI::~TaskSchedulerInternalsUI() = default; | 111 TaskSchedulerInternalsUI::~TaskSchedulerInternalsUI() = default; |
| OLD | NEW |