Add more checks to investigate SupervisedUserPrefStore crash at startup.
[chromium-blink-merge.git] / extensions / browser / extension_web_contents_observer.cc
blob020cc3257be491916530d1676ae9d89e1b23fd30
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.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 const Extension* extension = GetExtension(render_view_host);
36 if (!extension)
37 return;
39 content::RenderProcessHost* process = render_view_host->GetProcess();
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);
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(
59 process->GetID(), url::kFileScheme);
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).
73 // We also have to include the tab-specific permissions here, since it's
74 // an extension process.
75 render_view_host->Send(
76 new ExtensionMsg_Loaded(std::vector<ExtensionMsg_Loaded_Params>(
77 1, ExtensionMsg_Loaded_Params(
78 extension, true /* include tab permissions */))));
79 render_view_host->Send(
80 new ExtensionMsg_ActivateExtension(extension->id()));
81 break;
83 case Manifest::TYPE_UNKNOWN:
84 case Manifest::TYPE_THEME:
85 case Manifest::TYPE_SHARED_MODULE:
86 break;
88 case Manifest::NUM_LOAD_TYPES:
89 NOTREACHED();
93 void ExtensionWebContentsObserver::RenderFrameCreated(
94 content::RenderFrameHost* render_frame_host) {
95 RegisterCoreExtensionServices(render_frame_host);
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