Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / devtools / protocol / power_handler.cc
blobc88097b86d7b92c95ce20b62d4927c8f7b0ec921
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"
9 namespace content {
10 namespace devtools {
11 namespace power {
13 typedef DevToolsProtocolClient::Response Response;
15 PowerHandler::PowerHandler() : enabled_(false) {
18 PowerHandler::~PowerHandler() {
19 if (enabled_)
20 PowerProfilerService::GetInstance()->RemoveObserver(this);
23 void PowerHandler::SetClient(scoped_ptr<Client> client) {
24 client_.swap(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() {
44 if (enabled_) {
45 PowerProfilerService::GetInstance()->RemoveObserver(this);
46 enabled_ = false;
50 Response PowerHandler::Start() {
51 if (!PowerProfilerService::GetInstance()->IsAvailable())
52 return Response::InternalError("Power profiler service unavailable");
53 if (!enabled_) {
54 PowerProfilerService::GetInstance()->AddObserver(this);
55 enabled_ = true;
57 return Response::OK();
60 Response PowerHandler::End() {
61 if (!PowerProfilerService::GetInstance()->IsAvailable())
62 return Response::InternalError("Power profiler service unavailable");
63 if (enabled_) {
64 PowerProfilerService::GetInstance()->RemoveObserver(this);
65 enabled_ = false;
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();
82 } // namespace power
83 } // namespace devtools
84 } // namespace content