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/error_console/error_console.h"
9 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/common/extensions/api/messaging/message.h"
11 #include "chrome/common/extensions/extension_messages.h"
12 #include "chrome/common/render_messages.h"
13 #include "chrome/common/url_constants.h"
14 #include "content/public/browser/child_process_security_policy.h"
15 #include "content/public/browser/render_process_host.h"
16 #include "content/public/browser/render_view_host.h"
17 #include "content/public/browser/site_instance.h"
18 #include "content/public/browser/web_contents.h"
19 #include "extensions/browser/extension_registry.h"
20 #include "extensions/browser/extension_system.h"
21 #include "extensions/browser/view_type_utils.h"
22 #include "extensions/common/constants.h"
23 #include "extensions/common/extension_urls.h"
25 DEFINE_WEB_CONTENTS_USER_DATA_KEY(extensions::ExtensionWebContentsObserver
);
27 namespace extensions
{
29 ExtensionWebContentsObserver::ExtensionWebContentsObserver(
30 content::WebContents
* web_contents
)
31 : content::WebContentsObserver(web_contents
),
32 browser_context_(web_contents
->GetBrowserContext()) {
35 ExtensionWebContentsObserver::~ExtensionWebContentsObserver() {
38 void ExtensionWebContentsObserver::RenderViewCreated(
39 content::RenderViewHost
* render_view_host
) {
40 render_view_host
->Send(new ExtensionMsg_NotifyRenderViewType(
41 render_view_host
->GetRoutingID(),
42 GetViewType(web_contents())));
44 const Extension
* extension
= GetExtension(render_view_host
);
48 content::RenderProcessHost
* process
= render_view_host
->GetProcess();
50 // Some extensions use chrome:// URLs.
51 // This is a temporary solution. Replace it with access to chrome-static://
52 // once it is implemented. See: crbug.com/226927.
53 Manifest::Type type
= extension
->GetType();
54 if (type
== Manifest::TYPE_EXTENSION
||
55 type
== Manifest::TYPE_LEGACY_PACKAGED_APP
||
56 (type
== Manifest::TYPE_PLATFORM_APP
&&
57 extension
->location() == Manifest::COMPONENT
)) {
58 content::ChildProcessSecurityPolicy::GetInstance()->GrantScheme(
59 process
->GetID(), content::kChromeUIScheme
);
62 // Some extensions use file:// URLs.
63 if (type
== Manifest::TYPE_EXTENSION
||
64 type
== Manifest::TYPE_LEGACY_PACKAGED_APP
) {
65 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(browser_context_
);
66 if (prefs
->AllowFileAccess(extension
->id())) {
67 content::ChildProcessSecurityPolicy::GetInstance()->GrantScheme(
68 process
->GetID(), content::kFileScheme
);
73 case Manifest::TYPE_EXTENSION
:
74 case Manifest::TYPE_USER_SCRIPT
:
75 case Manifest::TYPE_HOSTED_APP
:
76 case Manifest::TYPE_LEGACY_PACKAGED_APP
:
77 case Manifest::TYPE_PLATFORM_APP
:
78 // Always send a Loaded message before ActivateExtension so that
79 // ExtensionDispatcher knows what Extension is active, not just its ID.
80 // This is important for classifying the Extension's JavaScript context
81 // correctly (see ExtensionDispatcher::ClassifyJavaScriptContext).
82 render_view_host
->Send(new ExtensionMsg_Loaded(
83 std::vector
<ExtensionMsg_Loaded_Params
>(
84 1, ExtensionMsg_Loaded_Params(extension
))));
85 render_view_host
->Send(
86 new ExtensionMsg_ActivateExtension(extension
->id()));
89 case Manifest::TYPE_UNKNOWN
:
90 case Manifest::TYPE_THEME
:
91 case Manifest::TYPE_SHARED_MODULE
:
96 bool ExtensionWebContentsObserver::OnMessageReceived(
97 const IPC::Message
& message
) {
99 IPC_BEGIN_MESSAGE_MAP(ExtensionWebContentsObserver
, message
)
100 IPC_MESSAGE_HANDLER(ExtensionHostMsg_PostMessage
, OnPostMessage
)
101 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DetailedConsoleMessageAdded
,
102 OnDetailedConsoleMessageAdded
)
103 IPC_MESSAGE_UNHANDLED(handled
= false)
104 IPC_END_MESSAGE_MAP()
108 void ExtensionWebContentsObserver::OnDetailedConsoleMessageAdded(
109 const base::string16
& message
,
110 const base::string16
& source
,
111 const StackTrace
& stack_trace
,
112 int32 severity_level
) {
113 if (IsSourceFromAnExtension(source
)) {
114 content::RenderViewHost
* rvh
= web_contents()->GetRenderViewHost();
115 ErrorConsole::Get(Profile::FromBrowserContext(browser_context_
))->
117 scoped_ptr
<ExtensionError
>(new RuntimeError(
119 browser_context_
->IsOffTheRecord(),
124 web_contents()->GetLastCommittedURL() : GURL::EmptyGURL(),
125 static_cast<logging::LogSeverity
>(severity_level
),
127 rvh
->GetProcess()->GetID())));
131 void ExtensionWebContentsObserver::OnPostMessage(int port_id
,
132 const Message
& message
) {
133 MessageService
* message_service
= MessageService::Get(browser_context_
);
134 if (message_service
) {
135 message_service
->PostMessage(port_id
, message
);
139 const Extension
* ExtensionWebContentsObserver::GetExtension(
140 content::RenderViewHost
* render_view_host
) {
141 // Note that due to ChromeContentBrowserClient::GetEffectiveURL(), hosted apps
142 // (excluding bookmark apps) will have a chrome-extension:// URL for their
143 // site, so we can ignore that wrinkle here.
144 content::SiteInstance
* site_instance
= render_view_host
->GetSiteInstance();
145 const GURL
& site
= site_instance
->GetSiteURL();
147 if (!site
.SchemeIs(kExtensionScheme
))
150 std::string extension_id
= site
.host();
151 ExtensionRegistry
* registry
= ExtensionRegistry::Get(browser_context_
);
153 // Reload the extension if it has crashed.
154 // TODO(yoz): This reload doesn't happen synchronously for unpacked
155 // extensions. It seems to be fast enough, but there is a race.
156 // We should delay loading until the extension has reloaded.
157 if (registry
->GetExtensionById(extension_id
, ExtensionRegistry::TERMINATED
)) {
158 ExtensionSystem::Get(browser_context_
)->
159 extension_service()->ReloadExtension(extension_id
);
162 // May be null if the extension doesn't exist, for example if somebody typos
163 // a chrome-extension:// URL.
164 return registry
->enabled_extensions().GetByID(extension_id
);
167 } // namespace extensions