1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/dbus/ibus/ibus_engine_factory_service.h"
#include <map>
#include <string>
#include "base/string_number_conversions.h"
#include "chromeos/dbus/ibus/ibus_constants.h"
#include "dbus/bus.h"
#include "dbus/message.h"
#include "dbus/exported_object.h"
namespace chromeos {
class IBusEngineFactoryServiceImpl : public IBusEngineFactoryService {
public:
explicit IBusEngineFactoryServiceImpl(dbus::Bus* bus)
: bus_(bus),
weak_ptr_factory_(this) {
exported_object_ = bus_->GetExportedObject(
dbus::ObjectPath(ibus::engine_factory::kServicePath));
exported_object_->ExportMethod(
ibus::engine_factory::kServiceInterface,
ibus::engine_factory::kCreateEngineMethod,
base::Bind(&IBusEngineFactoryServiceImpl::CreateEngine,
weak_ptr_factory_.GetWeakPtr()),
base::Bind(&IBusEngineFactoryServiceImpl::CreateEngineExported,
weak_ptr_factory_.GetWeakPtr()));
}
virtual ~IBusEngineFactoryServiceImpl() {
bus_->UnregisterExportedObject(dbus::ObjectPath(
ibus::engine_factory::kServicePath));
}
// IBusEngineFactoryService override.
virtual void SetCreateEngineHandler(
const std::string& engine_id,
const CreateEngineHandler& create_engine_handler) OVERRIDE {
DCHECK(create_engine_callback_map_[engine_id].is_null());
create_engine_callback_map_[engine_id] = create_engine_handler;
}
// IBusEngineFactoryService override.
virtual void UnsetCreateEngineHandler(
const std::string& engine_id) OVERRIDE {
create_engine_callback_map_[engine_id].Reset();
}
private:
// Called when the ibus-daemon requires new engine instance.
void CreateEngine(dbus::MethodCall* method_call,
dbus::ExportedObject::ResponseSender response_sender) {
if (!method_call) {
LOG(ERROR) << "method call does not have any arguments.";
return;
}
dbus::MessageReader reader(method_call);
std::string engine_name;
if (!reader.PopString(&engine_name)) {
LOG(ERROR) << "Expected argument is string.";
return;
}
if (create_engine_callback_map_[engine_name].is_null()) {
LOG(WARNING) << "The CreateEngine handler for " << engine_name
<< " is NULL.";
} else {
create_engine_callback_map_[engine_name].Run(
base::Bind(&IBusEngineFactoryServiceImpl::CreateEngineSendReply,
weak_ptr_factory_.GetWeakPtr(),
dbus::Response::FromMethodCall(method_call),
response_sender));
}
}
// Sends reply message for CreateEngine method call.
void CreateEngineSendReply(
dbus::Response* response,
const dbus::ExportedObject::ResponseSender response_sender,
const dbus::ObjectPath& path) {
dbus::MessageWriter writer(response);
writer.AppendObjectPath(path);
response_sender.Run(response);
}
// Called when the CreateEngine method is exported.
void CreateEngineExported(const std::string& interface_name,
const std::string& method_name,
bool success) {
DCHECK(success) << "Failed to export: "
<< interface_name << "." << method_name;
}
// CreateEngine method call handler.
std::map<std::string, CreateEngineHandler> create_engine_callback_map_;
dbus::Bus* bus_;
scoped_refptr<dbus::ExportedObject> exported_object_;
base::WeakPtrFactory<IBusEngineFactoryServiceImpl> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(IBusEngineFactoryServiceImpl);
};
// An implementation of IBusEngineFactoryService without ibus-daemon
// interaction. Currently this class is used only on linux desktop.
// TODO(nona): Use this on ChromeOS device once crbug.com/171351 is fixed.
class IBusEngineFactoryServiceDaemonlessImpl : public IBusEngineFactoryService {
public:
IBusEngineFactoryServiceDaemonlessImpl() {}
virtual ~IBusEngineFactoryServiceDaemonlessImpl() {}
// IBusEngineFactoryService override.
virtual void SetCreateEngineHandler(
const std::string& engine_id,
const CreateEngineHandler& create_engine_handler) OVERRIDE {
// TODO(nona): Implement this.
}
// IBusEngineFactoryService override.
virtual void UnsetCreateEngineHandler(
const std::string& engine_id) OVERRIDE {
// TODO(nona): Implement this.
}
private:
DISALLOW_COPY_AND_ASSIGN(IBusEngineFactoryServiceDaemonlessImpl);
};
IBusEngineFactoryService::IBusEngineFactoryService() {
}
IBusEngineFactoryService::~IBusEngineFactoryService() {
}
// static
IBusEngineFactoryService* IBusEngineFactoryService::Create(
dbus::Bus* bus,
DBusClientImplementationType type) {
if (type == REAL_DBUS_CLIENT_IMPLEMENTATION)
return new IBusEngineFactoryServiceImpl(bus);
else
return new IBusEngineFactoryServiceDaemonlessImpl();
}
} // namespace chromeos
|