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/protocol/power_handler.h"
7 #include "content/browser/power_profiler/power_profiler_service.h"
13 typedef DevToolsProtocolClient::Response Response
;
15 PowerHandler::PowerHandler() : enabled_(false) {
18 PowerHandler::~PowerHandler() {
20 PowerProfilerService::GetInstance()->RemoveObserver(this);
23 void PowerHandler::SetClient(scoped_ptr
<Client
> client
) {
27 void PowerHandler::OnPowerEvent(const PowerEventVector
& events
) {
28 std::vector
<scoped_refptr
<PowerEvent
>> event_list
;
29 for (const auto& event
: events
) {
30 DCHECK(event
.type
< content::PowerEvent::ID_COUNT
);
31 // Use internal value to be consistent with Blink's
32 // monotonicallyIncreasingTime.
33 double timestamp
= event
.time
.ToInternalValue() /
34 static_cast<double>(base::Time::kMicrosecondsPerMillisecond
);
35 event_list
.push_back(PowerEvent::Create()
36 ->set_type(kPowerTypeNames
[event
.type
])
37 ->set_timestamp(timestamp
)
38 ->set_value(event
.value
));
40 client_
->DataAvailable(DataAvailableParams::Create()->set_value(event_list
));
43 void PowerHandler::Detached() {
45 PowerProfilerService::GetInstance()->RemoveObserver(this);
50 Response
PowerHandler::Start() {
51 if (!PowerProfilerService::GetInstance()->IsAvailable())
52 return Response::InternalError("Power profiler service unavailable");
54 PowerProfilerService::GetInstance()->AddObserver(this);
57 return Response::OK();
60 Response
PowerHandler::End() {
61 if (!PowerProfilerService::GetInstance()->IsAvailable())
62 return Response::InternalError("Power profiler service unavailable");
64 PowerProfilerService::GetInstance()->RemoveObserver(this);
67 return Response::OK();
70 Response
PowerHandler::CanProfilePower(bool* result
) {
71 *result
= PowerProfilerService::GetInstance()->IsAvailable();
72 return Response::OK();
75 Response
PowerHandler::GetAccuracyLevel(std::string
* result
) {
76 if (!PowerProfilerService::GetInstance()->IsAvailable())
77 return Response::InternalError("Power profiler service unavailable");
78 *result
= PowerProfilerService::GetInstance()->GetAccuracyLevel();
79 return Response::OK();
83 } // namespace devtools
84 } // namespace content