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_client.h" | 5 #include "chromeos/dbus/ibus/ibus_client.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "chromeos/dbus/ibus/ibus_constants.h" | 9 #include "chromeos/dbus/ibus/ibus_constants.h" |
| 10 #include "chromeos/dbus/ibus/ibus_component.h" |
10 #include "dbus/bus.h" | 11 #include "dbus/bus.h" |
11 #include "dbus/message.h" | 12 #include "dbus/message.h" |
12 #include "dbus/object_path.h" | 13 #include "dbus/object_path.h" |
13 #include "dbus/object_proxy.h" | 14 #include "dbus/object_proxy.h" |
14 | 15 |
15 namespace chromeos { | 16 namespace chromeos { |
16 | 17 |
17 namespace { | 18 namespace { |
| 19 const char kIBusBusRegisterComponentMethod[] = "RegisterComponent"; |
18 | 20 |
19 // The IBusClient implementation. | 21 // The IBusClient implementation. |
20 class IBusClientImpl : public IBusClient { | 22 class IBusClientImpl : public IBusClient { |
21 public: | 23 public: |
22 explicit IBusClientImpl(dbus::Bus* bus) | 24 explicit IBusClientImpl(dbus::Bus* bus) |
23 : proxy_(bus->GetObjectProxy(kIBusServiceName, | 25 : proxy_(bus->GetObjectProxy(kIBusServiceName, |
24 dbus::ObjectPath(kIBusServicePath))), | 26 dbus::ObjectPath(kIBusServicePath))), |
25 weak_ptr_factory_(this) { | 27 weak_ptr_factory_(this) { |
26 } | 28 } |
27 | 29 |
(...skipping 15 matching lines...) Expand all Loading... |
43 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 45 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
44 base::Bind(&IBusClientImpl::OnCreateInputContext, | 46 base::Bind(&IBusClientImpl::OnCreateInputContext, |
45 weak_ptr_factory_.GetWeakPtr(), | 47 weak_ptr_factory_.GetWeakPtr(), |
46 callback, | 48 callback, |
47 error_callback), | 49 error_callback), |
48 base::Bind(&IBusClientImpl::OnCreateInputContextFail, | 50 base::Bind(&IBusClientImpl::OnCreateInputContextFail, |
49 weak_ptr_factory_.GetWeakPtr(), | 51 weak_ptr_factory_.GetWeakPtr(), |
50 error_callback)); | 52 error_callback)); |
51 } | 53 } |
52 | 54 |
| 55 // IBusClient override. |
| 56 virtual void RegisterComponent( |
| 57 const ibus::IBusComponent& ibus_component, |
| 58 const RegisterComponentCallback& callback, |
| 59 const ErrorCallback& error_callback) OVERRIDE { |
| 60 DCHECK(!callback.is_null()); |
| 61 DCHECK(!error_callback.is_null()); |
| 62 dbus::MethodCall method_call(kIBusServiceInterface, |
| 63 kIBusBusRegisterComponentMethod); |
| 64 dbus::MessageWriter writer(&method_call); |
| 65 ibus::AppendIBusComponent(ibus_component, &writer); |
| 66 proxy_->CallMethodWithErrorCallback( |
| 67 &method_call, |
| 68 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 69 base::Bind(&IBusClientImpl::OnRegisterComponent, |
| 70 weak_ptr_factory_.GetWeakPtr(), |
| 71 callback, |
| 72 error_callback), |
| 73 base::Bind(&IBusClientImpl::OnRegisterComponentFail, |
| 74 weak_ptr_factory_.GetWeakPtr(), |
| 75 error_callback)); |
| 76 } |
| 77 |
53 private: | 78 private: |
54 // Handles responses of CreateInputContext method calls. | 79 // Handles responses of CreateInputContext method calls. |
55 void OnCreateInputContext(const CreateInputContextCallback& callback, | 80 void OnCreateInputContext(const CreateInputContextCallback& callback, |
56 const ErrorCallback& error_callback, | 81 const ErrorCallback& error_callback, |
57 dbus::Response* response) { | 82 dbus::Response* response) { |
58 if (!response) { | 83 if (!response) { |
59 LOG(ERROR) << "Cannot get input context: response is NULL."; | 84 LOG(ERROR) << "Cannot get input context: response is NULL."; |
60 error_callback.Run(); | 85 error_callback.Run(); |
61 return; | 86 return; |
62 } | 87 } |
63 dbus::MessageReader reader(response); | 88 dbus::MessageReader reader(response); |
64 dbus::ObjectPath object_path; | 89 dbus::ObjectPath object_path; |
65 if (!reader.PopObjectPath(&object_path)) { | 90 if (!reader.PopObjectPath(&object_path)) { |
66 // The IBus message structure may be changed. | 91 // The IBus message structure may be changed. |
67 LOG(ERROR) << "Invalid response: " << response->ToString(); | 92 LOG(ERROR) << "Invalid response: " << response->ToString(); |
68 error_callback.Run(); | 93 error_callback.Run(); |
69 return; | 94 return; |
70 } | 95 } |
71 callback.Run(object_path); | 96 callback.Run(object_path); |
72 } | 97 } |
73 | 98 |
74 // Handles error response of CreateInputContext method calls. | 99 // Handles error response of CreateInputContext method calls. |
75 void OnCreateInputContextFail(const ErrorCallback& error_callback, | 100 void OnCreateInputContextFail(const ErrorCallback& error_callback, |
76 dbus::ErrorResponse* response) { | 101 dbus::ErrorResponse* response) { |
77 error_callback.Run(); | 102 error_callback.Run(); |
78 } | 103 } |
79 | 104 |
| 105 // Handles responses of RegisterComponent method calls. |
| 106 void OnRegisterComponent(const RegisterComponentCallback& callback, |
| 107 const ErrorCallback& error_callback, |
| 108 dbus::Response* response) { |
| 109 if (!response) { |
| 110 LOG(ERROR) << "Response is NULL."; |
| 111 error_callback.Run(); |
| 112 return; |
| 113 } |
| 114 callback.Run(); |
| 115 } |
| 116 |
| 117 // Handles error response of RegisterComponent method call. |
| 118 void OnRegisterComponentFail(const ErrorCallback& error_callback, |
| 119 dbus::ErrorResponse* response) { |
| 120 error_callback.Run(); |
| 121 } |
| 122 |
80 dbus::ObjectProxy* proxy_; | 123 dbus::ObjectProxy* proxy_; |
81 base::WeakPtrFactory<IBusClientImpl> weak_ptr_factory_; | 124 base::WeakPtrFactory<IBusClientImpl> weak_ptr_factory_; |
82 | 125 |
83 DISALLOW_COPY_AND_ASSIGN(IBusClientImpl); | 126 DISALLOW_COPY_AND_ASSIGN(IBusClientImpl); |
84 }; | 127 }; |
85 | 128 |
86 // A stub implementation of IBusClient. | 129 // A stub implementation of IBusClient. |
87 class IBusClientStubImpl : public IBusClient { | 130 class IBusClientStubImpl : public IBusClient { |
88 public: | 131 public: |
89 IBusClientStubImpl() {} | 132 IBusClientStubImpl() {} |
90 virtual ~IBusClientStubImpl() {} | 133 virtual ~IBusClientStubImpl() {} |
91 | 134 |
92 virtual void CreateInputContext( | 135 virtual void CreateInputContext( |
93 const std::string& client_name, | 136 const std::string& client_name, |
94 const CreateInputContextCallback & callback, | 137 const CreateInputContextCallback & callback, |
95 const ErrorCallback& error_callback) OVERRIDE {} | 138 const ErrorCallback& error_callback) OVERRIDE {} |
96 | 139 |
| 140 virtual void RegisterComponent( |
| 141 const ibus::IBusComponent& ibus_component, |
| 142 const RegisterComponentCallback& callback, |
| 143 const ErrorCallback& error_callback) OVERRIDE {} |
| 144 |
97 private: | 145 private: |
98 DISALLOW_COPY_AND_ASSIGN(IBusClientStubImpl); | 146 DISALLOW_COPY_AND_ASSIGN(IBusClientStubImpl); |
99 }; | 147 }; |
100 | 148 |
101 } // namespace | 149 } // namespace |
102 | 150 |
103 /////////////////////////////////////////////////////////////////////////////// | 151 /////////////////////////////////////////////////////////////////////////////// |
104 // IBusClient | 152 // IBusClient |
105 | 153 |
106 IBusClient::IBusClient() {} | 154 IBusClient::IBusClient() {} |
107 | 155 |
108 IBusClient::~IBusClient() {} | 156 IBusClient::~IBusClient() {} |
109 | 157 |
110 // static | 158 // static |
111 IBusClient* IBusClient::Create(DBusClientImplementationType type, | 159 IBusClient* IBusClient::Create(DBusClientImplementationType type, |
112 dbus::Bus* bus) { | 160 dbus::Bus* bus) { |
113 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) { | 161 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) { |
114 return new IBusClientImpl(bus); | 162 return new IBusClientImpl(bus); |
115 } | 163 } |
116 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | 164 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); |
117 return new IBusClientStubImpl(); | 165 return new IBusClientStubImpl(); |
118 } | 166 } |
119 | 167 |
120 } // namespace chromeos | 168 } // namespace chromeos |
OLD | NEW |