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 const PlatformExtensionView* view() const { return view_.get(); } 56 57 // Create an ExtensionView and tie it to this host and |browser|. Note NULL 58 // is a valid argument for |browser|. Extension views may be bound to 59 // tab-contents hosted in ExternalTabContainer objects, which do not 60 // instantiate Browser objects. 61 void CreateView(Browser* browser); 62 63 void SetAssociatedWebContents(content::WebContents* web_contents); 64 65 // Handles keyboard events that were not handled by HandleKeyboardEvent(). 66 // Platform specific implementation may override this method to handle the 67 // event in platform specific way. 68 virtual void UnhandledKeyboardEvent( 69 content::WebContents* source, 70 const content::NativeWebKeyboardEvent& event); 71 72 // ExtensionHost 73 virtual void OnDidStopLoading() OVERRIDE; 74 virtual void OnDocumentAvailable() OVERRIDE; 75 virtual void LoadInitialURL() OVERRIDE; 76 virtual bool IsBackgroundPage() const OVERRIDE; 77 78 // content::WebContentsDelegate 79 virtual content::WebContents* OpenURLFromTab( 80 content::WebContents* source, 81 const content::OpenURLParams& params) OVERRIDE; 82 virtual bool PreHandleKeyboardEvent( 83 content::WebContents* source, 84 const content::NativeWebKeyboardEvent& event, 85 bool* is_keyboard_shortcut) OVERRIDE; 86 virtual void HandleKeyboardEvent( 87 content::WebContents* source, 88 const content::NativeWebKeyboardEvent& event) OVERRIDE; 89 virtual bool PreHandleGestureEvent( 90 content::WebContents* source, 91 const blink::WebGestureEvent& event) OVERRIDE; 92 virtual content::ColorChooser* OpenColorChooser( 93 content::WebContents* web_contents, 94 SkColor color, 95 const std::vector<content::ColorSuggestion>& suggestions) OVERRIDE; 96 virtual void RunFileChooser( 97 content::WebContents* tab, 98 const content::FileChooserParams& params) OVERRIDE; 99 virtual void ResizeDueToAutoResize(content::WebContents* source, 100 const gfx::Size& new_size) OVERRIDE; 101 102 // content::WebContentsObserver 103 virtual void RenderViewCreated( 104 content::RenderViewHost* render_view_host) OVERRIDE; 105 106 // web_modal::WebContentsModalDialogManagerDelegate 107 virtual web_modal::WebContentsModalDialogHost* 108 GetWebContentsModalDialogHost() OVERRIDE; 109 virtual bool IsWebContentsVisible( 110 content::WebContents* web_contents) OVERRIDE; 111 112 // web_modal::WebContentsModalDialogHost 113 virtual gfx::NativeView GetHostView() const OVERRIDE; 114 virtual gfx::Point GetDialogPosition(const gfx::Size& size) OVERRIDE; 115 virtual gfx::Size GetMaximumDialogSize() OVERRIDE; 116 virtual void AddObserver( 117 web_modal::ModalDialogHostObserver* observer) OVERRIDE; 118 virtual void RemoveObserver( 119 web_modal::ModalDialogHostObserver* observer) OVERRIDE; 120 121 // extensions::ExtensionFunctionDispatcher::Delegate 122 virtual WindowController* GetExtensionWindowController() const OVERRIDE; 123 virtual content::WebContents* GetAssociatedWebContents() const OVERRIDE; 124 virtual content::WebContents* GetVisibleWebContents() const OVERRIDE; 125 126 // content::NotificationObserver 127 virtual void Observe(int type, 128 const content::NotificationSource& source, 129 const content::NotificationDetails& details) OVERRIDE; 130 131 private: 132 // Insert a default style sheet for Extension Infobars. 133 void InsertInfobarCSS(); 134 135 // Optional view that shows the rendered content in the UI. 136 scoped_ptr<PlatformExtensionView> view_; 137 138 // The relevant WebContents associated with this ExtensionViewHost, if any. 139 content::WebContents* associated_web_contents_; 140 141 // Observer to detect when the associated web contents is destroyed. 142 class AssociatedWebContentsObserver; 143 scoped_ptr<AssociatedWebContentsObserver> associated_web_contents_observer_; 144 145 DISALLOW_COPY_AND_ASSIGN(ExtensionViewHost); 146 }; 147 148 } // namespace extensions 149 150 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_VIEW_HOST_H_ 151