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 "chrome/browser/extensions/chrome_url_request_util.h"
9 #include "base/files/file_path.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/path_service.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/task_runner_util.h"
16 #include "chrome/common/chrome_paths.h"
17 #include "chrome/common/extensions/chrome_manifest_url_handlers.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/resource_request_info.h"
20 #include "extensions/browser/component_extension_resource_manager.h"
21 #include "extensions/browser/extension_protocols.h"
22 #include "extensions/browser/extensions_browser_client.h"
23 #include "extensions/browser/info_map.h"
24 #include "extensions/browser/url_request_util.h"
25 #include "extensions/common/file_util.h"
26 #include "net/base/mime_util.h"
27 #include "net/base/net_errors.h"
28 #include "net/http/http_request_headers.h"
29 #include "net/http/http_response_headers.h"
30 #include "net/http/http_response_info.h"
31 #include "net/url_request/url_request.h"
32 #include "net/url_request/url_request_simple_job.h"
33 #include "ui/base/resource/resource_bundle.h"
35 using content::BrowserThread
;
36 using content::ResourceType
;
37 using extensions::ExtensionsBrowserClient
;
41 // A request for an extension resource in a Chrome .pak file. These are used
42 // by component extensions.
43 class URLRequestResourceBundleJob
: public net::URLRequestSimpleJob
{
45 URLRequestResourceBundleJob(net::URLRequest
* request
,
46 net::NetworkDelegate
* network_delegate
,
47 const base::FilePath
& filename
,
49 const std::string
& content_security_policy
,
50 bool send_cors_header
)
51 : net::URLRequestSimpleJob(request
, network_delegate
),
53 resource_id_(resource_id
),
55 // Leave cache headers out of resource bundle requests.
56 response_info_
.headers
= extensions::BuildHttpHeaders(
57 content_security_policy
, send_cors_header
, base::Time());
60 // Overridden from URLRequestSimpleJob:
61 int GetRefCountedData(
62 std::string
* mime_type
,
64 scoped_refptr
<base::RefCountedMemory
>* data
,
65 const net::CompletionCallback
& callback
) const override
{
66 const ResourceBundle
& rb
= ResourceBundle::GetSharedInstance();
67 *data
= rb
.LoadDataResourceBytes(resource_id_
);
69 // Add the Content-Length header now that we know the resource length.
70 response_info_
.headers
->AddHeader(
71 base::StringPrintf("%s: %s", net::HttpRequestHeaders::kContentLength
,
72 base::UintToString((*data
)->size()).c_str()));
74 std::string
* read_mime_type
= new std::string
;
75 bool posted
= base::PostTaskAndReplyWithResult(
76 BrowserThread::GetBlockingPool(), FROM_HERE
,
77 base::Bind(&net::GetMimeTypeFromFile
, filename_
,
78 base::Unretained(read_mime_type
)),
79 base::Bind(&URLRequestResourceBundleJob::OnMimeTypeRead
,
80 weak_factory_
.GetWeakPtr(), mime_type
, charset
, *data
,
81 base::Owned(read_mime_type
), callback
));
84 return net::ERR_IO_PENDING
;
87 void GetResponseInfo(net::HttpResponseInfo
* info
) override
{
88 *info
= response_info_
;
92 ~URLRequestResourceBundleJob() override
{}
94 void OnMimeTypeRead(std::string
* out_mime_type
,
96 scoped_refptr
<base::RefCountedMemory
> data
,
97 std::string
* read_mime_type
,
98 const net::CompletionCallback
& callback
,
100 *out_mime_type
= *read_mime_type
;
101 if (StartsWithASCII(*read_mime_type
, "text/", false)) {
102 // All of our HTML files should be UTF-8 and for other resource types
103 // (like images), charset doesn't matter.
104 DCHECK(base::IsStringUTF8(base::StringPiece(
105 reinterpret_cast<const char*>(data
->front()), data
->size())));
108 int result
= read_result
? net::OK
: net::ERR_INVALID_URL
;
109 callback
.Run(result
);
112 // We need the filename of the resource to determine the mime type.
113 base::FilePath filename_
;
115 // The resource bundle id to load.
118 net::HttpResponseInfo response_info_
;
120 mutable base::WeakPtrFactory
<URLRequestResourceBundleJob
> weak_factory_
;
125 namespace extensions
{
126 namespace chrome_url_request_util
{
128 bool AllowCrossRendererResourceLoad(net::URLRequest
* request
,
130 const Extension
* extension
,
131 InfoMap
* extension_info_map
,
133 if (url_request_util::AllowCrossRendererResourceLoad(
134 request
, is_incognito
, extension
, extension_info_map
, allowed
)) {
138 // If there aren't any explicitly marked web accessible resources, the
139 // load should be allowed only if it is by DevTools. A close approximation is
140 // checking if the extension contains a DevTools page.
141 if (!chrome_manifest_urls::GetDevToolsPage(extension
).is_empty()) {
146 // Couldn't determine if the resource is allowed or not.
150 net::URLRequestJob
* MaybeCreateURLRequestResourceBundleJob(
151 net::URLRequest
* request
,
152 net::NetworkDelegate
* network_delegate
,
153 const base::FilePath
& directory_path
,
154 const std::string
& content_security_policy
,
155 bool send_cors_header
) {
156 base::FilePath resources_path
;
157 base::FilePath relative_path
;
158 // Try to load extension resources from chrome resource file if
159 // directory_path is a descendant of resources_path. resources_path
160 // corresponds to src/chrome/browser/resources in source tree.
161 if (PathService::Get(chrome::DIR_RESOURCES
, &resources_path
) &&
162 // Since component extension resources are included in
163 // component_extension_resources.pak file in resources_path, calculate
164 // extension relative path against resources_path.
165 resources_path
.AppendRelativePath(directory_path
, &relative_path
)) {
166 base::FilePath request_path
=
167 extensions::file_util::ExtensionURLToRelativeFilePath(request
->url());
169 if (ExtensionsBrowserClient::Get()
170 ->GetComponentExtensionResourceManager()
171 ->IsComponentExtensionResource(
172 directory_path
, request_path
, &resource_id
)) {
173 relative_path
= relative_path
.Append(request_path
);
174 relative_path
= relative_path
.NormalizePathSeparators();
175 return new URLRequestResourceBundleJob(request
,
179 content_security_policy
,
186 } // namespace chrome_url_request_util
187 } // namespace extensions