Bug 1640914 [wpt PR 23771] - Python 3: port tests in resource-timing, a=testonly
[gecko.git] / widget / PuppetWidget.cpp
blob6bc1b794e13b0268fe30d42880cd7fe2d6686b62
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/BrowserChild.h"
13 #include "mozilla/gfx/gfxVars.h"
14 #include "mozilla/Hal.h"
15 #include "mozilla/IMEStateManager.h"
16 #include "mozilla/layers/APZChild.h"
17 #include "mozilla/layers/PLayerTransactionChild.h"
18 #include "mozilla/layers/WebRenderLayerManager.h"
19 #include "mozilla/Preferences.h"
20 #include "mozilla/PresShell.h"
21 #include "mozilla/SchedulerGroup.h"
22 #include "mozilla/StaticPrefs_browser.h"
23 #include "mozilla/TextComposition.h"
24 #include "mozilla/TextEventDispatcher.h"
25 #include "mozilla/TextEvents.h"
26 #include "mozilla/Unused.h"
27 #include "BasicLayers.h"
28 #include "PuppetWidget.h"
29 #include "nsContentUtils.h"
30 #include "nsIWidgetListener.h"
31 #include "imgIContainer.h"
32 #include "nsView.h"
33 #include "nsXPLookAndFeel.h"
34 #include "nsPrintfCString.h"
36 using namespace mozilla;
37 using namespace mozilla::dom;
38 using namespace mozilla::hal;
39 using namespace mozilla::gfx;
40 using namespace mozilla::layers;
41 using namespace mozilla::widget;
43 static void InvalidateRegion(nsIWidget* aWidget,
44 const LayoutDeviceIntRegion& aRegion) {
45 for (auto iter = aRegion.RectIter(); !iter.Done(); iter.Next()) {
46 aWidget->Invalidate(iter.Get());
50 /*static*/
51 already_AddRefed<nsIWidget> nsIWidget::CreatePuppetWidget(
52 BrowserChild* aBrowserChild) {
53 MOZ_ASSERT(!aBrowserChild || nsIWidget::UsePuppetWidgets(),
54 "PuppetWidgets not allowed in this configuration");
56 nsCOMPtr<nsIWidget> widget = new PuppetWidget(aBrowserChild);
57 return widget.forget();
60 namespace mozilla {
61 namespace widget {
63 static bool IsPopup(const nsWidgetInitData* aInitData) {
64 return aInitData && aInitData->mWindowType == eWindowType_popup;
67 static bool MightNeedIMEFocus(const nsWidgetInitData* aInitData) {
68 // In the puppet-widget world, popup widgets are just dummies and
69 // shouldn't try to mess with IME state.
70 #ifdef MOZ_CROSS_PROCESS_IME
71 return !IsPopup(aInitData);
72 #else
73 return false;
74 #endif
77 // Arbitrary, fungible.
78 const size_t PuppetWidget::kMaxDimension = 4000;
80 NS_IMPL_ISUPPORTS_INHERITED(PuppetWidget, nsBaseWidget,
81 TextEventDispatcherListener)
83 PuppetWidget::PuppetWidget(BrowserChild* aBrowserChild)
84 : mBrowserChild(aBrowserChild),
85 mMemoryPressureObserver(nullptr),
86 mDPI(-1),
87 mRounding(1),
88 mDefaultScale(-1),
89 mCursorHotspotX(0),
90 mCursorHotspotY(0),
91 mEnabled(false),
92 mVisible(false),
93 mNeedIMEStateInit(false),
94 mIgnoreCompositionEvents(false) {
95 // Setting 'Unknown' means "not yet cached".
96 mInputContext.mIMEState.mEnabled = IMEState::UNKNOWN;
99 PuppetWidget::~PuppetWidget() { Destroy(); }
101 void PuppetWidget::InfallibleCreate(nsIWidget* aParent,
102 nsNativeWidget aNativeParent,
103 const LayoutDeviceIntRect& aRect,
104 nsWidgetInitData* aInitData) {
105 MOZ_ASSERT(!aNativeParent, "got a non-Puppet native parent");
107 BaseCreate(nullptr, aInitData);
109 mBounds = aRect;
110 mEnabled = true;
111 mVisible = true;
113 mDrawTarget = gfxPlatform::GetPlatform()->CreateOffscreenContentDrawTarget(
114 IntSize(1, 1), SurfaceFormat::B8G8R8A8);
116 mNeedIMEStateInit = MightNeedIMEFocus(aInitData);
118 PuppetWidget* parent = static_cast<PuppetWidget*>(aParent);
119 if (parent) {
120 parent->SetChild(this);
121 mLayerManager = parent->GetLayerManager();
122 } else {
123 Resize(mBounds.X(), mBounds.Y(), mBounds.Width(), mBounds.Height(), false);
125 mMemoryPressureObserver = MemoryPressureObserver::Create(this);
128 nsresult PuppetWidget::Create(nsIWidget* aParent, nsNativeWidget aNativeParent,
129 const LayoutDeviceIntRect& aRect,
130 nsWidgetInitData* aInitData) {
131 InfallibleCreate(aParent, aNativeParent, aRect, aInitData);
132 return NS_OK;
135 void PuppetWidget::InitIMEState() {
136 MOZ_ASSERT(mBrowserChild);
137 if (mNeedIMEStateInit) {
138 mContentCache.Clear();
139 mBrowserChild->SendUpdateContentCache(mContentCache);
140 mIMENotificationRequestsOfParent = IMENotificationRequests();
141 mNeedIMEStateInit = false;
145 already_AddRefed<nsIWidget> PuppetWidget::CreateChild(
146 const LayoutDeviceIntRect& aRect, nsWidgetInitData* aInitData,
147 bool aForceUseIWidgetParent) {
148 bool isPopup = IsPopup(aInitData);
149 nsCOMPtr<nsIWidget> widget = nsIWidget::CreatePuppetWidget(mBrowserChild);
150 return ((widget && NS_SUCCEEDED(widget->Create(isPopup ? nullptr : this,
151 nullptr, aRect, aInitData)))
152 ? widget.forget()
153 : nullptr);
156 void PuppetWidget::Destroy() {
157 if (mOnDestroyCalled) {
158 return;
160 mOnDestroyCalled = true;
162 Base::OnDestroy();
163 Base::Destroy();
164 mPaintTask.Revoke();
165 if (mMemoryPressureObserver) {
166 mMemoryPressureObserver->Unregister();
167 mMemoryPressureObserver = nullptr;
169 mChild = nullptr;
170 if (mLayerManager) {
171 mLayerManager->Destroy();
173 mLayerManager = nullptr;
174 mBrowserChild = nullptr;
177 void PuppetWidget::Show(bool aState) {
178 NS_ASSERTION(mEnabled,
179 "does it make sense to Show()/Hide() a disabled widget?");
181 bool wasVisible = mVisible;
182 mVisible = aState;
184 if (mChild) {
185 mChild->mVisible = aState;
188 if (!wasVisible && mVisible) {
189 // The previously attached widget listener is handy if
190 // we're transitioning from page to page without dropping
191 // layers (since we'll continue to show the old layers
192 // associated with that old widget listener). If the
193 // PuppetWidget was hidden, those layers are dropped,
194 // so the previously attached widget listener is really
195 // of no use anymore (and is actually actively harmful - see
196 // bug 1323586).
197 mPreviouslyAttachedWidgetListener = nullptr;
198 Resize(mBounds.Width(), mBounds.Height(), false);
199 Invalidate(mBounds);
203 void PuppetWidget::Resize(double aWidth, double aHeight, bool aRepaint) {
204 LayoutDeviceIntRect oldBounds = mBounds;
205 mBounds.SizeTo(
206 LayoutDeviceIntSize(NSToIntRound(aWidth), NSToIntRound(aHeight)));
208 if (mChild) {
209 mChild->Resize(aWidth, aHeight, aRepaint);
210 return;
213 // XXX: roc says that |aRepaint| dictates whether or not to
214 // invalidate the expanded area
215 if (oldBounds.Size() < mBounds.Size() && aRepaint) {
216 LayoutDeviceIntRegion dirty(mBounds);
217 dirty.Sub(dirty, oldBounds);
218 InvalidateRegion(this, dirty);
221 // call WindowResized() on both the current listener, and possibly
222 // also the previous one if we're in a state where we're drawing that one
223 // because the current one is paint suppressed
224 if (!oldBounds.IsEqualEdges(mBounds) && mAttachedWidgetListener) {
225 if (GetCurrentWidgetListener() &&
226 GetCurrentWidgetListener() != mAttachedWidgetListener) {
227 GetCurrentWidgetListener()->WindowResized(this, mBounds.Width(),
228 mBounds.Height());
230 mAttachedWidgetListener->WindowResized(this, mBounds.Width(),
231 mBounds.Height());
235 nsresult PuppetWidget::ConfigureChildren(
236 const nsTArray<Configuration>& aConfigurations) {
237 for (uint32_t i = 0; i < aConfigurations.Length(); ++i) {
238 const Configuration& configuration = aConfigurations[i];
239 PuppetWidget* w = static_cast<PuppetWidget*>(configuration.mChild.get());
240 NS_ASSERTION(w->GetParent() == this, "Configured widget is not a child");
241 w->SetWindowClipRegion(configuration.mClipRegion, true);
242 LayoutDeviceIntRect bounds = w->GetBounds();
243 if (bounds.Size() != configuration.mBounds.Size()) {
244 w->Resize(configuration.mBounds.X(), configuration.mBounds.Y(),
245 configuration.mBounds.Width(), configuration.mBounds.Height(),
246 true);
247 } else if (bounds.TopLeft() != configuration.mBounds.TopLeft()) {
248 w->Move(configuration.mBounds.X(), configuration.mBounds.Y());
250 w->SetWindowClipRegion(configuration.mClipRegion, false);
252 return NS_OK;
255 void PuppetWidget::SetFocus(Raise aRaise, CallerType aCallerType) {
256 if (aRaise == Raise::Yes && mBrowserChild) {
257 mBrowserChild->SendRequestFocus(true, aCallerType);
261 void PuppetWidget::Invalidate(const LayoutDeviceIntRect& aRect) {
262 #ifdef DEBUG
263 debug_DumpInvalidate(stderr, this, &aRect, "PuppetWidget", 0);
264 #endif
266 if (mChild) {
267 mChild->Invalidate(aRect);
268 return;
271 mDirtyRegion.Or(mDirtyRegion, aRect);
273 if (mBrowserChild && !mDirtyRegion.IsEmpty() && !mPaintTask.IsPending()) {
274 mPaintTask = new PaintTask(this);
275 nsCOMPtr<nsIRunnable> event(mPaintTask.get());
276 SchedulerGroup::Dispatch(TaskCategory::Other, event.forget());
277 return;
281 mozilla::LayoutDeviceToLayoutDeviceMatrix4x4
282 PuppetWidget::WidgetToTopLevelWidgetTransform() {
283 if (!GetOwningBrowserChild()) {
284 NS_WARNING("PuppetWidget without Tab does not have transform information.");
285 return mozilla::LayoutDeviceToLayoutDeviceMatrix4x4();
287 return GetOwningBrowserChild()->GetChildToParentConversionMatrix();
290 void PuppetWidget::InitEvent(WidgetGUIEvent& aEvent,
291 LayoutDeviceIntPoint* aPoint) {
292 if (nullptr == aPoint) {
293 aEvent.mRefPoint = LayoutDeviceIntPoint(0, 0);
294 } else {
295 // use the point override if provided
296 aEvent.mRefPoint = *aPoint;
298 aEvent.mTime = PR_Now() / 1000;
301 nsresult PuppetWidget::DispatchEvent(WidgetGUIEvent* aEvent,
302 nsEventStatus& aStatus) {
303 #ifdef DEBUG
304 debug_DumpEvent(stdout, aEvent->mWidget, aEvent, "PuppetWidget", 0);
305 #endif
307 MOZ_ASSERT(!mChild || mChild->mWindowType == eWindowType_popup,
308 "Unexpected event dispatch!");
310 MOZ_ASSERT(!aEvent->AsKeyboardEvent() ||
311 aEvent->mFlags.mIsSynthesizedForTests ||
312 aEvent->AsKeyboardEvent()->AreAllEditCommandsInitialized(),
313 "Non-sysnthesized keyboard events should have edit commands for "
314 "all types "
315 "before dispatched");
317 if (aEvent->mClass == eCompositionEventClass) {
318 // If we've already requested to commit/cancel the latest composition,
319 // TextComposition for the old composition has been destroyed. Then,
320 // the DOM tree needs to listen to next eCompositionStart and its
321 // following events. So, until we meet new eCompositionStart, let's
322 // discard all unnecessary composition events here.
323 if (mIgnoreCompositionEvents) {
324 if (aEvent->mMessage != eCompositionStart) {
325 aStatus = nsEventStatus_eIgnore;
326 return NS_OK;
328 // Now, we receive new eCompositionStart. Let's restart to handle
329 // composition in this process.
330 mIgnoreCompositionEvents = false;
332 // Store the latest native IME context of parent process's widget or
333 // TextEventDispatcher if it's in this process.
334 WidgetCompositionEvent* compositionEvent = aEvent->AsCompositionEvent();
335 #ifdef DEBUG
336 if (mNativeIMEContext.IsValid() &&
337 mNativeIMEContext != compositionEvent->mNativeIMEContext) {
338 RefPtr<TextComposition> composition =
339 IMEStateManager::GetTextCompositionFor(this);
340 MOZ_ASSERT(
341 !composition,
342 "When there is composition caused by old native IME context, "
343 "composition events caused by different native IME context are not "
344 "allowed");
346 #endif // #ifdef DEBUG
347 mNativeIMEContext = compositionEvent->mNativeIMEContext;
350 // If the event is a composition event or a keyboard event, it should be
351 // dispatched with TextEventDispatcher if we could do that with current
352 // design. However, we cannot do that without big changes and the behavior
353 // is not so complicated for now. Therefore, we should just notify it
354 // of dispatching events and TextEventDispatcher should emulate the state
355 // with events here.
356 if (aEvent->mClass == eCompositionEventClass ||
357 aEvent->mClass == eKeyboardEventClass) {
358 TextEventDispatcher* dispatcher = GetTextEventDispatcher();
359 // However, if the event is being dispatched by the text event dispatcher
360 // or, there is native text event dispatcher listener, that means that
361 // native text input event handler is in this process like on Android,
362 // and the event is not synthesized for tests, the event is coming from
363 // the TextEventDispatcher. In these cases, we shouldn't notify
364 // TextEventDispatcher of dispatching the event.
365 if (!dispatcher->IsDispatchingEvent() &&
366 !(mNativeTextEventDispatcherListener &&
367 !aEvent->mFlags.mIsSynthesizedForTests)) {
368 DebugOnly<nsresult> rv =
369 dispatcher->BeginInputTransactionFor(aEvent, this);
370 NS_WARNING_ASSERTION(
371 NS_SUCCEEDED(rv),
372 "The text event dispatcher should always succeed to start input "
373 "transaction for the event");
377 aStatus = nsEventStatus_eIgnore;
379 if (GetCurrentWidgetListener()) {
380 aStatus =
381 GetCurrentWidgetListener()->HandleEvent(aEvent, mUseAttachedEvents);
384 return NS_OK;
387 nsEventStatus PuppetWidget::DispatchInputEvent(WidgetInputEvent* aEvent) {
388 if (!AsyncPanZoomEnabled()) {
389 nsEventStatus status = nsEventStatus_eIgnore;
390 DispatchEvent(aEvent, status);
391 return status;
394 if (!mBrowserChild) {
395 return nsEventStatus_eIgnore;
398 switch (aEvent->mClass) {
399 case eWheelEventClass:
400 Unused << mBrowserChild->SendDispatchWheelEvent(*aEvent->AsWheelEvent());
401 break;
402 case eMouseEventClass:
403 Unused << mBrowserChild->SendDispatchMouseEvent(*aEvent->AsMouseEvent());
404 break;
405 case eKeyboardEventClass:
406 Unused << mBrowserChild->SendDispatchKeyboardEvent(
407 *aEvent->AsKeyboardEvent());
408 break;
409 default:
410 MOZ_ASSERT_UNREACHABLE("unsupported event type");
413 return nsEventStatus_eIgnore;
416 nsresult PuppetWidget::SynthesizeNativeKeyEvent(
417 int32_t aNativeKeyboardLayout, int32_t aNativeKeyCode,
418 uint32_t aModifierFlags, const nsAString& aCharacters,
419 const nsAString& aUnmodifiedCharacters, nsIObserver* aObserver) {
420 AutoObserverNotifier notifier(aObserver, "keyevent");
421 if (!mBrowserChild) {
422 return NS_ERROR_FAILURE;
424 mBrowserChild->SendSynthesizeNativeKeyEvent(
425 aNativeKeyboardLayout, aNativeKeyCode, aModifierFlags,
426 nsString(aCharacters), nsString(aUnmodifiedCharacters),
427 notifier.SaveObserver());
428 return NS_OK;
431 nsresult PuppetWidget::SynthesizeNativeMouseEvent(
432 mozilla::LayoutDeviceIntPoint aPoint, uint32_t aNativeMessage,
433 uint32_t aModifierFlags, nsIObserver* aObserver) {
434 AutoObserverNotifier notifier(aObserver, "mouseevent");
435 if (!mBrowserChild) {
436 return NS_ERROR_FAILURE;
438 mBrowserChild->SendSynthesizeNativeMouseEvent(
439 aPoint, aNativeMessage, aModifierFlags, notifier.SaveObserver());
440 return NS_OK;
443 nsresult PuppetWidget::SynthesizeNativeMouseMove(
444 mozilla::LayoutDeviceIntPoint aPoint, nsIObserver* aObserver) {
445 AutoObserverNotifier notifier(aObserver, "mousemove");
446 if (!mBrowserChild) {
447 return NS_ERROR_FAILURE;
449 mBrowserChild->SendSynthesizeNativeMouseMove(aPoint, notifier.SaveObserver());
450 return NS_OK;
453 nsresult PuppetWidget::SynthesizeNativeMouseScrollEvent(
454 mozilla::LayoutDeviceIntPoint aPoint, uint32_t aNativeMessage,
455 double aDeltaX, double aDeltaY, double aDeltaZ, uint32_t aModifierFlags,
456 uint32_t aAdditionalFlags, nsIObserver* aObserver) {
457 AutoObserverNotifier notifier(aObserver, "mousescrollevent");
458 if (!mBrowserChild) {
459 return NS_ERROR_FAILURE;
461 mBrowserChild->SendSynthesizeNativeMouseScrollEvent(
462 aPoint, aNativeMessage, aDeltaX, aDeltaY, aDeltaZ, aModifierFlags,
463 aAdditionalFlags, notifier.SaveObserver());
464 return NS_OK;
467 nsresult PuppetWidget::SynthesizeNativeTouchPoint(
468 uint32_t aPointerId, TouchPointerState aPointerState,
469 LayoutDeviceIntPoint aPoint, double aPointerPressure,
470 uint32_t aPointerOrientation, nsIObserver* aObserver) {
471 AutoObserverNotifier notifier(aObserver, "touchpoint");
472 if (!mBrowserChild) {
473 return NS_ERROR_FAILURE;
475 mBrowserChild->SendSynthesizeNativeTouchPoint(
476 aPointerId, aPointerState, aPoint, aPointerPressure, aPointerOrientation,
477 notifier.SaveObserver());
478 return NS_OK;
481 nsresult PuppetWidget::SynthesizeNativeTouchTap(LayoutDeviceIntPoint aPoint,
482 bool aLongTap,
483 nsIObserver* aObserver) {
484 AutoObserverNotifier notifier(aObserver, "touchtap");
485 if (!mBrowserChild) {
486 return NS_ERROR_FAILURE;
488 mBrowserChild->SendSynthesizeNativeTouchTap(aPoint, aLongTap,
489 notifier.SaveObserver());
490 return NS_OK;
493 nsresult PuppetWidget::ClearNativeTouchSequence(nsIObserver* aObserver) {
494 AutoObserverNotifier notifier(aObserver, "cleartouch");
495 if (!mBrowserChild) {
496 return NS_ERROR_FAILURE;
498 mBrowserChild->SendClearNativeTouchSequence(notifier.SaveObserver());
499 return NS_OK;
502 void PuppetWidget::SetConfirmedTargetAPZC(
503 uint64_t aInputBlockId,
504 const nsTArray<ScrollableLayerGuid>& aTargets) const {
505 if (mBrowserChild) {
506 mBrowserChild->SetTargetAPZC(aInputBlockId, aTargets);
510 void PuppetWidget::UpdateZoomConstraints(
511 const uint32_t& aPresShellId, const ScrollableLayerGuid::ViewID& aViewId,
512 const Maybe<ZoomConstraints>& aConstraints) {
513 if (mBrowserChild) {
514 mBrowserChild->DoUpdateZoomConstraints(aPresShellId, aViewId, aConstraints);
518 bool PuppetWidget::AsyncPanZoomEnabled() const {
519 return mBrowserChild && mBrowserChild->AsyncPanZoomEnabled();
522 bool PuppetWidget::GetEditCommands(NativeKeyBindingsType aType,
523 const WidgetKeyboardEvent& aEvent,
524 nsTArray<CommandInt>& aCommands) {
525 // Validate the arguments.
526 if (NS_WARN_IF(!nsIWidget::GetEditCommands(aType, aEvent, aCommands))) {
527 return false;
529 if (NS_WARN_IF(!mBrowserChild)) {
530 return false;
532 mBrowserChild->RequestEditCommands(aType, aEvent, aCommands);
533 return true;
536 LayerManager* PuppetWidget::GetLayerManager(
537 PLayerTransactionChild* aShadowManager, LayersBackend aBackendHint,
538 LayerManagerPersistence aPersistence) {
539 if (!mLayerManager) {
540 if (XRE_IsParentProcess()) {
541 // On the parent process there is no CompositorBridgeChild which confuses
542 // some layers code, so we use basic layers instead. Note that we create
543 // a non-retaining layer manager since we don't care about performance.
544 mLayerManager = new BasicLayerManager(BasicLayerManager::BLM_OFFSCREEN);
545 return mLayerManager;
548 // If we know for sure that the parent side of this BrowserChild is not
549 // connected to the compositor, we don't want to use a "remote" layer
550 // manager like WebRender or Client. Instead we use a Basic one which
551 // can do drawing in this process.
552 MOZ_ASSERT(!mBrowserChild ||
553 mBrowserChild->IsLayersConnected() != Some(true));
554 mLayerManager = new BasicLayerManager(this);
557 return mLayerManager;
560 bool PuppetWidget::CreateRemoteLayerManager(
561 const std::function<bool(LayerManager*)>& aInitializeFunc) {
562 RefPtr<LayerManager> lm;
563 MOZ_ASSERT(mBrowserChild);
564 if (mBrowserChild->GetCompositorOptions().UseWebRender()) {
565 lm = new WebRenderLayerManager(this);
566 } else {
567 lm = new ClientLayerManager(this);
570 if (!aInitializeFunc(lm)) {
571 return false;
574 // Force the old LM to self destruct, otherwise if the reference dangles we
575 // could fail to revoke the most recent transaction. We only want to replace
576 // it if we successfully create its successor because a partially initialized
577 // layer manager is worse than a fully initialized but shutdown layer manager.
578 DestroyLayerManager();
579 mLayerManager = std::move(lm);
580 return true;
583 nsresult PuppetWidget::RequestIMEToCommitComposition(bool aCancel) {
584 if (!mBrowserChild) {
585 return NS_ERROR_FAILURE;
588 MOZ_ASSERT(!Destroyed());
590 // There must not be composition which is caused by the PuppetWidget instance.
591 if (NS_WARN_IF(!mNativeIMEContext.IsValid())) {
592 return NS_OK;
595 // We've already requested to commit/cancel composition.
596 if (NS_WARN_IF(mIgnoreCompositionEvents)) {
597 #ifdef DEBUG
598 RefPtr<TextComposition> composition =
599 IMEStateManager::GetTextCompositionFor(this);
600 MOZ_ASSERT(!composition);
601 #endif // #ifdef DEBUG
602 return NS_OK;
605 RefPtr<TextComposition> composition =
606 IMEStateManager::GetTextCompositionFor(this);
607 // This method shouldn't be called when there is no text composition instance.
608 if (NS_WARN_IF(!composition)) {
609 return NS_OK;
612 MOZ_DIAGNOSTIC_ASSERT(
613 composition->IsRequestingCommitOrCancelComposition(),
614 "Requesting commit or cancel composition should be requested via "
615 "TextComposition instance");
617 bool isCommitted = false;
618 nsAutoString committedString;
619 if (NS_WARN_IF(!mBrowserChild->SendRequestIMEToCommitComposition(
620 aCancel, &isCommitted, &committedString))) {
621 return NS_ERROR_FAILURE;
624 // If the composition wasn't committed synchronously, we need to wait async
625 // composition events for destroying the TextComposition instance.
626 if (!isCommitted) {
627 return NS_OK;
630 // Dispatch eCompositionCommit event.
631 WidgetCompositionEvent compositionCommitEvent(true, eCompositionCommit, this);
632 InitEvent(compositionCommitEvent, nullptr);
633 compositionCommitEvent.mData = committedString;
634 nsEventStatus status = nsEventStatus_eIgnore;
635 DispatchEvent(&compositionCommitEvent, status);
637 #ifdef DEBUG
638 RefPtr<TextComposition> currentComposition =
639 IMEStateManager::GetTextCompositionFor(this);
640 MOZ_ASSERT(!currentComposition);
641 #endif // #ifdef DEBUG
643 // Ignore the following composition events until we receive new
644 // eCompositionStart event.
645 mIgnoreCompositionEvents = true;
647 Unused << mBrowserChild->SendOnEventNeedingAckHandled(
648 eCompositionCommitRequestHandled);
650 // NOTE: PuppetWidget might be destroyed already.
651 return NS_OK;
654 nsresult PuppetWidget::StartPluginIME(const WidgetKeyboardEvent& aKeyboardEvent,
655 int32_t aPanelX, int32_t aPanelY,
656 nsString& aCommitted) {
657 DebugOnly<bool> propagationAlreadyStopped =
658 aKeyboardEvent.mFlags.mPropagationStopped;
659 DebugOnly<bool> immediatePropagationAlreadyStopped =
660 aKeyboardEvent.mFlags.mImmediatePropagationStopped;
661 if (!mBrowserChild || !mBrowserChild->SendStartPluginIME(
662 aKeyboardEvent, aPanelX, aPanelY, &aCommitted)) {
663 return NS_ERROR_FAILURE;
665 // BrowserChild::SendStartPluginIME() sends back the keyboard event to the
666 // main process synchronously. At this time,
667 // ParamTraits<WidgetEvent>::Write() marks the event as "posted to remote
668 // process". However, this is not correct here since the event has been
669 // handled synchronously in the main process. So, we adjust the cross process
670 // dispatching state here.
671 const_cast<WidgetKeyboardEvent&>(aKeyboardEvent)
672 .ResetCrossProcessDispatchingState();
673 // Although it shouldn't occur in content process,
674 // ResetCrossProcessDispatchingState() may reset propagation state too
675 // if the event was posted to a remote process and we're waiting its
676 // result. So, if you saw hitting the following assertions, you'd
677 // need to restore the propagation state too.
678 MOZ_ASSERT(propagationAlreadyStopped ==
679 aKeyboardEvent.mFlags.mPropagationStopped);
680 MOZ_ASSERT(immediatePropagationAlreadyStopped ==
681 aKeyboardEvent.mFlags.mImmediatePropagationStopped);
682 return NS_OK;
685 void PuppetWidget::SetPluginFocused(bool& aFocused) {
686 if (mBrowserChild) {
687 mBrowserChild->SendSetPluginFocused(aFocused);
691 void PuppetWidget::DefaultProcOfPluginEvent(const WidgetPluginEvent& aEvent) {
692 if (!mBrowserChild) {
693 return;
695 mBrowserChild->SendDefaultProcOfPluginEvent(aEvent);
698 // When this widget caches input context and currently managed by
699 // IMEStateManager, the cache is valid.
700 bool PuppetWidget::HaveValidInputContextCache() const {
701 return (mInputContext.mIMEState.mEnabled != IMEState::UNKNOWN &&
702 IMEStateManager::GetWidgetForActiveInputContext() == this);
705 void PuppetWidget::SetInputContext(const InputContext& aContext,
706 const InputContextAction& aAction) {
707 mInputContext = aContext;
708 // Any widget instances cannot cache IME open state because IME open state
709 // can be changed by user but native IME may not notify us of changing the
710 // open state on some platforms.
711 mInputContext.mIMEState.mOpen = IMEState::OPEN_STATE_NOT_SUPPORTED;
712 if (!mBrowserChild) {
713 return;
715 mBrowserChild->SendSetInputContext(aContext, aAction);
718 InputContext PuppetWidget::GetInputContext() {
719 // XXX Currently, we don't support retrieving IME open state from child
720 // process.
722 // If the cache of input context is valid, we can avoid to use synchronous
723 // IPC.
724 if (HaveValidInputContextCache()) {
725 return mInputContext;
728 NS_WARNING("PuppetWidget::GetInputContext() needs to retrieve it with IPC");
730 // Don't cache InputContext here because this process isn't managing IME
731 // state of the chrome widget. So, we cannot modify mInputContext when
732 // chrome widget is set to new context.
733 InputContext context;
734 if (mBrowserChild) {
735 mBrowserChild->SendGetInputContext(&context.mIMEState);
737 return context;
740 NativeIMEContext PuppetWidget::GetNativeIMEContext() {
741 return mNativeIMEContext;
744 nsresult PuppetWidget::NotifyIMEOfFocusChange(
745 const IMENotification& aIMENotification) {
746 if (!mBrowserChild) {
747 return NS_ERROR_FAILURE;
750 bool gotFocus = aIMENotification.mMessage == NOTIFY_IME_OF_FOCUS;
751 if (gotFocus) {
752 if (mInputContext.mIMEState.mEnabled != IMEState::PLUGIN) {
753 // When IME gets focus, we should initalize all information of the
754 // content.
755 if (NS_WARN_IF(!mContentCache.CacheAll(this, &aIMENotification))) {
756 return NS_ERROR_FAILURE;
758 } else {
759 // However, if a plugin has focus, only the editor rect information is
760 // available.
761 if (NS_WARN_IF(!mContentCache.CacheEditorRect(this, &aIMENotification))) {
762 return NS_ERROR_FAILURE;
765 } else {
766 // When IME loses focus, we don't need to store anything.
767 mContentCache.Clear();
770 mIMENotificationRequestsOfParent =
771 IMENotificationRequests(IMENotificationRequests::NOTIFY_ALL);
772 RefPtr<PuppetWidget> self = this;
773 mBrowserChild->SendNotifyIMEFocus(mContentCache, aIMENotification)
774 ->Then(
775 GetMainThreadSerialEventTarget(), __func__,
776 [self](IMENotificationRequests&& aRequests) {
777 self->mIMENotificationRequestsOfParent = aRequests;
778 if (TextEventDispatcher* dispatcher =
779 self->GetTextEventDispatcher()) {
780 dispatcher->OnWidgetChangeIMENotificationRequests(self);
783 [self](mozilla::ipc::ResponseRejectReason&& aReason) {
784 NS_WARNING("SendNotifyIMEFocus got rejected.");
787 return NS_OK;
790 nsresult PuppetWidget::NotifyIMEOfCompositionUpdate(
791 const IMENotification& aIMENotification) {
792 if (NS_WARN_IF(!mBrowserChild)) {
793 return NS_ERROR_FAILURE;
796 if (mInputContext.mIMEState.mEnabled != IMEState::PLUGIN &&
797 NS_WARN_IF(!mContentCache.CacheSelection(this, &aIMENotification))) {
798 return NS_ERROR_FAILURE;
800 mBrowserChild->SendNotifyIMECompositionUpdate(mContentCache,
801 aIMENotification);
802 return NS_OK;
805 nsresult PuppetWidget::NotifyIMEOfTextChange(
806 const IMENotification& aIMENotification) {
807 MOZ_ASSERT(aIMENotification.mMessage == NOTIFY_IME_OF_TEXT_CHANGE,
808 "Passed wrong notification");
809 if (!mBrowserChild) {
810 return NS_ERROR_FAILURE;
813 // While a plugin has focus, text change notification shouldn't be available.
814 if (NS_WARN_IF(mInputContext.mIMEState.mEnabled == IMEState::PLUGIN)) {
815 return NS_ERROR_FAILURE;
818 // FYI: text change notification is the first notification after
819 // a user operation changes the content. So, we need to modify
820 // the cache as far as possible here.
822 if (NS_WARN_IF(!mContentCache.CacheText(this, &aIMENotification))) {
823 return NS_ERROR_FAILURE;
826 // BrowserParent doesn't this this to cache. we don't send the notification
827 // if parent process doesn't request NOTIFY_TEXT_CHANGE.
828 if (mIMENotificationRequestsOfParent.WantTextChange()) {
829 mBrowserChild->SendNotifyIMETextChange(mContentCache, aIMENotification);
830 } else {
831 mBrowserChild->SendUpdateContentCache(mContentCache);
833 return NS_OK;
836 nsresult PuppetWidget::NotifyIMEOfSelectionChange(
837 const IMENotification& aIMENotification) {
838 MOZ_ASSERT(aIMENotification.mMessage == NOTIFY_IME_OF_SELECTION_CHANGE,
839 "Passed wrong notification");
840 if (!mBrowserChild) {
841 return NS_ERROR_FAILURE;
844 // While a plugin has focus, selection change notification shouldn't be
845 // available.
846 if (NS_WARN_IF(mInputContext.mIMEState.mEnabled == IMEState::PLUGIN)) {
847 return NS_ERROR_FAILURE;
850 // Note that selection change must be notified after text change if it occurs.
851 // Therefore, we don't need to query text content again here.
852 mContentCache.SetSelection(
853 this, aIMENotification.mSelectionChangeData.mOffset,
854 aIMENotification.mSelectionChangeData.Length(),
855 aIMENotification.mSelectionChangeData.mReversed,
856 aIMENotification.mSelectionChangeData.GetWritingMode());
858 mBrowserChild->SendNotifyIMESelection(mContentCache, aIMENotification);
860 return NS_OK;
863 nsresult PuppetWidget::NotifyIMEOfMouseButtonEvent(
864 const IMENotification& aIMENotification) {
865 if (!mBrowserChild) {
866 return NS_ERROR_FAILURE;
869 // While a plugin has focus, mouse button event notification shouldn't be
870 // available.
871 if (NS_WARN_IF(mInputContext.mIMEState.mEnabled == IMEState::PLUGIN)) {
872 return NS_ERROR_FAILURE;
875 bool consumedByIME = false;
876 if (!mBrowserChild->SendNotifyIMEMouseButtonEvent(aIMENotification,
877 &consumedByIME)) {
878 return NS_ERROR_FAILURE;
881 return consumedByIME ? NS_SUCCESS_EVENT_CONSUMED : NS_OK;
884 nsresult PuppetWidget::NotifyIMEOfPositionChange(
885 const IMENotification& aIMENotification) {
886 if (NS_WARN_IF(!mBrowserChild)) {
887 return NS_ERROR_FAILURE;
890 if (NS_WARN_IF(!mContentCache.CacheEditorRect(this, &aIMENotification))) {
891 return NS_ERROR_FAILURE;
893 // While a plugin has focus, selection range isn't available. So, we don't
894 // need to cache it at that time.
895 if (mInputContext.mIMEState.mEnabled != IMEState::PLUGIN &&
896 NS_WARN_IF(!mContentCache.CacheSelection(this, &aIMENotification))) {
897 return NS_ERROR_FAILURE;
899 if (mIMENotificationRequestsOfParent.WantPositionChanged()) {
900 mBrowserChild->SendNotifyIMEPositionChange(mContentCache, aIMENotification);
901 } else {
902 mBrowserChild->SendUpdateContentCache(mContentCache);
904 return NS_OK;
907 struct CursorSurface {
908 UniquePtr<char[]> mData;
909 IntSize mSize;
912 void PuppetWidget::SetCursor(nsCursor aCursor, imgIContainer* aCursorImage,
913 uint32_t aHotspotX, uint32_t aHotspotY) {
914 if (!mBrowserChild) {
915 return;
918 // Don't cache on windows, Windowless flash breaks this via async cursor
919 // updates.
920 #if !defined(XP_WIN)
921 if (!mUpdateCursor && mCursor == aCursor && mCustomCursor == aCursorImage &&
922 (!aCursorImage ||
923 (mCursorHotspotX == aHotspotX && mCursorHotspotY == aHotspotY))) {
924 return;
926 #endif
928 bool hasCustomCursor = false;
929 UniquePtr<char[]> customCursorData;
930 size_t length = 0;
931 IntSize customCursorSize;
932 int32_t stride = 0;
933 auto format = SurfaceFormat::B8G8R8A8;
934 bool force = mUpdateCursor;
936 if (aCursorImage) {
937 RefPtr<SourceSurface> surface = aCursorImage->GetFrame(
938 imgIContainer::FRAME_CURRENT,
939 imgIContainer::FLAG_SYNC_DECODE | imgIContainer::FLAG_ASYNC_NOTIFY);
940 if (surface) {
941 if (RefPtr<DataSourceSurface> dataSurface = surface->GetDataSurface()) {
942 hasCustomCursor = true;
943 customCursorData = nsContentUtils::GetSurfaceData(
944 WrapNotNull(dataSurface), &length, &stride);
945 customCursorSize = dataSurface->GetSize();
946 format = dataSurface->GetFormat();
951 mCustomCursor = nullptr;
953 nsDependentCString cursorData(customCursorData ? customCursorData.get() : "",
954 length);
955 if (!mBrowserChild->SendSetCursor(aCursor, hasCustomCursor, cursorData,
956 customCursorSize.width,
957 customCursorSize.height, stride, format,
958 aHotspotX, aHotspotY, force)) {
959 return;
962 mCursor = aCursor;
963 mCustomCursor = aCursorImage;
964 mCursorHotspotX = aHotspotX;
965 mCursorHotspotY = aHotspotY;
966 mUpdateCursor = false;
969 void PuppetWidget::ClearCachedCursor() {
970 nsBaseWidget::ClearCachedCursor();
971 mCustomCursor = nullptr;
974 nsresult PuppetWidget::Paint() {
975 MOZ_ASSERT(!mDirtyRegion.IsEmpty(), "paint event logic messed up");
977 if (!GetCurrentWidgetListener()) return NS_OK;
979 LayoutDeviceIntRegion region = mDirtyRegion;
981 // reset repaint tracking
982 mDirtyRegion.SetEmpty();
983 mPaintTask.Revoke();
985 RefPtr<PuppetWidget> strongThis(this);
987 GetCurrentWidgetListener()->WillPaintWindow(this);
989 if (GetCurrentWidgetListener()) {
990 #ifdef DEBUG
991 debug_DumpPaintEvent(stderr, this, region.ToUnknownRegion(), "PuppetWidget",
993 #endif
995 if (mLayerManager->GetBackendType() ==
996 mozilla::layers::LayersBackend::LAYERS_CLIENT ||
997 mLayerManager->GetBackendType() ==
998 mozilla::layers::LayersBackend::LAYERS_WR ||
999 (mozilla::layers::LayersBackend::LAYERS_BASIC ==
1000 mLayerManager->GetBackendType() &&
1001 mBrowserChild && mBrowserChild->IsLayersConnected().isSome())) {
1002 // Do nothing, the compositor will handle drawing
1003 if (mBrowserChild) {
1004 mBrowserChild->NotifyPainted();
1006 } else if (mozilla::layers::LayersBackend::LAYERS_BASIC ==
1007 mLayerManager->GetBackendType()) {
1008 RefPtr<gfxContext> ctx = gfxContext::CreateOrNull(mDrawTarget);
1009 if (!ctx) {
1010 gfxDevCrash(LogReason::InvalidContext)
1011 << "PuppetWidget context problem " << gfx::hexa(mDrawTarget);
1012 return NS_ERROR_FAILURE;
1014 ctx->Rectangle(gfxRect(0, 0, 0, 0));
1015 ctx->Clip();
1016 AutoLayerManagerSetup setupLayerManager(this, ctx,
1017 BufferMode::BUFFER_NONE);
1018 GetCurrentWidgetListener()->PaintWindow(this, region);
1019 if (mBrowserChild) {
1020 mBrowserChild->NotifyPainted();
1025 if (GetCurrentWidgetListener()) {
1026 GetCurrentWidgetListener()->DidPaintWindow();
1029 return NS_OK;
1032 void PuppetWidget::SetChild(PuppetWidget* aChild) {
1033 MOZ_ASSERT(this != aChild, "can't parent a widget to itself");
1034 MOZ_ASSERT(!aChild->mChild,
1035 "fake widget 'hierarchy' only expected to have one level");
1037 mChild = aChild;
1040 NS_IMETHODIMP
1041 PuppetWidget::PaintTask::Run() {
1042 if (mWidget) {
1043 mWidget->Paint();
1045 return NS_OK;
1048 void PuppetWidget::PaintNowIfNeeded() {
1049 if (IsVisible() && mPaintTask.IsPending()) {
1050 Paint();
1054 void PuppetWidget::OnMemoryPressure(layers::MemoryPressureReason aWhy) {
1055 if (aWhy != MemoryPressureReason::LOW_MEMORY_ONGOING && !mVisible &&
1056 mLayerManager && XRE_IsContentProcess()) {
1057 mLayerManager->ClearCachedResources();
1061 bool PuppetWidget::NeedsPaint() {
1062 // e10s popups are handled by the parent process, so never should be painted
1063 // here
1064 if (XRE_IsContentProcess() &&
1065 StaticPrefs::browser_tabs_remote_desktopbehavior() &&
1066 mWindowType == eWindowType_popup) {
1067 NS_WARNING("Trying to paint an e10s popup in the child process!");
1068 return false;
1071 return mVisible;
1074 float PuppetWidget::GetDPI() { return mDPI; }
1076 double PuppetWidget::GetDefaultScaleInternal() { return mDefaultScale; }
1078 int32_t PuppetWidget::RoundsWidgetCoordinatesTo() { return mRounding; }
1080 void* PuppetWidget::GetNativeData(uint32_t aDataType) {
1081 switch (aDataType) {
1082 case NS_NATIVE_SHAREABLE_WINDOW: {
1083 // NOTE: We can not have a tab child in some situations, such as when
1084 // we're rendering to a fake widget for thumbnails.
1085 if (!mBrowserChild) {
1086 NS_WARNING("Need BrowserChild to get the nativeWindow from!");
1088 mozilla::WindowsHandle nativeData = 0;
1089 if (mBrowserChild) {
1090 nativeData = mBrowserChild->WidgetNativeData();
1092 return (void*)nativeData;
1094 case NS_NATIVE_WINDOW:
1095 case NS_NATIVE_WIDGET:
1096 case NS_NATIVE_DISPLAY:
1097 // These types are ignored (see bug 1183828, bug 1240891).
1098 break;
1099 case NS_RAW_NATIVE_IME_CONTEXT:
1100 MOZ_CRASH("You need to call GetNativeIMEContext() instead");
1101 case NS_NATIVE_PLUGIN_PORT:
1102 case NS_NATIVE_GRAPHIC:
1103 case NS_NATIVE_SHELLWIDGET:
1104 default:
1105 NS_WARNING("nsWindow::GetNativeData called with bad value");
1106 break;
1108 return nullptr;
1111 #if defined(XP_WIN)
1112 void PuppetWidget::SetNativeData(uint32_t aDataType, uintptr_t aVal) {
1113 switch (aDataType) {
1114 case NS_NATIVE_CHILD_OF_SHAREABLE_WINDOW:
1115 MOZ_ASSERT(mBrowserChild, "Need BrowserChild to send the message.");
1116 if (mBrowserChild) {
1117 mBrowserChild->SendSetNativeChildOfShareableWindow(aVal);
1119 break;
1120 default:
1121 NS_WARNING("SetNativeData called with unsupported data type.");
1124 #endif
1126 LayoutDeviceIntPoint PuppetWidget::GetChromeOffset() {
1127 if (!GetOwningBrowserChild()) {
1128 NS_WARNING("PuppetWidget without Tab does not have chrome information.");
1129 return LayoutDeviceIntPoint();
1131 return GetOwningBrowserChild()->GetChromeOffset();
1134 LayoutDeviceIntPoint PuppetWidget::WidgetToScreenOffset() {
1135 auto positionRalativeToWindow =
1136 WidgetToTopLevelWidgetTransform().TransformPoint(LayoutDevicePoint());
1138 return GetWindowPosition() +
1139 LayoutDeviceIntPoint::Round(positionRalativeToWindow);
1142 LayoutDeviceIntPoint PuppetWidget::GetWindowPosition() {
1143 if (!GetOwningBrowserChild()) {
1144 return LayoutDeviceIntPoint();
1147 int32_t winX, winY, winW, winH;
1148 NS_ENSURE_SUCCESS(
1149 GetOwningBrowserChild()->GetDimensions(0, &winX, &winY, &winW, &winH),
1150 LayoutDeviceIntPoint());
1151 return LayoutDeviceIntPoint(winX, winY) +
1152 GetOwningBrowserChild()->GetClientOffset();
1155 LayoutDeviceIntRect PuppetWidget::GetScreenBounds() {
1156 return LayoutDeviceIntRect(WidgetToScreenOffset(), mBounds.Size());
1159 LayoutDeviceIntSize PuppetWidget::GetCompositionSize() {
1160 Maybe<LayoutDeviceIntRect> visibleRect =
1161 mBrowserChild ? mBrowserChild->GetVisibleRect() : Nothing();
1162 if (!visibleRect) {
1163 return nsBaseWidget::GetCompositionSize();
1165 return visibleRect->Size();
1168 uint32_t PuppetWidget::GetMaxTouchPoints() const {
1169 return mBrowserChild ? mBrowserChild->MaxTouchPoints() : 0;
1172 void PuppetWidget::StartAsyncScrollbarDrag(
1173 const AsyncDragMetrics& aDragMetrics) {
1174 mBrowserChild->StartScrollbarDrag(aDragMetrics);
1177 PuppetScreen::PuppetScreen(void* nativeScreen) {}
1179 PuppetScreen::~PuppetScreen() = default;
1181 static ScreenConfiguration ScreenConfig() {
1182 ScreenConfiguration config;
1183 hal::GetCurrentScreenConfiguration(&config);
1184 return config;
1187 nsIntSize PuppetWidget::GetScreenDimensions() {
1188 nsIntRect r = ScreenConfig().rect();
1189 return nsIntSize(r.Width(), r.Height());
1192 NS_IMETHODIMP
1193 PuppetScreen::GetRect(int32_t* outLeft, int32_t* outTop, int32_t* outWidth,
1194 int32_t* outHeight) {
1195 nsIntRect r = ScreenConfig().rect();
1196 r.GetRect(outLeft, outTop, outWidth, outHeight);
1197 return NS_OK;
1200 NS_IMETHODIMP
1201 PuppetScreen::GetAvailRect(int32_t* outLeft, int32_t* outTop, int32_t* outWidth,
1202 int32_t* outHeight) {
1203 return GetRect(outLeft, outTop, outWidth, outHeight);
1206 NS_IMETHODIMP
1207 PuppetScreen::GetPixelDepth(int32_t* aPixelDepth) {
1208 *aPixelDepth = ScreenConfig().pixelDepth();
1209 return NS_OK;
1212 NS_IMETHODIMP
1213 PuppetScreen::GetColorDepth(int32_t* aColorDepth) {
1214 *aColorDepth = ScreenConfig().colorDepth();
1215 return NS_OK;
1218 NS_IMPL_ISUPPORTS(PuppetScreenManager, nsIScreenManager)
1220 PuppetScreenManager::PuppetScreenManager() {
1221 mOneScreen = new PuppetScreen(nullptr);
1224 PuppetScreenManager::~PuppetScreenManager() = default;
1226 NS_IMETHODIMP
1227 PuppetScreenManager::GetPrimaryScreen(nsIScreen** outScreen) {
1228 NS_IF_ADDREF(*outScreen = mOneScreen.get());
1229 return NS_OK;
1232 NS_IMETHODIMP
1233 PuppetScreenManager::GetTotalScreenPixels(int64_t* aTotalScreenPixels) {
1234 MOZ_ASSERT(aTotalScreenPixels);
1235 if (mOneScreen) {
1236 int32_t x, y, width, height;
1237 x = y = width = height = 0;
1238 mOneScreen->GetRect(&x, &y, &width, &height);
1239 *aTotalScreenPixels = width * height;
1240 } else {
1241 *aTotalScreenPixels = 0;
1243 return NS_OK;
1246 NS_IMETHODIMP
1247 PuppetScreenManager::ScreenForRect(int32_t inLeft, int32_t inTop,
1248 int32_t inWidth, int32_t inHeight,
1249 nsIScreen** outScreen) {
1250 return GetPrimaryScreen(outScreen);
1253 ScreenIntMargin PuppetWidget::GetSafeAreaInsets() const {
1254 return mSafeAreaInsets;
1257 void PuppetWidget::UpdateSafeAreaInsets(
1258 const ScreenIntMargin& aSafeAreaInsets) {
1259 mSafeAreaInsets = aSafeAreaInsets;
1262 nsIWidgetListener* PuppetWidget::GetCurrentWidgetListener() {
1263 if (!mPreviouslyAttachedWidgetListener || !mAttachedWidgetListener) {
1264 return mAttachedWidgetListener;
1267 if (mAttachedWidgetListener->GetView()->IsPrimaryFramePaintSuppressed()) {
1268 return mPreviouslyAttachedWidgetListener;
1271 return mAttachedWidgetListener;
1274 void PuppetWidget::SetCandidateWindowForPlugin(
1275 const CandidateWindowPosition& aPosition) {
1276 if (!mBrowserChild) {
1277 return;
1280 mBrowserChild->SendSetCandidateWindowForPlugin(aPosition);
1283 void PuppetWidget::EnableIMEForPlugin(bool aEnable) {
1284 if (!mBrowserChild) {
1285 return;
1288 // If current IME state isn't plugin, we ignore this call.
1289 if (NS_WARN_IF(HaveValidInputContextCache() &&
1290 mInputContext.mIMEState.mEnabled != IMEState::UNKNOWN &&
1291 mInputContext.mIMEState.mEnabled != IMEState::PLUGIN)) {
1292 return;
1295 // We don't have valid state in cache or state is plugin, so delegate to
1296 // chrome process.
1297 mBrowserChild->SendEnableIMEForPlugin(aEnable);
1300 void PuppetWidget::ZoomToRect(const uint32_t& aPresShellId,
1301 const ScrollableLayerGuid::ViewID& aViewId,
1302 const CSSRect& aRect, const uint32_t& aFlags) {
1303 if (!mBrowserChild) {
1304 return;
1307 mBrowserChild->ZoomToRect(aPresShellId, aViewId, aRect, aFlags);
1310 void PuppetWidget::LookUpDictionary(
1311 const nsAString& aText, const nsTArray<mozilla::FontRange>& aFontRangeArray,
1312 const bool aIsVertical, const LayoutDeviceIntPoint& aPoint) {
1313 if (!mBrowserChild) {
1314 return;
1317 mBrowserChild->SendLookUpDictionary(nsString(aText), aFontRangeArray,
1318 aIsVertical, aPoint);
1321 bool PuppetWidget::HasPendingInputEvent() {
1322 if (!mBrowserChild) {
1323 return false;
1326 bool ret = false;
1328 mBrowserChild->GetIPCChannel()->PeekMessages(
1329 [&ret](const IPC::Message& aMsg) -> bool {
1330 if (nsContentUtils::IsMessageInputEvent(aMsg)) {
1331 ret = true;
1332 return false; // Stop peeking.
1334 return true;
1337 return ret;
1340 void PuppetWidget::HandledWindowedPluginKeyEvent(
1341 const NativeEventData& aKeyEventData, bool aIsConsumed) {
1342 if (NS_WARN_IF(mKeyEventInPluginCallbacks.IsEmpty())) {
1343 return;
1345 nsCOMPtr<nsIKeyEventInPluginCallback> callback =
1346 mKeyEventInPluginCallbacks[0];
1347 MOZ_ASSERT(callback);
1348 mKeyEventInPluginCallbacks.RemoveElementAt(0);
1349 callback->HandledWindowedPluginKeyEvent(aKeyEventData, aIsConsumed);
1352 nsresult PuppetWidget::OnWindowedPluginKeyEvent(
1353 const NativeEventData& aKeyEventData,
1354 nsIKeyEventInPluginCallback* aCallback) {
1355 if (NS_WARN_IF(!mBrowserChild)) {
1356 return NS_ERROR_NOT_AVAILABLE;
1358 if (NS_WARN_IF(!mBrowserChild->SendOnWindowedPluginKeyEvent(aKeyEventData))) {
1359 return NS_ERROR_FAILURE;
1361 mKeyEventInPluginCallbacks.AppendElement(aCallback);
1362 return NS_SUCCESS_EVENT_HANDLED_ASYNCHRONOUSLY;
1365 // TextEventDispatcherListener
1367 NS_IMETHODIMP
1368 PuppetWidget::NotifyIME(TextEventDispatcher* aTextEventDispatcher,
1369 const IMENotification& aIMENotification) {
1370 MOZ_ASSERT(aTextEventDispatcher == mTextEventDispatcher);
1372 // If there is different text event dispatcher listener for handling
1373 // text event dispatcher, that means that native keyboard events and
1374 // IME events are handled in this process. Therefore, we don't need
1375 // to send any requests and notifications to the parent process.
1376 if (mNativeTextEventDispatcherListener) {
1377 return NS_ERROR_NOT_IMPLEMENTED;
1380 switch (aIMENotification.mMessage) {
1381 case REQUEST_TO_COMMIT_COMPOSITION:
1382 return RequestIMEToCommitComposition(false);
1383 case REQUEST_TO_CANCEL_COMPOSITION:
1384 return RequestIMEToCommitComposition(true);
1385 case NOTIFY_IME_OF_FOCUS:
1386 case NOTIFY_IME_OF_BLUR:
1387 return NotifyIMEOfFocusChange(aIMENotification);
1388 case NOTIFY_IME_OF_SELECTION_CHANGE:
1389 return NotifyIMEOfSelectionChange(aIMENotification);
1390 case NOTIFY_IME_OF_TEXT_CHANGE:
1391 return NotifyIMEOfTextChange(aIMENotification);
1392 case NOTIFY_IME_OF_COMPOSITION_EVENT_HANDLED:
1393 return NotifyIMEOfCompositionUpdate(aIMENotification);
1394 case NOTIFY_IME_OF_MOUSE_BUTTON_EVENT:
1395 return NotifyIMEOfMouseButtonEvent(aIMENotification);
1396 case NOTIFY_IME_OF_POSITION_CHANGE:
1397 return NotifyIMEOfPositionChange(aIMENotification);
1398 default:
1399 return NS_ERROR_NOT_IMPLEMENTED;
1402 return NS_ERROR_NOT_IMPLEMENTED;
1405 NS_IMETHODIMP_(IMENotificationRequests)
1406 PuppetWidget::GetIMENotificationRequests() {
1407 if (mInputContext.mIMEState.mEnabled == IMEState::PLUGIN) {
1408 // If a plugin has focus, we cannot receive text nor selection change
1409 // in the plugin. Therefore, PuppetWidget needs to receive only position
1410 // change event for updating the editor rect cache.
1411 return IMENotificationRequests(
1412 mIMENotificationRequestsOfParent.mWantUpdates |
1413 IMENotificationRequests::NOTIFY_POSITION_CHANGE);
1415 return IMENotificationRequests(
1416 mIMENotificationRequestsOfParent.mWantUpdates |
1417 IMENotificationRequests::NOTIFY_TEXT_CHANGE |
1418 IMENotificationRequests::NOTIFY_POSITION_CHANGE);
1421 NS_IMETHODIMP_(void)
1422 PuppetWidget::OnRemovedFrom(TextEventDispatcher* aTextEventDispatcher) {
1423 MOZ_ASSERT(aTextEventDispatcher == mTextEventDispatcher);
1426 NS_IMETHODIMP_(void)
1427 PuppetWidget::WillDispatchKeyboardEvent(
1428 TextEventDispatcher* aTextEventDispatcher,
1429 WidgetKeyboardEvent& aKeyboardEvent, uint32_t aIndexOfKeypress,
1430 void* aData) {
1431 MOZ_ASSERT(aTextEventDispatcher == mTextEventDispatcher);
1434 nsresult PuppetWidget::SetSystemFont(const nsCString& aFontName) {
1435 if (!mBrowserChild) {
1436 return NS_ERROR_FAILURE;
1439 mBrowserChild->SendSetSystemFont(aFontName);
1440 return NS_OK;
1443 nsresult PuppetWidget::GetSystemFont(nsCString& aFontName) {
1444 if (!mBrowserChild) {
1445 return NS_ERROR_FAILURE;
1447 mBrowserChild->SendGetSystemFont(&aFontName);
1448 return NS_OK;
1451 } // namespace widget
1452 } // namespace mozilla