Remove linux_chromium_gn_dbg from the chromium CQ.
[chromium-blink-merge.git] / extensions / browser / url_request_util.cc
blobc7321eb5f08de5e9ede91c4de06eb7dd316a97b8
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"
7 #include <string>
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,
22 bool is_incognito,
23 const Extension* extension,
24 InfoMap* extension_info_map,
25 bool* allowed) {
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
31 // manifest file.
32 std::string owner_extension_id;
33 int owner_process_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,
46 resource_path)) {
47 *allowed = true;
48 return true;
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) {
55 *allowed = true;
56 return true;
59 if (!ui::PageTransitionIsWebTriggerable(info->GetPageTransition())) {
60 *allowed = false;
61 return true;
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
66 // checks.
67 if (!extension) {
68 *allowed = true;
69 return true;
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
75 // launchers.
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 "
83 << "hosted app.";
84 *allowed = false;
85 return true;
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)) {
94 *allowed = true;
95 return true;
98 // Couldn't determine if the resource is allowed or not.
99 return false;
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.
106 if (!info)
107 return false;
108 return WebViewRendererState::GetInstance()->IsGuest(info->GetChildID());
111 } // namespace url_request_util
112 } // namespace extensions