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