web_contents_view_mac.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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.
4
5#ifndef CONTENT_BROWSER_WEB_CONTENTS_WEB_CONTENTS_VIEW_MAC_H_
6#define CONTENT_BROWSER_WEB_CONTENTS_WEB_CONTENTS_VIEW_MAC_H_
7
8#import <Cocoa/Cocoa.h>
9
10#include <string>
11#include <vector>
12
13#include "base/memory/scoped_nsobject.h"
14#include "base/memory/scoped_ptr.h"
15#include "content/common/drag_event_source_info.h"
16#include "content/port/browser/render_view_host_delegate_view.h"
17#include "content/public/browser/web_contents_view.h"
18#include "ui/base/cocoa/base_view.h"
19#include "ui/gfx/size.h"
20
21@class FocusTracker;
22class SkBitmap;
23@class WebDragDest;
24@class WebDragSource;
25
26namespace content {
27class WebContentsImpl;
28class WebContentsViewDelegate;
29class WebContentsViewMac;
30}
31
32namespace gfx {
33class Vector2d;
34}
35
36@interface WebContentsViewCocoa : BaseView {
37 @private
38  content::WebContentsViewMac* webContentsView_;  // WEAK; owns us
39  scoped_nsobject<WebDragSource> dragSource_;
40  scoped_nsobject<WebDragDest> dragDest_;
41  BOOL mouseDownCanMoveWindow_;
42}
43
44- (void)setMouseDownCanMoveWindow:(BOOL)canMove;
45
46// Expose this, since sometimes one needs both the NSView and the
47// WebContentsImpl.
48- (content::WebContentsImpl*)webContents;
49@end
50
51namespace content {
52
53// Mac-specific implementation of the WebContentsView. It owns an NSView that
54// contains all of the contents of the tab and associated child views.
55class WebContentsViewMac : public WebContentsView,
56                           public RenderViewHostDelegateView {
57 public:
58  // The corresponding WebContentsImpl is passed in the constructor, and manages
59  // our lifetime. This doesn't need to be the case, but is this way currently
60  // because that's what was easiest when they were split.
61  WebContentsViewMac(WebContentsImpl* web_contents,
62                     WebContentsViewDelegate* delegate);
63  virtual ~WebContentsViewMac();
64
65  // WebContentsView implementation --------------------------------------------
66
67  virtual void CreateView(const gfx::Size& initial_size) OVERRIDE;
68  virtual RenderWidgetHostView* CreateViewForWidget(
69      RenderWidgetHost* render_widget_host) OVERRIDE;
70  virtual gfx::NativeView GetNativeView() const OVERRIDE;
71  virtual gfx::NativeView GetContentNativeView() const OVERRIDE;
72  virtual gfx::NativeWindow GetTopLevelNativeWindow() const OVERRIDE;
73  virtual void GetContainerBounds(gfx::Rect* out) const OVERRIDE;
74  virtual void RenderViewCreated(RenderViewHost* host) OVERRIDE;
75  virtual void SetPageTitle(const string16& title) OVERRIDE;
76  virtual void OnTabCrashed(base::TerminationStatus status,
77                            int error_code) OVERRIDE;
78  virtual void SizeContents(const gfx::Size& size) OVERRIDE;
79  virtual void Focus() OVERRIDE;
80  virtual void SetInitialFocus() OVERRIDE;
81  virtual void StoreFocus() OVERRIDE;
82  virtual void RestoreFocus() OVERRIDE;
83  virtual WebDropData* GetDropData() const OVERRIDE;
84  virtual bool IsEventTracking() const OVERRIDE;
85  virtual void CloseTabAfterEventTracking() OVERRIDE;
86  virtual gfx::Rect GetViewBounds() const OVERRIDE;
87
88  // Backend implementation of RenderViewHostDelegateView.
89  virtual void ShowContextMenu(const ContextMenuParams& params,
90                               ContextMenuSourceType type) OVERRIDE;
91  virtual void ShowPopupMenu(const gfx::Rect& bounds,
92                             int item_height,
93                             double item_font_size,
94                             int selected_item,
95                             const std::vector<WebMenuItem>& items,
96                             bool right_aligned,
97                             bool allow_multiple_selection) OVERRIDE;
98  virtual void StartDragging(const WebDropData& drop_data,
99                             WebKit::WebDragOperationsMask allowed_operations,
100                             const gfx::ImageSkia& image,
101                             const gfx::Vector2d& image_offset,
102                             const DragEventSourceInfo& event_info) OVERRIDE;
103  virtual void UpdateDragCursor(WebKit::WebDragOperation operation) OVERRIDE;
104  virtual void GotFocus() OVERRIDE;
105  virtual void TakeFocus(bool reverse) OVERRIDE;
106
107  // A helper method for closing the tab in the
108  // CloseTabAfterEventTracking() implementation.
109  void CloseTab();
110
111  WebContentsImpl* web_contents() { return web_contents_; }
112  WebContentsViewDelegate* delegate() { return delegate_.get(); }
113
114 private:
115  // The WebContentsImpl whose contents we display.
116  WebContentsImpl* web_contents_;
117
118  // The Cocoa NSView that lives in the view hierarchy.
119  scoped_nsobject<WebContentsViewCocoa> cocoa_view_;
120
121  // Keeps track of which NSView has focus so we can restore the focus when
122  // focus returns.
123  scoped_nsobject<FocusTracker> focus_tracker_;
124
125  // Our optional delegate.
126  scoped_ptr<WebContentsViewDelegate> delegate_;
127
128  DISALLOW_COPY_AND_ASSIGN(WebContentsViewMac);
129};
130
131}  // namespace content
132
133#endif  // CONTENT_BROWSER_WEB_CONTENTS_WEB_CONTENTS_VIEW_MAC_H_
134