[email protected] | 1ce1597 | 2014-03-20 19:25:48 | [diff] [blame] | 1 | // Copyright 2014 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 EXTENSIONS_BROWSER_EXTENSION_WEB_CONTENTS_OBSERVER_H_ |
| 6 | #define EXTENSIONS_BROWSER_EXTENSION_WEB_CONTENTS_OBSERVER_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | |
| 10 | #include "base/compiler_specific.h" |
| 11 | #include "base/macros.h" |
| 12 | #include "content/public/browser/web_contents_observer.h" |
rdevlin.cronin | cb2ec659a | 2015-06-10 23:32:41 | [diff] [blame] | 13 | #include "extensions/browser/extension_function_dispatcher.h" |
[email protected] | 1ce1597 | 2014-03-20 19:25:48 | [diff] [blame] | 14 | |
| 15 | namespace content { |
| 16 | class BrowserContext; |
| 17 | class RenderViewHost; |
| 18 | class WebContents; |
| 19 | } |
| 20 | |
| 21 | namespace extensions { |
| 22 | class Extension; |
| 23 | |
| 24 | // A web contents observer used for renderer and extension processes. Grants the |
| 25 | // renderer access to certain URL scheme patterns for extensions and notifies |
| 26 | // the renderer that the extension was loaded. |
| 27 | // |
| 28 | // Extension system embedders must create an instance for every extension |
| 29 | // WebContents. It must be a subclass so that creating an instance via |
| 30 | // content::WebContentsUserData::CreateForWebContents() provides an object of |
| 31 | // the correct type. For an example, see ChromeExtensionWebContentsObserver. |
rob | cdcc4b8 | 2015-12-06 12:39:45 | [diff] [blame] | 32 | // |
| 33 | // This class is responsible for maintaining the registrations of extension |
| 34 | // frames with the ProcessManager. Only frames in an extension process are |
| 35 | // registered. If out-of-process frames are enabled, every frame hosts a |
| 36 | // chrome-extension: page. Otherwise non-extension frames may erroneously be |
| 37 | // registered, but only briefly until they are correctly classified. This is |
| 38 | // achieved using the following notifications: |
| 39 | // 1. RenderFrameCreated - registers all new frames in extension processes. |
| 40 | // 2. DidCommitProvisionalLoadForFrame - unregisters non-extension frames. |
| 41 | // 3. DidNavigateAnyFrame - registers extension frames if they had been |
| 42 | // unregistered. |
| 43 | // |
| 44 | // Without OOPIF, non-extension frames created by the Chrome extension are also |
| 45 | // registered at RenderFrameCreated. When the non-extension page is committed, |
| 46 | // we detect that the unexpected URL and unregister the frame. |
| 47 | // With OOPIF only the first notification is sufficient in most cases, except |
| 48 | // for sandboxed frames with a unique origin. |
rdevlin.cronin | cb2ec659a | 2015-06-10 23:32:41 | [diff] [blame] | 49 | class ExtensionWebContentsObserver |
| 50 | : public content::WebContentsObserver, |
| 51 | public ExtensionFunctionDispatcher::Delegate { |
| 52 | public: |
| 53 | // Returns the ExtensionWebContentsObserver for the given |web_contents|. |
| 54 | static ExtensionWebContentsObserver* GetForWebContents( |
| 55 | content::WebContents* web_contents); |
| 56 | |
| 57 | ExtensionFunctionDispatcher* dispatcher() { return &dispatcher_; } |
| 58 | |
[email protected] | 1ce1597 | 2014-03-20 19:25:48 | [diff] [blame] | 59 | protected: |
| 60 | explicit ExtensionWebContentsObserver(content::WebContents* web_contents); |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 61 | ~ExtensionWebContentsObserver() override; |
[email protected] | 1ce1597 | 2014-03-20 19:25:48 | [diff] [blame] | 62 | |
| 63 | content::BrowserContext* browser_context() { return browser_context_; } |
| 64 | |
rdevlin.cronin | 6f42c252 | 2015-06-19 18:58:51 | [diff] [blame] | 65 | // Initializes a new render frame. Subclasses should invoke this |
| 66 | // implementation if extending. |
| 67 | virtual void InitializeRenderFrame( |
| 68 | content::RenderFrameHost* render_frame_host); |
| 69 | |
rdevlin.cronin | cb2ec659a | 2015-06-10 23:32:41 | [diff] [blame] | 70 | // ExtensionFunctionDispatcher::Delegate overrides. |
| 71 | content::WebContents* GetAssociatedWebContents() const override; |
| 72 | |
[email protected] | 1ce1597 | 2014-03-20 19:25:48 | [diff] [blame] | 73 | // content::WebContentsObserver overrides. |
| 74 | |
| 75 | // A subclass should invoke this method to finish extension process setup. |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 76 | void RenderViewCreated(content::RenderViewHost* render_view_host) override; |
[email protected] | 1ce1597 | 2014-03-20 19:25:48 | [diff] [blame] | 77 | |
sammc | 143f3c5 | 2015-02-13 09:42:38 | [diff] [blame] | 78 | void RenderFrameCreated(content::RenderFrameHost* render_frame_host) override; |
rdevlin.cronin | 6f42c252 | 2015-06-19 18:58:51 | [diff] [blame] | 79 | void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override; |
rob | cdcc4b8 | 2015-12-06 12:39:45 | [diff] [blame] | 80 | void DidCommitProvisionalLoadForFrame( |
| 81 | content::RenderFrameHost* render_frame_host, |
| 82 | const GURL& url, |
| 83 | ui::PageTransition transition_type) override; |
| 84 | void DidNavigateAnyFrame(content::RenderFrameHost* render_frame_host, |
| 85 | const content::LoadCommittedDetails& details, |
| 86 | const content::FrameNavigateParams& params) override; |
sammc | 143f3c5 | 2015-02-13 09:42:38 | [diff] [blame] | 87 | |
rdevlin.cronin | cb2ec659a | 2015-06-10 23:32:41 | [diff] [blame] | 88 | // Subclasses should call this first before doing their own message handling. |
rdevlin.cronin | 92503ba | 2015-06-12 17:00:56 | [diff] [blame] | 89 | bool OnMessageReceived(const IPC::Message& message, |
| 90 | content::RenderFrameHost* render_frame_host) override; |
rdevlin.cronin | cb2ec659a | 2015-06-10 23:32:41 | [diff] [blame] | 91 | |
emaxx | e70f5e1 | 2015-05-29 11:26:00 | [diff] [blame] | 92 | // Per the documentation in WebContentsObserver, these two methods are invoked |
| 93 | // when a Pepper plugin instance is attached/detached in the page DOM. |
| 94 | void PepperInstanceCreated() override; |
| 95 | void PepperInstanceDeleted() override; |
| 96 | |
rdevlin.cronin | 86f5b70 | 2015-06-24 18:49:17 | [diff] [blame] | 97 | // Returns the extension id associated with the given |render_frame_host|, or |
| 98 | // the empty string if there is none. |
| 99 | std::string GetExtensionIdFromFrame( |
| 100 | content::RenderFrameHost* render_frame_host) const; |
| 101 | |
| 102 | // Returns the extension associated with the given |render_frame_host|, or |
| 103 | // null if there is none. |
rob | cdcc4b8 | 2015-12-06 12:39:45 | [diff] [blame] | 104 | // If |verify_url| is false, only the SiteInstance is taken into account. |
| 105 | // If |verify_url| is true, the frame's last committed URL is also used to |
| 106 | // improve the classification of the frame. |
rdevlin.cronin | 86f5b70 | 2015-06-24 18:49:17 | [diff] [blame] | 107 | const Extension* GetExtensionFromFrame( |
rob | cdcc4b8 | 2015-12-06 12:39:45 | [diff] [blame] | 108 | content::RenderFrameHost* render_frame_host, |
| 109 | bool verify_url) const; |
rdevlin.cronin | 86f5b70 | 2015-06-24 18:49:17 | [diff] [blame] | 110 | |
| 111 | // TODO(devlin): Remove these once callers are updated to use the FromFrame |
| 112 | // equivalents. |
[email protected] | 1ce1597 | 2014-03-20 19:25:48 | [diff] [blame] | 113 | // Returns the extension or app associated with a render view host. Returns |
| 114 | // NULL if the render view host is not for a valid extension. |
| 115 | const Extension* GetExtension(content::RenderViewHost* render_view_host); |
[email protected] | 1ce1597 | 2014-03-20 19:25:48 | [diff] [blame] | 116 | // Returns the extension or app ID associated with a render view host. Returns |
| 117 | // the empty string if the render view host is not for a valid extension. |
| 118 | static std::string GetExtensionId(content::RenderViewHost* render_view_host); |
| 119 | |
| 120 | private: |
rdevlin.cronin | 92503ba | 2015-06-12 17:00:56 | [diff] [blame] | 121 | void OnRequest(content::RenderFrameHost* render_frame_host, |
| 122 | const ExtensionHostMsg_Request_Params& params); |
rdevlin.cronin | cb2ec659a | 2015-06-10 23:32:41 | [diff] [blame] | 123 | |
rdevlin.cronin | 6f42c252 | 2015-06-19 18:58:51 | [diff] [blame] | 124 | // A helper function for initializing render frames at the creation of the |
| 125 | // observer. |
| 126 | void InitializeFrameHelper(content::RenderFrameHost* render_frame_host); |
| 127 | |
[email protected] | 1ce1597 | 2014-03-20 19:25:48 | [diff] [blame] | 128 | // The BrowserContext associated with the WebContents being observed. |
| 129 | content::BrowserContext* browser_context_; |
| 130 | |
rdevlin.cronin | cb2ec659a | 2015-06-10 23:32:41 | [diff] [blame] | 131 | ExtensionFunctionDispatcher dispatcher_; |
| 132 | |
[email protected] | 1ce1597 | 2014-03-20 19:25:48 | [diff] [blame] | 133 | DISALLOW_COPY_AND_ASSIGN(ExtensionWebContentsObserver); |
| 134 | }; |
| 135 | |
| 136 | } // namespace extensions |
| 137 | |
| 138 | #endif // EXTENSIONS_BROWSER_EXTENSION_WEB_CONTENTS_OBSERVER_H_ |