blob: bbb8d7e7d661b7c8d2554f8d7773351f0d8c0878 [file] [log] [blame]
[email protected]9e790bd2011-01-10 23:48:541// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]c64631652009-04-29 22:24:312// 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]32b76ef2010-07-26 23:08:247#pragma once
[email protected]c64631652009-04-29 22:24:318
[email protected]caf706f2010-10-26 17:54:089#include <string>
10#include <vector>
[email protected]8a17bd52009-06-06 08:19:4911
[email protected]3b63f8f42011-03-28 01:54:1512#include "base/memory/scoped_ptr.h"
[email protected]cc2c3432009-11-06 17:24:3613#include "base/perftimer.h"
[email protected]811bfe32009-07-01 08:46:2514#include "chrome/browser/extensions/extension_function_dispatcher.h"
[email protected]3ab9cb82011-06-03 18:02:0715#include "content/browser/javascript_dialogs.h"
[email protected]952a68e2011-11-17 00:36:1016#include "content/browser/tab_contents/tab_contents_delegate.h"
17#include "content/browser/tab_contents/tab_contents_observer.h"
[email protected]6c2381d2011-10-19 02:52:5318#include "content/public/browser/notification_observer.h"
19#include "content/public/browser/notification_registrar.h"
[email protected]14afc012011-12-01 01:04:3920#include "content/public/common/view_type.h"
[email protected]5de634712011-03-02 00:20:1921
[email protected]ab4eaf782009-06-10 00:11:2422#if defined(TOOLKIT_VIEWS)
[email protected]9e790bd2011-01-10 23:48:5423#include "chrome/browser/ui/views/extensions/extension_view.h"
[email protected]10eaf87f2009-09-16 00:59:0424#elif defined(OS_MACOSX)
[email protected]0920e652011-01-17 17:03:5325#include "chrome/browser/ui/cocoa/extensions/extension_view_mac.h"
[email protected]753efc42010-03-09 19:52:1626#elif defined(TOOLKIT_GTK)
[email protected]613af4d2011-03-10 19:01:4827#include "chrome/browser/ui/gtk/extensions/extension_view_gtk.h"
[email protected]ab4eaf782009-06-10 00:11:2428#endif
[email protected]c64631652009-04-29 22:24:3129
30class Browser;
31class Extension;
[email protected]c64631652009-04-29 22:24:3132class RenderWidgetHostView;
[email protected]57c6a652009-05-04 07:58:3433class TabContents;
[email protected]c64631652009-04-29 22:24:3134struct WebPreferences;
35
[email protected]f3b1a082011-11-18 00:34:3036namespace content {
37class RenderProcessHost;
38}
39
[email protected]c64631652009-04-29 22:24:3140// 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]2d2f6cfc2011-05-06 21:09:3343// browser UI, or it may be hidden.
[email protected]952a68e2011-11-17 00:36:1044class ExtensionHost : public TabContentsDelegate,
45 public TabContentsObserver,
[email protected]e95ad332009-08-03 19:44:2546 public ExtensionFunctionDispatcher::Delegate,
[email protected]952a68e2011-11-17 00:36:1047 public content::NotificationObserver {
[email protected]c64631652009-04-29 22:24:3148 public:
[email protected]84455002009-11-20 01:06:0049 class ProcessCreationQueue;
50
[email protected]9adb9692010-10-29 23:14:0251 ExtensionHost(const Extension* extension, SiteInstance* site_instance,
[email protected]da4dfc42011-10-12 15:53:5652 const GURL& url, content::ViewType host_type);
[email protected]3690ebe02011-05-25 09:08:1953 virtual ~ExtensionHost();
[email protected]c64631652009-04-29 22:24:3154
[email protected]a4c368182009-05-29 21:53:2755#if defined(TOOLKIT_VIEWS)
[email protected]ab4eaf782009-06-10 00:11:2456 void set_view(ExtensionView* view) { view_.reset(view); }
[email protected]f4f50ef2011-01-21 19:01:1957 const ExtensionView* view() const { return view_.get(); }
58 ExtensionView* view() { return view_.get(); }
[email protected]10eaf87f2009-09-16 00:59:0459#elif defined(OS_MACOSX)
[email protected]f4f50ef2011-01-21 19:01:1960 const ExtensionViewMac* view() const { return view_.get(); }
61 ExtensionViewMac* view() { return view_.get(); }
[email protected]753efc42010-03-09 19:52:1662#elif defined(TOOLKIT_GTK)
[email protected]f4f50ef2011-01-21 19:01:1963 const ExtensionViewGtk* view() const { return view_.get(); }
64 ExtensionViewGtk* view() { return view_.get(); }
[email protected]68000152009-05-29 06:41:1665#endif
[email protected]ab4eaf782009-06-10 00:11:2466
[email protected]a95631cb2009-12-10 01:59:1167 // 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]ab4eaf782009-06-10 00:11:2471 void CreateView(Browser* browser);
72
[email protected]f4f50ef2011-01-21 19:01:1973 const Extension* extension() const { return extension_; }
[email protected]2d2f6cfc2011-05-06 21:09:3374 const std::string& extension_id() const { return extension_id_; }
[email protected]952a68e2011-11-17 00:36:1075 TabContents* host_contents() const { return host_contents_.get(); }
76 RenderViewHost* render_view_host() const;
[email protected]f3b1a082011-11-18 00:34:3077 content::RenderProcessHost* render_process_host() const;
[email protected]c64631652009-04-29 22:24:3178 bool did_stop_loading() const { return did_stop_loading_; }
[email protected]e95ad332009-08-03 19:44:2579 bool document_element_available() const {
80 return document_element_available_;
81 }
[email protected]01f829a2010-03-17 18:20:3182
[email protected]2d8d9232009-10-02 20:19:2083 Profile* profile() const { return profile_; }
[email protected]c64631652009-04-29 22:24:3184
[email protected]952a68e2011-11-17 00:36:1085 content::ViewType extension_host_type() const { return extension_host_type_; }
86 const GURL& GetURL() const;
[email protected]f8e55e72010-02-25 06:13:4387
[email protected]9aa2eaa2010-04-15 19:05:0788 // ExtensionFunctionDispatcher::Delegate
[email protected]17902752011-08-31 22:52:5489 virtual TabContents* GetAssociatedTabContents() const OVERRIDE;
[email protected]9aa2eaa2010-04-15 19:05:0790 void set_associated_tab_contents(TabContents* associated_tab_contents) {
91 associated_tab_contents_ = associated_tab_contents;
92 }
93
[email protected]7c6877d2009-06-19 13:56:2594 // Returns true if the render view is initialized and didn't crash.
95 bool IsRenderViewLive() const;
96
[email protected]84455002009-11-20 01:06:0097 // 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]952a68e2011-11-17 00:36:10100 void CreateRenderViewSoon();
[email protected]bbc945542009-07-26 00:11:42101
[email protected]df3bdcf2010-03-18 21:30:32102 // Insert a default style sheet for Extension Infobars.
[email protected]f34e79632010-03-17 02:34:08103 void InsertInfobarCSS();
104
[email protected]cda45c02010-02-25 19:28:10105 // 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]952a68e2011-11-17 00:36:10109 // TabContentsObserver
[email protected]544e27f2011-07-25 21:41:54110 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
[email protected]544e27f2011-07-25 21:41:54111 virtual void RenderViewCreated(RenderViewHost* render_view_host) OVERRIDE;
[email protected]4f4d42a2011-12-02 02:42:49112 virtual void RenderViewDeleted(RenderViewHost* render_view_host) OVERRIDE;
[email protected]952a68e2011-11-17 00:36:10113 virtual void RenderViewReady() OVERRIDE;
114 virtual void RenderViewGone(base::TerminationStatus status) OVERRIDE;
115 virtual void DocumentAvailableInMainFrame() OVERRIDE;
[email protected]544e27f2011-07-25 21:41:54116 virtual void DidStopLoading() OVERRIDE;
[email protected]952a68e2011-11-17 00:36:10117
118 // TabContentsDelegate
[email protected]63954792011-07-11 04:17:48119 virtual bool PreHandleKeyboardEvent(const NativeWebKeyboardEvent& event,
[email protected]544e27f2011-07-25 21:41:54120 bool* is_keyboard_shortcut) OVERRIDE;
121 virtual void HandleKeyboardEvent(const NativeWebKeyboardEvent& event)
122 OVERRIDE;
[email protected]952a68e2011-11-17 00:36:10123 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]c64631652009-04-29 22:24:31133
[email protected]6c2381d2011-10-19 02:52:53134 // content::NotificationObserver
[email protected]432115822011-07-10 15:52:27135 virtual void Observe(int type,
[email protected]6c2381d2011-10-19 02:52:53136 const content::NotificationSource& source,
137 const content::NotificationDetails& details) OVERRIDE;
[email protected]e95ad332009-08-03 19:44:25138
[email protected]cf390ea42009-12-09 04:14:02139 protected:
[email protected]b6cf240f2011-10-15 22:09:53140 // This should only be used by unit tests.
141 ExtensionHost(const Extension* extension, content::ViewType host_type);
142
[email protected]c64631652009-04-29 22:24:31143 private:
[email protected]84455002009-11-20 01:06:00144 friend class ProcessCreationQueue;
145
[email protected]84455002009-11-20 01:06:00146 // Actually create the RenderView for this host. See CreateRenderViewSoon.
147 void CreateRenderViewNow();
148
[email protected]952a68e2011-11-17 00:36:10149 // Navigates to the initial page.
150 void LoadInitialURL();
151
[email protected]f4f50ef2011-01-21 19:01:19152 // Const version of below function.
153 const Browser* GetBrowser() const;
154
[email protected]7eecaed52009-05-07 21:44:12155 // ExtensionFunctionDispatcher::Delegate
[email protected]17902752011-08-31 22:52:54156 virtual Browser* GetBrowser() OVERRIDE;
157 virtual gfx::NativeView GetNativeViewOfHost() OVERRIDE;
[email protected]b27257562009-11-16 23:28:26158
[email protected]34f128d2011-01-25 19:07:44159 // Message handlers.
[email protected]c5dbef02011-05-13 05:06:09160 void OnRequest(const ExtensionHostMsg_Request_Params& params);
[email protected]34f128d2011-01-25 19:07:44161
[email protected]f8e55e72010-02-25 06:13:43162 // 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]e95ad332009-08-03 19:44:25167 // 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]c64631652009-04-29 22:24:31171 // The extension that we're hosting in this view.
[email protected]9adb9692010-10-29 23:14:02172 const Extension* extension_;
[email protected]c64631652009-04-29 22:24:31173
[email protected]2d2f6cfc2011-05-06 21:09:33174 // Id of extension that we're hosting in this view.
175 const std::string extension_id_;
176
[email protected]382a0702009-06-26 17:12:27177 // The profile that this host is tied to.
178 Profile* profile_;
[email protected]8a17bd52009-06-06 08:19:49179
[email protected]c64631652009-04-29 22:24:31180 // Optional view that shows the rendered content in the UI.
[email protected]f86ff452009-07-28 22:45:58181#if defined(TOOLKIT_VIEWS)
[email protected]ab4eaf782009-06-10 00:11:24182 scoped_ptr<ExtensionView> view_;
[email protected]10eaf87f2009-09-16 00:59:04183#elif defined(OS_MACOSX)
184 scoped_ptr<ExtensionViewMac> view_;
[email protected]753efc42010-03-09 19:52:16185#elif defined(TOOLKIT_GTK)
186 scoped_ptr<ExtensionViewGtk> view_;
[email protected]68000152009-05-29 06:41:16187#endif
[email protected]c64631652009-04-29 22:24:31188
189 // The host for our HTML content.
[email protected]952a68e2011-11-17 00:36:10190 scoped_ptr<TabContents> host_contents_;
[email protected]c64631652009-04-29 22:24:31191
[email protected]4f4d42a2011-12-02 02:42:49192 // 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]c64631652009-04-29 22:24:31197 // Whether the RenderWidget has reported that it has stopped loading.
198 bool did_stop_loading_;
199
[email protected]e95ad332009-08-03 19:44:25200 // True if the main frame has finished parsing.
201 bool document_element_available_;
202
[email protected]952a68e2011-11-17 00:36:10203 // The original URL of the page being hosted.
204 GURL initial_url_;
[email protected]e916901c2009-05-07 00:14:31205
[email protected]6c2381d2011-10-19 02:52:53206 content::NotificationRegistrar registrar_;
[email protected]e95ad332009-08-03 19:44:25207
[email protected]c5dbef02011-05-13 05:06:09208 ExtensionFunctionDispatcher extension_function_dispatcher_;
[email protected]811bfe32009-07-01 08:46:25209
[email protected]6d7a6042010-08-12 20:12:42210 // Only EXTENSION_INFOBAR, EXTENSION_POPUP, and EXTENSION_BACKGROUND_PAGE
[email protected]1c1c77a52009-11-03 00:37:31211 // are used here, others are not hosted by ExtensionHost.
[email protected]da4dfc42011-10-12 15:53:56212 content::ViewType extension_host_type_;
[email protected]7b291f92009-08-14 05:43:53213
[email protected]9aa2eaa2010-04-15 19:05:07214 // The relevant TabContents associated with this ExtensionHost, if any.
215 TabContents* associated_tab_contents_;
216
[email protected]cc2c3432009-11-06 17:24:36217 // Used to measure how long it's been since the host was created.
218 PerfTimer since_created_;
219
[email protected]c64631652009-04-29 22:24:31220 DISALLOW_COPY_AND_ASSIGN(ExtensionHost);
221};
222
223#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_HOST_H_