1 // Copyright (c) 2013 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 5 #include "base/bind.h" 6 #include "base/message_loop/message_loop.h" 7 #include "chromeos/dbus/fake_gsm_sms_client.h" 8 9 namespace chromeos { 10 11 FakeGsmSMSClient::FakeGsmSMSClient() 12 : test_index_(-1), 13 sms_test_message_switch_present_(false), 14 weak_ptr_factory_(this) { 15 test_messages_.push_back("Test Message 0"); 16 test_messages_.push_back("Test Message 1"); 17 test_messages_.push_back("Test a relatively long message 2"); 18 test_messages_.push_back("Test a very, the quick brown fox jumped" 19 " over the lazy dog, long message 3"); 20 test_messages_.push_back("Test Message 4"); 21 test_messages_.push_back("Test Message 5"); 22 test_messages_.push_back("Test Message 6"); 23 } 24 25 FakeGsmSMSClient::~FakeGsmSMSClient() {} 26 27 void FakeGsmSMSClient::SetSmsReceivedHandler( 28 const std::string& service_name, 29 const dbus::ObjectPath& object_path, 30 const SmsReceivedHandler& handler) { 31 handler_ = handler; 32 } 33 34 void FakeGsmSMSClient::ResetSmsReceivedHandler( 35 const std::string& service_name, 36 const dbus::ObjectPath& object_path) { 37 handler_.Reset(); 38 } 39 40 void FakeGsmSMSClient::Delete(const std::string& service_name, 41 const dbus::ObjectPath& object_path, 42 uint32 index, 43 const DeleteCallback& callback) { 44 message_list_.Remove(index, NULL); 45 callback.Run(); 46 } 47 48 void FakeGsmSMSClient::Get(const std::string& service_name, 49 const dbus::ObjectPath& object_path, 50 uint32 index, 51 const GetCallback& callback) { 52 base::DictionaryValue* dictionary = NULL; 53 if (message_list_.GetDictionary(index, &dictionary)) { 54 callback.Run(*dictionary); 55 return; 56 } 57 base::DictionaryValue empty_dictionary; 58 callback.Run(empty_dictionary); 59 } 60 61 void FakeGsmSMSClient::List(const std::string& service_name, 62 const dbus::ObjectPath& object_path, 63 const ListCallback& callback) { 64 callback.Run(message_list_); 65 } 66 67 void FakeGsmSMSClient::RequestUpdate(const std::string& service_name, 68 const dbus::ObjectPath& object_path) { 69 if (!sms_test_message_switch_present_) 70 return; 71 72 if (test_index_ >= 0) 73 return; 74 test_index_ = 0; 75 // Call PushTestMessageChain asynchronously so that the handler_ callback 76 // does not get called from the update request. 77 base::MessageLoop::current()->PostTask( 78 FROM_HERE, 79 base::Bind(&FakeGsmSMSClient::PushTestMessageChain, 80 weak_ptr_factory_.GetWeakPtr())); 81 } 82 83 void FakeGsmSMSClient::PushTestMessageChain() { 84 if (PushTestMessage()) 85 PushTestMessageDelayed(); 86 } 87 88 void FakeGsmSMSClient::PushTestMessageDelayed() { 89 const int kSmsMessageDelaySeconds = 5; 90 base::MessageLoop::current()->PostDelayedTask( 91 FROM_HERE, 92 base::Bind(&FakeGsmSMSClient::PushTestMessageChain, 93 weak_ptr_factory_.GetWeakPtr()), 94 base::TimeDelta::FromSeconds(kSmsMessageDelaySeconds)); 95 } 96 97 bool FakeGsmSMSClient::PushTestMessage() { 98 if (test_index_ >= static_cast<int>(test_messages_.size())) 99 return false; 100 base::DictionaryValue* message = new base::DictionaryValue; 101 message->SetString("number", "000-000-0000"); 102 message->SetString("text", test_messages_[test_index_]); 103 message->SetInteger("index", test_index_); 104 int msg_index = message_list_.GetSize(); 105 message_list_.Append(message); 106 if (!handler_.is_null()) 107 handler_.Run(msg_index, true); 108 ++test_index_; 109 return true; 110 } 111 112 } // namespace chromeos 113