Chromium Code Reviews| 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 | 4 |
| 5 #include "chrome/browser/chromeos/cros/sms_watcher.h" | 5 #include "chrome/browser/chromeos/cros/sms_watcher.h" |
| 6 | 6 |
| 7 #include <algorithm> | |
| 8 #include <deque> | |
| 9 #include <string> | |
| 10 #include <vector> | |
| 11 | |
| 7 #include "base/bind.h" | 12 #include "base/bind.h" |
| 8 #include "base/values.h" | 13 #include "base/values.h" |
| 9 #include "chromeos/dbus/dbus_thread_manager.h" | 14 #include "chromeos/dbus/dbus_thread_manager.h" |
| 10 #include "chromeos/dbus/flimflam_device_client.h" | 15 #include "chromeos/dbus/flimflam_device_client.h" |
| 11 #include "chromeos/dbus/gsm_sms_client.h" | 16 #include "chromeos/dbus/gsm_sms_client.h" |
| 17 #include "chromeos/dbus/modem_messaging_client.h" | |
| 18 #include "chromeos/dbus/sms_client.h" | |
| 12 #include "third_party/cros_system_api/dbus/service_constants.h" | 19 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 13 | 20 |
| 14 namespace chromeos { | 21 namespace chromeos { |
| 15 | 22 |
| 16 namespace { | 23 namespace { |
| 17 | 24 |
| 18 int decode_bcd(const char *s) { | 25 int decode_bcd(const char *s) { |
| 19 return (s[0] - '0') * 10 + s[1] - '0'; | 26 return (s[0] - '0') * 10 + s[1] - '0'; |
| 20 } | 27 } |
| 21 | 28 |
| 29 void decode_timestamp(const std::string& sms_timestamp, SMS *sms) { | |
| 30 base::Time::Exploded exp; | |
| 31 exp.year = decode_bcd(&sms_timestamp[0]); | |
| 32 if (exp.year > 95) | |
| 33 exp.year += 1900; | |
| 34 else | |
| 35 exp.year += 2000; | |
| 36 exp.month = decode_bcd(&sms_timestamp[2]); | |
| 37 exp.day_of_month = decode_bcd(&sms_timestamp[4]); | |
| 38 exp.hour = decode_bcd(&sms_timestamp[6]); | |
| 39 exp.minute = decode_bcd(&sms_timestamp[8]); | |
| 40 exp.second = decode_bcd(&sms_timestamp[10]); | |
| 41 exp.millisecond = 0; | |
| 42 sms->timestamp = base::Time::FromUTCExploded(exp); | |
| 43 int hours = decode_bcd(&sms_timestamp[13]); | |
| 44 if (sms_timestamp[12] == '-') | |
| 45 hours = -hours; | |
| 46 sms->timestamp -= base::TimeDelta::FromHours(hours); | |
| 47 } | |
| 48 | |
| 22 // Callback for Delete() method. This method does nothing. | 49 // Callback for Delete() method. This method does nothing. |
| 23 void DeleteSMSCallback() {} | 50 void DeleteSMSCallback() {} |
| 24 | 51 |
| 25 } // namespace | 52 } // namespace |
| 26 | 53 |
| 27 const char SMSWatcher::kNumberKey[] = "number"; | 54 const char SMSWatcher::kNumberKey[] = "number"; |
| 28 const char SMSWatcher::kTextKey[] = "text"; | 55 const char SMSWatcher::kTextKey[] = "text"; |
| 29 const char SMSWatcher::kTimestampKey[] = "timestamp"; | 56 const char SMSWatcher::kTimestampKey[] = "timestamp"; |
| 30 const char SMSWatcher::kSmscKey[] = "smsc"; | 57 const char SMSWatcher::kSmscKey[] = "smsc"; |
| 31 const char SMSWatcher::kValidityKey[] = "validity"; | 58 const char SMSWatcher::kValidityKey[] = "validity"; |
| 32 const char SMSWatcher::kClassKey[] = "class"; | 59 const char SMSWatcher::kClassKey[] = "class"; |
| 33 const char SMSWatcher::kIndexKey[] = "index"; | 60 const char SMSWatcher::kIndexKey[] = "index"; |
| 34 | 61 |
| 62 const char SMSWatcher::kModemManager1NumberKey[] = "Number"; | |
| 63 const char SMSWatcher::kModemManager1TextKey[] = "Text"; | |
| 64 const char SMSWatcher::kModemManager1TimestampKey[] = "Timestamp"; | |
| 65 const char SMSWatcher::kModemManager1SmscKey[] = "Smsc"; | |
| 66 const char SMSWatcher::kModemManager1ValidityKey[] = "Validity"; | |
| 67 const char SMSWatcher::kModemManager1ClassKey[] = "Class"; | |
| 68 const char SMSWatcher::kModemManager1IndexKey[] = "Index"; | |
| 69 | |
| 70 class SMSWatcher::WatcherBase { | |
| 71 public: | |
| 72 WatcherBase(const std::string& device_path, | |
| 73 MonitorSMSCallback callback, | |
| 74 void* object, | |
| 75 const std::string& dbus_connection, | |
| 76 const dbus::ObjectPath& object_path) : | |
| 77 device_path_(device_path), | |
| 78 callback_(callback), | |
| 79 object_(object), | |
| 80 dbus_connection_(dbus_connection), | |
| 81 object_path_(object_path) {} | |
| 82 | |
| 83 virtual ~WatcherBase() {} | |
| 84 | |
| 85 protected: | |
| 86 const std::string device_path_; | |
| 87 MonitorSMSCallback callback_; | |
| 88 void* object_; | |
| 89 const std::string dbus_connection_; | |
| 90 const dbus::ObjectPath object_path_; | |
| 91 | |
| 92 DISALLOW_COPY_AND_ASSIGN(WatcherBase); | |
| 93 }; | |
| 94 | |
| 95 namespace { | |
| 96 | |
| 97 class GsmWatcher : public SMSWatcher::WatcherBase { | |
| 98 public: | |
| 99 GsmWatcher(const std::string& device_path, | |
| 100 MonitorSMSCallback callback, | |
| 101 void* object, | |
| 102 const std::string& dbus_connection, | |
| 103 const dbus::ObjectPath& object_path) | |
| 104 : WatcherBase(device_path, callback, object, dbus_connection, | |
| 105 object_path), | |
| 106 weak_ptr_factory_(this) { | |
| 107 DBusThreadManager::Get()->GetGsmSMSClient()->SetSmsReceivedHandler( | |
| 108 dbus_connection_, | |
| 109 object_path_, | |
| 110 base::Bind(&GsmWatcher::OnSmsReceived, weak_ptr_factory_.GetWeakPtr())); | |
| 111 | |
| 112 DBusThreadManager::Get()->GetGsmSMSClient()->List( | |
| 113 dbus_connection_, object_path_, | |
| 114 base::Bind(&GsmWatcher::ListSMSCallback, | |
| 115 weak_ptr_factory_.GetWeakPtr())); | |
| 116 } | |
| 117 | |
| 118 virtual ~GsmWatcher() { | |
| 119 DBusThreadManager::Get()->GetGsmSMSClient()->ResetSmsReceivedHandler( | |
| 120 dbus_connection_, object_path_); | |
| 121 } | |
| 122 | |
| 123 private: | |
| 124 // Callback for SmsReceived signal from ModemManager.Modem.Gsm.SMS | |
| 125 void OnSmsReceived(uint32 index, bool complete) { | |
| 126 // Only handle complete messages. | |
| 127 if (!complete) | |
| 128 return; | |
| 129 DBusThreadManager::Get()->GetGsmSMSClient()->Get( | |
| 130 dbus_connection_, object_path_, index, | |
| 131 base::Bind(&GsmWatcher::GetSMSCallback, | |
| 132 weak_ptr_factory_.GetWeakPtr(), | |
| 133 index)); | |
| 134 } | |
| 135 | |
| 136 // Runs |callback_| with a SMS. | |
| 137 void RunCallbackWithSMS(const base::DictionaryValue& sms_dictionary) { | |
| 138 SMS sms; | |
| 139 | |
| 140 std::string number; | |
| 141 if (!sms_dictionary.GetStringWithoutPathExpansion(SMSWatcher::kNumberKey, | |
| 142 &number)) | |
| 143 LOG(WARNING) << "SMS did not contain a number"; | |
|
stevenjb
2012/06/06 23:04:18
nit: {}
| |
| 144 sms.number = number.c_str(); | |
| 145 | |
| 146 std::string text; | |
| 147 if (!sms_dictionary.GetStringWithoutPathExpansion(SMSWatcher::kTextKey, | |
| 148 &text)) { | |
| 149 LOG(WARNING) << "SMS did not contain message text"; | |
| 150 } | |
| 151 sms.text = text.c_str(); | |
| 152 | |
| 153 std::string sms_timestamp; | |
| 154 if (sms_dictionary.GetStringWithoutPathExpansion(SMSWatcher::kTimestampKey, | |
| 155 &sms_timestamp)) { | |
| 156 decode_timestamp(sms_timestamp, &sms); | |
| 157 } else { | |
| 158 LOG(WARNING) << "SMS did not contain a timestamp"; | |
| 159 sms.timestamp = base::Time(); | |
| 160 } | |
| 161 | |
| 162 std::string smsc; | |
| 163 if (!sms_dictionary.GetStringWithoutPathExpansion(SMSWatcher::kSmscKey, | |
| 164 &smsc)) | |
| 165 sms.smsc = NULL; | |
| 166 else | |
| 167 sms.smsc = smsc.c_str(); | |
|
stevenjb
2012/06/06 23:04:18
nit: {}
| |
| 168 | |
| 169 double validity = 0; | |
| 170 if (!sms_dictionary.GetDoubleWithoutPathExpansion(SMSWatcher::kValidityKey, | |
| 171 &validity)) | |
| 172 validity = -1; | |
|
stevenjb
2012/06/06 23:04:18
nit: {}
| |
| 173 sms.validity = validity; | |
| 174 | |
| 175 double msgclass = 0; | |
| 176 if (!sms_dictionary.GetDoubleWithoutPathExpansion(SMSWatcher::kClassKey, | |
| 177 &msgclass)) | |
| 178 msgclass = -1; | |
|
stevenjb
2012/06/06 23:04:18
nit: {}
| |
| 179 sms.msgclass = msgclass; | |
| 180 | |
| 181 callback_(object_, device_path_.c_str(), &sms); | |
| 182 } | |
| 183 | |
| 184 // Callback for Get() method from ModemManager.Modem.Gsm.SMS | |
| 185 void GetSMSCallback(uint32 index, | |
| 186 const base::DictionaryValue& sms_dictionary) { | |
| 187 RunCallbackWithSMS(sms_dictionary); | |
| 188 | |
| 189 DBusThreadManager::Get()->GetGsmSMSClient()->Delete( | |
| 190 dbus_connection_, object_path_, index, base::Bind(&DeleteSMSCallback)); | |
| 191 } | |
| 192 | |
| 193 // Callback for List() method. | |
| 194 void ListSMSCallback(const base::ListValue& result) { | |
| 195 // List() is called only once; no one touches delete_queue_ before List(). | |
| 196 CHECK(delete_queue_.empty()); | |
| 197 for (size_t i = 0; i != result.GetSize(); ++i) { | |
| 198 base::DictionaryValue* sms_dictionary = NULL; | |
| 199 if (!result.GetDictionary(i, &sms_dictionary)) { | |
| 200 LOG(ERROR) << "result[" << i << "] is not a dictionary."; | |
| 201 continue; | |
| 202 } | |
| 203 RunCallbackWithSMS(*sms_dictionary); | |
| 204 double index = 0; | |
| 205 if (sms_dictionary->GetDoubleWithoutPathExpansion(SMSWatcher::kIndexKey, | |
| 206 &index)) { | |
| 207 delete_queue_.push_back(index); | |
| 208 } | |
| 209 } | |
| 210 DeleteSMSInChain(); | |
| 211 } | |
| 212 | |
| 213 // Deletes SMSs in the queue. | |
| 214 void DeleteSMSInChain() { | |
| 215 if (!delete_queue_.empty()) { | |
| 216 DBusThreadManager::Get()->GetGsmSMSClient()->Delete( | |
| 217 dbus_connection_, object_path_, delete_queue_.back(), | |
| 218 base::Bind(&GsmWatcher::DeleteSMSInChain, | |
| 219 weak_ptr_factory_.GetWeakPtr())); | |
| 220 delete_queue_.pop_back(); | |
| 221 } | |
| 222 } | |
| 223 | |
| 224 base::WeakPtrFactory<GsmWatcher> weak_ptr_factory_; | |
| 225 std::vector<uint32> delete_queue_; | |
| 226 | |
| 227 DISALLOW_COPY_AND_ASSIGN(GsmWatcher); | |
| 228 }; | |
| 229 | |
| 230 class ModemManager1Watcher : public SMSWatcher::WatcherBase { | |
| 231 public: | |
| 232 ModemManager1Watcher(const std::string& device_path, | |
| 233 MonitorSMSCallback callback, | |
| 234 void *object, | |
| 235 const std::string& dbus_connection, | |
| 236 const dbus::ObjectPath& object_path) | |
| 237 : WatcherBase(device_path, callback, object, dbus_connection, | |
| 238 object_path), | |
| 239 weak_ptr_factory_(this), | |
| 240 deleting_messages_(false), | |
| 241 retrieving_messages_(false) { | |
| 242 DBusThreadManager::Get()->GetModemMessagingClient()->SetSmsReceivedHandler( | |
| 243 dbus_connection_, | |
| 244 object_path_, | |
| 245 base::Bind(&ModemManager1Watcher::OnSmsReceived, | |
| 246 weak_ptr_factory_.GetWeakPtr())); | |
| 247 | |
| 248 DBusThreadManager::Get()->GetModemMessagingClient()->List( | |
| 249 dbus_connection_, object_path_, | |
| 250 base::Bind(&ModemManager1Watcher::ListSMSCallback, | |
| 251 weak_ptr_factory_.GetWeakPtr())); | |
| 252 } | |
| 253 | |
| 254 virtual ~ModemManager1Watcher() { | |
| 255 DBusThreadManager::Get()->GetModemMessagingClient() | |
| 256 ->ResetSmsReceivedHandler(dbus_connection_, object_path_); | |
| 257 } | |
| 258 | |
| 259 private: | |
| 260 void ListSMSCallback( | |
| 261 const std::vector<dbus::ObjectPath>& paths) { | |
| 262 // This receives all messages, so clear any pending gets and deletes. | |
| 263 retrieval_queue_.clear(); | |
| 264 delete_queue_.clear(); | |
| 265 | |
| 266 std::copy(paths.begin(), paths.end(), retrieval_queue_.begin()); | |
| 267 if (!retrieving_messages_) | |
| 268 GetMessages(); | |
| 269 } | |
| 270 | |
| 271 // Messages must be deleted one at a time, since we can not | |
| 272 // guarantee the order the deletion will be executed in. Delete | |
| 273 // messages from the back of the list so that the indices are | |
| 274 // valid. | |
| 275 void DeleteMessages() { | |
| 276 if (delete_queue_.empty()) { | |
| 277 deleting_messages_ = false; | |
| 278 return; | |
| 279 } | |
| 280 deleting_messages_ = true; | |
| 281 dbus::ObjectPath sms_path = delete_queue_.back(); | |
| 282 delete_queue_.pop_back(); | |
| 283 DBusThreadManager::Get()->GetModemMessagingClient()->Delete( | |
| 284 dbus_connection_, object_path_, sms_path, | |
| 285 base::Bind(&ModemManager1Watcher::DeleteMessages, | |
| 286 weak_ptr_factory_.GetWeakPtr())); | |
| 287 } | |
| 288 | |
| 289 // Messages must be fetched one at a time, so that we do not queue too | |
| 290 // many requests to a single threaded server. | |
| 291 void GetMessages() { | |
| 292 if (retrieval_queue_.empty()) { | |
| 293 retrieving_messages_ = false; | |
| 294 if (!deleting_messages_) | |
| 295 DeleteMessages(); | |
| 296 return; | |
| 297 } | |
| 298 retrieving_messages_ = true; | |
| 299 dbus::ObjectPath sms_path = retrieval_queue_.front(); | |
| 300 retrieval_queue_.pop_front(); | |
| 301 DBusThreadManager::Get()->GetSMSClient()->GetAll( | |
| 302 dbus_connection_, sms_path, | |
| 303 base::Bind(&ModemManager1Watcher::GetCallback, | |
| 304 weak_ptr_factory_.GetWeakPtr())); | |
| 305 delete_queue_.push_back(sms_path); | |
| 306 } | |
| 307 | |
| 308 // Handles arrival of a new SMS message. | |
| 309 void OnSmsReceived(const dbus::ObjectPath& sms_path, bool complete) { | |
| 310 // Only handle complete messages. | |
| 311 if (!complete) | |
| 312 return; | |
| 313 retrieval_queue_.push_back(sms_path); | |
| 314 if (!retrieving_messages_) | |
| 315 GetMessages(); | |
| 316 } | |
| 317 | |
| 318 // Runs |callback_| with a SMS. | |
| 319 void RunCallbackWithSMS(const base::DictionaryValue& sms_dictionary) { | |
| 320 SMS sms; | |
| 321 | |
| 322 std::string number; | |
| 323 if (!sms_dictionary.GetStringWithoutPathExpansion( | |
| 324 SMSWatcher::kModemManager1NumberKey, &number)) | |
| 325 LOG(WARNING) << "SMS did not contain a number"; | |
| 326 sms.number = number.c_str(); | |
| 327 | |
| 328 std::string text; | |
| 329 if (!sms_dictionary.GetStringWithoutPathExpansion( | |
| 330 SMSWatcher::kModemManager1TextKey, &text)) { | |
| 331 LOG(WARNING) << "SMS did not contain message text"; | |
| 332 } | |
| 333 sms.text = text.c_str(); | |
| 334 | |
| 335 std::string sms_timestamp; | |
| 336 if (sms_dictionary.GetStringWithoutPathExpansion( | |
| 337 SMSWatcher::kModemManager1TimestampKey, &sms_timestamp)) { | |
| 338 decode_timestamp(sms_timestamp, &sms); | |
| 339 } else { | |
| 340 LOG(WARNING) << "SMS did not contain a timestamp"; | |
| 341 sms.timestamp = base::Time(); | |
| 342 } | |
| 343 | |
| 344 std::string smsc; | |
| 345 if (!sms_dictionary.GetStringWithoutPathExpansion( | |
| 346 SMSWatcher::kModemManager1SmscKey, &smsc)) | |
| 347 sms.smsc = NULL; | |
| 348 else | |
| 349 sms.smsc = smsc.c_str(); | |
| 350 | |
| 351 double validity = 0; | |
| 352 if (!sms_dictionary.GetDoubleWithoutPathExpansion( | |
| 353 SMSWatcher::kModemManager1ValidityKey, &validity)) | |
| 354 validity = -1; | |
| 355 sms.validity = validity; | |
| 356 | |
| 357 double msgclass = 0; | |
| 358 if (!sms_dictionary.GetDoubleWithoutPathExpansion( | |
| 359 SMSWatcher::kModemManager1ClassKey, &msgclass)) | |
| 360 msgclass = -1; | |
| 361 sms.msgclass = msgclass; | |
| 362 | |
| 363 callback_(object_, device_path_.c_str(), &sms); | |
| 364 } | |
| 365 | |
| 366 void GetCallback(const base::DictionaryValue& dictionary) { | |
| 367 RunCallbackWithSMS(dictionary); | |
| 368 GetMessages(); | |
| 369 } | |
| 370 | |
| 371 base::WeakPtrFactory<ModemManager1Watcher> weak_ptr_factory_; | |
| 372 bool deleting_messages_; | |
| 373 bool retrieving_messages_; | |
| 374 std::vector<dbus::ObjectPath> delete_queue_; | |
| 375 std::deque<dbus::ObjectPath> retrieval_queue_; | |
| 376 | |
| 377 DISALLOW_COPY_AND_ASSIGN(ModemManager1Watcher); | |
| 378 }; | |
| 379 | |
| 380 } // namespace | |
| 381 | |
| 35 SMSWatcher::SMSWatcher(const std::string& modem_device_path, | 382 SMSWatcher::SMSWatcher(const std::string& modem_device_path, |
| 36 MonitorSMSCallback callback, | 383 MonitorSMSCallback callback, |
| 37 void* object) | 384 void* object) |
| 38 : weak_ptr_factory_(this), | 385 : weak_ptr_factory_(this), |
| 39 device_path_(modem_device_path), | 386 device_path_(modem_device_path), |
| 40 callback_(callback), | 387 callback_(callback), |
| 41 object_(object) { | 388 object_(object) { |
| 42 DBusThreadManager::Get()->GetFlimflamDeviceClient()->GetProperties( | 389 DBusThreadManager::Get()->GetFlimflamDeviceClient()->GetProperties( |
| 43 dbus::ObjectPath(modem_device_path), | 390 dbus::ObjectPath(modem_device_path), |
| 44 base::Bind(&SMSWatcher::DevicePropertiesCallback, | 391 base::Bind(&SMSWatcher::DevicePropertiesCallback, |
| 45 weak_ptr_factory_.GetWeakPtr())); | 392 weak_ptr_factory_.GetWeakPtr())); |
| 46 } | 393 } |
| 47 | 394 |
| 48 SMSWatcher::~SMSWatcher() { | 395 SMSWatcher::~SMSWatcher() { |
| 49 if (!dbus_connection_.empty() && !object_path_.value().empty()) { | |
| 50 DBusThreadManager::Get()->GetGsmSMSClient()->ResetSmsReceivedHandler( | |
| 51 dbus_connection_, object_path_); | |
| 52 } | |
| 53 } | 396 } |
| 54 | 397 |
| 55 void SMSWatcher::DevicePropertiesCallback( | 398 void SMSWatcher::DevicePropertiesCallback( |
| 56 DBusMethodCallStatus call_status, | 399 DBusMethodCallStatus call_status, |
| 57 const base::DictionaryValue& properties) { | 400 const base::DictionaryValue& properties) { |
| 58 if (call_status != DBUS_METHOD_CALL_SUCCESS) | 401 if (call_status != DBUS_METHOD_CALL_SUCCESS) |
| 59 return; | 402 return; |
| 60 | 403 |
| 404 std::string dbus_connection; | |
| 61 if (!properties.GetStringWithoutPathExpansion( | 405 if (!properties.GetStringWithoutPathExpansion( |
| 62 flimflam::kDBusConnectionProperty, &dbus_connection_)) { | 406 flimflam::kDBusConnectionProperty, &dbus_connection)) { |
| 63 LOG(WARNING) << "Modem device properties do not include DBus connection."; | 407 LOG(WARNING) << "Modem device properties do not include DBus connection."; |
| 64 return; | 408 return; |
| 65 } | 409 } |
| 66 | 410 |
| 67 std::string object_path_string; | 411 std::string object_path_string; |
| 68 if (!properties.GetStringWithoutPathExpansion( | 412 if (!properties.GetStringWithoutPathExpansion( |
| 69 flimflam::kDBusObjectProperty, &object_path_string)) { | 413 flimflam::kDBusObjectProperty, &object_path_string)) { |
| 70 LOG(WARNING) << "Modem device properties do not include DBus object."; | 414 LOG(WARNING) << "Modem device properties do not include DBus object."; |
| 71 return; | 415 return; |
| 72 } | 416 } |
| 73 object_path_ = dbus::ObjectPath(object_path_string); | |
| 74 | 417 |
| 75 DBusThreadManager::Get()->GetGsmSMSClient()->SetSmsReceivedHandler( | 418 // TODO(jglasgow): or is it modemmanager::kModemManager1 ??? |
|
stevenjb
2012/06/06 23:04:18
Has this been figured out?
Jason Glasgow
2012/06/08 03:28:34
Yes, this has been figured out. I need to remove
| |
| 76 dbus_connection_, | 419 if (object_path_string.compare( |
| 77 object_path_, | 420 0, sizeof(modemmanager::kModemManager1ServicePath) - 1, |
| 78 base::Bind(&SMSWatcher::OnSmsReceived, weak_ptr_factory_.GetWeakPtr())); | 421 modemmanager::kModemManager1ServicePath) == 0) { |
| 79 | 422 watcher_.reset( |
| 80 DBusThreadManager::Get()->GetGsmSMSClient()->List( | 423 new ModemManager1Watcher(device_path_, |
| 81 dbus_connection_, object_path_, | 424 callback_, object_, dbus_connection, |
| 82 base::Bind(&SMSWatcher::ListSMSCallback, | 425 dbus::ObjectPath(object_path_string))); |
| 83 weak_ptr_factory_.GetWeakPtr())); | |
| 84 } | |
| 85 | |
| 86 void SMSWatcher::OnSmsReceived(uint32 index, bool complete) { | |
| 87 // Only handle complete messages. | |
| 88 if (!complete) | |
| 89 return; | |
| 90 DBusThreadManager::Get()->GetGsmSMSClient()->Get( | |
| 91 dbus_connection_, object_path_, index, | |
| 92 base::Bind(&SMSWatcher::GetSMSCallback, | |
| 93 weak_ptr_factory_.GetWeakPtr(), | |
| 94 index)); | |
| 95 } | |
| 96 | |
| 97 void SMSWatcher::RunCallbackWithSMS( | |
| 98 const base::DictionaryValue& sms_dictionary) { | |
| 99 SMS sms; | |
| 100 | |
| 101 std::string number; | |
| 102 if (!sms_dictionary.GetStringWithoutPathExpansion(kNumberKey, &number)) | |
| 103 LOG(WARNING) << "SMS did not contain a number"; | |
| 104 sms.number = number.c_str(); | |
| 105 | |
| 106 std::string text; | |
| 107 if (!sms_dictionary.GetStringWithoutPathExpansion(kTextKey, &text)) { | |
| 108 LOG(WARNING) << "SMS did not contain message text"; | |
| 109 } | |
| 110 sms.text = text.c_str(); | |
| 111 | |
| 112 std::string sms_timestamp; | |
| 113 if (sms_dictionary.GetStringWithoutPathExpansion(kTimestampKey, | |
| 114 &sms_timestamp)) { | |
| 115 base::Time::Exploded exp; | |
| 116 exp.year = decode_bcd(&sms_timestamp[0]); | |
| 117 if (exp.year > 95) | |
| 118 exp.year += 1900; | |
| 119 else | |
| 120 exp.year += 2000; | |
| 121 exp.month = decode_bcd(&sms_timestamp[2]); | |
| 122 exp.day_of_month = decode_bcd(&sms_timestamp[4]); | |
| 123 exp.hour = decode_bcd(&sms_timestamp[6]); | |
| 124 exp.minute = decode_bcd(&sms_timestamp[8]); | |
| 125 exp.second = decode_bcd(&sms_timestamp[10]); | |
| 126 exp.millisecond = 0; | |
| 127 sms.timestamp = base::Time::FromUTCExploded(exp); | |
| 128 int hours = decode_bcd(&sms_timestamp[13]); | |
| 129 if (sms_timestamp[12] == '-') | |
| 130 hours = -hours; | |
| 131 sms.timestamp -= base::TimeDelta::FromHours(hours); | |
| 132 } else { | 426 } else { |
| 133 LOG(WARNING) << "SMS did not contain a timestamp"; | 427 watcher_.reset( |
| 134 sms.timestamp = base::Time(); | 428 new GsmWatcher(device_path_, |
| 135 } | 429 callback_, object_, dbus_connection, |
| 136 | 430 dbus::ObjectPath(object_path_string))); |
| 137 std::string smsc; | |
| 138 if (!sms_dictionary.GetStringWithoutPathExpansion(kSmscKey, &smsc)) | |
| 139 sms.smsc = NULL; | |
| 140 else | |
| 141 sms.smsc = smsc.c_str(); | |
| 142 | |
| 143 double validity = 0; | |
| 144 if (!sms_dictionary.GetDoubleWithoutPathExpansion(kValidityKey, &validity)) | |
| 145 validity = -1; | |
| 146 sms.validity = validity; | |
| 147 | |
| 148 double msgclass = 0; | |
| 149 if (!sms_dictionary.GetDoubleWithoutPathExpansion(kClassKey, &msgclass)) | |
| 150 msgclass = -1; | |
| 151 sms.msgclass = msgclass; | |
| 152 | |
| 153 callback_(object_, device_path_.c_str(), &sms); | |
| 154 } | |
| 155 | |
| 156 void SMSWatcher::GetSMSCallback(uint32 index, | |
| 157 const base::DictionaryValue& sms_dictionary) { | |
| 158 RunCallbackWithSMS(sms_dictionary); | |
| 159 | |
| 160 DBusThreadManager::Get()->GetGsmSMSClient()->Delete( | |
| 161 dbus_connection_, object_path_, index, base::Bind(&DeleteSMSCallback)); | |
| 162 } | |
| 163 | |
| 164 void SMSWatcher::ListSMSCallback(const base::ListValue& result) { | |
| 165 // List() is called only once and no one touches delete_queue_ before List(). | |
| 166 CHECK(delete_queue_.empty()); | |
| 167 for (size_t i = 0; i != result.GetSize(); ++i) { | |
| 168 base::DictionaryValue* sms_dictionary = NULL; | |
| 169 if (!result.GetDictionary(i, &sms_dictionary)) { | |
| 170 LOG(ERROR) << "result[" << i << "] is not a dictionary."; | |
| 171 continue; | |
| 172 } | |
| 173 RunCallbackWithSMS(*sms_dictionary); | |
| 174 double index = 0; | |
| 175 if (sms_dictionary->GetDoubleWithoutPathExpansion(kIndexKey, &index)) | |
| 176 delete_queue_.push_back(index); | |
| 177 } | |
| 178 DeleteSMSInChain(); | |
| 179 } | |
| 180 | |
| 181 void SMSWatcher::DeleteSMSInChain() { | |
| 182 if (!delete_queue_.empty()) { | |
| 183 DBusThreadManager::Get()->GetGsmSMSClient()->Delete( | |
| 184 dbus_connection_, object_path_, delete_queue_.back(), | |
| 185 base::Bind(&SMSWatcher::DeleteSMSInChain, | |
| 186 weak_ptr_factory_.GetWeakPtr())); | |
| 187 delete_queue_.pop_back(); | |
| 188 } | 431 } |
| 189 } | 432 } |
| 190 | 433 |
| 191 } // namespace chromeos | 434 } // namespace chromeos |
| OLD | NEW |