Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chromeos / dbus / modem_messaging_client.cc
blob731adf90f478627169e4ea62ae00fa6ee050ed04
1 // Copyright (c) 2012 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 #include "chromeos/dbus/modem_messaging_client.h"
6 #include <utility>
8 #include "base/bind.h"
9 #include "base/containers/scoped_ptr_map.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/values.h"
14 #include "dbus/bus.h"
15 #include "dbus/message.h"
16 #include "dbus/object_proxy.h"
17 #include "third_party/cros_system_api/dbus/service_constants.h"
19 namespace chromeos {
21 namespace {
23 // A class which makes method calls for SMS services via the
24 // org.freedesktop.ModemManager1.Messaging object.
25 class ModemMessagingProxy {
26 public:
27 typedef ModemMessagingClient::SmsReceivedHandler SmsReceivedHandler;
28 typedef ModemMessagingClient::ListCallback ListCallback;
29 typedef ModemMessagingClient::DeleteCallback DeleteCallback;
31 ModemMessagingProxy(dbus::Bus* bus,
32 const std::string& service_name,
33 const dbus::ObjectPath& object_path)
34 : proxy_(bus->GetObjectProxy(service_name, object_path)),
35 service_name_(service_name),
36 weak_ptr_factory_(this) {
37 proxy_->ConnectToSignal(
38 modemmanager::kModemManager1MessagingInterface,
39 modemmanager::kSMSAddedSignal,
40 base::Bind(&ModemMessagingProxy::OnSmsAdded,
41 weak_ptr_factory_.GetWeakPtr()),
42 base::Bind(&ModemMessagingProxy::OnSignalConnected,
43 weak_ptr_factory_.GetWeakPtr()));
45 virtual ~ModemMessagingProxy() {}
47 // Sets SmsReceived signal handler.
48 void SetSmsReceivedHandler(const SmsReceivedHandler& handler) {
49 DCHECK(sms_received_handler_.is_null());
50 sms_received_handler_ = handler;
53 // Resets SmsReceived signal handler.
54 void ResetSmsReceivedHandler() {
55 sms_received_handler_.Reset();
58 // Calls Delete method.
59 void Delete(const dbus::ObjectPath& message_path,
60 const DeleteCallback& callback) {
61 dbus::MethodCall method_call(modemmanager::kModemManager1MessagingInterface,
62 modemmanager::kSMSDeleteFunction);
63 dbus::MessageWriter writer(&method_call);
64 writer.AppendObjectPath(message_path);
65 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
66 base::Bind(&ModemMessagingProxy::OnDelete,
67 weak_ptr_factory_.GetWeakPtr(),
68 callback));
71 // Calls List method.
72 virtual void List(const ListCallback& callback) {
73 dbus::MethodCall method_call(modemmanager::kModemManager1MessagingInterface,
74 modemmanager::kSMSListFunction);
75 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
76 base::Bind(&ModemMessagingProxy::OnList,
77 weak_ptr_factory_.GetWeakPtr(),
78 callback));
81 private:
82 // Handles SmsAdded signal.
83 void OnSmsAdded(dbus::Signal* signal) {
84 dbus::ObjectPath message_path;
85 bool complete = false;
86 dbus::MessageReader reader(signal);
87 if (!reader.PopObjectPath(&message_path) ||
88 !reader.PopBool(&complete)) {
89 LOG(ERROR) << "Invalid signal: " << signal->ToString();
90 return;
92 if (!sms_received_handler_.is_null()) {
93 sms_received_handler_.Run(message_path, complete);
97 // Handles responses of Delete method calls.
98 void OnDelete(const DeleteCallback& callback, dbus::Response* response) {
99 if (!response)
100 return;
101 callback.Run();
104 // Handles responses of List method calls.
105 void OnList(const ListCallback& callback, dbus::Response* response) {
106 if (!response)
107 return;
108 dbus::MessageReader reader(response);
109 std::vector<dbus::ObjectPath> sms_paths;
110 if (!reader.PopArrayOfObjectPaths(&sms_paths))
111 LOG(WARNING) << "Invalid response: " << response->ToString();
112 callback.Run(sms_paths);
115 // Handles the result of signal connection setup.
116 void OnSignalConnected(const std::string& interface,
117 const std::string& signal,
118 bool succeeded) {
119 LOG_IF(ERROR, !succeeded) << "Connect to " << interface << " "
120 << signal << " failed.";
123 dbus::ObjectProxy* proxy_;
124 std::string service_name_;
125 SmsReceivedHandler sms_received_handler_;
127 // Note: This should remain the last member so it'll be destroyed and
128 // invalidate its weak pointers before any other members are destroyed.
129 base::WeakPtrFactory<ModemMessagingProxy> weak_ptr_factory_;
131 DISALLOW_COPY_AND_ASSIGN(ModemMessagingProxy);
134 class CHROMEOS_EXPORT ModemMessagingClientImpl : public ModemMessagingClient {
135 public:
136 ModemMessagingClientImpl() : bus_(NULL) {}
138 void SetSmsReceivedHandler(const std::string& service_name,
139 const dbus::ObjectPath& object_path,
140 const SmsReceivedHandler& handler) override {
141 GetProxy(service_name, object_path)->SetSmsReceivedHandler(handler);
144 void ResetSmsReceivedHandler(const std::string& service_name,
145 const dbus::ObjectPath& object_path) override {
146 GetProxy(service_name, object_path)->ResetSmsReceivedHandler();
149 void Delete(const std::string& service_name,
150 const dbus::ObjectPath& object_path,
151 const dbus::ObjectPath& sms_path,
152 const DeleteCallback& callback) override {
153 GetProxy(service_name, object_path)->Delete(sms_path, callback);
156 void List(const std::string& service_name,
157 const dbus::ObjectPath& object_path,
158 const ListCallback& callback) override {
159 GetProxy(service_name, object_path)->List(callback);
162 protected:
163 void Init(dbus::Bus* bus) override { bus_ = bus; };
165 private:
166 typedef base::ScopedPtrMap<std::pair<std::string, std::string>,
167 scoped_ptr<ModemMessagingProxy>> ProxyMap;
169 // Returns a SMSProxy for the given service name and object path.
170 ModemMessagingProxy* GetProxy(const std::string& service_name,
171 const dbus::ObjectPath& object_path) {
172 const ProxyMap::key_type key(service_name, object_path.value());
173 ProxyMap::const_iterator it = proxies_.find(key);
174 if (it != proxies_.end())
175 return it->second;
177 // There is no proxy for the service_name and object_path, create it.
178 scoped_ptr<ModemMessagingProxy> proxy(
179 new ModemMessagingProxy(bus_, service_name, object_path));
180 ModemMessagingProxy* proxy_ptr = proxy.get();
181 proxies_.insert(key, proxy.Pass());
182 return proxy_ptr;
185 dbus::Bus* bus_;
186 ProxyMap proxies_;
188 DISALLOW_COPY_AND_ASSIGN(ModemMessagingClientImpl);
191 } // namespace
193 ////////////////////////////////////////////////////////////////////////////////
194 // ModemMessagingClient
196 ModemMessagingClient::ModemMessagingClient() {}
198 ModemMessagingClient::~ModemMessagingClient() {}
201 // static
202 ModemMessagingClient* ModemMessagingClient::Create() {
203 return new ModemMessagingClientImpl();
207 } // namespace chromeos