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.
4
5#include "battery_status_dispatcher.h"
6
7#include "base/logging.h"
8#include "content/common/battery_status_messages.h"
9#include "content/renderer/render_thread_impl.h"
10#include "third_party/WebKit/public/platform/WebBatteryStatusListener.h"
11
12namespace content {
13
14BatteryStatusDispatcher::BatteryStatusDispatcher(RenderThread* thread)
15    : listener_(0) {
16  if (thread)
17    thread->AddObserver(this);
18}
19
20BatteryStatusDispatcher::~BatteryStatusDispatcher() {
21  if (listener_)
22    Stop();
23}
24
25bool BatteryStatusDispatcher::SetListener(
26    blink::WebBatteryStatusListener* listener) {
27  listener_ = listener;
28  return listener ? Start() : Stop();
29}
30
31bool BatteryStatusDispatcher::OnControlMessageReceived(
32    const IPC::Message& message) {
33  bool handled = true;
34  IPC_BEGIN_MESSAGE_MAP(BatteryStatusDispatcher, message)
35    IPC_MESSAGE_HANDLER(BatteryStatusMsg_DidChange, OnDidChange)
36    IPC_MESSAGE_UNHANDLED(handled = false)
37  IPC_END_MESSAGE_MAP()
38  return handled;
39}
40
41bool BatteryStatusDispatcher::Start() {
42  return RenderThread::Get()->Send(new BatteryStatusHostMsg_Start());
43}
44
45bool BatteryStatusDispatcher::Stop() {
46  return RenderThread::Get()->Send(new BatteryStatusHostMsg_Stop());
47}
48
49void BatteryStatusDispatcher::OnDidChange(
50    const blink::WebBatteryStatus& status) {
51  if (listener_)
52    listener_->updateBatteryStatus(status);
53}
54
55}  // namespace content
56