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"; |
| 144 } |
| 145 sms.number = number.c_str(); |
| 146 |
| 147 std::string text; |
| 148 if (!sms_dictionary.GetStringWithoutPathExpansion(SMSWatcher::kTextKey, |
| 149 &text)) { |
| 150 LOG(WARNING) << "SMS did not contain message text"; |
| 151 } |
| 152 sms.text = text.c_str(); |
| 153 |
| 154 std::string sms_timestamp; |
| 155 if (sms_dictionary.GetStringWithoutPathExpansion(SMSWatcher::kTimestampKey, |
| 156 &sms_timestamp)) { |
| 157 decode_timestamp(sms_timestamp, &sms); |
| 158 } else { |
| 159 LOG(WARNING) << "SMS did not contain a timestamp"; |
| 160 sms.timestamp = base::Time(); |
| 161 } |
| 162 |
| 163 std::string smsc; |
| 164 if (!sms_dictionary.GetStringWithoutPathExpansion(SMSWatcher::kSmscKey, |
| 165 &smsc)) { |
| 166 sms.smsc = NULL; |
| 167 } else { |
| 168 sms.smsc = smsc.c_str(); |
| 169 } |
| 170 |
| 171 double validity = 0; |
| 172 if (!sms_dictionary.GetDoubleWithoutPathExpansion(SMSWatcher::kValidityKey, |
| 173 &validity)) { |
| 174 validity = -1; |
| 175 } |
| 176 sms.validity = validity; |
| 177 |
| 178 double msgclass = 0; |
| 179 if (!sms_dictionary.GetDoubleWithoutPathExpansion(SMSWatcher::kClassKey, |
| 180 &msgclass)) { |
| 181 msgclass = -1; |
| 182 } |
| 183 sms.msgclass = msgclass; |
| 184 |
| 185 callback_(object_, device_path_.c_str(), &sms); |
| 186 } |
| 187 |
| 188 // Callback for Get() method from ModemManager.Modem.Gsm.SMS |
| 189 void GetSMSCallback(uint32 index, |
| 190 const base::DictionaryValue& sms_dictionary) { |
| 191 RunCallbackWithSMS(sms_dictionary); |
| 192 |
| 193 DBusThreadManager::Get()->GetGsmSMSClient()->Delete( |
| 194 dbus_connection_, object_path_, index, base::Bind(&DeleteSMSCallback)); |
| 195 } |
| 196 |
| 197 // Callback for List() method. |
| 198 void ListSMSCallback(const base::ListValue& result) { |
| 199 // List() is called only once; no one touches delete_queue_ before List(). |
| 200 CHECK(delete_queue_.empty()); |
| 201 for (size_t i = 0; i != result.GetSize(); ++i) { |
| 202 base::DictionaryValue* sms_dictionary = NULL; |
| 203 if (!result.GetDictionary(i, &sms_dictionary)) { |
| 204 LOG(ERROR) << "result[" << i << "] is not a dictionary."; |
| 205 continue; |
| 206 } |
| 207 RunCallbackWithSMS(*sms_dictionary); |
| 208 double index = 0; |
| 209 if (sms_dictionary->GetDoubleWithoutPathExpansion(SMSWatcher::kIndexKey, |
| 210 &index)) { |
| 211 delete_queue_.push_back(index); |
| 212 } |
| 213 } |
| 214 DeleteSMSInChain(); |
| 215 } |
| 216 |
| 217 // Deletes SMSs in the queue. |
| 218 void DeleteSMSInChain() { |
| 219 if (!delete_queue_.empty()) { |
| 220 DBusThreadManager::Get()->GetGsmSMSClient()->Delete( |
| 221 dbus_connection_, object_path_, delete_queue_.back(), |
| 222 base::Bind(&GsmWatcher::DeleteSMSInChain, |
| 223 weak_ptr_factory_.GetWeakPtr())); |
| 224 delete_queue_.pop_back(); |
| 225 } |
| 226 } |
| 227 |
| 228 base::WeakPtrFactory<GsmWatcher> weak_ptr_factory_; |
| 229 std::vector<uint32> delete_queue_; |
| 230 |
| 231 DISALLOW_COPY_AND_ASSIGN(GsmWatcher); |
| 232 }; |
| 233 |
| 234 class ModemManager1Watcher : public SMSWatcher::WatcherBase { |
| 235 public: |
| 236 ModemManager1Watcher(const std::string& device_path, |
| 237 MonitorSMSCallback callback, |
| 238 void *object, |
| 239 const std::string& dbus_connection, |
| 240 const dbus::ObjectPath& object_path) |
| 241 : WatcherBase(device_path, callback, object, dbus_connection, |
| 242 object_path), |
| 243 weak_ptr_factory_(this), |
| 244 deleting_messages_(false), |
| 245 retrieving_messages_(false) { |
| 246 DBusThreadManager::Get()->GetModemMessagingClient()->SetSmsReceivedHandler( |
| 247 dbus_connection_, |
| 248 object_path_, |
| 249 base::Bind(&ModemManager1Watcher::OnSmsReceived, |
| 250 weak_ptr_factory_.GetWeakPtr())); |
| 251 |
| 252 DBusThreadManager::Get()->GetModemMessagingClient()->List( |
| 253 dbus_connection_, object_path_, |
| 254 base::Bind(&ModemManager1Watcher::ListSMSCallback, |
| 255 weak_ptr_factory_.GetWeakPtr())); |
| 256 } |
| 257 |
| 258 virtual ~ModemManager1Watcher() { |
| 259 DBusThreadManager::Get()->GetModemMessagingClient() |
| 260 ->ResetSmsReceivedHandler(dbus_connection_, object_path_); |
| 261 } |
| 262 |
| 263 private: |
| 264 void ListSMSCallback( |
| 265 const std::vector<dbus::ObjectPath>& paths) { |
| 266 // This receives all messages, so clear any pending gets and deletes. |
| 267 retrieval_queue_.clear(); |
| 268 delete_queue_.clear(); |
| 269 |
| 270 retrieval_queue_.resize(paths.size()); |
| 271 std::copy(paths.begin(), paths.end(), retrieval_queue_.begin()); |
| 272 if (!retrieving_messages_) |
| 273 GetMessages(); |
| 274 } |
| 275 |
| 276 // Messages must be deleted one at a time, since we can not |
| 277 // guarantee the order the deletion will be executed in. Delete |
| 278 // messages from the back of the list so that the indices are |
| 279 // valid. |
| 280 void DeleteMessages() { |
| 281 if (delete_queue_.empty()) { |
| 282 deleting_messages_ = false; |
| 283 return; |
| 284 } |
| 285 deleting_messages_ = true; |
| 286 dbus::ObjectPath sms_path = delete_queue_.back(); |
| 287 delete_queue_.pop_back(); |
| 288 DBusThreadManager::Get()->GetModemMessagingClient()->Delete( |
| 289 dbus_connection_, object_path_, sms_path, |
| 290 base::Bind(&ModemManager1Watcher::DeleteMessages, |
| 291 weak_ptr_factory_.GetWeakPtr())); |
| 292 } |
| 293 |
| 294 // Messages must be fetched one at a time, so that we do not queue too |
| 295 // many requests to a single threaded server. |
| 296 void GetMessages() { |
| 297 if (retrieval_queue_.empty()) { |
| 298 retrieving_messages_ = false; |
| 299 if (!deleting_messages_) |
| 300 DeleteMessages(); |
| 301 return; |
| 302 } |
| 303 retrieving_messages_ = true; |
| 304 dbus::ObjectPath sms_path = retrieval_queue_.front(); |
| 305 retrieval_queue_.pop_front(); |
| 306 DBusThreadManager::Get()->GetSMSClient()->GetAll( |
| 307 dbus_connection_, sms_path, |
| 308 base::Bind(&ModemManager1Watcher::GetCallback, |
| 309 weak_ptr_factory_.GetWeakPtr())); |
| 310 delete_queue_.push_back(sms_path); |
| 311 } |
| 312 |
| 313 // Handles arrival of a new SMS message. |
| 314 void OnSmsReceived(const dbus::ObjectPath& sms_path, bool complete) { |
| 315 // Only handle complete messages. |
| 316 if (!complete) |
| 317 return; |
| 318 retrieval_queue_.push_back(sms_path); |
| 319 if (!retrieving_messages_) |
| 320 GetMessages(); |
| 321 } |
| 322 |
| 323 // Runs |callback_| with a SMS. |
| 324 void RunCallbackWithSMS(const base::DictionaryValue& sms_dictionary) { |
| 325 SMS sms; |
| 326 |
| 327 std::string number; |
| 328 if (!sms_dictionary.GetStringWithoutPathExpansion( |
| 329 SMSWatcher::kModemManager1NumberKey, &number)) { |
| 330 LOG(WARNING) << "SMS did not contain a number"; |
| 331 } |
| 332 sms.number = number.c_str(); |
| 333 |
| 334 std::string text; |
| 335 if (!sms_dictionary.GetStringWithoutPathExpansion( |
| 336 SMSWatcher::kModemManager1TextKey, &text)) { |
| 337 LOG(WARNING) << "SMS did not contain message text"; |
| 338 } |
| 339 sms.text = text.c_str(); |
| 340 |
| 341 std::string sms_timestamp; |
| 342 if (sms_dictionary.GetStringWithoutPathExpansion( |
| 343 SMSWatcher::kModemManager1TimestampKey, &sms_timestamp)) { |
| 344 decode_timestamp(sms_timestamp, &sms); |
| 345 } else { |
| 346 LOG(WARNING) << "SMS did not contain a timestamp"; |
| 347 sms.timestamp = base::Time(); |
| 348 } |
| 349 |
| 350 std::string smsc; |
| 351 if (!sms_dictionary.GetStringWithoutPathExpansion( |
| 352 SMSWatcher::kModemManager1SmscKey, &smsc)) { |
| 353 sms.smsc = NULL; |
| 354 } else { |
| 355 sms.smsc = smsc.c_str(); |
| 356 } |
| 357 |
| 358 double validity = 0; |
| 359 if (!sms_dictionary.GetDoubleWithoutPathExpansion( |
| 360 SMSWatcher::kModemManager1ValidityKey, &validity)) { |
| 361 validity = -1; |
| 362 } |
| 363 sms.validity = validity; |
| 364 |
| 365 double msgclass = 0; |
| 366 if (!sms_dictionary.GetDoubleWithoutPathExpansion( |
| 367 SMSWatcher::kModemManager1ClassKey, &msgclass)) { |
| 368 msgclass = -1; |
| 369 } |
| 370 sms.msgclass = msgclass; |
| 371 |
| 372 callback_(object_, device_path_.c_str(), &sms); |
| 373 } |
| 374 |
| 375 void GetCallback(const base::DictionaryValue& dictionary) { |
| 376 RunCallbackWithSMS(dictionary); |
| 377 GetMessages(); |
| 378 } |
| 379 |
| 380 base::WeakPtrFactory<ModemManager1Watcher> weak_ptr_factory_; |
| 381 bool deleting_messages_; |
| 382 bool retrieving_messages_; |
| 383 std::vector<dbus::ObjectPath> delete_queue_; |
| 384 std::deque<dbus::ObjectPath> retrieval_queue_; |
| 385 |
| 386 DISALLOW_COPY_AND_ASSIGN(ModemManager1Watcher); |
| 387 }; |
| 388 |
| 389 } // namespace |
| 390 |
35 SMSWatcher::SMSWatcher(const std::string& modem_device_path, | 391 SMSWatcher::SMSWatcher(const std::string& modem_device_path, |
36 MonitorSMSCallback callback, | 392 MonitorSMSCallback callback, |
37 void* object) | 393 void* object) |
38 : weak_ptr_factory_(this), | 394 : weak_ptr_factory_(this), |
39 device_path_(modem_device_path), | 395 device_path_(modem_device_path), |
40 callback_(callback), | 396 callback_(callback), |
41 object_(object) { | 397 object_(object) { |
42 DBusThreadManager::Get()->GetFlimflamDeviceClient()->GetProperties( | 398 DBusThreadManager::Get()->GetFlimflamDeviceClient()->GetProperties( |
43 dbus::ObjectPath(modem_device_path), | 399 dbus::ObjectPath(modem_device_path), |
44 base::Bind(&SMSWatcher::DevicePropertiesCallback, | 400 base::Bind(&SMSWatcher::DevicePropertiesCallback, |
45 weak_ptr_factory_.GetWeakPtr())); | 401 weak_ptr_factory_.GetWeakPtr())); |
46 } | 402 } |
47 | 403 |
48 SMSWatcher::~SMSWatcher() { | 404 SMSWatcher::~SMSWatcher() { |
49 if (!dbus_connection_.empty() && !object_path_.value().empty()) { | |
50 DBusThreadManager::Get()->GetGsmSMSClient()->ResetSmsReceivedHandler( | |
51 dbus_connection_, object_path_); | |
52 } | |
53 } | 405 } |
54 | 406 |
55 void SMSWatcher::DevicePropertiesCallback( | 407 void SMSWatcher::DevicePropertiesCallback( |
56 DBusMethodCallStatus call_status, | 408 DBusMethodCallStatus call_status, |
57 const base::DictionaryValue& properties) { | 409 const base::DictionaryValue& properties) { |
58 if (call_status != DBUS_METHOD_CALL_SUCCESS) | 410 if (call_status != DBUS_METHOD_CALL_SUCCESS) |
59 return; | 411 return; |
60 | 412 |
| 413 std::string dbus_connection; |
61 if (!properties.GetStringWithoutPathExpansion( | 414 if (!properties.GetStringWithoutPathExpansion( |
62 flimflam::kDBusConnectionProperty, &dbus_connection_)) { | 415 flimflam::kDBusConnectionProperty, &dbus_connection)) { |
63 LOG(WARNING) << "Modem device properties do not include DBus connection."; | 416 LOG(WARNING) << "Modem device properties do not include DBus connection."; |
64 return; | 417 return; |
65 } | 418 } |
66 | 419 |
67 std::string object_path_string; | 420 std::string object_path_string; |
68 if (!properties.GetStringWithoutPathExpansion( | 421 if (!properties.GetStringWithoutPathExpansion( |
69 flimflam::kDBusObjectProperty, &object_path_string)) { | 422 flimflam::kDBusObjectProperty, &object_path_string)) { |
70 LOG(WARNING) << "Modem device properties do not include DBus object."; | 423 LOG(WARNING) << "Modem device properties do not include DBus object."; |
71 return; | 424 return; |
72 } | 425 } |
73 object_path_ = dbus::ObjectPath(object_path_string); | |
74 | 426 |
75 DBusThreadManager::Get()->GetGsmSMSClient()->SetSmsReceivedHandler( | 427 if (object_path_string.compare( |
76 dbus_connection_, | 428 0, sizeof(modemmanager::kModemManager1ServicePath) - 1, |
77 object_path_, | 429 modemmanager::kModemManager1ServicePath) == 0) { |
78 base::Bind(&SMSWatcher::OnSmsReceived, weak_ptr_factory_.GetWeakPtr())); | 430 watcher_.reset( |
79 | 431 new ModemManager1Watcher(device_path_, |
80 DBusThreadManager::Get()->GetGsmSMSClient()->List( | 432 callback_, object_, dbus_connection, |
81 dbus_connection_, object_path_, | 433 dbus::ObjectPath(object_path_string))); |
82 base::Bind(&SMSWatcher::ListSMSCallback, | |
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 { | 434 } else { |
133 LOG(WARNING) << "SMS did not contain a timestamp"; | 435 watcher_.reset( |
134 sms.timestamp = base::Time(); | 436 new GsmWatcher(device_path_, |
135 } | 437 callback_, object_, dbus_connection, |
136 | 438 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 } | 439 } |
189 } | 440 } |
190 | 441 |
191 } // namespace chromeos | 442 } // namespace chromeos |
OLD | NEW |