Track active references in ShillClientHelper (Take 2)
[chromium-blink-merge.git] / chromeos / dbus / sms_client.cc
blobcd867a58f51782025b012ff68fa849c3be850f1c
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"
6 #include <map>
7 #include <utility>
9 #include "base/bind.h"
10 #include "base/command_line.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/stl_util.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/values.h"
17 #include "chromeos/chromeos_switches.h"
18 #include "dbus/bus.h"
19 #include "dbus/message.h"
20 #include "dbus/object_proxy.h"
21 #include "dbus/values_util.h"
22 #include "third_party/cros_system_api/dbus/service_constants.h"
24 namespace chromeos {
26 namespace {
28 // SMSClient is used to communicate with the
29 // org.freedesktop.ModemManager1.SMS service. All methods should be
30 // called from the origin thread (UI thread) which initializes the
31 // DBusThreadManager instance.
32 class SMSClientImpl : public SMSClient {
33 public:
34 SMSClientImpl() : bus_(NULL), weak_ptr_factory_(this) {}
36 virtual ~SMSClientImpl() {}
38 // Calls GetAll method. |callback| is called after the method call succeeds.
39 virtual void GetAll(const std::string& service_name,
40 const dbus::ObjectPath& object_path,
41 const GetAllCallback& callback) OVERRIDE {
42 dbus::ObjectProxy *proxy = bus_->GetObjectProxy(service_name, object_path);
43 dbus::MethodCall method_call(dbus::kDBusPropertiesInterface,
44 dbus::kDBusPropertiesGetAll);
45 dbus::MessageWriter writer(&method_call);
46 writer.AppendString(modemmanager::kModemManager1SmsInterface);
47 proxy->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
48 base::Bind(&SMSClientImpl::OnGetAll,
49 weak_ptr_factory_.GetWeakPtr(),
50 callback));
53 protected:
54 virtual void Init(dbus::Bus* bus) OVERRIDE {
55 bus_ = bus;
58 private:
59 // Handles responses of GetAll method calls.
60 void OnGetAll(const GetAllCallback& callback, dbus::Response* response) {
61 if (!response) {
62 // Must invoke the callback, even if there is no message.
63 base::DictionaryValue empty_dictionary;
64 callback.Run(empty_dictionary);
65 return;
67 dbus::MessageReader reader(response);
68 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader));
69 base::DictionaryValue* dictionary_value = NULL;
70 if (!value.get() || !value->GetAsDictionary(&dictionary_value)) {
71 LOG(WARNING) << "Invalid response: " << response->ToString();
72 base::DictionaryValue empty_dictionary;
73 callback.Run(empty_dictionary);
74 return;
76 callback.Run(*dictionary_value);
79 dbus::Bus* bus_;
81 // Note: This should remain the last member so it'll be destroyed and
82 // invalidate its weak pointers before any other members are destroyed.
83 base::WeakPtrFactory<SMSClientImpl> weak_ptr_factory_;
85 DISALLOW_COPY_AND_ASSIGN(SMSClientImpl);
88 class SMSClientStubImpl : public SMSClient {
89 public:
90 SMSClientStubImpl() : weak_ptr_factory_(this) {}
91 virtual ~SMSClientStubImpl() {}
93 virtual void Init(dbus::Bus* bus) OVERRIDE {}
95 virtual void GetAll(const std::string& service_name,
96 const dbus::ObjectPath& object_path,
97 const GetAllCallback& callback) OVERRIDE {
98 if (!CommandLine::ForCurrentProcess()->HasSwitch(
99 chromeos::switches::kSmsTestMessages))
100 return;
102 // Ownership passed to callback
103 base::DictionaryValue *sms = new base::DictionaryValue();
104 sms->SetString("Number", "000-000-0000");
105 sms->SetString("Text",
106 "SMSClientStubImpl: Test Message: " + object_path.value());
107 sms->SetString("Timestamp", "Fri Jun 8 13:26:04 EDT 2012");
109 // Run callback asynchronously.
110 if (callback.is_null())
111 return;
112 base::MessageLoop::current()->PostTask(
113 FROM_HERE,
114 base::Bind(&SMSClientStubImpl::OnGetAll,
115 weak_ptr_factory_.GetWeakPtr(),
116 base::Owned(sms),
117 callback));
120 private:
121 void OnGetAll(base::DictionaryValue *sms,
122 const GetAllCallback& callback) {
123 callback.Run(*sms);
126 base::WeakPtrFactory<SMSClientStubImpl> weak_ptr_factory_;
128 DISALLOW_COPY_AND_ASSIGN(SMSClientStubImpl);
131 } // namespace
133 ////////////////////////////////////////////////////////////////////////////////
134 // SMSClient
136 SMSClient::SMSClient() {}
138 SMSClient::~SMSClient() {}
141 // static
142 SMSClient* SMSClient::Create(DBusClientImplementationType type) {
143 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) {
144 return new SMSClientImpl();
146 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type);
147 return new SMSClientStubImpl();
150 } // namespace chromeos