Revert 268405 "Make sure that ScratchBuffer::Allocate() always r..."
[chromium-blink-merge.git] / content / browser / devtools / devtools_power_handler.cc
blob27fdf294c6fe88cb35343980cedd6e6f56beb3f3
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 RegisterCommandHandler(devtools::Power::start::kName,
16 base::Bind(&DevToolsPowerHandler::OnStart,
17 base::Unretained(this)));
18 RegisterCommandHandler(devtools::Power::end::kName,
19 base::Bind(&DevToolsPowerHandler::OnEnd,
20 base::Unretained(this)));
21 RegisterCommandHandler(devtools::Power::canProfilePower::kName,
22 base::Bind(&DevToolsPowerHandler::OnCanProfilePower,
23 base::Unretained(this)));
26 DevToolsPowerHandler::~DevToolsPowerHandler() {
29 void DevToolsPowerHandler::OnPowerEvent(const PowerEventVector& events) {
30 base::DictionaryValue* params = new base::DictionaryValue();
31 base::ListValue* event_list = new base::ListValue();
33 std::vector<PowerEvent>::const_iterator iter;
34 for (iter = events.begin(); iter != events.end(); ++iter) {
35 base::DictionaryValue* event_body = new base::DictionaryValue();
37 DCHECK(iter->type < PowerEvent::ID_COUNT);
38 event_body->SetString("type", kPowerTypeNames[iter->type]);
39 // Use internal value to be consistent with Blink's
40 // monotonicallyIncreasingTime.
41 event_body->SetDouble("timestamp", iter->time.ToInternalValue() /
42 static_cast<double>(base::Time::kMicrosecondsPerMillisecond));
43 event_body->SetDouble("value", iter->value);
44 event_list->Append(event_body);
47 params->Set(devtools::Power::dataAvailable::kParamValue, event_list);
48 SendNotification(devtools::Power::dataAvailable::kName, params);
51 scoped_refptr<DevToolsProtocol::Response>
52 DevToolsPowerHandler::OnStart(
53 scoped_refptr<DevToolsProtocol::Command> command) {
54 if (PowerProfilerService::GetInstance()->IsAvailable()) {
55 PowerProfilerService::GetInstance()->AddObserver(this);
56 return command->SuccessResponse(NULL);
59 return command->InternalErrorResponse("Power profiler service unavailable");
62 scoped_refptr<DevToolsProtocol::Response>
63 DevToolsPowerHandler::OnEnd(scoped_refptr<DevToolsProtocol::Command> command) {
64 if (PowerProfilerService::GetInstance()->IsAvailable()) {
65 PowerProfilerService::GetInstance()->RemoveObserver(this);
66 return command->SuccessResponse(NULL);
69 return command->InternalErrorResponse("Power profiler service unavailable");
72 scoped_refptr<DevToolsProtocol::Response>
73 DevToolsPowerHandler::OnCanProfilePower(
74 scoped_refptr<DevToolsProtocol::Command> command) {
75 base::DictionaryValue* result = new base::DictionaryValue();
76 result->SetBoolean(devtools::kResult,
77 PowerProfilerService::GetInstance()->IsAvailable());
79 return command->SuccessResponse(result);
82 } // namespace content