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.
5 #ifndef CONTENT_CHILD_NPAPI_WEBPLUGIN_IME_WIN_H_
6 #define CONTENT_CHILD_NPAPI_WEBPLUGIN_IME_WIN_H_
11 #include "base/basictypes.h"
12 #include "base/strings/string16.h"
13 #include "third_party/npapi/bindings/npapi.h"
14 #include "ui/gfx/geometry/rect.h"
20 // A class that emulates an IME for windowless plugins. A windowless plugin
21 // does not have a window. Therefore, we cannot attach an IME to a windowless
22 // plugin. To allow such windowless plugins to use IMEs without any changes to
23 // them, this class receives the IME data from a browser and patches IMM32
24 // functions to return the IME data when a windowless plugin calls IMM32
25 // functions. I would not Flash retrieves pointers to IMM32 functions with
26 // GetProcAddress(), this class also needs a hook to GetProcAddress() to
27 // dispatch IMM32 function calls from a plugin to this class as listed in the
30 // FARPROC WINAPI GetProcAddressPatch(HMODULE module, LPCSTR name) {
31 // FARPROC* proc = WebPluginIMEWin::GetProcAddress(name);
34 // return ::GetProcAddress(module, name);
37 // app::win::IATPatchFunction get_proc_address;
38 // get_proc_address.Patch(
39 // GetPluginPath().value().c_str(), "kernel32.dll", "GetProcAddress",
40 // GetProcAddressPatch);
42 // After we successfuly dispatch IMM32 calls from a plugin to this class, we
43 // need to update its IME data so the class can return it to the plugin through
44 // its IMM32 calls. To update the IME data, we call CompositionUpdated() or
45 // CompositionCompleted() BEFORE sending an IMM32 Window message to the plugin
46 // with a SendEvents() call as listed in the following snippet. (Plugins call
47 // IMM32 functions when it receives IMM32 window messages. We need to update the
48 // IME data of this class before sending IMM32 messages so the plugins can get
51 // WebPluginIMEWin ime;
53 // base::string16 text = "composing";
54 // std::vector<int> clauses;
55 // clauses.push_back(0);
56 // clauses.push_back(text.length());
57 // std::vector<int> target;
58 // ime.CompositionUpdated(text, clauses, target, text.length());
59 // ime.SendEvents(instance());
61 // base::string16 result = "result";
62 // ime.CompositionCompleted(result);
63 // ime.SendEvents(instance());
65 // This class also provides GetStatus() so we can retrieve the IME status
66 // changed by a plugin with IMM32 functions. This function is mainly used for
67 // retrieving the position of a caret.
69 class WebPluginIMEWin
{
71 // A simple class that allows a plugin to access a WebPluginIMEWin instance
75 explicit ScopedLock(WebPluginIMEWin
* instance
) : instance_(instance
) {
85 WebPluginIMEWin
* instance_
;
91 // Sends raw IME events sent from a browser to this IME emulator and updates
92 // the list of Windows events to be sent to a plugin. A raw IME event is
93 // mapped to two or more Windows events and it is not so trivial to send these
94 // Windows events to a plugin. This function inserts Windows events in the
95 // order expected by a plugin.
96 void CompositionUpdated(const base::string16
& text
,
97 std::vector
<int> clauses
,
98 std::vector
<int> target
,
100 void CompositionCompleted(const base::string16
& text
);
102 // Send all the events added in Update() to a plugin.
103 bool SendEvents(PluginInstance
* instance
);
105 // Retrieves the status of this IME emulator.
106 bool GetStatus(int* input_type
, gfx::Rect
* caret_rect
);
108 // Returns the pointers to IMM32-emulation functions implemented by this
109 // class. This function is used for over-writing the ones returned from
110 // GetProcAddress() calls of Win32 API.
111 static FARPROC
GetProcAddress(const char* name
);
114 // Allow (or disallow) the patch functions to use this WebPluginIMEWin
115 // instance through our patch functions. Our patch functions need a static
116 // member variable |instance_| to access a WebPluginIMEWIn instance. We lock
117 // this static variable to prevent two or more plugins from accessing a
118 // WebPluginIMEWin instance.
122 // Retrieve the instance of this class.
123 static WebPluginIMEWin
* GetInstance(HIMC context
);
125 // IMM32 patch functions implemented by this class.
126 static BOOL WINAPI
ImmAssociateContextEx(HWND window
,
129 static LONG WINAPI
ImmGetCompositionStringW(HIMC context
,
133 static HIMC WINAPI
ImmGetContext(HWND window
);
134 static BOOL WINAPI
ImmReleaseContext(HWND window
, HIMC context
);
135 static BOOL WINAPI
ImmSetCandidateWindow(HIMC context
,
136 CANDIDATEFORM
* candidate
);
137 static BOOL WINAPI
ImmSetOpenStatus(HIMC context
, BOOL open
);
139 // a list of NPEvents to be sent to a plugin.
140 std::vector
<NPEvent
> events_
;
142 // The return value for GCS_COMPSTR.
143 base::string16 composition_text_
;
145 // The return value for GCS_RESULTSTR.
146 base::string16 result_text_
;
148 // The return value for GCS_COMPATTR.
149 std::string composition_attributes_
;
151 // The return value for GCS_COMPCLAUSE.
152 std::vector
<uint32
> composition_clauses_
;
154 // The return value for GCS_RESULTCLAUSE.
155 uint32 result_clauses_
[2];
157 // The return value for GCS_CURSORPOS.
158 int cursor_position_
;
160 // The return value for GCS_DELTASTART.
163 // Whether we are composing text. This variable is used for sending a
164 // WM_IME_STARTCOMPOSITION message when we start composing IME text.
165 bool composing_text_
;
167 // Whether a plugin supports IME messages. When a plugin cannot handle
168 // IME messages, we need to send the IME text with WM_CHAR messages as Windows
170 bool support_ime_messages_
;
172 // The IME status received from a plugin.
173 bool status_updated_
;
175 gfx::Rect caret_rect_
;
177 // The pointer to the WebPluginIMEWin instance used by patch functions.
178 static WebPluginIMEWin
* instance_
;
181 } // namespace content
183 #endif // CONTENT_CHILD_NPAPI_WEBPLUGIN_IME_WIN_H_