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.
5 #include "extensions/browser/extension_web_contents_observer.h"
7 #include "content/public/browser/child_process_security_policy.h"
8 #include "content/public/browser/render_frame_host.h"
9 #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"
16 #include "extensions/browser/extensions_browser_client.h"
17 #include "extensions/browser/mojo/service_registration.h"
18 #include "extensions/browser/process_manager.h"
19 #include "extensions/browser/view_type_utils.h"
20 #include "extensions/common/constants.h"
21 #include "extensions/common/extension.h"
22 #include "extensions/common/extension_messages.h"
24 namespace extensions
{
27 const Extension
* GetExtensionForRenderFrame(
28 content::RenderFrameHost
* render_frame_host
) {
29 content::SiteInstance
* site_instance
= render_frame_host
->GetSiteInstance();
30 GURL url
= render_frame_host
->GetLastCommittedURL();
31 if (!url
.is_empty()) {
32 if (site_instance
->GetSiteURL().GetOrigin() != url
.GetOrigin())
35 url
= site_instance
->GetSiteURL();
37 content::BrowserContext
* browser_context
= site_instance
->GetBrowserContext();
38 if (!url
.SchemeIs(kExtensionScheme
))
41 return ExtensionRegistry::Get(browser_context
)
42 ->enabled_extensions()
43 .GetExtensionOrAppByURL(url
);
48 ExtensionWebContentsObserver::ExtensionWebContentsObserver(
49 content::WebContents
* web_contents
)
50 : content::WebContentsObserver(web_contents
),
51 browser_context_(web_contents
->GetBrowserContext()) {
52 NotifyRenderViewType(web_contents
->GetRenderViewHost());
53 content::RenderFrameHost
* host
= web_contents
->GetMainFrame();
55 RenderFrameHostChanged(nullptr, host
);
58 ExtensionWebContentsObserver::~ExtensionWebContentsObserver() {
61 void ExtensionWebContentsObserver::RenderViewCreated(
62 content::RenderViewHost
* render_view_host
) {
63 NotifyRenderViewType(render_view_host
);
65 const Extension
* extension
= GetExtension(render_view_host
);
69 content::RenderProcessHost
* process
= render_view_host
->GetProcess();
71 // Some extensions use chrome:// URLs.
72 // This is a temporary solution. Replace it with access to chrome-static://
73 // once it is implemented. See: crbug.com/226927.
74 Manifest::Type type
= extension
->GetType();
75 if (type
== Manifest::TYPE_EXTENSION
||
76 type
== Manifest::TYPE_LEGACY_PACKAGED_APP
||
77 (type
== Manifest::TYPE_PLATFORM_APP
&&
78 extension
->location() == Manifest::COMPONENT
)) {
79 content::ChildProcessSecurityPolicy::GetInstance()->GrantScheme(
80 process
->GetID(), content::kChromeUIScheme
);
83 // Some extensions use file:// URLs.
84 if (type
== Manifest::TYPE_EXTENSION
||
85 type
== Manifest::TYPE_LEGACY_PACKAGED_APP
) {
86 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(browser_context_
);
87 if (prefs
->AllowFileAccess(extension
->id())) {
88 content::ChildProcessSecurityPolicy::GetInstance()->GrantScheme(
89 process
->GetID(), url::kFileScheme
);
94 case Manifest::TYPE_EXTENSION
:
95 case Manifest::TYPE_USER_SCRIPT
:
96 case Manifest::TYPE_HOSTED_APP
:
97 case Manifest::TYPE_LEGACY_PACKAGED_APP
:
98 case Manifest::TYPE_PLATFORM_APP
:
99 // Always send a Loaded message before ActivateExtension so that
100 // ExtensionDispatcher knows what Extension is active, not just its ID.
101 // This is important for classifying the Extension's JavaScript context
102 // correctly (see ExtensionDispatcher::ClassifyJavaScriptContext).
103 // We also have to include the tab-specific permissions here, since it's
104 // an extension process.
105 render_view_host
->Send(
106 new ExtensionMsg_Loaded(std::vector
<ExtensionMsg_Loaded_Params
>(
107 1, ExtensionMsg_Loaded_Params(
108 extension
, true /* include tab permissions */))));
109 render_view_host
->Send(
110 new ExtensionMsg_ActivateExtension(extension
->id()));
113 case Manifest::TYPE_UNKNOWN
:
114 case Manifest::TYPE_THEME
:
115 case Manifest::TYPE_SHARED_MODULE
:
118 case Manifest::NUM_LOAD_TYPES
:
123 void ExtensionWebContentsObserver::RenderFrameCreated(
124 content::RenderFrameHost
* render_frame_host
) {
125 const Extension
* extension
= GetExtensionForRenderFrame(render_frame_host
);
127 ExtensionsBrowserClient::Get()->RegisterMojoServices(render_frame_host
,
132 void ExtensionWebContentsObserver::FrameDeleted(
133 content::RenderFrameHost
* render_frame_host
) {
134 ProcessManager::Get(browser_context_
)->UnregisterRenderFrameHost(
138 void ExtensionWebContentsObserver::RenderFrameHostChanged(
139 content::RenderFrameHost
* old_host
,
140 content::RenderFrameHost
* new_host
) {
141 ProcessManager
* process_manager
= ProcessManager::Get(browser_context_
);
143 process_manager
->UnregisterRenderFrameHost(old_host
);
145 const Extension
* extension
= GetExtension(new_host
->GetRenderViewHost());
147 process_manager
->RegisterRenderFrameHost(
148 web_contents(), new_host
, extension
);
152 void ExtensionWebContentsObserver::NotifyRenderViewType(
153 content::RenderViewHost
* render_view_host
) {
154 if (render_view_host
) {
155 render_view_host
->Send(new ExtensionMsg_NotifyRenderViewType(
156 render_view_host
->GetRoutingID(), GetViewType(web_contents())));
160 void ExtensionWebContentsObserver::PepperInstanceCreated() {
161 ProcessManager
* const process_manager
= ProcessManager::Get(browser_context_
);
162 const Extension
* const extension
=
163 process_manager
->GetExtensionForWebContents(web_contents());
165 process_manager
->IncrementLazyKeepaliveCount(extension
);
168 void ExtensionWebContentsObserver::PepperInstanceDeleted() {
169 ProcessManager
* const process_manager
= ProcessManager::Get(browser_context_
);
170 const Extension
* const extension
=
171 process_manager
->GetExtensionForWebContents(web_contents());
173 process_manager
->DecrementLazyKeepaliveCount(extension
);
176 const Extension
* ExtensionWebContentsObserver::GetExtension(
177 content::RenderViewHost
* render_view_host
) {
178 std::string extension_id
= GetExtensionId(render_view_host
);
179 if (extension_id
.empty())
182 // May be null if the extension doesn't exist, for example if somebody typos
183 // a chrome-extension:// URL.
184 return ExtensionRegistry::Get(browser_context_
)
185 ->GetExtensionById(extension_id
, ExtensionRegistry::ENABLED
);
189 std::string
ExtensionWebContentsObserver::GetExtensionId(
190 content::RenderViewHost
* render_view_host
) {
191 // Note that due to ChromeContentBrowserClient::GetEffectiveURL(), hosted apps
192 // (excluding bookmark apps) will have a chrome-extension:// URL for their
193 // site, so we can ignore that wrinkle here.
194 const GURL
& site
= render_view_host
->GetSiteInstance()->GetSiteURL();
196 if (!site
.SchemeIs(kExtensionScheme
))
197 return std::string();
202 } // namespace extensions