/src/mozilla-central/dom/events/IMEStateManager.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include "mozilla/Logging.h" |
8 | | |
9 | | #include "mozilla/IMEStateManager.h" |
10 | | |
11 | | #include "mozilla/Attributes.h" |
12 | | #include "mozilla/EditorBase.h" |
13 | | #include "mozilla/EventListenerManager.h" |
14 | | #include "mozilla/EventStates.h" |
15 | | #include "mozilla/MouseEvents.h" |
16 | | #include "mozilla/Preferences.h" |
17 | | #include "mozilla/Services.h" |
18 | | #include "mozilla/TextComposition.h" |
19 | | #include "mozilla/TextEvents.h" |
20 | | #include "mozilla/Unused.h" |
21 | | #include "mozilla/dom/HTMLFormElement.h" |
22 | | #include "mozilla/dom/MouseEventBinding.h" |
23 | | #include "mozilla/dom/TabParent.h" |
24 | | |
25 | | #include "HTMLInputElement.h" |
26 | | #include "IMEContentObserver.h" |
27 | | |
28 | | #include "nsCOMPtr.h" |
29 | | #include "nsContentUtils.h" |
30 | | #include "nsIContent.h" |
31 | | #include "nsIDocument.h" |
32 | | #include "nsIForm.h" |
33 | | #include "nsIFormControl.h" |
34 | | #include "nsINode.h" |
35 | | #include "nsIObserverService.h" |
36 | | #include "nsIPresShell.h" |
37 | | #include "nsISupports.h" |
38 | | #include "nsPresContext.h" |
39 | | |
40 | | namespace mozilla { |
41 | | |
42 | | using namespace dom; |
43 | | using namespace widget; |
44 | | |
45 | | /** |
46 | | * When a method is called, log its arguments and/or related static variables |
47 | | * with LogLevel::Info. However, if it puts too many logs like |
48 | | * OnDestroyPresContext(), should long only when the method actually does |
49 | | * something. In this case, the log should start with "<method name>". |
50 | | * |
51 | | * When a method quits due to unexpected situation, log the reason with |
52 | | * LogLevel::Error. In this case, the log should start with |
53 | | * "<method name>(), FAILED". The indent makes the log look easier. |
54 | | * |
55 | | * When a method does something only in some situations and it may be important |
56 | | * for debug, log the information with LogLevel::Debug. In this case, the log |
57 | | * should start with " <method name>(),". |
58 | | */ |
59 | | LazyLogModule sISMLog("IMEStateManager"); |
60 | | |
61 | | static const char* |
62 | | GetBoolName(bool aBool) |
63 | 0 | { |
64 | 0 | return aBool ? "true" : "false"; |
65 | 0 | } |
66 | | |
67 | | static const char* |
68 | | GetActionCauseName(InputContextAction::Cause aCause) |
69 | | { |
70 | | switch (aCause) { |
71 | | case InputContextAction::CAUSE_UNKNOWN: |
72 | | return "CAUSE_UNKNOWN"; |
73 | | case InputContextAction::CAUSE_UNKNOWN_CHROME: |
74 | | return "CAUSE_UNKNOWN_CHROME"; |
75 | | case InputContextAction::CAUSE_KEY: |
76 | | return "CAUSE_KEY"; |
77 | | case InputContextAction::CAUSE_MOUSE: |
78 | | return "CAUSE_MOUSE"; |
79 | | case InputContextAction::CAUSE_TOUCH: |
80 | | return "CAUSE_TOUCH"; |
81 | | default: |
82 | | return "illegal value"; |
83 | | } |
84 | | } |
85 | | |
86 | | static const char* |
87 | | GetActionFocusChangeName(InputContextAction::FocusChange aFocusChange) |
88 | | { |
89 | | switch (aFocusChange) { |
90 | | case InputContextAction::FOCUS_NOT_CHANGED: |
91 | | return "FOCUS_NOT_CHANGED"; |
92 | | case InputContextAction::GOT_FOCUS: |
93 | | return "GOT_FOCUS"; |
94 | | case InputContextAction::LOST_FOCUS: |
95 | | return "LOST_FOCUS"; |
96 | | case InputContextAction::MENU_GOT_PSEUDO_FOCUS: |
97 | | return "MENU_GOT_PSEUDO_FOCUS"; |
98 | | case InputContextAction::MENU_LOST_PSEUDO_FOCUS: |
99 | | return "MENU_LOST_PSEUDO_FOCUS"; |
100 | | default: |
101 | | return "illegal value"; |
102 | | } |
103 | | } |
104 | | |
105 | | static const char* |
106 | | GetIMEStateEnabledName(IMEState::Enabled aEnabled) |
107 | | { |
108 | | switch (aEnabled) { |
109 | | case IMEState::DISABLED: |
110 | | return "DISABLED"; |
111 | | case IMEState::ENABLED: |
112 | | return "ENABLED"; |
113 | | case IMEState::PASSWORD: |
114 | | return "PASSWORD"; |
115 | | case IMEState::PLUGIN: |
116 | | return "PLUGIN"; |
117 | | default: |
118 | | return "illegal value"; |
119 | | } |
120 | | } |
121 | | |
122 | | static const char* |
123 | | GetIMEStateSetOpenName(IMEState::Open aOpen) |
124 | | { |
125 | | switch (aOpen) { |
126 | | case IMEState::DONT_CHANGE_OPEN_STATE: |
127 | | return "DONT_CHANGE_OPEN_STATE"; |
128 | | case IMEState::OPEN: |
129 | | return "OPEN"; |
130 | | case IMEState::CLOSED: |
131 | | return "CLOSED"; |
132 | | default: |
133 | | return "illegal value"; |
134 | | } |
135 | | } |
136 | | |
137 | | static bool |
138 | | IsSameProcess(const TabParent* aTabParent1, const TabParent* aTabParent2) |
139 | 0 | { |
140 | 0 | if (aTabParent1 == aTabParent2) { |
141 | 0 | return true; |
142 | 0 | } |
143 | 0 | if (!aTabParent1 != !aTabParent2) { |
144 | 0 | return false; |
145 | 0 | } |
146 | 0 | return aTabParent1->Manager() == aTabParent2->Manager(); |
147 | 0 | } |
148 | | |
149 | | StaticRefPtr<nsIContent> IMEStateManager::sContent; |
150 | | StaticRefPtr<nsPresContext> IMEStateManager::sPresContext; |
151 | | nsIWidget* IMEStateManager::sWidget = nullptr; |
152 | | nsIWidget* IMEStateManager::sFocusedIMEWidget = nullptr; |
153 | | StaticRefPtr<TabParent> IMEStateManager::sFocusedIMETabParent; |
154 | | nsIWidget* IMEStateManager::sActiveInputContextWidget = nullptr; |
155 | | StaticRefPtr<TabParent> IMEStateManager::sActiveTabParent; |
156 | | StaticRefPtr<IMEContentObserver> IMEStateManager::sActiveIMEContentObserver; |
157 | | TextCompositionArray* IMEStateManager::sTextCompositions = nullptr; |
158 | | InputContext::Origin IMEStateManager::sOrigin = InputContext::ORIGIN_MAIN; |
159 | | InputContext IMEStateManager::sActiveChildInputContext; |
160 | | bool IMEStateManager::sInstalledMenuKeyboardListener = false; |
161 | | bool IMEStateManager::sIsGettingNewIMEState = false; |
162 | | bool IMEStateManager::sCheckForIMEUnawareWebApps = false; |
163 | | bool IMEStateManager::sInputModeSupported = false; |
164 | | |
165 | | // static |
166 | | void |
167 | | IMEStateManager::Init() |
168 | 3 | { |
169 | 3 | Preferences::AddBoolVarCache( |
170 | 3 | &sCheckForIMEUnawareWebApps, |
171 | 3 | "intl.ime.hack.on_ime_unaware_apps.fire_key_events_for_composition", |
172 | 3 | false); |
173 | 3 | |
174 | 3 | Preferences::AddBoolVarCache( |
175 | 3 | &sInputModeSupported, |
176 | 3 | "dom.forms.inputmode", |
177 | 3 | false); |
178 | 3 | |
179 | 3 | sOrigin = XRE_IsParentProcess() ? InputContext::ORIGIN_MAIN : |
180 | 3 | InputContext::ORIGIN_CONTENT; |
181 | 3 | ResetActiveChildInputContext(); |
182 | 3 | } |
183 | | |
184 | | // static |
185 | | void |
186 | | IMEStateManager::Shutdown() |
187 | 0 | { |
188 | 0 | MOZ_LOG(sISMLog, LogLevel::Info, |
189 | 0 | ("Shutdown(), sTextCompositions=0x%p, sTextCompositions->Length()=%zu", |
190 | 0 | sTextCompositions, sTextCompositions ? sTextCompositions->Length() : 0)); |
191 | 0 |
|
192 | 0 | MOZ_ASSERT(!sTextCompositions || !sTextCompositions->Length()); |
193 | 0 | delete sTextCompositions; |
194 | 0 | sTextCompositions = nullptr; |
195 | 0 | // All string instances in the global space need to be empty after XPCOM |
196 | 0 | // shutdown. |
197 | 0 | sActiveChildInputContext.ShutDown(); |
198 | 0 | } |
199 | | |
200 | | // static |
201 | | void |
202 | | IMEStateManager::OnTabParentDestroying(TabParent* aTabParent) |
203 | 0 | { |
204 | 0 | if (sFocusedIMETabParent == aTabParent) { |
205 | 0 | NotifyIMEOfBlurForChildProcess(); |
206 | 0 | } |
207 | 0 |
|
208 | 0 | if (sActiveTabParent != aTabParent) { |
209 | 0 | return; |
210 | 0 | } |
211 | 0 | |
212 | 0 | MOZ_LOG(sISMLog, LogLevel::Info, |
213 | 0 | ("OnTabParentDestroying(aTabParent=0x%p), " |
214 | 0 | "The active TabParent is being destroyed", aTabParent)); |
215 | 0 |
|
216 | 0 | // The active remote process might have crashed. |
217 | 0 | sActiveTabParent = nullptr; |
218 | 0 |
|
219 | 0 | // XXX: Need to disable IME? |
220 | 0 | } |
221 | | |
222 | | // static |
223 | | void |
224 | | IMEStateManager::WidgetDestroyed(nsIWidget* aWidget) |
225 | 0 | { |
226 | 0 | if (sWidget == aWidget) { |
227 | 0 | sWidget = nullptr; |
228 | 0 | } |
229 | 0 | if (sFocusedIMEWidget == aWidget) { |
230 | 0 | NotifyIMEOfBlurForChildProcess(); |
231 | 0 | sFocusedIMEWidget = nullptr; |
232 | 0 | } |
233 | 0 | if (sActiveInputContextWidget == aWidget) { |
234 | 0 | sActiveInputContextWidget = nullptr; |
235 | 0 | } |
236 | 0 | } |
237 | | |
238 | | // static |
239 | | void |
240 | | IMEStateManager::StopIMEStateManagement() |
241 | 0 | { |
242 | 0 | MOZ_LOG(sISMLog, LogLevel::Info, |
243 | 0 | ("StopIMEStateManagement()")); |
244 | 0 |
|
245 | 0 | // NOTE: Don't set input context from here since this has already lost |
246 | 0 | // the rights to change input context. |
247 | 0 |
|
248 | 0 | if (sTextCompositions && sPresContext) { |
249 | 0 | NotifyIME(REQUEST_TO_COMMIT_COMPOSITION, sPresContext, sActiveTabParent); |
250 | 0 | } |
251 | 0 | sActiveInputContextWidget = nullptr; |
252 | 0 | sPresContext = nullptr; |
253 | 0 | sContent = nullptr; |
254 | 0 | sActiveTabParent = nullptr; |
255 | 0 | DestroyIMEContentObserver(); |
256 | 0 | } |
257 | | |
258 | | // static |
259 | | void |
260 | | IMEStateManager::NotifyIMEOfBlurForChildProcess() |
261 | 0 | { |
262 | 0 | MOZ_LOG(sISMLog, LogLevel::Debug, |
263 | 0 | ("NotifyIMEOfBlurForChildProcess(), sFocusedIMETabParent=0x%p, " |
264 | 0 | "sFocusedIMEWidget=0x%p", |
265 | 0 | sFocusedIMETabParent.get(), sFocusedIMEWidget)); |
266 | 0 |
|
267 | 0 | if (!sFocusedIMETabParent) { |
268 | 0 | MOZ_ASSERT(!sFocusedIMEWidget); |
269 | 0 | return; |
270 | 0 | } |
271 | 0 |
|
272 | 0 | MOZ_ASSERT(sFocusedIMEWidget); |
273 | 0 |
|
274 | 0 | if (MOZ_LOG_TEST(sISMLog, LogLevel::Debug) && sTextCompositions) { |
275 | 0 | RefPtr<TextComposition> composition = |
276 | 0 | sTextCompositions->GetCompositionFor(sFocusedIMEWidget); |
277 | 0 | if (composition) { |
278 | 0 | MOZ_LOG(sISMLog, LogLevel::Debug, |
279 | 0 | (" NotifyIMEOfBlurForChildProcess(), sFocusedIMEWidget still has " |
280 | 0 | "composition")); |
281 | 0 | } |
282 | 0 | } |
283 | 0 |
|
284 | 0 | NotifyIME(NOTIFY_IME_OF_BLUR, sFocusedIMEWidget, sFocusedIMETabParent); |
285 | 0 |
|
286 | 0 | MOZ_ASSERT(!sFocusedIMETabParent); |
287 | 0 | MOZ_ASSERT(!sFocusedIMEWidget); |
288 | 0 | } |
289 | | |
290 | | // static |
291 | | void |
292 | | IMEStateManager::MaybeStartOffsetUpdatedInChild(nsIWidget* aWidget, |
293 | | uint32_t aStartOffset) |
294 | 0 | { |
295 | 0 | if (NS_WARN_IF(!sTextCompositions)) { |
296 | 0 | MOZ_LOG(sISMLog, LogLevel::Warning, |
297 | 0 | ("MaybeStartOffsetUpdatedInChild(aWidget=0x%p, aStartOffset=%u), " |
298 | 0 | "called when there is no composition", aWidget, aStartOffset)); |
299 | 0 | return; |
300 | 0 | } |
301 | 0 |
|
302 | 0 | RefPtr<TextComposition> composition = GetTextCompositionFor(aWidget); |
303 | 0 | if (NS_WARN_IF(!composition)) { |
304 | 0 | MOZ_LOG(sISMLog, LogLevel::Warning, |
305 | 0 | ("MaybeStartOffsetUpdatedInChild(aWidget=0x%p, aStartOffset=%u), " |
306 | 0 | "called when there is no composition", aWidget, aStartOffset)); |
307 | 0 | return; |
308 | 0 | } |
309 | 0 |
|
310 | 0 | if (composition->NativeOffsetOfStartComposition() == aStartOffset) { |
311 | 0 | return; |
312 | 0 | } |
313 | 0 | |
314 | 0 | MOZ_LOG(sISMLog, LogLevel::Info, |
315 | 0 | ("MaybeStartOffsetUpdatedInChild(aWidget=0x%p, aStartOffset=%u), " |
316 | 0 | "old offset=%u", |
317 | 0 | aWidget, aStartOffset, composition->NativeOffsetOfStartComposition())); |
318 | 0 | composition->OnStartOffsetUpdatedInChild(aStartOffset); |
319 | 0 | } |
320 | | |
321 | | // static |
322 | | nsresult |
323 | | IMEStateManager::OnDestroyPresContext(nsPresContext* aPresContext) |
324 | 0 | { |
325 | 0 | NS_ENSURE_ARG_POINTER(aPresContext); |
326 | 0 |
|
327 | 0 | // First, if there is a composition in the aPresContext, clean up it. |
328 | 0 | if (sTextCompositions) { |
329 | 0 | TextCompositionArray::index_type i = |
330 | 0 | sTextCompositions->IndexOf(aPresContext); |
331 | 0 | if (i != TextCompositionArray::NoIndex) { |
332 | 0 | MOZ_LOG(sISMLog, LogLevel::Debug, |
333 | 0 | (" OnDestroyPresContext(), " |
334 | 0 | "removing TextComposition instance from the array (index=%zu)", i)); |
335 | 0 | // there should be only one composition per presContext object. |
336 | 0 | sTextCompositions->ElementAt(i)->Destroy(); |
337 | 0 | sTextCompositions->RemoveElementAt(i); |
338 | 0 | if (sTextCompositions->IndexOf(aPresContext) != |
339 | 0 | TextCompositionArray::NoIndex) { |
340 | 0 | MOZ_LOG(sISMLog, LogLevel::Error, |
341 | 0 | (" OnDestroyPresContext(), FAILED to remove " |
342 | 0 | "TextComposition instance from the array")); |
343 | 0 | MOZ_CRASH("Failed to remove TextComposition instance from the array"); |
344 | 0 | } |
345 | 0 | } |
346 | 0 | } |
347 | 0 |
|
348 | 0 | if (aPresContext != sPresContext) { |
349 | 0 | return NS_OK; |
350 | 0 | } |
351 | 0 | |
352 | 0 | MOZ_LOG(sISMLog, LogLevel::Info, |
353 | 0 | ("OnDestroyPresContext(aPresContext=0x%p), " |
354 | 0 | "sPresContext=0x%p, sContent=0x%p, sTextCompositions=0x%p", |
355 | 0 | aPresContext, sPresContext.get(), sContent.get(), sTextCompositions)); |
356 | 0 |
|
357 | 0 | DestroyIMEContentObserver(); |
358 | 0 |
|
359 | 0 | if (sWidget) { |
360 | 0 | IMEState newState = GetNewIMEState(sPresContext, nullptr); |
361 | 0 | InputContextAction action(InputContextAction::CAUSE_UNKNOWN, |
362 | 0 | InputContextAction::LOST_FOCUS); |
363 | 0 | InputContext::Origin origin = |
364 | 0 | sActiveTabParent ? InputContext::ORIGIN_CONTENT : sOrigin; |
365 | 0 | SetIMEState(newState, nullptr, nullptr, sWidget, action, origin); |
366 | 0 | } |
367 | 0 | sWidget = nullptr; |
368 | 0 | sContent = nullptr; |
369 | 0 | sPresContext = nullptr; |
370 | 0 | sActiveTabParent = nullptr; |
371 | 0 | return NS_OK; |
372 | 0 | } |
373 | | |
374 | | // static |
375 | | nsresult |
376 | | IMEStateManager::OnRemoveContent(nsPresContext* aPresContext, |
377 | | nsIContent* aContent) |
378 | 0 | { |
379 | 0 | NS_ENSURE_ARG_POINTER(aPresContext); |
380 | 0 |
|
381 | 0 | // First, if there is a composition in the aContent, clean up it. |
382 | 0 | if (sTextCompositions) { |
383 | 0 | RefPtr<TextComposition> compositionInContent = |
384 | 0 | sTextCompositions->GetCompositionInContent(aPresContext, aContent); |
385 | 0 |
|
386 | 0 | if (compositionInContent) { |
387 | 0 | MOZ_LOG(sISMLog, LogLevel::Debug, |
388 | 0 | (" OnRemoveContent(), " |
389 | 0 | "composition is in the content")); |
390 | 0 |
|
391 | 0 | // Try resetting the native IME state. Be aware, typically, this method |
392 | 0 | // is called during the content being removed. Then, the native |
393 | 0 | // composition events which are caused by following APIs are ignored due |
394 | 0 | // to unsafe to run script (in PresShell::HandleEvent()). |
395 | 0 | nsresult rv = |
396 | 0 | compositionInContent->NotifyIME(REQUEST_TO_CANCEL_COMPOSITION); |
397 | 0 | if (NS_FAILED(rv)) { |
398 | 0 | compositionInContent->NotifyIME(REQUEST_TO_COMMIT_COMPOSITION); |
399 | 0 | } |
400 | 0 | } |
401 | 0 | } |
402 | 0 |
|
403 | 0 | if (!sPresContext || !sContent || |
404 | 0 | !nsContentUtils::ContentIsDescendantOf(sContent, aContent)) { |
405 | 0 | return NS_OK; |
406 | 0 | } |
407 | 0 | |
408 | 0 | MOZ_LOG(sISMLog, LogLevel::Info, |
409 | 0 | ("OnRemoveContent(aPresContext=0x%p, aContent=0x%p), " |
410 | 0 | "sPresContext=0x%p, sContent=0x%p, sTextCompositions=0x%p", |
411 | 0 | aPresContext, aContent, sPresContext.get(), sContent.get(), sTextCompositions)); |
412 | 0 |
|
413 | 0 | DestroyIMEContentObserver(); |
414 | 0 |
|
415 | 0 | // Current IME transaction should commit |
416 | 0 | if (sWidget) { |
417 | 0 | IMEState newState = GetNewIMEState(sPresContext, nullptr); |
418 | 0 | InputContextAction action(InputContextAction::CAUSE_UNKNOWN, |
419 | 0 | InputContextAction::LOST_FOCUS); |
420 | 0 | InputContext::Origin origin = |
421 | 0 | sActiveTabParent ? InputContext::ORIGIN_CONTENT : sOrigin; |
422 | 0 | SetIMEState(newState, aPresContext, nullptr, sWidget, action, origin); |
423 | 0 | } |
424 | 0 |
|
425 | 0 | sWidget = nullptr; |
426 | 0 | sContent = nullptr; |
427 | 0 | sPresContext = nullptr; |
428 | 0 | sActiveTabParent = nullptr; |
429 | 0 |
|
430 | 0 | return NS_OK; |
431 | 0 | } |
432 | | |
433 | | // static |
434 | | bool |
435 | | IMEStateManager::CanHandleWith(nsPresContext* aPresContext) |
436 | 0 | { |
437 | 0 | return aPresContext && |
438 | 0 | aPresContext->GetPresShell() && |
439 | 0 | !aPresContext->PresShell()->IsDestroying(); |
440 | 0 | } |
441 | | |
442 | | // static |
443 | | nsresult |
444 | | IMEStateManager::OnChangeFocus(nsPresContext* aPresContext, |
445 | | nsIContent* aContent, |
446 | | InputContextAction::Cause aCause) |
447 | 0 | { |
448 | 0 | MOZ_LOG(sISMLog, LogLevel::Info, |
449 | 0 | ("OnChangeFocus(aPresContext=0x%p, aContent=0x%p, aCause=%s)", |
450 | 0 | aPresContext, aContent, GetActionCauseName(aCause))); |
451 | 0 |
|
452 | 0 | InputContextAction action(aCause); |
453 | 0 | return OnChangeFocusInternal(aPresContext, aContent, action); |
454 | 0 | } |
455 | | |
456 | | // static |
457 | | nsresult |
458 | | IMEStateManager::OnChangeFocusInternal(nsPresContext* aPresContext, |
459 | | nsIContent* aContent, |
460 | | InputContextAction aAction) |
461 | 0 | { |
462 | 0 | RefPtr<TabParent> newTabParent = TabParent::GetFrom(aContent); |
463 | 0 |
|
464 | 0 | MOZ_LOG(sISMLog, LogLevel::Info, |
465 | 0 | ("OnChangeFocusInternal(aPresContext=0x%p (available: %s), " |
466 | 0 | "aContent=0x%p (TabParent=0x%p), aAction={ mCause=%s, mFocusChange=%s }), " |
467 | 0 | "sPresContext=0x%p (available: %s), sContent=0x%p, " |
468 | 0 | "sWidget=0x%p (available: %s), sActiveTabParent=0x%p, " |
469 | 0 | "sActiveIMEContentObserver=0x%p, sInstalledMenuKeyboardListener=%s", |
470 | 0 | aPresContext, GetBoolName(CanHandleWith(aPresContext)), aContent, |
471 | 0 | newTabParent.get(), GetActionCauseName(aAction.mCause), |
472 | 0 | GetActionFocusChangeName(aAction.mFocusChange), |
473 | 0 | sPresContext.get(), GetBoolName(CanHandleWith(sPresContext)), |
474 | 0 | sContent.get(), sWidget, GetBoolName(sWidget && !sWidget->Destroyed()), |
475 | 0 | sActiveTabParent.get(), sActiveIMEContentObserver.get(), |
476 | 0 | GetBoolName(sInstalledMenuKeyboardListener))); |
477 | 0 |
|
478 | 0 | // If new aPresShell has been destroyed, this should handle the focus change |
479 | 0 | // as nobody is getting focus. |
480 | 0 | if (NS_WARN_IF(aPresContext && !CanHandleWith(aPresContext))) { |
481 | 0 | MOZ_LOG(sISMLog, LogLevel::Warning, |
482 | 0 | (" OnChangeFocusInternal(), called with destroyed PresShell, " |
483 | 0 | "handling this call as nobody getting focus")); |
484 | 0 | aPresContext = nullptr; |
485 | 0 | aContent = nullptr; |
486 | 0 | } |
487 | 0 |
|
488 | 0 | nsCOMPtr<nsIWidget> oldWidget = sWidget; |
489 | 0 | nsCOMPtr<nsIWidget> newWidget = |
490 | 0 | aPresContext ? aPresContext->GetRootWidget() : nullptr; |
491 | 0 | bool focusActuallyChanging = |
492 | 0 | (sContent != aContent || sPresContext != aPresContext || |
493 | 0 | oldWidget != newWidget || sActiveTabParent != newTabParent); |
494 | 0 |
|
495 | 0 | // If old widget has composition, we may need to commit composition since |
496 | 0 | // a native IME context is shared on all editors on some widgets or all |
497 | 0 | // widgets (it depends on platforms). |
498 | 0 | if (oldWidget && focusActuallyChanging && sTextCompositions) { |
499 | 0 | RefPtr<TextComposition> composition = |
500 | 0 | sTextCompositions->GetCompositionFor(oldWidget); |
501 | 0 | if (composition) { |
502 | 0 | // However, don't commit the composition if we're being inactivated |
503 | 0 | // but the composition should be kept even during deactive. |
504 | 0 | // Note that oldWidget and sFocusedIMEWidget may be different here (in |
505 | 0 | // such case, sFocusedIMEWidget is perhaps nullptr). For example, IME |
506 | 0 | // may receive only blur notification but still has composition. |
507 | 0 | // We need to clean up only the oldWidget's composition state here. |
508 | 0 | if (aPresContext || |
509 | 0 | !oldWidget->IMENotificationRequestsRef().WantDuringDeactive()) { |
510 | 0 | MOZ_LOG(sISMLog, LogLevel::Info, |
511 | 0 | (" OnChangeFocusInternal(), requesting to commit composition to " |
512 | 0 | "the (previous) focused widget")); |
513 | 0 | NotifyIME(REQUEST_TO_COMMIT_COMPOSITION, oldWidget, |
514 | 0 | composition->GetTabParent()); |
515 | 0 | } |
516 | 0 | } |
517 | 0 | } |
518 | 0 |
|
519 | 0 | if (sActiveIMEContentObserver) { |
520 | 0 | // If there is active IMEContentObserver, it means that focused content was |
521 | 0 | // in this process. So, if a tab parent gets focus, it means that the |
522 | 0 | // focused editor in this process is being blurred. |
523 | 0 | if (newTabParent) { |
524 | 0 | DestroyIMEContentObserver(); |
525 | 0 | } |
526 | 0 | // If the process is being inactivated, then, IMEContentObserver should |
527 | 0 | // stop observing the contents unless native IME requests to keep |
528 | 0 | // composition even during deactivated. |
529 | 0 | else if (!aPresContext) { |
530 | 0 | if (!sActiveIMEContentObserver->KeepAliveDuringDeactive()) { |
531 | 0 | DestroyIMEContentObserver(); |
532 | 0 | } |
533 | 0 | } |
534 | 0 | // Otherwise, i.e., new focused content is in this process, let's check |
535 | 0 | // whether the new focused content is already being managed by the |
536 | 0 | // active IME content observer. |
537 | 0 | else if (!sActiveIMEContentObserver->IsManaging(aPresContext, aContent)) { |
538 | 0 | DestroyIMEContentObserver(); |
539 | 0 | } |
540 | 0 | } else { |
541 | 0 | // If there is no active IMEContentObserver, it means that focused content |
542 | 0 | // may be in another process. |
543 | 0 |
|
544 | 0 | // If focus is moving from current focused remote process to different |
545 | 0 | // process while the process has IME focus too, we need to notify IME of |
546 | 0 | // blur here because it may be too late the blur notification to reach |
547 | 0 | // this process especially when closing active window. |
548 | 0 | // However, don't send blur if we're being deactivated and IME wants to |
549 | 0 | // keep composition during deactive because notifying blur will commit |
550 | 0 | // or cancel composition. |
551 | 0 | if (sFocusedIMETabParent && sFocusedIMEWidget && |
552 | 0 | (aPresContext || |
553 | 0 | !sFocusedIMEWidget->IMENotificationRequestsRef(). |
554 | 0 | WantDuringDeactive()) && |
555 | 0 | !IsSameProcess(sFocusedIMETabParent, newTabParent)) { |
556 | 0 | MOZ_LOG(sISMLog, LogLevel::Info, |
557 | 0 | (" OnChangeFocusInternal(), notifying IME of blur of previous focused " |
558 | 0 | "remote process because it may be too late actual notification to " |
559 | 0 | "reach this process")); |
560 | 0 | NotifyIMEOfBlurForChildProcess(); |
561 | 0 | } |
562 | 0 | } |
563 | 0 |
|
564 | 0 | if (!aPresContext) { |
565 | 0 | MOZ_LOG(sISMLog, LogLevel::Debug, |
566 | 0 | (" OnChangeFocusInternal(), " |
567 | 0 | "no nsPresContext is being activated")); |
568 | 0 | return NS_OK; |
569 | 0 | } |
570 | 0 |
|
571 | 0 | if (sActiveTabParent && !IsSameProcess(sActiveTabParent, newTabParent)) { |
572 | 0 | MOZ_LOG(sISMLog, LogLevel::Debug, |
573 | 0 | (" OnChangeFocusInternal(), notifying previous " |
574 | 0 | "focused child process of parent process or another child process " |
575 | 0 | "getting focus")); |
576 | 0 | Unused << sActiveTabParent->SendStopIMEStateManagement(); |
577 | 0 | } |
578 | 0 |
|
579 | 0 | if (NS_WARN_IF(!newWidget)) { |
580 | 0 | MOZ_LOG(sISMLog, LogLevel::Error, |
581 | 0 | (" OnChangeFocusInternal(), FAILED due to " |
582 | 0 | "no widget to manage its IME state")); |
583 | 0 | return NS_OK; |
584 | 0 | } |
585 | 0 |
|
586 | 0 | // Update the cached widget since root view of the presContext may be |
587 | 0 | // changed to different view. |
588 | 0 | sWidget = newWidget; |
589 | 0 |
|
590 | 0 | // If a child process has focus, we should disable IME state until the child |
591 | 0 | // process actually gets focus because if user types keys before that they |
592 | 0 | // are handled by IME. |
593 | 0 | IMEState newState = |
594 | 0 | newTabParent ? IMEState(IMEState::DISABLED) : |
595 | 0 | GetNewIMEState(aPresContext, aContent); |
596 | 0 | bool setIMEState = true; |
597 | 0 |
|
598 | 0 | if (newTabParent) { |
599 | 0 | MOZ_ASSERT(XRE_IsParentProcess()); |
600 | 0 | if (aAction.mFocusChange == InputContextAction::MENU_GOT_PSEUDO_FOCUS) { |
601 | 0 | // If menu keyboard listener is installed, we need to disable IME now. |
602 | 0 | setIMEState = true; |
603 | 0 | } else if (aAction.mFocusChange == |
604 | 0 | InputContextAction::MENU_LOST_PSEUDO_FOCUS) { |
605 | 0 | // If menu keyboard listener is uninstalled, we need to restore |
606 | 0 | // input context which was set by the remote process. However, if |
607 | 0 | // the remote process hasn't been set input context yet, we need to |
608 | 0 | // wait next SetInputContextForChildProcess() call. |
609 | 0 | if (HasActiveChildSetInputContext()) { |
610 | 0 | setIMEState = true; |
611 | 0 | newState = sActiveChildInputContext.mIMEState; |
612 | 0 | } else { |
613 | 0 | setIMEState = false; |
614 | 0 | } |
615 | 0 | } else if (focusActuallyChanging) { |
616 | 0 | InputContext context = newWidget->GetInputContext(); |
617 | 0 | if (context.mIMEState.mEnabled == IMEState::DISABLED && |
618 | 0 | context.mOrigin == InputContext::ORIGIN_CONTENT) { |
619 | 0 | setIMEState = false; |
620 | 0 | MOZ_LOG(sISMLog, LogLevel::Debug, |
621 | 0 | (" OnChangeFocusInternal(), doesn't set IME " |
622 | 0 | "state because focused element (or document) is in a child process " |
623 | 0 | "and the IME state is already disabled by a remote process")); |
624 | 0 | } else { |
625 | 0 | // When new remote process gets focus, we should forget input context |
626 | 0 | // coming from old focused remote process. |
627 | 0 | ResetActiveChildInputContext(); |
628 | 0 | MOZ_LOG(sISMLog, LogLevel::Debug, |
629 | 0 | (" OnChangeFocusInternal(), will disable IME " |
630 | 0 | "until new focused element (or document) in the child process " |
631 | 0 | "will get focus actually")); |
632 | 0 | } |
633 | 0 | } else if (newWidget->GetInputContext().mOrigin != |
634 | 0 | InputContext::ORIGIN_MAIN) { |
635 | 0 | // When focus is NOT changed actually, we shouldn't set IME state if |
636 | 0 | // current input context was set by a remote process since that means |
637 | 0 | // that the window is being activated and the child process may have |
638 | 0 | // composition. Then, we shouldn't commit the composition with making |
639 | 0 | // IME state disabled. |
640 | 0 | setIMEState = false; |
641 | 0 | MOZ_LOG(sISMLog, LogLevel::Debug, |
642 | 0 | (" OnChangeFocusInternal(), doesn't set IME " |
643 | 0 | "state because focused element (or document) is already in the child " |
644 | 0 | "process")); |
645 | 0 | } |
646 | 0 | } else { |
647 | 0 | // When this process gets focus, we should forget input context coming |
648 | 0 | // from remote process. |
649 | 0 | ResetActiveChildInputContext(); |
650 | 0 | } |
651 | 0 |
|
652 | 0 | if (setIMEState) { |
653 | 0 | if (!focusActuallyChanging) { |
654 | 0 | // actual focus isn't changing, but if IME enabled state is changing, |
655 | 0 | // we should do it. |
656 | 0 | InputContext context = newWidget->GetInputContext(); |
657 | 0 | if (context.mIMEState.mEnabled == newState.mEnabled) { |
658 | 0 | MOZ_LOG(sISMLog, LogLevel::Debug, |
659 | 0 | (" OnChangeFocusInternal(), " |
660 | 0 | "neither focus nor IME state is changing")); |
661 | 0 | return NS_OK; |
662 | 0 | } |
663 | 0 | aAction.mFocusChange = InputContextAction::FOCUS_NOT_CHANGED; |
664 | 0 |
|
665 | 0 | // Even if focus isn't changing actually, we should commit current |
666 | 0 | // composition here since the IME state is changing. |
667 | 0 | if (sPresContext && oldWidget && !focusActuallyChanging) { |
668 | 0 | NotifyIME(REQUEST_TO_COMMIT_COMPOSITION, oldWidget, |
669 | 0 | sFocusedIMETabParent); |
670 | 0 | } |
671 | 0 | } else if (aAction.mFocusChange == InputContextAction::FOCUS_NOT_CHANGED) { |
672 | 0 | // If aContent isn't null or aContent is null but editable, somebody gets |
673 | 0 | // focus. |
674 | 0 | bool gotFocus = aContent || (newState.mEnabled == IMEState::ENABLED); |
675 | 0 | aAction.mFocusChange = |
676 | 0 | gotFocus ? InputContextAction::GOT_FOCUS : |
677 | 0 | InputContextAction::LOST_FOCUS; |
678 | 0 | } |
679 | 0 |
|
680 | 0 | if (newTabParent && HasActiveChildSetInputContext() && |
681 | 0 | aAction.mFocusChange == InputContextAction::MENU_LOST_PSEUDO_FOCUS) { |
682 | 0 | // Restore the input context in the active remote process when |
683 | 0 | // menu keyboard listener is uninstalled and active remote tab has |
684 | 0 | // focus. |
685 | 0 | SetInputContext(newWidget, sActiveChildInputContext, aAction); |
686 | 0 | } else { |
687 | 0 | // Update IME state for new focus widget |
688 | 0 | SetIMEState(newState, aPresContext, aContent, newWidget, aAction, |
689 | 0 | newTabParent ? InputContext::ORIGIN_CONTENT : sOrigin); |
690 | 0 | } |
691 | 0 | } |
692 | 0 |
|
693 | 0 | sActiveTabParent = newTabParent; |
694 | 0 | sPresContext = aPresContext; |
695 | 0 | sContent = aContent; |
696 | 0 |
|
697 | 0 | // Don't call CreateIMEContentObserver() here except when a plugin gets |
698 | 0 | // focus because it will be called from the focus event handler of focused |
699 | 0 | // editor. |
700 | 0 | if (newState.mEnabled == IMEState::PLUGIN) { |
701 | 0 | CreateIMEContentObserver(nullptr); |
702 | 0 | if (sActiveIMEContentObserver) { |
703 | 0 | MOZ_LOG(sISMLog, LogLevel::Debug, |
704 | 0 | (" OnChangeFocusInternal(), an " |
705 | 0 | "IMEContentObserver instance is created for plugin and trying to " |
706 | 0 | "flush its pending notifications...")); |
707 | 0 | sActiveIMEContentObserver->TryToFlushPendingNotifications(false); |
708 | 0 | } |
709 | 0 | } |
710 | 0 |
|
711 | 0 | return NS_OK; |
712 | 0 | } |
713 | | |
714 | | // static |
715 | | void |
716 | | IMEStateManager::OnInstalledMenuKeyboardListener(bool aInstalling) |
717 | 0 | { |
718 | 0 | MOZ_LOG(sISMLog, LogLevel::Info, |
719 | 0 | ("OnInstalledMenuKeyboardListener(aInstalling=%s), " |
720 | 0 | "sInstalledMenuKeyboardListener=%s, sActiveTabParent=0x%p, " |
721 | 0 | "sActiveChildInputContext={ mIMEState={ mEnabled=%s, mOpen=%s }, " |
722 | 0 | "mHTMLInputType=\"%s\", mHTMLInputInputmode=\"%s\", mActionHint=\"%s\", " |
723 | 0 | "mInPrivateBrowsing=%s }", |
724 | 0 | GetBoolName(aInstalling), |
725 | 0 | GetBoolName(sInstalledMenuKeyboardListener), |
726 | 0 | sActiveTabParent.get(), |
727 | 0 | GetIMEStateEnabledName(sActiveChildInputContext.mIMEState.mEnabled), |
728 | 0 | GetIMEStateSetOpenName(sActiveChildInputContext.mIMEState.mOpen), |
729 | 0 | NS_ConvertUTF16toUTF8(sActiveChildInputContext.mHTMLInputType).get(), |
730 | 0 | NS_ConvertUTF16toUTF8(sActiveChildInputContext.mHTMLInputInputmode).get(), |
731 | 0 | NS_ConvertUTF16toUTF8(sActiveChildInputContext.mActionHint).get(), |
732 | 0 | GetBoolName(sActiveChildInputContext.mInPrivateBrowsing))); |
733 | 0 |
|
734 | 0 | sInstalledMenuKeyboardListener = aInstalling; |
735 | 0 |
|
736 | 0 | InputContextAction action(InputContextAction::CAUSE_UNKNOWN, |
737 | 0 | aInstalling ? InputContextAction::MENU_GOT_PSEUDO_FOCUS : |
738 | 0 | InputContextAction::MENU_LOST_PSEUDO_FOCUS); |
739 | 0 | OnChangeFocusInternal(sPresContext, sContent, action); |
740 | 0 | } |
741 | | |
742 | | // static |
743 | | bool |
744 | | IMEStateManager::OnMouseButtonEventInEditor(nsPresContext* aPresContext, |
745 | | nsIContent* aContent, |
746 | | WidgetMouseEvent* aMouseEvent) |
747 | 0 | { |
748 | 0 | MOZ_LOG(sISMLog, LogLevel::Info, |
749 | 0 | ("OnMouseButtonEventInEditor(aPresContext=0x%p, " |
750 | 0 | "aContent=0x%p, aMouseEvent=0x%p), sPresContext=0x%p, sContent=0x%p", |
751 | 0 | aPresContext, aContent, aMouseEvent, sPresContext.get(), sContent.get())); |
752 | 0 |
|
753 | 0 | if (NS_WARN_IF(!aMouseEvent)) { |
754 | 0 | MOZ_LOG(sISMLog, LogLevel::Debug, |
755 | 0 | (" OnMouseButtonEventInEditor(), aMouseEvent is nullptr")); |
756 | 0 | return false; |
757 | 0 | } |
758 | 0 |
|
759 | 0 | if (sPresContext != aPresContext || sContent != aContent) { |
760 | 0 | MOZ_LOG(sISMLog, LogLevel::Debug, |
761 | 0 | (" OnMouseButtonEventInEditor(), " |
762 | 0 | "the mouse event isn't fired on the editor managed by ISM")); |
763 | 0 | return false; |
764 | 0 | } |
765 | 0 |
|
766 | 0 | if (!sActiveIMEContentObserver) { |
767 | 0 | MOZ_LOG(sISMLog, LogLevel::Debug, |
768 | 0 | (" OnMouseButtonEventInEditor(), " |
769 | 0 | "there is no active IMEContentObserver")); |
770 | 0 | return false; |
771 | 0 | } |
772 | 0 |
|
773 | 0 | if (!sActiveIMEContentObserver->IsManaging(aPresContext, aContent)) { |
774 | 0 | MOZ_LOG(sISMLog, LogLevel::Debug, |
775 | 0 | (" OnMouseButtonEventInEditor(), " |
776 | 0 | "the active IMEContentObserver isn't managing the editor")); |
777 | 0 | return false; |
778 | 0 | } |
779 | 0 |
|
780 | 0 | bool consumed = |
781 | 0 | sActiveIMEContentObserver->OnMouseButtonEvent(aPresContext, aMouseEvent); |
782 | 0 |
|
783 | 0 | if (MOZ_LOG_TEST(sISMLog, LogLevel::Info)) { |
784 | 0 | nsAutoString eventType; |
785 | 0 | MOZ_LOG(sISMLog, LogLevel::Info, |
786 | 0 | (" OnMouseButtonEventInEditor(), " |
787 | 0 | "mouse event (mMessage=%s, button=%d) is %s", |
788 | 0 | ToChar(aMouseEvent->mMessage), aMouseEvent->button, |
789 | 0 | consumed ? "consumed" : "not consumed")); |
790 | 0 | } |
791 | 0 |
|
792 | 0 | return consumed; |
793 | 0 | } |
794 | | |
795 | | // static |
796 | | void |
797 | | IMEStateManager::OnClickInEditor(nsPresContext* aPresContext, |
798 | | nsIContent* aContent, |
799 | | const WidgetMouseEvent* aMouseEvent) |
800 | 0 | { |
801 | 0 | MOZ_LOG(sISMLog, LogLevel::Info, |
802 | 0 | ("OnClickInEditor(aPresContext=0x%p, aContent=0x%p, aMouseEvent=0x%p), " |
803 | 0 | "sPresContext=0x%p, sContent=0x%p, sWidget=0x%p (available: %s)", |
804 | 0 | aPresContext, aContent, aMouseEvent, sPresContext.get(), sContent.get(), |
805 | 0 | sWidget, GetBoolName(sWidget && !sWidget->Destroyed()))); |
806 | 0 |
|
807 | 0 | if (NS_WARN_IF(!aMouseEvent)) { |
808 | 0 | return; |
809 | 0 | } |
810 | 0 | |
811 | 0 | if (sPresContext != aPresContext || sContent != aContent || |
812 | 0 | NS_WARN_IF(!sPresContext) || NS_WARN_IF(!sWidget) || |
813 | 0 | NS_WARN_IF(sWidget->Destroyed())) { |
814 | 0 | MOZ_LOG(sISMLog, LogLevel::Debug, |
815 | 0 | (" OnClickInEditor(), " |
816 | 0 | "the mouse event isn't fired on the editor managed by ISM")); |
817 | 0 | return; |
818 | 0 | } |
819 | 0 |
|
820 | 0 | nsCOMPtr<nsIWidget> widget(sWidget); |
821 | 0 |
|
822 | 0 | MOZ_ASSERT(!sPresContext->GetRootWidget() || |
823 | 0 | sPresContext->GetRootWidget() == widget); |
824 | 0 |
|
825 | 0 | if (!aMouseEvent->IsTrusted()) { |
826 | 0 | MOZ_LOG(sISMLog, LogLevel::Debug, |
827 | 0 | (" OnClickInEditor(), " |
828 | 0 | "the mouse event isn't a trusted event")); |
829 | 0 | return; // ignore untrusted event. |
830 | 0 | } |
831 | 0 |
|
832 | 0 | if (aMouseEvent->button) { |
833 | 0 | MOZ_LOG(sISMLog, LogLevel::Debug, |
834 | 0 | (" OnClickInEditor(), " |
835 | 0 | "the mouse event isn't a left mouse button event")); |
836 | 0 | return; // not a left click event. |
837 | 0 | } |
838 | 0 |
|
839 | 0 | if (aMouseEvent->mClickCount != 1) { |
840 | 0 | MOZ_LOG(sISMLog, LogLevel::Debug, |
841 | 0 | (" OnClickInEditor(), " |
842 | 0 | "the mouse event isn't a single click event")); |
843 | 0 | return; // should notify only first click event. |
844 | 0 | } |
845 | 0 |
|
846 | 0 | InputContextAction::Cause cause = |
847 | 0 | aMouseEvent->inputSource == MouseEvent_Binding::MOZ_SOURCE_TOUCH ? |
848 | 0 | InputContextAction::CAUSE_TOUCH : InputContextAction::CAUSE_MOUSE; |
849 | 0 |
|
850 | 0 | InputContextAction action(cause, InputContextAction::FOCUS_NOT_CHANGED); |
851 | 0 | IMEState newState = GetNewIMEState(aPresContext, aContent); |
852 | 0 | SetIMEState(newState, aPresContext, aContent, widget, action, sOrigin); |
853 | 0 | } |
854 | | |
855 | | // static |
856 | | void |
857 | | IMEStateManager::OnFocusInEditor(nsPresContext* aPresContext, |
858 | | nsIContent* aContent, |
859 | | EditorBase& aEditorBase) |
860 | 0 | { |
861 | 0 | MOZ_LOG(sISMLog, LogLevel::Info, |
862 | 0 | ("OnFocusInEditor(aPresContext=0x%p, aContent=0x%p, aEditorBase=0x%p), " |
863 | 0 | "sPresContext=0x%p, sContent=0x%p, sActiveIMEContentObserver=0x%p", |
864 | 0 | aPresContext, aContent, &aEditorBase, sPresContext.get(), sContent.get(), |
865 | 0 | sActiveIMEContentObserver.get())); |
866 | 0 |
|
867 | 0 | if (sPresContext != aPresContext || sContent != aContent) { |
868 | 0 | MOZ_LOG(sISMLog, LogLevel::Debug, |
869 | 0 | (" OnFocusInEditor(), " |
870 | 0 | "an editor not managed by ISM gets focus")); |
871 | 0 | return; |
872 | 0 | } |
873 | 0 |
|
874 | 0 | // If the IMEContentObserver instance isn't managing the editor actually, |
875 | 0 | // we need to recreate the instance. |
876 | 0 | if (sActiveIMEContentObserver) { |
877 | 0 | if (sActiveIMEContentObserver->IsManaging(aPresContext, aContent)) { |
878 | 0 | MOZ_LOG(sISMLog, LogLevel::Debug, |
879 | 0 | (" OnFocusInEditor(), " |
880 | 0 | "the editor is already being managed by sActiveIMEContentObserver")); |
881 | 0 | return; |
882 | 0 | } |
883 | 0 | DestroyIMEContentObserver(); |
884 | 0 | } |
885 | 0 |
|
886 | 0 | CreateIMEContentObserver(&aEditorBase); |
887 | 0 |
|
888 | 0 | // Let's flush the focus notification now. |
889 | 0 | if (sActiveIMEContentObserver) { |
890 | 0 | MOZ_LOG(sISMLog, LogLevel::Debug, |
891 | 0 | (" OnFocusInEditor(), new IMEContentObserver is " |
892 | 0 | "created, trying to flush pending notifications...")); |
893 | 0 | sActiveIMEContentObserver->TryToFlushPendingNotifications(false); |
894 | 0 | } |
895 | 0 | } |
896 | | |
897 | | // static |
898 | | void |
899 | | IMEStateManager::OnEditorInitialized(EditorBase& aEditorBase) |
900 | 0 | { |
901 | 0 | if (!sActiveIMEContentObserver || |
902 | 0 | !sActiveIMEContentObserver->WasInitializedWith(aEditorBase)) { |
903 | 0 | return; |
904 | 0 | } |
905 | 0 | |
906 | 0 | MOZ_LOG(sISMLog, LogLevel::Info, |
907 | 0 | ("OnEditorInitialized(aEditorBase=0x%p)", |
908 | 0 | &aEditorBase)); |
909 | 0 |
|
910 | 0 | sActiveIMEContentObserver->UnsuppressNotifyingIME(); |
911 | 0 | } |
912 | | |
913 | | // static |
914 | | void |
915 | | IMEStateManager::OnEditorDestroying(EditorBase& aEditorBase) |
916 | 0 | { |
917 | 0 | if (!sActiveIMEContentObserver || |
918 | 0 | !sActiveIMEContentObserver->WasInitializedWith(aEditorBase)) { |
919 | 0 | return; |
920 | 0 | } |
921 | 0 | |
922 | 0 | MOZ_LOG(sISMLog, LogLevel::Info, |
923 | 0 | ("OnEditorDestroying(aEditorBase=0x%p)", |
924 | 0 | &aEditorBase)); |
925 | 0 |
|
926 | 0 | // The IMEContentObserver shouldn't notify IME of anything until reframing |
927 | 0 | // is finished. |
928 | 0 | sActiveIMEContentObserver->SuppressNotifyingIME(); |
929 | 0 | } |
930 | | |
931 | | // static |
932 | | void |
933 | | IMEStateManager::UpdateIMEState(const IMEState& aNewIMEState, |
934 | | nsIContent* aContent, |
935 | | EditorBase* aEditorBase) |
936 | 0 | { |
937 | 0 | MOZ_LOG(sISMLog, LogLevel::Info, |
938 | 0 | ("UpdateIMEState(aNewIMEState={ mEnabled=%s, " |
939 | 0 | "mOpen=%s }, aContent=0x%p, aEditorBase=0x%p), " |
940 | 0 | "sPresContext=0x%p, sContent=0x%p, sWidget=0x%p (available: %s), " |
941 | 0 | "sActiveIMEContentObserver=0x%p, sIsGettingNewIMEState=%s", |
942 | 0 | GetIMEStateEnabledName(aNewIMEState.mEnabled), |
943 | 0 | GetIMEStateSetOpenName(aNewIMEState.mOpen), aContent, aEditorBase, |
944 | 0 | sPresContext.get(), sContent.get(), |
945 | 0 | sWidget, GetBoolName(sWidget && !sWidget->Destroyed()), |
946 | 0 | sActiveIMEContentObserver.get(), |
947 | 0 | GetBoolName(sIsGettingNewIMEState))); |
948 | 0 |
|
949 | 0 | if (sIsGettingNewIMEState) { |
950 | 0 | MOZ_LOG(sISMLog, LogLevel::Debug, |
951 | 0 | (" UpdateIMEState(), " |
952 | 0 | "does nothing because of called while getting new IME state")); |
953 | 0 | return; |
954 | 0 | } |
955 | 0 |
|
956 | 0 | nsCOMPtr<nsIPresShell> presShell; |
957 | 0 | if (!aEditorBase) { |
958 | 0 | MOZ_ASSERT(aContent, "we must have content"); |
959 | 0 | nsIDocument* doc = aContent->OwnerDoc(); |
960 | 0 | presShell = doc->GetShell(); |
961 | 0 | } else { |
962 | 0 | presShell = aEditorBase->GetPresShell(); |
963 | 0 | } |
964 | 0 |
|
965 | 0 | if (NS_WARN_IF(!presShell)) { |
966 | 0 | MOZ_LOG(sISMLog, LogLevel::Error, |
967 | 0 | (" UpdateIMEState(), FAILED due to " |
968 | 0 | "editor doesn't have PresShell")); |
969 | 0 | return; |
970 | 0 | } |
971 | 0 |
|
972 | 0 | nsPresContext* presContext = presShell->GetPresContext(); |
973 | 0 | if (NS_WARN_IF(!presContext)) { |
974 | 0 | MOZ_LOG(sISMLog, LogLevel::Error, |
975 | 0 | (" UpdateIMEState(), FAILED due to " |
976 | 0 | "editor doesn't have PresContext")); |
977 | 0 | return; |
978 | 0 | } |
979 | 0 |
|
980 | 0 | // IMEStateManager::UpdateIMEState() should be called after |
981 | 0 | // IMEStateManager::OnChangeFocus() is called for setting focus to aContent |
982 | 0 | // and aEditorBase. However, when aEditorBase is an HTMLEditor, this may be |
983 | 0 | // called by nsIEditor::PostCreate() before IMEStateManager::OnChangeFocus(). |
984 | 0 | // Similarly, when aEditorBase is a TextEditor, this may be called by |
985 | 0 | // nsIEditor::SetFlags(). In such cases, this method should do nothing |
986 | 0 | // because input context should be updated when |
987 | 0 | // IMEStateManager::OnChangeFocus() is called later. |
988 | 0 | if (sPresContext != presContext) { |
989 | 0 | MOZ_LOG(sISMLog, LogLevel::Warning, |
990 | 0 | (" UpdateIMEState(), does nothing due to " |
991 | 0 | "the editor hasn't managed by IMEStateManager yet")); |
992 | 0 | return; |
993 | 0 | } |
994 | 0 |
|
995 | 0 | // If IMEStateManager doesn't manage any document, this cannot update IME |
996 | 0 | // state of any widget. |
997 | 0 | if (NS_WARN_IF(!sPresContext)) { |
998 | 0 | MOZ_LOG(sISMLog, LogLevel::Error, |
999 | 0 | (" UpdateIMEState(), FAILED due to " |
1000 | 0 | "no managing nsPresContext")); |
1001 | 0 | return; |
1002 | 0 | } |
1003 | 0 |
|
1004 | 0 | if (NS_WARN_IF(!sWidget) || NS_WARN_IF(sWidget->Destroyed())) { |
1005 | 0 | MOZ_LOG(sISMLog, LogLevel::Error, |
1006 | 0 | (" UpdateIMEState(), FAILED due to " |
1007 | 0 | "the widget for the managing nsPresContext has gone")); |
1008 | 0 | return; |
1009 | 0 | } |
1010 | 0 |
|
1011 | 0 | nsCOMPtr<nsIWidget> widget(sWidget); |
1012 | 0 |
|
1013 | 0 | MOZ_ASSERT(!sPresContext->GetRootWidget() || |
1014 | 0 | sPresContext->GetRootWidget() == widget); |
1015 | 0 |
|
1016 | 0 | // Even if there is active IMEContentObserver, it may not be observing the |
1017 | 0 | // editor with current editable root content due to reframed. In such case, |
1018 | 0 | // We should try to reinitialize the IMEContentObserver. |
1019 | 0 | if (sActiveIMEContentObserver && IsIMEObserverNeeded(aNewIMEState)) { |
1020 | 0 | MOZ_LOG(sISMLog, LogLevel::Debug, |
1021 | 0 | (" UpdateIMEState(), try to reinitialize the " |
1022 | 0 | "active IMEContentObserver")); |
1023 | 0 | RefPtr<IMEContentObserver> contentObserver = sActiveIMEContentObserver; |
1024 | 0 | if (!contentObserver->MaybeReinitialize(widget, sPresContext, |
1025 | 0 | aContent, aEditorBase)) { |
1026 | 0 | MOZ_LOG(sISMLog, LogLevel::Error, |
1027 | 0 | (" UpdateIMEState(), failed to reinitialize the " |
1028 | 0 | "active IMEContentObserver")); |
1029 | 0 | } |
1030 | 0 | if (NS_WARN_IF(widget->Destroyed())) { |
1031 | 0 | MOZ_LOG(sISMLog, LogLevel::Error, |
1032 | 0 | (" UpdateIMEState(), widget has gone during reinitializing the " |
1033 | 0 | "active IMEContentObserver")); |
1034 | 0 | return; |
1035 | 0 | } |
1036 | 0 | } |
1037 | 0 |
|
1038 | 0 | // If there is no active IMEContentObserver or it isn't observing the |
1039 | 0 | // editor correctly, we should recreate it. |
1040 | 0 | bool createTextStateManager = |
1041 | 0 | (!sActiveIMEContentObserver || |
1042 | 0 | !sActiveIMEContentObserver->IsManaging(sPresContext, aContent)); |
1043 | 0 |
|
1044 | 0 | bool updateIMEState = |
1045 | 0 | (widget->GetInputContext().mIMEState.mEnabled != aNewIMEState.mEnabled); |
1046 | 0 | if (NS_WARN_IF(widget->Destroyed())) { |
1047 | 0 | MOZ_LOG(sISMLog, LogLevel::Error, |
1048 | 0 | (" UpdateIMEState(), widget has gone during getting input context")); |
1049 | 0 | return; |
1050 | 0 | } |
1051 | 0 |
|
1052 | 0 | if (updateIMEState) { |
1053 | 0 | // commit current composition before modifying IME state. |
1054 | 0 | NotifyIME(REQUEST_TO_COMMIT_COMPOSITION, widget, sFocusedIMETabParent); |
1055 | 0 | if (NS_WARN_IF(widget->Destroyed())) { |
1056 | 0 | MOZ_LOG(sISMLog, LogLevel::Error, |
1057 | 0 | (" UpdateIMEState(), widget has gone during committing composition")); |
1058 | 0 | return; |
1059 | 0 | } |
1060 | 0 | } |
1061 | 0 |
|
1062 | 0 | if (createTextStateManager) { |
1063 | 0 | DestroyIMEContentObserver(); |
1064 | 0 | } |
1065 | 0 |
|
1066 | 0 | if (updateIMEState) { |
1067 | 0 | InputContextAction action(InputContextAction::CAUSE_UNKNOWN, |
1068 | 0 | InputContextAction::FOCUS_NOT_CHANGED); |
1069 | 0 | SetIMEState(aNewIMEState, presContext, aContent, widget, action, sOrigin); |
1070 | 0 | if (NS_WARN_IF(widget->Destroyed())) { |
1071 | 0 | MOZ_LOG(sISMLog, LogLevel::Error, |
1072 | 0 | (" UpdateIMEState(), widget has gone during setting input context")); |
1073 | 0 | return; |
1074 | 0 | } |
1075 | 0 | } |
1076 | 0 |
|
1077 | 0 | if (createTextStateManager) { |
1078 | 0 | // XXX In this case, it might not be enough safe to notify IME of anything. |
1079 | 0 | // So, don't try to flush pending notifications of IMEContentObserver |
1080 | 0 | // here. |
1081 | 0 | CreateIMEContentObserver(aEditorBase); |
1082 | 0 | } |
1083 | 0 | } |
1084 | | |
1085 | | // static |
1086 | | IMEState |
1087 | | IMEStateManager::GetNewIMEState(nsPresContext* aPresContext, |
1088 | | nsIContent* aContent) |
1089 | 0 | { |
1090 | 0 | MOZ_LOG(sISMLog, LogLevel::Info, |
1091 | 0 | ("GetNewIMEState(aPresContext=0x%p, aContent=0x%p), " |
1092 | 0 | "sInstalledMenuKeyboardListener=%s", |
1093 | 0 | aPresContext, aContent, GetBoolName(sInstalledMenuKeyboardListener))); |
1094 | 0 |
|
1095 | 0 | if (!CanHandleWith(aPresContext)) { |
1096 | 0 | MOZ_LOG(sISMLog, LogLevel::Debug, |
1097 | 0 | (" GetNewIMEState() returns DISABLED because " |
1098 | 0 | "the nsPresContext has been destroyed")); |
1099 | 0 | return IMEState(IMEState::DISABLED); |
1100 | 0 | } |
1101 | 0 |
|
1102 | 0 | // On Printing or Print Preview, we don't need IME. |
1103 | 0 | if (aPresContext->Type() == nsPresContext::eContext_PrintPreview || |
1104 | 0 | aPresContext->Type() == nsPresContext::eContext_Print) { |
1105 | 0 | MOZ_LOG(sISMLog, LogLevel::Debug, |
1106 | 0 | (" GetNewIMEState() returns DISABLED because " |
1107 | 0 | "the nsPresContext is for print or print preview")); |
1108 | 0 | return IMEState(IMEState::DISABLED); |
1109 | 0 | } |
1110 | 0 |
|
1111 | 0 | if (sInstalledMenuKeyboardListener) { |
1112 | 0 | MOZ_LOG(sISMLog, LogLevel::Debug, |
1113 | 0 | (" GetNewIMEState() returns DISABLED because " |
1114 | 0 | "menu keyboard listener was installed")); |
1115 | 0 | return IMEState(IMEState::DISABLED); |
1116 | 0 | } |
1117 | 0 |
|
1118 | 0 | if (!aContent) { |
1119 | 0 | // Even if there are no focused content, the focused document might be |
1120 | 0 | // editable, such case is design mode. |
1121 | 0 | nsIDocument* doc = aPresContext->Document(); |
1122 | 0 | if (doc && doc->HasFlag(NODE_IS_EDITABLE)) { |
1123 | 0 | MOZ_LOG(sISMLog, LogLevel::Debug, |
1124 | 0 | (" GetNewIMEState() returns ENABLED because " |
1125 | 0 | "design mode editor has focus")); |
1126 | 0 | return IMEState(IMEState::ENABLED); |
1127 | 0 | } |
1128 | 0 | MOZ_LOG(sISMLog, LogLevel::Debug, |
1129 | 0 | (" GetNewIMEState() returns DISABLED because " |
1130 | 0 | "no content has focus")); |
1131 | 0 | return IMEState(IMEState::DISABLED); |
1132 | 0 | } |
1133 | 0 | |
1134 | 0 | // nsIContent::GetDesiredIMEState() may cause a call of UpdateIMEState() |
1135 | 0 | // from EditorBase::PostCreate() because GetDesiredIMEState() needs to |
1136 | 0 | // retrieve an editor instance for the element if it's editable element. |
1137 | 0 | // For avoiding such nested IME state updates, we should set |
1138 | 0 | // sIsGettingNewIMEState here and UpdateIMEState() should check it. |
1139 | 0 | GettingNewIMEStateBlocker blocker; |
1140 | 0 |
|
1141 | 0 | IMEState newIMEState = aContent->GetDesiredIMEState(); |
1142 | 0 | MOZ_LOG(sISMLog, LogLevel::Debug, |
1143 | 0 | (" GetNewIMEState() returns { mEnabled=%s, " |
1144 | 0 | "mOpen=%s }", |
1145 | 0 | GetIMEStateEnabledName(newIMEState.mEnabled), |
1146 | 0 | GetIMEStateSetOpenName(newIMEState.mOpen))); |
1147 | 0 | return newIMEState; |
1148 | 0 | } |
1149 | | |
1150 | | static bool |
1151 | | MayBeIMEUnawareWebApp(nsINode* aNode) |
1152 | 0 | { |
1153 | 0 | bool haveKeyEventsListener = false; |
1154 | 0 |
|
1155 | 0 | while (aNode) { |
1156 | 0 | EventListenerManager* const mgr = aNode->GetExistingListenerManager(); |
1157 | 0 | if (mgr) { |
1158 | 0 | if (mgr->MayHaveInputOrCompositionEventListener()) { |
1159 | 0 | return false; |
1160 | 0 | } |
1161 | 0 | haveKeyEventsListener |= mgr->MayHaveKeyEventListener(); |
1162 | 0 | } |
1163 | 0 | aNode = aNode->GetParentNode(); |
1164 | 0 | } |
1165 | 0 |
|
1166 | 0 | return haveKeyEventsListener; |
1167 | 0 | } |
1168 | | |
1169 | | // static |
1170 | | void |
1171 | | IMEStateManager::ResetActiveChildInputContext() |
1172 | 3 | { |
1173 | 3 | sActiveChildInputContext.mIMEState.mEnabled = IMEState::UNKNOWN; |
1174 | 3 | } |
1175 | | |
1176 | | // static |
1177 | | bool |
1178 | | IMEStateManager::HasActiveChildSetInputContext() |
1179 | 0 | { |
1180 | 0 | return sActiveChildInputContext.mIMEState.mEnabled != IMEState::UNKNOWN; |
1181 | 0 | } |
1182 | | |
1183 | | // static |
1184 | | void |
1185 | | IMEStateManager::SetInputContextForChildProcess( |
1186 | | TabParent* aTabParent, |
1187 | | const InputContext& aInputContext, |
1188 | | const InputContextAction& aAction) |
1189 | 0 | { |
1190 | 0 | MOZ_LOG(sISMLog, LogLevel::Info, |
1191 | 0 | ("SetInputContextForChildProcess(aTabParent=0x%p, " |
1192 | 0 | "aInputContext={ mIMEState={ mEnabled=%s, mOpen=%s }, " |
1193 | 0 | "mHTMLInputType=\"%s\", mHTMLInputInputmode=\"%s\", mActionHint=\"%s\", " |
1194 | 0 | "mInPrivateBrowsing=%s }, aAction={ mCause=%s, mAction=%s }), " |
1195 | 0 | "sPresContext=0x%p (available: %s), sWidget=0x%p (available: %s), " |
1196 | 0 | "sActiveTabParent=0x%p, sInstalledMenuKeyboardListener=%s", |
1197 | 0 | aTabParent, GetIMEStateEnabledName(aInputContext.mIMEState.mEnabled), |
1198 | 0 | GetIMEStateSetOpenName(aInputContext.mIMEState.mOpen), |
1199 | 0 | NS_ConvertUTF16toUTF8(aInputContext.mHTMLInputType).get(), |
1200 | 0 | NS_ConvertUTF16toUTF8(aInputContext.mHTMLInputInputmode).get(), |
1201 | 0 | NS_ConvertUTF16toUTF8(aInputContext.mActionHint).get(), |
1202 | 0 | GetBoolName(aInputContext.mInPrivateBrowsing), |
1203 | 0 | GetActionCauseName(aAction.mCause), |
1204 | 0 | GetActionFocusChangeName(aAction.mFocusChange), |
1205 | 0 | sPresContext.get(), GetBoolName(CanHandleWith(sPresContext)), |
1206 | 0 | sWidget, GetBoolName(sWidget && !sWidget->Destroyed()), |
1207 | 0 | sActiveTabParent.get(), GetBoolName(sInstalledMenuKeyboardListener))); |
1208 | 0 |
|
1209 | 0 | if (aTabParent != sActiveTabParent) { |
1210 | 0 | MOZ_LOG(sISMLog, LogLevel::Error, |
1211 | 0 | (" SetInputContextForChildProcess(), FAILED, " |
1212 | 0 | "because non-focused tab parent tries to set input context")); |
1213 | 0 | return; |
1214 | 0 | } |
1215 | 0 |
|
1216 | 0 | if (NS_WARN_IF(!CanHandleWith(sPresContext))) { |
1217 | 0 | MOZ_LOG(sISMLog, LogLevel::Error, |
1218 | 0 | (" SetInputContextForChildProcess(), FAILED, " |
1219 | 0 | "due to no focused presContext")); |
1220 | 0 | return; |
1221 | 0 | } |
1222 | 0 |
|
1223 | 0 | if (NS_WARN_IF(!sWidget) || NS_WARN_IF(sWidget->Destroyed())) { |
1224 | 0 | MOZ_LOG(sISMLog, LogLevel::Error, |
1225 | 0 | (" SetInputContextForChildProcess(), FAILED, " |
1226 | 0 | "due to the widget for the nsPresContext has gone")); |
1227 | 0 | return; |
1228 | 0 | } |
1229 | 0 |
|
1230 | 0 | nsCOMPtr<nsIWidget> widget(sWidget); |
1231 | 0 |
|
1232 | 0 | MOZ_ASSERT(!sPresContext->GetRootWidget() || |
1233 | 0 | sPresContext->GetRootWidget() == widget); |
1234 | 0 | MOZ_ASSERT(aInputContext.mOrigin == InputContext::ORIGIN_CONTENT); |
1235 | 0 |
|
1236 | 0 | sActiveChildInputContext = aInputContext; |
1237 | 0 | MOZ_ASSERT(HasActiveChildSetInputContext()); |
1238 | 0 |
|
1239 | 0 | // If input context is changed in remote process while menu keyboard listener |
1240 | 0 | // is installed, this process shouldn't set input context now. When it's |
1241 | 0 | // uninstalled, input context should be restored from |
1242 | 0 | // sActiveChildInputContext. |
1243 | 0 | if (sInstalledMenuKeyboardListener) { |
1244 | 0 | MOZ_LOG(sISMLog, LogLevel::Info, |
1245 | 0 | (" SetInputContextForChildProcess(), waiting to set input context " |
1246 | 0 | "until menu keyboard listener is uninstalled")); |
1247 | 0 | return; |
1248 | 0 | } |
1249 | 0 |
|
1250 | 0 | SetInputContext(widget, aInputContext, aAction); |
1251 | 0 | } |
1252 | | |
1253 | | // static |
1254 | | void |
1255 | | IMEStateManager::SetIMEState(const IMEState& aState, |
1256 | | nsPresContext* aPresContext, |
1257 | | nsIContent* aContent, |
1258 | | nsIWidget* aWidget, |
1259 | | InputContextAction aAction, |
1260 | | InputContext::Origin aOrigin) |
1261 | 0 | { |
1262 | 0 | MOZ_LOG(sISMLog, LogLevel::Info, |
1263 | 0 | ("SetIMEState(aState={ mEnabled=%s, mOpen=%s }, " |
1264 | 0 | "aContent=0x%p (TabParent=0x%p), aWidget=0x%p, aAction={ mCause=%s, " |
1265 | 0 | "mFocusChange=%s }, aOrigin=%s)", |
1266 | 0 | GetIMEStateEnabledName(aState.mEnabled), |
1267 | 0 | GetIMEStateSetOpenName(aState.mOpen), aContent, |
1268 | 0 | TabParent::GetFrom(aContent), aWidget, |
1269 | 0 | GetActionCauseName(aAction.mCause), |
1270 | 0 | GetActionFocusChangeName(aAction.mFocusChange), |
1271 | 0 | ToChar(aOrigin))); |
1272 | 0 |
|
1273 | 0 | NS_ENSURE_TRUE_VOID(aWidget); |
1274 | 0 |
|
1275 | 0 | InputContext context; |
1276 | 0 | context.mIMEState = aState; |
1277 | 0 | context.mOrigin = aOrigin; |
1278 | 0 | context.mMayBeIMEUnaware = context.mIMEState.IsEditable() && |
1279 | 0 | sCheckForIMEUnawareWebApps && MayBeIMEUnawareWebApp(aContent); |
1280 | 0 |
|
1281 | 0 | context.mHasHandledUserInput = |
1282 | 0 | aPresContext && aPresContext->PresShell()->HasHandledUserInput(); |
1283 | 0 |
|
1284 | 0 | context.mInPrivateBrowsing = |
1285 | 0 | aPresContext && |
1286 | 0 | nsContentUtils::IsInPrivateBrowsing(aPresContext->Document()); |
1287 | 0 |
|
1288 | 0 | if (aContent && |
1289 | 0 | aContent->IsAnyOfHTMLElements(nsGkAtoms::input, nsGkAtoms::textarea)) { |
1290 | 0 | if (!aContent->IsHTMLElement(nsGkAtoms::textarea)) { |
1291 | 0 | // <input type=number> has an anonymous <input type=text> descendant |
1292 | 0 | // that gets focus whenever anyone tries to focus the number control. We |
1293 | 0 | // need to check if aContent is one of those anonymous text controls and, |
1294 | 0 | // if so, use the number control instead: |
1295 | 0 | Element* element = aContent->AsElement(); |
1296 | 0 | HTMLInputElement* inputElement = |
1297 | 0 | HTMLInputElement::FromNodeOrNull(aContent); |
1298 | 0 | if (inputElement) { |
1299 | 0 | HTMLInputElement* ownerNumberControl = |
1300 | 0 | inputElement->GetOwnerNumberControl(); |
1301 | 0 | if (ownerNumberControl) { |
1302 | 0 | element = ownerNumberControl; // an <input type=number> |
1303 | 0 | } |
1304 | 0 | } |
1305 | 0 | element->GetAttr(kNameSpaceID_None, nsGkAtoms::type, |
1306 | 0 | context.mHTMLInputType); |
1307 | 0 | } else { |
1308 | 0 | context.mHTMLInputType.Assign(nsGkAtoms::textarea->GetUTF16String()); |
1309 | 0 | } |
1310 | 0 |
|
1311 | 0 | if (sInputModeSupported || |
1312 | 0 | nsContentUtils::IsChromeDoc(aContent->OwnerDoc())) { |
1313 | 0 | aContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::inputmode, |
1314 | 0 | context.mHTMLInputInputmode); |
1315 | 0 | if (context.mHTMLInputInputmode.EqualsLiteral("mozAwesomebar") && |
1316 | 0 | !nsContentUtils::IsChromeDoc(aContent->OwnerDoc())) { |
1317 | 0 | // mozAwesomebar should be allowed only in chrome |
1318 | 0 | context.mHTMLInputInputmode.Truncate(); |
1319 | 0 | } |
1320 | 0 | } |
1321 | 0 |
|
1322 | 0 | aContent->AsElement()->GetAttr(kNameSpaceID_None, |
1323 | 0 | nsGkAtoms::moz_action_hint, |
1324 | 0 | context.mActionHint); |
1325 | 0 |
|
1326 | 0 | // Get the input content corresponding to the focused node, |
1327 | 0 | // which may be an anonymous child of the input content. |
1328 | 0 | nsIContent* inputContent = aContent->FindFirstNonChromeOnlyAccessContent(); |
1329 | 0 |
|
1330 | 0 | // If we don't have an action hint and |
1331 | 0 | // return won't submit the form, use "next". |
1332 | 0 | if (context.mActionHint.IsEmpty() && |
1333 | 0 | inputContent->IsHTMLElement(nsGkAtoms::input)) { |
1334 | 0 | bool willSubmit = false; |
1335 | 0 | nsCOMPtr<nsIFormControl> control(do_QueryInterface(inputContent)); |
1336 | 0 | mozilla::dom::Element* formElement = nullptr; |
1337 | 0 | nsCOMPtr<nsIForm> form; |
1338 | 0 | if (control) { |
1339 | 0 | formElement = control->GetFormElement(); |
1340 | 0 | // is this a form and does it have a default submit element? |
1341 | 0 | if ((form = do_QueryInterface(formElement)) && |
1342 | 0 | form->GetDefaultSubmitElement()) { |
1343 | 0 | willSubmit = true; |
1344 | 0 | // is this an html form... |
1345 | 0 | } else if (formElement && formElement->IsHTMLElement(nsGkAtoms::form)) { |
1346 | 0 | dom::HTMLFormElement* htmlForm = |
1347 | 0 | static_cast<dom::HTMLFormElement*>(formElement); |
1348 | 0 | // ... and does it only have a single text input element ? |
1349 | 0 | if (!htmlForm->ImplicitSubmissionIsDisabled() || |
1350 | 0 | // ... or is this the last non-disabled element? |
1351 | 0 | htmlForm->IsLastActiveElement(control)) { |
1352 | 0 | willSubmit = true; |
1353 | 0 | } |
1354 | 0 | } |
1355 | 0 | } |
1356 | 0 | context.mActionHint.Assign( |
1357 | 0 | willSubmit ? (control->ControlType() == NS_FORM_INPUT_SEARCH ? |
1358 | 0 | NS_LITERAL_STRING("search") : NS_LITERAL_STRING("go")) : |
1359 | 0 | (formElement ? |
1360 | 0 | NS_LITERAL_STRING("next") : EmptyString())); |
1361 | 0 | } |
1362 | 0 | } |
1363 | 0 |
|
1364 | 0 | // XXX I think that we should use nsContentUtils::IsCallerChrome() instead |
1365 | 0 | // of the process type. |
1366 | 0 | if (aAction.mCause == InputContextAction::CAUSE_UNKNOWN && |
1367 | 0 | !XRE_IsContentProcess()) { |
1368 | 0 | aAction.mCause = InputContextAction::CAUSE_UNKNOWN_CHROME; |
1369 | 0 | } |
1370 | 0 |
|
1371 | 0 | if ((aAction.mCause == InputContextAction::CAUSE_UNKNOWN || |
1372 | 0 | aAction.mCause == InputContextAction::CAUSE_UNKNOWN_CHROME) && |
1373 | 0 | EventStateManager::IsHandlingUserInput()) { |
1374 | 0 | aAction.mCause = EventStateManager::IsHandlingKeyboardInput() ? |
1375 | 0 | InputContextAction::CAUSE_UNKNOWN_DURING_KEYBOARD_INPUT : |
1376 | 0 | InputContextAction::CAUSE_UNKNOWN_DURING_NON_KEYBOARD_INPUT; |
1377 | 0 | } |
1378 | 0 |
|
1379 | 0 | SetInputContext(aWidget, context, aAction); |
1380 | 0 | } |
1381 | | |
1382 | | // static |
1383 | | void |
1384 | | IMEStateManager::SetInputContext(nsIWidget* aWidget, |
1385 | | const InputContext& aInputContext, |
1386 | | const InputContextAction& aAction) |
1387 | 0 | { |
1388 | 0 | MOZ_LOG(sISMLog, LogLevel::Info, |
1389 | 0 | ("SetInputContext(aWidget=0x%p, aInputContext={ " |
1390 | 0 | "mIMEState={ mEnabled=%s, mOpen=%s }, mHTMLInputType=\"%s\", " |
1391 | 0 | "mHTMLInputInputmode=\"%s\", mActionHint=\"%s\", " |
1392 | 0 | "mInPrivateBrowsing=%s }, " |
1393 | 0 | "aAction={ mCause=%s, mAction=%s }), sActiveTabParent=0x%p", |
1394 | 0 | aWidget, |
1395 | 0 | GetIMEStateEnabledName(aInputContext.mIMEState.mEnabled), |
1396 | 0 | GetIMEStateSetOpenName(aInputContext.mIMEState.mOpen), |
1397 | 0 | NS_ConvertUTF16toUTF8(aInputContext.mHTMLInputType).get(), |
1398 | 0 | NS_ConvertUTF16toUTF8(aInputContext.mHTMLInputInputmode).get(), |
1399 | 0 | NS_ConvertUTF16toUTF8(aInputContext.mActionHint).get(), |
1400 | 0 | GetBoolName(aInputContext.mInPrivateBrowsing), |
1401 | 0 | GetActionCauseName(aAction.mCause), |
1402 | 0 | GetActionFocusChangeName(aAction.mFocusChange), |
1403 | 0 | sActiveTabParent.get())); |
1404 | 0 |
|
1405 | 0 | MOZ_RELEASE_ASSERT(aWidget); |
1406 | 0 |
|
1407 | 0 | nsCOMPtr<nsIWidget> widget(aWidget); |
1408 | 0 | widget->SetInputContext(aInputContext, aAction); |
1409 | 0 | sActiveInputContextWidget = widget; |
1410 | 0 | } |
1411 | | |
1412 | | // static |
1413 | | void |
1414 | | IMEStateManager::EnsureTextCompositionArray() |
1415 | 0 | { |
1416 | 0 | if (sTextCompositions) { |
1417 | 0 | return; |
1418 | 0 | } |
1419 | 0 | sTextCompositions = new TextCompositionArray(); |
1420 | 0 | } |
1421 | | |
1422 | | // static |
1423 | | void |
1424 | | IMEStateManager::DispatchCompositionEvent( |
1425 | | nsINode* aEventTargetNode, |
1426 | | nsPresContext* aPresContext, |
1427 | | WidgetCompositionEvent* aCompositionEvent, |
1428 | | nsEventStatus* aStatus, |
1429 | | EventDispatchingCallback* aCallBack, |
1430 | | bool aIsSynthesized) |
1431 | 0 | { |
1432 | 0 | RefPtr<TabParent> tabParent = |
1433 | 0 | aEventTargetNode->IsContent() ? |
1434 | 0 | TabParent::GetFrom(aEventTargetNode->AsContent()) : nullptr; |
1435 | 0 |
|
1436 | 0 | MOZ_LOG(sISMLog, LogLevel::Info, |
1437 | 0 | ("DispatchCompositionEvent(aNode=0x%p, " |
1438 | 0 | "aPresContext=0x%p, aCompositionEvent={ mMessage=%s, " |
1439 | 0 | "mNativeIMEContext={ mRawNativeIMEContext=0x%" PRIXPTR ", " |
1440 | 0 | "mOriginProcessID=0x%" PRIX64 " }, mWidget(0x%p)={ " |
1441 | 0 | "GetNativeIMEContext()={ mRawNativeIMEContext=0x%" PRIXPTR ", " |
1442 | 0 | "mOriginProcessID=0x%" PRIX64 " }, Destroyed()=%s }, " |
1443 | 0 | "mFlags={ mIsTrusted=%s, mPropagationStopped=%s } }, " |
1444 | 0 | "aIsSynthesized=%s), tabParent=%p", |
1445 | 0 | aEventTargetNode, aPresContext, |
1446 | 0 | ToChar(aCompositionEvent->mMessage), |
1447 | 0 | aCompositionEvent->mNativeIMEContext.mRawNativeIMEContext, |
1448 | 0 | aCompositionEvent->mNativeIMEContext.mOriginProcessID, |
1449 | 0 | aCompositionEvent->mWidget.get(), |
1450 | 0 | aCompositionEvent->mWidget->GetNativeIMEContext().mRawNativeIMEContext, |
1451 | 0 | aCompositionEvent->mWidget->GetNativeIMEContext().mOriginProcessID, |
1452 | 0 | GetBoolName(aCompositionEvent->mWidget->Destroyed()), |
1453 | 0 | GetBoolName(aCompositionEvent->mFlags.mIsTrusted), |
1454 | 0 | GetBoolName(aCompositionEvent->mFlags.mPropagationStopped), |
1455 | 0 | GetBoolName(aIsSynthesized), tabParent.get())); |
1456 | 0 |
|
1457 | 0 | if (!aCompositionEvent->IsTrusted() || |
1458 | 0 | aCompositionEvent->PropagationStopped()) { |
1459 | 0 | return; |
1460 | 0 | } |
1461 | 0 | |
1462 | 0 | MOZ_ASSERT(aCompositionEvent->mMessage != eCompositionUpdate, |
1463 | 0 | "compositionupdate event shouldn't be dispatched manually"); |
1464 | 0 |
|
1465 | 0 | EnsureTextCompositionArray(); |
1466 | 0 |
|
1467 | 0 | RefPtr<TextComposition> composition = |
1468 | 0 | sTextCompositions->GetCompositionFor(aCompositionEvent); |
1469 | 0 | if (!composition) { |
1470 | 0 | // If synthesized event comes after delayed native composition events |
1471 | 0 | // for request of commit or cancel, we should ignore it. |
1472 | 0 | if (NS_WARN_IF(aIsSynthesized)) { |
1473 | 0 | return; |
1474 | 0 | } |
1475 | 0 | MOZ_LOG(sISMLog, LogLevel::Debug, |
1476 | 0 | (" DispatchCompositionEvent(), " |
1477 | 0 | "adding new TextComposition to the array")); |
1478 | 0 | MOZ_ASSERT(aCompositionEvent->mMessage == eCompositionStart); |
1479 | 0 | composition = |
1480 | 0 | new TextComposition(aPresContext, aEventTargetNode, tabParent, |
1481 | 0 | aCompositionEvent); |
1482 | 0 | sTextCompositions->AppendElement(composition); |
1483 | 0 | } |
1484 | | #ifdef DEBUG |
1485 | | else { |
1486 | | MOZ_ASSERT(aCompositionEvent->mMessage != eCompositionStart); |
1487 | | } |
1488 | | #endif // #ifdef DEBUG |
1489 | |
|
1490 | 0 | // Dispatch the event on composing target. |
1491 | 0 | composition->DispatchCompositionEvent(aCompositionEvent, aStatus, aCallBack, |
1492 | 0 | aIsSynthesized); |
1493 | 0 |
|
1494 | 0 | // WARNING: the |composition| might have been destroyed already. |
1495 | 0 |
|
1496 | 0 | // Remove the ended composition from the array. |
1497 | 0 | // NOTE: When TextComposition is synthesizing compositionend event for |
1498 | 0 | // emulating a commit, the instance shouldn't be removed from the array |
1499 | 0 | // because IME may perform it later. Then, we need to ignore the |
1500 | 0 | // following commit events in TextComposition::DispatchEvent(). |
1501 | 0 | // However, if commit or cancel for a request is performed synchronously |
1502 | 0 | // during not safe to dispatch events, PresShell must have discarded |
1503 | 0 | // compositionend event. Then, the synthesized compositionend event is |
1504 | 0 | // the last event for the composition. In this case, we need to |
1505 | 0 | // destroy the TextComposition with synthesized compositionend event. |
1506 | 0 | if ((!aIsSynthesized || |
1507 | 0 | composition->WasNativeCompositionEndEventDiscarded()) && |
1508 | 0 | aCompositionEvent->CausesDOMCompositionEndEvent()) { |
1509 | 0 | TextCompositionArray::index_type i = |
1510 | 0 | sTextCompositions->IndexOf(aCompositionEvent->mWidget); |
1511 | 0 | if (i != TextCompositionArray::NoIndex) { |
1512 | 0 | MOZ_LOG(sISMLog, LogLevel::Debug, |
1513 | 0 | (" DispatchCompositionEvent(), " |
1514 | 0 | "removing TextComposition from the array since NS_COMPOSTION_END " |
1515 | 0 | "was dispatched")); |
1516 | 0 | sTextCompositions->ElementAt(i)->Destroy(); |
1517 | 0 | sTextCompositions->RemoveElementAt(i); |
1518 | 0 | } |
1519 | 0 | } |
1520 | 0 | } |
1521 | | |
1522 | | // static |
1523 | | IMEContentObserver* |
1524 | | IMEStateManager::GetActiveContentObserver() |
1525 | 0 | { |
1526 | 0 | return sActiveIMEContentObserver; |
1527 | 0 | } |
1528 | | |
1529 | | // static |
1530 | | nsIContent* |
1531 | | IMEStateManager::GetRootContent(nsPresContext* aPresContext) |
1532 | 0 | { |
1533 | 0 | nsIDocument* doc = aPresContext->Document(); |
1534 | 0 | if (NS_WARN_IF(!doc)) { |
1535 | 0 | return nullptr; |
1536 | 0 | } |
1537 | 0 | return doc->GetRootElement(); |
1538 | 0 | } |
1539 | | |
1540 | | // static |
1541 | | void |
1542 | | IMEStateManager::HandleSelectionEvent(nsPresContext* aPresContext, |
1543 | | nsIContent* aEventTargetContent, |
1544 | | WidgetSelectionEvent* aSelectionEvent) |
1545 | 0 | { |
1546 | 0 | nsIContent* eventTargetContent = |
1547 | 0 | aEventTargetContent ? aEventTargetContent : |
1548 | 0 | GetRootContent(aPresContext); |
1549 | 0 | RefPtr<TabParent> tabParent = |
1550 | 0 | eventTargetContent ? TabParent::GetFrom(eventTargetContent) : nullptr; |
1551 | 0 |
|
1552 | 0 | MOZ_LOG(sISMLog, LogLevel::Info, |
1553 | 0 | ("HandleSelectionEvent(aPresContext=0x%p, " |
1554 | 0 | "aEventTargetContent=0x%p, aSelectionEvent={ mMessage=%s, " |
1555 | 0 | "mFlags={ mIsTrusted=%s } }), tabParent=%p", |
1556 | 0 | aPresContext, aEventTargetContent, |
1557 | 0 | ToChar(aSelectionEvent->mMessage), |
1558 | 0 | GetBoolName(aSelectionEvent->mFlags.mIsTrusted), |
1559 | 0 | tabParent.get())); |
1560 | 0 |
|
1561 | 0 | if (!aSelectionEvent->IsTrusted()) { |
1562 | 0 | return; |
1563 | 0 | } |
1564 | 0 | |
1565 | 0 | RefPtr<TextComposition> composition = sTextCompositions ? |
1566 | 0 | sTextCompositions->GetCompositionFor(aSelectionEvent->mWidget) : nullptr; |
1567 | 0 | if (composition) { |
1568 | 0 | // When there is a composition, TextComposition should guarantee that the |
1569 | 0 | // selection event will be handled in same target as composition events. |
1570 | 0 | composition->HandleSelectionEvent(aSelectionEvent); |
1571 | 0 | } else { |
1572 | 0 | // When there is no composition, the selection event should be handled |
1573 | 0 | // in the aPresContext or tabParent. |
1574 | 0 | TextComposition::HandleSelectionEvent(aPresContext, tabParent, |
1575 | 0 | aSelectionEvent); |
1576 | 0 | } |
1577 | 0 | } |
1578 | | |
1579 | | // static |
1580 | | void |
1581 | | IMEStateManager::OnCompositionEventDiscarded( |
1582 | | WidgetCompositionEvent* aCompositionEvent) |
1583 | 0 | { |
1584 | 0 | // Note that this method is never called for synthesized events for emulating |
1585 | 0 | // commit or cancel composition. |
1586 | 0 |
|
1587 | 0 | MOZ_LOG(sISMLog, LogLevel::Info, |
1588 | 0 | ("OnCompositionEventDiscarded(aCompositionEvent={ " |
1589 | 0 | "mMessage=%s, mNativeIMEContext={ mRawNativeIMEContext=0x%" PRIXPTR ", " |
1590 | 0 | "mOriginProcessID=0x%" PRIX64 " }, mWidget(0x%p)={ " |
1591 | 0 | "GetNativeIMEContext()={ mRawNativeIMEContext=0x%" PRIXPTR ", " |
1592 | 0 | "mOriginProcessID=0x%" PRIX64 " }, Destroyed()=%s }, " |
1593 | 0 | "mFlags={ mIsTrusted=%s } })", |
1594 | 0 | ToChar(aCompositionEvent->mMessage), |
1595 | 0 | aCompositionEvent->mNativeIMEContext.mRawNativeIMEContext, |
1596 | 0 | aCompositionEvent->mNativeIMEContext.mOriginProcessID, |
1597 | 0 | aCompositionEvent->mWidget.get(), |
1598 | 0 | aCompositionEvent->mWidget->GetNativeIMEContext().mRawNativeIMEContext, |
1599 | 0 | aCompositionEvent->mWidget->GetNativeIMEContext().mOriginProcessID, |
1600 | 0 | GetBoolName(aCompositionEvent->mWidget->Destroyed()), |
1601 | 0 | GetBoolName(aCompositionEvent->mFlags.mIsTrusted))); |
1602 | 0 |
|
1603 | 0 | if (!aCompositionEvent->IsTrusted()) { |
1604 | 0 | return; |
1605 | 0 | } |
1606 | 0 | |
1607 | 0 | // Ignore compositionstart for now because sTextCompositions may not have |
1608 | 0 | // been created yet. |
1609 | 0 | if (aCompositionEvent->mMessage == eCompositionStart) { |
1610 | 0 | return; |
1611 | 0 | } |
1612 | 0 | |
1613 | 0 | RefPtr<TextComposition> composition = |
1614 | 0 | sTextCompositions->GetCompositionFor(aCompositionEvent->mWidget); |
1615 | 0 | if (!composition) { |
1616 | 0 | // If the PresShell has been being destroyed during composition, |
1617 | 0 | // a TextComposition instance for the composition was already removed from |
1618 | 0 | // the array and destroyed in OnDestroyPresContext(). Therefore, we may |
1619 | 0 | // fail to retrieve a TextComposition instance here. |
1620 | 0 | MOZ_LOG(sISMLog, LogLevel::Info, |
1621 | 0 | (" OnCompositionEventDiscarded(), " |
1622 | 0 | "TextComposition instance for the widget has already gone")); |
1623 | 0 | return; |
1624 | 0 | } |
1625 | 0 | composition->OnCompositionEventDiscarded(aCompositionEvent); |
1626 | 0 | } |
1627 | | |
1628 | | // static |
1629 | | nsresult |
1630 | | IMEStateManager::NotifyIME(IMEMessage aMessage, |
1631 | | nsIWidget* aWidget, |
1632 | | TabParent* aTabParent) |
1633 | 0 | { |
1634 | 0 | return IMEStateManager::NotifyIME(IMENotification(aMessage), aWidget, |
1635 | 0 | aTabParent); |
1636 | 0 | } |
1637 | | |
1638 | | // static |
1639 | | nsresult |
1640 | | IMEStateManager::NotifyIME(const IMENotification& aNotification, |
1641 | | nsIWidget* aWidget, |
1642 | | TabParent* aTabParent) |
1643 | 0 | { |
1644 | 0 | MOZ_LOG(sISMLog, LogLevel::Info, |
1645 | 0 | ("NotifyIME(aNotification={ mMessage=%s }, " |
1646 | 0 | "aWidget=0x%p, aTabParent=0x%p), sFocusedIMEWidget=0x%p, " |
1647 | 0 | "sActiveTabParent=0x%p, sFocusedIMETabParent=0x%p, " |
1648 | 0 | "IsSameProcess(aTabParent, sActiveTabParent)=%s, " |
1649 | 0 | "IsSameProcess(aTabParent, sFocusedIMETabParent)=%s", |
1650 | 0 | ToChar(aNotification.mMessage), aWidget, |
1651 | 0 | aTabParent, sFocusedIMEWidget, sActiveTabParent.get(), |
1652 | 0 | sFocusedIMETabParent.get(), |
1653 | 0 | GetBoolName(IsSameProcess(aTabParent, sActiveTabParent)), |
1654 | 0 | GetBoolName(IsSameProcess(aTabParent, sFocusedIMETabParent)))); |
1655 | 0 |
|
1656 | 0 | if (NS_WARN_IF(!aWidget)) { |
1657 | 0 | MOZ_LOG(sISMLog, LogLevel::Error, |
1658 | 0 | (" NotifyIME(), FAILED due to no widget")); |
1659 | 0 | return NS_ERROR_INVALID_ARG; |
1660 | 0 | } |
1661 | 0 |
|
1662 | 0 | switch (aNotification.mMessage) { |
1663 | 0 | case NOTIFY_IME_OF_FOCUS: { |
1664 | 0 | // If focus notification comes from a remote process which already lost |
1665 | 0 | // focus, we shouldn't accept the focus notification. Then, following |
1666 | 0 | // notifications from the process will be ignored. |
1667 | 0 | if (NS_WARN_IF(!IsSameProcess(aTabParent, sActiveTabParent))) { |
1668 | 0 | MOZ_ASSERT(aTabParent, |
1669 | 0 | "Why was the input context initialized for a remote process but " |
1670 | 0 | "does this process get IME focus?"); |
1671 | 0 | MOZ_LOG(sISMLog, LogLevel::Warning, |
1672 | 0 | (" NotifyIME(), WARNING, the received focus notification is ignored " |
1673 | 0 | "because input context was initialized for %s, perhaps, it came " |
1674 | 0 | "from a busy remote process", |
1675 | 0 | sActiveTabParent ? "another remote process" : "current process")); |
1676 | 0 | return NS_OK; |
1677 | 0 | } |
1678 | 0 | // If there is pending blur notification for current focused IME, |
1679 | 0 | // we should notify IME of blur by ourselves. Then, we should ignore |
1680 | 0 | // following notifications coming from the process. |
1681 | 0 | if (sFocusedIMEWidget) { |
1682 | 0 | MOZ_ASSERT(sFocusedIMETabParent || aTabParent, |
1683 | 0 | "This case shouldn't be caused by focus move in this process"); |
1684 | 0 | nsCOMPtr<nsIWidget> focusedIMEWidget(sFocusedIMEWidget); |
1685 | 0 | sFocusedIMEWidget = nullptr; |
1686 | 0 | sFocusedIMETabParent = nullptr; |
1687 | 0 | focusedIMEWidget->NotifyIME(IMENotification(NOTIFY_IME_OF_BLUR)); |
1688 | 0 | } |
1689 | 0 | sFocusedIMETabParent = aTabParent; |
1690 | 0 | sFocusedIMEWidget = aWidget; |
1691 | 0 | nsCOMPtr<nsIWidget> widget(aWidget); |
1692 | 0 | return widget->NotifyIME(aNotification); |
1693 | 0 | } |
1694 | 0 | case NOTIFY_IME_OF_BLUR: { |
1695 | 0 | if (!IsSameProcess(aTabParent, sFocusedIMETabParent)) { |
1696 | 0 | MOZ_LOG(sISMLog, LogLevel::Warning, |
1697 | 0 | (" NotifyIME(), WARNING, the received blur notification is ignored " |
1698 | 0 | "because it's not from current focused IME process")); |
1699 | 0 | return NS_OK; |
1700 | 0 | } |
1701 | 0 | if (!sFocusedIMEWidget) { |
1702 | 0 | MOZ_LOG(sISMLog, LogLevel::Error, |
1703 | 0 | (" NotifyIME(), WARNING, received blur notification but there is " |
1704 | 0 | "no focused IME widget")); |
1705 | 0 | return NS_OK; |
1706 | 0 | } |
1707 | 0 | if (NS_WARN_IF(sFocusedIMEWidget != aWidget)) { |
1708 | 0 | MOZ_LOG(sISMLog, LogLevel::Warning, |
1709 | 0 | (" NotifyIME(), WARNING, the received blur notification is ignored " |
1710 | 0 | "because it's not for current focused IME widget")); |
1711 | 0 | return NS_OK; |
1712 | 0 | } |
1713 | 0 | nsCOMPtr<nsIWidget> focusedIMEWidget(sFocusedIMEWidget); |
1714 | 0 | sFocusedIMEWidget = nullptr; |
1715 | 0 | sFocusedIMETabParent = nullptr; |
1716 | 0 | return focusedIMEWidget->NotifyIME(IMENotification(NOTIFY_IME_OF_BLUR)); |
1717 | 0 | } |
1718 | 0 | case NOTIFY_IME_OF_SELECTION_CHANGE: |
1719 | 0 | case NOTIFY_IME_OF_TEXT_CHANGE: |
1720 | 0 | case NOTIFY_IME_OF_POSITION_CHANGE: |
1721 | 0 | case NOTIFY_IME_OF_MOUSE_BUTTON_EVENT: |
1722 | 0 | case NOTIFY_IME_OF_COMPOSITION_EVENT_HANDLED: { |
1723 | 0 | if (!IsSameProcess(aTabParent, sFocusedIMETabParent)) { |
1724 | 0 | MOZ_LOG(sISMLog, LogLevel::Warning, |
1725 | 0 | (" NotifyIME(), WARNING, the received content change notification " |
1726 | 0 | "is ignored because it's not from current focused IME process")); |
1727 | 0 | return NS_OK; |
1728 | 0 | } |
1729 | 0 | if (!sFocusedIMEWidget) { |
1730 | 0 | MOZ_LOG(sISMLog, LogLevel::Warning, |
1731 | 0 | (" NotifyIME(), WARNING, the received content change notification " |
1732 | 0 | "is ignored because there is no focused IME widget")); |
1733 | 0 | return NS_OK; |
1734 | 0 | } |
1735 | 0 | if (NS_WARN_IF(sFocusedIMEWidget != aWidget)) { |
1736 | 0 | MOZ_LOG(sISMLog, LogLevel::Warning, |
1737 | 0 | (" NotifyIME(), WARNING, the received content change notification " |
1738 | 0 | "is ignored because it's not for current focused IME widget")); |
1739 | 0 | return NS_OK; |
1740 | 0 | } |
1741 | 0 | nsCOMPtr<nsIWidget> widget(aWidget); |
1742 | 0 | return widget->NotifyIME(aNotification); |
1743 | 0 | } |
1744 | 0 | default: |
1745 | 0 | // Other notifications should be sent only when there is composition. |
1746 | 0 | // So, we need to handle the others below. |
1747 | 0 | break; |
1748 | 0 | } |
1749 | 0 | |
1750 | 0 | if (!sTextCompositions) { |
1751 | 0 | MOZ_LOG(sISMLog, LogLevel::Info, |
1752 | 0 | (" NotifyIME(), the request to IME is ignored because " |
1753 | 0 | "there have been no compositions yet")); |
1754 | 0 | return NS_OK; |
1755 | 0 | } |
1756 | 0 |
|
1757 | 0 | RefPtr<TextComposition> composition = |
1758 | 0 | sTextCompositions->GetCompositionFor(aWidget); |
1759 | 0 | if (!composition) { |
1760 | 0 | MOZ_LOG(sISMLog, LogLevel::Info, |
1761 | 0 | (" NotifyIME(), the request to IME is ignored because " |
1762 | 0 | "there is no active composition")); |
1763 | 0 | return NS_OK; |
1764 | 0 | } |
1765 | 0 |
|
1766 | 0 | if (!IsSameProcess(aTabParent, composition->GetTabParent())) { |
1767 | 0 | MOZ_LOG(sISMLog, LogLevel::Warning, |
1768 | 0 | (" NotifyIME(), WARNING, the request to IME is ignored because " |
1769 | 0 | "it does not come from the remote process which has the composition " |
1770 | 0 | "on aWidget")); |
1771 | 0 | return NS_OK; |
1772 | 0 | } |
1773 | 0 |
|
1774 | 0 | switch (aNotification.mMessage) { |
1775 | 0 | case REQUEST_TO_COMMIT_COMPOSITION: |
1776 | 0 | return composition->RequestToCommit(aWidget, false); |
1777 | 0 | case REQUEST_TO_CANCEL_COMPOSITION: |
1778 | 0 | return composition->RequestToCommit(aWidget, true); |
1779 | 0 | default: |
1780 | 0 | MOZ_CRASH("Unsupported notification"); |
1781 | 0 | } |
1782 | 0 | MOZ_CRASH( |
1783 | 0 | "Failed to handle the notification for non-synthesized composition"); |
1784 | 0 | return NS_ERROR_FAILURE; |
1785 | 0 | } |
1786 | | |
1787 | | // static |
1788 | | nsresult |
1789 | | IMEStateManager::NotifyIME(IMEMessage aMessage, |
1790 | | nsPresContext* aPresContext, |
1791 | | TabParent* aTabParent) |
1792 | 0 | { |
1793 | 0 | MOZ_LOG(sISMLog, LogLevel::Info, |
1794 | 0 | ("NotifyIME(aMessage=%s, aPresContext=0x%p, aTabParent=0x%p)", |
1795 | 0 | ToChar(aMessage), aPresContext, aTabParent)); |
1796 | 0 |
|
1797 | 0 | if (NS_WARN_IF(!CanHandleWith(aPresContext))) { |
1798 | 0 | return NS_ERROR_INVALID_ARG; |
1799 | 0 | } |
1800 | 0 | |
1801 | 0 | nsIWidget* widget = aPresContext->GetRootWidget(); |
1802 | 0 | if (NS_WARN_IF(!widget)) { |
1803 | 0 | MOZ_LOG(sISMLog, LogLevel::Error, |
1804 | 0 | (" NotifyIME(), FAILED due to no widget for the " |
1805 | 0 | "nsPresContext")); |
1806 | 0 | return NS_ERROR_NOT_AVAILABLE; |
1807 | 0 | } |
1808 | 0 | return NotifyIME(aMessage, widget, aTabParent); |
1809 | 0 | } |
1810 | | |
1811 | | // static |
1812 | | bool |
1813 | | IMEStateManager::IsEditable(nsINode* node) |
1814 | 0 | { |
1815 | 0 | if (node->IsEditable()) { |
1816 | 0 | return true; |
1817 | 0 | } |
1818 | 0 | // |node| might be readwrite (for example, a text control) |
1819 | 0 | if (node->IsElement() && |
1820 | 0 | node->AsElement()->State().HasState(NS_EVENT_STATE_MOZ_READWRITE)) { |
1821 | 0 | return true; |
1822 | 0 | } |
1823 | 0 | return false; |
1824 | 0 | } |
1825 | | |
1826 | | // static |
1827 | | nsINode* |
1828 | | IMEStateManager::GetRootEditableNode(nsPresContext* aPresContext, |
1829 | | nsIContent* aContent) |
1830 | 0 | { |
1831 | 0 | if (aContent) { |
1832 | 0 | nsINode* root = nullptr; |
1833 | 0 | nsINode* node = aContent; |
1834 | 0 | while (node && IsEditable(node)) { |
1835 | 0 | // If the node has independent selection like <input type="text"> or |
1836 | 0 | // <textarea>, the node should be the root editable node for aContent. |
1837 | 0 | // FYI: <select> element also has independent selection but IsEditable() |
1838 | 0 | // returns false. |
1839 | 0 | // XXX: If somebody adds new editable element which has independent |
1840 | 0 | // selection but doesn't own editor, we'll need more checks here. |
1841 | 0 | if (node->IsContent() && |
1842 | 0 | node->AsContent()->HasIndependentSelection()) { |
1843 | 0 | return node; |
1844 | 0 | } |
1845 | 0 | root = node; |
1846 | 0 | node = node->GetParentNode(); |
1847 | 0 | } |
1848 | 0 | return root; |
1849 | 0 | } |
1850 | 0 | if (aPresContext) { |
1851 | 0 | nsIDocument* document = aPresContext->Document(); |
1852 | 0 | if (document && document->IsEditable()) { |
1853 | 0 | return document; |
1854 | 0 | } |
1855 | 0 | } |
1856 | 0 | return nullptr; |
1857 | 0 | } |
1858 | | |
1859 | | // static |
1860 | | bool |
1861 | | IMEStateManager::IsIMEObserverNeeded(const IMEState& aState) |
1862 | 0 | { |
1863 | 0 | return aState.MaybeEditable(); |
1864 | 0 | } |
1865 | | |
1866 | | // static |
1867 | | void |
1868 | | IMEStateManager::DestroyIMEContentObserver() |
1869 | 0 | { |
1870 | 0 | MOZ_LOG(sISMLog, LogLevel::Info, |
1871 | 0 | ("DestroyIMEContentObserver(), sActiveIMEContentObserver=0x%p", |
1872 | 0 | sActiveIMEContentObserver.get())); |
1873 | 0 |
|
1874 | 0 | if (!sActiveIMEContentObserver) { |
1875 | 0 | MOZ_LOG(sISMLog, LogLevel::Debug, |
1876 | 0 | (" DestroyIMEContentObserver() does nothing")); |
1877 | 0 | return; |
1878 | 0 | } |
1879 | 0 |
|
1880 | 0 | MOZ_LOG(sISMLog, LogLevel::Debug, |
1881 | 0 | (" DestroyIMEContentObserver(), destroying " |
1882 | 0 | "the active IMEContentObserver...")); |
1883 | 0 | RefPtr<IMEContentObserver> tsm = sActiveIMEContentObserver.get(); |
1884 | 0 | sActiveIMEContentObserver = nullptr; |
1885 | 0 | tsm->Destroy(); |
1886 | 0 | } |
1887 | | |
1888 | | // static |
1889 | | void |
1890 | | IMEStateManager::CreateIMEContentObserver(EditorBase* aEditorBase) |
1891 | 0 | { |
1892 | 0 | MOZ_LOG(sISMLog, LogLevel::Info, |
1893 | 0 | ("CreateIMEContentObserver(aEditorBase=0x%p), " |
1894 | 0 | "sPresContext=0x%p, sContent=0x%p, sWidget=0x%p (available: %s), " |
1895 | 0 | "sActiveIMEContentObserver=0x%p, " |
1896 | 0 | "sActiveIMEContentObserver->IsManaging(sPresContext, sContent)=%s", |
1897 | 0 | aEditorBase, sPresContext.get(), sContent.get(), |
1898 | 0 | sWidget, GetBoolName(sWidget && !sWidget->Destroyed()), |
1899 | 0 | sActiveIMEContentObserver.get(), |
1900 | 0 | GetBoolName(sActiveIMEContentObserver ? |
1901 | 0 | sActiveIMEContentObserver->IsManaging(sPresContext, sContent) : false))); |
1902 | 0 |
|
1903 | 0 | if (NS_WARN_IF(sActiveIMEContentObserver)) { |
1904 | 0 | MOZ_LOG(sISMLog, LogLevel::Error, |
1905 | 0 | (" CreateIMEContentObserver(), FAILED due to " |
1906 | 0 | "there is already an active IMEContentObserver")); |
1907 | 0 | MOZ_ASSERT(sActiveIMEContentObserver->IsManaging(sPresContext, sContent)); |
1908 | 0 | return; |
1909 | 0 | } |
1910 | 0 |
|
1911 | 0 | if (!sWidget || NS_WARN_IF(sWidget->Destroyed())) { |
1912 | 0 | MOZ_LOG(sISMLog, LogLevel::Error, |
1913 | 0 | (" CreateIMEContentObserver(), FAILED due to " |
1914 | 0 | "the widget for the nsPresContext has gone")); |
1915 | 0 | return; // Sometimes, there are no widgets. |
1916 | 0 | } |
1917 | 0 |
|
1918 | 0 | nsCOMPtr<nsIWidget> widget(sWidget); |
1919 | 0 |
|
1920 | 0 | // If it's not text editable, we don't need to create IMEContentObserver. |
1921 | 0 | if (!IsIMEObserverNeeded(widget->GetInputContext().mIMEState)) { |
1922 | 0 | MOZ_LOG(sISMLog, LogLevel::Debug, |
1923 | 0 | (" CreateIMEContentObserver() doesn't create " |
1924 | 0 | "IMEContentObserver because of non-editable IME state")); |
1925 | 0 | return; |
1926 | 0 | } |
1927 | 0 |
|
1928 | 0 | if (NS_WARN_IF(widget->Destroyed())) { |
1929 | 0 | MOZ_LOG(sISMLog, LogLevel::Error, |
1930 | 0 | (" CreateIMEContentObserver(), FAILED due to " |
1931 | 0 | "the widget for the nsPresContext has gone")); |
1932 | 0 | return; |
1933 | 0 | } |
1934 | 0 |
|
1935 | 0 | MOZ_ASSERT(sPresContext->GetRootWidget() == widget); |
1936 | 0 |
|
1937 | 0 | MOZ_LOG(sISMLog, LogLevel::Debug, |
1938 | 0 | (" CreateIMEContentObserver() is creating an " |
1939 | 0 | "IMEContentObserver instance...")); |
1940 | 0 | sActiveIMEContentObserver = new IMEContentObserver(); |
1941 | 0 |
|
1942 | 0 | // IMEContentObserver::Init() might create another IMEContentObserver |
1943 | 0 | // instance. So, sActiveIMEContentObserver would be replaced with new one. |
1944 | 0 | // We should hold the current instance here. |
1945 | 0 | RefPtr<IMEContentObserver> activeIMEContentObserver(sActiveIMEContentObserver); |
1946 | 0 | activeIMEContentObserver->Init(widget, sPresContext, sContent, aEditorBase); |
1947 | 0 | } |
1948 | | |
1949 | | // static |
1950 | | nsresult |
1951 | | IMEStateManager::GetFocusSelectionAndRoot(Selection** aSelection, |
1952 | | nsIContent** aRootContent) |
1953 | 0 | { |
1954 | 0 | if (!sActiveIMEContentObserver) { |
1955 | 0 | return NS_ERROR_NOT_AVAILABLE; |
1956 | 0 | } |
1957 | 0 | return sActiveIMEContentObserver->GetSelectionAndRoot(aSelection, |
1958 | 0 | aRootContent); |
1959 | 0 | } |
1960 | | |
1961 | | // static |
1962 | | already_AddRefed<TextComposition> |
1963 | | IMEStateManager::GetTextCompositionFor(nsIWidget* aWidget) |
1964 | 0 | { |
1965 | 0 | if (!sTextCompositions) { |
1966 | 0 | return nullptr; |
1967 | 0 | } |
1968 | 0 | RefPtr<TextComposition> textComposition = |
1969 | 0 | sTextCompositions->GetCompositionFor(aWidget); |
1970 | 0 | return textComposition.forget(); |
1971 | 0 | } |
1972 | | |
1973 | | // static |
1974 | | already_AddRefed<TextComposition> |
1975 | | IMEStateManager::GetTextCompositionFor( |
1976 | | const WidgetCompositionEvent* aCompositionEvent) |
1977 | 0 | { |
1978 | 0 | if (!sTextCompositions) { |
1979 | 0 | return nullptr; |
1980 | 0 | } |
1981 | 0 | RefPtr<TextComposition> textComposition = |
1982 | 0 | sTextCompositions->GetCompositionFor(aCompositionEvent); |
1983 | 0 | return textComposition.forget(); |
1984 | 0 | } |
1985 | | |
1986 | | // static |
1987 | | already_AddRefed<TextComposition> |
1988 | | IMEStateManager::GetTextCompositionFor(nsPresContext* aPresContext) |
1989 | 0 | { |
1990 | 0 | if (!sTextCompositions) { |
1991 | 0 | return nullptr; |
1992 | 0 | } |
1993 | 0 | RefPtr<TextComposition> textComposition = |
1994 | 0 | sTextCompositions->GetCompositionFor(aPresContext); |
1995 | 0 | return textComposition.forget(); |
1996 | 0 | } |
1997 | | |
1998 | | } // namespace mozilla |