1// Copyright 2013 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 CHROME_BROWSER_EXTENSIONS_EXTENSION_VIEW_HOST_H_
6#define CHROME_BROWSER_EXTENSIONS_EXTENSION_VIEW_HOST_H_
7
8#include "base/memory/scoped_ptr.h"
9#include "components/web_modal/popup_manager.h"
10#include "components/web_modal/web_contents_modal_dialog_host.h"
11#include "components/web_modal/web_contents_modal_dialog_manager_delegate.h"
12#include "extensions/browser/extension_host.h"
13
14class Browser;
15
16namespace content {
17class SiteInstance;
18class WebContents;
19}
20
21namespace extensions {
22
23class ExtensionView;
24
25// The ExtensionHost for an extension that backs a view in the browser UI. For
26// example, this could be an extension popup, infobar or dialog, but not a
27// background page.
28// TODO(gbillock): See if we can remove WebContentsModalDialogManager here.
29class ExtensionViewHost
30    : public ExtensionHost,
31      public web_modal::WebContentsModalDialogManagerDelegate,
32      public web_modal::WebContentsModalDialogHost {
33 public:
34  ExtensionViewHost(const Extension* extension,
35                    content::SiteInstance* site_instance,
36                    const GURL& url,
37                    ViewType host_type);
38  virtual ~ExtensionViewHost();
39
40  ExtensionView* view() { return view_.get(); }
41  const ExtensionView* view() const { return view_.get(); }
42
43  // Create an ExtensionView and tie it to this host and |browser|.  Note NULL
44  // is a valid argument for |browser|.  Extension views may be bound to
45  // tab-contents hosted in ExternalTabContainer objects, which do not
46  // instantiate Browser objects.
47  void CreateView(Browser* browser);
48
49  void SetAssociatedWebContents(content::WebContents* web_contents);
50
51  // Handles keyboard events that were not handled by HandleKeyboardEvent().
52  // Platform specific implementation may override this method to handle the
53  // event in platform specific way.
54  virtual void UnhandledKeyboardEvent(
55      content::WebContents* source,
56      const content::NativeWebKeyboardEvent& event);
57
58  // ExtensionHost
59  virtual void OnDidStopLoading() OVERRIDE;
60  virtual void OnDocumentAvailable() OVERRIDE;
61  virtual void LoadInitialURL() OVERRIDE;
62  virtual bool IsBackgroundPage() const OVERRIDE;
63
64  // content::WebContentsDelegate
65  virtual content::WebContents* OpenURLFromTab(
66      content::WebContents* source,
67      const content::OpenURLParams& params) OVERRIDE;
68  virtual bool PreHandleKeyboardEvent(
69      content::WebContents* source,
70      const content::NativeWebKeyboardEvent& event,
71      bool* is_keyboard_shortcut) OVERRIDE;
72  virtual void HandleKeyboardEvent(
73      content::WebContents* source,
74      const content::NativeWebKeyboardEvent& event) OVERRIDE;
75  virtual bool PreHandleGestureEvent(
76      content::WebContents* source,
77      const blink::WebGestureEvent& event) OVERRIDE;
78  virtual content::ColorChooser* OpenColorChooser(
79      content::WebContents* web_contents,
80      SkColor color,
81      const std::vector<content::ColorSuggestion>& suggestions) OVERRIDE;
82  virtual void RunFileChooser(
83      content::WebContents* tab,
84      const content::FileChooserParams& params) OVERRIDE;
85  virtual void ResizeDueToAutoResize(content::WebContents* source,
86                                     const gfx::Size& new_size) OVERRIDE;
87
88  // content::WebContentsObserver
89  virtual void RenderViewCreated(
90      content::RenderViewHost* render_view_host) OVERRIDE;
91
92  // web_modal::WebContentsModalDialogManagerDelegate
93  virtual web_modal::WebContentsModalDialogHost*
94      GetWebContentsModalDialogHost() OVERRIDE;
95  virtual bool IsWebContentsVisible(
96      content::WebContents* web_contents) OVERRIDE;
97
98  // web_modal::WebContentsModalDialogHost
99  virtual gfx::NativeView GetHostView() const OVERRIDE;
100  virtual gfx::Point GetDialogPosition(const gfx::Size& size) OVERRIDE;
101  virtual gfx::Size GetMaximumDialogSize() OVERRIDE;
102  virtual void AddObserver(
103      web_modal::ModalDialogHostObserver* observer) OVERRIDE;
104  virtual void RemoveObserver(
105      web_modal::ModalDialogHostObserver* observer) OVERRIDE;
106
107  // extensions::ExtensionFunctionDispatcher::Delegate
108  virtual WindowController* GetExtensionWindowController() const OVERRIDE;
109  virtual content::WebContents* GetAssociatedWebContents() const OVERRIDE;
110  virtual content::WebContents* GetVisibleWebContents() const OVERRIDE;
111
112  // content::NotificationObserver
113  virtual void Observe(int type,
114                       const content::NotificationSource& source,
115                       const content::NotificationDetails& details) OVERRIDE;
116
117 private:
118  // Implemented per-platform. Create the platform-specific ExtensionView.
119  static scoped_ptr<ExtensionView> CreateExtensionView(ExtensionViewHost* host,
120                                                       Browser* browser);
121
122  // Insert a default style sheet for Extension Infobars.
123  void InsertInfobarCSS();
124
125  // Optional view that shows the rendered content in the UI.
126  scoped_ptr<ExtensionView> view_;
127
128  // The relevant WebContents associated with this ExtensionViewHost, if any.
129  content::WebContents* associated_web_contents_;
130
131  // Observer to detect when the associated web contents is destroyed.
132  class AssociatedWebContentsObserver;
133  scoped_ptr<AssociatedWebContentsObserver> associated_web_contents_observer_;
134
135  // Manage popups overlaying the WebContents in this EVH.
136  // TODO(gbillock): should usually not be used -- instead use the parent
137  // window's popup manager. Should only be used when the EVH is created without
138  // a parent window.
139  scoped_ptr<web_modal::PopupManager> popup_manager_;
140
141  DISALLOW_COPY_AND_ASSIGN(ExtensionViewHost);
142};
143
144}  // namespace extensions
145
146#endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_VIEW_HOST_H_
147