1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved. 2a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 3a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// found in the LICENSE file. 4a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/browser/devtools/devtools_power_handler.h" 6a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 7a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/bind.h" 8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/values.h" 9a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/browser/devtools/devtools_protocol_constants.h" 10a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/browser/power_profiler/power_profiler_service.h" 11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace content { 13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 145f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)DevToolsPowerHandler::DevToolsPowerHandler() 155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) : is_profiling_(false) { 16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) RegisterCommandHandler(devtools::Power::start::kName, 17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) base::Bind(&DevToolsPowerHandler::OnStart, 18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) base::Unretained(this))); 19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) RegisterCommandHandler(devtools::Power::end::kName, 20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) base::Bind(&DevToolsPowerHandler::OnEnd, 21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) base::Unretained(this))); 22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) RegisterCommandHandler(devtools::Power::canProfilePower::kName, 23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) base::Bind(&DevToolsPowerHandler::OnCanProfilePower, 24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) base::Unretained(this))); 25116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch RegisterCommandHandler(devtools::Power::getAccuracyLevel::kName, 26116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch base::Bind(&DevToolsPowerHandler::OnGetAccuracyLevel, 27116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch base::Unretained(this))); 28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} 29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)DevToolsPowerHandler::~DevToolsPowerHandler() { 315f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) if (is_profiling_) 325f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) PowerProfilerService::GetInstance()->RemoveObserver(this); 33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} 34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void DevToolsPowerHandler::OnPowerEvent(const PowerEventVector& events) { 36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) base::DictionaryValue* params = new base::DictionaryValue(); 37a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) base::ListValue* event_list = new base::ListValue(); 38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) std::vector<PowerEvent>::const_iterator iter; 40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) for (iter = events.begin(); iter != events.end(); ++iter) { 41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) base::DictionaryValue* event_body = new base::DictionaryValue(); 42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) DCHECK(iter->type < PowerEvent::ID_COUNT); 44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) event_body->SetString("type", kPowerTypeNames[iter->type]); 45c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch // Use internal value to be consistent with Blink's 46c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch // monotonicallyIncreasingTime. 47c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch event_body->SetDouble("timestamp", iter->time.ToInternalValue() / 48c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch static_cast<double>(base::Time::kMicrosecondsPerMillisecond)); 49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) event_body->SetDouble("value", iter->value); 50a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) event_list->Append(event_body); 51a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } 52a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) params->Set(devtools::Power::dataAvailable::kParamValue, event_list); 54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) SendNotification(devtools::Power::dataAvailable::kName, params); 55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} 56a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 57a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)scoped_refptr<DevToolsProtocol::Response> 58a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)DevToolsPowerHandler::OnStart( 59a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) scoped_refptr<DevToolsProtocol::Command> command) { 60a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) if (PowerProfilerService::GetInstance()->IsAvailable()) { 61a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) PowerProfilerService::GetInstance()->AddObserver(this); 625f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) is_profiling_ = true; 63a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return command->SuccessResponse(NULL); 64a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } 65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return command->InternalErrorResponse("Power profiler service unavailable"); 67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} 68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)scoped_refptr<DevToolsProtocol::Response> 70a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)DevToolsPowerHandler::OnEnd(scoped_refptr<DevToolsProtocol::Command> command) { 71a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) if (PowerProfilerService::GetInstance()->IsAvailable()) { 72a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) PowerProfilerService::GetInstance()->RemoveObserver(this); 735f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) is_profiling_ = false; 74a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return command->SuccessResponse(NULL); 75a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) } 76a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 77a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return command->InternalErrorResponse("Power profiler service unavailable"); 78a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} 79a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 80a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)scoped_refptr<DevToolsProtocol::Response> 81a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)DevToolsPowerHandler::OnCanProfilePower( 82a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) scoped_refptr<DevToolsProtocol::Command> command) { 83a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) base::DictionaryValue* result = new base::DictionaryValue(); 84a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) result->SetBoolean(devtools::kResult, 85a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) PowerProfilerService::GetInstance()->IsAvailable()); 86a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 87a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return command->SuccessResponse(result); 88a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} 89a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 90116680a4aac90f2aa7413d9095a592090648e557Ben Murdochscoped_refptr<DevToolsProtocol::Response> 91116680a4aac90f2aa7413d9095a592090648e557Ben MurdochDevToolsPowerHandler::OnGetAccuracyLevel( 92116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch scoped_refptr<DevToolsProtocol::Command> command) { 93116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch if (PowerProfilerService::GetInstance()->IsAvailable()) { 94116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch base::DictionaryValue* result = new base::DictionaryValue(); 95116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch result->SetString( 96116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch devtools::kResult, 97116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch PowerProfilerService::GetInstance()->GetAccuracyLevel()); 98116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch return command->SuccessResponse(result); 99116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch } 100116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch return command->InternalErrorResponse("Power profiler service unavailable"); 101116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch} 102116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 103116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid DevToolsPowerHandler::OnClientDetached() { 1045f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) if (is_profiling_) 1055f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) PowerProfilerService::GetInstance()->RemoveObserver(this); 106116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch} 107116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 108a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} // namespace content 109