blob: a36661b2c26e60aa434ac9aec093821734e41c19 [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#include "extensions/browser/extension_web_contents_observer.h"
6
7#include "content/public/browser/child_process_security_policy.h"
rdevlin.cronin6ae04a012015-04-03 20:19:408#include "content/public/browser/render_frame_host.h"
[email protected]1ce15972014-03-20 19:25:489#include "content/public/browser/render_process_host.h"
10#include "content/public/browser/render_view_host.h"
11#include "content/public/browser/site_instance.h"
12#include "content/public/browser/web_contents.h"
13#include "content/public/common/url_constants.h"
14#include "extensions/browser/extension_prefs.h"
15#include "extensions/browser/extension_registry.h"
kmarshall166e5b42015-04-03 22:29:4316#include "extensions/browser/extensions_browser_client.h"
sammc143f3c52015-02-13 09:42:3817#include "extensions/browser/mojo/service_registration.h"
rdevlin.cronin6ae04a012015-04-03 20:19:4018#include "extensions/browser/process_manager.h"
[email protected]1ce15972014-03-20 19:25:4819#include "extensions/browser/view_type_utils.h"
20#include "extensions/common/constants.h"
emaxxe70f5e12015-05-29 11:26:0021#include "extensions/common/extension.h"
[email protected]1ce15972014-03-20 19:25:4822#include "extensions/common/extension_messages.h"
23
24namespace extensions {
25
rdevlin.cronincb2ec659a2015-06-10 23:32:4126// static
27ExtensionWebContentsObserver* ExtensionWebContentsObserver::GetForWebContents(
28 content::WebContents* web_contents) {
29 return ExtensionsBrowserClient::Get()->GetExtensionWebContentsObserver(
30 web_contents);
31}
32
[email protected]1ce15972014-03-20 19:25:4833ExtensionWebContentsObserver::ExtensionWebContentsObserver(
34 content::WebContents* web_contents)
35 : content::WebContentsObserver(web_contents),
rdevlin.cronincb2ec659a2015-06-10 23:32:4136 browser_context_(web_contents->GetBrowserContext()),
37 dispatcher_(browser_context_) {
rdevlin.cronin6f42c2522015-06-19 18:58:5138 web_contents->ForEachFrame(
39 base::Bind(&ExtensionWebContentsObserver::InitializeFrameHelper,
40 base::Unretained(this)));
rdevlin.cronincb2ec659a2015-06-10 23:32:4141 dispatcher_.set_delegate(this);
[email protected]0b365072014-03-22 06:14:1842}
[email protected]1ce15972014-03-20 19:25:4843
rdevlin.cronin6ae04a012015-04-03 20:19:4044ExtensionWebContentsObserver::~ExtensionWebContentsObserver() {
45}
[email protected]1ce15972014-03-20 19:25:4846
rdevlin.cronin6f42c2522015-06-19 18:58:5147void ExtensionWebContentsObserver::InitializeRenderFrame(
48 content::RenderFrameHost* render_frame_host) {
49 DCHECK(render_frame_host);
50 DCHECK(render_frame_host->IsRenderFrameLive());
51
52 // Notify the render frame of the view type.
53 render_frame_host->Send(new ExtensionMsg_NotifyRenderViewType(
54 render_frame_host->GetRoutingID(), GetViewType(web_contents())));
55
rdevlin.cronin86f5b702015-06-24 18:49:1756 const Extension* frame_extension = GetExtensionFromFrame(render_frame_host);
rdevlin.cronin6f42c2522015-06-19 18:58:5157 if (frame_extension) {
58 ExtensionsBrowserClient::Get()->RegisterMojoServices(render_frame_host,
59 frame_extension);
60 ProcessManager::Get(browser_context_)
61 ->RegisterRenderFrameHost(web_contents(), render_frame_host,
62 frame_extension);
63 }
64
65 // This can be different from |frame_extension| above in the case of, e.g.,
66 // a non-extension iframe hosted in a chrome-extension:// page.
67 const Extension* main_frame_extension =
rdevlin.cronin86f5b702015-06-24 18:49:1768 GetExtensionFromFrame(web_contents()->GetMainFrame());
rdevlin.cronin6f42c2522015-06-19 18:58:5169 // We notify the render frame that it's in an extension's tab, but not if this
70 // is a hosted app (we don't mind scripting on hosted apps' pages).
71 if (main_frame_extension && !main_frame_extension->is_hosted_app()) {
72 render_frame_host->Send(new ExtensionMsg_SetMainFrameExtensionOwner(
73 render_frame_host->GetRoutingID(), main_frame_extension->id()));
74 }
75}
76
rdevlin.cronincb2ec659a2015-06-10 23:32:4177content::WebContents* ExtensionWebContentsObserver::GetAssociatedWebContents()
78 const {
79 return web_contents();
80}
81
[email protected]1ce15972014-03-20 19:25:4882void ExtensionWebContentsObserver::RenderViewCreated(
83 content::RenderViewHost* render_view_host) {
rdevlin.cronin6f42c2522015-06-19 18:58:5184 // TODO(devlin): Most/all of this should move to RenderFrameCreated.
[email protected]1ce15972014-03-20 19:25:4885 const Extension* extension = GetExtension(render_view_host);
86 if (!extension)
87 return;
88
89 content::RenderProcessHost* process = render_view_host->GetProcess();
90
91 // Some extensions use chrome:// URLs.
92 // This is a temporary solution. Replace it with access to chrome-static://
93 // once it is implemented. See: crbug.com/226927.
94 Manifest::Type type = extension->GetType();
95 if (type == Manifest::TYPE_EXTENSION ||
96 type == Manifest::TYPE_LEGACY_PACKAGED_APP ||
97 (type == Manifest::TYPE_PLATFORM_APP &&
98 extension->location() == Manifest::COMPONENT)) {
99 content::ChildProcessSecurityPolicy::GetInstance()->GrantScheme(
100 process->GetID(), content::kChromeUIScheme);
101 }
102
103 // Some extensions use file:// URLs.
104 if (type == Manifest::TYPE_EXTENSION ||
105 type == Manifest::TYPE_LEGACY_PACKAGED_APP) {
106 ExtensionPrefs* prefs = ExtensionPrefs::Get(browser_context_);
107 if (prefs->AllowFileAccess(extension->id())) {
108 content::ChildProcessSecurityPolicy::GetInstance()->GrantScheme(
[email protected]cca6f392014-05-28 21:32:26109 process->GetID(), url::kFileScheme);
[email protected]1ce15972014-03-20 19:25:48110 }
111 }
112
kalman8bcbc7592015-06-03 23:12:27113 // Tells the new view that it's hosted in an extension process.
114 //
115 // This will often be a rendant IPC, because activating extensions happens at
116 // the process level, not at the view level. However, without some mild
117 // refactoring this isn't trivial to do, and this way is simpler.
118 //
119 // Plus, we can delete the concept of activating an extension once site
120 // isolation is turned on.
121 render_view_host->Send(new ExtensionMsg_ActivateExtension(extension->id()));
[email protected]1ce15972014-03-20 19:25:48122}
123
sammc143f3c52015-02-13 09:42:38124void ExtensionWebContentsObserver::RenderFrameCreated(
125 content::RenderFrameHost* render_frame_host) {
rdevlin.cronin6f42c2522015-06-19 18:58:51126 InitializeRenderFrame(render_frame_host);
127}
128
129void ExtensionWebContentsObserver::RenderFrameDeleted(
130 content::RenderFrameHost* render_frame_host) {
131 ProcessManager::Get(browser_context_)
132 ->UnregisterRenderFrameHost(render_frame_host);
sammc143f3c52015-02-13 09:42:38133}
134
rdevlin.cronincb2ec659a2015-06-10 23:32:41135bool ExtensionWebContentsObserver::OnMessageReceived(
rdevlin.cronin92503ba2015-06-12 17:00:56136 const IPC::Message& message,
137 content::RenderFrameHost* render_frame_host) {
rdevlin.cronincb2ec659a2015-06-10 23:32:41138 bool handled = true;
rdevlin.cronin92503ba2015-06-12 17:00:56139 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(
140 ExtensionWebContentsObserver, message, render_frame_host)
rdevlin.cronincb2ec659a2015-06-10 23:32:41141 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest)
142 IPC_MESSAGE_UNHANDLED(handled = false)
143 IPC_END_MESSAGE_MAP()
144 return handled;
145}
146
emaxxe70f5e12015-05-29 11:26:00147void ExtensionWebContentsObserver::PepperInstanceCreated() {
148 ProcessManager* const process_manager = ProcessManager::Get(browser_context_);
149 const Extension* const extension =
150 process_manager->GetExtensionForWebContents(web_contents());
151 if (extension)
152 process_manager->IncrementLazyKeepaliveCount(extension);
153}
154
155void ExtensionWebContentsObserver::PepperInstanceDeleted() {
156 ProcessManager* const process_manager = ProcessManager::Get(browser_context_);
157 const Extension* const extension =
158 process_manager->GetExtensionForWebContents(web_contents());
159 if (extension)
160 process_manager->DecrementLazyKeepaliveCount(extension);
161}
162
rdevlin.cronin86f5b702015-06-24 18:49:17163std::string ExtensionWebContentsObserver::GetExtensionIdFromFrame(
164 content::RenderFrameHost* render_frame_host) const {
165 content::SiteInstance* site_instance = render_frame_host->GetSiteInstance();
166 GURL url = render_frame_host->GetLastCommittedURL();
167 if (!url.is_empty()) {
168 if (site_instance->GetSiteURL().GetOrigin() != url.GetOrigin())
169 return std::string();
170 } else {
171 url = site_instance->GetSiteURL();
172 }
173
174 return url.SchemeIs(kExtensionScheme) ? url.host() : std::string();
175}
176
177const Extension* ExtensionWebContentsObserver::GetExtensionFromFrame(
178 content::RenderFrameHost* render_frame_host) const {
179 return ExtensionRegistry::Get(
180 render_frame_host->GetProcess()->GetBrowserContext())
181 ->enabled_extensions()
182 .GetByID(GetExtensionIdFromFrame(render_frame_host));
183}
184
[email protected]1ce15972014-03-20 19:25:48185const Extension* ExtensionWebContentsObserver::GetExtension(
186 content::RenderViewHost* render_view_host) {
187 std::string extension_id = GetExtensionId(render_view_host);
188 if (extension_id.empty())
189 return NULL;
190
191 // May be null if the extension doesn't exist, for example if somebody typos
192 // a chrome-extension:// URL.
193 return ExtensionRegistry::Get(browser_context_)
194 ->GetExtensionById(extension_id, ExtensionRegistry::ENABLED);
195}
196
197// static
198std::string ExtensionWebContentsObserver::GetExtensionId(
199 content::RenderViewHost* render_view_host) {
200 // Note that due to ChromeContentBrowserClient::GetEffectiveURL(), hosted apps
201 // (excluding bookmark apps) will have a chrome-extension:// URL for their
202 // site, so we can ignore that wrinkle here.
203 const GURL& site = render_view_host->GetSiteInstance()->GetSiteURL();
204
205 if (!site.SchemeIs(kExtensionScheme))
206 return std::string();
207
208 return site.host();
209}
210
rdevlin.cronincb2ec659a2015-06-10 23:32:41211void ExtensionWebContentsObserver::OnRequest(
rdevlin.cronin92503ba2015-06-12 17:00:56212 content::RenderFrameHost* render_frame_host,
rdevlin.cronincb2ec659a2015-06-10 23:32:41213 const ExtensionHostMsg_Request_Params& params) {
rdevlin.cronin92503ba2015-06-12 17:00:56214 dispatcher_.Dispatch(params, render_frame_host);
rdevlin.cronincb2ec659a2015-06-10 23:32:41215}
216
rdevlin.cronin6f42c2522015-06-19 18:58:51217void ExtensionWebContentsObserver::InitializeFrameHelper(
218 content::RenderFrameHost* render_frame_host) {
219 // Since this is called for all existing RenderFrameHosts during the
220 // ExtensionWebContentsObserver's creation, it's possible that not all hosts
221 // are ready.
222 // We only initialize the frame if the renderer counterpart is live; otherwise
223 // we wait for the RenderFrameCreated notification.
224 if (render_frame_host->IsRenderFrameLive())
225 InitializeRenderFrame(render_frame_host);
226}
227
[email protected]1ce15972014-03-20 19:25:48228} // namespace extensions