| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/performance_monitor/web_ui_handler.h" | 5 #include "chrome/browser/ui/webui/performance_monitor/web_ui_handler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/threading/sequenced_worker_pool.h" | |
| 10 #include "base/time.h" | 9 #include "base/time.h" |
| 11 #include "base/values.h" | 10 #include "base/values.h" |
| 12 #include "chrome/browser/performance_monitor/database.h" | 11 #include "chrome/browser/performance_monitor/database.h" |
| 12 #include "chrome/browser/performance_monitor/event.h" |
| 13 #include "chrome/browser/performance_monitor/metric_details.h" |
| 13 #include "chrome/browser/performance_monitor/performance_monitor.h" | 14 #include "chrome/browser/performance_monitor/performance_monitor.h" |
| 14 #include "chrome/browser/performance_monitor/performance_monitor_util.h" | 15 #include "chrome/browser/performance_monitor/performance_monitor_util.h" |
| 16 #include "chrome/common/extensions/value_builder.h" |
| 15 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 16 #include "content/public/browser/web_ui.h" | 18 #include "content/public/browser/web_ui.h" |
| 17 | 19 |
| 18 namespace performance_monitor { | 20 namespace performance_monitor { |
| 19 namespace { | 21 namespace { |
| 20 | 22 |
| 23 // Queries the performance monitor database for active intervals between |
| 24 // |start| and |end| times and appends the results to |results|. |
| 21 void DoGetActiveIntervals(ListValue* results, | 25 void DoGetActiveIntervals(ListValue* results, |
| 22 const base::Time& start, const base::Time& end) { | 26 const base::Time& start, const base::Time& end) { |
| 23 std::vector<TimeRange> intervals = | 27 Database* db = PerformanceMonitor::GetInstance()->database(); |
| 24 PerformanceMonitor::GetInstance()->database()->GetActiveIntervals( | 28 std::vector<TimeRange> intervals = db->GetActiveIntervals(start, end); |
| 25 start, end); | 29 |
| 26 for (std::vector<TimeRange>::iterator it = intervals.begin(); | 30 for (std::vector<TimeRange>::iterator it = intervals.begin(); |
| 27 it != intervals.end(); ++it) { | 31 it != intervals.end(); ++it) { |
| 28 DictionaryValue* interval_value = new DictionaryValue(); | 32 DictionaryValue* interval_value = new DictionaryValue(); |
| 29 interval_value->SetDouble("start", it->start.ToDoubleT()); | 33 interval_value->SetDouble("start", it->start.ToJsTime()); |
| 30 interval_value->SetDouble("end", it->end.ToDoubleT()); | 34 interval_value->SetDouble("end", it->end.ToJsTime()); |
| 31 results->Append(interval_value); | 35 results->Append(interval_value); |
| 32 } | 36 } |
| 33 } | 37 } |
| 34 | 38 |
| 39 // Queries the performance monitor database for events of type |event_type| |
| 40 // between |start| and |end| times and appends the results to |results|. |
| 35 // TODO(mtytel): Add an internationalized longDescription field to each event. | 41 // TODO(mtytel): Add an internationalized longDescription field to each event. |
| 36 void DoGetEvents(ListValue* results, EventType event, | 42 void DoGetEvents(ListValue* results, EventType event_type, |
| 37 const base::Time& start, const base::Time& end) { | 43 const base::Time& start, const base::Time& end) { |
| 44 Database* db = PerformanceMonitor::GetInstance()->database(); |
| 38 std::vector<linked_ptr<Event> > events = | 45 std::vector<linked_ptr<Event> > events = |
| 39 PerformanceMonitor::GetInstance()->database()->GetEvents( | 46 db->GetEvents(event_type, start, end); |
| 40 event, start, end); | 47 |
| 41 for (std::vector<linked_ptr<Event> >::iterator it = events.begin(); | 48 for (std::vector<linked_ptr<Event> >::iterator it = events.begin(); |
| 42 it != events.end(); ++it) { | 49 it != events.end(); ++it) { |
| 43 results->Append((*it)->data()); | 50 results->Append((*it)->data()->DeepCopy()); |
| 44 } | 51 } |
| 45 } | 52 } |
| 46 | 53 |
| 47 bool PostTaskToDatabaseAndReply(const base::Closure& request, | 54 // Queries the performance monitor database for metrics of type |metric_type| |
| 48 const base::Closure& reply) { | 55 // between |start| and |end| times and appends the results to |results|. |
| 49 base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool(); | 56 void DoGetMetric(ListValue* results, |
| 50 base::SequencedWorkerPool::SequenceToken token = pool->GetSequenceToken(); | 57 MetricType metric_type, |
| 51 return pool->GetSequencedTaskRunner(token)->PostTaskAndReply( | 58 const base::Time& start, const base::Time& end, |
| 52 FROM_HERE, request, reply); | 59 const base::TimeDelta& resolution) { |
| 60 Database* db = PerformanceMonitor::GetInstance()->database(); |
| 61 Database::MetricVectorMap metric_vector_map = db->GetStatsForMetricByActivity( |
| 62 MetricTypeToString(metric_type), start, end); |
| 63 |
| 64 linked_ptr<Database::MetricInfoVector> metric_vector = |
| 65 metric_vector_map[kProcessChromeAggregate]; |
| 66 if (!metric_vector.get()) |
| 67 metric_vector.reset(new Database::MetricInfoVector()); |
| 68 |
| 69 Database::MetricInfoVector aggregated_metrics = |
| 70 util::AggregateMetric(*metric_vector, start, resolution); |
| 71 for (Database::MetricInfoVector::iterator it = aggregated_metrics.begin(); |
| 72 it != aggregated_metrics.end(); ++it) { |
| 73 DictionaryValue* metric_value = new DictionaryValue(); |
| 74 metric_value->SetDouble("time", it->time.ToJsTime()); |
| 75 metric_value->SetDouble("value", it->value); |
| 76 results->Append(metric_value); |
| 77 } |
| 53 } | 78 } |
| 54 | 79 |
| 55 } // namespace | 80 } // namespace |
| 56 | 81 |
| 57 WebUIHandler::WebUIHandler() {} | 82 WebUIHandler::WebUIHandler() {} |
| 58 WebUIHandler::~WebUIHandler() {} | 83 WebUIHandler::~WebUIHandler() {} |
| 59 | 84 |
| 60 void WebUIHandler::RegisterMessages() { | 85 void WebUIHandler::RegisterMessages() { |
| 61 web_ui()->RegisterMessageCallback( | 86 web_ui()->RegisterMessageCallback( |
| 62 "getActiveIntervals", | 87 "getActiveIntervals", |
| 63 base::Bind(&WebUIHandler::HandleGetActiveIntervals, | 88 base::Bind(&WebUIHandler::HandleGetActiveIntervals, |
| 64 base::Unretained(this))); | 89 base::Unretained(this))); |
| 65 web_ui()->RegisterMessageCallback( | 90 web_ui()->RegisterMessageCallback( |
| 66 "getAllEventTypes", | 91 "getAllEventTypes", |
| 67 base::Bind(&WebUIHandler::HandleGetAllEventTypes, | 92 base::Bind(&WebUIHandler::HandleGetAllEventTypes, |
| 68 base::Unretained(this))); | 93 base::Unretained(this))); |
| 69 web_ui()->RegisterMessageCallback( | 94 web_ui()->RegisterMessageCallback( |
| 70 "getEvents", | 95 "getEvents", |
| 71 base::Bind(&WebUIHandler::HandleGetEvents, | 96 base::Bind(&WebUIHandler::HandleGetEvents, |
| 72 base::Unretained(this))); | 97 base::Unretained(this))); |
| 98 web_ui()->RegisterMessageCallback( |
| 99 "getAllMetricTypes", |
| 100 base::Bind(&WebUIHandler::HandleGetAllMetricTypes, |
| 101 base::Unretained(this))); |
| 102 web_ui()->RegisterMessageCallback( |
| 103 "getMetric", |
| 104 base::Bind(&WebUIHandler::HandleGetMetric, |
| 105 base::Unretained(this))); |
| 73 } | 106 } |
| 74 | 107 |
| 75 void WebUIHandler::ReturnResults(const std::string& function, | 108 void WebUIHandler::ReturnResults(const std::string& function, |
| 76 const ListValue* results) { | 109 const Value* results) { |
| 77 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 110 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 78 web_ui()->CallJavascriptFunction(function, *results); | 111 web_ui()->CallJavascriptFunction(function, *results); |
| 79 } | 112 } |
| 80 | 113 |
| 81 void WebUIHandler::HandleGetActiveIntervals(const ListValue* args) { | 114 void WebUIHandler::HandleGetActiveIntervals(const ListValue* args) { |
| 82 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 115 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 83 CHECK_EQ(2u, args->GetSize()); | 116 CHECK_EQ(2u, args->GetSize()); |
| 84 double double_time = 0.0; | 117 double double_time = 0.0; |
| 85 CHECK(args->GetDouble(0, &double_time)); | 118 CHECK(args->GetDouble(0, &double_time)); |
| 86 base::Time start = base::Time::FromJsTime(double_time); | 119 base::Time start = base::Time::FromJsTime(double_time); |
| 87 CHECK(args->GetDouble(1, &double_time)); | 120 CHECK(args->GetDouble(1, &double_time)); |
| 88 base::Time end = base::Time::FromJsTime(double_time); | 121 base::Time end = base::Time::FromJsTime(double_time); |
| 89 | 122 |
| 90 ListValue* results = new ListValue(); | 123 ListValue* results = new ListValue(); |
| 91 PostTaskToDatabaseAndReply( | 124 util::PostTaskToDatabaseThreadAndReply( |
| 92 base::Bind(&DoGetActiveIntervals, results, start, end), | 125 base::Bind(&DoGetActiveIntervals, results, start, end), |
| 93 base::Bind(&WebUIHandler::ReturnResults, AsWeakPtr(), | 126 base::Bind(&WebUIHandler::ReturnResults, AsWeakPtr(), |
| 94 "performance_monitor.getActiveIntervalsCallback", | 127 "performance_monitor.getActiveIntervalsCallback", |
| 95 base::Owned(results))); | 128 base::Owned(results))); |
| 96 } | 129 } |
| 97 | 130 |
| 98 void WebUIHandler::HandleGetAllEventTypes(const ListValue* args) { | 131 void WebUIHandler::HandleGetAllEventTypes(const ListValue* args) { |
| 99 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 132 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 100 CHECK_EQ(0u, args->GetSize()); | 133 CHECK_EQ(0u, args->GetSize()); |
| 101 ListValue results; | 134 ListValue results; |
| 102 for (int i = 0; i < EVENT_NUMBER_OF_EVENTS; ++i) { | 135 for (int i = 0; i < EVENT_NUMBER_OF_EVENTS; ++i) { |
| 103 EventType event_type = static_cast<EventType>(i); | 136 EventType event_type = static_cast<EventType>(i); |
| 104 DictionaryValue* event_type_info = new DictionaryValue(); | 137 DictionaryValue* event_type_info = new DictionaryValue(); |
| 105 event_type_info->SetInteger("eventType", event_type); | 138 event_type_info->SetInteger("eventType", event_type); |
| 106 event_type_info->SetString("shortDescription", | 139 event_type_info->SetString("shortDescription", |
| 107 EventTypeToString(event_type)); | 140 EventTypeToString(event_type)); |
| 108 results.Append(event_type_info); | 141 results.Append(event_type_info); |
| 109 } | 142 } |
| 143 |
| 110 ReturnResults("performance_monitor.getAllEventTypesCallback", &results); | 144 ReturnResults("performance_monitor.getAllEventTypesCallback", &results); |
| 111 } | 145 } |
| 112 | 146 |
| 113 void WebUIHandler::HandleGetEvents(const ListValue* args) { | 147 void WebUIHandler::HandleGetEvents(const ListValue* args) { |
| 114 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 148 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 115 CHECK_EQ(3u, args->GetSize()); | 149 CHECK_EQ(3u, args->GetSize()); |
| 116 int event = 0; | 150 double event = 0; |
| 117 CHECK(args->GetInteger(0, &event)); | 151 CHECK(args->GetDouble(0, &event)); |
| 152 EventType event_type = static_cast<EventType>(static_cast<int>(event)); |
| 153 |
| 118 double double_time = 0.0; | 154 double double_time = 0.0; |
| 119 CHECK(args->GetDouble(1, &double_time)); | 155 CHECK(args->GetDouble(1, &double_time)); |
| 120 base::Time start = base::Time::FromJsTime(double_time); | 156 base::Time start = base::Time::FromJsTime(double_time); |
| 121 CHECK(args->GetDouble(2, &double_time)); | 157 CHECK(args->GetDouble(2, &double_time)); |
| 122 base::Time end = base::Time::FromJsTime(double_time); | 158 base::Time end = base::Time::FromJsTime(double_time); |
| 123 | 159 |
| 124 ListValue* results = new ListValue(); | 160 DictionaryValue* results = new DictionaryValue(); |
| 125 PostTaskToDatabaseAndReply( | 161 ListValue* points_results = new ListValue(); |
| 126 base::Bind(&DoGetEvents, results, static_cast<EventType>(event), | 162 results->Set("points", points_results); |
| 127 start, end), | 163 results->SetInteger("type", event_type); |
| 164 util::PostTaskToDatabaseThreadAndReply( |
| 165 base::Bind(&DoGetEvents, points_results, event_type, start, end), |
| 128 base::Bind(&WebUIHandler::ReturnResults, AsWeakPtr(), | 166 base::Bind(&WebUIHandler::ReturnResults, AsWeakPtr(), |
| 129 "performance_monitor.getEventsCallback", | 167 "performance_monitor.getEventsCallback", |
| 130 base::Owned(results))); | 168 base::Owned(results))); |
| 131 } | 169 } |
| 132 | 170 |
| 171 void WebUIHandler::HandleGetAllMetricTypes(const ListValue* args) { |
| 172 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 173 CHECK_EQ(0u, args->GetSize()); |
| 174 ListValue results; |
| 175 for (int i = 0; i < METRIC_NUMBER_OF_METRICS; ++i) { |
| 176 MetricType metric_type = static_cast<MetricType>(i); |
| 177 DictionaryValue* metric_type_info = new DictionaryValue(); |
| 178 metric_type_info->SetInteger("metricType", metric_type); |
| 179 metric_type_info->SetString("shortDescription", |
| 180 MetricTypeToString(metric_type)); |
| 181 results.Append(metric_type_info); |
| 182 } |
| 183 |
| 184 ReturnResults("performance_monitor.getAllMetricTypesCallback", &results); |
| 185 } |
| 186 |
| 187 void WebUIHandler::HandleGetMetric(const ListValue* args) { |
| 188 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 189 CHECK_EQ(4u, args->GetSize()); |
| 190 double metric = 0; |
| 191 CHECK(args->GetDouble(0, &metric)); |
| 192 MetricType metric_type = static_cast<MetricType>(static_cast<int>(metric)); |
| 193 |
| 194 double double_time = 0.0; |
| 195 CHECK(args->GetDouble(1, &double_time)); |
| 196 base::Time start = base::Time::FromJsTime(double_time); |
| 197 CHECK(args->GetDouble(2, &double_time)); |
| 198 base::Time end = base::Time::FromJsTime(double_time); |
| 199 |
| 200 double resolution_in_milliseconds = 0; |
| 201 CHECK(args->GetDouble(3, &resolution_in_milliseconds)); |
| 202 base::TimeDelta resolution = |
| 203 base::TimeDelta::FromMilliseconds(resolution_in_milliseconds); |
| 204 |
| 205 DictionaryValue* results = new DictionaryValue(); |
| 206 results->SetInteger("type", metric_type); |
| 207 ListValue* points_results = new ListValue(); |
| 208 results->Set("points", points_results); |
| 209 util::PostTaskToDatabaseThreadAndReply( |
| 210 base::Bind(&DoGetMetric, points_results, metric_type, |
| 211 start, end, resolution), |
| 212 base::Bind(&WebUIHandler::ReturnResults, AsWeakPtr(), |
| 213 "performance_monitor.getMetricCallback", |
| 214 base::Owned(results))); |
| 215 } |
| 216 |
| 133 } // namespace performance_monitor | 217 } // namespace performance_monitor |
| OLD | NEW |