OLD | NEW |
(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 #ifndef WEBKIT_PLUGINS_NPAPI_WEBPLUGIN_IME_WIN_H_ |
| 6 #define WEBKIT_PLUGINS_NPAPI_WEBPLUGIN_IME_WIN_H_ |
| 7 |
| 8 #include <string> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/basictypes.h" |
| 12 #include "third_party/npapi/bindings/npapi.h" |
| 13 #include "ui/gfx/rect.h" |
| 14 |
| 15 namespace WebKit { |
| 16 class WebInputEvent; |
| 17 class WebCompositionEvent; |
| 18 } |
| 19 |
| 20 namespace webkit { |
| 21 namespace npapi { |
| 22 |
| 23 class PluginInstance; |
| 24 |
| 25 // A class that emulates an IME for windowless plug-ins. A windowless plug-in |
| 26 // does not have a window. Therefore, we cannot attach an IME to a windowless |
| 27 // plug-in. To allow such windowless plug-ins to use IMEs without any changes to |
| 28 // them, this class receives the IME data from a browser and patches IMM32 |
| 29 // functions to return the IME data when a windowless plug-in calls IMM32 |
| 30 // functions. I would not Flash retrieves pointers to IMM32 functions with |
| 31 // GetProcAddress(), this class also needs a hook to GetProcAddress() to |
| 32 // dispatch IMM32 function calls from a plug-in to this class as listed in the |
| 33 // following snippet. |
| 34 // |
| 35 // FARPROC WINAPI GetProcAddressPatch(HMODULE module, LPCSTR name) { |
| 36 // FARPROC* proc = WebPluginIMEWin::GetProcAddress(name); |
| 37 // if (proc) |
| 38 // return proc; |
| 39 // return ::GetProcAddress(module, name); |
| 40 // } |
| 41 // ... |
| 42 // app::win::IATPatchFunction get_proc_address; |
| 43 // get_proc_address.Patch( |
| 44 // GetPluginPath().value().c_str(), "kernel32.dll", "GetProcAddress", |
| 45 // GetProcAddressPatch); |
| 46 // |
| 47 // After we successfuly dispatch IMM32 calls from a plug-in to this class, we |
| 48 // need to updates its IME data with WebCompositionEvent events so the class can |
| 49 // return it to the plug-in through its IMM32 calls. To update the IME data, |
| 50 // we need to call the Update() function BEFORE sending an IMM32 Window message |
| 51 // to the plugin with a SendEvents() call as listed in the following snippet. |
| 52 // (Plug-ins call IMM32 functions when it receives IMM32 window messages. We |
| 53 // need to update the IME data of this class before sending IMM32 messages so |
| 54 // the plug-ins can get the latest data.) |
| 55 // |
| 56 // WebPluginIMEWin ime; |
| 57 // WebInputEvent event; |
| 58 // NPEvent np_event; |
| 59 // NPEventFromWebInputEvent(event, &np_event); |
| 60 // ... |
| 61 // if (WebInputEvent::isCompositionEvent(event.type)) |
| 62 // ime.Update(event); |
| 63 // ... |
| 64 // if (WebInputEvent::isCompositionEvent(event.type)) |
| 65 // ime.SendEvents(instance()); |
| 66 // |
| 67 // This class also provides GetStatus() so we can retrieve the IME status |
| 68 // changed by a plug-in with IMM32 functions. This function is mainly used for |
| 69 // retrieving the position of a caret. |
| 70 // |
| 71 class WebPluginIMEWin { |
| 72 public: |
| 73 // A simple class that allows a plug-in to access a WebPluginIMEWin instance |
| 74 // only in a scope. |
| 75 class ScopedLock { |
| 76 public: |
| 77 explicit ScopedLock(WebPluginIMEWin* instance) : instance_(instance) { |
| 78 if (instance_) |
| 79 instance_->Lock(); |
| 80 } |
| 81 ~ScopedLock() { |
| 82 if (instance_) |
| 83 instance_->Unlock(); |
| 84 } |
| 85 |
| 86 private: |
| 87 WebPluginIMEWin* instance_; |
| 88 }; |
| 89 |
| 90 WebPluginIMEWin(); |
| 91 ~WebPluginIMEWin(); |
| 92 |
| 93 // Sends an IME event to this IME emulator and updates the list of Windows |
| 94 // events to be sent to a plug-in. An IME event is mapped to two or more |
| 95 // Windows events and it is not so trivial to send these Windows events to a |
| 96 // plug-in. This function inserts Windows events in the order expected by a |
| 97 // plug-in. |
| 98 void Update(const WebKit::WebInputEvent* event); |
| 99 |
| 100 // Send all the events added in Update() to a plug-in. |
| 101 bool SendEvents(PluginInstance* instance); |
| 102 |
| 103 // Retrieves the status of this IME emulator. |
| 104 bool GetStatus(int* input_type, gfx::Rect* caret_rect); |
| 105 |
| 106 // Initialize an NPEvent with the specified WebComposition event. |
| 107 static bool NPEventFromWebCompositionEvent(const WebKit::WebInputEvent* event, |
| 108 NPEvent* np_event); |
| 109 |
| 110 // Returns the pointers to IMM32-emulation functions implemented by this |
| 111 // class. This function is used for over-writing the ones returned from |
| 112 // GetProcAddress() calls of Win32 API. |
| 113 static FARPROC GetProcAddress(const char* name); |
| 114 |
| 115 private: |
| 116 // Allow (or disallow) the patch functions to use this WebPluginIMEWin |
| 117 // instance through our patch functions. Our patch functions need a static |
| 118 // member variable |instance_| to access a WebPluginIMEWIn instance. We lock |
| 119 // this static variable to prevent two or more plug-ins from accessing a |
| 120 // WebPluginIMEWin instance. |
| 121 void Lock(); |
| 122 void Unlock(); |
| 123 |
| 124 // Retrieve the instance of this class. |
| 125 static WebPluginIMEWin* GetInstance(HIMC context); |
| 126 |
| 127 // IMM32 patch functions implemented by this class. |
| 128 static BOOL WINAPI ImmAssociateContextEx(HWND window, |
| 129 HIMC context, |
| 130 DWORD flags); |
| 131 static LONG WINAPI ImmGetCompositionStringW(HIMC context, |
| 132 DWORD index, |
| 133 LPVOID dst_data, |
| 134 DWORD dst_size); |
| 135 static HIMC WINAPI ImmGetContext(HWND window); |
| 136 static BOOL WINAPI ImmReleaseContext(HWND window, HIMC context); |
| 137 static BOOL WINAPI ImmSetCandidateWindow(HIMC context, |
| 138 CANDIDATEFORM* candidate); |
| 139 static BOOL WINAPI ImmSetOpenStatus(HIMC context, BOOL open); |
| 140 |
| 141 // a list of NPEvents to be sent to a plug-in. |
| 142 std::vector<NPEvent> events_; |
| 143 |
| 144 // The return value for GCS_COMPSTR. |
| 145 std::wstring composition_text_; |
| 146 |
| 147 // The return value for GCS_RESULTSTR. |
| 148 std::wstring result_text_; |
| 149 |
| 150 // The return value for GCS_COMPATTR. |
| 151 std::string composition_attributes_; |
| 152 |
| 153 // The return value for GCS_COMPCLAUSE. |
| 154 std::vector<uint32> composition_clauses_; |
| 155 |
| 156 // The return value for GCS_RESULTCLAUSE. |
| 157 uint32 result_clauses_[2]; |
| 158 |
| 159 // The return value for GCS_CURSORPOS. |
| 160 int cursor_position_; |
| 161 |
| 162 // The return value for GCS_DELTASTART. |
| 163 int delta_start_; |
| 164 |
| 165 // Whether we are composing text. This variable is used for sending a |
| 166 // WM_IME_STARTCOMPOSITION message when we start composing IME text. |
| 167 bool composing_text_; |
| 168 |
| 169 // Whether a plug-in supports IME messages. When a plug-in cannot handle |
| 170 // IME messages, we need to send the IME text with WM_CHAR messages as Windows |
| 171 // does. |
| 172 bool support_ime_messages_; |
| 173 |
| 174 // The IME status received from a plug-in. |
| 175 bool status_updated_; |
| 176 int input_type_; |
| 177 gfx::Rect caret_rect_; |
| 178 |
| 179 // The pointer to the WebPluginIMEWin instance used by patch functions. |
| 180 static WebPluginIMEWin* instance_; |
| 181 }; |
| 182 |
| 183 } // namespace npapi |
| 184 } // namespace webkit |
| 185 |
| 186 #endif // WEBKIT_PLUGINS_NPAPI_WEBPLUGIN_IME_WIN_H_ |
OLD | NEW |