net: Update mime_util to disable Ogg Opus on Android.
[chromium-blink-merge.git] / extensions / browser / extension_web_contents_observer.cc
blobba13edd69c385e2a5dc46df0c00481489b7b5b85
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_process_host.h"
9 #include "content/public/browser/render_view_host.h"
10 #include "content/public/browser/site_instance.h"
11 #include "content/public/browser/web_contents.h"
12 #include "content/public/common/url_constants.h"
13 #include "extensions/browser/extension_prefs.h"
14 #include "extensions/browser/extension_registry.h"
15 #include "extensions/browser/mojo/service_registration_manager.h"
16 #include "extensions/browser/view_type_utils.h"
17 #include "extensions/common/constants.h"
18 #include "extensions/common/extension_messages.h"
20 namespace extensions {
22 ExtensionWebContentsObserver::ExtensionWebContentsObserver(
23 content::WebContents* web_contents)
24 : content::WebContentsObserver(web_contents),
25 browser_context_(web_contents->GetBrowserContext()) {
26 NotifyRenderViewType(web_contents->GetRenderViewHost());
29 ExtensionWebContentsObserver::~ExtensionWebContentsObserver() {}
31 void ExtensionWebContentsObserver::RenderViewCreated(
32 content::RenderViewHost* render_view_host) {
33 NotifyRenderViewType(render_view_host);
35 // TODO(sammc): Call AddServicesToRenderFrame() for frames that aren't main
36 // frames.
37 ServiceRegistrationManager::GetSharedInstance()->AddServicesToRenderFrame(
38 render_view_host->GetMainFrame());
40 const Extension* extension = GetExtension(render_view_host);
41 if (!extension)
42 return;
44 content::RenderProcessHost* process = render_view_host->GetProcess();
46 // Some extensions use chrome:// URLs.
47 // This is a temporary solution. Replace it with access to chrome-static://
48 // once it is implemented. See: crbug.com/226927.
49 Manifest::Type type = extension->GetType();
50 if (type == Manifest::TYPE_EXTENSION ||
51 type == Manifest::TYPE_LEGACY_PACKAGED_APP ||
52 (type == Manifest::TYPE_PLATFORM_APP &&
53 extension->location() == Manifest::COMPONENT)) {
54 content::ChildProcessSecurityPolicy::GetInstance()->GrantScheme(
55 process->GetID(), content::kChromeUIScheme);
58 // Some extensions use file:// URLs.
59 if (type == Manifest::TYPE_EXTENSION ||
60 type == Manifest::TYPE_LEGACY_PACKAGED_APP) {
61 ExtensionPrefs* prefs = ExtensionPrefs::Get(browser_context_);
62 if (prefs->AllowFileAccess(extension->id())) {
63 content::ChildProcessSecurityPolicy::GetInstance()->GrantScheme(
64 process->GetID(), url::kFileScheme);
68 switch (type) {
69 case Manifest::TYPE_EXTENSION:
70 case Manifest::TYPE_USER_SCRIPT:
71 case Manifest::TYPE_HOSTED_APP:
72 case Manifest::TYPE_LEGACY_PACKAGED_APP:
73 case Manifest::TYPE_PLATFORM_APP:
74 // Always send a Loaded message before ActivateExtension so that
75 // ExtensionDispatcher knows what Extension is active, not just its ID.
76 // This is important for classifying the Extension's JavaScript context
77 // correctly (see ExtensionDispatcher::ClassifyJavaScriptContext).
78 // We also have to include the tab-specific permissions here, since it's
79 // an extension process.
80 render_view_host->Send(
81 new ExtensionMsg_Loaded(std::vector<ExtensionMsg_Loaded_Params>(
82 1, ExtensionMsg_Loaded_Params(
83 extension, true /* include tab permissions */))));
84 render_view_host->Send(
85 new ExtensionMsg_ActivateExtension(extension->id()));
86 break;
88 case Manifest::TYPE_UNKNOWN:
89 case Manifest::TYPE_THEME:
90 case Manifest::TYPE_SHARED_MODULE:
91 break;
93 case Manifest::NUM_LOAD_TYPES:
94 NOTREACHED();
98 void ExtensionWebContentsObserver::NotifyRenderViewType(
99 content::RenderViewHost* render_view_host) {
100 if (render_view_host) {
101 render_view_host->Send(new ExtensionMsg_NotifyRenderViewType(
102 render_view_host->GetRoutingID(), GetViewType(web_contents())));
106 const Extension* ExtensionWebContentsObserver::GetExtension(
107 content::RenderViewHost* render_view_host) {
108 std::string extension_id = GetExtensionId(render_view_host);
109 if (extension_id.empty())
110 return NULL;
112 // May be null if the extension doesn't exist, for example if somebody typos
113 // a chrome-extension:// URL.
114 return ExtensionRegistry::Get(browser_context_)
115 ->GetExtensionById(extension_id, ExtensionRegistry::ENABLED);
118 // static
119 std::string ExtensionWebContentsObserver::GetExtensionId(
120 content::RenderViewHost* render_view_host) {
121 // Note that due to ChromeContentBrowserClient::GetEffectiveURL(), hosted apps
122 // (excluding bookmark apps) will have a chrome-extension:// URL for their
123 // site, so we can ignore that wrinkle here.
124 const GURL& site = render_view_host->GetSiteInstance()->GetSiteURL();
126 if (!site.SchemeIs(kExtensionScheme))
127 return std::string();
129 return site.host();
132 } // namespace extensions