OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 #include "chromeos/dbus/modem_messaging_client.h" | 4 #include "chromeos/dbus/modem_messaging_client.h" |
5 | 5 |
6 #include <map> | |
7 #include <utility> | 6 #include <utility> |
8 | 7 |
9 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/containers/scoped_ptr_map.h" |
| 10 #include "base/memory/scoped_ptr.h" |
10 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
11 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
12 #include "base/stl_util.h" | |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 #include "dbus/bus.h" | 14 #include "dbus/bus.h" |
15 #include "dbus/message.h" | 15 #include "dbus/message.h" |
16 #include "dbus/object_proxy.h" | 16 #include "dbus/object_proxy.h" |
17 #include "third_party/cros_system_api/dbus/service_constants.h" | 17 #include "third_party/cros_system_api/dbus/service_constants.h" |
18 | 18 |
19 namespace chromeos { | 19 namespace chromeos { |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 | 126 |
127 // Note: This should remain the last member so it'll be destroyed and | 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. | 128 // invalidate its weak pointers before any other members are destroyed. |
129 base::WeakPtrFactory<ModemMessagingProxy> weak_ptr_factory_; | 129 base::WeakPtrFactory<ModemMessagingProxy> weak_ptr_factory_; |
130 | 130 |
131 DISALLOW_COPY_AND_ASSIGN(ModemMessagingProxy); | 131 DISALLOW_COPY_AND_ASSIGN(ModemMessagingProxy); |
132 }; | 132 }; |
133 | 133 |
134 class CHROMEOS_EXPORT ModemMessagingClientImpl : public ModemMessagingClient { | 134 class CHROMEOS_EXPORT ModemMessagingClientImpl : public ModemMessagingClient { |
135 public: | 135 public: |
136 ModemMessagingClientImpl() | 136 ModemMessagingClientImpl() : bus_(NULL) {} |
137 : bus_(NULL), | |
138 proxies_deleter_(&proxies_) { | |
139 } | |
140 | 137 |
141 void SetSmsReceivedHandler(const std::string& service_name, | 138 void SetSmsReceivedHandler(const std::string& service_name, |
142 const dbus::ObjectPath& object_path, | 139 const dbus::ObjectPath& object_path, |
143 const SmsReceivedHandler& handler) override { | 140 const SmsReceivedHandler& handler) override { |
144 GetProxy(service_name, object_path)->SetSmsReceivedHandler(handler); | 141 GetProxy(service_name, object_path)->SetSmsReceivedHandler(handler); |
145 } | 142 } |
146 | 143 |
147 void ResetSmsReceivedHandler(const std::string& service_name, | 144 void ResetSmsReceivedHandler(const std::string& service_name, |
148 const dbus::ObjectPath& object_path) override { | 145 const dbus::ObjectPath& object_path) override { |
149 GetProxy(service_name, object_path)->ResetSmsReceivedHandler(); | 146 GetProxy(service_name, object_path)->ResetSmsReceivedHandler(); |
150 } | 147 } |
151 | 148 |
152 void Delete(const std::string& service_name, | 149 void Delete(const std::string& service_name, |
153 const dbus::ObjectPath& object_path, | 150 const dbus::ObjectPath& object_path, |
154 const dbus::ObjectPath& sms_path, | 151 const dbus::ObjectPath& sms_path, |
155 const DeleteCallback& callback) override { | 152 const DeleteCallback& callback) override { |
156 GetProxy(service_name, object_path)->Delete(sms_path, callback); | 153 GetProxy(service_name, object_path)->Delete(sms_path, callback); |
157 } | 154 } |
158 | 155 |
159 void List(const std::string& service_name, | 156 void List(const std::string& service_name, |
160 const dbus::ObjectPath& object_path, | 157 const dbus::ObjectPath& object_path, |
161 const ListCallback& callback) override { | 158 const ListCallback& callback) override { |
162 GetProxy(service_name, object_path)->List(callback); | 159 GetProxy(service_name, object_path)->List(callback); |
163 } | 160 } |
164 | 161 |
165 protected: | 162 protected: |
166 void Init(dbus::Bus* bus) override { bus_ = bus; }; | 163 void Init(dbus::Bus* bus) override { bus_ = bus; }; |
167 | 164 |
168 private: | 165 private: |
169 typedef std::map<std::pair<std::string, std::string>, ModemMessagingProxy*> | 166 typedef base::ScopedPtrMap<std::pair<std::string, std::string>, |
170 ProxyMap; | 167 scoped_ptr<ModemMessagingProxy>> ProxyMap; |
171 | 168 |
172 // Returns a SMSProxy for the given service name and object path. | 169 // Returns a SMSProxy for the given service name and object path. |
173 ModemMessagingProxy* GetProxy(const std::string& service_name, | 170 ModemMessagingProxy* GetProxy(const std::string& service_name, |
174 const dbus::ObjectPath& object_path) { | 171 const dbus::ObjectPath& object_path) { |
175 const ProxyMap::key_type key(service_name, object_path.value()); | 172 const ProxyMap::key_type key(service_name, object_path.value()); |
176 ProxyMap::iterator it = proxies_.find(key); | 173 ProxyMap::const_iterator it = proxies_.find(key); |
177 if (it != proxies_.end()) | 174 if (it != proxies_.end()) |
178 return it->second; | 175 return it->second; |
179 | 176 |
180 // There is no proxy for the service_name and object_path, create it. | 177 // There is no proxy for the service_name and object_path, create it. |
181 ModemMessagingProxy* proxy | 178 scoped_ptr<ModemMessagingProxy> proxy( |
182 = new ModemMessagingProxy(bus_, service_name, object_path); | 179 new ModemMessagingProxy(bus_, service_name, object_path)); |
183 proxies_.insert(ProxyMap::value_type(key, proxy)); | 180 ModemMessagingProxy* proxy_ptr = proxy.get(); |
184 return proxy; | 181 proxies_.insert(key, proxy.Pass()); |
| 182 return proxy_ptr; |
185 } | 183 } |
186 | 184 |
187 dbus::Bus* bus_; | 185 dbus::Bus* bus_; |
188 ProxyMap proxies_; | 186 ProxyMap proxies_; |
189 STLValueDeleter<ProxyMap> proxies_deleter_; | |
190 | 187 |
191 DISALLOW_COPY_AND_ASSIGN(ModemMessagingClientImpl); | 188 DISALLOW_COPY_AND_ASSIGN(ModemMessagingClientImpl); |
192 }; | 189 }; |
193 | 190 |
194 } // namespace | 191 } // namespace |
195 | 192 |
196 //////////////////////////////////////////////////////////////////////////////// | 193 //////////////////////////////////////////////////////////////////////////////// |
197 // ModemMessagingClient | 194 // ModemMessagingClient |
198 | 195 |
199 ModemMessagingClient::ModemMessagingClient() {} | 196 ModemMessagingClient::ModemMessagingClient() {} |
200 | 197 |
201 ModemMessagingClient::~ModemMessagingClient() {} | 198 ModemMessagingClient::~ModemMessagingClient() {} |
202 | 199 |
203 | 200 |
204 // static | 201 // static |
205 ModemMessagingClient* ModemMessagingClient::Create() { | 202 ModemMessagingClient* ModemMessagingClient::Create() { |
206 return new ModemMessagingClientImpl(); | 203 return new ModemMessagingClientImpl(); |
207 } | 204 } |
208 | 205 |
209 | 206 |
210 } // namespace chromeos | 207 } // namespace chromeos |
OLD | NEW |