| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/dbus/cashew_client.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/chromeos/chromeos_version.h" | |
| 9 #include "base/values.h" | |
| 10 #include "dbus/bus.h" | |
| 11 #include "dbus/message.h" | |
| 12 #include "dbus/object_path.h" | |
| 13 #include "dbus/object_proxy.h" | |
| 14 #include "dbus/values_util.h" | |
| 15 #include "third_party/cros_system_api/dbus/service_constants.h" | |
| 16 | |
| 17 namespace chromeos { | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 // Does nothing. | |
| 22 // This method is used to handle results of RequestDataPlansUpdate method call. | |
| 23 void DoNothing(dbus::Response* response) { | |
| 24 } | |
| 25 | |
| 26 // The CashewClient implementation. | |
| 27 class CashewClientImpl : public CashewClient { | |
| 28 public: | |
| 29 explicit CashewClientImpl(dbus::Bus* bus) | |
| 30 : proxy_(bus->GetObjectProxy( | |
| 31 cashew::kCashewServiceName, | |
| 32 dbus::ObjectPath(cashew::kCashewServicePath))), | |
| 33 weak_ptr_factory_(this) { | |
| 34 proxy_->ConnectToSignal( | |
| 35 cashew::kCashewServiceInterface, | |
| 36 cashew::kMonitorDataPlanUpdate, | |
| 37 base::Bind(&CashewClientImpl::OnDataPlansUpdate, | |
| 38 weak_ptr_factory_.GetWeakPtr()), | |
| 39 base::Bind(&CashewClientImpl::OnSignalConnected, | |
| 40 weak_ptr_factory_.GetWeakPtr())); | |
| 41 } | |
| 42 | |
| 43 // CashewClient override. | |
| 44 virtual void SetDataPlansUpdateHandler( | |
| 45 DataPlansUpdateHandler handler) OVERRIDE { | |
| 46 data_plans_update_handler_ = handler; | |
| 47 } | |
| 48 | |
| 49 // CashewClient override. | |
| 50 virtual void ResetDataPlansUpdateHandler() OVERRIDE { | |
| 51 data_plans_update_handler_.Reset(); | |
| 52 } | |
| 53 | |
| 54 // CashewClient override. | |
| 55 virtual void RequestDataPlansUpdate() OVERRIDE { | |
| 56 dbus::MethodCall method_call(cashew::kCashewServiceInterface, | |
| 57 cashew::kRequestDataPlanFunction); | |
| 58 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 59 base::Bind(&DoNothing)); | |
| 60 } | |
| 61 | |
| 62 private: | |
| 63 // Handles DataPlansUpdate signal. | |
| 64 void OnDataPlansUpdate(dbus::Signal* signal) { | |
| 65 dbus::MessageReader reader(signal); | |
| 66 scoped_ptr<Value> value(dbus::PopDataAsValue(&reader)); | |
| 67 ListValue* data_plans = NULL; | |
| 68 if (!value.get() || !value->GetAsList(&data_plans)) { | |
| 69 LOG(ERROR) << "Invalid signal: " << signal->ToString(); | |
| 70 return; | |
| 71 } | |
| 72 if (!data_plans_update_handler_.is_null()) | |
| 73 data_plans_update_handler_.Run(*data_plans); | |
| 74 } | |
| 75 | |
| 76 // Handles the result of signal connection setup. | |
| 77 void OnSignalConnected(const std::string& interface, | |
| 78 const std::string& signal, | |
| 79 bool successed) { | |
| 80 LOG_IF(ERROR, !successed) << "Connect to " << interface << " " << | |
| 81 signal << " failed."; | |
| 82 } | |
| 83 | |
| 84 dbus::ObjectProxy* proxy_; | |
| 85 base::WeakPtrFactory<CashewClientImpl> weak_ptr_factory_; | |
| 86 DataPlansUpdateHandler data_plans_update_handler_; | |
| 87 | |
| 88 DISALLOW_COPY_AND_ASSIGN(CashewClientImpl); | |
| 89 }; | |
| 90 | |
| 91 // A stub implementaion of CashewClient. | |
| 92 class CashewClientStubImpl : public CashewClient { | |
| 93 public: | |
| 94 CashewClientStubImpl() {} | |
| 95 | |
| 96 virtual ~CashewClientStubImpl() {} | |
| 97 | |
| 98 // CashewClient override. | |
| 99 virtual void SetDataPlansUpdateHandler( | |
| 100 DataPlansUpdateHandler handler) OVERRIDE {} | |
| 101 | |
| 102 // CashewClient override. | |
| 103 virtual void ResetDataPlansUpdateHandler() OVERRIDE {} | |
| 104 | |
| 105 // CashewClient override. | |
| 106 virtual void RequestDataPlansUpdate() OVERRIDE {} | |
| 107 | |
| 108 private: | |
| 109 DISALLOW_COPY_AND_ASSIGN(CashewClientStubImpl); | |
| 110 }; | |
| 111 | |
| 112 } // namespace | |
| 113 | |
| 114 //////////////////////////////////////////////////////////////////////////////// | |
| 115 // CashewClient | |
| 116 | |
| 117 CashewClient::CashewClient() {} | |
| 118 | |
| 119 CashewClient::~CashewClient() {} | |
| 120 | |
| 121 // static | |
| 122 CashewClient* CashewClient::Create(dbus::Bus* bus) { | |
| 123 if (base::chromeos::IsRunningOnChromeOS()) | |
| 124 return new CashewClientImpl(bus); | |
| 125 else | |
| 126 return new CashewClientStubImpl(); | |
| 127 } | |
| 128 | |
| 129 } // namespace chromeos | |
| OLD | NEW |