Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(699)

Side by Side Diff: webkit/plugins/npapi/webplugin_ime_win.cc

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

Powered by Google App Engine
This is Rietveld 408576698