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