forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnative_ime.cpp
More file actions
246 lines (201 loc) · 6.9 KB
/
Copy pathnative_ime.cpp
File metadata and controls
246 lines (201 loc) · 6.9 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include "build_environment.h"
#ifndef GECKO_19_COMPATIBILITY
#ifndef BUILD_ON_UNIX
#define MOZ_NO_MOZALLOC
#include <mozilla-config.h>
#endif
#include <xpcom-config.h>
#undef HAVE_CPP_CHAR16_T
#else // Gecko 1.9
#ifdef BUILD_ON_UNIX
#include <xpcom-config.h>
#endif
#endif
#include "errorcodes.h"
#include "interactions.h"
#include "logging.h"
#include "native_ime.h"
#include "library_loading.h"
#ifndef GECKO_19_COMPATIBILITY
#include "mozilla/ModuleUtils.h"
#else
#include "nsIGenericFactory.h"
#endif
#include "nsIComponentManager.h"
#include "nsComponentManagerUtils.h"
#include <assert.h>
#include "nsISupportsPrimitives.h"
#ifdef BUILD_ON_WINDOWS
#define WD_RESULT LRESULT
#else
#define WD_RESULT int
#endif
/* IME */
#include <string>
#include "imehandler.h"
#include "nsISupportsPrimitives.h"
#include "nsArrayUtils.h"
#include <nsStringAPI.h>
#include <algorithm>
#ifdef WEBDRIVER_GECKO_USES_ISUPPORTS1
NS_IMPL_ISUPPORTS1(nsNativeIME, nsINativeIME)
#else
NS_IMPL_ISUPPORTS(nsNativeIME, nsINativeIME)
#endif
nsNativeIME::nsNativeIME()
{
LOG(DEBUG) << "Native IME instantiated.";
}
nsNativeIME::~nsNativeIME()
{
}
/* void imeIsActivated (out boolean isActive); */
NS_IMETHODIMP nsNativeIME::ImeIsActivated(bool *isActive)
{
LOG(DEBUG) << "Getting if IME is active or not";
IMELIB_TYPE lib = tryToOpenImeLib();
if (lib)
{
create_h* create_handler = getCreateHandler(lib);
ImeHandler* handler = create_handler();
*isActive = handler->IsActivated();
tryToCloseImeLib(handler, lib);
LOG(DEBUG) << "All done. value: " << *isActive;
return NS_OK;
}
return NS_ERROR_FAILURE;
}
/* void imeGetActiveEngine (out string activeEngine); */
NS_IMETHODIMP nsNativeIME::ImeGetActiveEngine(nsAString &activeEngine)
{
LOG(DEBUG) << "Getting active engine";
IMELIB_TYPE lib = tryToOpenImeLib();
if (lib)
{
create_h* create_handler = getCreateHandler(lib);
ImeHandler* handler = create_handler();
std::string engine = handler->GetActiveEngine();
LOG(DEBUG) << "Active engine:" << engine;
std::wstring wengine(engine.begin(), engine.end());
// We know that PRUnichar* is wchar_t*. see comment in sendKeys
#ifdef WEBDRIVER_LEGACY_GECKO
activeEngine.Assign((const PRUnichar*) wengine.c_str(), wengine.length());
#else
activeEngine.Assign((const nsAString::char_type*) wengine.c_str(), wengine.length());
#endif // WEBDRIVER_LEGACY_GECKO
tryToCloseImeLib(handler, lib);
return NS_OK;
}
return NS_ERROR_FAILURE;
}
/* void imeDeactivate (); */
NS_IMETHODIMP nsNativeIME::ImeDeactivate()
{
LOG(DEBUG) << "Deactivating IME";
IMELIB_TYPE lib = tryToOpenImeLib();
if (lib)
{
create_h* create_handler = getCreateHandler(lib);
ImeHandler* handler = create_handler();
handler->Deactivate();
tryToCloseImeLib(handler, lib);
return NS_OK;
}
return NS_ERROR_FAILURE;
}
/* void imeActivateEngine (in string engine, out boolean activationSucceeded); */
NS_IMETHODIMP nsNativeIME::ImeActivateEngine(const char *engine, bool *activationSucceeded)
{
LOG(DEBUG) << "Activating IME engine " << engine;
IMELIB_TYPE lib = tryToOpenImeLib();
if (lib == NULL) {
return NS_ERROR_FAILURE;
}
create_h* create_handler = getCreateHandler(lib);
ImeHandler* handler = create_handler();
// 1. Make sure the requested engine is in the list of installed engines.
std::string engine_name(engine);
std::vector<std::string> engines = handler->GetAvailableEngines();
if (std::find(engines.begin(), engines.end(), engine_name) == engines.end()) {
// Not found
LOG(DEBUG) << "Engine not installed.";
*activationSucceeded = false;
tryToCloseImeLib(handler, lib);
return NS_OK;
}
// 2. If the engine is available but not loaded, load it.
std::vector<std::string> loaded_engines = handler->GetInstalledEngines();
if (std::find(loaded_engines.begin(), loaded_engines.end(), engine_name) ==
loaded_engines.end()) {
LOG(DEBUG) << "Engine not loaded, loading.";
// Append the engine to the list of loaded engines - not to override
// the engines already in use by the user.
int currently_loaded = loaded_engines.size();
loaded_engines.push_back(engine_name);
int newly_loaded = handler->LoadEngines(loaded_engines);
LOG(DEBUG) << "Number of engines loaded:" << newly_loaded;
// Make sure that the engine was loaded by comparing the number of engines
// now loaded to the number of engines loaded before the LoadEngine call.
if (currently_loaded + 1 != newly_loaded) {
LOG(DEBUG) << "Engine is installed but could not be loaded.";
*activationSucceeded = false;
tryToCloseImeLib(handler, lib);
return NS_OK;
}
// Wait for ibus to register the engine. Without the sleep statement here,
// the call to ActivateEngine will fail. This is only needed on Linux.
#ifdef BUILD_ON_UNIX
sleep(1);
#endif
} else {
LOG(DEBUG) << "Engine already loaded, not calling LoadEngines again.";
}
// 3. Finally, call ActivateEngine to immediately make this engine active.
*activationSucceeded = handler->ActivateEngine(engine);
LOG(DEBUG) << "Activation result: " << *activationSucceeded << " isActive: "
<< handler->IsActivated();
tryToCloseImeLib(handler, lib);
return NS_OK;
}
/*
* Fill a scriptable array with the values from a vector of strings.
* The scriptable array can only contain scriptable elements so a
* nsISupportsCString is created for each std::string in the vector.
*/
static void fillIMutableArrayFromVector(std::vector<std::string> &engines,
nsCOMPtr<nsIMutableArray> &outArray)
{
for(std::vector<std::string>::const_iterator it = engines.begin() ; it != engines.end() ; it++)
{
LOG(DEBUG) << "Outputting engine : " << *it;
std::string engine(it->begin(), it->end());
nsCString new_data;
new_data.Assign(engine.c_str(), engine.length());
nsCOMPtr<nsISupportsCString> curr_engine = do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID);
curr_engine->SetData(new_data);
outArray->AppendElement(curr_engine, false /* STRONG reference. */);
}
}
/* void imeGetAvailableEngines (out AString enginesList); */
NS_IMETHODIMP nsNativeIME::ImeGetAvailableEngines(nsIArray **enginesList)
{
NS_ENSURE_ARG_POINTER(enginesList);
*enginesList = NULL;
LOG(DEBUG) << "getting available engines";
IMELIB_TYPE lib = tryToOpenImeLib();
if (lib)
{
create_h* create_handler = getCreateHandler(lib);
ImeHandler* handler = create_handler();
std::vector<std::string> engines = handler->GetAvailableEngines();
LOG(DEBUG) << "Number of engines received: " << engines.size();
nsCOMPtr<nsIMutableArray> returnArray = do_CreateInstance(NS_ARRAY_CONTRACTID);
NS_ENSURE_TRUE(returnArray, NS_ERROR_FAILURE);
fillIMutableArrayFromVector(engines, returnArray);
NS_ADDREF(*enginesList = returnArray);
tryToCloseImeLib(handler, lib);
LOG(DEBUG) << "Done getAvailableEngines.";
return NS_OK;
}
return NS_ERROR_FAILURE;
}