1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
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/. */
9 * This "puppet widget" isn't really a platform widget. It's intended
10 * to be used in widgetless rendering contexts, such as sandboxed
11 * content processes. If any "real" widgetry is needed, the request
12 * is forwarded to and/or data received from elsewhere.
15 #ifndef mozilla_widget_PuppetWidget_h__
16 #define mozilla_widget_PuppetWidget_h__
18 #include "mozilla/gfx/2D.h"
19 #include "mozilla/RefPtr.h"
20 #include "nsBaseScreen.h"
21 #include "nsBaseWidget.h"
22 #include "nsIScreenManager.h"
23 #include "nsThreadUtils.h"
24 #include "nsWeakReference.h"
25 #include "mozilla/Attributes.h"
26 #include "mozilla/EventForwards.h"
38 struct AutoCacheNativeKeyCommands
;
40 class PuppetWidget
: public nsBaseWidget
,
41 public nsSupportsWeakReference
43 typedef mozilla::dom::TabChild TabChild
;
44 typedef mozilla::gfx::DrawTarget DrawTarget
;
45 typedef nsBaseWidget Base
;
47 // The width and height of the "widget" are clamped to this.
48 static const size_t kMaxDimension
;
51 explicit PuppetWidget(TabChild
* aTabChild
);
54 virtual ~PuppetWidget();
57 NS_DECL_ISUPPORTS_INHERITED
59 NS_IMETHOD
Create(nsIWidget
* aParent
,
60 nsNativeWidget aNativeParent
,
61 const nsIntRect
& aRect
,
62 nsDeviceContext
* aContext
,
63 nsWidgetInitData
* aInitData
= nullptr) MOZ_OVERRIDE
;
67 virtual already_AddRefed
<nsIWidget
>
68 CreateChild(const nsIntRect
&aRect
,
69 nsDeviceContext
*aContext
,
70 nsWidgetInitData
*aInitData
= nullptr,
71 bool aForceUseIWidgetParent
= false) MOZ_OVERRIDE
;
73 NS_IMETHOD
Destroy() MOZ_OVERRIDE
;
75 NS_IMETHOD
Show(bool aState
) MOZ_OVERRIDE
;
77 virtual bool IsVisible() const MOZ_OVERRIDE
80 NS_IMETHOD
ConstrainPosition(bool /*ignored aAllowSlop*/,
82 int32_t* aY
) MOZ_OVERRIDE
83 { *aX
= kMaxDimension
; *aY
= kMaxDimension
; return NS_OK
; }
85 // We're always at <0, 0>, and so ignore move requests.
86 NS_IMETHOD
Move(double aX
, double aY
) MOZ_OVERRIDE
89 NS_IMETHOD
Resize(double aWidth
,
91 bool aRepaint
) MOZ_OVERRIDE
;
92 NS_IMETHOD
Resize(double aX
,
96 bool aRepaint
) MOZ_OVERRIDE
97 // (we're always at <0, 0>)
98 { return Resize(aWidth
, aHeight
, aRepaint
); }
100 // XXX/cjones: copying gtk behavior here; unclear what disabling a
101 // widget is supposed to entail
102 NS_IMETHOD
Enable(bool aState
) MOZ_OVERRIDE
103 { mEnabled
= aState
; return NS_OK
; }
104 virtual bool IsEnabled() const MOZ_OVERRIDE
107 NS_IMETHOD
SetFocus(bool aRaise
= false) MOZ_OVERRIDE
;
109 virtual nsresult
ConfigureChildren(const nsTArray
<Configuration
>& aConfigurations
) MOZ_OVERRIDE
;
111 NS_IMETHOD
Invalidate(const nsIntRect
& aRect
) MOZ_OVERRIDE
;
113 // This API is going away, steer clear.
114 virtual void Scroll(const nsIntPoint
& aDelta
,
115 const nsTArray
<nsIntRect
>& aDestRects
,
116 const nsTArray
<Configuration
>& aReconfigureChildren
)
117 { /* dead man walking */ }
119 // PuppetWidgets don't have native data, as they're purely nonnative.
120 virtual void* GetNativeData(uint32_t aDataType
) MOZ_OVERRIDE
;
121 NS_IMETHOD
ReparentNativeWidget(nsIWidget
* aNewParent
) MOZ_OVERRIDE
122 { return NS_ERROR_UNEXPECTED
; }
124 // PuppetWidgets don't have any concept of titles.
125 NS_IMETHOD
SetTitle(const nsAString
& aTitle
) MOZ_OVERRIDE
126 { return NS_ERROR_UNEXPECTED
; }
128 // PuppetWidgets are always at <0, 0>.
129 virtual nsIntPoint
WidgetToScreenOffset() MOZ_OVERRIDE
130 { return nsIntPoint(0, 0); }
132 void InitEvent(WidgetGUIEvent
& aEvent
, nsIntPoint
* aPoint
= nullptr);
134 NS_IMETHOD
DispatchEvent(WidgetGUIEvent
* aEvent
, nsEventStatus
& aStatus
) MOZ_OVERRIDE
;
136 NS_IMETHOD
CaptureRollupEvents(nsIRollupListener
* aListener
,
137 bool aDoCapture
) MOZ_OVERRIDE
138 { return NS_ERROR_UNEXPECTED
; }
141 ExecuteNativeKeyBinding(NativeKeyBindingsType aType
,
142 const mozilla::WidgetKeyboardEvent
& aEvent
,
143 DoCommandCallback aCallback
,
144 void* aCallbackData
) MOZ_OVERRIDE
;
146 friend struct AutoCacheNativeKeyCommands
;
149 // nsBaseWidget methods we override
152 // Documents loaded in child processes are always subdocuments of
153 // other docs in an ancestor process. To ensure that the
154 // backgrounds of those documents are painted like those of
155 // same-process subdocuments, we force the widget here to be
156 // transparent, which in turn will cause layout to use a transparent
157 // backstop background color.
158 virtual nsTransparencyMode
GetTransparencyMode() MOZ_OVERRIDE
159 { return eTransparencyTransparent
; }
161 virtual LayerManager
*
162 GetLayerManager(PLayerTransactionChild
* aShadowManager
= nullptr,
163 LayersBackend aBackendHint
= mozilla::layers::LayersBackend::LAYERS_NONE
,
164 LayerManagerPersistence aPersistence
= LAYER_MANAGER_CURRENT
,
165 bool* aAllowRetaining
= nullptr) MOZ_OVERRIDE
;
167 NS_IMETHOD
NotifyIME(const IMENotification
& aIMENotification
) MOZ_OVERRIDE
;
168 NS_IMETHOD_(void) SetInputContext(const InputContext
& aContext
,
169 const InputContextAction
& aAction
) MOZ_OVERRIDE
;
170 NS_IMETHOD_(InputContext
) GetInputContext() MOZ_OVERRIDE
;
171 virtual nsIMEUpdatePreference
GetIMEUpdatePreference() MOZ_OVERRIDE
;
173 NS_IMETHOD
SetCursor(nsCursor aCursor
) MOZ_OVERRIDE
;
174 NS_IMETHOD
SetCursor(imgIContainer
* aCursor
,
175 uint32_t aHotspotX
, uint32_t aHotspotY
) MOZ_OVERRIDE
177 return nsBaseWidget::SetCursor(aCursor
, aHotspotX
, aHotspotY
);
180 // Gets the DPI of the screen corresponding to this widget.
181 // Contacts the parent process which gets the DPI from the
182 // proper widget there. TODO: Handle DPI changes that happen
184 virtual float GetDPI() MOZ_OVERRIDE
;
185 virtual double GetDefaultScaleInternal() MOZ_OVERRIDE
;
187 virtual bool NeedsPaint() MOZ_OVERRIDE
;
189 virtual TabChild
* GetOwningTabChild() MOZ_OVERRIDE
{ return mTabChild
; }
190 virtual void ClearBackingScaleCache()
196 nsIntSize
GetScreenDimensions();
198 // Get the size of the chrome of the window that this tab belongs to.
199 nsIntPoint
GetChromeDimensions();
201 // Get the screen position of the application window.
202 nsIntPoint
GetWindowPosition();
204 NS_IMETHOD
StartPluginIME(const mozilla::WidgetKeyboardEvent
& aKeyboardEvent
,
205 int32_t aPanelX
, int32_t aPanelY
,
206 nsString
& aCommitted
) MOZ_OVERRIDE
;
208 NS_IMETHOD
SetPluginFocused(bool& aFocused
) MOZ_OVERRIDE
;
217 void SetChild(PuppetWidget
* aChild
);
219 nsresult
IMEEndComposition(bool aCancel
);
220 nsresult
NotifyIMEOfFocusChange(bool aFocus
);
221 nsresult
NotifyIMEOfSelectionChange(const IMENotification
& aIMENotification
);
222 nsresult
NotifyIMEOfUpdateComposition();
223 nsresult
NotifyIMEOfTextChange(const IMENotification
& aIMENotification
);
224 nsresult
NotifyIMEOfMouseButtonEvent(const IMENotification
& aIMENotification
);
225 nsresult
NotifyIMEOfEditorRect();
226 nsresult
NotifyIMEOfPositionChange();
228 bool GetEditorRect(nsIntRect
& aEditorRect
);
229 bool GetCompositionRects(uint32_t& aStartOffset
,
230 nsTArray
<nsIntRect
>& aRectArray
,
231 uint32_t& aTargetCauseOffset
);
232 bool GetCaretRect(nsIntRect
& aCaretRect
, uint32_t aCaretOffset
);
233 uint32_t GetCaretOffset();
235 class PaintTask
: public nsRunnable
{
238 explicit PaintTask(PuppetWidget
* widget
) : mWidget(widget
) {}
239 void Revoke() { mWidget
= nullptr; }
241 PuppetWidget
* mWidget
;
244 // TabChild normally holds a strong reference to this PuppetWidget
245 // or its root ancestor, but each PuppetWidget also needs a
246 // reference back to TabChild (e.g. to delegate nsIWidget IME calls
247 // to chrome) So we hold a weak reference to TabChild here. Since
248 // it's possible for TabChild to outlive the PuppetWidget, we clear
249 // this weak reference in Destroy()
251 // The "widget" to which we delegate events if we don't have an
253 nsRefPtr
<PuppetWidget
> mChild
;
254 nsIntRegion mDirtyRegion
;
255 nsRevocableEventPtr
<PaintTask
> mPaintTask
;
256 // XXX/cjones: keeping this around until we teach LayerManager to do
257 // retained-content-only transactions
258 mozilla::RefPtr
<DrawTarget
> mDrawTarget
;
260 nsIMEUpdatePreference mIMEPreferenceOfParent
;
262 // Latest seqno received through events
263 uint32_t mIMELastReceivedSeqno
;
264 // Chrome's seqno value when last blur occurred
265 // arriving events with seqno up to this should be discarded
266 // Note that if seqno overflows (~50 days at 1 ms increment rate),
267 // events will be discarded until new focus/blur occurs
268 uint32_t mIMELastBlurSeqno
;
269 bool mNeedIMEStateInit
;
271 // The DPI of the screen corresponding to this widget
273 double mDefaultScale
;
275 // Precomputed answers for ExecuteNativeKeyBinding
276 bool mNativeKeyCommandsValid
;
277 InfallibleTArray
<mozilla::CommandInt
> mSingleLineCommands
;
278 InfallibleTArray
<mozilla::CommandInt
> mMultiLineCommands
;
279 InfallibleTArray
<mozilla::CommandInt
> mRichTextCommands
;
282 struct AutoCacheNativeKeyCommands
284 explicit AutoCacheNativeKeyCommands(PuppetWidget
* aWidget
)
287 mSavedValid
= mWidget
->mNativeKeyCommandsValid
;
288 mSavedSingleLine
= mWidget
->mSingleLineCommands
;
289 mSavedMultiLine
= mWidget
->mMultiLineCommands
;
290 mSavedRichText
= mWidget
->mRichTextCommands
;
293 void Cache(const InfallibleTArray
<mozilla::CommandInt
>& aSingleLineCommands
,
294 const InfallibleTArray
<mozilla::CommandInt
>& aMultiLineCommands
,
295 const InfallibleTArray
<mozilla::CommandInt
>& aRichTextCommands
)
297 mWidget
->mNativeKeyCommandsValid
= true;
298 mWidget
->mSingleLineCommands
= aSingleLineCommands
;
299 mWidget
->mMultiLineCommands
= aMultiLineCommands
;
300 mWidget
->mRichTextCommands
= aRichTextCommands
;
303 void CacheNoCommands()
305 mWidget
->mNativeKeyCommandsValid
= true;
306 mWidget
->mSingleLineCommands
.Clear();
307 mWidget
->mMultiLineCommands
.Clear();
308 mWidget
->mRichTextCommands
.Clear();
311 ~AutoCacheNativeKeyCommands()
313 mWidget
->mNativeKeyCommandsValid
= mSavedValid
;
314 mWidget
->mSingleLineCommands
= mSavedSingleLine
;
315 mWidget
->mMultiLineCommands
= mSavedMultiLine
;
316 mWidget
->mRichTextCommands
= mSavedRichText
;
320 PuppetWidget
* mWidget
;
322 InfallibleTArray
<mozilla::CommandInt
> mSavedSingleLine
;
323 InfallibleTArray
<mozilla::CommandInt
> mSavedMultiLine
;
324 InfallibleTArray
<mozilla::CommandInt
> mSavedRichText
;
327 class PuppetScreen
: public nsBaseScreen
330 explicit PuppetScreen(void* nativeScreen
);
333 NS_IMETHOD
GetId(uint32_t* aId
) MOZ_OVERRIDE
;
334 NS_IMETHOD
GetRect(int32_t* aLeft
, int32_t* aTop
, int32_t* aWidth
, int32_t* aHeight
) MOZ_OVERRIDE
;
335 NS_IMETHOD
GetAvailRect(int32_t* aLeft
, int32_t* aTop
, int32_t* aWidth
, int32_t* aHeight
) MOZ_OVERRIDE
;
336 NS_IMETHOD
GetPixelDepth(int32_t* aPixelDepth
) MOZ_OVERRIDE
;
337 NS_IMETHOD
GetColorDepth(int32_t* aColorDepth
) MOZ_OVERRIDE
;
338 NS_IMETHOD
GetRotation(uint32_t* aRotation
) MOZ_OVERRIDE
;
339 NS_IMETHOD
SetRotation(uint32_t aRotation
) MOZ_OVERRIDE
;
342 class PuppetScreenManager MOZ_FINAL
: public nsIScreenManager
344 ~PuppetScreenManager();
347 PuppetScreenManager();
350 NS_DECL_NSISCREENMANAGER
353 nsCOMPtr
<nsIScreen
> mOneScreen
;
356 } // namespace widget
357 } // namespace mozilla
359 #endif // mozilla_widget_PuppetWidget_h__