Home | History | Annotate | Download | only in dbus
      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 
     28 void FakeGsmSMSClient::Init(dbus::Bus* bus) {
     29 }
     30 
     31 void FakeGsmSMSClient::SetSmsReceivedHandler(
     32     const std::string& service_name,
     33     const dbus::ObjectPath& object_path,
     34     const SmsReceivedHandler& handler) {
     35   handler_ = handler;
     36 }
     37 
     38 void FakeGsmSMSClient::ResetSmsReceivedHandler(
     39     const std::string& service_name,
     40     const dbus::ObjectPath& object_path) {
     41   handler_.Reset();
     42 }
     43 
     44 void FakeGsmSMSClient::Delete(const std::string& service_name,
     45                               const dbus::ObjectPath& object_path,
     46                               uint32 index,
     47                               const DeleteCallback& callback) {
     48   message_list_.Remove(index, NULL);
     49   callback.Run();
     50 }
     51 
     52 void FakeGsmSMSClient::Get(const std::string& service_name,
     53                            const dbus::ObjectPath& object_path,
     54                            uint32 index,
     55                            const GetCallback& callback) {
     56   base::DictionaryValue* dictionary = NULL;
     57   if (message_list_.GetDictionary(index, &dictionary)) {
     58     callback.Run(*dictionary);
     59     return;
     60   }
     61   base::DictionaryValue empty_dictionary;
     62   callback.Run(empty_dictionary);
     63 }
     64 
     65 void FakeGsmSMSClient::List(const std::string& service_name,
     66                             const dbus::ObjectPath& object_path,
     67                             const ListCallback& callback) {
     68   callback.Run(message_list_);
     69 }
     70 
     71 void FakeGsmSMSClient::RequestUpdate(const std::string& service_name,
     72                                      const dbus::ObjectPath& object_path) {
     73   if (!sms_test_message_switch_present_)
     74     return;
     75 
     76   if (test_index_ >= 0)
     77     return;
     78   test_index_ = 0;
     79   // Call PushTestMessageChain asynchronously so that the handler_ callback
     80   // does not get called from the update request.
     81   base::MessageLoop::current()->PostTask(
     82       FROM_HERE,
     83       base::Bind(&FakeGsmSMSClient::PushTestMessageChain,
     84                  weak_ptr_factory_.GetWeakPtr()));
     85 }
     86 
     87 void FakeGsmSMSClient::PushTestMessageChain() {
     88   if (PushTestMessage())
     89     PushTestMessageDelayed();
     90 }
     91 
     92 void FakeGsmSMSClient::PushTestMessageDelayed() {
     93   const int kSmsMessageDelaySeconds = 5;
     94   base::MessageLoop::current()->PostDelayedTask(
     95       FROM_HERE,
     96       base::Bind(&FakeGsmSMSClient::PushTestMessageChain,
     97                  weak_ptr_factory_.GetWeakPtr()),
     98       base::TimeDelta::FromSeconds(kSmsMessageDelaySeconds));
     99 }
    100 
    101 bool FakeGsmSMSClient::PushTestMessage() {
    102   if (test_index_ >= static_cast<int>(test_messages_.size()))
    103     return false;
    104   base::DictionaryValue* message = new base::DictionaryValue;
    105   message->SetString("number", "000-000-0000");
    106   message->SetString("text", test_messages_[test_index_]);
    107   message->SetInteger("index", test_index_);
    108   int msg_index = message_list_.GetSize();
    109   message_list_.Append(message);
    110   if (!handler_.is_null())
    111     handler_.Run(msg_index, true);
    112   ++test_index_;
    113   return true;
    114 }
    115 
    116 }  // namespace chromeos
    117