OLD | NEW |
(Empty) | |
| 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/sms_client.h" |
| 5 |
| 6 #include <map> |
| 7 #include <utility> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/memory/weak_ptr.h" |
| 12 #include "base/message_loop.h" |
| 13 #include "base/stringprintf.h" |
| 14 #include "base/stl_util.h" |
| 15 #include "base/values.h" |
| 16 #include "dbus/bus.h" |
| 17 #include "dbus/message.h" |
| 18 #include "dbus/object_proxy.h" |
| 19 #include "dbus/values_util.h" |
| 20 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 21 |
| 22 namespace chromeos { |
| 23 |
| 24 namespace { |
| 25 |
| 26 // SMSClient is used to communicate with the |
| 27 // org.freedesktop.ModemManager1.SMS service. All methods should be |
| 28 // called from the origin thread (UI thread) which initializes the |
| 29 // DBusThreadManager instance. |
| 30 class SMSClientImpl : public SMSClient { |
| 31 public: |
| 32 explicit SMSClientImpl(dbus::Bus* bus) : bus_(bus), weak_ptr_factory_(this) {} |
| 33 virtual ~SMSClientImpl() {} |
| 34 |
| 35 // Calls GetAll method. |callback| is called after the method call succeeds. |
| 36 virtual void GetAll(const std::string& service_name, |
| 37 const dbus::ObjectPath& object_path, |
| 38 const GetAllCallback& callback) OVERRIDE { |
| 39 dbus::ObjectProxy *proxy = bus_->GetObjectProxy(service_name, object_path); |
| 40 dbus::MethodCall method_call(dbus::kDBusPropertiesInterface, |
| 41 dbus::kDBusPropertiesGetAll); |
| 42 dbus::MessageWriter writer(&method_call); |
| 43 writer.AppendString(modemmanager::kModemManager1SmsInterface); |
| 44 proxy->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 45 base::Bind(&SMSClientImpl::OnGetAll, |
| 46 weak_ptr_factory_.GetWeakPtr(), |
| 47 callback)); |
| 48 } |
| 49 |
| 50 private: |
| 51 // Handles responses of GetAll method calls. |
| 52 void OnGetAll(const GetAllCallback& callback, dbus::Response* response) { |
| 53 if (!response) { |
| 54 // Must invoke the callback, even if there is no message. |
| 55 callback.Run(base::DictionaryValue()); |
| 56 return; |
| 57 } |
| 58 dbus::MessageReader reader(response); |
| 59 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); |
| 60 base::DictionaryValue* dictionary_value = NULL; |
| 61 if (!value.get() || !value->GetAsDictionary(&dictionary_value)) { |
| 62 LOG(WARNING) << "Invalid response: " << response->ToString(); |
| 63 callback.Run(base::DictionaryValue()); |
| 64 return; |
| 65 } |
| 66 callback.Run(*dictionary_value); |
| 67 } |
| 68 |
| 69 dbus::Bus* bus_; |
| 70 base::WeakPtrFactory<SMSClientImpl> weak_ptr_factory_; |
| 71 |
| 72 DISALLOW_COPY_AND_ASSIGN(SMSClientImpl); |
| 73 }; |
| 74 |
| 75 class SMSClientStubImpl : public SMSClient { |
| 76 public: |
| 77 SMSClientStubImpl() : weak_ptr_factory_(this) {} |
| 78 virtual ~SMSClientStubImpl() {} |
| 79 |
| 80 virtual void GetAll(const std::string& service_name, |
| 81 const dbus::ObjectPath& object_path, |
| 82 const GetAllCallback& callback) OVERRIDE { |
| 83 // Ownership passed to callback |
| 84 base::DictionaryValue *sms = new base::DictionaryValue(); |
| 85 sms->SetString("Number", "000-000-0000"); |
| 86 sms->SetString("Text", |
| 87 "SMSClientStubImpl: Test Message: " + object_path.value()); |
| 88 sms->SetString("Timestamp", "Fri Jun 8 13:26:04 EDT 2012"); |
| 89 |
| 90 // Run callback asynchronously. |
| 91 MessageLoop::current()->PostTask( |
| 92 FROM_HERE, |
| 93 base::Bind(&SMSClientStubImpl::OnGetAll, |
| 94 weak_ptr_factory_.GetWeakPtr(), |
| 95 base::Owned(sms), |
| 96 callback)); |
| 97 } |
| 98 |
| 99 private: |
| 100 void OnGetAll(base::DictionaryValue *sms, |
| 101 const GetAllCallback& callback) { |
| 102 callback.Run(*sms); |
| 103 } |
| 104 |
| 105 base::WeakPtrFactory<SMSClientStubImpl> weak_ptr_factory_; |
| 106 |
| 107 DISALLOW_COPY_AND_ASSIGN(SMSClientStubImpl); |
| 108 }; |
| 109 |
| 110 } // namespace |
| 111 |
| 112 //////////////////////////////////////////////////////////////////////////////// |
| 113 // SMSClient |
| 114 |
| 115 SMSClient::SMSClient() {} |
| 116 |
| 117 SMSClient::~SMSClient() {} |
| 118 |
| 119 |
| 120 // static |
| 121 SMSClient* SMSClient::Create(DBusClientImplementationType type, |
| 122 dbus::Bus* bus) { |
| 123 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) { |
| 124 return new SMSClientImpl(bus); |
| 125 } |
| 126 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); |
| 127 return new SMSClientStubImpl(); |
| 128 } |
| 129 |
| 130 } // namespace chromeos |
OLD | NEW |