blob: 020cc3257be491916530d1676ae9d89e1b23fd30 [file] [log] [blame]
[email protected]1ce15972014-03-20 19:25:481// 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"
sammc143f3c52015-02-13 09:42:3815#include "extensions/browser/mojo/service_registration.h"
[email protected]1ce15972014-03-20 19:25:4816#include "extensions/browser/view_type_utils.h"
17#include "extensions/common/constants.h"
18#include "extensions/common/extension_messages.h"
19
20namespace extensions {
21
22ExtensionWebContentsObserver::ExtensionWebContentsObserver(
23 content::WebContents* web_contents)
24 : content::WebContentsObserver(web_contents),
[email protected]0b365072014-03-22 06:14:1825 browser_context_(web_contents->GetBrowserContext()) {
26 NotifyRenderViewType(web_contents->GetRenderViewHost());
27}
[email protected]1ce15972014-03-20 19:25:4828
29ExtensionWebContentsObserver::~ExtensionWebContentsObserver() {}
30
31void ExtensionWebContentsObserver::RenderViewCreated(
32 content::RenderViewHost* render_view_host) {
[email protected]0b365072014-03-22 06:14:1833 NotifyRenderViewType(render_view_host);
[email protected]1ce15972014-03-20 19:25:4834
[email protected]1ce15972014-03-20 19:25:4835 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]cca6f392014-05-28 21:32:2659 process->GetID(), url::kFileScheme);
[email protected]1ce15972014-03-20 19:25:4860 }
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.croninc491dbf2015-02-07 00:31:5573 // We also have to include the tab-specific permissions here, since it's
74 // an extension process.
[email protected]1ce15972014-03-20 19:25:4875 render_view_host->Send(
76 new ExtensionMsg_Loaded(std::vector<ExtensionMsg_Loaded_Params>(
rdevlin.croninc491dbf2015-02-07 00:31:5577 1, ExtensionMsg_Loaded_Params(
78 extension, true /* include tab permissions */))));
[email protected]1ce15972014-03-20 19:25:4879 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]180d4e92014-05-22 15:35:1687
88 case Manifest::NUM_LOAD_TYPES:
89 NOTREACHED();
[email protected]1ce15972014-03-20 19:25:4890 }
91}
92
sammc143f3c52015-02-13 09:42:3893void ExtensionWebContentsObserver::RenderFrameCreated(
94 content::RenderFrameHost* render_frame_host) {
95 RegisterCoreExtensionServices(render_frame_host);
96}
97
[email protected]0b365072014-03-22 06:14:1898void 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]1ce15972014-03-20 19:25:48106const 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
119std::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