[email protected] | 1ce1597 | 2014-03-20 19:25:48 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "extensions/browser/extension_web_contents_observer.h" |
| 6 | |
| 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" |
sammc | 143f3c5 | 2015-02-13 09:42:38 | [diff] [blame^] | 15 | #include "extensions/browser/mojo/service_registration.h" |
[email protected] | 1ce1597 | 2014-03-20 19:25:48 | [diff] [blame] | 16 | #include "extensions/browser/view_type_utils.h" |
| 17 | #include "extensions/common/constants.h" |
| 18 | #include "extensions/common/extension_messages.h" |
| 19 | |
| 20 | namespace extensions { |
| 21 | |
| 22 | ExtensionWebContentsObserver::ExtensionWebContentsObserver( |
| 23 | content::WebContents* web_contents) |
| 24 | : content::WebContentsObserver(web_contents), |
[email protected] | 0b36507 | 2014-03-22 06:14:18 | [diff] [blame] | 25 | browser_context_(web_contents->GetBrowserContext()) { |
| 26 | NotifyRenderViewType(web_contents->GetRenderViewHost()); |
| 27 | } |
[email protected] | 1ce1597 | 2014-03-20 19:25:48 | [diff] [blame] | 28 | |
| 29 | ExtensionWebContentsObserver::~ExtensionWebContentsObserver() {} |
| 30 | |
| 31 | void ExtensionWebContentsObserver::RenderViewCreated( |
| 32 | content::RenderViewHost* render_view_host) { |
[email protected] | 0b36507 | 2014-03-22 06:14:18 | [diff] [blame] | 33 | NotifyRenderViewType(render_view_host); |
[email protected] | 1ce1597 | 2014-03-20 19:25:48 | [diff] [blame] | 34 | |
[email protected] | 1ce1597 | 2014-03-20 19:25:48 | [diff] [blame] | 35 | const Extension* extension = GetExtension(render_view_host); |
| 36 | if (!extension) |
| 37 | return; |
| 38 | |
| 39 | content::RenderProcessHost* process = render_view_host->GetProcess(); |
| 40 | |
| 41 | // Some extensions use chrome:// URLs. |
| 42 | // This is a temporary solution. Replace it with access to chrome-static:// |
| 43 | // once it is implemented. See: crbug.com/226927. |
| 44 | Manifest::Type type = extension->GetType(); |
| 45 | if (type == Manifest::TYPE_EXTENSION || |
| 46 | type == Manifest::TYPE_LEGACY_PACKAGED_APP || |
| 47 | (type == Manifest::TYPE_PLATFORM_APP && |
| 48 | extension->location() == Manifest::COMPONENT)) { |
| 49 | content::ChildProcessSecurityPolicy::GetInstance()->GrantScheme( |
| 50 | process->GetID(), content::kChromeUIScheme); |
| 51 | } |
| 52 | |
| 53 | // Some extensions use file:// URLs. |
| 54 | if (type == Manifest::TYPE_EXTENSION || |
| 55 | type == Manifest::TYPE_LEGACY_PACKAGED_APP) { |
| 56 | ExtensionPrefs* prefs = ExtensionPrefs::Get(browser_context_); |
| 57 | if (prefs->AllowFileAccess(extension->id())) { |
| 58 | content::ChildProcessSecurityPolicy::GetInstance()->GrantScheme( |
[email protected] | cca6f39 | 2014-05-28 21:32:26 | [diff] [blame] | 59 | process->GetID(), url::kFileScheme); |
[email protected] | 1ce1597 | 2014-03-20 19:25:48 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
| 63 | switch (type) { |
| 64 | case Manifest::TYPE_EXTENSION: |
| 65 | case Manifest::TYPE_USER_SCRIPT: |
| 66 | case Manifest::TYPE_HOSTED_APP: |
| 67 | case Manifest::TYPE_LEGACY_PACKAGED_APP: |
| 68 | case Manifest::TYPE_PLATFORM_APP: |
| 69 | // Always send a Loaded message before ActivateExtension so that |
| 70 | // ExtensionDispatcher knows what Extension is active, not just its ID. |
| 71 | // This is important for classifying the Extension's JavaScript context |
| 72 | // correctly (see ExtensionDispatcher::ClassifyJavaScriptContext). |
rdevlin.cronin | c491dbf | 2015-02-07 00:31:55 | [diff] [blame] | 73 | // We also have to include the tab-specific permissions here, since it's |
| 74 | // an extension process. |
[email protected] | 1ce1597 | 2014-03-20 19:25:48 | [diff] [blame] | 75 | render_view_host->Send( |
| 76 | new ExtensionMsg_Loaded(std::vector<ExtensionMsg_Loaded_Params>( |
rdevlin.cronin | c491dbf | 2015-02-07 00:31:55 | [diff] [blame] | 77 | 1, ExtensionMsg_Loaded_Params( |
| 78 | extension, true /* include tab permissions */)))); |
[email protected] | 1ce1597 | 2014-03-20 19:25:48 | [diff] [blame] | 79 | render_view_host->Send( |
| 80 | new ExtensionMsg_ActivateExtension(extension->id())); |
| 81 | break; |
| 82 | |
| 83 | case Manifest::TYPE_UNKNOWN: |
| 84 | case Manifest::TYPE_THEME: |
| 85 | case Manifest::TYPE_SHARED_MODULE: |
| 86 | break; |
[email protected] | 180d4e9 | 2014-05-22 15:35:16 | [diff] [blame] | 87 | |
| 88 | case Manifest::NUM_LOAD_TYPES: |
| 89 | NOTREACHED(); |
[email protected] | 1ce1597 | 2014-03-20 19:25:48 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
sammc | 143f3c5 | 2015-02-13 09:42:38 | [diff] [blame^] | 93 | void ExtensionWebContentsObserver::RenderFrameCreated( |
| 94 | content::RenderFrameHost* render_frame_host) { |
| 95 | RegisterCoreExtensionServices(render_frame_host); |
| 96 | } |
| 97 | |
[email protected] | 0b36507 | 2014-03-22 06:14:18 | [diff] [blame] | 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()))); |
| 103 | } |
| 104 | } |
| 105 | |
[email protected] | 1ce1597 | 2014-03-20 19:25:48 | [diff] [blame] | 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; |
| 111 | |
| 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); |
| 116 | } |
| 117 | |
| 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(); |
| 125 | |
| 126 | if (!site.SchemeIs(kExtensionScheme)) |
| 127 | return std::string(); |
| 128 | |
| 129 | return site.host(); |
| 130 | } |
| 131 | |
| 132 | } // namespace extensions |