[email protected] | 22401dc | 2014-03-21 01:38:57 | [diff] [blame] | 1 | // Copyright 2014 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 | |
[email protected] | 22401dc | 2014-03-21 01:38:57 | [diff] [blame] | 5 | #ifndef EXTENSIONS_BROWSER_EXTENSION_HOST_H_ |
| 6 | #define EXTENSIONS_BROWSER_EXTENSION_HOST_H_ |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 7 | |
avi | c9cec10 | 2015-12-23 00:39:26 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame^] | 10 | #include <memory> |
chirantan | 79788f6 | 2015-02-02 23:57:25 | [diff] [blame] | 11 | #include <set> |
[email protected] | caf706f | 2010-10-26 17:54:08 | [diff] [blame] | 12 | #include <string> |
[email protected] | 8a17bd5 | 2009-06-06 08:19:49 | [diff] [blame] | 13 | |
[email protected] | a808bc5 | 2012-02-14 03:20:37 | [diff] [blame] | 14 | #include "base/logging.h" |
avi | c9cec10 | 2015-12-23 00:39:26 | [diff] [blame] | 15 | #include "base/macros.h" |
chirantan | 79788f6 | 2015-02-02 23:57:25 | [diff] [blame] | 16 | #include "base/observer_list.h" |
[email protected] | bd5a374 | 2013-09-29 18:06:13 | [diff] [blame] | 17 | #include "base/timer/elapsed_timer.h" |
[email protected] | 674bc59 | 2011-12-20 23:00:42 | [diff] [blame] | 18 | #include "content/public/browser/web_contents_delegate.h" |
[email protected] | d8c66043 | 2011-12-22 20:51:25 | [diff] [blame] | 19 | #include "content/public/browser/web_contents_observer.h" |
yoz | d61dfe19 | 2015-02-21 01:30:37 | [diff] [blame] | 20 | #include "extensions/browser/deferred_start_render_host.h" |
[email protected] | 0b9de03 | 2014-03-15 05:47:01 | [diff] [blame] | 21 | #include "extensions/browser/extension_function_dispatcher.h" |
limasdf | bbaa913d | 2015-03-02 23:52:33 | [diff] [blame] | 22 | #include "extensions/browser/extension_registry_observer.h" |
[email protected] | 88b50b6 | 2013-09-01 23:05:06 | [diff] [blame] | 23 | #include "extensions/common/stack_frame.h" |
[email protected] | cb2edf2 | 2013-04-01 20:25:23 | [diff] [blame] | 24 | #include "extensions/common/view_type.h" |
[email protected] | 5de63471 | 2011-03-02 00:20:19 | [diff] [blame] | 25 | |
[email protected] | ea6ad01a | 2011-12-21 21:53:49 | [diff] [blame] | 26 | class PrefsTabHelper; |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 27 | |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 28 | namespace content { |
[email protected] | 9fe4204 | 2013-10-29 21:13:33 | [diff] [blame] | 29 | class BrowserContext; |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 30 | class RenderProcessHost; |
[email protected] | 5626b089 | 2012-02-20 14:46:58 | [diff] [blame] | 31 | class RenderWidgetHostView; |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 32 | class SiteInstance; |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 33 | } |
| 34 | |
[email protected] | 1c321ee5 | 2012-05-21 03:02:34 | [diff] [blame] | 35 | namespace extensions { |
| 36 | class Extension; |
[email protected] | 6c0f179f | 2014-03-19 11:04:41 | [diff] [blame] | 37 | class ExtensionHostDelegate; |
chirantan | 79788f6 | 2015-02-02 23:57:25 | [diff] [blame] | 38 | class ExtensionHostObserver; |
kalman | fcece45 | 2015-02-18 18:20:42 | [diff] [blame] | 39 | class ExtensionHostQueue; |
[email protected] | 44f4b13 | 2012-07-17 20:36:57 | [diff] [blame] | 40 | class WindowController; |
[email protected] | 1c321ee5 | 2012-05-21 03:02:34 | [diff] [blame] | 41 | |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 42 | // This class is the browser component of an extension component's RenderView. |
| 43 | // It handles setting up the renderer process, if needed, with special |
| 44 | // 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] | 45 | // browser UI, or it may be hidden. |
[email protected] | 22401dc | 2014-03-21 01:38:57 | [diff] [blame] | 46 | // |
| 47 | // If you are adding code that only affects visible extension views (and not |
| 48 | // invisible background pages) you should add it to ExtensionViewHost. |
yoz | d61dfe19 | 2015-02-21 01:30:37 | [diff] [blame] | 49 | class ExtensionHost : public DeferredStartRenderHost, |
| 50 | public content::WebContentsDelegate, |
[email protected] | d8c66043 | 2011-12-22 20:51:25 | [diff] [blame] | 51 | public content::WebContentsObserver, |
[email protected] | e95ad33 | 2009-08-03 19:44:25 | [diff] [blame] | 52 | public ExtensionFunctionDispatcher::Delegate, |
limasdf | bbaa913d | 2015-03-02 23:52:33 | [diff] [blame] | 53 | public ExtensionRegistryObserver { |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 54 | public: |
[email protected] | 3a1dc57 | 2012-07-31 22:25:13 | [diff] [blame] | 55 | ExtensionHost(const Extension* extension, |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 56 | content::SiteInstance* site_instance, |
[email protected] | cb2edf2 | 2013-04-01 20:25:23 | [diff] [blame] | 57 | const GURL& url, ViewType host_type); |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 58 | ~ExtensionHost() override; |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 59 | |
[email protected] | 3a1dc57 | 2012-07-31 22:25:13 | [diff] [blame] | 60 | const Extension* extension() const { return extension_; } |
[email protected] | 2d2f6cfc | 2011-05-06 21:09:33 | [diff] [blame] | 61 | const std::string& extension_id() const { return extension_id_; } |
[email protected] | 3a7d7d3 | 2012-01-05 17:44:01 | [diff] [blame] | 62 | content::WebContents* host_contents() const { return host_contents_.get(); } |
[email protected] | eaabba2 | 2012-03-07 15:02:11 | [diff] [blame] | 63 | content::RenderViewHost* render_view_host() const; |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 64 | content::RenderProcessHost* render_process_host() const; |
kalman | fd474fa | 2015-03-16 22:30:57 | [diff] [blame] | 65 | bool has_loaded_once() const { return has_loaded_once_; } |
rdevlin.cronin | b48a98e | 2015-05-01 00:00:28 | [diff] [blame] | 66 | const GURL& initial_url() const { return initial_url_; } |
[email protected] | e95ad33 | 2009-08-03 19:44:25 | [diff] [blame] | 67 | bool document_element_available() const { |
| 68 | return document_element_available_; |
| 69 | } |
[email protected] | 01f829a | 2010-03-17 18:20:31 | [diff] [blame] | 70 | |
[email protected] | 96e6a103 | 2013-11-28 06:58:03 | [diff] [blame] | 71 | content::BrowserContext* browser_context() { return browser_context_; } |
[email protected] | 9fe4204 | 2013-10-29 21:13:33 | [diff] [blame] | 72 | |
[email protected] | cb2edf2 | 2013-04-01 20:25:23 | [diff] [blame] | 73 | ViewType extension_host_type() const { return extension_host_type_; } |
[email protected] | 952a68e | 2011-11-17 00:36:10 | [diff] [blame] | 74 | const GURL& GetURL() const; |
[email protected] | f8e55e7 | 2010-02-25 06:13:43 | [diff] [blame] | 75 | |
[email protected] | 7c6877d | 2009-06-19 13:56:25 | [diff] [blame] | 76 | // Returns true if the render view is initialized and didn't crash. |
| 77 | bool IsRenderViewLive() const; |
| 78 | |
[email protected] | 84455000 | 2009-11-20 01:06:00 | [diff] [blame] | 79 | // Prepares to initializes our RenderViewHost by creating its RenderView and |
| 80 | // navigating to this host's url. Uses host_view for the RenderViewHost's view |
| 81 | // (can be NULL). This happens delayed to avoid locking the UI. |
[email protected] | 952a68e | 2011-11-17 00:36:10 | [diff] [blame] | 82 | void CreateRenderViewSoon(); |
[email protected] | bbc94554 | 2009-07-26 00:11:42 | [diff] [blame] | 83 | |
rdevlin.cronin | 42efb7dd | 2015-02-11 17:50:52 | [diff] [blame] | 84 | // Closes this host (results in [possibly asynchronous] deletion). |
| 85 | void Close(); |
| 86 | |
chirantan | 79788f6 | 2015-02-02 23:57:25 | [diff] [blame] | 87 | // Typical observer interface. |
| 88 | void AddObserver(ExtensionHostObserver* observer); |
| 89 | void RemoveObserver(ExtensionHostObserver* observer); |
| 90 | |
chirantan | 669993c | 2015-03-05 23:38:33 | [diff] [blame] | 91 | // Called when an event is dispatched to the event page associated with this |
| 92 | // ExtensionHost. |
| 93 | void OnBackgroundEventDispatched(const std::string& event_name, int event_id); |
chirantan | 79788f6 | 2015-02-02 23:57:25 | [diff] [blame] | 94 | |
| 95 | // Called by the ProcessManager when a network request is started by the |
| 96 | // extension corresponding to this ExtensionHost. |
avi | c9cec10 | 2015-12-23 00:39:26 | [diff] [blame] | 97 | void OnNetworkRequestStarted(uint64_t request_id); |
chirantan | 79788f6 | 2015-02-02 23:57:25 | [diff] [blame] | 98 | |
| 99 | // Called by the ProcessManager when a previously started network request is |
| 100 | // finished. |
avi | c9cec10 | 2015-12-23 00:39:26 | [diff] [blame] | 101 | void OnNetworkRequestDone(uint64_t request_id); |
chirantan | 79788f6 | 2015-02-02 23:57:25 | [diff] [blame] | 102 | |
yoz | d61dfe19 | 2015-02-21 01:30:37 | [diff] [blame] | 103 | // content::WebContentsObserver: |
rdevlin.cronin | 6f42c252 | 2015-06-19 18:58:51 | [diff] [blame] | 104 | bool OnMessageReceived(const IPC::Message& message, |
| 105 | content::RenderFrameHost* host) override; |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 106 | void RenderViewCreated(content::RenderViewHost* render_view_host) override; |
| 107 | void RenderViewDeleted(content::RenderViewHost* render_view_host) override; |
| 108 | void RenderViewReady() override; |
| 109 | void RenderProcessGone(base::TerminationStatus status) override; |
| 110 | void DocumentAvailableInMainFrame() override; |
fdegans | 6ce28f5 | 2015-03-19 12:52:22 | [diff] [blame] | 111 | void DidStartLoading() override; |
| 112 | void DidStopLoading() override; |
[email protected] | 952a68e | 2011-11-17 00:36:10 | [diff] [blame] | 113 | |
yoz | d61dfe19 | 2015-02-21 01:30:37 | [diff] [blame] | 114 | // content::WebContentsDelegate: |
mathiash | 72a5e46 | 2014-11-19 08:18:50 | [diff] [blame] | 115 | content::JavaScriptDialogManager* GetJavaScriptDialogManager( |
| 116 | content::WebContents* source) override; |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 117 | void AddNewContents(content::WebContents* source, |
| 118 | content::WebContents* new_contents, |
| 119 | WindowOpenDisposition disposition, |
bokan | 107a47f | 2015-02-03 23:23:39 | [diff] [blame] | 120 | const gfx::Rect& initial_rect, |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 121 | bool user_gesture, |
| 122 | bool* was_blocked) override; |
| 123 | void CloseContents(content::WebContents* contents) override; |
| 124 | void RequestMediaAccessPermission( |
[email protected] | 0b9383a | 2012-10-26 00:58:16 | [diff] [blame] | 125 | content::WebContents* web_contents, |
[email protected] | 33662e5 | 2013-01-07 21:31:09 | [diff] [blame] | 126 | const content::MediaStreamRequest& request, |
mostynb | 0eac4e1b | 2014-10-03 16:32:19 | [diff] [blame] | 127 | const content::MediaResponseCallback& callback) override; |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 128 | bool CheckMediaAccessPermission(content::WebContents* web_contents, |
| 129 | const GURL& security_origin, |
| 130 | content::MediaStreamType type) override; |
| 131 | bool IsNeverVisible(content::WebContents* web_contents) override; |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 132 | |
limasdf | bbaa913d | 2015-03-02 23:52:33 | [diff] [blame] | 133 | // ExtensionRegistryObserver: |
rockot | 494f007 | 2015-07-29 17:58:07 | [diff] [blame] | 134 | void OnExtensionReady(content::BrowserContext* browser_context, |
| 135 | const Extension* extension) override; |
limasdf | bbaa913d | 2015-03-02 23:52:33 | [diff] [blame] | 136 | void OnExtensionUnloaded(content::BrowserContext* browser_context, |
| 137 | const Extension* extension, |
| 138 | UnloadedExtensionInfo::Reason reason) override; |
[email protected] | e95ad33 | 2009-08-03 19:44:25 | [diff] [blame] | 139 | |
[email protected] | 45a73d5 | 2013-11-26 00:10:55 | [diff] [blame] | 140 | protected: |
kalman | fd474fa | 2015-03-16 22:30:57 | [diff] [blame] | 141 | // Called each time this ExtensionHost completes a load finishes loading, |
| 142 | // before any stop-loading notifications or observer methods are called. |
| 143 | virtual void OnDidStopFirstLoad(); |
[email protected] | 45a73d5 | 2013-11-26 00:10:55 | [diff] [blame] | 144 | |
[email protected] | 91abb92 | 2013-12-03 18:39:56 | [diff] [blame] | 145 | // Navigates to the initial page. |
| 146 | virtual void LoadInitialURL(); |
| 147 | |
[email protected] | 45a73d5 | 2013-11-26 00:10:55 | [diff] [blame] | 148 | // Returns true if we're hosting a background page. |
| 149 | virtual bool IsBackgroundPage() const; |
| 150 | |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 151 | private: |
yoz | d61dfe19 | 2015-02-21 01:30:37 | [diff] [blame] | 152 | // DeferredStartRenderHost: |
| 153 | void CreateRenderViewNow() override; |
kalman | d49594b5 | 2015-02-27 17:27:16 | [diff] [blame] | 154 | void AddDeferredStartRenderHostObserver( |
| 155 | DeferredStartRenderHostObserver* observer) override; |
| 156 | void RemoveDeferredStartRenderHostObserver( |
| 157 | DeferredStartRenderHostObserver* observer) override; |
yoz | d61dfe19 | 2015-02-21 01:30:37 | [diff] [blame] | 158 | |
[email protected] | 34f128d | 2011-01-25 19:07:44 | [diff] [blame] | 159 | // Message handlers. |
chirantan | 669993c | 2015-03-05 23:38:33 | [diff] [blame] | 160 | void OnEventAck(int event_id); |
[email protected] | 7042b68 | 2012-04-19 22:57:51 | [diff] [blame] | 161 | void OnIncrementLazyKeepaliveCount(); |
| 162 | void OnDecrementLazyKeepaliveCount(); |
[email protected] | 34f128d | 2011-01-25 19:07:44 | [diff] [blame] | 163 | |
kalman | fd474fa | 2015-03-16 22:30:57 | [diff] [blame] | 164 | // Records UMA for load events. |
| 165 | void RecordStopLoadingUMA(); |
| 166 | |
[email protected] | 6c0f179f | 2014-03-19 11:04:41 | [diff] [blame] | 167 | // Delegate for functionality that cannot exist in the extensions module. |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame^] | 168 | std::unique_ptr<ExtensionHostDelegate> delegate_; |
[email protected] | 6c0f179f | 2014-03-19 11:04:41 | [diff] [blame] | 169 | |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 170 | // The extension that we're hosting in this view. |
[email protected] | 3a1dc57 | 2012-07-31 22:25:13 | [diff] [blame] | 171 | const Extension* extension_; |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 172 | |
[email protected] | 2d2f6cfc | 2011-05-06 21:09:33 | [diff] [blame] | 173 | // Id of extension that we're hosting in this view. |
| 174 | const std::string extension_id_; |
| 175 | |
[email protected] | 96e6a103 | 2013-11-28 06:58:03 | [diff] [blame] | 176 | // The browser context that this host is tied to. |
| 177 | content::BrowserContext* browser_context_; |
[email protected] | 8a17bd5 | 2009-06-06 08:19:49 | [diff] [blame] | 178 | |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 179 | // The host for our HTML content. |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame^] | 180 | std::unique_ptr<content::WebContents> host_contents_; |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 181 | |
[email protected] | 4f4d42a | 2011-12-02 02:42:49 | [diff] [blame] | 182 | // A weak pointer to the current or pending RenderViewHost. We don't access |
| 183 | // this through the host_contents because we want to deal with the pending |
| 184 | // host, so we can send messages to it before it finishes loading. |
[email protected] | eaabba2 | 2012-03-07 15:02:11 | [diff] [blame] | 185 | content::RenderViewHost* render_view_host_; |
[email protected] | 4f4d42a | 2011-12-02 02:42:49 | [diff] [blame] | 186 | |
rockot | 494f007 | 2015-07-29 17:58:07 | [diff] [blame] | 187 | // Whether CreateRenderViewNow was called before the extension was ready. |
| 188 | bool is_render_view_creation_pending_; |
| 189 | |
kalman | fd474fa | 2015-03-16 22:30:57 | [diff] [blame] | 190 | // Whether the ExtensionHost has finished loading some content at least once. |
| 191 | // There may be subsequent loads - such as reloads and navigations - and this |
| 192 | // will not affect its value (it will remain true). |
| 193 | bool has_loaded_once_; |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 194 | |
[email protected] | e95ad33 | 2009-08-03 19:44:25 | [diff] [blame] | 195 | // True if the main frame has finished parsing. |
| 196 | bool document_element_available_; |
| 197 | |
[email protected] | 952a68e | 2011-11-17 00:36:10 | [diff] [blame] | 198 | // The original URL of the page being hosted. |
| 199 | GURL initial_url_; |
[email protected] | e916901c | 2009-05-07 00:14:31 | [diff] [blame] | 200 | |
chirantan | 79788f6 | 2015-02-02 23:57:25 | [diff] [blame] | 201 | // Messages sent out to the renderer that have not been acknowledged yet. |
| 202 | std::set<int> unacked_messages_; |
| 203 | |
[email protected] | 720ad131 | 2012-02-27 23:07:36 | [diff] [blame] | 204 | // The type of view being hosted. |
[email protected] | cb2edf2 | 2013-04-01 20:25:23 | [diff] [blame] | 205 | ViewType extension_host_type_; |
[email protected] | 7b291f9 | 2009-08-14 05:43:53 | [diff] [blame] | 206 | |
kalman | 0f101ac | 2015-05-06 01:23:17 | [diff] [blame] | 207 | // Measures how long since the ExtensionHost object was created. This can be |
| 208 | // used to measure the responsiveness of UI. For example, it's important to |
| 209 | // keep this as low as possible for popups. Contrast this to |load_start_|, |
| 210 | // for which a low value does not necessarily mean a responsive UI, as |
| 211 | // ExtensionHosts may sit in an ExtensionHostQueue for a long time. |
| 212 | base::ElapsedTimer create_start_; |
| 213 | |
| 214 | // Measures how long since the initial URL started loading. This timer is |
| 215 | // started only once the ExtensionHost has exited the ExtensionHostQueue. |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame^] | 216 | std::unique_ptr<base::ElapsedTimer> load_start_; |
[email protected] | cc2c343 | 2009-11-06 17:24:36 | [diff] [blame] | 217 | |
brettw | 236d317 | 2015-06-03 16:31:43 | [diff] [blame] | 218 | base::ObserverList<ExtensionHostObserver> observer_list_; |
| 219 | base::ObserverList<DeferredStartRenderHostObserver> |
kalman | d49594b5 | 2015-02-27 17:27:16 | [diff] [blame] | 220 | deferred_start_render_host_observer_list_; |
chirantan | 79788f6 | 2015-02-02 23:57:25 | [diff] [blame] | 221 | |
[email protected] | c6463165 | 2009-04-29 22:24:31 | [diff] [blame] | 222 | DISALLOW_COPY_AND_ASSIGN(ExtensionHost); |
| 223 | }; |
| 224 | |
[email protected] | 3a1dc57 | 2012-07-31 22:25:13 | [diff] [blame] | 225 | } // namespace extensions |
| 226 | |
[email protected] | 22401dc | 2014-03-21 01:38:57 | [diff] [blame] | 227 | #endif // EXTENSIONS_BROWSER_EXTENSION_HOST_H_ |