blob: 093fb25589a96e6dc0cb9b15a03237c89531e0d4 [file] [log] [blame]
[email protected]22401dc2014-03-21 01:38:571// Copyright 2014 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
[email protected]22401dc2014-03-21 01:38:575#ifndef EXTENSIONS_BROWSER_EXTENSION_HOST_H_
6#define EXTENSIONS_BROWSER_EXTENSION_HOST_H_
[email protected]c64631652009-04-29 22:24:317
avic9cec102015-12-23 00:39:268#include <stdint.h>
9
dchengf5d241082016-04-21 03:43:1110#include <memory>
[email protected]caf706f2010-10-26 17:54:0811#include <string>
David Bertoni3e1e9fa2018-08-29 20:39:3012#include <unordered_map>
[email protected]8a17bd52009-06-06 08:19:4913
[email protected]a808bc52012-02-14 03:20:3714#include "base/logging.h"
avic9cec102015-12-23 00:39:2615#include "base/macros.h"
chirantan79788f62015-02-02 23:57:2516#include "base/observer_list.h"
[email protected]bd5a3742013-09-29 18:06:1317#include "base/timer/elapsed_timer.h"
Guido Urdaneta73fa6632019-01-14 18:46:2618#include "content/public/browser/media_stream_request.h"
[email protected]674bc592011-12-20 23:00:4219#include "content/public/browser/web_contents_delegate.h"
[email protected]d8c660432011-12-22 20:51:2520#include "content/public/browser/web_contents_observer.h"
yozd61dfe192015-02-21 01:30:3721#include "extensions/browser/deferred_start_render_host.h"
[email protected]0b9de032014-03-15 05:47:0122#include "extensions/browser/extension_function_dispatcher.h"
limasdfbbaa913d2015-03-02 23:52:3323#include "extensions/browser/extension_registry_observer.h"
[email protected]88b50b62013-09-01 23:05:0624#include "extensions/common/stack_frame.h"
[email protected]cb2edf22013-04-01 20:25:2325#include "extensions/common/view_type.h"
[email protected]5de634712011-03-02 00:20:1926
[email protected]f3b1a082011-11-18 00:34:3027namespace content {
[email protected]9fe42042013-10-29 21:13:3328class BrowserContext;
[email protected]f3b1a082011-11-18 00:34:3029class RenderProcessHost;
[email protected]b6583592012-01-25 19:52:3330class SiteInstance;
[email protected]f3b1a082011-11-18 00:34:3031}
32
[email protected]1c321ee52012-05-21 03:02:3433namespace extensions {
34class Extension;
[email protected]6c0f179f2014-03-19 11:04:4135class ExtensionHostDelegate;
chirantan79788f62015-02-02 23:57:2536class ExtensionHostObserver;
kalmanfcece452015-02-18 18:20:4237class ExtensionHostQueue;
[email protected]1c321ee52012-05-21 03:02:3438
[email protected]c64631652009-04-29 22:24:3139// This class is the browser component of an extension component's RenderView.
40// It handles setting up the renderer process, if needed, with special
41// privileges available to extensions. It may have a view to be shown in the
[email protected]2d2f6cfc2011-05-06 21:09:3342// browser UI, or it may be hidden.
[email protected]22401dc2014-03-21 01:38:5743//
44// If you are adding code that only affects visible extension views (and not
45// invisible background pages) you should add it to ExtensionViewHost.
yozd61dfe192015-02-21 01:30:3746class ExtensionHost : public DeferredStartRenderHost,
47 public content::WebContentsDelegate,
[email protected]d8c660432011-12-22 20:51:2548 public content::WebContentsObserver,
[email protected]e95ad332009-08-03 19:44:2549 public ExtensionFunctionDispatcher::Delegate,
limasdfbbaa913d2015-03-02 23:52:3350 public ExtensionRegistryObserver {
[email protected]c64631652009-04-29 22:24:3151 public:
[email protected]3a1dc572012-07-31 22:25:1352 ExtensionHost(const Extension* extension,
[email protected]b6583592012-01-25 19:52:3353 content::SiteInstance* site_instance,
[email protected]cb2edf22013-04-01 20:25:2354 const GURL& url, ViewType host_type);
dcheng9168b2f2014-10-21 12:38:2455 ~ExtensionHost() override;
[email protected]c64631652009-04-29 22:24:3156
Karan Bhatiaee1e6a52017-11-09 21:32:5157 // This may be null if the extension has been or is being unloaded.
[email protected]3a1dc572012-07-31 22:25:1358 const Extension* extension() const { return extension_; }
Karan Bhatiaee1e6a52017-11-09 21:32:5159
[email protected]2d2f6cfc2011-05-06 21:09:3360 const std::string& extension_id() const { return extension_id_; }
[email protected]3a7d7d32012-01-05 17:44:0161 content::WebContents* host_contents() const { return host_contents_.get(); }
[email protected]eaabba22012-03-07 15:02:1162 content::RenderViewHost* render_view_host() const;
[email protected]f3b1a082011-11-18 00:34:3063 content::RenderProcessHost* render_process_host() const;
kalmanfd474fa2015-03-16 22:30:5764 bool has_loaded_once() const { return has_loaded_once_; }
rdevlin.croninb48a98e2015-05-01 00:00:2865 const GURL& initial_url() const { return initial_url_; }
[email protected]e95ad332009-08-03 19:44:2566 bool document_element_available() const {
67 return document_element_available_;
68 }
[email protected]01f829a2010-03-17 18:20:3169
[email protected]96e6a1032013-11-28 06:58:0370 content::BrowserContext* browser_context() { return browser_context_; }
[email protected]9fe42042013-10-29 21:13:3371
[email protected]cb2edf22013-04-01 20:25:2372 ViewType extension_host_type() const { return extension_host_type_; }
[email protected]952a68e2011-11-17 00:36:1073 const GURL& GetURL() const;
[email protected]f8e55e72010-02-25 06:13:4374
[email protected]7c6877d2009-06-19 13:56:2575 // Returns true if the render view is initialized and didn't crash.
76 bool IsRenderViewLive() const;
77
[email protected]844550002009-11-20 01:06:0078 // Prepares to initializes our RenderViewHost by creating its RenderView and
79 // navigating to this host's url. Uses host_view for the RenderViewHost's view
80 // (can be NULL). This happens delayed to avoid locking the UI.
[email protected]952a68e2011-11-17 00:36:1081 void CreateRenderViewSoon();
[email protected]bbc945542009-07-26 00:11:4282
rdevlin.cronin42efb7dd2015-02-11 17:50:5283 // Closes this host (results in [possibly asynchronous] deletion).
84 void Close();
85
chirantan79788f62015-02-02 23:57:2586 // Typical observer interface.
87 void AddObserver(ExtensionHostObserver* observer);
88 void RemoveObserver(ExtensionHostObserver* observer);
89
chirantan669993c2015-03-05 23:38:3390 // Called when an event is dispatched to the event page associated with this
91 // ExtensionHost.
92 void OnBackgroundEventDispatched(const std::string& event_name, int event_id);
chirantan79788f62015-02-02 23:57:2593
94 // Called by the ProcessManager when a network request is started by the
95 // extension corresponding to this ExtensionHost.
avic9cec102015-12-23 00:39:2696 void OnNetworkRequestStarted(uint64_t request_id);
chirantan79788f62015-02-02 23:57:2597
98 // Called by the ProcessManager when a previously started network request is
99 // finished.
avic9cec102015-12-23 00:39:26100 void OnNetworkRequestDone(uint64_t request_id);
chirantan79788f62015-02-02 23:57:25101
yozd61dfe192015-02-21 01:30:37102 // content::WebContentsObserver:
rdevlin.cronin6f42c2522015-06-19 18:58:51103 bool OnMessageReceived(const IPC::Message& message,
104 content::RenderFrameHost* host) override;
dcheng9168b2f2014-10-21 12:38:24105 void RenderViewCreated(content::RenderViewHost* render_view_host) override;
106 void RenderViewDeleted(content::RenderViewHost* render_view_host) override;
107 void RenderViewReady() override;
108 void RenderProcessGone(base::TerminationStatus status) override;
109 void DocumentAvailableInMainFrame() override;
fdegans6ce28f52015-03-19 12:52:22110 void DidStopLoading() override;
[email protected]952a68e2011-11-17 00:36:10111
yozd61dfe192015-02-21 01:30:37112 // content::WebContentsDelegate:
mathiash72a5e462014-11-19 08:18:50113 content::JavaScriptDialogManager* GetJavaScriptDialogManager(
114 content::WebContents* source) override;
dcheng9168b2f2014-10-21 12:38:24115 void AddNewContents(content::WebContents* source,
erikchenbee5c9622018-04-27 19:30:25116 std::unique_ptr<content::WebContents> new_contents,
dcheng9168b2f2014-10-21 12:38:24117 WindowOpenDisposition disposition,
bokan107a47f2015-02-03 23:23:39118 const gfx::Rect& initial_rect,
dcheng9168b2f2014-10-21 12:38:24119 bool user_gesture,
120 bool* was_blocked) override;
121 void CloseContents(content::WebContents* contents) override;
122 void RequestMediaAccessPermission(
[email protected]0b9383a2012-10-26 00:58:16123 content::WebContents* web_contents,
[email protected]33662e52013-01-07 21:31:09124 const content::MediaStreamRequest& request,
Mark Pilgrim57499082018-06-12 12:38:30125 content::MediaResponseCallback callback) override;
Raymes Khouryad7c24a12018-03-05 23:22:58126 bool CheckMediaAccessPermission(content::RenderFrameHost* render_frame_host,
dcheng9168b2f2014-10-21 12:38:24127 const GURL& security_origin,
Antonio Gomesc8b734b2019-06-05 18:22:16128 blink::mojom::MediaStreamType type) override;
danakj77eb7e82020-01-09 19:38:46129 bool IsNeverComposited(content::WebContents* web_contents) override;
Becca Hughes112832e2019-06-11 17:19:02130 content::PictureInPictureResult EnterPictureInPicture(
131 content::WebContents* web_contents,
132 const viz::SurfaceId& surface_id,
133 const gfx::Size& natural_size) override;
François Beaufort48cf2722018-08-28 09:33:52134 void ExitPictureInPicture() override;
[email protected]c64631652009-04-29 22:24:31135
limasdfbbaa913d2015-03-02 23:52:33136 // ExtensionRegistryObserver:
rockot494f0072015-07-29 17:58:07137 void OnExtensionReady(content::BrowserContext* browser_context,
138 const Extension* extension) override;
limasdfbbaa913d2015-03-02 23:52:33139 void OnExtensionUnloaded(content::BrowserContext* browser_context,
140 const Extension* extension,
limasdf0deef2042017-05-03 19:17:17141 UnloadedExtensionReason reason) override;
[email protected]e95ad332009-08-03 19:44:25142
[email protected]45a73d52013-11-26 00:10:55143 protected:
kalmanfd474fa2015-03-16 22:30:57144 // Called each time this ExtensionHost completes a load finishes loading,
145 // before any stop-loading notifications or observer methods are called.
146 virtual void OnDidStopFirstLoad();
[email protected]45a73d52013-11-26 00:10:55147
[email protected]91abb922013-12-03 18:39:56148 // Navigates to the initial page.
149 virtual void LoadInitialURL();
150
[email protected]45a73d52013-11-26 00:10:55151 // Returns true if we're hosting a background page.
152 virtual bool IsBackgroundPage() const;
153
[email protected]c64631652009-04-29 22:24:31154 private:
yozd61dfe192015-02-21 01:30:37155 // DeferredStartRenderHost:
156 void CreateRenderViewNow() override;
yozd61dfe192015-02-21 01:30:37157
[email protected]34f128d2011-01-25 19:07:44158 // Message handlers.
chirantan669993c2015-03-05 23:38:33159 void OnEventAck(int event_id);
[email protected]7042b682012-04-19 22:57:51160 void OnIncrementLazyKeepaliveCount();
161 void OnDecrementLazyKeepaliveCount();
[email protected]34f128d2011-01-25 19:07:44162
kalmanfd474fa2015-03-16 22:30:57163 // Records UMA for load events.
164 void RecordStopLoadingUMA();
165
[email protected]6c0f179f2014-03-19 11:04:41166 // Delegate for functionality that cannot exist in the extensions module.
dchengf5d241082016-04-21 03:43:11167 std::unique_ptr<ExtensionHostDelegate> delegate_;
[email protected]6c0f179f2014-03-19 11:04:41168
[email protected]c64631652009-04-29 22:24:31169 // The extension that we're hosting in this view.
[email protected]3a1dc572012-07-31 22:25:13170 const Extension* extension_;
[email protected]c64631652009-04-29 22:24:31171
[email protected]2d2f6cfc2011-05-06 21:09:33172 // Id of extension that we're hosting in this view.
173 const std::string extension_id_;
174
[email protected]96e6a1032013-11-28 06:58:03175 // The browser context that this host is tied to.
176 content::BrowserContext* browser_context_;
[email protected]8a17bd52009-06-06 08:19:49177
[email protected]c64631652009-04-29 22:24:31178 // The host for our HTML content.
dchengf5d241082016-04-21 03:43:11179 std::unique_ptr<content::WebContents> host_contents_;
[email protected]c64631652009-04-29 22:24:31180
[email protected]4f4d42a2011-12-02 02:42:49181 // A weak pointer to the current or pending RenderViewHost. We don't access
182 // this through the host_contents because we want to deal with the pending
183 // host, so we can send messages to it before it finishes loading.
[email protected]eaabba22012-03-07 15:02:11184 content::RenderViewHost* render_view_host_;
[email protected]4f4d42a2011-12-02 02:42:49185
rockot494f0072015-07-29 17:58:07186 // Whether CreateRenderViewNow was called before the extension was ready.
187 bool is_render_view_creation_pending_;
188
kalmanfd474fa2015-03-16 22:30:57189 // Whether the ExtensionHost has finished loading some content at least once.
190 // There may be subsequent loads - such as reloads and navigations - and this
191 // will not affect its value (it will remain true).
192 bool has_loaded_once_;
[email protected]c64631652009-04-29 22:24:31193
[email protected]e95ad332009-08-03 19:44:25194 // True if the main frame has finished parsing.
195 bool document_element_available_;
196
[email protected]952a68e2011-11-17 00:36:10197 // The original URL of the page being hosted.
198 GURL initial_url_;
[email protected]e916901c2009-05-07 00:14:31199
chirantan79788f62015-02-02 23:57:25200 // Messages sent out to the renderer that have not been acknowledged yet.
David Bertoni3e1e9fa2018-08-29 20:39:30201 // Maps event ID to event name.
202 std::unordered_map<int, std::string> unacked_messages_;
chirantan79788f62015-02-02 23:57:25203
[email protected]720ad1312012-02-27 23:07:36204 // The type of view being hosted.
[email protected]cb2edf22013-04-01 20:25:23205 ViewType extension_host_type_;
[email protected]7b291f92009-08-14 05:43:53206
kalman0f101ac2015-05-06 01:23:17207 // 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.
dchengf5d241082016-04-21 03:43:11216 std::unique_ptr<base::ElapsedTimer> load_start_;
[email protected]cc2c3432009-11-06 17:24:36217
Trent Apteda250ec3ab2018-08-19 08:52:19218 base::ObserverList<ExtensionHostObserver>::Unchecked observer_list_;
chirantan79788f62015-02-02 23:57:25219
[email protected]c64631652009-04-29 22:24:31220 DISALLOW_COPY_AND_ASSIGN(ExtensionHost);
221};
222
[email protected]3a1dc572012-07-31 22:25:13223} // namespace extensions
224
[email protected]22401dc2014-03-21 01:38:57225#endif // EXTENSIONS_BROWSER_EXTENSION_HOST_H_