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 | 4 |
5 #include "chromeos/dbus/ibus/ibus_config_client.h" | 5 #include "chromeos/dbus/ibus/ibus_config_client.h" |
6 | 6 |
7 #include <vector> | |
8 | |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/callback.h" | 10 #include "base/callback.h" |
9 #include "chromeos/dbus/ibus/ibus_constants.h" | 11 #include "chromeos/dbus/ibus/ibus_constants.h" |
10 #include "chromeos/dbus/ibus/ibus_component.h" | 12 #include "chromeos/dbus/ibus/ibus_component.h" |
11 #include "dbus/bus.h" | 13 #include "dbus/bus.h" |
12 #include "dbus/message.h" | 14 #include "dbus/message.h" |
13 #include "dbus/object_path.h" | 15 #include "dbus/object_path.h" |
14 #include "dbus/object_proxy.h" | 16 #include "dbus/object_proxy.h" |
15 | 17 |
16 namespace chromeos { | 18 namespace chromeos { |
17 | 19 |
18 namespace { | 20 namespace { |
19 | 21 |
20 // The IBusConfigClient implementation. | 22 // The IBusConfigClient implementation. |
21 class IBusConfigClientImpl : public IBusConfigClient { | 23 class IBusConfigClientImpl : public IBusConfigClient { |
22 public: | 24 public: |
23 explicit IBusConfigClientImpl(dbus::Bus* bus) | 25 explicit IBusConfigClientImpl(dbus::Bus* bus) |
24 : proxy_(bus->GetObjectProxy(ibus::kServiceName, | 26 : proxy_(NULL), |
25 dbus::ObjectPath( | 27 bus_(bus), |
26 ibus::config::kServicePath))), | |
27 weak_ptr_factory_(this) { | 28 weak_ptr_factory_(this) { |
28 } | 29 } |
29 | 30 |
30 virtual ~IBusConfigClientImpl() {} | 31 virtual ~IBusConfigClientImpl() {} |
31 | 32 |
32 // IBusConfigClient override. | 33 // IBusConfigClient override. |
34 virtual void AsyncInitialize(const OnIBusConfigReady& on_ready) OVERRIDE { | |
35 // We should check that the ibus-config daemon actually works first, so we | |
36 // can't initialize synchronously. | |
37 dbus::ObjectProxy* dbus_proxy = bus_->GetObjectProxy( | |
38 ibus::kServiceName, | |
39 dbus::ObjectPath(ibus::kDBusObjectPath)); | |
40 dbus::MethodCall method_call(ibus::kDBusInterface, | |
41 ibus::kGetNameOwnerMethod); | |
42 dbus::MessageWriter writer(&method_call); | |
43 writer.AppendString(ibus::config::kServiceName); | |
44 dbus_proxy->CallMethod( | |
45 &method_call, | |
46 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
47 base::Bind(&IBusConfigClientImpl::OnGetNameOwner, | |
48 weak_ptr_factory_.GetWeakPtr(), | |
49 on_ready)); | |
50 | |
51 // Also watch NameOwnerChanged signal which is fired when the ibus-config | |
52 // daemon request its name ownership. | |
53 dbus_proxy->SetNameOwnerChangedCallback( | |
54 base::Bind(&IBusConfigClientImpl::OnNameOwnerChanged, | |
55 weak_ptr_factory_.GetWeakPtr(), | |
56 on_ready)); | |
57 } | |
58 | |
59 // IBusConfigClient override. | |
33 virtual void SetStringValue(const std::string& section, | 60 virtual void SetStringValue(const std::string& section, |
34 const std::string& key, | 61 const std::string& key, |
35 const std::string& value, | 62 const std::string& value, |
36 const ErrorCallback& error_callback) OVERRIDE { | 63 const ErrorCallback& error_callback) OVERRIDE { |
64 if (!proxy_) | |
65 return; | |
37 DCHECK(!error_callback.is_null()); | 66 DCHECK(!error_callback.is_null()); |
38 dbus::MethodCall method_call(ibus::config::kServiceInterface, | 67 dbus::MethodCall method_call(ibus::config::kServiceInterface, |
39 ibus::config::kSetValueMethod); | 68 ibus::config::kSetValueMethod); |
40 dbus::MessageWriter writer(&method_call); | 69 dbus::MessageWriter writer(&method_call); |
41 writer.AppendString(section); | 70 writer.AppendString(section); |
42 writer.AppendString(key); | 71 writer.AppendString(key); |
43 dbus::MessageWriter variant_writer(NULL); | 72 dbus::MessageWriter variant_writer(NULL); |
44 writer.OpenVariant("s", &variant_writer); | 73 writer.OpenVariant("s", &variant_writer); |
45 variant_writer.AppendString(value); | 74 variant_writer.AppendString(value); |
46 writer.CloseContainer(&variant_writer); | 75 writer.CloseContainer(&variant_writer); |
47 CallWithDefaultCallback(&method_call, error_callback); | 76 CallWithDefaultCallback(&method_call, error_callback); |
48 } | 77 } |
49 | 78 |
50 // IBusConfigClient override. | 79 // IBusConfigClient override. |
51 virtual void SetIntValue(const std::string& section, | 80 virtual void SetIntValue(const std::string& section, |
52 const std::string& key, | 81 const std::string& key, |
53 int value, | 82 int value, |
54 const ErrorCallback& error_callback) OVERRIDE { | 83 const ErrorCallback& error_callback) OVERRIDE { |
84 if (!proxy_) | |
85 return; | |
55 DCHECK(!error_callback.is_null()); | 86 DCHECK(!error_callback.is_null()); |
56 dbus::MethodCall method_call(ibus::config::kServiceInterface, | 87 dbus::MethodCall method_call(ibus::config::kServiceInterface, |
57 ibus::config::kSetValueMethod); | 88 ibus::config::kSetValueMethod); |
58 dbus::MessageWriter writer(&method_call); | 89 dbus::MessageWriter writer(&method_call); |
59 writer.AppendString(section); | 90 writer.AppendString(section); |
60 writer.AppendString(key); | 91 writer.AppendString(key); |
61 dbus::MessageWriter variant_writer(NULL); | 92 dbus::MessageWriter variant_writer(NULL); |
62 writer.OpenVariant("i", &variant_writer); | 93 writer.OpenVariant("i", &variant_writer); |
63 variant_writer.AppendInt32(value); | 94 variant_writer.AppendInt32(value); |
64 writer.CloseContainer(&variant_writer); | 95 writer.CloseContainer(&variant_writer); |
65 CallWithDefaultCallback(&method_call, error_callback); | 96 CallWithDefaultCallback(&method_call, error_callback); |
66 } | 97 } |
67 | 98 |
68 // IBusConfigClient override. | 99 // IBusConfigClient override. |
69 virtual void SetBoolValue(const std::string& section, | 100 virtual void SetBoolValue(const std::string& section, |
70 const std::string& key, | 101 const std::string& key, |
71 bool value, | 102 bool value, |
72 const ErrorCallback& error_callback) OVERRIDE { | 103 const ErrorCallback& error_callback) OVERRIDE { |
104 if (!proxy_) | |
105 return; | |
73 DCHECK(!error_callback.is_null()); | 106 DCHECK(!error_callback.is_null()); |
74 dbus::MethodCall method_call(ibus::config::kServiceInterface, | 107 dbus::MethodCall method_call(ibus::config::kServiceInterface, |
75 ibus::config::kSetValueMethod); | 108 ibus::config::kSetValueMethod); |
76 dbus::MessageWriter writer(&method_call); | 109 dbus::MessageWriter writer(&method_call); |
77 writer.AppendString(section); | 110 writer.AppendString(section); |
78 writer.AppendString(key); | 111 writer.AppendString(key); |
79 dbus::MessageWriter variant_writer(NULL); | 112 dbus::MessageWriter variant_writer(NULL); |
80 writer.OpenVariant("b", &variant_writer); | 113 writer.OpenVariant("b", &variant_writer); |
81 variant_writer.AppendBool(value); | 114 variant_writer.AppendBool(value); |
82 writer.CloseContainer(&variant_writer); | 115 writer.CloseContainer(&variant_writer); |
83 CallWithDefaultCallback(&method_call, error_callback); | 116 CallWithDefaultCallback(&method_call, error_callback); |
84 } | 117 } |
85 | 118 |
86 // IBusConfigClient override. | 119 // IBusConfigClient override. |
87 virtual void SetStringListValue( | 120 virtual void SetStringListValue( |
88 const std::string& section, | 121 const std::string& section, |
89 const std::string& key, | 122 const std::string& key, |
90 const std::vector<std::string>& value, | 123 const std::vector<std::string>& value, |
91 const ErrorCallback& error_callback) OVERRIDE { | 124 const ErrorCallback& error_callback) OVERRIDE { |
125 if (!proxy_) | |
126 return; | |
92 DCHECK(!error_callback.is_null()); | 127 DCHECK(!error_callback.is_null()); |
93 dbus::MethodCall method_call(ibus::config::kServiceInterface, | 128 dbus::MethodCall method_call(ibus::config::kServiceInterface, |
94 ibus::config::kSetValueMethod); | 129 ibus::config::kSetValueMethod); |
95 dbus::MessageWriter writer(&method_call); | 130 dbus::MessageWriter writer(&method_call); |
96 writer.AppendString(section); | 131 writer.AppendString(section); |
97 writer.AppendString(key); | 132 writer.AppendString(key); |
98 dbus::MessageWriter variant_writer(NULL); | 133 dbus::MessageWriter variant_writer(NULL); |
99 dbus::MessageWriter array_writer(NULL); | 134 dbus::MessageWriter array_writer(NULL); |
100 | 135 |
101 writer.OpenVariant("as", &variant_writer); | 136 writer.OpenVariant("as", &variant_writer); |
102 variant_writer.OpenArray("s", &array_writer); | 137 variant_writer.OpenArray("s", &array_writer); |
103 for (size_t i = 0; i < value.size(); ++i) { | 138 for (size_t i = 0; i < value.size(); ++i) { |
104 array_writer.AppendString(value[i]); | 139 array_writer.AppendString(value[i]); |
105 } | 140 } |
106 variant_writer.CloseContainer(&array_writer); | 141 variant_writer.CloseContainer(&array_writer); |
107 writer.CloseContainer(&variant_writer); | 142 writer.CloseContainer(&variant_writer); |
108 CallWithDefaultCallback(&method_call, error_callback); | 143 CallWithDefaultCallback(&method_call, error_callback); |
109 } | 144 } |
110 | 145 |
111 private: | 146 private: |
112 void CallWithDefaultCallback(dbus::MethodCall* method_call, | 147 void CallWithDefaultCallback(dbus::MethodCall* method_call, |
113 const ErrorCallback& error_callback) { | 148 const ErrorCallback& error_callback) { |
149 if (!proxy_) | |
150 return; | |
114 proxy_->CallMethodWithErrorCallback( | 151 proxy_->CallMethodWithErrorCallback( |
115 method_call, | 152 method_call, |
116 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 153 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
117 base::Bind(&IBusConfigClientImpl::OnSetValue, | 154 base::Bind(&IBusConfigClientImpl::OnSetValue, |
118 weak_ptr_factory_.GetWeakPtr(), | 155 weak_ptr_factory_.GetWeakPtr(), |
119 error_callback), | 156 error_callback), |
120 base::Bind(&IBusConfigClientImpl::OnSetValueFail, | 157 base::Bind(&IBusConfigClientImpl::OnSetValueFail, |
121 weak_ptr_factory_.GetWeakPtr(), | 158 weak_ptr_factory_.GetWeakPtr(), |
122 error_callback)); | 159 error_callback)); |
123 } | 160 } |
124 | 161 |
125 void OnSetValue(const ErrorCallback& error_callback, | 162 void OnSetValue(const ErrorCallback& error_callback, |
126 dbus::Response* response) { | 163 dbus::Response* response) { |
127 if (!response) { | 164 if (!response) { |
128 LOG(ERROR) << "Response is NULL."; | 165 LOG(ERROR) << "Response is NULL."; |
129 error_callback.Run(); | 166 error_callback.Run(); |
130 return; | 167 return; |
131 } | 168 } |
132 } | 169 } |
133 | 170 |
134 void OnSetValueFail(const ErrorCallback& error_callback, | 171 void OnSetValueFail(const ErrorCallback& error_callback, |
135 dbus::ErrorResponse* response) { | 172 dbus::ErrorResponse* response) { |
136 error_callback.Run(); | 173 error_callback.Run(); |
137 } | 174 } |
138 | 175 |
176 void OnNameOwnerChanged(const OnIBusConfigReady& on_ready, | |
177 dbus::Signal* signal) { | |
178 DCHECK(signal); | |
179 std::string name; | |
180 std::string old_owner; | |
181 std::string new_owner; | |
182 | |
183 dbus::MessageReader reader(signal); | |
184 if (!reader.PopString(&name) || | |
185 !reader.PopString(&old_owner) || | |
186 !reader.PopString(&new_owner)) { | |
187 DLOG(ERROR) << "Invalid response of NameOwnerChanged."; | |
satorux1
2012/11/28 06:43:12
Include signal->ToString() ?
Seigo Nonaka
2012/11/28 07:00:37
Done.
| |
188 return; | |
189 } | |
190 | |
191 if (name != ibus::config::kServiceName) | |
192 return; // Not a signal for ibus-config. | |
193 | |
194 if (!old_owner.empty() || new_owner.empty()) { | |
195 DVLOG(1) << "Unexpected name owner change: name=" << name | |
196 << ", old_owner=" << old_owner << ", new_owner=" << new_owner; | |
197 proxy_ = NULL; | |
198 return; | |
199 } | |
200 | |
201 if (proxy_) | |
202 return; // Already initialized. | |
203 | |
204 proxy_ = bus_->GetObjectProxy(ibus::kServiceName, | |
205 dbus::ObjectPath( | |
206 ibus::config::kServicePath)); | |
207 if (!on_ready.is_null()) | |
208 on_ready.Run(); | |
209 } | |
210 | |
211 // Handles response of GetNameOwner. | |
212 void OnGetNameOwner(const OnIBusConfigReady& on_ready, | |
213 dbus::Response* response) { | |
214 if (!response) { | |
215 DLOG(ERROR) << "Response is NULL."; | |
216 return; | |
217 } | |
218 std::string owner; | |
219 dbus::MessageReader reader(response); | |
220 | |
221 if (!reader.PopString(&owner)) { | |
222 DLOG(ERROR) << "Invalid response of GetNameOwner."; | |
satorux1
2012/11/28 06:43:12
Include response->ToString() ?
Seigo Nonaka
2012/11/28 07:00:37
Done.
| |
223 return; | |
224 } | |
225 | |
226 // If the owner is empty, ibus-config daemon is not ready. So will | |
227 // initialize object proxy on NameOwnerChanged signal. | |
228 if (owner.empty()) | |
229 return; | |
230 | |
231 proxy_ = bus_->GetObjectProxy(ibus::kServiceName, | |
232 dbus::ObjectPath( | |
233 ibus::config::kServicePath)); | |
234 if (!on_ready.is_null()) | |
235 on_ready.Run(); | |
236 } | |
237 | |
139 dbus::ObjectProxy* proxy_; | 238 dbus::ObjectProxy* proxy_; |
239 dbus::Bus* bus_; | |
140 base::WeakPtrFactory<IBusConfigClientImpl> weak_ptr_factory_; | 240 base::WeakPtrFactory<IBusConfigClientImpl> weak_ptr_factory_; |
141 | 241 |
142 DISALLOW_COPY_AND_ASSIGN(IBusConfigClientImpl); | 242 DISALLOW_COPY_AND_ASSIGN(IBusConfigClientImpl); |
143 }; | 243 }; |
144 | 244 |
145 // A stub implementation of IBusConfigClient. | 245 // A stub implementation of IBusConfigClient. |
146 class IBusConfigClientStubImpl : public IBusConfigClient { | 246 class IBusConfigClientStubImpl : public IBusConfigClient { |
147 public: | 247 public: |
148 IBusConfigClientStubImpl() {} | 248 IBusConfigClientStubImpl() {} |
149 virtual ~IBusConfigClientStubImpl() {} | 249 virtual ~IBusConfigClientStubImpl() {} |
250 virtual void AsyncInitialize(const OnIBusConfigReady& on_ready) OVERRIDE {} | |
150 virtual void SetStringValue(const std::string& section, | 251 virtual void SetStringValue(const std::string& section, |
151 const std::string& key, | 252 const std::string& key, |
152 const std::string& value, | 253 const std::string& value, |
153 const ErrorCallback& error_callback) OVERRIDE {} | 254 const ErrorCallback& error_callback) OVERRIDE {} |
154 virtual void SetIntValue(const std::string& section, | 255 virtual void SetIntValue(const std::string& section, |
155 const std::string& key, | 256 const std::string& key, |
156 int value, | 257 int value, |
157 const ErrorCallback& error_callback) OVERRIDE {} | 258 const ErrorCallback& error_callback) OVERRIDE {} |
158 virtual void SetBoolValue(const std::string& section, | 259 virtual void SetBoolValue(const std::string& section, |
159 const std::string& key, | 260 const std::string& key, |
(...skipping 22 matching lines...) Expand all Loading... | |
182 IBusConfigClient* IBusConfigClient::Create(DBusClientImplementationType type, | 283 IBusConfigClient* IBusConfigClient::Create(DBusClientImplementationType type, |
183 dbus::Bus* bus) { | 284 dbus::Bus* bus) { |
184 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) { | 285 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) { |
185 return new IBusConfigClientImpl(bus); | 286 return new IBusConfigClientImpl(bus); |
186 } | 287 } |
187 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | 288 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); |
188 return new IBusConfigClientStubImpl(); | 289 return new IBusConfigClientStubImpl(); |
189 } | 290 } |
190 | 291 |
191 } // namespace chromeos | 292 } // namespace chromeos |
OLD | NEW |