Chromium Code Reviews| 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::Unretained(sms), | |
|
hashimoto
2012/06/11 07:45:46
nit: Use base::Owned?
Jason Glasgow
2012/06/11 16:44:47
Done.
| |
| 96 callback)); | |
| 97 } | |
| 98 | |
| 99 private: | |
| 100 void OnGetAll(base::DictionaryValue *sms, | |
| 101 const GetAllCallback& callback) { | |
| 102 callback.Run(*sms); | |
| 103 delete sms; | |
|
Jason Glasgow
2012/06/11 16:44:47
Removed to be consistent with Owned.
| |
| 104 } | |
| 105 | |
| 106 base::WeakPtrFactory<SMSClientStubImpl> weak_ptr_factory_; | |
| 107 | |
| 108 DISALLOW_COPY_AND_ASSIGN(SMSClientStubImpl); | |
| 109 }; | |
| 110 | |
| 111 } // namespace | |
| 112 | |
| 113 //////////////////////////////////////////////////////////////////////////////// | |
| 114 // SMSClient | |
| 115 | |
| 116 SMSClient::SMSClient() {} | |
| 117 | |
| 118 SMSClient::~SMSClient() {} | |
| 119 | |
| 120 | |
| 121 // static | |
| 122 SMSClient* SMSClient::Create(DBusClientImplementationType type, | |
| 123 dbus::Bus* bus) { | |
| 124 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) { | |
| 125 return new SMSClientImpl(bus); | |
| 126 } | |
| 127 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | |
| 128 return new SMSClientStubImpl(); | |
| 129 } | |
| 130 | |
| 131 } // namespace chromeos | |
| OLD | NEW |