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/profiler/scoped_profile.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/task_runner_util.h"
17 #include "chrome/common/chrome_paths.h"
18 #include "chrome/common/extensions/chrome_manifest_url_handlers.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "content/public/browser/resource_request_info.h"
21 #include "extensions/browser/component_extension_resource_manager.h"
22 #include "extensions/browser/extension_protocols.h"
23 #include "extensions/browser/extensions_browser_client.h"
24 #include "extensions/browser/info_map.h"
25 #include "extensions/browser/url_request_util.h"
26 #include "extensions/common/file_util.h"
27 #include "net/base/mime_util.h"
28 #include "net/base/net_errors.h"
29 #include "net/http/http_request_headers.h"
30 #include "net/http/http_response_headers.h"
31 #include "net/http/http_response_info.h"
32 #include "net/url_request/url_request.h"
33 #include "net/url_request/url_request_simple_job.h"
34 #include "ui/base/resource/resource_bundle.h"
36 using content::BrowserThread
;
37 using content::ResourceType
;
38 using extensions::ExtensionsBrowserClient
;
42 // A request for an extension resource in a Chrome .pak file. These are used
43 // by component extensions.
44 class URLRequestResourceBundleJob
: public net::URLRequestSimpleJob
{
46 URLRequestResourceBundleJob(net::URLRequest
* request
,
47 net::NetworkDelegate
* network_delegate
,
48 const base::FilePath
& filename
,
50 const std::string
& content_security_policy
,
51 bool send_cors_header
)
52 : net::URLRequestSimpleJob(request
, network_delegate
),
54 resource_id_(resource_id
),
56 // Leave cache headers out of resource bundle requests.
57 response_info_
.headers
= extensions::BuildHttpHeaders(
58 content_security_policy
, send_cors_header
, base::Time());
61 // Overridden from URLRequestSimpleJob:
62 virtual int GetData(std::string
* mime_type
,
65 const net::CompletionCallback
& callback
) const override
{
66 // TODO(vadimt): Remove ScopedProfile below once crbug.com/422489 is fixed.
67 tracked_objects::ScopedProfile
tracking_profile(
68 FROM_HERE_WITH_EXPLICIT_FUNCTION(
69 "422489 URLRequestResourceBundleJob::GetData"));
71 const ResourceBundle
& rb
= ResourceBundle::GetSharedInstance();
72 *data
= rb
.GetRawDataResource(resource_id_
).as_string();
74 // Add the Content-Length header now that we know the resource length.
75 response_info_
.headers
->AddHeader(
76 base::StringPrintf("%s: %s",
77 net::HttpRequestHeaders::kContentLength
,
78 base::UintToString(data
->size()).c_str()));
80 std::string
* read_mime_type
= new std::string
;
81 bool posted
= base::PostTaskAndReplyWithResult(
82 BrowserThread::GetBlockingPool(),
84 base::Bind(&net::GetMimeTypeFromFile
,
86 base::Unretained(read_mime_type
)),
87 base::Bind(&URLRequestResourceBundleJob::OnMimeTypeRead
,
88 weak_factory_
.GetWeakPtr(),
92 base::Owned(read_mime_type
),
96 return net::ERR_IO_PENDING
;
99 virtual void GetResponseInfo(net::HttpResponseInfo
* info
) override
{
100 *info
= response_info_
;
104 virtual ~URLRequestResourceBundleJob() {}
106 void OnMimeTypeRead(std::string
* out_mime_type
,
107 std::string
* charset
,
109 std::string
* read_mime_type
,
110 const net::CompletionCallback
& callback
,
112 *out_mime_type
= *read_mime_type
;
113 if (StartsWithASCII(*read_mime_type
, "text/", false)) {
114 // All of our HTML files should be UTF-8 and for other resource types
115 // (like images), charset doesn't matter.
116 DCHECK(base::IsStringUTF8(*data
));
119 int result
= read_result
? net::OK
: net::ERR_INVALID_URL
;
120 callback
.Run(result
);
123 // We need the filename of the resource to determine the mime type.
124 base::FilePath filename_
;
126 // The resource bundle id to load.
129 net::HttpResponseInfo response_info_
;
131 mutable base::WeakPtrFactory
<URLRequestResourceBundleJob
> weak_factory_
;
136 namespace extensions
{
137 namespace chrome_url_request_util
{
139 bool AllowCrossRendererResourceLoad(net::URLRequest
* request
,
141 const Extension
* extension
,
142 InfoMap
* extension_info_map
,
144 if (url_request_util::AllowCrossRendererResourceLoad(
145 request
, is_incognito
, extension
, extension_info_map
, allowed
)) {
149 // If there aren't any explicitly marked web accessible resources, the
150 // load should be allowed only if it is by DevTools. A close approximation is
151 // checking if the extension contains a DevTools page.
152 if (!chrome_manifest_urls::GetDevToolsPage(extension
).is_empty()) {
157 // Couldn't determine if the resource is allowed or not.
161 net::URLRequestJob
* MaybeCreateURLRequestResourceBundleJob(
162 net::URLRequest
* request
,
163 net::NetworkDelegate
* network_delegate
,
164 const base::FilePath
& directory_path
,
165 const std::string
& content_security_policy
,
166 bool send_cors_header
) {
167 base::FilePath resources_path
;
168 base::FilePath relative_path
;
169 // Try to load extension resources from chrome resource file if
170 // directory_path is a descendant of resources_path. resources_path
171 // corresponds to src/chrome/browser/resources in source tree.
172 if (PathService::Get(chrome::DIR_RESOURCES
, &resources_path
) &&
173 // Since component extension resources are included in
174 // component_extension_resources.pak file in resources_path, calculate
175 // extension relative path against resources_path.
176 resources_path
.AppendRelativePath(directory_path
, &relative_path
)) {
177 base::FilePath request_path
=
178 extensions::file_util::ExtensionURLToRelativeFilePath(request
->url());
180 if (ExtensionsBrowserClient::Get()
181 ->GetComponentExtensionResourceManager()
182 ->IsComponentExtensionResource(
183 directory_path
, request_path
, &resource_id
)) {
184 relative_path
= relative_path
.Append(request_path
);
185 relative_path
= relative_path
.NormalizePathSeparators();
186 return new URLRequestResourceBundleJob(request
,
190 content_security_policy
,
197 } // namespace chrome_url_request_util
198 } // namespace extensions