Add ICU message format support
[chromium-blink-merge.git] / extensions / browser / extension_web_contents_observer.cc
blob187cfb22a95ead9a7d997d18aefe30ccd39cb71c
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 {
26 // static
27 ExtensionWebContentsObserver* ExtensionWebContentsObserver::GetForWebContents(
28 content::WebContents* web_contents) {
29 return ExtensionsBrowserClient::Get()->GetExtensionWebContentsObserver(
30 web_contents);
33 ExtensionWebContentsObserver::ExtensionWebContentsObserver(
34 content::WebContents* web_contents)
35 : content::WebContentsObserver(web_contents),
36 browser_context_(web_contents->GetBrowserContext()),
37 dispatcher_(browser_context_) {
38 web_contents->ForEachFrame(
39 base::Bind(&ExtensionWebContentsObserver::InitializeFrameHelper,
40 base::Unretained(this)));
41 dispatcher_.set_delegate(this);
44 ExtensionWebContentsObserver::~ExtensionWebContentsObserver() {
47 void ExtensionWebContentsObserver::InitializeRenderFrame(
48 content::RenderFrameHost* render_frame_host) {
49 DCHECK(render_frame_host);
50 DCHECK(render_frame_host->IsRenderFrameLive());
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())));
56 const Extension* frame_extension = GetExtensionFromFrame(render_frame_host);
57 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);
66 content::WebContents* ExtensionWebContentsObserver::GetAssociatedWebContents()
67 const {
68 return web_contents();
71 void ExtensionWebContentsObserver::RenderViewCreated(
72 content::RenderViewHost* render_view_host) {
73 // TODO(devlin): Most/all of this should move to RenderFrameCreated.
74 const Extension* extension = GetExtension(render_view_host);
75 if (!extension)
76 return;
78 content::RenderProcessHost* process = render_view_host->GetProcess();
80 // Some extensions use chrome:// URLs.
81 // This is a temporary solution. Replace it with access to chrome-static://
82 // once it is implemented. See: crbug.com/226927.
83 Manifest::Type type = extension->GetType();
84 if (type == Manifest::TYPE_EXTENSION ||
85 type == Manifest::TYPE_LEGACY_PACKAGED_APP ||
86 (type == Manifest::TYPE_PLATFORM_APP &&
87 extension->location() == Manifest::COMPONENT)) {
88 content::ChildProcessSecurityPolicy::GetInstance()->GrantScheme(
89 process->GetID(), content::kChromeUIScheme);
92 // Some extensions use file:// URLs.
93 if (type == Manifest::TYPE_EXTENSION ||
94 type == Manifest::TYPE_LEGACY_PACKAGED_APP) {
95 ExtensionPrefs* prefs = ExtensionPrefs::Get(browser_context_);
96 if (prefs->AllowFileAccess(extension->id())) {
97 content::ChildProcessSecurityPolicy::GetInstance()->GrantScheme(
98 process->GetID(), url::kFileScheme);
102 // Tells the new view that it's hosted in an extension process.
104 // This will often be a rendant IPC, because activating extensions happens at
105 // the process level, not at the view level. However, without some mild
106 // refactoring this isn't trivial to do, and this way is simpler.
108 // Plus, we can delete the concept of activating an extension once site
109 // isolation is turned on.
110 render_view_host->Send(new ExtensionMsg_ActivateExtension(extension->id()));
113 void ExtensionWebContentsObserver::RenderFrameCreated(
114 content::RenderFrameHost* render_frame_host) {
115 InitializeRenderFrame(render_frame_host);
118 void ExtensionWebContentsObserver::RenderFrameDeleted(
119 content::RenderFrameHost* render_frame_host) {
120 ProcessManager::Get(browser_context_)
121 ->UnregisterRenderFrameHost(render_frame_host);
124 bool ExtensionWebContentsObserver::OnMessageReceived(
125 const IPC::Message& message,
126 content::RenderFrameHost* render_frame_host) {
127 bool handled = true;
128 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(
129 ExtensionWebContentsObserver, message, render_frame_host)
130 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest)
131 IPC_MESSAGE_UNHANDLED(handled = false)
132 IPC_END_MESSAGE_MAP()
133 return handled;
136 void ExtensionWebContentsObserver::PepperInstanceCreated() {
137 ProcessManager* const process_manager = ProcessManager::Get(browser_context_);
138 const Extension* const extension =
139 process_manager->GetExtensionForWebContents(web_contents());
140 if (extension)
141 process_manager->IncrementLazyKeepaliveCount(extension);
144 void ExtensionWebContentsObserver::PepperInstanceDeleted() {
145 ProcessManager* const process_manager = ProcessManager::Get(browser_context_);
146 const Extension* const extension =
147 process_manager->GetExtensionForWebContents(web_contents());
148 if (extension)
149 process_manager->DecrementLazyKeepaliveCount(extension);
152 std::string ExtensionWebContentsObserver::GetExtensionIdFromFrame(
153 content::RenderFrameHost* render_frame_host) const {
154 content::SiteInstance* site_instance = render_frame_host->GetSiteInstance();
155 GURL url = render_frame_host->GetLastCommittedURL();
156 if (!url.is_empty()) {
157 if (site_instance->GetSiteURL().GetOrigin() != url.GetOrigin())
158 return std::string();
159 } else {
160 url = site_instance->GetSiteURL();
163 return url.SchemeIs(kExtensionScheme) ? url.host() : std::string();
166 const Extension* ExtensionWebContentsObserver::GetExtensionFromFrame(
167 content::RenderFrameHost* render_frame_host) const {
168 return ExtensionRegistry::Get(
169 render_frame_host->GetProcess()->GetBrowserContext())
170 ->enabled_extensions()
171 .GetByID(GetExtensionIdFromFrame(render_frame_host));
174 const Extension* ExtensionWebContentsObserver::GetExtension(
175 content::RenderViewHost* render_view_host) {
176 std::string extension_id = GetExtensionId(render_view_host);
177 if (extension_id.empty())
178 return NULL;
180 // May be null if the extension doesn't exist, for example if somebody typos
181 // a chrome-extension:// URL.
182 return ExtensionRegistry::Get(browser_context_)
183 ->GetExtensionById(extension_id, ExtensionRegistry::ENABLED);
186 // static
187 std::string ExtensionWebContentsObserver::GetExtensionId(
188 content::RenderViewHost* render_view_host) {
189 // Note that due to ChromeContentBrowserClient::GetEffectiveURL(), hosted apps
190 // (excluding bookmark apps) will have a chrome-extension:// URL for their
191 // site, so we can ignore that wrinkle here.
192 const GURL& site = render_view_host->GetSiteInstance()->GetSiteURL();
194 if (!site.SchemeIs(kExtensionScheme))
195 return std::string();
197 return site.host();
200 void ExtensionWebContentsObserver::OnRequest(
201 content::RenderFrameHost* render_frame_host,
202 const ExtensionHostMsg_Request_Params& params) {
203 dispatcher_.Dispatch(params, render_frame_host);
206 void ExtensionWebContentsObserver::InitializeFrameHelper(
207 content::RenderFrameHost* render_frame_host) {
208 // Since this is called for all existing RenderFrameHosts during the
209 // ExtensionWebContentsObserver's creation, it's possible that not all hosts
210 // are ready.
211 // We only initialize the frame if the renderer counterpart is live; otherwise
212 // we wait for the RenderFrameCreated notification.
213 if (render_frame_host->IsRenderFrameLive())
214 InitializeRenderFrame(render_frame_host);
217 } // namespace extensions