Rename Animate as Begin(Main)Frame
[chromium-blink-merge.git] / content / browser / devtools / devtools_power_handler.cc
blob339ead93fcb10d53b27f5b540659a88af9c0f1d8
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/devtools/devtools_power_handler.h"
7 #include "base/bind.h"
8 #include "base/values.h"
9 #include "content/browser/devtools/devtools_protocol_constants.h"
10 #include "content/browser/power_profiler/power_profiler_service.h"
12 namespace content {
14 DevToolsPowerHandler::DevToolsPowerHandler()
15 : is_profiling_(false) {
16 RegisterCommandHandler(devtools::Power::start::kName,
17 base::Bind(&DevToolsPowerHandler::OnStart,
18 base::Unretained(this)));
19 RegisterCommandHandler(devtools::Power::end::kName,
20 base::Bind(&DevToolsPowerHandler::OnEnd,
21 base::Unretained(this)));
22 RegisterCommandHandler(devtools::Power::canProfilePower::kName,
23 base::Bind(&DevToolsPowerHandler::OnCanProfilePower,
24 base::Unretained(this)));
25 RegisterCommandHandler(devtools::Power::getAccuracyLevel::kName,
26 base::Bind(&DevToolsPowerHandler::OnGetAccuracyLevel,
27 base::Unretained(this)));
30 DevToolsPowerHandler::~DevToolsPowerHandler() {
31 if (is_profiling_)
32 PowerProfilerService::GetInstance()->RemoveObserver(this);
35 void DevToolsPowerHandler::OnPowerEvent(const PowerEventVector& events) {
36 base::DictionaryValue* params = new base::DictionaryValue();
37 base::ListValue* event_list = new base::ListValue();
39 std::vector<PowerEvent>::const_iterator iter;
40 for (iter = events.begin(); iter != events.end(); ++iter) {
41 base::DictionaryValue* event_body = new base::DictionaryValue();
43 DCHECK(iter->type < PowerEvent::ID_COUNT);
44 event_body->SetString("type", kPowerTypeNames[iter->type]);
45 // Use internal value to be consistent with Blink's
46 // monotonicallyIncreasingTime.
47 event_body->SetDouble("timestamp", iter->time.ToInternalValue() /
48 static_cast<double>(base::Time::kMicrosecondsPerMillisecond));
49 event_body->SetDouble("value", iter->value);
50 event_list->Append(event_body);
53 params->Set(devtools::Power::dataAvailable::kParamValue, event_list);
54 SendNotification(devtools::Power::dataAvailable::kName, params);
57 scoped_refptr<DevToolsProtocol::Response>
58 DevToolsPowerHandler::OnStart(
59 scoped_refptr<DevToolsProtocol::Command> command) {
60 if (PowerProfilerService::GetInstance()->IsAvailable()) {
61 PowerProfilerService::GetInstance()->AddObserver(this);
62 is_profiling_ = true;
63 return command->SuccessResponse(NULL);
66 return command->InternalErrorResponse("Power profiler service unavailable");
69 scoped_refptr<DevToolsProtocol::Response>
70 DevToolsPowerHandler::OnEnd(scoped_refptr<DevToolsProtocol::Command> command) {
71 if (PowerProfilerService::GetInstance()->IsAvailable()) {
72 PowerProfilerService::GetInstance()->RemoveObserver(this);
73 is_profiling_ = false;
74 return command->SuccessResponse(NULL);
77 return command->InternalErrorResponse("Power profiler service unavailable");
80 scoped_refptr<DevToolsProtocol::Response>
81 DevToolsPowerHandler::OnCanProfilePower(
82 scoped_refptr<DevToolsProtocol::Command> command) {
83 base::DictionaryValue* result = new base::DictionaryValue();
84 result->SetBoolean(devtools::kResult,
85 PowerProfilerService::GetInstance()->IsAvailable());
87 return command->SuccessResponse(result);
90 scoped_refptr<DevToolsProtocol::Response>
91 DevToolsPowerHandler::OnGetAccuracyLevel(
92 scoped_refptr<DevToolsProtocol::Command> command) {
93 if (PowerProfilerService::GetInstance()->IsAvailable()) {
94 base::DictionaryValue* result = new base::DictionaryValue();
95 result->SetString(
96 devtools::kResult,
97 PowerProfilerService::GetInstance()->GetAccuracyLevel());
98 return command->SuccessResponse(result);
100 return command->InternalErrorResponse("Power profiler service unavailable");
103 void DevToolsPowerHandler::OnClientDetached() {
104 if (is_profiling_)
105 PowerProfilerService::GetInstance()->RemoveObserver(this);
108 } // namespace content