Bug 1487655 [wpt PR 12775] - [css-properties-values-api] Typify CSSStyleValue.parse...
[gecko.git] / widget / PuppetWidget.cpp
blob0440b7620dde97325221f185604067fb97e4b33e
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=8 et :
3 */
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "base/basictypes.h"
10 #include "ClientLayerManager.h"
11 #include "gfxPlatform.h"
12 #include "mozilla/dom/TabChild.h"
13 #include "mozilla/dom/TabGroup.h"
14 #include "mozilla/gfx/gfxVars.h"
15 #include "mozilla/Hal.h"
16 #include "mozilla/IMEStateManager.h"
17 #include "mozilla/layers/APZChild.h"
18 #include "mozilla/layers/PLayerTransactionChild.h"
19 #include "mozilla/layers/WebRenderLayerManager.h"
20 #include "mozilla/Preferences.h"
21 #include "mozilla/TextComposition.h"
22 #include "mozilla/TextEventDispatcher.h"
23 #include "mozilla/TextEvents.h"
24 #include "mozilla/Unused.h"
25 #include "BasicLayers.h"
26 #include "PuppetWidget.h"
27 #include "nsContentUtils.h"
28 #include "nsIWidgetListener.h"
29 #include "imgIContainer.h"
30 #include "nsView.h"
31 #include "nsPrintfCString.h"
33 using namespace mozilla;
34 using namespace mozilla::dom;
35 using namespace mozilla::hal;
36 using namespace mozilla::gfx;
37 using namespace mozilla::layers;
38 using namespace mozilla::widget;
40 static void
41 InvalidateRegion(nsIWidget* aWidget, const LayoutDeviceIntRegion& aRegion)
43 for (auto iter = aRegion.RectIter(); !iter.Done(); iter.Next()) {
44 aWidget->Invalidate(iter.Get());
48 /*static*/ already_AddRefed<nsIWidget>
49 nsIWidget::CreatePuppetWidget(TabChild* aTabChild)
51 MOZ_ASSERT(!aTabChild || nsIWidget::UsePuppetWidgets(),
52 "PuppetWidgets not allowed in this configuration");
54 nsCOMPtr<nsIWidget> widget = new PuppetWidget(aTabChild);
55 return widget.forget();
58 namespace mozilla {
59 namespace widget {
61 static bool
62 IsPopup(const nsWidgetInitData* aInitData)
64 return aInitData && aInitData->mWindowType == eWindowType_popup;
67 static bool
68 MightNeedIMEFocus(const nsWidgetInitData* aInitData)
70 // In the puppet-widget world, popup widgets are just dummies and
71 // shouldn't try to mess with IME state.
72 #ifdef MOZ_CROSS_PROCESS_IME
73 return !IsPopup(aInitData);
74 #else
75 return false;
76 #endif
79 // Arbitrary, fungible.
80 const size_t PuppetWidget::kMaxDimension = 4000;
82 static bool gRemoteDesktopBehaviorEnabled = false;
83 static bool gRemoteDesktopBehaviorInitialized = false;
85 NS_IMPL_ISUPPORTS_INHERITED(PuppetWidget, nsBaseWidget
86 , TextEventDispatcherListener)
88 PuppetWidget::PuppetWidget(TabChild* aTabChild)
89 : mTabChild(aTabChild)
90 , mMemoryPressureObserver(nullptr)
91 , mDPI(-1)
92 , mRounding(1)
93 , mDefaultScale(-1)
94 , mCursorHotspotX(0)
95 , mCursorHotspotY(0)
96 , mEnabled(false)
97 , mVisible(false)
98 , mNeedIMEStateInit(false)
99 , mIgnoreCompositionEvents(false)
101 // Setting 'Unknown' means "not yet cached".
102 mInputContext.mIMEState.mEnabled = IMEState::UNKNOWN;
104 if (!gRemoteDesktopBehaviorInitialized) {
105 Preferences::AddBoolVarCache(&gRemoteDesktopBehaviorEnabled, "browser.tabs.remote.desktopbehavior", false);
106 gRemoteDesktopBehaviorInitialized = true;
110 PuppetWidget::~PuppetWidget()
112 Destroy();
115 void
116 PuppetWidget::InfallibleCreate(nsIWidget* aParent,
117 nsNativeWidget aNativeParent,
118 const LayoutDeviceIntRect& aRect,
119 nsWidgetInitData* aInitData)
121 MOZ_ASSERT(!aNativeParent, "got a non-Puppet native parent");
123 BaseCreate(nullptr, aInitData);
125 mBounds = aRect;
126 mEnabled = true;
127 mVisible = true;
129 mDrawTarget = gfxPlatform::GetPlatform()->
130 CreateOffscreenContentDrawTarget(IntSize(1, 1), SurfaceFormat::B8G8R8A8);
132 mNeedIMEStateInit = MightNeedIMEFocus(aInitData);
134 PuppetWidget* parent = static_cast<PuppetWidget*>(aParent);
135 if (parent) {
136 parent->SetChild(this);
137 mLayerManager = parent->GetLayerManager();
139 else {
140 Resize(mBounds.X(), mBounds.Y(), mBounds.Width(), mBounds.Height(), false);
142 mMemoryPressureObserver = MemoryPressureObserver::Create(this);
145 nsresult
146 PuppetWidget::Create(nsIWidget* aParent,
147 nsNativeWidget aNativeParent,
148 const LayoutDeviceIntRect& aRect,
149 nsWidgetInitData* aInitData)
151 InfallibleCreate(aParent, aNativeParent, aRect, aInitData);
152 return NS_OK;
155 void
156 PuppetWidget::InitIMEState()
158 MOZ_ASSERT(mTabChild);
159 if (mNeedIMEStateInit) {
160 mContentCache.Clear();
161 mTabChild->SendUpdateContentCache(mContentCache);
162 mIMENotificationRequestsOfParent = IMENotificationRequests();
163 mNeedIMEStateInit = false;
167 already_AddRefed<nsIWidget>
168 PuppetWidget::CreateChild(const LayoutDeviceIntRect& aRect,
169 nsWidgetInitData* aInitData,
170 bool aForceUseIWidgetParent)
172 bool isPopup = IsPopup(aInitData);
173 nsCOMPtr<nsIWidget> widget = nsIWidget::CreatePuppetWidget(mTabChild);
174 return ((widget &&
175 NS_SUCCEEDED(widget->Create(isPopup ? nullptr: this, nullptr, aRect,
176 aInitData))) ?
177 widget.forget() : nullptr);
180 void
181 PuppetWidget::Destroy()
183 if (mOnDestroyCalled) {
184 return;
186 mOnDestroyCalled = true;
188 Base::OnDestroy();
189 Base::Destroy();
190 mPaintTask.Revoke();
191 if (mMemoryPressureObserver) {
192 mMemoryPressureObserver->Unregister();
193 mMemoryPressureObserver = nullptr;
195 mChild = nullptr;
196 if (mLayerManager) {
197 mLayerManager->Destroy();
199 mLayerManager = nullptr;
200 mTabChild = nullptr;
203 void
204 PuppetWidget::Show(bool aState)
206 NS_ASSERTION(mEnabled,
207 "does it make sense to Show()/Hide() a disabled widget?");
209 bool wasVisible = mVisible;
210 mVisible = aState;
212 if (mChild) {
213 mChild->mVisible = aState;
216 if (!wasVisible && mVisible) {
217 // The previously attached widget listener is handy if
218 // we're transitioning from page to page without dropping
219 // layers (since we'll continue to show the old layers
220 // associated with that old widget listener). If the
221 // PuppetWidget was hidden, those layers are dropped,
222 // so the previously attached widget listener is really
223 // of no use anymore (and is actually actively harmful - see
224 // bug 1323586).
225 mPreviouslyAttachedWidgetListener = nullptr;
226 Resize(mBounds.Width(), mBounds.Height(), false);
227 Invalidate(mBounds);
231 void
232 PuppetWidget::Resize(double aWidth,
233 double aHeight,
234 bool aRepaint)
236 LayoutDeviceIntRect oldBounds = mBounds;
237 mBounds.SizeTo(LayoutDeviceIntSize(NSToIntRound(aWidth),
238 NSToIntRound(aHeight)));
240 if (mChild) {
241 mChild->Resize(aWidth, aHeight, aRepaint);
242 return;
245 // XXX: roc says that |aRepaint| dictates whether or not to
246 // invalidate the expanded area
247 if (oldBounds.Size() < mBounds.Size() && aRepaint) {
248 LayoutDeviceIntRegion dirty(mBounds);
249 dirty.Sub(dirty, oldBounds);
250 InvalidateRegion(this, dirty);
253 // call WindowResized() on both the current listener, and possibly
254 // also the previous one if we're in a state where we're drawing that one
255 // because the current one is paint suppressed
256 if (!oldBounds.IsEqualEdges(mBounds) && mAttachedWidgetListener) {
257 if (GetCurrentWidgetListener() &&
258 GetCurrentWidgetListener() != mAttachedWidgetListener) {
259 GetCurrentWidgetListener()->WindowResized(this, mBounds.Width(), mBounds.Height());
261 mAttachedWidgetListener->WindowResized(this, mBounds.Width(), mBounds.Height());
265 nsresult
266 PuppetWidget::ConfigureChildren(const nsTArray<Configuration>& aConfigurations)
268 for (uint32_t i = 0; i < aConfigurations.Length(); ++i) {
269 const Configuration& configuration = aConfigurations[i];
270 PuppetWidget* w = static_cast<PuppetWidget*>(configuration.mChild.get());
271 NS_ASSERTION(w->GetParent() == this,
272 "Configured widget is not a child");
273 w->SetWindowClipRegion(configuration.mClipRegion, true);
274 LayoutDeviceIntRect bounds = w->GetBounds();
275 if (bounds.Size() != configuration.mBounds.Size()) {
276 w->Resize(configuration.mBounds.X(), configuration.mBounds.Y(),
277 configuration.mBounds.Width(), configuration.mBounds.Height(),
278 true);
279 } else if (bounds.TopLeft() != configuration.mBounds.TopLeft()) {
280 w->Move(configuration.mBounds.X(), configuration.mBounds.Y());
282 w->SetWindowClipRegion(configuration.mClipRegion, false);
284 return NS_OK;
287 nsresult
288 PuppetWidget::SetFocus(bool aRaise)
290 if (aRaise && mTabChild) {
291 mTabChild->SendRequestFocus(true);
294 return NS_OK;
297 void
298 PuppetWidget::Invalidate(const LayoutDeviceIntRect& aRect)
300 #ifdef DEBUG
301 debug_DumpInvalidate(stderr, this, &aRect, "PuppetWidget", 0);
302 #endif
304 if (mChild) {
305 mChild->Invalidate(aRect);
306 return;
309 mDirtyRegion.Or(mDirtyRegion, aRect);
311 if (mTabChild && !mDirtyRegion.IsEmpty() && !mPaintTask.IsPending()) {
312 mPaintTask = new PaintTask(this);
313 nsCOMPtr<nsIRunnable> event(mPaintTask.get());
314 mTabChild->TabGroup()->Dispatch(TaskCategory::Other, event.forget());
315 return;
319 void
320 PuppetWidget::InitEvent(WidgetGUIEvent& aEvent, LayoutDeviceIntPoint* aPoint)
322 if (nullptr == aPoint) {
323 aEvent.mRefPoint = LayoutDeviceIntPoint(0, 0);
324 } else {
325 // use the point override if provided
326 aEvent.mRefPoint = *aPoint;
328 aEvent.mTime = PR_Now() / 1000;
331 nsresult
332 PuppetWidget::DispatchEvent(WidgetGUIEvent* aEvent, nsEventStatus& aStatus)
334 #ifdef DEBUG
335 debug_DumpEvent(stdout, aEvent->mWidget, aEvent, "PuppetWidget", 0);
336 #endif
338 MOZ_ASSERT(!mChild || mChild->mWindowType == eWindowType_popup,
339 "Unexpected event dispatch!");
341 MOZ_ASSERT(!aEvent->AsKeyboardEvent() ||
342 aEvent->mFlags.mIsSynthesizedForTests ||
343 aEvent->AsKeyboardEvent()->AreAllEditCommandsInitialized(),
344 "Non-sysnthesized keyboard events should have edit commands for all types "
345 "before dispatched");
347 if (aEvent->mClass == eCompositionEventClass) {
348 // If we've already requested to commit/cancel the latest composition,
349 // TextComposition for the old composition has been destroyed. Then,
350 // the DOM tree needs to listen to next eCompositionStart and its
351 // following events. So, until we meet new eCompositionStart, let's
352 // discard all unnecessary composition events here.
353 if (mIgnoreCompositionEvents) {
354 if (aEvent->mMessage != eCompositionStart) {
355 aStatus = nsEventStatus_eIgnore;
356 return NS_OK;
358 // Now, we receive new eCompositionStart. Let's restart to handle
359 // composition in this process.
360 mIgnoreCompositionEvents = false;
362 // Store the latest native IME context of parent process's widget or
363 // TextEventDispatcher if it's in this process.
364 WidgetCompositionEvent* compositionEvent = aEvent->AsCompositionEvent();
365 #ifdef DEBUG
366 if (mNativeIMEContext.IsValid() &&
367 mNativeIMEContext != compositionEvent->mNativeIMEContext) {
368 RefPtr<TextComposition> composition =
369 IMEStateManager::GetTextCompositionFor(this);
370 MOZ_ASSERT(!composition,
371 "When there is composition caused by old native IME context, "
372 "composition events caused by different native IME context are not "
373 "allowed");
375 #endif // #ifdef DEBUG
376 mNativeIMEContext = compositionEvent->mNativeIMEContext;
379 // If the event is a composition event or a keyboard event, it should be
380 // dispatched with TextEventDispatcher if we could do that with current
381 // design. However, we cannot do that without big changes and the behavior
382 // is not so complicated for now. Therefore, we should just notify it
383 // of dispatching events and TextEventDispatcher should emulate the state
384 // with events here.
385 if (aEvent->mClass == eCompositionEventClass ||
386 aEvent->mClass == eKeyboardEventClass) {
387 TextEventDispatcher* dispatcher = GetTextEventDispatcher();
388 // However, if the event is being dispatched by the text event dispatcher
389 // or, there is native text event dispatcher listener, that means that
390 // native text input event handler is in this process like on Android,
391 // and the event is not synthesized for tests, the event is coming from
392 // the TextEventDispatcher. In these cases, we shouldn't notify
393 // TextEventDispatcher of dispatching the event.
394 if (!dispatcher->IsDispatchingEvent() &&
395 !(mNativeTextEventDispatcherListener &&
396 !aEvent->mFlags.mIsSynthesizedForTests)) {
397 DebugOnly<nsresult> rv =
398 dispatcher->BeginInputTransactionFor(aEvent, this);
399 NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
400 "The text event dispatcher should always succeed to start input "
401 "transaction for the event");
405 aStatus = nsEventStatus_eIgnore;
407 if (GetCurrentWidgetListener()) {
408 aStatus =
409 GetCurrentWidgetListener()->HandleEvent(aEvent, mUseAttachedEvents);
412 return NS_OK;
415 nsEventStatus
416 PuppetWidget::DispatchInputEvent(WidgetInputEvent* aEvent)
418 if (!AsyncPanZoomEnabled()) {
419 nsEventStatus status = nsEventStatus_eIgnore;
420 DispatchEvent(aEvent, status);
421 return status;
424 if (!mTabChild) {
425 return nsEventStatus_eIgnore;
428 switch (aEvent->mClass) {
429 case eWheelEventClass:
430 Unused <<
431 mTabChild->SendDispatchWheelEvent(*aEvent->AsWheelEvent());
432 break;
433 case eMouseEventClass:
434 Unused <<
435 mTabChild->SendDispatchMouseEvent(*aEvent->AsMouseEvent());
436 break;
437 case eKeyboardEventClass:
438 Unused <<
439 mTabChild->SendDispatchKeyboardEvent(*aEvent->AsKeyboardEvent());
440 break;
441 default:
442 MOZ_ASSERT_UNREACHABLE("unsupported event type");
445 return nsEventStatus_eIgnore;
448 nsresult
449 PuppetWidget::SynthesizeNativeKeyEvent(int32_t aNativeKeyboardLayout,
450 int32_t aNativeKeyCode,
451 uint32_t aModifierFlags,
452 const nsAString& aCharacters,
453 const nsAString& aUnmodifiedCharacters,
454 nsIObserver* aObserver)
456 AutoObserverNotifier notifier(aObserver, "keyevent");
457 if (!mTabChild) {
458 return NS_ERROR_FAILURE;
460 mTabChild->SendSynthesizeNativeKeyEvent(aNativeKeyboardLayout, aNativeKeyCode,
461 aModifierFlags, nsString(aCharacters),
462 nsString(aUnmodifiedCharacters),
463 notifier.SaveObserver());
464 return NS_OK;
467 nsresult
468 PuppetWidget::SynthesizeNativeMouseEvent(mozilla::LayoutDeviceIntPoint aPoint,
469 uint32_t aNativeMessage,
470 uint32_t aModifierFlags,
471 nsIObserver* aObserver)
473 AutoObserverNotifier notifier(aObserver, "mouseevent");
474 if (!mTabChild) {
475 return NS_ERROR_FAILURE;
477 mTabChild->SendSynthesizeNativeMouseEvent(aPoint, aNativeMessage,
478 aModifierFlags,
479 notifier.SaveObserver());
480 return NS_OK;
483 nsresult
484 PuppetWidget::SynthesizeNativeMouseMove(mozilla::LayoutDeviceIntPoint aPoint,
485 nsIObserver* aObserver)
487 AutoObserverNotifier notifier(aObserver, "mousemove");
488 if (!mTabChild) {
489 return NS_ERROR_FAILURE;
491 mTabChild->SendSynthesizeNativeMouseMove(aPoint, notifier.SaveObserver());
492 return NS_OK;
495 nsresult
496 PuppetWidget::SynthesizeNativeMouseScrollEvent(
497 mozilla::LayoutDeviceIntPoint aPoint,
498 uint32_t aNativeMessage,
499 double aDeltaX,
500 double aDeltaY,
501 double aDeltaZ,
502 uint32_t aModifierFlags,
503 uint32_t aAdditionalFlags,
504 nsIObserver* aObserver)
506 AutoObserverNotifier notifier(aObserver, "mousescrollevent");
507 if (!mTabChild) {
508 return NS_ERROR_FAILURE;
510 mTabChild->SendSynthesizeNativeMouseScrollEvent(aPoint, aNativeMessage,
511 aDeltaX, aDeltaY, aDeltaZ,
512 aModifierFlags,
513 aAdditionalFlags,
514 notifier.SaveObserver());
515 return NS_OK;
518 nsresult
519 PuppetWidget::SynthesizeNativeTouchPoint(uint32_t aPointerId,
520 TouchPointerState aPointerState,
521 LayoutDeviceIntPoint aPoint,
522 double aPointerPressure,
523 uint32_t aPointerOrientation,
524 nsIObserver* aObserver)
526 AutoObserverNotifier notifier(aObserver, "touchpoint");
527 if (!mTabChild) {
528 return NS_ERROR_FAILURE;
530 mTabChild->SendSynthesizeNativeTouchPoint(aPointerId, aPointerState,
531 aPoint, aPointerPressure,
532 aPointerOrientation,
533 notifier.SaveObserver());
534 return NS_OK;
537 nsresult
538 PuppetWidget::SynthesizeNativeTouchTap(LayoutDeviceIntPoint aPoint,
539 bool aLongTap,
540 nsIObserver* aObserver)
542 AutoObserverNotifier notifier(aObserver, "touchtap");
543 if (!mTabChild) {
544 return NS_ERROR_FAILURE;
546 mTabChild->SendSynthesizeNativeTouchTap(aPoint, aLongTap,
547 notifier.SaveObserver());
548 return NS_OK;
551 nsresult
552 PuppetWidget::ClearNativeTouchSequence(nsIObserver* aObserver)
554 AutoObserverNotifier notifier(aObserver, "cleartouch");
555 if (!mTabChild) {
556 return NS_ERROR_FAILURE;
558 mTabChild->SendClearNativeTouchSequence(notifier.SaveObserver());
559 return NS_OK;
562 void
563 PuppetWidget::SetConfirmedTargetAPZC(
564 uint64_t aInputBlockId,
565 const nsTArray<ScrollableLayerGuid>& aTargets) const
567 if (mTabChild) {
568 mTabChild->SetTargetAPZC(aInputBlockId, aTargets);
572 void
573 PuppetWidget::UpdateZoomConstraints(const uint32_t& aPresShellId,
574 const FrameMetrics::ViewID& aViewId,
575 const Maybe<ZoomConstraints>& aConstraints)
577 if (mTabChild) {
578 mTabChild->DoUpdateZoomConstraints(aPresShellId, aViewId, aConstraints);
582 bool
583 PuppetWidget::AsyncPanZoomEnabled() const
585 return mTabChild && mTabChild->AsyncPanZoomEnabled();
588 void
589 PuppetWidget::GetEditCommands(NativeKeyBindingsType aType,
590 const WidgetKeyboardEvent& aEvent,
591 nsTArray<CommandInt>& aCommands)
593 // Validate the arguments.
594 nsIWidget::GetEditCommands(aType, aEvent, aCommands);
596 mTabChild->RequestEditCommands(aType, aEvent, aCommands);
599 LayerManager*
600 PuppetWidget::GetLayerManager(PLayerTransactionChild* aShadowManager,
601 LayersBackend aBackendHint,
602 LayerManagerPersistence aPersistence)
604 if (!mLayerManager) {
605 if (XRE_IsParentProcess()) {
606 // On the parent process there is no CompositorBridgeChild which confuses
607 // some layers code, so we use basic layers instead. Note that we create
608 // a non-retaining layer manager since we don't care about performance.
609 mLayerManager = new BasicLayerManager(BasicLayerManager::BLM_OFFSCREEN);
610 return mLayerManager;
613 // If we know for sure that the parent side of this TabChild is not
614 // connected to the compositor, we don't want to use a "remote" layer
615 // manager like WebRender or Client. Instead we use a Basic one which
616 // can do drawing in this process.
617 MOZ_ASSERT(!mTabChild || mTabChild->IsLayersConnected() != Some(true));
618 mLayerManager = new BasicLayerManager(this);
621 return mLayerManager;
624 bool
625 PuppetWidget::CreateRemoteLayerManager(const std::function<bool(LayerManager*)>& aInitializeFunc)
627 RefPtr<LayerManager> lm;
628 MOZ_ASSERT(mTabChild);
629 if (mTabChild->GetCompositorOptions().UseWebRender()) {
630 lm = new WebRenderLayerManager(this);
631 } else {
632 lm = new ClientLayerManager(this);
635 if (!aInitializeFunc(lm)) {
636 return false;
639 // Force the old LM to self destruct, otherwise if the reference dangles we
640 // could fail to revoke the most recent transaction. We only want to replace
641 // it if we successfully create its successor because a partially initialized
642 // layer manager is worse than a fully initialized but shutdown layer manager.
643 DestroyLayerManager();
644 mLayerManager = lm.forget();
645 return true;
648 nsresult
649 PuppetWidget::RequestIMEToCommitComposition(bool aCancel)
651 if (!mTabChild) {
652 return NS_ERROR_FAILURE;
655 MOZ_ASSERT(!Destroyed());
657 // There must not be composition which is caused by the PuppetWidget instance.
658 if (NS_WARN_IF(!mNativeIMEContext.IsValid())) {
659 return NS_OK;
662 // We've already requested to commit/cancel composition.
663 if (NS_WARN_IF(mIgnoreCompositionEvents)) {
664 #ifdef DEBUG
665 RefPtr<TextComposition> composition =
666 IMEStateManager::GetTextCompositionFor(this);
667 MOZ_ASSERT(!composition);
668 #endif // #ifdef DEBUG
669 return NS_OK;
672 RefPtr<TextComposition> composition =
673 IMEStateManager::GetTextCompositionFor(this);
674 // This method shouldn't be called when there is no text composition instance.
675 if (NS_WARN_IF(!composition)) {
676 return NS_OK;
679 MOZ_DIAGNOSTIC_ASSERT(composition->IsRequestingCommitOrCancelComposition(),
680 "Requesting commit or cancel composition should be requested via "
681 "TextComposition instance");
683 bool isCommitted = false;
684 nsAutoString committedString;
685 if (NS_WARN_IF(!mTabChild->SendRequestIMEToCommitComposition(
686 aCancel, &isCommitted, &committedString))) {
687 return NS_ERROR_FAILURE;
690 // If the composition wasn't committed synchronously, we need to wait async
691 // composition events for destroying the TextComposition instance.
692 if (!isCommitted) {
693 return NS_OK;
696 // Dispatch eCompositionCommit event.
697 WidgetCompositionEvent compositionCommitEvent(true, eCompositionCommit, this);
698 InitEvent(compositionCommitEvent, nullptr);
699 compositionCommitEvent.mData = committedString;
700 nsEventStatus status = nsEventStatus_eIgnore;
701 DispatchEvent(&compositionCommitEvent, status);
703 #ifdef DEBUG
704 RefPtr<TextComposition> currentComposition =
705 IMEStateManager::GetTextCompositionFor(this);
706 MOZ_ASSERT(!currentComposition);
707 #endif // #ifdef DEBUG
709 // Ignore the following composition events until we receive new
710 // eCompositionStart event.
711 mIgnoreCompositionEvents = true;
713 Unused <<
714 mTabChild->SendOnEventNeedingAckHandled(eCompositionCommitRequestHandled);
716 // NOTE: PuppetWidget might be destroyed already.
717 return NS_OK;
720 nsresult
721 PuppetWidget::StartPluginIME(const mozilla::WidgetKeyboardEvent& aKeyboardEvent,
722 int32_t aPanelX, int32_t aPanelY,
723 nsString& aCommitted)
725 if (!mTabChild ||
726 !mTabChild->SendStartPluginIME(aKeyboardEvent, aPanelX,
727 aPanelY, &aCommitted)) {
728 return NS_ERROR_FAILURE;
730 return NS_OK;
733 void
734 PuppetWidget::SetPluginFocused(bool& aFocused)
736 if (mTabChild) {
737 mTabChild->SendSetPluginFocused(aFocused);
741 void
742 PuppetWidget::DefaultProcOfPluginEvent(const WidgetPluginEvent& aEvent)
744 if (!mTabChild) {
745 return;
747 mTabChild->SendDefaultProcOfPluginEvent(aEvent);
750 // When this widget caches input context and currently managed by
751 // IMEStateManager, the cache is valid.
752 bool
753 PuppetWidget::HaveValidInputContextCache() const
755 return (mInputContext.mIMEState.mEnabled != IMEState::UNKNOWN &&
756 IMEStateManager::GetWidgetForActiveInputContext() == this);
759 void
760 PuppetWidget::SetInputContext(const InputContext& aContext,
761 const InputContextAction& aAction)
763 mInputContext = aContext;
764 // Any widget instances cannot cache IME open state because IME open state
765 // can be changed by user but native IME may not notify us of changing the
766 // open state on some platforms.
767 mInputContext.mIMEState.mOpen = IMEState::OPEN_STATE_NOT_SUPPORTED;
768 if (!mTabChild) {
769 return;
771 mTabChild->SendSetInputContext(aContext, aAction);
774 InputContext
775 PuppetWidget::GetInputContext()
777 // XXX Currently, we don't support retrieving IME open state from child
778 // process.
780 // If the cache of input context is valid, we can avoid to use synchronous
781 // IPC.
782 if (HaveValidInputContextCache()) {
783 return mInputContext;
786 NS_WARNING("PuppetWidget::GetInputContext() needs to retrieve it with IPC");
788 // Don't cache InputContext here because this process isn't managing IME
789 // state of the chrome widget. So, we cannot modify mInputContext when
790 // chrome widget is set to new context.
791 InputContext context;
792 if (mTabChild) {
793 mTabChild->SendGetInputContext(&context.mIMEState);
795 return context;
798 NativeIMEContext
799 PuppetWidget::GetNativeIMEContext()
801 return mNativeIMEContext;
804 nsresult
805 PuppetWidget::NotifyIMEOfFocusChange(const IMENotification& aIMENotification)
807 if (!mTabChild) {
808 return NS_ERROR_FAILURE;
811 bool gotFocus = aIMENotification.mMessage == NOTIFY_IME_OF_FOCUS;
812 if (gotFocus) {
813 if (mInputContext.mIMEState.mEnabled != IMEState::PLUGIN) {
814 // When IME gets focus, we should initalize all information of the
815 // content.
816 if (NS_WARN_IF(!mContentCache.CacheAll(this, &aIMENotification))) {
817 return NS_ERROR_FAILURE;
819 } else {
820 // However, if a plugin has focus, only the editor rect information is
821 // available.
822 if (NS_WARN_IF(!mContentCache.CacheEditorRect(this, &aIMENotification))) {
823 return NS_ERROR_FAILURE;
826 } else {
827 // When IME loses focus, we don't need to store anything.
828 mContentCache.Clear();
831 mIMENotificationRequestsOfParent =
832 IMENotificationRequests(IMENotificationRequests::NOTIFY_ALL);
833 RefPtr<PuppetWidget> self = this;
834 mTabChild->SendNotifyIMEFocus(mContentCache, aIMENotification)->Then(
835 mTabChild->TabGroup()->EventTargetFor(TaskCategory::UI),
836 __func__,
837 [self] (IMENotificationRequests aRequests) {
838 self->mIMENotificationRequestsOfParent = aRequests;
840 [self] (mozilla::ipc::ResponseRejectReason aReason) {
841 NS_WARNING("SendNotifyIMEFocus got rejected.");
844 return NS_OK;
847 nsresult
848 PuppetWidget::NotifyIMEOfCompositionUpdate(
849 const IMENotification& aIMENotification)
851 if (NS_WARN_IF(!mTabChild)) {
852 return NS_ERROR_FAILURE;
855 if (mInputContext.mIMEState.mEnabled != IMEState::PLUGIN &&
856 NS_WARN_IF(!mContentCache.CacheSelection(this, &aIMENotification))) {
857 return NS_ERROR_FAILURE;
859 mTabChild->SendNotifyIMECompositionUpdate(mContentCache, aIMENotification);
860 return NS_OK;
863 nsresult
864 PuppetWidget::NotifyIMEOfTextChange(const IMENotification& aIMENotification)
866 MOZ_ASSERT(aIMENotification.mMessage == NOTIFY_IME_OF_TEXT_CHANGE,
867 "Passed wrong notification");
868 if (!mTabChild) {
869 return NS_ERROR_FAILURE;
872 // While a plugin has focus, text change notification shouldn't be available.
873 if (NS_WARN_IF(mInputContext.mIMEState.mEnabled == IMEState::PLUGIN)) {
874 return NS_ERROR_FAILURE;
877 // FYI: text change notification is the first notification after
878 // a user operation changes the content. So, we need to modify
879 // the cache as far as possible here.
881 if (NS_WARN_IF(!mContentCache.CacheText(this, &aIMENotification))) {
882 return NS_ERROR_FAILURE;
885 // TabParent doesn't this this to cache. we don't send the notification
886 // if parent process doesn't request NOTIFY_TEXT_CHANGE.
887 if (mIMENotificationRequestsOfParent.WantTextChange()) {
888 mTabChild->SendNotifyIMETextChange(mContentCache, aIMENotification);
889 } else {
890 mTabChild->SendUpdateContentCache(mContentCache);
892 return NS_OK;
895 nsresult
896 PuppetWidget::NotifyIMEOfSelectionChange(
897 const IMENotification& aIMENotification)
899 MOZ_ASSERT(aIMENotification.mMessage == NOTIFY_IME_OF_SELECTION_CHANGE,
900 "Passed wrong notification");
901 if (!mTabChild) {
902 return NS_ERROR_FAILURE;
905 // While a plugin has focus, selection change notification shouldn't be
906 // available.
907 if (NS_WARN_IF(mInputContext.mIMEState.mEnabled == IMEState::PLUGIN)) {
908 return NS_ERROR_FAILURE;
911 // Note that selection change must be notified after text change if it occurs.
912 // Therefore, we don't need to query text content again here.
913 mContentCache.SetSelection(
914 this,
915 aIMENotification.mSelectionChangeData.mOffset,
916 aIMENotification.mSelectionChangeData.Length(),
917 aIMENotification.mSelectionChangeData.mReversed,
918 aIMENotification.mSelectionChangeData.GetWritingMode());
920 mTabChild->SendNotifyIMESelection(mContentCache, aIMENotification);
922 return NS_OK;
925 nsresult
926 PuppetWidget::NotifyIMEOfMouseButtonEvent(
927 const IMENotification& aIMENotification)
929 if (!mTabChild) {
930 return NS_ERROR_FAILURE;
933 // While a plugin has focus, mouse button event notification shouldn't be
934 // available.
935 if (NS_WARN_IF(mInputContext.mIMEState.mEnabled == IMEState::PLUGIN)) {
936 return NS_ERROR_FAILURE;
940 bool consumedByIME = false;
941 if (!mTabChild->SendNotifyIMEMouseButtonEvent(aIMENotification,
942 &consumedByIME)) {
943 return NS_ERROR_FAILURE;
946 return consumedByIME ? NS_SUCCESS_EVENT_CONSUMED : NS_OK;
949 nsresult
950 PuppetWidget::NotifyIMEOfPositionChange(const IMENotification& aIMENotification)
952 if (NS_WARN_IF(!mTabChild)) {
953 return NS_ERROR_FAILURE;
956 if (NS_WARN_IF(!mContentCache.CacheEditorRect(this, &aIMENotification))) {
957 return NS_ERROR_FAILURE;
959 // While a plugin has focus, selection range isn't available. So, we don't
960 // need to cache it at that time.
961 if (mInputContext.mIMEState.mEnabled != IMEState::PLUGIN &&
962 NS_WARN_IF(!mContentCache.CacheSelection(this, &aIMENotification))) {
963 return NS_ERROR_FAILURE;
965 if (mIMENotificationRequestsOfParent.WantPositionChanged()) {
966 mTabChild->SendNotifyIMEPositionChange(mContentCache, aIMENotification);
967 } else {
968 mTabChild->SendUpdateContentCache(mContentCache);
970 return NS_OK;
973 void
974 PuppetWidget::SetCursor(nsCursor aCursor)
976 // Don't cache on windows, Windowless flash breaks this via async cursor updates.
977 #if !defined(XP_WIN)
978 if (mCursor == aCursor && !mCustomCursor && !mUpdateCursor) {
979 return;
981 #endif
983 mCustomCursor = nullptr;
985 if (mTabChild &&
986 !mTabChild->SendSetCursor(aCursor, mUpdateCursor)) {
987 return;
990 mCursor = aCursor;
991 mUpdateCursor = false;
994 nsresult
995 PuppetWidget::SetCursor(imgIContainer* aCursor,
996 uint32_t aHotspotX, uint32_t aHotspotY)
998 if (!aCursor || !mTabChild) {
999 return NS_OK;
1002 #if !defined(XP_WIN)
1003 if (mCustomCursor == aCursor &&
1004 mCursorHotspotX == aHotspotX &&
1005 mCursorHotspotY == aHotspotY &&
1006 !mUpdateCursor) {
1007 return NS_OK;
1009 #endif
1011 RefPtr<mozilla::gfx::SourceSurface> surface =
1012 aCursor->GetFrame(imgIContainer::FRAME_CURRENT,
1013 imgIContainer::FLAG_SYNC_DECODE);
1014 if (!surface) {
1015 return NS_ERROR_FAILURE;
1018 RefPtr<mozilla::gfx::DataSourceSurface> dataSurface =
1019 surface->GetDataSurface();
1020 if (!dataSurface) {
1021 return NS_ERROR_FAILURE;
1024 size_t length;
1025 int32_t stride;
1026 mozilla::UniquePtr<char[]> surfaceData =
1027 nsContentUtils::GetSurfaceData(WrapNotNull(dataSurface), &length, &stride);
1029 nsDependentCString cursorData(surfaceData.get(), length);
1030 mozilla::gfx::IntSize size = dataSurface->GetSize();
1031 if (!mTabChild->SendSetCustomCursor(cursorData, size.width, size.height, stride,
1032 dataSurface->GetFormat(),
1033 aHotspotX, aHotspotY, mUpdateCursor)) {
1034 return NS_ERROR_FAILURE;
1037 mCursor = eCursorInvalid;
1038 mCustomCursor = aCursor;
1039 mCursorHotspotX = aHotspotX;
1040 mCursorHotspotY = aHotspotY;
1041 mUpdateCursor = false;
1043 return NS_OK;
1046 void
1047 PuppetWidget::ClearCachedCursor()
1049 nsBaseWidget::ClearCachedCursor();
1050 mCustomCursor = nullptr;
1053 nsresult
1054 PuppetWidget::Paint()
1056 MOZ_ASSERT(!mDirtyRegion.IsEmpty(), "paint event logic messed up");
1058 if (!GetCurrentWidgetListener())
1059 return NS_OK;
1061 LayoutDeviceIntRegion region = mDirtyRegion;
1063 // reset repaint tracking
1064 mDirtyRegion.SetEmpty();
1065 mPaintTask.Revoke();
1067 RefPtr<PuppetWidget> strongThis(this);
1069 GetCurrentWidgetListener()->WillPaintWindow(this);
1071 if (GetCurrentWidgetListener()) {
1072 #ifdef DEBUG
1073 debug_DumpPaintEvent(stderr, this, region.ToUnknownRegion(),
1074 "PuppetWidget", 0);
1075 #endif
1077 if (mLayerManager->GetBackendType() == mozilla::layers::LayersBackend::LAYERS_CLIENT ||
1078 mLayerManager->GetBackendType() == mozilla::layers::LayersBackend::LAYERS_WR ||
1079 (mozilla::layers::LayersBackend::LAYERS_BASIC == mLayerManager->GetBackendType() &&
1080 mTabChild && mTabChild->IsLayersConnected().isSome())) {
1081 // Do nothing, the compositor will handle drawing
1082 if (mTabChild) {
1083 mTabChild->NotifyPainted();
1085 } else if (mozilla::layers::LayersBackend::LAYERS_BASIC == mLayerManager->GetBackendType()) {
1086 RefPtr<gfxContext> ctx = gfxContext::CreateOrNull(mDrawTarget);
1087 if (!ctx) {
1088 gfxDevCrash(LogReason::InvalidContext) << "PuppetWidget context problem " << gfx::hexa(mDrawTarget);
1089 return NS_ERROR_FAILURE;
1091 ctx->Rectangle(gfxRect(0,0,0,0));
1092 ctx->Clip();
1093 AutoLayerManagerSetup setupLayerManager(this, ctx,
1094 BufferMode::BUFFER_NONE);
1095 GetCurrentWidgetListener()->PaintWindow(this, region);
1096 if (mTabChild) {
1097 mTabChild->NotifyPainted();
1102 if (GetCurrentWidgetListener()) {
1103 GetCurrentWidgetListener()->DidPaintWindow();
1106 return NS_OK;
1109 void
1110 PuppetWidget::SetChild(PuppetWidget* aChild)
1112 MOZ_ASSERT(this != aChild, "can't parent a widget to itself");
1113 MOZ_ASSERT(!aChild->mChild,
1114 "fake widget 'hierarchy' only expected to have one level");
1116 mChild = aChild;
1119 NS_IMETHODIMP
1120 PuppetWidget::PaintTask::Run()
1122 if (mWidget) {
1123 mWidget->Paint();
1125 return NS_OK;
1128 void
1129 PuppetWidget::PaintNowIfNeeded()
1131 if (IsVisible() && mPaintTask.IsPending()) {
1132 Paint();
1136 void
1137 PuppetWidget::OnMemoryPressure(layers::MemoryPressureReason aWhy)
1139 if (aWhy != MemoryPressureReason::LOW_MEMORY_ONGOING &&
1140 !mVisible &&
1141 mLayerManager &&
1142 XRE_IsContentProcess()) {
1143 mLayerManager->ClearCachedResources();
1147 bool
1148 PuppetWidget::NeedsPaint()
1150 // e10s popups are handled by the parent process, so never should be painted here
1151 if (XRE_IsContentProcess() &&
1152 gRemoteDesktopBehaviorEnabled &&
1153 mWindowType == eWindowType_popup) {
1154 NS_WARNING("Trying to paint an e10s popup in the child process!");
1155 return false;
1158 return mVisible;
1161 float
1162 PuppetWidget::GetDPI()
1164 return mDPI;
1167 double
1168 PuppetWidget::GetDefaultScaleInternal()
1170 return mDefaultScale;
1173 int32_t
1174 PuppetWidget::RoundsWidgetCoordinatesTo()
1176 return mRounding;
1179 void*
1180 PuppetWidget::GetNativeData(uint32_t aDataType)
1182 switch (aDataType) {
1183 case NS_NATIVE_SHAREABLE_WINDOW: {
1184 // NOTE: We can not have a tab child in some situations, such as when we're
1185 // rendering to a fake widget for thumbnails.
1186 if (!mTabChild) {
1187 NS_WARNING("Need TabChild to get the nativeWindow from!");
1189 mozilla::WindowsHandle nativeData = 0;
1190 if (mTabChild) {
1191 nativeData = mTabChild->WidgetNativeData();
1193 return (void*)nativeData;
1195 case NS_NATIVE_WINDOW:
1196 case NS_NATIVE_WIDGET:
1197 case NS_NATIVE_DISPLAY:
1198 // These types are ignored (see bug 1183828, bug 1240891).
1199 break;
1200 case NS_RAW_NATIVE_IME_CONTEXT:
1201 MOZ_CRASH("You need to call GetNativeIMEContext() instead");
1202 case NS_NATIVE_PLUGIN_PORT:
1203 case NS_NATIVE_GRAPHIC:
1204 case NS_NATIVE_SHELLWIDGET:
1205 default:
1206 NS_WARNING("nsWindow::GetNativeData called with bad value");
1207 break;
1209 return nullptr;
1212 #if defined(XP_WIN)
1213 void
1214 PuppetWidget::SetNativeData(uint32_t aDataType, uintptr_t aVal)
1216 switch (aDataType) {
1217 case NS_NATIVE_CHILD_OF_SHAREABLE_WINDOW:
1218 MOZ_ASSERT(mTabChild, "Need TabChild to send the message.");
1219 if (mTabChild) {
1220 mTabChild->SendSetNativeChildOfShareableWindow(aVal);
1222 break;
1223 default:
1224 NS_WARNING("SetNativeData called with unsupported data type.");
1227 #endif
1229 LayoutDeviceIntPoint
1230 PuppetWidget::GetChromeOffset()
1232 if (!GetOwningTabChild()) {
1233 NS_WARNING("PuppetWidget without Tab does not have chrome information.");
1234 return LayoutDeviceIntPoint();
1236 return GetOwningTabChild()->GetChromeOffset();
1239 LayoutDeviceIntPoint
1240 PuppetWidget::GetWindowPosition()
1242 if (!GetOwningTabChild()) {
1243 return LayoutDeviceIntPoint();
1246 int32_t winX, winY, winW, winH;
1247 NS_ENSURE_SUCCESS(GetOwningTabChild()->GetDimensions(0, &winX, &winY, &winW, &winH), LayoutDeviceIntPoint());
1248 return LayoutDeviceIntPoint(winX, winY) + GetOwningTabChild()->GetClientOffset();
1251 LayoutDeviceIntRect
1252 PuppetWidget::GetScreenBounds()
1254 return LayoutDeviceIntRect(WidgetToScreenOffset(), mBounds.Size());
1257 uint32_t PuppetWidget::GetMaxTouchPoints() const
1259 uint32_t maxTouchPoints = 0;
1260 if (mTabChild) {
1261 mTabChild->GetMaxTouchPoints(&maxTouchPoints);
1263 return maxTouchPoints;
1266 void
1267 PuppetWidget::StartAsyncScrollbarDrag(const AsyncDragMetrics& aDragMetrics)
1269 mTabChild->StartScrollbarDrag(aDragMetrics);
1272 PuppetScreen::PuppetScreen(void *nativeScreen)
1276 PuppetScreen::~PuppetScreen()
1280 static ScreenConfiguration
1281 ScreenConfig()
1283 ScreenConfiguration config;
1284 hal::GetCurrentScreenConfiguration(&config);
1285 return config;
1288 nsIntSize
1289 PuppetWidget::GetScreenDimensions()
1291 nsIntRect r = ScreenConfig().rect();
1292 return nsIntSize(r.Width(), r.Height());
1295 NS_IMETHODIMP
1296 PuppetScreen::GetRect(int32_t *outLeft, int32_t *outTop,
1297 int32_t *outWidth, int32_t *outHeight)
1299 nsIntRect r = ScreenConfig().rect();
1300 r.GetRect(outLeft, outTop, outWidth, outHeight);
1301 return NS_OK;
1304 NS_IMETHODIMP
1305 PuppetScreen::GetAvailRect(int32_t *outLeft, int32_t *outTop,
1306 int32_t *outWidth, int32_t *outHeight)
1308 return GetRect(outLeft, outTop, outWidth, outHeight);
1311 NS_IMETHODIMP
1312 PuppetScreen::GetPixelDepth(int32_t *aPixelDepth)
1314 *aPixelDepth = ScreenConfig().pixelDepth();
1315 return NS_OK;
1318 NS_IMETHODIMP
1319 PuppetScreen::GetColorDepth(int32_t *aColorDepth)
1321 *aColorDepth = ScreenConfig().colorDepth();
1322 return NS_OK;
1325 NS_IMPL_ISUPPORTS(PuppetScreenManager, nsIScreenManager)
1327 PuppetScreenManager::PuppetScreenManager()
1329 mOneScreen = new PuppetScreen(nullptr);
1332 PuppetScreenManager::~PuppetScreenManager()
1336 NS_IMETHODIMP
1337 PuppetScreenManager::GetPrimaryScreen(nsIScreen** outScreen)
1339 NS_IF_ADDREF(*outScreen = mOneScreen.get());
1340 return NS_OK;
1343 NS_IMETHODIMP
1344 PuppetScreenManager::ScreenForRect(int32_t inLeft,
1345 int32_t inTop,
1346 int32_t inWidth,
1347 int32_t inHeight,
1348 nsIScreen** outScreen)
1350 return GetPrimaryScreen(outScreen);
1353 nsIWidgetListener*
1354 PuppetWidget::GetCurrentWidgetListener()
1356 if (!mPreviouslyAttachedWidgetListener ||
1357 !mAttachedWidgetListener) {
1358 return mAttachedWidgetListener;
1361 if (mAttachedWidgetListener->GetView()->IsPrimaryFramePaintSuppressed()) {
1362 return mPreviouslyAttachedWidgetListener;
1365 return mAttachedWidgetListener;
1368 void
1369 PuppetWidget::SetCandidateWindowForPlugin(
1370 const CandidateWindowPosition& aPosition)
1372 if (!mTabChild) {
1373 return;
1376 mTabChild->SendSetCandidateWindowForPlugin(aPosition);
1379 void
1380 PuppetWidget::EnableIMEForPlugin(bool aEnable)
1382 if (!mTabChild) {
1383 return;
1386 // If current IME state isn't plugin, we ignore this call.
1387 if (NS_WARN_IF(HaveValidInputContextCache() &&
1388 mInputContext.mIMEState.mEnabled != IMEState::UNKNOWN &&
1389 mInputContext.mIMEState.mEnabled != IMEState::PLUGIN)) {
1390 return;
1393 // We don't have valid state in cache or state is plugin, so delegate to
1394 // chrome process.
1395 mTabChild->SendEnableIMEForPlugin(aEnable);
1398 void
1399 PuppetWidget::ZoomToRect(const uint32_t& aPresShellId,
1400 const FrameMetrics::ViewID& aViewId,
1401 const CSSRect& aRect,
1402 const uint32_t& aFlags)
1404 if (!mTabChild) {
1405 return;
1408 mTabChild->ZoomToRect(aPresShellId, aViewId, aRect, aFlags);
1411 void
1412 PuppetWidget::LookUpDictionary(
1413 const nsAString& aText,
1414 const nsTArray<mozilla::FontRange>& aFontRangeArray,
1415 const bool aIsVertical,
1416 const LayoutDeviceIntPoint& aPoint)
1418 if (!mTabChild) {
1419 return;
1422 mTabChild->SendLookUpDictionary(nsString(aText), aFontRangeArray, aIsVertical, aPoint);
1425 bool
1426 PuppetWidget::HasPendingInputEvent()
1428 if (!mTabChild) {
1429 return false;
1432 bool ret = false;
1434 mTabChild->GetIPCChannel()->PeekMessages(
1435 [&ret](const IPC::Message& aMsg) -> bool {
1436 if (nsContentUtils::IsMessageInputEvent(aMsg)) {
1437 ret = true;
1438 return false; // Stop peeking.
1440 return true;
1444 return ret;
1447 void
1448 PuppetWidget::HandledWindowedPluginKeyEvent(
1449 const NativeEventData& aKeyEventData,
1450 bool aIsConsumed)
1452 if (NS_WARN_IF(mKeyEventInPluginCallbacks.IsEmpty())) {
1453 return;
1455 nsCOMPtr<nsIKeyEventInPluginCallback> callback =
1456 mKeyEventInPluginCallbacks[0];
1457 MOZ_ASSERT(callback);
1458 mKeyEventInPluginCallbacks.RemoveElementAt(0);
1459 callback->HandledWindowedPluginKeyEvent(aKeyEventData, aIsConsumed);
1462 nsresult
1463 PuppetWidget::OnWindowedPluginKeyEvent(const NativeEventData& aKeyEventData,
1464 nsIKeyEventInPluginCallback* aCallback)
1466 if (NS_WARN_IF(!mTabChild)) {
1467 return NS_ERROR_NOT_AVAILABLE;
1469 if (NS_WARN_IF(!mTabChild->SendOnWindowedPluginKeyEvent(aKeyEventData))) {
1470 return NS_ERROR_FAILURE;
1472 mKeyEventInPluginCallbacks.AppendElement(aCallback);
1473 return NS_SUCCESS_EVENT_HANDLED_ASYNCHRONOUSLY;
1476 // TextEventDispatcherListener
1478 NS_IMETHODIMP
1479 PuppetWidget::NotifyIME(TextEventDispatcher* aTextEventDispatcher,
1480 const IMENotification& aIMENotification)
1482 MOZ_ASSERT(aTextEventDispatcher == mTextEventDispatcher);
1484 // If there is different text event dispatcher listener for handling
1485 // text event dispatcher, that means that native keyboard events and
1486 // IME events are handled in this process. Therefore, we don't need
1487 // to send any requests and notifications to the parent process.
1488 if (mNativeTextEventDispatcherListener) {
1489 return NS_ERROR_NOT_IMPLEMENTED;
1492 switch (aIMENotification.mMessage) {
1493 case REQUEST_TO_COMMIT_COMPOSITION:
1494 return RequestIMEToCommitComposition(false);
1495 case REQUEST_TO_CANCEL_COMPOSITION:
1496 return RequestIMEToCommitComposition(true);
1497 case NOTIFY_IME_OF_FOCUS:
1498 case NOTIFY_IME_OF_BLUR:
1499 return NotifyIMEOfFocusChange(aIMENotification);
1500 case NOTIFY_IME_OF_SELECTION_CHANGE:
1501 return NotifyIMEOfSelectionChange(aIMENotification);
1502 case NOTIFY_IME_OF_TEXT_CHANGE:
1503 return NotifyIMEOfTextChange(aIMENotification);
1504 case NOTIFY_IME_OF_COMPOSITION_EVENT_HANDLED:
1505 return NotifyIMEOfCompositionUpdate(aIMENotification);
1506 case NOTIFY_IME_OF_MOUSE_BUTTON_EVENT:
1507 return NotifyIMEOfMouseButtonEvent(aIMENotification);
1508 case NOTIFY_IME_OF_POSITION_CHANGE:
1509 return NotifyIMEOfPositionChange(aIMENotification);
1510 default:
1511 return NS_ERROR_NOT_IMPLEMENTED;
1514 return NS_ERROR_NOT_IMPLEMENTED;
1517 NS_IMETHODIMP_(IMENotificationRequests)
1518 PuppetWidget::GetIMENotificationRequests()
1520 if (mInputContext.mIMEState.mEnabled == IMEState::PLUGIN) {
1521 // If a plugin has focus, we cannot receive text nor selection change
1522 // in the plugin. Therefore, PuppetWidget needs to receive only position
1523 // change event for updating the editor rect cache.
1524 return IMENotificationRequests(
1525 mIMENotificationRequestsOfParent.mWantUpdates |
1526 IMENotificationRequests::NOTIFY_POSITION_CHANGE);
1528 return IMENotificationRequests(
1529 mIMENotificationRequestsOfParent.mWantUpdates |
1530 IMENotificationRequests::NOTIFY_TEXT_CHANGE |
1531 IMENotificationRequests::NOTIFY_POSITION_CHANGE);
1534 NS_IMETHODIMP_(void)
1535 PuppetWidget::OnRemovedFrom(TextEventDispatcher* aTextEventDispatcher)
1537 MOZ_ASSERT(aTextEventDispatcher == mTextEventDispatcher);
1540 NS_IMETHODIMP_(void)
1541 PuppetWidget::WillDispatchKeyboardEvent(
1542 TextEventDispatcher* aTextEventDispatcher,
1543 WidgetKeyboardEvent& aKeyboardEvent,
1544 uint32_t aIndexOfKeypress,
1545 void* aData)
1547 MOZ_ASSERT(aTextEventDispatcher == mTextEventDispatcher);
1550 nsresult
1551 PuppetWidget::SetSystemFont(const nsCString& aFontName)
1553 if (!mTabChild) {
1554 return NS_ERROR_FAILURE;
1557 mTabChild->SendSetSystemFont(aFontName);
1558 return NS_OK;
1561 nsresult
1562 PuppetWidget::GetSystemFont(nsCString& aFontName)
1564 if (!mTabChild) {
1565 return NS_ERROR_FAILURE;
1567 mTabChild->SendGetSystemFont(&aFontName);
1568 return NS_OK;
1571 } // namespace widget
1572 } // namespace mozilla