summaryrefslogtreecommitdiffstats
path: root/chromeos/dbus/amplifier_client.cc
blob: 5a3ffa6c3f4413b650054b1c3abc53e233defad4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/dbus/amplifier_client.h"

#include "base/bind.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/observer_list.h"
#include "dbus/bus.h"
#include "dbus/message.h"
#include "dbus/object_path.h"
#include "dbus/object_proxy.h"

namespace chromeos {
namespace {

// TODO(benchan): Move these DBus constants to system_api.
namespace amplifier {

const char kAmplifierInterface[] = "org.chromium.Amplifier";
const char kAmplifierServiceName[] = "org.chromium.Amplifier";
const char kAmplifierServicePath[] = "/org/chromium/Amplifier";
const char kInitializeMethod[] = "Initialize";
const char kSetStandbyModeMethod[] = "SetStandbyMode";
const char kSetVolumeMethod[] = "SetVolume";
const char kErrorSignal[] = "Error";

}  // namespace amplifier

void OnBoolDBusMethod(const BoolDBusMethodCallback& callback,
                      dbus::Response* response) {
  if (!response) {
    callback.Run(DBUS_METHOD_CALL_FAILURE, false);
    return;
  }

  dbus::MessageReader reader(response);
  bool result;
  if (!reader.PopBool(&result)) {
    callback.Run(DBUS_METHOD_CALL_FAILURE, false);
    return;
  }

  callback.Run(DBUS_METHOD_CALL_SUCCESS, result);
}

void OnVoidDBusMethod(const VoidDBusMethodCallback& callback,
                      dbus::Response* response) {
  callback.Run(response ? DBUS_METHOD_CALL_SUCCESS : DBUS_METHOD_CALL_FAILURE);
}

// The AmplifierClient implementation.
class AmplifierClientImpl : public AmplifierClient {
 public:
  AmplifierClientImpl()
      : proxy_(nullptr), signal_connected_(false), weak_ptr_factory_(this) {}

  ~AmplifierClientImpl() override {}

  // DBusClient overrides:
  void Init(dbus::Bus* bus) override;

  // AmplifierClient overrides:
  void AddObserver(Observer* observer) override;
  void RemoveObserver(Observer* observer) override;
  void Initialize(const BoolDBusMethodCallback& callback) override;
  void SetStandbyMode(bool standby,
                      const VoidDBusMethodCallback& callback) override;
  void SetVolume(double db_spl,
                 const VoidDBusMethodCallback& callback) override;

 private:
  // Handles Error signal and notifies |observers_|.
  void OnError(dbus::Signal* signal);

  // Handles the result of signal connection setup.
  void OnSignalConnected(const std::string& interface,
                         const std::string& signal,
                         bool succeeded);

  dbus::ObjectProxy* proxy_;

  // True when |proxy_| has been connected to the Error signal.
  bool signal_connected_;

  // List of observers interested in event notifications from us.
  base::ObserverList<Observer> observers_;

  // Note: This should remain the last member so it'll be destroyed and
  // invalidate its weak pointers before any other members are destroyed.
  base::WeakPtrFactory<AmplifierClientImpl> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(AmplifierClientImpl);
};

void AmplifierClientImpl::Init(dbus::Bus* bus) {
  proxy_ =
      bus->GetObjectProxy(amplifier::kAmplifierServiceName,
                          dbus::ObjectPath(amplifier::kAmplifierServicePath));
  DCHECK(proxy_);
}

void AmplifierClientImpl::AddObserver(Observer* observer) {
  DCHECK(observer);
  if (!signal_connected_ && proxy_) {
    signal_connected_ = true;
    proxy_->ConnectToSignal(amplifier::kAmplifierInterface,
                            amplifier::kErrorSignal,
                            base::Bind(&AmplifierClientImpl::OnError,
                                       weak_ptr_factory_.GetWeakPtr()),
                            base::Bind(&AmplifierClientImpl::OnSignalConnected,
                                       weak_ptr_factory_.GetWeakPtr()));
  }
  observers_.AddObserver(observer);
}

void AmplifierClientImpl::RemoveObserver(Observer* observer) {
  DCHECK(observer);
  observers_.RemoveObserver(observer);
}

void AmplifierClientImpl::Initialize(const BoolDBusMethodCallback& callback) {
  dbus::MethodCall method_call(amplifier::kAmplifierInterface,
                               amplifier::kInitializeMethod);
  DCHECK(proxy_);
  proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
                     base::Bind(&OnBoolDBusMethod, callback));
}

void AmplifierClientImpl::SetStandbyMode(
    bool standby,
    const VoidDBusMethodCallback& callback) {
  dbus::MethodCall method_call(amplifier::kAmplifierInterface,
                               amplifier::kSetStandbyModeMethod);
  dbus::MessageWriter writer(&method_call);
  writer.AppendBool(standby);
  DCHECK(proxy_);
  proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
                     base::Bind(&OnVoidDBusMethod, callback));
}

void AmplifierClientImpl::SetVolume(double db_spl,
                                    const VoidDBusMethodCallback& callback) {
  dbus::MethodCall method_call(amplifier::kAmplifierInterface,
                               amplifier::kSetVolumeMethod);
  dbus::MessageWriter writer(&method_call);
  writer.AppendDouble(db_spl);
  DCHECK(proxy_);
  proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
                     base::Bind(&OnVoidDBusMethod, callback));
}

void AmplifierClientImpl::OnError(dbus::Signal* signal) {
  dbus::MessageReader reader(signal);
  int32 error_code = 0;
  if (!reader.PopInt32(&error_code)) {
    LOG(ERROR) << "Invalid signal: " << signal->ToString();
    return;
  }
  FOR_EACH_OBSERVER(Observer, observers_, OnError(error_code));
}

void AmplifierClientImpl::OnSignalConnected(const std::string& interface,
                                            const std::string& signal,
                                            bool succeeded) {
  LOG_IF(ERROR, !succeeded) << "Connect to " << interface << " " << signal
                            << " failed.";
}

}  // anonymous namespace

AmplifierClient::AmplifierClient() {
}

AmplifierClient::~AmplifierClient() {
}

// static
AmplifierClient* AmplifierClient::Create() {
  return new AmplifierClientImpl();
}

}  // namespace chromeos