[email protected] | 9e790bd | 2011-01-10 23:48:54 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 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_HOST_H_ |
| 6 | #define CHROME_BROWSER_EXTENSIONS_EXTENSION_HOST_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 8 | |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <vector> |
[email protected] | 8a17bd5 | 2009-06-06 08:19:49 | [diff] [blame] | 11 | |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 12 | #include "base/memory/scoped_ptr.h" |
[email protected] | cc2c343 | 2009-11-06 17:24:36 | [diff] [blame] | 13 | #include "base/perftimer.h" |
[email protected] | 811bfe3 | 2009-07-01 08:46:25 | [diff] [blame] | 14 | #include "chrome/browser/extensions/extension_function_dispatcher.h" |
[email protected] | 3ab9cb8 | 2011-06-03 18:02:07 | [diff] [blame] | 15 | #include "content/browser/javascript_dialogs.h" |
[email protected] | 952a68e | 2011-11-17 00:36:10 | [diff] [blame] | 16 | #include "content/browser/tab_contents/tab_contents_delegate.h" |
| 17 | #include "content/browser/tab_contents/tab_contents_observer.h" |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 18 | #include "content/public/browser/notification_observer.h" |
| 19 | #include "content/public/browser/notification_registrar.h" |
[email protected] | 14afc01 | 2011-12-01 01:04:39 | [diff] [blame] | 20 | #include "content/public/common/view_type.h" |
[email protected] | 5de63471 | 2011-03-02 00:20:19 | [diff] [blame] | 21 | |
[email protected] | ab4eaf78 | 2009-06-10 00:11:24 | [diff] [blame] | 22 | #if defined(TOOLKIT_VIEWS) |
[email protected] | 9e790bd | 2011-01-10 23:48:54 | [diff] [blame] | 23 | #include "chrome/browser/ui/views/extensions/extension_view.h" |
[email protected] | 10eaf87f | 2009-09-16 00:59:04 | [diff] [blame] | 24 | #elif defined(OS_MACOSX) |
[email protected] | 0920e65 | 2011-01-17 17:03:53 | [diff] [blame] | 25 | #include "chrome/browser/ui/cocoa/extensions/extension_view_mac.h" |
[email protected] | 753efc4 | 2010-03-09 19:52:16 | [diff] [blame] | 26 | #elif defined(TOOLKIT_GTK) |
[email protected] | 613af4d | 2011-03-10 19:01:48 | [diff] [blame] | 27 | #include "chrome/browser/ui/gtk/extensions/extension_view_gtk.h" |
[email protected] | ab4eaf78 | 2009-06-10 00:11:24 | [diff] [blame] | 28 | #endif |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 29 | |
| 30 | class Browser; |
| 31 | class Extension; |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 32 | class RenderWidgetHostView; |
[email protected] | 57c6a65 | 2009-05-04 07:58:34 | [diff] [blame] | 33 | class TabContents; |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 34 | struct WebPreferences; |
| 35 | |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 36 | namespace content { |
| 37 | class RenderProcessHost; |
| 38 | } |
| 39 | |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 40 | // This class is the browser component of an extension component's RenderView. |
| 41 | // It handles setting up the renderer process, if needed, with special |
| 42 | // privileges available to extensions. It may have a view to be shown in the |
[email protected] | 2d2f6cfc | 2011-05-06 21:09:33 | [diff] [blame] | 43 | // browser UI, or it may be hidden. |
[email protected] | 952a68e | 2011-11-17 00:36:10 | [diff] [blame] | 44 | class ExtensionHost : public TabContentsDelegate, |
| 45 | public TabContentsObserver, |
[email protected] | e95ad33 | 2009-08-03 19:44:25 | [diff] [blame] | 46 | public ExtensionFunctionDispatcher::Delegate, |
[email protected] | 952a68e | 2011-11-17 00:36:10 | [diff] [blame] | 47 | public content::NotificationObserver { |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 48 | public: |
[email protected] | 8445500 | 2009-11-20 01:06:00 | [diff] [blame] | 49 | class ProcessCreationQueue; |
| 50 | |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 51 | ExtensionHost(const Extension* extension, SiteInstance* site_instance, |
[email protected] | da4dfc4 | 2011-10-12 15:53:56 | [diff] [blame] | 52 | const GURL& url, content::ViewType host_type); |
[email protected] | 3690ebe0 | 2011-05-25 09:08:19 | [diff] [blame] | 53 | virtual ~ExtensionHost(); |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 54 | |
[email protected] | a4c36818 | 2009-05-29 21:53:27 | [diff] [blame] | 55 | #if defined(TOOLKIT_VIEWS) |
[email protected] | ab4eaf78 | 2009-06-10 00:11:24 | [diff] [blame] | 56 | void set_view(ExtensionView* view) { view_.reset(view); } |
[email protected] | f4f50ef | 2011-01-21 19:01:19 | [diff] [blame] | 57 | const ExtensionView* view() const { return view_.get(); } |
| 58 | ExtensionView* view() { return view_.get(); } |
[email protected] | 10eaf87f | 2009-09-16 00:59:04 | [diff] [blame] | 59 | #elif defined(OS_MACOSX) |
[email protected] | f4f50ef | 2011-01-21 19:01:19 | [diff] [blame] | 60 | const ExtensionViewMac* view() const { return view_.get(); } |
| 61 | ExtensionViewMac* view() { return view_.get(); } |
[email protected] | 753efc4 | 2010-03-09 19:52:16 | [diff] [blame] | 62 | #elif defined(TOOLKIT_GTK) |
[email protected] | f4f50ef | 2011-01-21 19:01:19 | [diff] [blame] | 63 | const ExtensionViewGtk* view() const { return view_.get(); } |
| 64 | ExtensionViewGtk* view() { return view_.get(); } |
[email protected] | 6800015 | 2009-05-29 06:41:16 | [diff] [blame] | 65 | #endif |
[email protected] | ab4eaf78 | 2009-06-10 00:11:24 | [diff] [blame] | 66 | |
[email protected] | a95631cb | 2009-12-10 01:59:11 | [diff] [blame] | 67 | // Create an ExtensionView and tie it to this host and |browser|. Note NULL |
| 68 | // is a valid argument for |browser|. Extension views may be bound to |
| 69 | // tab-contents hosted in ExternalTabContainer objects, which do not |
| 70 | // instantiate Browser objects. |
[email protected] | ab4eaf78 | 2009-06-10 00:11:24 | [diff] [blame] | 71 | void CreateView(Browser* browser); |
| 72 | |
[email protected] | f4f50ef | 2011-01-21 19:01:19 | [diff] [blame] | 73 | const Extension* extension() const { return extension_; } |
[email protected] | 2d2f6cfc | 2011-05-06 21:09:33 | [diff] [blame] | 74 | const std::string& extension_id() const { return extension_id_; } |
[email protected] | 952a68e | 2011-11-17 00:36:10 | [diff] [blame] | 75 | TabContents* host_contents() const { return host_contents_.get(); } |
| 76 | RenderViewHost* render_view_host() const; |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 77 | content::RenderProcessHost* render_process_host() const; |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 78 | bool did_stop_loading() const { return did_stop_loading_; } |
[email protected] | e95ad33 | 2009-08-03 19:44:25 | [diff] [blame] | 79 | bool document_element_available() const { |
| 80 | return document_element_available_; |
| 81 | } |
[email protected] | 01f829a | 2010-03-17 18:20:31 | [diff] [blame] | 82 | |
[email protected] | 2d8d923 | 2009-10-02 20:19:20 | [diff] [blame] | 83 | Profile* profile() const { return profile_; } |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 84 | |
[email protected] | 952a68e | 2011-11-17 00:36:10 | [diff] [blame] | 85 | content::ViewType extension_host_type() const { return extension_host_type_; } |
| 86 | const GURL& GetURL() const; |
[email protected] | f8e55e7 | 2010-02-25 06:13:43 | [diff] [blame] | 87 | |
[email protected] | 9aa2eaa | 2010-04-15 19:05:07 | [diff] [blame] | 88 | // ExtensionFunctionDispatcher::Delegate |
[email protected] | 1790275 | 2011-08-31 22:52:54 | [diff] [blame] | 89 | virtual TabContents* GetAssociatedTabContents() const OVERRIDE; |
[email protected] | 9aa2eaa | 2010-04-15 19:05:07 | [diff] [blame] | 90 | void set_associated_tab_contents(TabContents* associated_tab_contents) { |
| 91 | associated_tab_contents_ = associated_tab_contents; |
| 92 | } |
| 93 | |
[email protected] | 7c6877d | 2009-06-19 13:56:25 | [diff] [blame] | 94 | // Returns true if the render view is initialized and didn't crash. |
| 95 | bool IsRenderViewLive() const; |
| 96 | |
[email protected] | 8445500 | 2009-11-20 01:06:00 | [diff] [blame] | 97 | // Prepares to initializes our RenderViewHost by creating its RenderView and |
| 98 | // navigating to this host's url. Uses host_view for the RenderViewHost's view |
| 99 | // (can be NULL). This happens delayed to avoid locking the UI. |
[email protected] | 952a68e | 2011-11-17 00:36:10 | [diff] [blame] | 100 | void CreateRenderViewSoon(); |
[email protected] | bbc94554 | 2009-07-26 00:11:42 | [diff] [blame] | 101 | |
[email protected] | df3bdcf | 2010-03-18 21:30:32 | [diff] [blame] | 102 | // Insert a default style sheet for Extension Infobars. |
[email protected] | f34e7963 | 2010-03-17 02:34:08 | [diff] [blame] | 103 | void InsertInfobarCSS(); |
| 104 | |
[email protected] | cda45c0 | 2010-02-25 19:28:10 | [diff] [blame] | 105 | // Tell the renderer not to draw scrollbars on windows smaller than |
| 106 | // |size_limit| in both width and height. |
| 107 | void DisableScrollbarsForSmallWindows(const gfx::Size& size_limit); |
| 108 | |
[email protected] | 952a68e | 2011-11-17 00:36:10 | [diff] [blame] | 109 | // TabContentsObserver |
[email protected] | 544e27f | 2011-07-25 21:41:54 | [diff] [blame] | 110 | virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
[email protected] | 544e27f | 2011-07-25 21:41:54 | [diff] [blame] | 111 | virtual void RenderViewCreated(RenderViewHost* render_view_host) OVERRIDE; |
[email protected] | 4f4d42a | 2011-12-02 02:42:49 | [diff] [blame^] | 112 | virtual void RenderViewDeleted(RenderViewHost* render_view_host) OVERRIDE; |
[email protected] | 952a68e | 2011-11-17 00:36:10 | [diff] [blame] | 113 | virtual void RenderViewReady() OVERRIDE; |
| 114 | virtual void RenderViewGone(base::TerminationStatus status) OVERRIDE; |
| 115 | virtual void DocumentAvailableInMainFrame() OVERRIDE; |
[email protected] | 544e27f | 2011-07-25 21:41:54 | [diff] [blame] | 116 | virtual void DidStopLoading() OVERRIDE; |
[email protected] | 952a68e | 2011-11-17 00:36:10 | [diff] [blame] | 117 | |
| 118 | // TabContentsDelegate |
[email protected] | 6395479 | 2011-07-11 04:17:48 | [diff] [blame] | 119 | virtual bool PreHandleKeyboardEvent(const NativeWebKeyboardEvent& event, |
[email protected] | 544e27f | 2011-07-25 21:41:54 | [diff] [blame] | 120 | bool* is_keyboard_shortcut) OVERRIDE; |
| 121 | virtual void HandleKeyboardEvent(const NativeWebKeyboardEvent& event) |
| 122 | OVERRIDE; |
[email protected] | 952a68e | 2011-11-17 00:36:10 | [diff] [blame] | 123 | virtual void UpdatePreferredSize(TabContents* source, |
| 124 | const gfx::Size& pref_size) OVERRIDE; |
| 125 | virtual content::JavaScriptDialogCreator* GetJavaScriptDialogCreator() |
| 126 | OVERRIDE; |
| 127 | virtual void AddNewContents(TabContents* source, |
| 128 | TabContents* new_contents, |
| 129 | WindowOpenDisposition disposition, |
| 130 | const gfx::Rect& initial_pos, |
| 131 | bool user_gesture) OVERRIDE; |
| 132 | virtual void CloseContents(TabContents* contents) OVERRIDE; |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 133 | |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 134 | // content::NotificationObserver |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 135 | virtual void Observe(int type, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 136 | const content::NotificationSource& source, |
| 137 | const content::NotificationDetails& details) OVERRIDE; |
[email protected] | e95ad33 | 2009-08-03 19:44:25 | [diff] [blame] | 138 | |
[email protected] | cf390ea4 | 2009-12-09 04:14:02 | [diff] [blame] | 139 | protected: |
[email protected] | b6cf240f | 2011-10-15 22:09:53 | [diff] [blame] | 140 | // This should only be used by unit tests. |
| 141 | ExtensionHost(const Extension* extension, content::ViewType host_type); |
| 142 | |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 143 | private: |
[email protected] | 8445500 | 2009-11-20 01:06:00 | [diff] [blame] | 144 | friend class ProcessCreationQueue; |
| 145 | |
[email protected] | 8445500 | 2009-11-20 01:06:00 | [diff] [blame] | 146 | // Actually create the RenderView for this host. See CreateRenderViewSoon. |
| 147 | void CreateRenderViewNow(); |
| 148 | |
[email protected] | 952a68e | 2011-11-17 00:36:10 | [diff] [blame] | 149 | // Navigates to the initial page. |
| 150 | void LoadInitialURL(); |
| 151 | |
[email protected] | f4f50ef | 2011-01-21 19:01:19 | [diff] [blame] | 152 | // Const version of below function. |
| 153 | const Browser* GetBrowser() const; |
| 154 | |
[email protected] | 7eecaed5 | 2009-05-07 21:44:12 | [diff] [blame] | 155 | // ExtensionFunctionDispatcher::Delegate |
[email protected] | 1790275 | 2011-08-31 22:52:54 | [diff] [blame] | 156 | virtual Browser* GetBrowser() OVERRIDE; |
| 157 | virtual gfx::NativeView GetNativeViewOfHost() OVERRIDE; |
[email protected] | b2725756 | 2009-11-16 23:28:26 | [diff] [blame] | 158 | |
[email protected] | 34f128d | 2011-01-25 19:07:44 | [diff] [blame] | 159 | // Message handlers. |
[email protected] | c5dbef0 | 2011-05-13 05:06:09 | [diff] [blame] | 160 | void OnRequest(const ExtensionHostMsg_Request_Params& params); |
[email protected] | 34f128d | 2011-01-25 19:07:44 | [diff] [blame] | 161 | |
[email protected] | f8e55e7 | 2010-02-25 06:13:43 | [diff] [blame] | 162 | // Handles keyboard events that were not handled by HandleKeyboardEvent(). |
| 163 | // Platform specific implementation may override this method to handle the |
| 164 | // event in platform specific way. |
| 165 | virtual void UnhandledKeyboardEvent(const NativeWebKeyboardEvent& event) {} |
| 166 | |
[email protected] | e95ad33 | 2009-08-03 19:44:25 | [diff] [blame] | 167 | // Returns true if we're hosting a background page. |
| 168 | // This isn't valid until CreateRenderView is called. |
| 169 | bool is_background_page() const { return !view(); } |
| 170 | |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 171 | // The extension that we're hosting in this view. |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 172 | const Extension* extension_; |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 173 | |
[email protected] | 2d2f6cfc | 2011-05-06 21:09:33 | [diff] [blame] | 174 | // Id of extension that we're hosting in this view. |
| 175 | const std::string extension_id_; |
| 176 | |
[email protected] | 382a070 | 2009-06-26 17:12:27 | [diff] [blame] | 177 | // The profile that this host is tied to. |
| 178 | Profile* profile_; |
[email protected] | 8a17bd5 | 2009-06-06 08:19:49 | [diff] [blame] | 179 | |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 180 | // Optional view that shows the rendered content in the UI. |
[email protected] | f86ff45 | 2009-07-28 22:45:58 | [diff] [blame] | 181 | #if defined(TOOLKIT_VIEWS) |
[email protected] | ab4eaf78 | 2009-06-10 00:11:24 | [diff] [blame] | 182 | scoped_ptr<ExtensionView> view_; |
[email protected] | 10eaf87f | 2009-09-16 00:59:04 | [diff] [blame] | 183 | #elif defined(OS_MACOSX) |
| 184 | scoped_ptr<ExtensionViewMac> view_; |
[email protected] | 753efc4 | 2010-03-09 19:52:16 | [diff] [blame] | 185 | #elif defined(TOOLKIT_GTK) |
| 186 | scoped_ptr<ExtensionViewGtk> view_; |
[email protected] | 6800015 | 2009-05-29 06:41:16 | [diff] [blame] | 187 | #endif |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 188 | |
| 189 | // The host for our HTML content. |
[email protected] | 952a68e | 2011-11-17 00:36:10 | [diff] [blame] | 190 | scoped_ptr<TabContents> host_contents_; |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 191 | |
[email protected] | 4f4d42a | 2011-12-02 02:42:49 | [diff] [blame^] | 192 | // A weak pointer to the current or pending RenderViewHost. We don't access |
| 193 | // this through the host_contents because we want to deal with the pending |
| 194 | // host, so we can send messages to it before it finishes loading. |
| 195 | RenderViewHost* render_view_host_; |
| 196 | |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 197 | // Whether the RenderWidget has reported that it has stopped loading. |
| 198 | bool did_stop_loading_; |
| 199 | |
[email protected] | e95ad33 | 2009-08-03 19:44:25 | [diff] [blame] | 200 | // True if the main frame has finished parsing. |
| 201 | bool document_element_available_; |
| 202 | |
[email protected] | 952a68e | 2011-11-17 00:36:10 | [diff] [blame] | 203 | // The original URL of the page being hosted. |
| 204 | GURL initial_url_; |
[email protected] | e916901c | 2009-05-07 00:14:31 | [diff] [blame] | 205 | |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 206 | content::NotificationRegistrar registrar_; |
[email protected] | e95ad33 | 2009-08-03 19:44:25 | [diff] [blame] | 207 | |
[email protected] | c5dbef0 | 2011-05-13 05:06:09 | [diff] [blame] | 208 | ExtensionFunctionDispatcher extension_function_dispatcher_; |
[email protected] | 811bfe3 | 2009-07-01 08:46:25 | [diff] [blame] | 209 | |
[email protected] | 6d7a604 | 2010-08-12 20:12:42 | [diff] [blame] | 210 | // Only EXTENSION_INFOBAR, EXTENSION_POPUP, and EXTENSION_BACKGROUND_PAGE |
[email protected] | 1c1c77a5 | 2009-11-03 00:37:31 | [diff] [blame] | 211 | // are used here, others are not hosted by ExtensionHost. |
[email protected] | da4dfc4 | 2011-10-12 15:53:56 | [diff] [blame] | 212 | content::ViewType extension_host_type_; |
[email protected] | 7b291f9 | 2009-08-14 05:43:53 | [diff] [blame] | 213 | |
[email protected] | 9aa2eaa | 2010-04-15 19:05:07 | [diff] [blame] | 214 | // The relevant TabContents associated with this ExtensionHost, if any. |
| 215 | TabContents* associated_tab_contents_; |
| 216 | |
[email protected] | cc2c343 | 2009-11-06 17:24:36 | [diff] [blame] | 217 | // Used to measure how long it's been since the host was created. |
| 218 | PerfTimer since_created_; |
| 219 | |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 220 | DISALLOW_COPY_AND_ASSIGN(ExtensionHost); |
| 221 | }; |
| 222 | |
| 223 | #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_HOST_H_ |