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