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