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