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