1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_MAC_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_MAC_H_
8 #import <Cocoa/Cocoa.h>
9 #include <IOSurface/IOSurfaceAPI.h>
17 #include "base/mac/scoped_nsobject.h"
18 #include "base/memory/scoped_ptr.h"
19 #include "base/memory/weak_ptr.h"
20 #include "base/time/time.h"
21 #include "content/browser/compositor/browser_compositor_view_mac.h"
22 #include "content/browser/compositor/delegated_frame_host.h"
23 #include "content/browser/renderer_host/display_link_mac.h"
24 #include "content/browser/renderer_host/input/mouse_wheel_rails_filter_mac.h"
25 #include "content/browser/renderer_host/render_widget_host_view_base.h"
26 #include "content/common/content_export.h"
27 #include "content/common/cursors/webcursor.h"
28 #include "content/common/edit_command.h"
29 #import "content/public/browser/render_widget_host_view_mac_base.h"
30 #include "ipc/ipc_sender.h"
31 #include "third_party/WebKit/public/web/WebCompositionUnderline.h"
32 #include "ui/accelerated_widget_mac/accelerated_widget_mac.h"
33 #include "ui/accelerated_widget_mac/io_surface_layer.h"
34 #include "ui/base/cocoa/base_view.h"
35 #include "ui/base/cocoa/remote_layer_api.h"
36 #include "ui/gfx/display_observer.h"
39 class RenderWidgetHostImpl
;
40 class RenderWidgetHostViewMac
;
41 class RenderWidgetHostViewMacEditCommandHelper
;
50 @
class FullscreenWindowManager
;
51 @protocol RenderWidgetHostViewMacDelegate
;
54 @protocol RenderWidgetHostViewMacOwner
55 - (content::RenderWidgetHostViewMac
*)renderWidgetHostViewMac
;
58 // This is the view that lives in the Cocoa view hierarchy. In Windows-land,
59 // RenderWidgetHostViewWin is both the view and the delegate. We split the roles
60 // but that means that the view needs to own the delegate and will dispose of it
61 // when it's removed from the view system.
62 @interface RenderWidgetHostViewCocoa
63 : BaseView
<RenderWidgetHostViewMacBase
,
64 RenderWidgetHostViewMacOwner
,
67 scoped_ptr
<content::RenderWidgetHostViewMac
> renderWidgetHostView_
;
68 // This ivar is the cocoa delegate of the NSResponder.
69 base::scoped_nsobject
<NSObject
<RenderWidgetHostViewMacDelegate
>>
72 BOOL closeOnDeactivate_
;
74 scoped_ptr
<content::RenderWidgetHostViewMacEditCommandHelper
>
77 // These are part of the magic tooltip code from WebKit's WebHTMLView:
78 id trackingRectOwner_
; // (not retained)
79 void* trackingRectUserData_
;
80 NSTrackingRectTag lastToolTipTag_
;
81 base::scoped_nsobject
<NSString
> toolTip_
;
83 // Is YES if there was a mouse-down as yet unbalanced with a mouse-up.
84 BOOL hasOpenMouseDown_
;
86 NSWindow
* lastWindow_
; // weak
88 // The cursor for the page. This is passed up from the renderer.
89 base::scoped_nsobject
<NSCursor
> currentCursor_
;
91 // Variables used by our implementaion of the NSTextInput protocol.
92 // An input method of Mac calls the methods of this protocol not only to
93 // notify an application of its status, but also to retrieve the status of
94 // the application. That is, an application cannot control an input method
96 // This object keeps the status of a composition of the renderer and returns
97 // it when an input method asks for it.
98 // We need to implement Objective-C methods for the NSTextInput protocol. On
99 // the other hand, we need to implement a C++ method for an IPC-message
100 // handler which receives input-method events from the renderer.
102 // Represents the input-method attributes supported by this object.
103 base::scoped_nsobject
<NSArray
> validAttributesForMarkedText_
;
105 // Indicates if we are currently handling a key down event.
106 BOOL handlingKeyDown_
;
108 // Indicates if there is any marked text.
111 // Indicates if unmarkText is called or not when handling a keyboard
113 BOOL unmarkTextCalled_
;
115 // The range of current marked text inside the whole content of the DOM node
117 // TODO(suzhe): This is currently a fake value, as we do not support accessing
118 // the whole content yet.
119 NSRange markedRange_
;
121 // The selected range, cached from a message sent by the renderer.
122 NSRange selectedRange_
;
124 // Text to be inserted which was generated by handling a key down event.
125 base::string16 textToBeInserted_
;
127 // Marked text which was generated by handling a key down event.
128 base::string16 markedText_
;
130 // Underline information of the |markedText_|.
131 std::vector
<blink::WebCompositionUnderline
> underlines_
;
133 // Indicates if doCommandBySelector method receives any edit command when
134 // handling a key down event.
135 BOOL hasEditCommands_
;
137 // Contains edit commands received by the -doCommandBySelector: method when
138 // handling a key down event, not including inserting commands, eg. insertTab,
140 content::EditCommands editCommands_
;
142 // The plugin that currently has focus (-1 if no plugin has focus).
143 int focusedPluginIdentifier_
;
145 // Whether or not plugin IME is currently enabled active.
146 BOOL pluginImeActive_
;
148 // Whether the previous mouse event was ignored due to hitTest check.
149 BOOL mouseEventWasIgnored_
;
151 // Event monitor for scroll wheel end event.
154 // When a gesture starts, the system does not inform the view of which type
155 // of gesture is happening (magnify, rotate, etc), rather, it just informs
156 // the view that some as-yet-undefined gesture is starting. Capture the
157 // information about the gesture's beginning event here. It will be used to
158 // create a specific gesture begin event later.
159 scoped_ptr
<blink::WebGestureEvent
> gestureBeginEvent_
;
161 // This is set if a GesturePinchBegin event has been sent in the lifetime of
162 // |gestureBeginEvent_|. If set, a GesturePinchEnd will be sent when the
164 BOOL gestureBeginPinchSent_
;
166 // If true then escape key down events are suppressed until the first escape
167 // key up event. (The up event is suppressed as well). This is used by the
168 // flash fullscreen code to avoid sending a key up event without a matching
170 BOOL suppressNextEscapeKeyUp_
;
172 // The set of key codes from key down events that we haven't seen the matching
173 // key up events yet.
174 // Used for filtering out non-matching NSKeyUp events.
175 std::set
<unsigned short> keyDownCodes_
;
177 // The filter used to guide touch events towards a horizontal or vertical
179 content::MouseWheelRailsFilterMac mouseWheelFilter_
;
182 @
property(nonatomic
, readonly
) NSRange selectedRange
;
183 @
property(nonatomic
, readonly
) BOOL suppressNextEscapeKeyUp
;
185 - (void)setCanBeKeyView
:(BOOL
)can
;
186 - (void)setCloseOnDeactivate
:(BOOL
)b
;
187 - (void)setOpaque
:(BOOL
)opaque
;
188 - (void)setToolTipAtMousePoint
:(NSString
*)string
;
189 // True for always-on-top special windows (e.g. Balloons and Panels).
190 - (BOOL
)acceptsMouseEventsWhenInactive
;
191 // Cancel ongoing composition (abandon the marked text).
192 - (void)cancelComposition
;
193 // Confirm ongoing composition.
194 - (void)confirmComposition
;
195 // Enables or disables plugin IME.
196 - (void)setPluginImeActive
:(BOOL
)active
;
197 // Updates the current plugin focus state.
198 - (void)pluginFocusChanged
:(BOOL
)focused forPlugin
:(int)pluginId
;
199 // Evaluates the event in the context of plugin IME, if plugin IME is enabled.
200 // Returns YES if the event was handled.
201 - (BOOL
)postProcessEventForPluginIme
:(NSEvent
*)event
;
202 - (void)updateCursor
:(NSCursor
*)cursor
;
203 - (NSRect
)firstViewRectForCharacterRange
:(NSRange
)theRange
204 actualRange
:(NSRangePointer
)actualRange
;
209 ///////////////////////////////////////////////////////////////////////////////
210 // RenderWidgetHostViewMac
212 // An object representing the "View" of a rendered web page. This object is
213 // responsible for displaying the content of the web page, and integrating with
214 // the Cocoa view system. It is the implementation of the RenderWidgetHostView
215 // that the cross-platform RenderWidgetHost object uses
216 // to display the data.
218 // Comment excerpted from render_widget_host.h:
220 // "The lifetime of the RenderWidgetHost* is tied to the render process.
221 // If the render process dies, the RenderWidgetHost* goes away and all
222 // references to it must become NULL."
224 // RenderWidgetHostView class hierarchy described in render_widget_host_view.h.
225 class CONTENT_EXPORT RenderWidgetHostViewMac
226 : public RenderWidgetHostViewBase
,
227 public DelegatedFrameHostClient
,
228 public ui::AcceleratedWidgetMacNSView
,
230 public gfx::DisplayObserver
{
232 // The view will associate itself with the given widget. The native view must
233 // be hooked up immediately to the view hierarchy, or else when it is
234 // deleted it will delete this out from under the caller.
236 // When |is_guest_view_hack| is true, this view isn't really the view for
237 // the |widget|, a RenderWidgetHostViewGuest is.
238 // TODO(lazyboy): Remove |is_guest_view_hack| once BrowserPlugin has migrated
239 // to use RWHVChildFrame (http://crbug.com/330264).
240 RenderWidgetHostViewMac(RenderWidgetHost
* widget
, bool is_guest_view_hack
);
241 ~RenderWidgetHostViewMac() override
;
243 RenderWidgetHostViewCocoa
* cocoa_view() const { return cocoa_view_
; }
245 // |delegate| is used to separate out the logic from the NSResponder delegate.
246 // |delegate| is retained by this class.
247 // |delegate| should be set at most once.
248 CONTENT_EXPORT
void SetDelegate(
249 NSObject
<RenderWidgetHostViewMacDelegate
>* delegate
);
250 void SetAllowPauseForResizeOrRepaint(bool allow
);
252 // RenderWidgetHostView implementation.
253 bool OnMessageReceived(const IPC::Message
& msg
) override
;
254 void InitAsChild(gfx::NativeView parent_view
) override
;
255 RenderWidgetHost
* GetRenderWidgetHost() const override
;
256 void SetSize(const gfx::Size
& size
) override
;
257 void SetBounds(const gfx::Rect
& rect
) override
;
258 gfx::Vector2dF
GetLastScrollOffset() const override
;
259 gfx::NativeView
GetNativeView() const override
;
260 gfx::NativeViewId
GetNativeViewId() const override
;
261 gfx::NativeViewAccessible
GetNativeViewAccessible() override
;
262 bool HasFocus() const override
;
263 bool IsSurfaceAvailableForCopy() const override
;
264 void Show() override
;
265 void Hide() override
;
266 bool IsShowing() override
;
267 void WasUnOccluded() override
;
268 void WasOccluded() override
;
269 gfx::Rect
GetViewBounds() const override
;
270 void SetShowingContextMenu(bool showing
) override
;
271 void SetActive(bool active
) override
;
272 void SetWindowVisibility(bool visible
) override
;
273 void WindowFrameChanged() override
;
274 void ShowDefinitionForSelection() override
;
275 bool SupportsSpeech() const override
;
276 void SpeakSelection() override
;
277 bool IsSpeaking() const override
;
278 void StopSpeaking() override
;
279 void SetBackgroundColor(SkColor color
) override
;
281 // Implementation of RenderWidgetHostViewBase.
282 void InitAsPopup(RenderWidgetHostView
* parent_host_view
,
283 const gfx::Rect
& pos
) override
;
284 void InitAsFullscreen(RenderWidgetHostView
* reference_host_view
) override
;
285 void MovePluginWindows(const std::vector
<WebPluginGeometry
>& moves
) override
;
286 void Focus() override
;
287 void Blur() override
;
288 void UpdateCursor(const WebCursor
& cursor
) override
;
289 void SetIsLoading(bool is_loading
) override
;
290 void TextInputTypeChanged(ui::TextInputType type
,
291 ui::TextInputMode input_mode
,
292 bool can_compose_inline
,
294 void ImeCancelComposition() override
;
295 void ImeCompositionRangeChanged(
296 const gfx::Range
& range
,
297 const std::vector
<gfx::Rect
>& character_bounds
) override
;
298 void RenderProcessGone(base::TerminationStatus status
,
299 int error_code
) override
;
300 void RenderWidgetHostGone() override
;
301 void Destroy() override
;
302 void SetTooltipText(const base::string16
& tooltip_text
) override
;
303 void SelectionChanged(const base::string16
& text
,
305 const gfx::Range
& range
) override
;
306 void SelectionBoundsChanged(
307 const ViewHostMsg_SelectionBounds_Params
& params
) override
;
308 void CopyFromCompositingSurface(const gfx::Rect
& src_subrect
,
309 const gfx::Size
& dst_size
,
310 ReadbackRequestCallback
& callback
,
311 SkColorType color_type
) override
;
312 void CopyFromCompositingSurfaceToVideoFrame(
313 const gfx::Rect
& src_subrect
,
314 const scoped_refptr
<media::VideoFrame
>& target
,
315 const base::Callback
<void(bool)>& callback
) override
;
316 bool CanCopyToVideoFrame() const override
;
317 bool CanSubscribeFrame() const override
;
318 void BeginFrameSubscription(
319 scoped_ptr
<RenderWidgetHostViewFrameSubscriber
> subscriber
) override
;
320 void EndFrameSubscription() override
;
321 void OnSwapCompositorFrame(uint32 output_surface_id
,
322 scoped_ptr
<cc::CompositorFrame
> frame
) override
;
323 BrowserAccessibilityManager
* CreateBrowserAccessibilityManager(
324 BrowserAccessibilityDelegate
* delegate
) override
;
325 gfx::Point
AccessibilityOriginInScreen(const gfx::Rect
& bounds
) override
;
326 void AccessibilityShowMenu(const gfx::Point
& point
) override
;
327 bool PostProcessEventForPluginIme(
328 const NativeWebKeyboardEvent
& event
) override
;
330 bool HasAcceleratedSurface(const gfx::Size
& desired_size
) override
;
331 void GetScreenInfo(blink::WebScreenInfo
* results
) override
;
332 gfx::Rect
GetBoundsInRootWindow() override
;
333 gfx::GLSurfaceHandle
GetCompositingSurface() override
;
335 bool LockMouse() override
;
336 void UnlockMouse() override
;
337 void WheelEventAck(const blink::WebMouseWheelEvent
& event
,
338 InputEventAckState ack_result
) override
;
340 // IPC::Sender implementation.
341 bool Send(IPC::Message
* message
) override
;
343 SkColorType
PreferredReadbackFormat() override
;
345 // gfx::DisplayObserver implementation.
346 void OnDisplayAdded(const gfx::Display
& new_display
) override
;
347 void OnDisplayRemoved(const gfx::Display
& old_display
) override
;
348 void OnDisplayMetricsChanged(const gfx::Display
& display
,
349 uint32_t metrics
) override
;
351 // Forwards the mouse event to the renderer.
352 void ForwardMouseEvent(const blink::WebMouseEvent
& event
);
356 void SetTextInputActive(bool active
);
358 // Sends completed plugin IME notification and text back to the renderer.
359 void PluginImeCompositionCompleted(const base::string16
& text
, int plugin_id
);
361 const std::string
& selected_text() const { return selected_text_
; }
363 // Returns true and stores first rectangle for character range if the
364 // requested |range| is already cached, otherwise returns false.
365 // Exposed for testing.
366 CONTENT_EXPORT
bool GetCachedFirstRectForCharacterRange(
367 NSRange range
, NSRect
* rect
, NSRange
* actual_range
);
369 // Returns true if there is line break in |range| and stores line breaking
370 // point to |line_breaking_point|. The |line_break_point| is valid only if
371 // this function returns true.
372 bool GetLineBreakIndex(const std::vector
<gfx::Rect
>& bounds
,
373 const gfx::Range
& range
,
374 size_t* line_break_point
);
376 // Returns composition character boundary rectangle. The |range| is
377 // composition based range. Also stores |actual_range| which is corresponding
378 // to actually used range for returned rectangle.
379 gfx::Rect
GetFirstRectForCompositionRange(const gfx::Range
& range
,
380 gfx::Range
* actual_range
);
382 // Converts from given whole character range to composition oriented range. If
383 // the conversion failed, return gfx::Range::InvalidRange.
384 gfx::Range
ConvertCharacterRangeToCompositionRange(
385 const gfx::Range
& request_range
);
387 WebContents
* GetWebContents();
389 // These member variables should be private, but the associated ObjC class
390 // needs access to them and can't be made a friend.
392 // The associated Model. Can be NULL if Destroy() is called when
393 // someone (other than superview) has retained |cocoa_view_|.
394 RenderWidgetHostImpl
* render_widget_host_
;
396 // Current text input type.
397 ui::TextInputType text_input_type_
;
398 bool can_compose_inline_
;
400 // The background CoreAnimation layer which is hosted by |cocoa_view_|.
401 base::scoped_nsobject
<CALayer
> background_layer_
;
403 // The state of |delegated_frame_host_| and |browser_compositor_| to
404 // manage being visible, hidden, or occluded.
405 enum BrowserCompositorViewState
{
407 // - |browser_compositor_| exists and |delegated_frame_host_| is
410 // - |render_widet_host_| is in the visible state (this includes when
411 // the tab isn't visible, but tab capture is enabled).
412 BrowserCompositorActive
,
414 // - |browser_compositor_| exists, but |delegated_frame_host_| has
417 // - The |render_widget_host_| is hidden, but |cocoa_view_| is still in the
418 // NSWindow hierarchy.
419 // - This happens when |cocoa_view_| is hidden (minimized, on another
420 // occluded by other windows, etc). The |browser_compositor_| and
421 // its CALayers are kept around so that we will have content to show when
422 // we are un-occluded.
423 BrowserCompositorSuspended
,
425 // - |browser_compositor_| has been destroyed and
426 // |delegated_frame_host_| has been hidden.
428 // - The |render_widget_host_| is hidden or dead, and |cocoa_view_| is not
429 // attached to a NSWindow.
430 // - This happens for backgrounded tabs.
431 BrowserCompositorDestroyed
,
433 BrowserCompositorViewState browser_compositor_state_
;
435 // Delegated frame management and compositior.
436 scoped_ptr
<DelegatedFrameHost
> delegated_frame_host_
;
437 scoped_ptr
<ui::Layer
> root_layer_
;
439 // Container for ui::Compositor the CALayer tree drawn by it.
440 scoped_ptr
<BrowserCompositorMac
> browser_compositor_
;
442 // Placeholder that is allocated while browser_compositor_ is NULL,
443 // indicating that a BrowserCompositorViewMac may be allocated. This is to
444 // help in recycling the internals of BrowserCompositorViewMac.
445 scoped_ptr
<BrowserCompositorMacPlaceholder
>
446 browser_compositor_placeholder_
;
448 NSWindow
* pepper_fullscreen_window() const {
449 return pepper_fullscreen_window_
;
452 CONTENT_EXPORT
void release_pepper_fullscreen_window_for_testing();
454 RenderWidgetHostViewMac
* fullscreen_parent_host_view() const {
455 return fullscreen_parent_host_view_
;
458 int window_number() const;
460 // The scale factor for the screen that the view is currently on.
461 float ViewScaleFactor() const;
463 // Update the scale factor for the backing store and for any CALayers.
464 void UpdateBackingStoreScaleFactor();
466 // Ensure that the display link is associated with the correct display.
467 void UpdateDisplayLink();
469 void PauseForPendingResizeOrRepaintsAndDraw();
471 // DelegatedFrameHostClient implementation.
472 ui::Layer
* DelegatedFrameHostGetLayer() const override
;
473 bool DelegatedFrameHostIsVisible() const override
;
474 gfx::Size
DelegatedFrameHostDesiredSizeInDIP() const override
;
475 bool DelegatedFrameCanCreateResizeLock() const override
;
476 scoped_ptr
<ResizeLock
> DelegatedFrameHostCreateResizeLock(
477 bool defer_compositor_lock
) override
;
478 void DelegatedFrameHostResizeLockWasReleased() override
;
479 void DelegatedFrameHostSendCompositorSwapAck(
480 int output_surface_id
,
481 const cc::CompositorFrameAck
& ack
) override
;
482 void DelegatedFrameHostSendReclaimCompositorResources(
483 int output_surface_id
,
484 const cc::CompositorFrameAck
& ack
) override
;
485 void DelegatedFrameHostOnLostCompositorResources() override
;
486 void DelegatedFrameHostUpdateVSyncParameters(
487 const base::TimeTicks
& timebase
,
488 const base::TimeDelta
& interval
) override
;
490 // AcceleratedWidgetMacNSView implementation.
491 NSView
* AcceleratedWidgetGetNSView() const override
;
492 bool AcceleratedWidgetShouldIgnoreBackpressure() const override
;
493 void AcceleratedWidgetSwapCompleted(
494 const std::vector
<ui::LatencyInfo
>& latency_info
) override
;
495 void AcceleratedWidgetHitError() override
;
497 // Transition from being in the Suspended state to being in the Destroyed
498 // state, if appropriate (see BrowserCompositorViewState for details).
499 void DestroySuspendedBrowserCompositorViewIfNeeded();
502 friend class RenderWidgetHostViewMacTest
;
504 // Returns whether this render view is a popup (autocomplete window).
505 bool IsPopup() const;
507 // Shuts down the render_widget_host_. This is a separate function so we can
508 // invoke it from the message loop.
511 // Tear down all components of the browser compositor in an order that will
512 // ensure no dangling references.
513 void ShutdownBrowserCompositor();
515 // The state of the the browser compositor and delegated frame host. See
516 // BrowserCompositorViewState for details.
517 void EnsureBrowserCompositorView();
518 void SuspendBrowserCompositorView();
519 void DestroyBrowserCompositorView();
521 // IPC message handlers.
522 void OnPluginFocusChanged(bool focused
, int plugin_id
);
523 void OnStartPluginIme();
524 void OnGetRenderedTextCompleted(const std::string
& text
);
526 // Send updated vsync parameters to the renderer.
527 void SendVSyncParametersToRenderer();
529 // Dispatches a TTS session.
530 void SpeakText(const std::string
& text
);
532 // The associated view. This is weak and is inserted into the view hierarchy
533 // to own this RenderWidgetHostViewMac object. Set to nil at the start of the
535 RenderWidgetHostViewCocoa
* cocoa_view_
;
537 // Indicates if the page is loading.
540 // Whether it's allowed to pause waiting for a new frame.
541 bool allow_pause_for_resize_or_repaint_
;
543 // The last scroll offset of the view.
544 gfx::Vector2dF last_scroll_offset_
;
546 // The text to be shown in the tooltip, supplied by the renderer.
547 base::string16 tooltip_text_
;
549 // True when this view acts as a platform view hack for a
550 // RenderWidgetHostViewGuest.
551 bool is_guest_view_hack_
;
553 // selected text on the renderer.
554 std::string selected_text_
;
556 // The window used for popup widgets.
557 base::scoped_nsobject
<NSWindow
> popup_window_
;
559 // The fullscreen window used for pepper flash.
560 base::scoped_nsobject
<NSWindow
> pepper_fullscreen_window_
;
561 base::scoped_nsobject
<FullscreenWindowManager
> fullscreen_window_manager_
;
562 // Our parent host view, if this is fullscreen. NULL otherwise.
563 RenderWidgetHostViewMac
* fullscreen_parent_host_view_
;
565 // Display link for getting vsync info.
566 scoped_refptr
<DisplayLinkMac
> display_link_
;
568 // The current VSync timebase and interval. This is zero until the first call
569 // to SendVSyncParametersToRenderer(), and refreshed regularly thereafter.
570 base::TimeTicks vsync_timebase_
;
571 base::TimeDelta vsync_interval_
;
573 // The current composition character range and its bounds.
574 gfx::Range composition_range_
;
575 std::vector
<gfx::Rect
> composition_bounds_
;
577 // The current caret bounds.
578 gfx::Rect caret_rect_
;
580 // Factory used to safely scope delayed calls to ShutdownHost().
581 base::WeakPtrFactory
<RenderWidgetHostViewMac
> weak_factory_
;
583 DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewMac
);
586 } // namespace content
588 #endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_MAC_H_