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

Side by Side Diff: chrome/browser/ui/webui/performance_monitor/web_ui_handler.cc

Issue 10797056: CPM: API changes for API/UI integration. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 5 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
« no previous file with comments | « chrome/browser/performance_monitor/performance_monitor.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 double UTCTimeToJsTime(const base::Time& utc_time) {
koz (OOO until 15th September) 2012/07/23 06:58:18 What's the difference between using this function
Matt Tytel 2012/07/23 22:41:38 Ah forgot to specify the locality of the JsTime. W
24 base::Time::Exploded exploded;
25 utc_time.LocalExplode(&exploded);
26 return base::Time::FromUTCExploded(exploded).ToJsTime();
27 }
28
29 base::Time JsTimeToUTCTime(double local_time) {
30 base::Time::Exploded exploded;
31 base::Time::FromJsTime(local_time).UTCExplode(&exploded);
32 return base::Time::FromLocalExploded(exploded);
33 }
34
23 // Queries the performance monitor database for active intervals between 35 // Queries the performance monitor database for active intervals between
24 // |start| and |end| times and appends the results to |results|. 36 // |start| and |end| times and appends the results to |results|.
25 void DoGetActiveIntervals(ListValue* results, 37 void DoGetActiveIntervals(ListValue* results,
26 const base::Time& start, const base::Time& end) { 38 const base::Time& start, const base::Time& end) {
27 Database* db = PerformanceMonitor::GetInstance()->database(); 39 Database* db = PerformanceMonitor::GetInstance()->database();
28 std::vector<TimeRange> intervals = db->GetActiveIntervals(start, end); 40 std::vector<TimeRange> intervals = db->GetActiveIntervals(start, end);
29 41
30 for (std::vector<TimeRange>::iterator it = intervals.begin(); 42 for (std::vector<TimeRange>::iterator it = intervals.begin();
31 it != intervals.end(); ++it) { 43 it != intervals.end(); ++it) {
32 DictionaryValue* interval_value = new DictionaryValue(); 44 DictionaryValue* interval_value = new DictionaryValue();
33 interval_value->SetDouble("start", it->start.ToJsTime()); 45 interval_value->SetDouble("start", UTCTimeToJsTime(it->start));
34 interval_value->SetDouble("end", it->end.ToJsTime()); 46 interval_value->SetDouble("end", UTCTimeToJsTime(it->end));
35 results->Append(interval_value); 47 results->Append(interval_value);
36 } 48 }
37 } 49 }
38 50
39 // Queries the performance monitor database for events of type |event_type| 51 // Queries the performance monitor database for events of type |event_type|
40 // between |start| and |end| times and appends the results to |results|. 52 // between |start| and |end| times and appends the results to |results|.
41 // TODO(mtytel): Add an internationalized longDescription field to each event. 53 // TODO(mtytel): Add an internationalized longDescription field to each event.
42 void DoGetEvents(ListValue* results, EventType event_type, 54 void DoGetEvents(ListValue* results, EventType event_type,
43 const base::Time& start, const base::Time& end) { 55 const base::Time& start, const base::Time& end) {
44 Database* db = PerformanceMonitor::GetInstance()->database(); 56 Database* db = PerformanceMonitor::GetInstance()->database();
(...skipping 19 matching lines...) Expand all
64 linked_ptr<Database::MetricInfoVector> metric_vector = 76 linked_ptr<Database::MetricInfoVector> metric_vector =
65 metric_vector_map[kProcessChromeAggregate]; 77 metric_vector_map[kProcessChromeAggregate];
66 if (!metric_vector.get()) 78 if (!metric_vector.get())
67 metric_vector.reset(new Database::MetricInfoVector()); 79 metric_vector.reset(new Database::MetricInfoVector());
68 80
69 Database::MetricInfoVector aggregated_metrics = 81 Database::MetricInfoVector aggregated_metrics =
70 util::AggregateMetric(*metric_vector, start, resolution); 82 util::AggregateMetric(*metric_vector, start, resolution);
71 for (Database::MetricInfoVector::iterator it = aggregated_metrics.begin(); 83 for (Database::MetricInfoVector::iterator it = aggregated_metrics.begin();
72 it != aggregated_metrics.end(); ++it) { 84 it != aggregated_metrics.end(); ++it) {
73 DictionaryValue* metric_value = new DictionaryValue(); 85 DictionaryValue* metric_value = new DictionaryValue();
74 metric_value->SetDouble("time", it->time.ToJsTime()); 86 metric_value->SetDouble("time", UTCTimeToJsTime(it->time));
75 metric_value->SetDouble("value", it->value); 87 metric_value->SetDouble("value", it->value);
76 results->Append(metric_value); 88 results->Append(metric_value);
77 } 89 }
78 } 90 }
79 91
80 } // namespace 92 } // namespace
81 93
82 WebUIHandler::WebUIHandler() {} 94 WebUIHandler::WebUIHandler() {
95 // TODO(mtytel): Remove this check when the PerformanceMonitor starts up
96 // before the WebUI.
97 if (!PerformanceMonitor::GetInstance()->database())
98 PerformanceMonitor::GetInstance()->Start();
99 }
83 WebUIHandler::~WebUIHandler() {} 100 WebUIHandler::~WebUIHandler() {}
84 101
85 void WebUIHandler::RegisterMessages() { 102 void WebUIHandler::RegisterMessages() {
86 web_ui()->RegisterMessageCallback( 103 web_ui()->RegisterMessageCallback(
87 "getActiveIntervals", 104 "getActiveIntervals",
88 base::Bind(&WebUIHandler::HandleGetActiveIntervals, 105 base::Bind(&WebUIHandler::HandleGetActiveIntervals,
89 base::Unretained(this))); 106 base::Unretained(this)));
90 web_ui()->RegisterMessageCallback( 107 web_ui()->RegisterMessageCallback(
91 "getAllEventTypes", 108 "getAllEventTypes",
92 base::Bind(&WebUIHandler::HandleGetAllEventTypes, 109 base::Bind(&WebUIHandler::HandleGetAllEventTypes,
(...skipping 16 matching lines...) Expand all
109 const Value* results) { 126 const Value* results) {
110 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 127 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
111 web_ui()->CallJavascriptFunction(function, *results); 128 web_ui()->CallJavascriptFunction(function, *results);
112 } 129 }
113 130
114 void WebUIHandler::HandleGetActiveIntervals(const ListValue* args) { 131 void WebUIHandler::HandleGetActiveIntervals(const ListValue* args) {
115 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 132 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
116 CHECK_EQ(2u, args->GetSize()); 133 CHECK_EQ(2u, args->GetSize());
117 double double_time = 0.0; 134 double double_time = 0.0;
118 CHECK(args->GetDouble(0, &double_time)); 135 CHECK(args->GetDouble(0, &double_time));
119 base::Time start = base::Time::FromJsTime(double_time); 136 base::Time start = JsTimeToUTCTime(double_time);
120 CHECK(args->GetDouble(1, &double_time)); 137 CHECK(args->GetDouble(1, &double_time));
121 base::Time end = base::Time::FromJsTime(double_time); 138 base::Time end = JsTimeToUTCTime(double_time);
122 139
123 ListValue* results = new ListValue(); 140 ListValue* results = new ListValue();
124 util::PostTaskToDatabaseThreadAndReply( 141 util::PostTaskToDatabaseThreadAndReply(
125 base::Bind(&DoGetActiveIntervals, results, start, end), 142 base::Bind(&DoGetActiveIntervals, results, start, end),
126 base::Bind(&WebUIHandler::ReturnResults, AsWeakPtr(), 143 base::Bind(&WebUIHandler::ReturnResults, AsWeakPtr(),
127 "performance_monitor.getActiveIntervalsCallback", 144 "PerformanceMonitor.getActiveIntervalsCallback",
128 base::Owned(results))); 145 base::Owned(results)));
129 } 146 }
130 147
131 void WebUIHandler::HandleGetAllEventTypes(const ListValue* args) { 148 void WebUIHandler::HandleGetAllEventTypes(const ListValue* args) {
132 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 149 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
133 CHECK_EQ(0u, args->GetSize()); 150 CHECK_EQ(0u, args->GetSize());
134 ListValue results; 151 ListValue results;
135 for (int i = 0; i < EVENT_NUMBER_OF_EVENTS; ++i) { 152 for (int i = 0; i < EVENT_NUMBER_OF_EVENTS; ++i) {
136 EventType event_type = static_cast<EventType>(i); 153 EventType event_type = static_cast<EventType>(i);
137 DictionaryValue* event_type_info = new DictionaryValue(); 154 DictionaryValue* event_type_info = new DictionaryValue();
138 event_type_info->SetInteger("eventType", event_type); 155 event_type_info->SetInteger("eventType", event_type);
139 event_type_info->SetString("shortDescription", 156 event_type_info->SetString("shortDescription",
140 EventTypeToString(event_type)); 157 EventTypeToString(event_type));
141 results.Append(event_type_info); 158 results.Append(event_type_info);
142 } 159 }
143 160 ReturnResults("PerformanceMonitor.getAllEventTypesCallback", &results);
144 ReturnResults("performance_monitor.getAllEventTypesCallback", &results);
145 } 161 }
146 162
147 void WebUIHandler::HandleGetEvents(const ListValue* args) { 163 void WebUIHandler::HandleGetEvents(const ListValue* args) {
148 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 164 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
149 CHECK_EQ(3u, args->GetSize()); 165 CHECK_EQ(3u, args->GetSize());
150 double event = 0; 166 double event = 0;
151 CHECK(args->GetDouble(0, &event)); 167 CHECK(args->GetDouble(0, &event));
152 EventType event_type = static_cast<EventType>(static_cast<int>(event)); 168 EventType event_type = static_cast<EventType>(static_cast<int>(event));
153 169
154 double double_time = 0.0; 170 double double_time = 0.0;
155 CHECK(args->GetDouble(1, &double_time)); 171 CHECK(args->GetDouble(1, &double_time));
156 base::Time start = base::Time::FromJsTime(double_time); 172 base::Time start = JsTimeToUTCTime(double_time);
157 CHECK(args->GetDouble(2, &double_time)); 173 CHECK(args->GetDouble(2, &double_time));
158 base::Time end = base::Time::FromJsTime(double_time); 174 base::Time end = JsTimeToUTCTime(double_time);
159 175
160 DictionaryValue* results = new DictionaryValue(); 176 DictionaryValue* results = new DictionaryValue();
161 ListValue* points_results = new ListValue(); 177 ListValue* points_results = new ListValue();
162 results->Set("points", points_results); 178 results->Set("points", points_results);
163 results->SetInteger("type", event_type); 179 results->SetInteger("eventType", event_type);
164 util::PostTaskToDatabaseThreadAndReply( 180 util::PostTaskToDatabaseThreadAndReply(
165 base::Bind(&DoGetEvents, points_results, event_type, start, end), 181 base::Bind(&DoGetEvents, points_results, event_type, start, end),
166 base::Bind(&WebUIHandler::ReturnResults, AsWeakPtr(), 182 base::Bind(&WebUIHandler::ReturnResults, AsWeakPtr(),
167 "performance_monitor.getEventsCallback", 183 "PerformanceMonitor.getEventsCallback",
168 base::Owned(results))); 184 base::Owned(results)));
169 } 185 }
170 186
171 void WebUIHandler::HandleGetAllMetricTypes(const ListValue* args) { 187 void WebUIHandler::HandleGetAllMetricTypes(const ListValue* args) {
172 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 188 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
173 CHECK_EQ(0u, args->GetSize()); 189 CHECK_EQ(0u, args->GetSize());
174 ListValue results; 190 ListValue results;
175 for (int i = 0; i < METRIC_NUMBER_OF_METRICS; ++i) { 191 for (int i = 0; i < METRIC_NUMBER_OF_METRICS; ++i) {
176 MetricType metric_type = static_cast<MetricType>(i); 192 MetricType metric_type = static_cast<MetricType>(i);
177 DictionaryValue* metric_type_info = new DictionaryValue(); 193 DictionaryValue* metric_type_info = new DictionaryValue();
178 metric_type_info->SetInteger("metricType", metric_type); 194 metric_type_info->SetInteger("metricType", metric_type);
179 metric_type_info->SetString("shortDescription", 195 metric_type_info->SetString("shortDescription",
180 MetricTypeToString(metric_type)); 196 MetricTypeToString(metric_type));
181 results.Append(metric_type_info); 197 results.Append(metric_type_info);
182 } 198 }
183 199
184 ReturnResults("performance_monitor.getAllMetricTypesCallback", &results); 200 ReturnResults("PerformanceMonitor.getAllMetricTypesCallback", &results);
185 } 201 }
186 202
187 void WebUIHandler::HandleGetMetric(const ListValue* args) { 203 void WebUIHandler::HandleGetMetric(const ListValue* args) {
188 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 204 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
189 CHECK_EQ(4u, args->GetSize()); 205 CHECK_EQ(4u, args->GetSize());
190 double metric = 0; 206 double metric = 0;
191 CHECK(args->GetDouble(0, &metric)); 207 CHECK(args->GetDouble(0, &metric));
192 MetricType metric_type = static_cast<MetricType>(static_cast<int>(metric)); 208 MetricType metric_type = static_cast<MetricType>(static_cast<int>(metric));
193 209
194 double double_time = 0.0; 210 double double_time = 0.0;
195 CHECK(args->GetDouble(1, &double_time)); 211 CHECK(args->GetDouble(1, &double_time));
196 base::Time start = base::Time::FromJsTime(double_time); 212 base::Time start = JsTimeToUTCTime(double_time);
197 CHECK(args->GetDouble(2, &double_time)); 213 CHECK(args->GetDouble(2, &double_time));
198 base::Time end = base::Time::FromJsTime(double_time); 214 base::Time end = JsTimeToUTCTime(double_time);
199 215
200 double resolution_in_milliseconds = 0; 216 double resolution_in_milliseconds = 0;
201 CHECK(args->GetDouble(3, &resolution_in_milliseconds)); 217 CHECK(args->GetDouble(3, &resolution_in_milliseconds));
202 base::TimeDelta resolution = 218 base::TimeDelta resolution =
203 base::TimeDelta::FromMilliseconds(resolution_in_milliseconds); 219 base::TimeDelta::FromMilliseconds(resolution_in_milliseconds);
204 220
205 DictionaryValue* results = new DictionaryValue(); 221 DictionaryValue* results = new DictionaryValue();
206 results->SetInteger("type", metric_type); 222 results->SetInteger("metricType", metric_type);
207 ListValue* points_results = new ListValue(); 223 ListValue* points_results = new ListValue();
208 results->Set("points", points_results); 224 results->Set("points", points_results);
209 util::PostTaskToDatabaseThreadAndReply( 225 util::PostTaskToDatabaseThreadAndReply(
210 base::Bind(&DoGetMetric, points_results, metric_type, 226 base::Bind(&DoGetMetric, points_results, metric_type,
211 start, end, resolution), 227 start, end, resolution),
212 base::Bind(&WebUIHandler::ReturnResults, AsWeakPtr(), 228 base::Bind(&WebUIHandler::ReturnResults, AsWeakPtr(),
213 "performance_monitor.getMetricCallback", 229 "PerformanceMonitor.getMetricCallback",
214 base::Owned(results))); 230 base::Owned(results)));
215 } 231 }
216 232
217 } // namespace performance_monitor 233 } // namespace performance_monitor
OLDNEW
« no previous file with comments | « chrome/browser/performance_monitor/performance_monitor.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698