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 MOZ_FINAL
: 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);
67 virtual already_AddRefed
<nsIWidget
>
68 CreateChild(const nsIntRect
&aRect
,
69 nsDeviceContext
*aContext
,
70 nsWidgetInitData
*aInitData
= nullptr,
71 bool aForceUseIWidgetParent
= false);
75 NS_IMETHOD
Show(bool aState
);
77 virtual bool IsVisible() const
80 NS_IMETHOD
ConstrainPosition(bool /*ignored aAllowSlop*/,
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
)
89 NS_IMETHOD
Resize(double aWidth
,
92 NS_IMETHOD
Resize(double aX
,
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
)
103 { mEnabled
= aState
; return NS_OK
; }
104 virtual bool IsEnabled() const
107 NS_IMETHOD
SetFocus(bool aRaise
= false);
109 // PuppetWidgets don't care about children.
110 virtual nsresult
ConfigureChildren(const nsTArray
<Configuration
>& aConfigurations
)
113 NS_IMETHOD
Invalidate(const nsIntRect
& aRect
);
115 // This API is going away, steer clear.
116 virtual void Scroll(const nsIntPoint
& aDelta
,
117 const nsTArray
<nsIntRect
>& aDestRects
,
118 const nsTArray
<Configuration
>& aReconfigureChildren
)
119 { /* dead man walking */ }
121 // PuppetWidgets don't have native data, as they're purely nonnative.
122 virtual void* GetNativeData(uint32_t aDataType
);
123 NS_IMETHOD
ReparentNativeWidget(nsIWidget
* aNewParent
)
124 { return NS_ERROR_UNEXPECTED
; }
126 // PuppetWidgets don't have any concept of titles.
127 NS_IMETHOD
SetTitle(const nsAString
& aTitle
)
128 { return NS_ERROR_UNEXPECTED
; }
130 // PuppetWidgets are always at <0, 0>.
131 virtual nsIntPoint
WidgetToScreenOffset()
132 { return nsIntPoint(0, 0); }
134 void InitEvent(WidgetGUIEvent
& aEvent
, nsIntPoint
* aPoint
= nullptr);
136 NS_IMETHOD
DispatchEvent(WidgetGUIEvent
* aEvent
, nsEventStatus
& aStatus
);
138 NS_IMETHOD
CaptureRollupEvents(nsIRollupListener
* aListener
,
140 { return NS_ERROR_UNEXPECTED
; }
143 ExecuteNativeKeyBinding(NativeKeyBindingsType aType
,
144 const mozilla::WidgetKeyboardEvent
& aEvent
,
145 DoCommandCallback aCallback
,
146 void* aCallbackData
) MOZ_OVERRIDE
;
148 friend struct AutoCacheNativeKeyCommands
;
151 // nsBaseWidget methods we override
154 // Documents loaded in child processes are always subdocuments of
155 // other docs in an ancestor process. To ensure that the
156 // backgrounds of those documents are painted like those of
157 // same-process subdocuments, we force the widget here to be
158 // transparent, which in turn will cause layout to use a transparent
159 // backstop background color.
160 virtual nsTransparencyMode
GetTransparencyMode() MOZ_OVERRIDE
161 { return eTransparencyTransparent
; }
163 virtual LayerManager
*
164 GetLayerManager(PLayerTransactionChild
* aShadowManager
= nullptr,
165 LayersBackend aBackendHint
= mozilla::layers::LayersBackend::LAYERS_NONE
,
166 LayerManagerPersistence aPersistence
= LAYER_MANAGER_CURRENT
,
167 bool* aAllowRetaining
= nullptr);
169 NS_IMETHOD
NotifyIME(const IMENotification
& aIMENotification
) MOZ_OVERRIDE
;
170 NS_IMETHOD_(void) SetInputContext(const InputContext
& aContext
,
171 const InputContextAction
& aAction
);
172 NS_IMETHOD_(InputContext
) GetInputContext();
173 virtual nsIMEUpdatePreference
GetIMEUpdatePreference() MOZ_OVERRIDE
;
175 NS_IMETHOD
SetCursor(nsCursor aCursor
);
176 NS_IMETHOD
SetCursor(imgIContainer
* aCursor
,
177 uint32_t aHotspotX
, uint32_t aHotspotY
)
179 return nsBaseWidget::SetCursor(aCursor
, aHotspotX
, aHotspotY
);
182 // Gets the DPI of the screen corresponding to this widget.
183 // Contacts the parent process which gets the DPI from the
184 // proper widget there. TODO: Handle DPI changes that happen
186 virtual float GetDPI();
187 virtual double GetDefaultScaleInternal();
189 virtual bool NeedsPaint() MOZ_OVERRIDE
;
191 virtual TabChild
* GetOwningTabChild() MOZ_OVERRIDE
{ return mTabChild
; }
192 virtual void ClearBackingScaleCache()
201 void SetChild(PuppetWidget
* aChild
);
203 nsresult
IMEEndComposition(bool aCancel
);
204 nsresult
NotifyIMEOfFocusChange(bool aFocus
);
205 nsresult
NotifyIMEOfSelectionChange(const IMENotification
& aIMENotification
);
206 nsresult
NotifyIMEOfUpdateComposition();
207 nsresult
NotifyIMEOfTextChange(const IMENotification
& aIMENotification
);
209 class PaintTask
: public nsRunnable
{
212 explicit PaintTask(PuppetWidget
* widget
) : mWidget(widget
) {}
213 void Revoke() { mWidget
= nullptr; }
215 PuppetWidget
* mWidget
;
218 // TabChild normally holds a strong reference to this PuppetWidget
219 // or its root ancestor, but each PuppetWidget also needs a
220 // reference back to TabChild (e.g. to delegate nsIWidget IME calls
221 // to chrome) So we hold a weak reference to TabChild here. Since
222 // it's possible for TabChild to outlive the PuppetWidget, we clear
223 // this weak reference in Destroy()
225 // The "widget" to which we delegate events if we don't have an
227 nsRefPtr
<PuppetWidget
> mChild
;
228 nsIntRegion mDirtyRegion
;
229 nsRevocableEventPtr
<PaintTask
> mPaintTask
;
232 // XXX/cjones: keeping this around until we teach LayerManager to do
233 // retained-content-only transactions
234 mozilla::RefPtr
<DrawTarget
> mDrawTarget
;
236 nsIMEUpdatePreference mIMEPreferenceOfParent
;
238 // Latest seqno received through events
239 uint32_t mIMELastReceivedSeqno
;
240 // Chrome's seqno value when last blur occurred
241 // arriving events with seqno up to this should be discarded
242 // Note that if seqno overflows (~50 days at 1 ms increment rate),
243 // events will be discarded until new focus/blur occurs
244 uint32_t mIMELastBlurSeqno
;
245 bool mNeedIMEStateInit
;
247 // The DPI of the screen corresponding to this widget
249 double mDefaultScale
;
251 // Precomputed answers for ExecuteNativeKeyBinding
252 bool mNativeKeyCommandsValid
;
253 InfallibleTArray
<mozilla::CommandInt
> mSingleLineCommands
;
254 InfallibleTArray
<mozilla::CommandInt
> mMultiLineCommands
;
255 InfallibleTArray
<mozilla::CommandInt
> mRichTextCommands
;
258 struct AutoCacheNativeKeyCommands
260 explicit AutoCacheNativeKeyCommands(PuppetWidget
* aWidget
)
263 mSavedValid
= mWidget
->mNativeKeyCommandsValid
;
264 mSavedSingleLine
= mWidget
->mSingleLineCommands
;
265 mSavedMultiLine
= mWidget
->mMultiLineCommands
;
266 mSavedRichText
= mWidget
->mRichTextCommands
;
269 void Cache(const InfallibleTArray
<mozilla::CommandInt
>& aSingleLineCommands
,
270 const InfallibleTArray
<mozilla::CommandInt
>& aMultiLineCommands
,
271 const InfallibleTArray
<mozilla::CommandInt
>& aRichTextCommands
)
273 mWidget
->mNativeKeyCommandsValid
= true;
274 mWidget
->mSingleLineCommands
= aSingleLineCommands
;
275 mWidget
->mMultiLineCommands
= aMultiLineCommands
;
276 mWidget
->mRichTextCommands
= aRichTextCommands
;
279 void CacheNoCommands()
281 mWidget
->mNativeKeyCommandsValid
= true;
282 mWidget
->mSingleLineCommands
.Clear();
283 mWidget
->mMultiLineCommands
.Clear();
284 mWidget
->mRichTextCommands
.Clear();
287 ~AutoCacheNativeKeyCommands()
289 mWidget
->mNativeKeyCommandsValid
= mSavedValid
;
290 mWidget
->mSingleLineCommands
= mSavedSingleLine
;
291 mWidget
->mMultiLineCommands
= mSavedMultiLine
;
292 mWidget
->mRichTextCommands
= mSavedRichText
;
296 PuppetWidget
* mWidget
;
298 InfallibleTArray
<mozilla::CommandInt
> mSavedSingleLine
;
299 InfallibleTArray
<mozilla::CommandInt
> mSavedMultiLine
;
300 InfallibleTArray
<mozilla::CommandInt
> mSavedRichText
;
303 class PuppetScreen
: public nsBaseScreen
306 explicit PuppetScreen(void* nativeScreen
);
309 NS_IMETHOD
GetId(uint32_t* aId
) MOZ_OVERRIDE
;
310 NS_IMETHOD
GetRect(int32_t* aLeft
, int32_t* aTop
, int32_t* aWidth
, int32_t* aHeight
) MOZ_OVERRIDE
;
311 NS_IMETHOD
GetAvailRect(int32_t* aLeft
, int32_t* aTop
, int32_t* aWidth
, int32_t* aHeight
) MOZ_OVERRIDE
;
312 NS_IMETHOD
GetPixelDepth(int32_t* aPixelDepth
) MOZ_OVERRIDE
;
313 NS_IMETHOD
GetColorDepth(int32_t* aColorDepth
) MOZ_OVERRIDE
;
314 NS_IMETHOD
GetRotation(uint32_t* aRotation
) MOZ_OVERRIDE
;
315 NS_IMETHOD
SetRotation(uint32_t aRotation
) MOZ_OVERRIDE
;
318 class PuppetScreenManager MOZ_FINAL
: public nsIScreenManager
320 ~PuppetScreenManager();
323 PuppetScreenManager();
326 NS_DECL_NSISCREENMANAGER
329 nsCOMPtr
<nsIScreen
> mOneScreen
;
332 } // namespace widget
333 } // namespace mozilla
335 #endif // mozilla_widget_PuppetWidget_h__