| 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/gsm_sms_client.h" | 4 #include "chromeos/dbus/gsm_sms_client.h" |
| 5 | 5 |
| 6 #include "base/bind.h" | 6 #include "base/bind.h" |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 dbus::Bus* bus_; | 223 dbus::Bus* bus_; |
| 224 ProxyMap proxies_; | 224 ProxyMap proxies_; |
| 225 STLValueDeleter<ProxyMap> proxies_deleter_; | 225 STLValueDeleter<ProxyMap> proxies_deleter_; |
| 226 | 226 |
| 227 DISALLOW_COPY_AND_ASSIGN(GsmSMSClientImpl); | 227 DISALLOW_COPY_AND_ASSIGN(GsmSMSClientImpl); |
| 228 }; | 228 }; |
| 229 | 229 |
| 230 // A stub implementaion of GsmSMSClient. | 230 // A stub implementaion of GsmSMSClient. |
| 231 class GsmSMSClientStubImpl : public GsmSMSClient { | 231 class GsmSMSClientStubImpl : public GsmSMSClient { |
| 232 public: | 232 public: |
| 233 GsmSMSClientStubImpl() : test_index_(0), weak_ptr_factory_(this) { | 233 GsmSMSClientStubImpl() : test_index_(-1), weak_ptr_factory_(this) { |
| 234 test_messages_.push_back("Test Message 0"); | 234 test_messages_.push_back("Test Message 0"); |
| 235 test_messages_.push_back("Test Message 1"); | 235 test_messages_.push_back("Test Message 1"); |
| 236 test_messages_.push_back("Test a relatively long message 2"); | 236 test_messages_.push_back("Test a relatively long message 2"); |
| 237 test_messages_.push_back("Test a very, the quick brown fox jumped" | 237 test_messages_.push_back("Test a very, the quick brown fox jumped" |
| 238 " over the lazy dog, long message 3"); | 238 " over the lazy dog, long message 3"); |
| 239 test_messages_.push_back("Test Message 4"); | 239 test_messages_.push_back("Test Message 4"); |
| 240 test_messages_.push_back("Test Message 5"); | 240 test_messages_.push_back("Test Message 5"); |
| 241 test_messages_.push_back("Test Message 6"); | 241 test_messages_.push_back("Test Message 6"); |
| 242 } | 242 } |
| 243 | 243 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 // GsmSMSClient override. | 284 // GsmSMSClient override. |
| 285 virtual void List(const std::string& service_name, | 285 virtual void List(const std::string& service_name, |
| 286 const dbus::ObjectPath& object_path, | 286 const dbus::ObjectPath& object_path, |
| 287 const ListCallback& callback) OVERRIDE { | 287 const ListCallback& callback) OVERRIDE { |
| 288 callback.Run(message_list_); | 288 callback.Run(message_list_); |
| 289 } | 289 } |
| 290 | 290 |
| 291 // GsmSMSClient override. | 291 // GsmSMSClient override. |
| 292 virtual void RequestUpdate(const std::string& service_name, | 292 virtual void RequestUpdate(const std::string& service_name, |
| 293 const dbus::ObjectPath& object_path) { | 293 const dbus::ObjectPath& object_path) { |
| 294 PushTestMessageChain(); | 294 if (test_index_ >= 0) |
| 295 return; |
| 296 test_index_ = 0; |
| 297 // Call PushTestMessageChain asynchronously so that the handler_ callback |
| 298 // does not get called from the update request. |
| 299 MessageLoop::current()->PostTask( |
| 300 FROM_HERE, |
| 301 base::Bind(&GsmSMSClientStubImpl::PushTestMessageChain, |
| 302 weak_ptr_factory_.GetWeakPtr())); |
| 295 } | 303 } |
| 296 | 304 |
| 297 private: | 305 private: |
| 298 void PushTestMessageChain() { | 306 void PushTestMessageChain() { |
| 299 if (PushTestMessage()) | 307 if (PushTestMessage()) |
| 300 PushTestMessageDelayed(); | 308 PushTestMessageDelayed(); |
| 301 } | 309 } |
| 302 | 310 |
| 303 void PushTestMessageDelayed() { | 311 void PushTestMessageDelayed() { |
| 304 const int kSmsMessageDelaySeconds = 5; | 312 const int kSmsMessageDelaySeconds = 5; |
| 305 MessageLoop::current()->PostDelayedTask( | 313 MessageLoop::current()->PostDelayedTask( |
| 306 FROM_HERE, | 314 FROM_HERE, |
| 307 base::Bind(&GsmSMSClientStubImpl::PushTestMessageChain, | 315 base::Bind(&GsmSMSClientStubImpl::PushTestMessageChain, |
| 308 weak_ptr_factory_.GetWeakPtr()), | 316 weak_ptr_factory_.GetWeakPtr()), |
| 309 base::TimeDelta::FromSeconds(kSmsMessageDelaySeconds)); | 317 base::TimeDelta::FromSeconds(kSmsMessageDelaySeconds)); |
| 310 } | 318 } |
| 311 | 319 |
| 312 bool PushTestMessage() { | 320 bool PushTestMessage() { |
| 313 if (test_index_ >= test_messages_.size()) | 321 if (test_index_ >= static_cast<int>(test_messages_.size())) |
| 314 return false; | 322 return false; |
| 315 base::DictionaryValue* message = new base::DictionaryValue; | 323 base::DictionaryValue* message = new base::DictionaryValue; |
| 316 message->SetString("number", "000-000-0000"); | 324 message->SetString("number", "000-000-0000"); |
| 317 message->SetString("text", test_messages_[test_index_]); | 325 message->SetString("text", test_messages_[test_index_]); |
| 318 message->SetInteger("index", test_index_); | 326 message->SetInteger("index", test_index_); |
| 319 int msg_index = message_list_.GetSize(); | 327 int msg_index = message_list_.GetSize(); |
| 320 message_list_.Append(message); | 328 message_list_.Append(message); |
| 321 if (!handler_.is_null()) | 329 if (!handler_.is_null()) |
| 322 handler_.Run(msg_index, true); | 330 handler_.Run(msg_index, true); |
| 323 ++test_index_; | 331 ++test_index_; |
| 324 return true; | 332 return true; |
| 325 } | 333 } |
| 326 | 334 |
| 327 size_t test_index_; | 335 int test_index_; |
| 328 std::vector<std::string> test_messages_; | 336 std::vector<std::string> test_messages_; |
| 329 base::ListValue message_list_; | 337 base::ListValue message_list_; |
| 330 SmsReceivedHandler handler_; | 338 SmsReceivedHandler handler_; |
| 331 base::WeakPtrFactory<GsmSMSClientStubImpl> weak_ptr_factory_; | 339 base::WeakPtrFactory<GsmSMSClientStubImpl> weak_ptr_factory_; |
| 332 | 340 |
| 333 DISALLOW_COPY_AND_ASSIGN(GsmSMSClientStubImpl); | 341 DISALLOW_COPY_AND_ASSIGN(GsmSMSClientStubImpl); |
| 334 }; | 342 }; |
| 335 | 343 |
| 336 } // namespace | 344 } // namespace |
| 337 | 345 |
| 338 //////////////////////////////////////////////////////////////////////////////// | 346 //////////////////////////////////////////////////////////////////////////////// |
| 339 // GsmSMSClient | 347 // GsmSMSClient |
| 340 | 348 |
| 341 GsmSMSClient::GsmSMSClient() {} | 349 GsmSMSClient::GsmSMSClient() {} |
| 342 | 350 |
| 343 GsmSMSClient::~GsmSMSClient() {} | 351 GsmSMSClient::~GsmSMSClient() {} |
| 344 | 352 |
| 345 // static | 353 // static |
| 346 GsmSMSClient* GsmSMSClient::Create(DBusClientImplementationType type, | 354 GsmSMSClient* GsmSMSClient::Create(DBusClientImplementationType type, |
| 347 dbus::Bus* bus) { | 355 dbus::Bus* bus) { |
| 348 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | 356 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) |
| 349 return new GsmSMSClientImpl(bus); | 357 return new GsmSMSClientImpl(bus); |
| 350 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | 358 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); |
| 351 return new GsmSMSClientStubImpl(); | 359 return new GsmSMSClientStubImpl(); |
| 352 } | 360 } |
| 353 | 361 |
| 354 } // namespace chromeos | 362 } // namespace chromeos |
| OLD | NEW |