OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "webkit/plugins/npapi/webplugin_ime_win.h" | |
6 | |
7 #include <cstring> | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/lazy_instance.h" | |
12 #include "base/logging.h" | |
13 #include "base/strings/utf_string_conversions.h" | |
14 #include "base/synchronization/lock.h" | |
15 #include "webkit/plugins/npapi/plugin_constants_win.h" | |
16 #include "webkit/plugins/npapi/plugin_instance.h" | |
17 | |
18 #pragma comment(lib, "imm32.lib") | |
19 | |
20 namespace webkit { | |
21 namespace npapi { | |
22 | |
23 // A critical section that prevents two or more plug-ins from accessing a | |
24 // WebPluginIMEWin instance through our patch function. | |
25 base::LazyInstance<base::Lock>::Leaky | |
26 g_webplugin_ime_lock = LAZY_INSTANCE_INITIALIZER; | |
27 | |
28 WebPluginIMEWin* WebPluginIMEWin::instance_ = NULL; | |
29 | |
30 WebPluginIMEWin::WebPluginIMEWin() | |
31 : cursor_position_(0), | |
32 delta_start_(0), | |
33 composing_text_(false), | |
34 support_ime_messages_(false), | |
35 status_updated_(false), | |
36 input_type_(1) { | |
37 memset(result_clauses_, 0, sizeof(result_clauses_)); | |
38 } | |
39 | |
40 WebPluginIMEWin::~WebPluginIMEWin() { | |
41 } | |
42 | |
43 void WebPluginIMEWin::CompositionUpdated(const base::string16& text, | |
44 std::vector<int> clauses, | |
45 std::vector<int> target, | |
46 int cursor_position) { | |
47 // Send a WM_IME_STARTCOMPOSITION message when a user starts a composition. | |
48 NPEvent np_event; | |
49 if (!composing_text_) { | |
50 composing_text_ = true; | |
51 result_text_.clear(); | |
52 | |
53 np_event.event = WM_IME_STARTCOMPOSITION; | |
54 np_event.wParam = 0; | |
55 np_event.lParam = 0; | |
56 events_.push_back(np_event); | |
57 } | |
58 | |
59 // We can update the following values from this event: GCS_COMPSTR, | |
60 // GCS_COMPATTR, GCSCOMPCLAUSE, GCS_CURSORPOS, and GCS_DELTASTART. We send a | |
61 // WM_IME_COMPOSITION message to notify the list of updated values. | |
62 np_event.event = WM_IME_COMPOSITION; | |
63 np_event.wParam = 0; | |
64 np_event.lParam = GCS_COMPSTR | GCS_COMPATTR | GCS_COMPCLAUSE | | |
65 GCS_CURSORPOS | GCS_DELTASTART; | |
66 events_.push_back(np_event); | |
67 | |
68 // Converts this event to the IMM32 data so we do not have to convert it every | |
69 // time when a plug-in call an IMM32 function. | |
70 composition_text_ = text; | |
71 | |
72 // Create the composition clauses returned when a plug-in calls | |
73 // ImmGetCompositionString() with GCS_COMPCLAUSE. | |
74 composition_clauses_.clear(); | |
75 for (size_t i = 0; i < clauses.size(); ++i) | |
76 composition_clauses_.push_back(clauses[i]); | |
77 | |
78 // Create the composition attributes used by GCS_COMPATTR. | |
79 if (target.size() == 2) { | |
80 composition_attributes_.assign(text.length(), ATTR_CONVERTED); | |
81 for (int i = target[0]; i < target[1]; ++i) | |
82 composition_attributes_[i] = ATTR_TARGET_CONVERTED; | |
83 } else { | |
84 composition_attributes_.assign(text.length(), ATTR_INPUT); | |
85 } | |
86 | |
87 cursor_position_ = cursor_position; | |
88 delta_start_ = cursor_position; | |
89 } | |
90 | |
91 void WebPluginIMEWin::CompositionCompleted(const base::string16& text) { | |
92 composing_text_ = false; | |
93 | |
94 // We should update the following values when we finish a composition: | |
95 // GCS_RESULTSTR, GCS_RESULTCLAUSE, GCS_CURSORPOS, and GCS_DELTASTART. We | |
96 // send a WM_IME_COMPOSITION message to notify the list of updated values. | |
97 NPEvent np_event; | |
98 np_event.event = WM_IME_COMPOSITION; | |
99 np_event.wParam = 0; | |
100 np_event.lParam = GCS_CURSORPOS | GCS_DELTASTART | GCS_RESULTSTR | | |
101 GCS_RESULTCLAUSE; | |
102 events_.push_back(np_event); | |
103 | |
104 // We also send a WM_IME_ENDCOMPOSITION message after the final | |
105 // WM_IME_COMPOSITION message (i.e. after finishing a composition). | |
106 np_event.event = WM_IME_ENDCOMPOSITION; | |
107 np_event.wParam = 0; | |
108 np_event.lParam = 0; | |
109 events_.push_back(np_event); | |
110 | |
111 // If the target plug-in does not seem to support IME messages, we send | |
112 // each character in IME text with a WM_CHAR message so the plug-in can | |
113 // insert the IME text. | |
114 if (!support_ime_messages_) { | |
115 np_event.event = WM_CHAR; | |
116 np_event.wParam = 0; | |
117 np_event.lParam = 0; | |
118 for (size_t i = 0; i < result_text_.length(); ++i) { | |
119 np_event.wParam = result_text_[i]; | |
120 events_.push_back(np_event); | |
121 } | |
122 } | |
123 | |
124 // Updated the result text and its clause. (Unlike composition clauses, a | |
125 // result clause consists of only one region.) | |
126 result_text_ = text; | |
127 | |
128 result_clauses_[0] = 0; | |
129 result_clauses_[1] = result_text_.length(); | |
130 | |
131 cursor_position_ = result_clauses_[1]; | |
132 delta_start_ = result_clauses_[1]; | |
133 } | |
134 | |
135 bool WebPluginIMEWin::SendEvents(PluginInstance* instance) { | |
136 // We allow the patch functions to access this WebPluginIMEWin instance only | |
137 // while we send IME events to the plug-in. | |
138 ScopedLock lock(this); | |
139 | |
140 bool ret = true; | |
141 for (std::vector<NPEvent>::iterator it = events_.begin(); | |
142 it != events_.end(); ++it) { | |
143 if (!instance->NPP_HandleEvent(&(*it))) | |
144 ret = false; | |
145 } | |
146 | |
147 events_.clear(); | |
148 return ret; | |
149 } | |
150 | |
151 bool WebPluginIMEWin::GetStatus(int* input_type, gfx::Rect* caret_rect) { | |
152 *input_type = input_type_; | |
153 *caret_rect = caret_rect_; | |
154 return true; | |
155 } | |
156 | |
157 // static | |
158 FARPROC WebPluginIMEWin::GetProcAddress(LPCSTR name) { | |
159 static const struct { | |
160 const char* name; | |
161 FARPROC function; | |
162 } kImm32Functions[] = { | |
163 { "ImmAssociateContextEx", | |
164 reinterpret_cast<FARPROC>(ImmAssociateContextEx) }, | |
165 { "ImmGetCompositionStringW", | |
166 reinterpret_cast<FARPROC>(ImmGetCompositionStringW) }, | |
167 { "ImmGetContext", reinterpret_cast<FARPROC>(ImmGetContext) }, | |
168 { "ImmReleaseContext", reinterpret_cast<FARPROC>(ImmReleaseContext) }, | |
169 { "ImmSetCandidateWindow", | |
170 reinterpret_cast<FARPROC>(ImmSetCandidateWindow) }, | |
171 { "ImmSetOpenStatus", reinterpret_cast<FARPROC>(ImmSetOpenStatus) }, | |
172 }; | |
173 for (int i = 0; i < arraysize(kImm32Functions); ++i) { | |
174 if (!lstrcmpiA(name, kImm32Functions[i].name)) | |
175 return kImm32Functions[i].function; | |
176 } | |
177 return NULL; | |
178 } | |
179 | |
180 void WebPluginIMEWin::Lock() { | |
181 g_webplugin_ime_lock.Pointer()->Acquire(); | |
182 instance_ = this; | |
183 } | |
184 | |
185 void WebPluginIMEWin::Unlock() { | |
186 instance_ = NULL; | |
187 g_webplugin_ime_lock.Pointer()->Release(); | |
188 } | |
189 | |
190 // static | |
191 WebPluginIMEWin* WebPluginIMEWin::GetInstance(HIMC context) { | |
192 return instance_ && context == reinterpret_cast<HIMC>(instance_) ? | |
193 instance_ : NULL; | |
194 } | |
195 | |
196 // static | |
197 BOOL WINAPI WebPluginIMEWin::ImmAssociateContextEx(HWND window, | |
198 HIMC context, | |
199 DWORD flags) { | |
200 WebPluginIMEWin* instance = GetInstance(context); | |
201 if (!instance) | |
202 return ::ImmAssociateContextEx(window, context, flags); | |
203 | |
204 int input_type = !context && !flags; | |
205 instance->input_type_ = input_type; | |
206 instance->status_updated_ = true; | |
207 return TRUE; | |
208 } | |
209 | |
210 // static | |
211 LONG WINAPI WebPluginIMEWin::ImmGetCompositionStringW(HIMC context, | |
212 DWORD index, | |
213 LPVOID dst_data, | |
214 DWORD dst_size) { | |
215 WebPluginIMEWin* instance = GetInstance(context); | |
216 if (!instance) | |
217 return ::ImmGetCompositionStringW(context, index, dst_data, dst_size); | |
218 | |
219 const void* src_data = NULL; | |
220 DWORD src_size = 0; | |
221 switch (index) { | |
222 case GCS_COMPSTR: | |
223 src_data = instance->composition_text_.c_str(); | |
224 src_size = instance->composition_text_.length() * sizeof(wchar_t); | |
225 break; | |
226 | |
227 case GCS_COMPATTR: | |
228 src_data = instance->composition_attributes_.c_str(); | |
229 src_size = instance->composition_attributes_.length(); | |
230 break; | |
231 | |
232 case GCS_COMPCLAUSE: | |
233 src_data = &instance->composition_clauses_[0]; | |
234 src_size = instance->composition_clauses_.size() * sizeof(uint32); | |
235 break; | |
236 | |
237 case GCS_CURSORPOS: | |
238 return instance->cursor_position_; | |
239 | |
240 case GCS_DELTASTART: | |
241 return instance->delta_start_; | |
242 | |
243 case GCS_RESULTSTR: | |
244 src_data = instance->result_text_.c_str(); | |
245 src_size = instance->result_text_.length() * sizeof(wchar_t); | |
246 break; | |
247 | |
248 case GCS_RESULTCLAUSE: | |
249 src_data = &instance->result_clauses_[0]; | |
250 src_size = sizeof(instance->result_clauses_); | |
251 break; | |
252 | |
253 default: | |
254 break; | |
255 } | |
256 if (!src_data || !src_size) | |
257 return IMM_ERROR_NODATA; | |
258 | |
259 if (dst_size >= src_size) | |
260 memcpy(dst_data, src_data, src_size); | |
261 | |
262 return src_size; | |
263 } | |
264 | |
265 // static | |
266 HIMC WINAPI WebPluginIMEWin::ImmGetContext(HWND window) { | |
267 // Call the original ImmGetContext() function if the given window is the one | |
268 // created in WebPluginDelegateImpl::WindowedCreatePlugin(). (We attached IME | |
269 // context only with the windows created in this function.) On the other hand, | |
270 // some windowless plug-ins (such as Flash) call this function with a dummy | |
271 // window handle. We return our dummy IME context for these plug-ins so they | |
272 // can use our IME emulator. | |
273 if (IsWindow(window)) { | |
274 wchar_t name[128]; | |
275 GetClassName(window, &name[0], arraysize(name)); | |
276 if (!wcscmp(&name[0], kNativeWindowClassName)) | |
277 return ::ImmGetContext(window); | |
278 } | |
279 | |
280 WebPluginIMEWin* instance = instance_; | |
281 if (instance) | |
282 instance->support_ime_messages_ = true; | |
283 return reinterpret_cast<HIMC>(instance); | |
284 } | |
285 | |
286 // static | |
287 BOOL WINAPI WebPluginIMEWin::ImmReleaseContext(HWND window, HIMC context) { | |
288 if (!GetInstance(context)) | |
289 return ::ImmReleaseContext(window, context); | |
290 return TRUE; | |
291 } | |
292 | |
293 // static | |
294 BOOL WINAPI WebPluginIMEWin::ImmSetCandidateWindow(HIMC context, | |
295 CANDIDATEFORM* candidate) { | |
296 WebPluginIMEWin* instance = GetInstance(context); | |
297 if (!instance) | |
298 return ::ImmSetCandidateWindow(context, candidate); | |
299 | |
300 gfx::Rect caret_rect(candidate->rcArea); | |
301 if ((candidate->dwStyle & CFS_EXCLUDE) && | |
302 instance->caret_rect_ != caret_rect) { | |
303 instance->caret_rect_ = caret_rect; | |
304 instance->status_updated_ = true; | |
305 } | |
306 return TRUE; | |
307 } | |
308 | |
309 // static | |
310 BOOL WINAPI WebPluginIMEWin::ImmSetOpenStatus(HIMC context, BOOL open) { | |
311 WebPluginIMEWin* instance = GetInstance(context); | |
312 if (!instance) | |
313 return ::ImmSetOpenStatus(context, open); | |
314 | |
315 int input_type = open ? 1 : 0; | |
316 if (instance->input_type_ != input_type) { | |
317 instance->input_type_ = input_type; | |
318 instance->status_updated_ = true; | |
319 } | |
320 | |
321 return TRUE; | |
322 } | |
323 | |
324 } // namespace npapi | |
325 } // namespace webkit | |
OLD | NEW |