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

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

Issue 10702117: Chrome Performance Monitor: Metric API Functions. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Fixed it up. 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
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/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"
15 #include "content/public/browser/browser_thread.h" 16 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/web_ui.h" 17 #include "content/public/browser/web_ui.h"
17 18
18 namespace performance_monitor { 19 namespace performance_monitor {
19 namespace { 20 namespace {
20 21
21 void DoGetActiveIntervals(ListValue* results, 22 void DoGetActiveIntervals(ListValue* results,
22 const base::Time& start, const base::Time& end) { 23 const base::Time& start, const base::Time& end) {
23 std::vector<TimeRange> intervals = 24 Database* db = PerformanceMonitor::GetInstance()->database();
24 PerformanceMonitor::GetInstance()->database()->GetActiveIntervals( 25 std::vector<TimeRange> intervals = db->GetActiveIntervals(start, end);
25 start, end);
26 for (std::vector<TimeRange>::iterator it = intervals.begin(); 26 for (std::vector<TimeRange>::iterator it = intervals.begin();
27 it != intervals.end(); ++it) { 27 it != intervals.end(); ++it) {
28 DictionaryValue* interval_value = new DictionaryValue(); 28 DictionaryValue* interval_value = new DictionaryValue();
29 interval_value->SetDouble("start", it->start.ToDoubleT()); 29 interval_value->SetDouble("start", it->start.ToDoubleT());
30 interval_value->SetDouble("end", it->end.ToDoubleT()); 30 interval_value->SetDouble("end", it->end.ToDoubleT());
31 results->Append(interval_value); 31 results->Append(interval_value);
32 } 32 }
33 } 33 }
34 34
35 // TODO(mtytel): Add an internationalized longDescription field to each event. 35 // TODO(mtytel): Add an internationalized longDescription field to each event.
36 void DoGetEvents(ListValue* results, EventType event, 36 void DoGetEvents(ListValue* results, EventType event,
37 const base::Time& start, const base::Time& end) { 37 const base::Time& start, const base::Time& end) {
38 std::vector<linked_ptr<Event> > events = 38 Database* db = PerformanceMonitor::GetInstance()->database();
39 PerformanceMonitor::GetInstance()->database()->GetEvents( 39 std::vector<linked_ptr<Event> > events = db->GetEvents(event, start, end);
40 event, start, end);
41 for (std::vector<linked_ptr<Event> >::iterator it = events.begin(); 40 for (std::vector<linked_ptr<Event> >::iterator it = events.begin();
42 it != events.end(); ++it) { 41 it != events.end(); ++it) {
43 results->Append((*it)->data()); 42 results->Append((*it)->data()->DeepCopy());
44 } 43 }
45 } 44 }
46 45
47 bool PostTaskToDatabaseAndReply(const base::Closure& request, 46 void DoGetMetric(ListValue* results,
Evan Stade 2012/07/10 07:59:39 docs
Matt Tytel 2012/07/10 19:08:19 Done.
48 const base::Closure& reply) { 47 MetricType metric_type,
49 base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool(); 48 const base::Time& start, const base::Time& end,
50 base::SequencedWorkerPool::SequenceToken token = pool->GetSequenceToken(); 49 const base::TimeDelta& resolution) {
51 return pool->GetSequencedTaskRunner(token)->PostTaskAndReply( 50 Database* db = PerformanceMonitor::GetInstance()->database();
52 FROM_HERE, request, reply); 51 Database::MetricVectorMap metric_vector_map = db->GetStatsForMetricByActivity(
52 MetricTypeToString(metric_type), start, end);
53
54 linked_ptr<Database::MetricInfoVector> metric_vector =
55 metric_vector_map[kProcessChromeAggregate];
56 if (!metric_vector.get())
57 metric_vector.reset(new Database::MetricInfoVector());
58
59 Database::MetricInfoVector aggregated_metrics =
60 util::AggregateMetric(*(metric_vector.get()), start, resolution);
61 for (Database::MetricInfoVector::iterator it = aggregated_metrics.begin();
62 it != aggregated_metrics.end(); ++it) {
63 DictionaryValue* metric_value = new DictionaryValue();
64 metric_value->SetDouble("time", it->time.ToDoubleT());
65 metric_value->SetDouble("value", it->value);
66 results->Append(metric_value);
67 }
53 } 68 }
54 69
55 } // namespace 70 } // namespace
56 71
57 WebUIHandler::WebUIHandler() {} 72 WebUIHandler::WebUIHandler() {}
58 WebUIHandler::~WebUIHandler() {} 73 WebUIHandler::~WebUIHandler() {}
59 74
60 void WebUIHandler::RegisterMessages() { 75 void WebUIHandler::RegisterMessages() {
61 web_ui()->RegisterMessageCallback( 76 web_ui()->RegisterMessageCallback(
62 "getActiveIntervals", 77 "getActiveIntervals",
63 base::Bind(&WebUIHandler::HandleGetActiveIntervals, 78 base::Bind(&WebUIHandler::HandleGetActiveIntervals,
64 base::Unretained(this))); 79 base::Unretained(this)));
65 web_ui()->RegisterMessageCallback( 80 web_ui()->RegisterMessageCallback(
66 "getAllEventTypes", 81 "getAllEventTypes",
67 base::Bind(&WebUIHandler::HandleGetAllEventTypes, 82 base::Bind(&WebUIHandler::HandleGetAllEventTypes,
68 base::Unretained(this))); 83 base::Unretained(this)));
69 web_ui()->RegisterMessageCallback( 84 web_ui()->RegisterMessageCallback(
70 "getEvents", 85 "getEvents",
71 base::Bind(&WebUIHandler::HandleGetEvents, 86 base::Bind(&WebUIHandler::HandleGetEvents,
72 base::Unretained(this))); 87 base::Unretained(this)));
88 web_ui()->RegisterMessageCallback(
89 "getAllMetricTypes",
90 base::Bind(&WebUIHandler::HandleGetAllMetricTypes,
91 base::Unretained(this)));
92 web_ui()->RegisterMessageCallback(
93 "getMetric",
94 base::Bind(&WebUIHandler::HandleGetMetric,
95 base::Unretained(this)));
73 } 96 }
74 97
75 void WebUIHandler::ReturnResults(const std::string& function, 98 void WebUIHandler::ReturnResults(const std::string& function,
76 const ListValue* results) { 99 const ListValue* results) {
77 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 100 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
78 web_ui()->CallJavascriptFunction(function, *results); 101 web_ui()->CallJavascriptFunction(function, *results);
79 } 102 }
80 103
81 void WebUIHandler::HandleGetActiveIntervals(const ListValue* args) { 104 void WebUIHandler::HandleGetActiveIntervals(const ListValue* args) {
82 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 105 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
83 CHECK_EQ(2u, args->GetSize()); 106 CHECK_EQ(2u, args->GetSize());
84 double double_time = 0.0; 107 double double_time = 0.0;
85 CHECK(args->GetDouble(0, &double_time)); 108 CHECK(args->GetDouble(0, &double_time));
86 base::Time start = base::Time::FromJsTime(double_time); 109 base::Time start = base::Time::FromJsTime(double_time);
87 CHECK(args->GetDouble(1, &double_time)); 110 CHECK(args->GetDouble(1, &double_time));
88 base::Time end = base::Time::FromJsTime(double_time); 111 base::Time end = base::Time::FromJsTime(double_time);
89 112
90 ListValue* results = new ListValue(); 113 ListValue* results = new ListValue();
91 PostTaskToDatabaseAndReply( 114 util::PostTaskToDatabaseAndReply(
92 base::Bind(&DoGetActiveIntervals, results, start, end), 115 base::Bind(&DoGetActiveIntervals, results, start, end),
93 base::Bind(&WebUIHandler::ReturnResults, AsWeakPtr(), 116 base::Bind(&WebUIHandler::ReturnResults, AsWeakPtr(),
94 "performance_monitor.getActiveIntervalsCallback", 117 "performance_monitor.getActiveIntervalsCallback",
95 base::Owned(results))); 118 base::Owned(results)));
96 } 119 }
97 120
98 void WebUIHandler::HandleGetAllEventTypes(const ListValue* args) { 121 void WebUIHandler::HandleGetAllEventTypes(const ListValue* args) {
99 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 122 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
100 CHECK_EQ(0u, args->GetSize()); 123 CHECK_EQ(0u, args->GetSize());
101 ListValue results; 124 ListValue results;
102 for (int i = 0; i < EVENT_NUMBER_OF_EVENTS; ++i) { 125 for (int i = 0; i < EVENT_NUMBER_OF_EVENTS; ++i) {
103 EventType event_type = static_cast<EventType>(i); 126 EventType event_type = static_cast<EventType>(i);
104 DictionaryValue* event_type_info = new DictionaryValue(); 127 DictionaryValue* event_type_info = new DictionaryValue();
105 event_type_info->SetInteger("eventType", event_type); 128 event_type_info->SetInteger("eventType", event_type);
106 event_type_info->SetString("shortDescription", 129 event_type_info->SetString("shortDescription",
107 EventTypeToString(event_type)); 130 EventTypeToString(event_type));
108 results.Append(event_type_info); 131 results.Append(event_type_info);
109 } 132 }
110 ReturnResults("performance_monitor.getAllEventTypesCallback", &results); 133 ReturnResults("performance_monitor.getAllEventTypesCallback", &results);
111 } 134 }
112 135
113 void WebUIHandler::HandleGetEvents(const ListValue* args) { 136 void WebUIHandler::HandleGetEvents(const ListValue* args) {
114 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 137 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
115 CHECK_EQ(3u, args->GetSize()); 138 CHECK_EQ(3u, args->GetSize());
116 int event = 0; 139 double event = 0;
117 CHECK(args->GetInteger(0, &event)); 140 CHECK(args->GetDouble(0, &event));
118 double double_time = 0.0; 141 double double_time = 0.0;
119 CHECK(args->GetDouble(1, &double_time)); 142 CHECK(args->GetDouble(1, &double_time));
120 base::Time start = base::Time::FromJsTime(double_time); 143 base::Time start = base::Time::FromJsTime(double_time);
121 CHECK(args->GetDouble(2, &double_time)); 144 CHECK(args->GetDouble(2, &double_time));
122 base::Time end = base::Time::FromJsTime(double_time); 145 base::Time end = base::Time::FromJsTime(double_time);
123 146
124 ListValue* results = new ListValue(); 147 ListValue* results = new ListValue();
125 PostTaskToDatabaseAndReply( 148 util::PostTaskToDatabaseAndReply(
126 base::Bind(&DoGetEvents, results, static_cast<EventType>(event), 149 base::Bind(&DoGetEvents, results, static_cast<EventType>(event),
127 start, end), 150 start, end),
128 base::Bind(&WebUIHandler::ReturnResults, AsWeakPtr(), 151 base::Bind(&WebUIHandler::ReturnResults, AsWeakPtr(),
129 "performance_monitor.getEventsCallback", 152 "performance_monitor.getEventsCallback",
130 base::Owned(results))); 153 base::Owned(results)));
131 } 154 }
132 155
156 void WebUIHandler::HandleGetAllMetricTypes(const ListValue* args) {
157 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
158 CHECK_EQ(0u, args->GetSize());
159 ListValue results;
160 for (int i = 0; i < METRIC_NUMBER_OF_METRICS; ++i) {
161 MetricType metric_type = static_cast<MetricType>(i);
162 DictionaryValue* metric_type_info = new DictionaryValue();
163 metric_type_info->SetInteger("metricType", metric_type);
164 metric_type_info->SetString("shortDescription",
165 MetricTypeToString(metric_type));
Evan Stade 2012/07/10 07:59:39 indent off by one
Matt Tytel 2012/07/10 19:08:19 Done.
166 results.Append(metric_type_info);
167 }
Evan Stade 2012/07/10 07:59:39 \n
Matt Tytel 2012/07/10 19:08:19 Done.
168 ReturnResults("performance_monitor.getAllMetricTypesCallback", &results);
169 }
170
171 void WebUIHandler::HandleGetMetric(const ListValue* args) {
172 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
173 CHECK_EQ(4u, args->GetSize());
174 double metric = 0;
175 CHECK(args->GetDouble(0, &metric));
176
177 double double_time = 0.0;
178 CHECK(args->GetDouble(1, &double_time));
179 base::Time start = base::Time::FromJsTime(double_time);
180 CHECK(args->GetDouble(2, &double_time));
181 base::Time end = base::Time::FromJsTime(double_time);
182
183 double resolution_in_milliseconds = 0;
184 CHECK(args->GetDouble(3, &resolution_in_milliseconds));
185 base::TimeDelta resolution =
186 base::TimeDelta::FromMilliseconds(resolution_in_milliseconds);
187
188 ListValue* results = new ListValue();
189 util::PostTaskToDatabaseAndReply(
190 base::Bind(&DoGetMetric, results, static_cast<MetricType>(metric),
191 start, end, resolution),
192 base::Bind(&WebUIHandler::ReturnResults, AsWeakPtr(),
193 "performance_monitor.getMetricCallback",
194 base::Owned(results)));
195 }
196
133 } // namespace performance_monitor 197 } // namespace performance_monitor
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698