1 // Copyright 2013 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 "chrome/browser/extensions/extension_web_contents_observer.h"
7 #include "chrome/browser/extensions/api/messaging/message_service.h"
8 #include "chrome/browser/extensions/extension_service.h"
9 #include "chrome/browser/extensions/extension_system.h"
10 #include "chrome/common/extensions/api/messaging/message.h"
11 #include "chrome/common/extensions/extension_messages.h"
12 #include "chrome/common/url_constants.h"
13 #include "content/public/browser/child_process_security_policy.h"
14 #include "content/public/browser/render_process_host.h"
15 #include "content/public/browser/render_view_host.h"
16 #include "content/public/browser/site_instance.h"
17 #include "content/public/browser/web_contents.h"
18 #include "extensions/browser/extension_registry.h"
19 #include "extensions/browser/view_type_utils.h"
20 #include "extensions/common/constants.h"
22 DEFINE_WEB_CONTENTS_USER_DATA_KEY(extensions::ExtensionWebContentsObserver
);
24 namespace extensions
{
26 ExtensionWebContentsObserver::ExtensionWebContentsObserver(
27 content::WebContents
* web_contents
)
28 : content::WebContentsObserver(web_contents
),
29 browser_context_(web_contents
->GetBrowserContext()) {
32 ExtensionWebContentsObserver::~ExtensionWebContentsObserver() {
35 void ExtensionWebContentsObserver::RenderViewCreated(
36 content::RenderViewHost
* render_view_host
) {
37 render_view_host
->Send(new ExtensionMsg_NotifyRenderViewType(
38 render_view_host
->GetRoutingID(),
39 extensions::GetViewType(web_contents())));
41 const Extension
* extension
= GetExtension(render_view_host
);
45 content::RenderProcessHost
* process
= render_view_host
->GetProcess();
47 // Some extensions use chrome:// URLs.
48 // This is a temporary solution. Replace it with access to chrome-static://
49 // once it is implemented. See: crbug.com/226927.
50 Manifest::Type type
= extension
->GetType();
51 if (type
== Manifest::TYPE_EXTENSION
||
52 type
== Manifest::TYPE_LEGACY_PACKAGED_APP
||
53 (type
== Manifest::TYPE_PLATFORM_APP
&&
54 extension
->location() == Manifest::COMPONENT
)) {
55 content::ChildProcessSecurityPolicy::GetInstance()->GrantScheme(
56 process
->GetID(), chrome::kChromeUIScheme
);
59 // Some extensions use file:// URLs.
60 if (type
== Manifest::TYPE_EXTENSION
||
61 type
== Manifest::TYPE_LEGACY_PACKAGED_APP
) {
62 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(browser_context_
);
63 if (prefs
->AllowFileAccess(extension
->id())) {
64 content::ChildProcessSecurityPolicy::GetInstance()->GrantScheme(
65 process
->GetID(), content::kFileScheme
);
70 case Manifest::TYPE_EXTENSION
:
71 case Manifest::TYPE_USER_SCRIPT
:
72 case Manifest::TYPE_HOSTED_APP
:
73 case Manifest::TYPE_LEGACY_PACKAGED_APP
:
74 case Manifest::TYPE_PLATFORM_APP
:
75 // Always send a Loaded message before ActivateExtension so that
76 // ExtensionDispatcher knows what Extension is active, not just its ID.
77 // This is important for classifying the Extension's JavaScript context
78 // correctly (see ExtensionDispatcher::ClassifyJavaScriptContext).
79 render_view_host
->Send(new ExtensionMsg_Loaded(
80 std::vector
<ExtensionMsg_Loaded_Params
>(
81 1, ExtensionMsg_Loaded_Params(extension
))));
82 render_view_host
->Send(
83 new ExtensionMsg_ActivateExtension(extension
->id()));
86 case Manifest::TYPE_UNKNOWN
:
87 case Manifest::TYPE_THEME
:
88 case Manifest::TYPE_SHARED_MODULE
:
93 bool ExtensionWebContentsObserver::OnMessageReceived(
94 const IPC::Message
& message
) {
96 IPC_BEGIN_MESSAGE_MAP(ExtensionWebContentsObserver
, message
)
97 IPC_MESSAGE_HANDLER(ExtensionHostMsg_PostMessage
, OnPostMessage
)
98 IPC_MESSAGE_UNHANDLED(handled
= false)
103 void ExtensionWebContentsObserver::OnPostMessage(int port_id
,
104 const Message
& message
) {
105 MessageService
* message_service
= MessageService::Get(browser_context_
);
106 if (message_service
) {
107 message_service
->PostMessage(port_id
, message
);
111 const Extension
* ExtensionWebContentsObserver::GetExtension(
112 content::RenderViewHost
* render_view_host
) {
113 // Note that due to ChromeContentBrowserClient::GetEffectiveURL(), hosted apps
114 // (excluding bookmark apps) will have a chrome-extension:// URL for their
115 // site, so we can ignore that wrinkle here.
116 content::SiteInstance
* site_instance
= render_view_host
->GetSiteInstance();
117 const GURL
& site
= site_instance
->GetSiteURL();
119 if (!site
.SchemeIs(kExtensionScheme
))
122 std::string extension_id
= site
.host();
123 ExtensionRegistry
* registry
= ExtensionRegistry::Get(browser_context_
);
125 // Reload the extension if it has crashed.
126 // TODO(yoz): This reload doesn't happen synchronously for unpacked
127 // extensions. It seems to be fast enough, but there is a race.
128 // We should delay loading until the extension has reloaded.
129 if (registry
->GetExtensionById(extension_id
, ExtensionRegistry::TERMINATED
)) {
130 ExtensionSystem::GetForBrowserContext(browser_context_
)->
131 extension_service()->ReloadExtension(extension_id
);
134 // May be null if the extension doesn't exist, for example if somebody typos
135 // a chrome-extension:// URL.
136 return registry
->enabled_extensions().GetByID(extension_id
);
139 } // namespace extensions