Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / extensions / browser / extension_web_contents_observer.cc
blob5ba2ebb5fcf81f9d3306e921877d52012bb3d44c
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_messages.h"
23 namespace extensions {
24 namespace {
26 const Extension* GetExtensionForRenderFrame(
27 content::RenderFrameHost* render_frame_host) {
28 content::SiteInstance* site_instance = render_frame_host->GetSiteInstance();
29 GURL url = render_frame_host->GetLastCommittedURL();
30 if (!url.is_empty()) {
31 if (site_instance->GetSiteURL().GetOrigin() != url.GetOrigin())
32 return nullptr;
33 } else {
34 url = site_instance->GetSiteURL();
36 content::BrowserContext* browser_context = site_instance->GetBrowserContext();
37 if (!url.SchemeIs(kExtensionScheme))
38 return nullptr;
40 return ExtensionRegistry::Get(browser_context)
41 ->enabled_extensions()
42 .GetExtensionOrAppByURL(url);
45 } // namespace
47 ExtensionWebContentsObserver::ExtensionWebContentsObserver(
48 content::WebContents* web_contents)
49 : content::WebContentsObserver(web_contents),
50 browser_context_(web_contents->GetBrowserContext()) {
51 NotifyRenderViewType(web_contents->GetRenderViewHost());
52 content::RenderFrameHost* host = web_contents->GetMainFrame();
53 if (host)
54 RenderFrameHostChanged(nullptr, host);
57 ExtensionWebContentsObserver::~ExtensionWebContentsObserver() {
60 void ExtensionWebContentsObserver::RenderViewCreated(
61 content::RenderViewHost* render_view_host) {
62 NotifyRenderViewType(render_view_host);
64 const Extension* extension = GetExtension(render_view_host);
65 if (!extension)
66 return;
68 content::RenderProcessHost* process = render_view_host->GetProcess();
70 // Some extensions use chrome:// URLs.
71 // This is a temporary solution. Replace it with access to chrome-static://
72 // once it is implemented. See: crbug.com/226927.
73 Manifest::Type type = extension->GetType();
74 if (type == Manifest::TYPE_EXTENSION ||
75 type == Manifest::TYPE_LEGACY_PACKAGED_APP ||
76 (type == Manifest::TYPE_PLATFORM_APP &&
77 extension->location() == Manifest::COMPONENT)) {
78 content::ChildProcessSecurityPolicy::GetInstance()->GrantScheme(
79 process->GetID(), content::kChromeUIScheme);
82 // Some extensions use file:// URLs.
83 if (type == Manifest::TYPE_EXTENSION ||
84 type == Manifest::TYPE_LEGACY_PACKAGED_APP) {
85 ExtensionPrefs* prefs = ExtensionPrefs::Get(browser_context_);
86 if (prefs->AllowFileAccess(extension->id())) {
87 content::ChildProcessSecurityPolicy::GetInstance()->GrantScheme(
88 process->GetID(), url::kFileScheme);
92 switch (type) {
93 case Manifest::TYPE_EXTENSION:
94 case Manifest::TYPE_USER_SCRIPT:
95 case Manifest::TYPE_HOSTED_APP:
96 case Manifest::TYPE_LEGACY_PACKAGED_APP:
97 case Manifest::TYPE_PLATFORM_APP:
98 // Always send a Loaded message before ActivateExtension so that
99 // ExtensionDispatcher knows what Extension is active, not just its ID.
100 // This is important for classifying the Extension's JavaScript context
101 // correctly (see ExtensionDispatcher::ClassifyJavaScriptContext).
102 // We also have to include the tab-specific permissions here, since it's
103 // an extension process.
104 render_view_host->Send(
105 new ExtensionMsg_Loaded(std::vector<ExtensionMsg_Loaded_Params>(
106 1, ExtensionMsg_Loaded_Params(
107 extension, true /* include tab permissions */))));
108 render_view_host->Send(
109 new ExtensionMsg_ActivateExtension(extension->id()));
110 break;
112 case Manifest::TYPE_UNKNOWN:
113 case Manifest::TYPE_THEME:
114 case Manifest::TYPE_SHARED_MODULE:
115 break;
117 case Manifest::NUM_LOAD_TYPES:
118 NOTREACHED();
122 void ExtensionWebContentsObserver::RenderFrameCreated(
123 content::RenderFrameHost* render_frame_host) {
124 const Extension* extension = GetExtensionForRenderFrame(render_frame_host);
125 if (extension) {
126 ExtensionsBrowserClient::Get()->RegisterMojoServices(render_frame_host,
127 extension);
131 void ExtensionWebContentsObserver::FrameDeleted(
132 content::RenderFrameHost* render_frame_host) {
133 ProcessManager::Get(browser_context_)->UnregisterRenderFrameHost(
134 render_frame_host);
137 void ExtensionWebContentsObserver::RenderFrameHostChanged(
138 content::RenderFrameHost* old_host,
139 content::RenderFrameHost* new_host) {
140 ProcessManager* process_manager = ProcessManager::Get(browser_context_);
141 if (old_host)
142 process_manager->UnregisterRenderFrameHost(old_host);
144 const Extension* extension = GetExtension(new_host->GetRenderViewHost());
145 if (extension) {
146 process_manager->RegisterRenderFrameHost(
147 web_contents(), new_host, extension);
151 void ExtensionWebContentsObserver::NotifyRenderViewType(
152 content::RenderViewHost* render_view_host) {
153 if (render_view_host) {
154 render_view_host->Send(new ExtensionMsg_NotifyRenderViewType(
155 render_view_host->GetRoutingID(), GetViewType(web_contents())));
159 const Extension* ExtensionWebContentsObserver::GetExtension(
160 content::RenderViewHost* render_view_host) {
161 std::string extension_id = GetExtensionId(render_view_host);
162 if (extension_id.empty())
163 return NULL;
165 // May be null if the extension doesn't exist, for example if somebody typos
166 // a chrome-extension:// URL.
167 return ExtensionRegistry::Get(browser_context_)
168 ->GetExtensionById(extension_id, ExtensionRegistry::ENABLED);
171 // static
172 std::string ExtensionWebContentsObserver::GetExtensionId(
173 content::RenderViewHost* render_view_host) {
174 // Note that due to ChromeContentBrowserClient::GetEffectiveURL(), hosted apps
175 // (excluding bookmark apps) will have a chrome-extension:// URL for their
176 // site, so we can ignore that wrinkle here.
177 const GURL& site = render_view_host->GetSiteInstance()->GetSiteURL();
179 if (!site.SchemeIs(kExtensionScheme))
180 return std::string();
182 return site.host();
185 } // namespace extensions