Chromium Code Reviews| 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/time.h" | 9 #include "base/time.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "chrome/browser/performance_monitor/database.h" | 11 #include "chrome/browser/performance_monitor/database.h" |
| 12 #include "chrome/browser/performance_monitor/event.h" | 12 #include "chrome/browser/performance_monitor/event.h" |
| 13 #include "chrome/browser/performance_monitor/metric_details.h" | 13 #include "chrome/browser/performance_monitor/metric_details.h" |
| 14 #include "chrome/browser/performance_monitor/performance_monitor.h" | 14 #include "chrome/browser/performance_monitor/performance_monitor.h" |
| 15 #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" | 16 #include "chrome/common/extensions/value_builder.h" |
| 17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/browser/web_ui.h" | 18 #include "content/public/browser/web_ui.h" |
| 19 | 19 |
| 20 namespace performance_monitor { | 20 namespace performance_monitor { |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 // Converts a base::Time object in UTC to a double representing local time from | |
| 24 // javascript epoch. | |
| 25 double UTCToLocalJsTime(const base::Time& utc_time) { | |
| 26 base::Time::Exploded exploded; | |
| 27 utc_time.LocalExplode(&exploded); | |
| 28 return base::Time::FromUTCExploded(exploded).ToJsTime(); | |
|
Evan Stade
2012/07/23 23:09:50
piping LocalExplode to FromUTCExploded strikes me
Matt Tytel
2012/07/24 20:44:16
Yea, I talked to Brettw about this.
We decided jus
| |
| 29 } | |
| 30 | |
| 31 // Converts a double representing local time from javascript epoch to a | |
| 32 // base::Time object in UTC. | |
| 33 base::Time LocalJsTimeToUTC(double local_time) { | |
| 34 base::Time::Exploded exploded; | |
| 35 base::Time::FromJsTime(local_time).UTCExplode(&exploded); | |
| 36 return base::Time::FromLocalExploded(exploded); | |
| 37 } | |
| 38 | |
| 23 // Queries the performance monitor database for active intervals between | 39 // Queries the performance monitor database for active intervals between |
| 24 // |start| and |end| times and appends the results to |results|. | 40 // |start| and |end| times and appends the results to |results|. |
| 25 void DoGetActiveIntervals(ListValue* results, | 41 void DoGetActiveIntervals(ListValue* results, |
| 26 const base::Time& start, const base::Time& end) { | 42 const base::Time& start, const base::Time& end) { |
| 27 Database* db = PerformanceMonitor::GetInstance()->database(); | 43 Database* db = PerformanceMonitor::GetInstance()->database(); |
| 28 std::vector<TimeRange> intervals = db->GetActiveIntervals(start, end); | 44 std::vector<TimeRange> intervals = db->GetActiveIntervals(start, end); |
| 29 | 45 |
| 30 for (std::vector<TimeRange>::iterator it = intervals.begin(); | 46 for (std::vector<TimeRange>::iterator it = intervals.begin(); |
| 31 it != intervals.end(); ++it) { | 47 it != intervals.end(); ++it) { |
| 32 DictionaryValue* interval_value = new DictionaryValue(); | 48 DictionaryValue* interval_value = new DictionaryValue(); |
| 33 interval_value->SetDouble("start", it->start.ToJsTime()); | 49 interval_value->SetDouble("start", UTCToLocalJsTime(it->start)); |
| 34 interval_value->SetDouble("end", it->end.ToJsTime()); | 50 interval_value->SetDouble("end", UTCToLocalJsTime(it->end)); |
| 35 results->Append(interval_value); | 51 results->Append(interval_value); |
| 36 } | 52 } |
| 37 } | 53 } |
| 38 | 54 |
| 39 // Queries the performance monitor database for events of type |event_type| | 55 // Queries the performance monitor database for events of type |event_type| |
| 40 // between |start| and |end| times and appends the results to |results|. | 56 // between |start| and |end| times and appends the results to |results|. |
| 41 // TODO(mtytel): Add an internationalized longDescription field to each event. | 57 // TODO(mtytel): Add an internationalized longDescription field to each event. |
| 42 void DoGetEvents(ListValue* results, EventType event_type, | 58 void DoGetEvents(ListValue* results, EventType event_type, |
| 43 const base::Time& start, const base::Time& end) { | 59 const base::Time& start, const base::Time& end) { |
| 44 Database* db = PerformanceMonitor::GetInstance()->database(); | 60 Database* db = PerformanceMonitor::GetInstance()->database(); |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 64 linked_ptr<Database::MetricInfoVector> metric_vector = | 80 linked_ptr<Database::MetricInfoVector> metric_vector = |
| 65 metric_vector_map[kProcessChromeAggregate]; | 81 metric_vector_map[kProcessChromeAggregate]; |
| 66 if (!metric_vector.get()) | 82 if (!metric_vector.get()) |
| 67 metric_vector.reset(new Database::MetricInfoVector()); | 83 metric_vector.reset(new Database::MetricInfoVector()); |
| 68 | 84 |
| 69 Database::MetricInfoVector aggregated_metrics = | 85 Database::MetricInfoVector aggregated_metrics = |
| 70 util::AggregateMetric(*metric_vector, start, resolution); | 86 util::AggregateMetric(*metric_vector, start, resolution); |
| 71 for (Database::MetricInfoVector::iterator it = aggregated_metrics.begin(); | 87 for (Database::MetricInfoVector::iterator it = aggregated_metrics.begin(); |
| 72 it != aggregated_metrics.end(); ++it) { | 88 it != aggregated_metrics.end(); ++it) { |
| 73 DictionaryValue* metric_value = new DictionaryValue(); | 89 DictionaryValue* metric_value = new DictionaryValue(); |
| 74 metric_value->SetDouble("time", it->time.ToJsTime()); | 90 metric_value->SetDouble("time", UTCToLocalJsTime(it->time)); |
| 75 metric_value->SetDouble("value", it->value); | 91 metric_value->SetDouble("value", it->value); |
| 76 results->Append(metric_value); | 92 results->Append(metric_value); |
| 77 } | 93 } |
| 78 } | 94 } |
| 79 | 95 |
| 80 } // namespace | 96 } // namespace |
| 81 | 97 |
| 82 WebUIHandler::WebUIHandler() {} | 98 WebUIHandler::WebUIHandler() { |
| 99 // TODO(mtytel): Remove this check when the PerformanceMonitor starts up | |
| 100 // before the WebUI. | |
| 101 if (!PerformanceMonitor::GetInstance()->database()) | |
| 102 PerformanceMonitor::GetInstance()->Start(); | |
| 103 } | |
|
Evan Stade
2012/07/23 23:09:50
\n
Matt Tytel
2012/07/24 20:44:16
Done.
| |
| 83 WebUIHandler::~WebUIHandler() {} | 104 WebUIHandler::~WebUIHandler() {} |
| 84 | 105 |
| 85 void WebUIHandler::RegisterMessages() { | 106 void WebUIHandler::RegisterMessages() { |
| 86 web_ui()->RegisterMessageCallback( | 107 web_ui()->RegisterMessageCallback( |
| 87 "getActiveIntervals", | 108 "getActiveIntervals", |
| 88 base::Bind(&WebUIHandler::HandleGetActiveIntervals, | 109 base::Bind(&WebUIHandler::HandleGetActiveIntervals, |
| 89 base::Unretained(this))); | 110 base::Unretained(this))); |
| 90 web_ui()->RegisterMessageCallback( | 111 web_ui()->RegisterMessageCallback( |
| 91 "getAllEventTypes", | 112 "getAllEventTypes", |
| 92 base::Bind(&WebUIHandler::HandleGetAllEventTypes, | 113 base::Bind(&WebUIHandler::HandleGetAllEventTypes, |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 109 const Value* results) { | 130 const Value* results) { |
| 110 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 131 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 111 web_ui()->CallJavascriptFunction(function, *results); | 132 web_ui()->CallJavascriptFunction(function, *results); |
| 112 } | 133 } |
| 113 | 134 |
| 114 void WebUIHandler::HandleGetActiveIntervals(const ListValue* args) { | 135 void WebUIHandler::HandleGetActiveIntervals(const ListValue* args) { |
| 115 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 136 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 116 CHECK_EQ(2u, args->GetSize()); | 137 CHECK_EQ(2u, args->GetSize()); |
| 117 double double_time = 0.0; | 138 double double_time = 0.0; |
| 118 CHECK(args->GetDouble(0, &double_time)); | 139 CHECK(args->GetDouble(0, &double_time)); |
| 119 base::Time start = base::Time::FromJsTime(double_time); | 140 base::Time start = LocalJsTimeToUTC(double_time); |
| 120 CHECK(args->GetDouble(1, &double_time)); | 141 CHECK(args->GetDouble(1, &double_time)); |
| 121 base::Time end = base::Time::FromJsTime(double_time); | 142 base::Time end = LocalJsTimeToUTC(double_time); |
| 122 | 143 |
| 123 ListValue* results = new ListValue(); | 144 ListValue* results = new ListValue(); |
| 124 util::PostTaskToDatabaseThreadAndReply( | 145 util::PostTaskToDatabaseThreadAndReply( |
| 125 base::Bind(&DoGetActiveIntervals, results, start, end), | 146 base::Bind(&DoGetActiveIntervals, results, start, end), |
| 126 base::Bind(&WebUIHandler::ReturnResults, AsWeakPtr(), | 147 base::Bind(&WebUIHandler::ReturnResults, AsWeakPtr(), |
| 127 "performance_monitor.getActiveIntervalsCallback", | 148 "PerformanceMonitor.getActiveIntervalsCallback", |
| 128 base::Owned(results))); | 149 base::Owned(results))); |
| 129 } | 150 } |
| 130 | 151 |
| 131 void WebUIHandler::HandleGetAllEventTypes(const ListValue* args) { | 152 void WebUIHandler::HandleGetAllEventTypes(const ListValue* args) { |
| 132 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 153 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 133 CHECK_EQ(0u, args->GetSize()); | 154 CHECK_EQ(0u, args->GetSize()); |
| 134 ListValue results; | 155 ListValue results; |
| 135 for (int i = 0; i < EVENT_NUMBER_OF_EVENTS; ++i) { | 156 for (int i = 0; i < EVENT_NUMBER_OF_EVENTS; ++i) { |
| 136 EventType event_type = static_cast<EventType>(i); | 157 EventType event_type = static_cast<EventType>(i); |
| 137 DictionaryValue* event_type_info = new DictionaryValue(); | 158 DictionaryValue* event_type_info = new DictionaryValue(); |
| 138 event_type_info->SetInteger("eventType", event_type); | 159 event_type_info->SetInteger("eventType", event_type); |
| 139 event_type_info->SetString("shortDescription", | 160 event_type_info->SetString("shortDescription", |
| 140 EventTypeToString(event_type)); | 161 EventTypeToString(event_type)); |
| 141 results.Append(event_type_info); | 162 results.Append(event_type_info); |
| 142 } | 163 } |
| 143 | 164 ReturnResults("PerformanceMonitor.getAllEventTypesCallback", &results); |
| 144 ReturnResults("performance_monitor.getAllEventTypesCallback", &results); | |
| 145 } | 165 } |
| 146 | 166 |
| 147 void WebUIHandler::HandleGetEvents(const ListValue* args) { | 167 void WebUIHandler::HandleGetEvents(const ListValue* args) { |
| 148 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 168 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 149 CHECK_EQ(3u, args->GetSize()); | 169 CHECK_EQ(3u, args->GetSize()); |
| 150 double event = 0; | 170 double event = 0; |
| 151 CHECK(args->GetDouble(0, &event)); | 171 CHECK(args->GetDouble(0, &event)); |
| 152 EventType event_type = static_cast<EventType>(static_cast<int>(event)); | 172 EventType event_type = static_cast<EventType>(static_cast<int>(event)); |
| 153 | 173 |
| 154 double double_time = 0.0; | 174 double double_time = 0.0; |
| 155 CHECK(args->GetDouble(1, &double_time)); | 175 CHECK(args->GetDouble(1, &double_time)); |
| 156 base::Time start = base::Time::FromJsTime(double_time); | 176 base::Time start = LocalJsTimeToUTC(double_time); |
| 157 CHECK(args->GetDouble(2, &double_time)); | 177 CHECK(args->GetDouble(2, &double_time)); |
| 158 base::Time end = base::Time::FromJsTime(double_time); | 178 base::Time end = LocalJsTimeToUTC(double_time); |
| 159 | 179 |
| 160 DictionaryValue* results = new DictionaryValue(); | 180 DictionaryValue* results = new DictionaryValue(); |
| 161 ListValue* points_results = new ListValue(); | 181 ListValue* points_results = new ListValue(); |
| 162 results->Set("points", points_results); | 182 results->Set("points", points_results); |
| 163 results->SetInteger("type", event_type); | 183 results->SetInteger("eventType", event_type); |
| 164 util::PostTaskToDatabaseThreadAndReply( | 184 util::PostTaskToDatabaseThreadAndReply( |
| 165 base::Bind(&DoGetEvents, points_results, event_type, start, end), | 185 base::Bind(&DoGetEvents, points_results, event_type, start, end), |
| 166 base::Bind(&WebUIHandler::ReturnResults, AsWeakPtr(), | 186 base::Bind(&WebUIHandler::ReturnResults, AsWeakPtr(), |
| 167 "performance_monitor.getEventsCallback", | 187 "PerformanceMonitor.getEventsCallback", |
| 168 base::Owned(results))); | 188 base::Owned(results))); |
| 169 } | 189 } |
| 170 | 190 |
| 171 void WebUIHandler::HandleGetAllMetricTypes(const ListValue* args) { | 191 void WebUIHandler::HandleGetAllMetricTypes(const ListValue* args) { |
| 172 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 192 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 173 CHECK_EQ(0u, args->GetSize()); | 193 CHECK_EQ(0u, args->GetSize()); |
| 174 ListValue results; | 194 ListValue results; |
| 175 for (int i = 0; i < METRIC_NUMBER_OF_METRICS; ++i) { | 195 for (int i = 0; i < METRIC_NUMBER_OF_METRICS; ++i) { |
| 176 MetricType metric_type = static_cast<MetricType>(i); | 196 MetricType metric_type = static_cast<MetricType>(i); |
| 177 DictionaryValue* metric_type_info = new DictionaryValue(); | 197 DictionaryValue* metric_type_info = new DictionaryValue(); |
| 178 metric_type_info->SetInteger("metricType", metric_type); | 198 metric_type_info->SetInteger("metricType", metric_type); |
| 179 metric_type_info->SetString("shortDescription", | 199 metric_type_info->SetString("shortDescription", |
| 180 MetricTypeToString(metric_type)); | 200 MetricTypeToString(metric_type)); |
| 181 results.Append(metric_type_info); | 201 results.Append(metric_type_info); |
| 182 } | 202 } |
| 183 | 203 |
| 184 ReturnResults("performance_monitor.getAllMetricTypesCallback", &results); | 204 ReturnResults("PerformanceMonitor.getAllMetricTypesCallback", &results); |
| 185 } | 205 } |
| 186 | 206 |
| 187 void WebUIHandler::HandleGetMetric(const ListValue* args) { | 207 void WebUIHandler::HandleGetMetric(const ListValue* args) { |
| 188 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 208 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 189 CHECK_EQ(4u, args->GetSize()); | 209 CHECK_EQ(4u, args->GetSize()); |
| 190 double metric = 0; | 210 double metric = 0; |
| 191 CHECK(args->GetDouble(0, &metric)); | 211 CHECK(args->GetDouble(0, &metric)); |
| 192 MetricType metric_type = static_cast<MetricType>(static_cast<int>(metric)); | 212 MetricType metric_type = static_cast<MetricType>(static_cast<int>(metric)); |
| 193 | 213 |
| 194 double double_time = 0.0; | 214 double double_time = 0.0; |
| 195 CHECK(args->GetDouble(1, &double_time)); | 215 CHECK(args->GetDouble(1, &double_time)); |
| 196 base::Time start = base::Time::FromJsTime(double_time); | 216 base::Time start = LocalJsTimeToUTC(double_time); |
| 197 CHECK(args->GetDouble(2, &double_time)); | 217 CHECK(args->GetDouble(2, &double_time)); |
| 198 base::Time end = base::Time::FromJsTime(double_time); | 218 base::Time end = LocalJsTimeToUTC(double_time); |
| 199 | 219 |
| 200 double resolution_in_milliseconds = 0; | 220 double resolution_in_milliseconds = 0; |
| 201 CHECK(args->GetDouble(3, &resolution_in_milliseconds)); | 221 CHECK(args->GetDouble(3, &resolution_in_milliseconds)); |
| 202 base::TimeDelta resolution = | 222 base::TimeDelta resolution = |
| 203 base::TimeDelta::FromMilliseconds(resolution_in_milliseconds); | 223 base::TimeDelta::FromMilliseconds(resolution_in_milliseconds); |
| 204 | 224 |
| 205 DictionaryValue* results = new DictionaryValue(); | 225 DictionaryValue* results = new DictionaryValue(); |
| 206 results->SetInteger("type", metric_type); | 226 results->SetInteger("metricType", metric_type); |
| 207 ListValue* points_results = new ListValue(); | 227 ListValue* points_results = new ListValue(); |
| 208 results->Set("points", points_results); | 228 results->Set("points", points_results); |
| 209 util::PostTaskToDatabaseThreadAndReply( | 229 util::PostTaskToDatabaseThreadAndReply( |
| 210 base::Bind(&DoGetMetric, points_results, metric_type, | 230 base::Bind(&DoGetMetric, points_results, metric_type, |
| 211 start, end, resolution), | 231 start, end, resolution), |
| 212 base::Bind(&WebUIHandler::ReturnResults, AsWeakPtr(), | 232 base::Bind(&WebUIHandler::ReturnResults, AsWeakPtr(), |
| 213 "performance_monitor.getMetricCallback", | 233 "PerformanceMonitor.getMetricCallback", |
| 214 base::Owned(results))); | 234 base::Owned(results))); |
| 215 } | 235 } |
| 216 | 236 |
| 217 } // namespace performance_monitor | 237 } // namespace performance_monitor |
| OLD | NEW |