blob: 5b86dc08eba0b687f6c25acc39563af6dfce647a [file] [log] [blame]
[email protected]1ce15972014-03-20 19:25:481// 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.cronincb2ec659a2015-06-10 23:32:4113#include "extensions/browser/extension_function_dispatcher.h"
[email protected]1ce15972014-03-20 19:25:4814
15namespace content {
16class BrowserContext;
17class RenderViewHost;
18class WebContents;
19}
20
21namespace extensions {
22class 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.
robcdcc4b82015-12-06 12:39:4532//
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.cronincb2ec659a2015-06-10 23:32:4149class 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]1ce15972014-03-20 19:25:4859 protected:
60 explicit ExtensionWebContentsObserver(content::WebContents* web_contents);
dcheng9168b2f2014-10-21 12:38:2461 ~ExtensionWebContentsObserver() override;
[email protected]1ce15972014-03-20 19:25:4862
63 content::BrowserContext* browser_context() { return browser_context_; }
64
rdevlin.cronin6f42c2522015-06-19 18:58:5165 // 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.cronincb2ec659a2015-06-10 23:32:4170 // ExtensionFunctionDispatcher::Delegate overrides.
71 content::WebContents* GetAssociatedWebContents() const override;
72
[email protected]1ce15972014-03-20 19:25:4873 // content::WebContentsObserver overrides.
74
75 // A subclass should invoke this method to finish extension process setup.
dcheng9168b2f2014-10-21 12:38:2476 void RenderViewCreated(content::RenderViewHost* render_view_host) override;
[email protected]1ce15972014-03-20 19:25:4877
sammc143f3c52015-02-13 09:42:3878 void RenderFrameCreated(content::RenderFrameHost* render_frame_host) override;
rdevlin.cronin6f42c2522015-06-19 18:58:5179 void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override;
robcdcc4b82015-12-06 12:39:4580 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;
sammc143f3c52015-02-13 09:42:3887
rdevlin.cronincb2ec659a2015-06-10 23:32:4188 // Subclasses should call this first before doing their own message handling.
rdevlin.cronin92503ba2015-06-12 17:00:5689 bool OnMessageReceived(const IPC::Message& message,
90 content::RenderFrameHost* render_frame_host) override;
rdevlin.cronincb2ec659a2015-06-10 23:32:4191
emaxxe70f5e12015-05-29 11:26:0092 // 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.cronin86f5b702015-06-24 18:49:1797 // 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.
robcdcc4b82015-12-06 12:39:45104 // 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.cronin86f5b702015-06-24 18:49:17107 const Extension* GetExtensionFromFrame(
robcdcc4b82015-12-06 12:39:45108 content::RenderFrameHost* render_frame_host,
109 bool verify_url) const;
rdevlin.cronin86f5b702015-06-24 18:49:17110
111 // TODO(devlin): Remove these once callers are updated to use the FromFrame
112 // equivalents.
[email protected]1ce15972014-03-20 19:25:48113 // 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]1ce15972014-03-20 19:25:48116 // 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.cronin92503ba2015-06-12 17:00:56121 void OnRequest(content::RenderFrameHost* render_frame_host,
122 const ExtensionHostMsg_Request_Params& params);
rdevlin.cronincb2ec659a2015-06-10 23:32:41123
rdevlin.cronin6f42c2522015-06-19 18:58:51124 // 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]1ce15972014-03-20 19:25:48128 // The BrowserContext associated with the WebContents being observed.
129 content::BrowserContext* browser_context_;
130
rdevlin.cronincb2ec659a2015-06-10 23:32:41131 ExtensionFunctionDispatcher dispatcher_;
132
[email protected]1ce15972014-03-20 19:25:48133 DISALLOW_COPY_AND_ASSIGN(ExtensionWebContentsObserver);
134};
135
136} // namespace extensions
137
138#endif // EXTENSIONS_BROWSER_EXTENSION_WEB_CONTENTS_OBSERVER_H_