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/url_request_util.h"
9 #include "content/public/browser/resource_request_info.h"
10 #include "extensions/browser/guest_view/web_view/web_view_renderer_state.h"
11 #include "extensions/browser/info_map.h"
12 #include "extensions/common/extension.h"
13 #include "extensions/common/manifest_handlers/icons_handler.h"
14 #include "extensions/common/manifest_handlers/web_accessible_resources_info.h"
15 #include "extensions/common/manifest_handlers/webview_info.h"
16 #include "net/url_request/url_request.h"
18 namespace extensions
{
19 namespace url_request_util
{
21 bool AllowCrossRendererResourceLoad(net::URLRequest
* request
,
23 const Extension
* extension
,
24 InfoMap
* extension_info_map
,
26 const content::ResourceRequestInfo
* info
=
27 content::ResourceRequestInfo::ForRequest(request
);
29 // Extensions with webview: allow loading certain resources by guest renderers
30 // with privileged partition IDs as specified in owner's extension the
32 std::string owner_extension_id
;
34 WebViewRendererState::GetInstance()->GetOwnerInfo(
35 info
->GetChildID(), &owner_process_id
, &owner_extension_id
);
36 const Extension
* owner_extension
=
37 extension_info_map
->extensions().GetByID(owner_extension_id
);
38 std::string partition_id
;
39 bool is_guest
= WebViewRendererState::GetInstance()->GetPartitionID(
40 info
->GetChildID(), &partition_id
);
41 std::string resource_path
= request
->url().path();
42 // |owner_extension == extension| needs to be checked because extension
43 // resources should only be accessible to WebViews owned by that extension.
44 if (is_guest
&& owner_extension
== extension
&&
45 WebviewInfo::IsResourceWebviewAccessible(extension
, partition_id
,
51 // If the request is for navigations outside of webviews, then it should be
52 // allowed. The navigation logic in CrossSiteResourceHandler will properly
53 // transfer the navigation to a privileged process before it commits.
54 if (content::IsResourceTypeFrame(info
->GetResourceType()) && !is_guest
) {
59 if (!ui::PageTransitionIsWebTriggerable(info
->GetPageTransition())) {
64 // The following checks require that we have an actual extension object. If we
65 // don't have it, allow the request handling to continue with the rest of the
72 // Disallow loading of packaged resources for hosted apps. We don't allow
73 // hybrid hosted/packaged apps. The one exception is access to icons, since
74 // some extensions want to be able to do things like create their own
76 std::string resource_root_relative_path
=
77 request
->url().path().empty() ? std::string()
78 : request
->url().path().substr(1);
79 if (extension
->is_hosted_app() &&
80 !IconsInfo::GetIcons(extension
)
81 .ContainsPath(resource_root_relative_path
)) {
82 LOG(ERROR
) << "Denying load of " << request
->url().spec() << " from "
88 // Extensions with web_accessible_resources: allow loading by regular
89 // renderers. Since not all subresources are required to be listed in a v2
90 // manifest, we must allow all loads if there are any web accessible
91 // resources. See http://crbug.com/179127.
92 if (extension
->manifest_version() < 2 ||
93 WebAccessibleResourcesInfo::HasWebAccessibleResources(extension
)) {
98 // Couldn't determine if the resource is allowed or not.
102 bool IsWebViewRequest(const net::URLRequest
* request
) {
103 const content::ResourceRequestInfo
* info
=
104 content::ResourceRequestInfo::ForRequest(request
);
105 // |info| can be NULL sometimes: http://crbug.com/370070.
108 return WebViewRendererState::GetInstance()->IsGuest(info
->GetChildID());
111 } // namespace url_request_util
112 } // namespace extensions