- add sources.
[platform/framework/web/crosswalk.git] / src / chromeos / dbus / sms_client.cc
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/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"
23
24 namespace chromeos {
25
26 namespace {
27
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) {}
35
36   virtual ~SMSClientImpl() {}
37
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));
51   }
52
53  protected:
54   virtual void Init(dbus::Bus* bus) OVERRIDE {
55     bus_ = bus;
56   }
57
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;
66     }
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;
75     }
76     callback.Run(*dictionary_value);
77   }
78
79   dbus::Bus* bus_;
80
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_;
84
85   DISALLOW_COPY_AND_ASSIGN(SMSClientImpl);
86 };
87
88 class SMSClientStubImpl : public SMSClient {
89  public:
90   SMSClientStubImpl() : weak_ptr_factory_(this) {}
91   virtual ~SMSClientStubImpl() {}
92
93   virtual void Init(dbus::Bus* bus) OVERRIDE {}
94
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;
101
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");
108
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));
118   }
119
120  private:
121   void OnGetAll(base::DictionaryValue *sms,
122                 const GetAllCallback& callback) {
123     callback.Run(*sms);
124   }
125
126   base::WeakPtrFactory<SMSClientStubImpl> weak_ptr_factory_;
127
128   DISALLOW_COPY_AND_ASSIGN(SMSClientStubImpl);
129 };
130
131 }  // namespace
132
133 ////////////////////////////////////////////////////////////////////////////////
134 // SMSClient
135
136 SMSClient::SMSClient() {}
137
138 SMSClient::~SMSClient() {}
139
140
141 // static
142 SMSClient* SMSClient::Create(DBusClientImplementationType type) {
143   if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) {
144     return new SMSClientImpl();
145   }
146   DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type);
147   return new SMSClientStubImpl();
148 }
149
150 }  // namespace chromeos