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 "ios/web/webui/url_data_manager_ios_backend.h"
9 #include "base/basictypes.h"
10 #include "base/bind.h"
11 #include "base/command_line.h"
12 #include "base/compiler_specific.h"
13 #include "base/debug/alias.h"
14 #include "base/lazy_instance.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/ref_counted_memory.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/message_loop/message_loop.h"
19 #include "base/strings/string_util.h"
20 #include "base/strings/stringprintf.h"
21 #include "base/trace_event/trace_event.h"
22 #include "ios/web/public/browser_state.h"
23 #include "ios/web/public/web_client.h"
24 #include "ios/web/public/web_thread.h"
25 #include "ios/web/webui/shared_resources_data_source_ios.h"
26 #include "ios/web/webui/url_data_source_ios_impl.h"
27 #include "net/base/io_buffer.h"
28 #include "net/base/net_errors.h"
29 #include "net/http/http_response_headers.h"
30 #include "net/http/http_status_code.h"
31 #include "net/url_request/url_request.h"
32 #include "net/url_request/url_request_context.h"
33 #include "net/url_request/url_request_job.h"
34 #include "net/url_request/url_request_job_factory.h"
35 #include "url/url_util.h"
43 // TODO(tsepez) remove unsafe-eval when bidichecker_packaged.js fixed.
44 const char kChromeURLContentSecurityPolicyHeaderBase
[] =
45 "Content-Security-Policy: script-src chrome://resources "
46 "'self' 'unsafe-eval'; ";
48 const char kChromeURLXFrameOptionsHeader
[] = "X-Frame-Options: DENY";
50 bool SchemeIsInSchemes(const std::string
& scheme
,
51 const std::vector
<std::string
>& schemes
) {
52 return std::find(schemes
.begin(), schemes
.end(), scheme
) != schemes
.end();
55 // Returns whether |url| passes some sanity checks and is a valid GURL.
56 bool CheckURLIsValid(const GURL
& url
) {
57 std::vector
<std::string
> additional_schemes
;
58 DCHECK(GetWebClient()->IsAppSpecificURL(url
) ||
59 (GetWebClient()->GetAdditionalWebUISchemes(&additional_schemes
),
60 SchemeIsInSchemes(url
.scheme(), additional_schemes
)));
62 if (!url
.is_valid()) {
70 // Parse |url| to get the path which will be used to resolve the request. The
71 // path is the remaining portion after the scheme and hostname.
72 void URLToRequestPath(const GURL
& url
, std::string
* path
) {
73 const std::string
& spec
= url
.possibly_invalid_spec();
74 const url::Parsed
& parsed
= url
.parsed_for_possibly_invalid_spec();
75 // + 1 to skip the slash at the beginning of the path.
76 int offset
= parsed
.CountCharactersBefore(url::Parsed::PATH
, false) + 1;
78 if (offset
< static_cast<int>(spec
.size()))
79 path
->assign(spec
.substr(offset
));
84 // URLRequestChromeJob is a net::URLRequestJob that manages running
85 // chrome-internal resource requests asynchronously.
86 // It hands off URL requests to ChromeURLDataManagerIOS, which asynchronously
87 // calls back once the data is available.
88 class URLRequestChromeJob
: public net::URLRequestJob
{
90 // |is_incognito| set when job is generated from an incognito profile.
91 URLRequestChromeJob(net::URLRequest
* request
,
92 net::NetworkDelegate
* network_delegate
,
93 BrowserState
* browser_state
,
96 // net::URLRequestJob implementation.
97 void Start() override
;
99 bool ReadRawData(net::IOBuffer
* buf
, int buf_size
, int* bytes_read
) override
;
100 bool GetMimeType(std::string
* mime_type
) const override
;
101 int GetResponseCode() const override
;
102 void GetResponseInfo(net::HttpResponseInfo
* info
) override
;
104 // Used to notify that the requested data's |mime_type| is ready.
105 void MimeTypeAvailable(const std::string
& mime_type
);
107 // Called by ChromeURLDataManagerIOS to notify us that the data blob is ready
109 void DataAvailable(base::RefCountedMemory
* bytes
);
111 void set_mime_type(const std::string
& mime_type
) { mime_type_
= mime_type
; }
113 void set_allow_caching(bool allow_caching
) { allow_caching_
= allow_caching
; }
115 void set_add_content_security_policy(bool add_content_security_policy
) {
116 add_content_security_policy_
= add_content_security_policy
;
119 void set_content_security_policy_object_source(const std::string
& data
) {
120 content_security_policy_object_source_
= data
;
123 void set_content_security_policy_frame_source(const std::string
& data
) {
124 content_security_policy_frame_source_
= data
;
127 void set_deny_xframe_options(bool deny_xframe_options
) {
128 deny_xframe_options_
= deny_xframe_options
;
131 void set_send_content_type_header(bool send_content_type_header
) {
132 send_content_type_header_
= send_content_type_header
;
135 // Returns true when job was generated from an incognito profile.
136 bool is_incognito() const { return is_incognito_
; }
139 friend class URLDataManagerIOSBackend
;
141 ~URLRequestChromeJob() override
;
143 // Do the actual copy from data_ (the data we're serving) into |buf|.
144 // Separate from ReadRawData so we can handle async I/O.
145 void CompleteRead(net::IOBuffer
* buf
, int buf_size
, int* bytes_read
);
147 // The actual data we're serving. NULL until it's been fetched.
148 scoped_refptr
<base::RefCountedMemory
> data_
;
149 // The current offset into the data that we're handing off to our
150 // callers via the Read interfaces.
153 // For async reads, we keep around a pointer to the buffer that
154 // we're reading into.
155 scoped_refptr
<net::IOBuffer
> pending_buf_
;
156 int pending_buf_size_
;
157 std::string mime_type_
;
159 // If true, set a header in the response to prevent it from being cached.
162 // If true, set the Content Security Policy (CSP) header.
163 bool add_content_security_policy_
;
165 // These are used with the CSP.
166 std::string content_security_policy_object_source_
;
167 std::string content_security_policy_frame_source_
;
169 // If true, sets the "X-Frame-Options: DENY" header.
170 bool deny_xframe_options_
;
172 // If true, sets the "Content-Type: <mime-type>" header.
173 bool send_content_type_header_
;
175 // True when job is generated from an incognito profile.
176 const bool is_incognito_
;
178 // The BrowserState with which this job is associated.
179 BrowserState
* browser_state_
;
181 // The backend is owned by the BrowserState and always outlives us. It is
182 // obtained from the BrowserState on the IO thread.
183 URLDataManagerIOSBackend
* backend_
;
185 base::WeakPtrFactory
<URLRequestChromeJob
> weak_factory_
;
187 DISALLOW_COPY_AND_ASSIGN(URLRequestChromeJob
);
190 URLRequestChromeJob::URLRequestChromeJob(
191 net::URLRequest
* request
,
192 net::NetworkDelegate
* network_delegate
,
193 BrowserState
* browser_state
,
195 : net::URLRequestJob(request
, network_delegate
),
197 pending_buf_size_(0),
198 allow_caching_(true),
199 add_content_security_policy_(true),
200 content_security_policy_object_source_("object-src 'none';"),
201 content_security_policy_frame_source_("frame-src 'none';"),
202 deny_xframe_options_(true),
203 send_content_type_header_(false),
204 is_incognito_(is_incognito
),
205 browser_state_(browser_state
),
207 weak_factory_(this) {
208 DCHECK(browser_state_
);
211 URLRequestChromeJob::~URLRequestChromeJob() {
213 CHECK(!backend_
->HasPendingJob(this));
217 void URLRequestChromeJob::Start() {
218 TRACE_EVENT_ASYNC_BEGIN1("browser",
219 "DataManager:Request",
222 request_
->url().possibly_invalid_spec());
226 DCHECK(browser_state_
);
228 // Obtain the URLDataManagerIOSBackend instance that is associated with
229 // |browser_state_|. Note that this *must* be done on the IO thread.
230 backend_
= browser_state_
->GetURLDataManagerIOSBackendOnIOThread();
233 if (!backend_
->StartRequest(request_
, this)) {
234 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED
,
235 net::ERR_INVALID_URL
));
239 void URLRequestChromeJob::Kill() {
241 backend_
->RemoveRequest(this);
244 bool URLRequestChromeJob::GetMimeType(std::string
* mime_type
) const {
245 *mime_type
= mime_type_
;
246 return !mime_type_
.empty();
249 int URLRequestChromeJob::GetResponseCode() const {
253 void URLRequestChromeJob::GetResponseInfo(net::HttpResponseInfo
* info
) {
254 DCHECK(!info
->headers
.get());
255 // Set the headers so that requests serviced by ChromeURLDataManagerIOS
256 // return a status code of 200. Without this they return a 0, which makes the
257 // status indistiguishable from other error types. Instant relies on getting
259 info
->headers
= new net::HttpResponseHeaders("HTTP/1.1 200 OK");
261 // Determine the least-privileged content security policy header, if any,
262 // that is compatible with a given WebUI URL, and append it to the existing
264 if (add_content_security_policy_
) {
265 std::string base
= kChromeURLContentSecurityPolicyHeaderBase
;
266 base
.append(content_security_policy_object_source_
);
267 base
.append(content_security_policy_frame_source_
);
268 info
->headers
->AddHeader(base
);
271 if (deny_xframe_options_
)
272 info
->headers
->AddHeader(kChromeURLXFrameOptionsHeader
);
275 info
->headers
->AddHeader("Cache-Control: no-cache");
277 if (send_content_type_header_
&& !mime_type_
.empty()) {
278 std::string content_type
= base::StringPrintf(
279 "%s:%s", net::HttpRequestHeaders::kContentType
, mime_type_
.c_str());
280 info
->headers
->AddHeader(content_type
);
284 void URLRequestChromeJob::MimeTypeAvailable(const std::string
& mime_type
) {
285 set_mime_type(mime_type
);
286 NotifyHeadersComplete();
289 void URLRequestChromeJob::DataAvailable(base::RefCountedMemory
* bytes
) {
290 TRACE_EVENT_ASYNC_END0("browser", "DataManager:Request", this);
292 // The request completed, and we have all the data.
293 // Clear any IO pending status.
294 SetStatus(net::URLRequestStatus());
298 if (pending_buf_
.get()) {
299 CHECK(pending_buf_
->data());
300 CompleteRead(pending_buf_
.get(), pending_buf_size_
, &bytes_read
);
302 NotifyReadComplete(bytes_read
);
305 // The request failed.
307 net::URLRequestStatus(net::URLRequestStatus::FAILED
, net::ERR_FAILED
));
311 bool URLRequestChromeJob::ReadRawData(net::IOBuffer
* buf
,
315 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING
, 0));
316 DCHECK(!pending_buf_
.get());
319 pending_buf_size_
= buf_size
;
320 return false; // Tell the caller we're still waiting for data.
323 // Otherwise, the data is available.
324 CompleteRead(buf
, buf_size
, bytes_read
);
328 void URLRequestChromeJob::CompleteRead(net::IOBuffer
* buf
,
331 // http://crbug.com/373841
333 base::strlcpy(url_buf
, request_
->url().spec().c_str(), arraysize(url_buf
));
334 base::debug::Alias(url_buf
);
336 int remaining
= static_cast<int>(data_
->size()) - data_offset_
;
337 if (buf_size
> remaining
)
338 buf_size
= remaining
;
340 memcpy(buf
->data(), data_
->front() + data_offset_
, buf_size
);
341 data_offset_
+= buf_size
;
343 *bytes_read
= buf_size
;
348 // Gets mime type for data that is available from |source| by |path|.
349 // After that, notifies |job| that mime type is available. This method
350 // should be called on the UI thread, but notification is performed on
352 void GetMimeTypeOnUI(URLDataSourceIOSImpl
* source
,
353 const std::string
& path
,
354 const base::WeakPtr
<URLRequestChromeJob
>& job
) {
355 DCHECK_CURRENTLY_ON_WEB_THREAD(WebThread::UI
);
356 std::string mime_type
= source
->source()->GetMimeType(path
);
358 WebThread::IO
, FROM_HERE
,
359 base::Bind(&URLRequestChromeJob::MimeTypeAvailable
, job
, mime_type
));
366 class ChromeProtocolHandler
367 : public net::URLRequestJobFactory::ProtocolHandler
{
369 // |is_incognito| should be set for incognito profiles.
370 ChromeProtocolHandler(BrowserState
* browser_state
,
372 : browser_state_(browser_state
), is_incognito_(is_incognito
) {}
373 ~ChromeProtocolHandler() override
{}
375 net::URLRequestJob
* MaybeCreateJob(
376 net::URLRequest
* request
,
377 net::NetworkDelegate
* network_delegate
) const override
{
380 return new URLRequestChromeJob(
381 request
, network_delegate
, browser_state_
, is_incognito_
);
384 bool IsSafeRedirectTarget(const GURL
& location
) const override
{
389 BrowserState
* browser_state_
;
391 // True when generated from an incognito profile.
392 const bool is_incognito_
;
394 DISALLOW_COPY_AND_ASSIGN(ChromeProtocolHandler
);
399 URLDataManagerIOSBackend::URLDataManagerIOSBackend() : next_request_id_(0) {
400 URLDataSourceIOS
* shared_source
= new SharedResourcesDataSourceIOS();
401 URLDataSourceIOSImpl
* source_impl
=
402 new URLDataSourceIOSImpl(shared_source
->GetSource(), shared_source
);
403 AddDataSource(source_impl
);
406 URLDataManagerIOSBackend::~URLDataManagerIOSBackend() {
407 for (DataSourceMap::iterator i
= data_sources_
.begin();
408 i
!= data_sources_
.end();
410 i
->second
->backend_
= NULL
;
412 data_sources_
.clear();
416 net::URLRequestJobFactory::ProtocolHandler
*
417 URLDataManagerIOSBackend::CreateProtocolHandler(
418 BrowserState
* browser_state
) {
419 DCHECK(browser_state
);
420 return new ChromeProtocolHandler(browser_state
,
421 browser_state
->IsOffTheRecord());
424 void URLDataManagerIOSBackend::AddDataSource(URLDataSourceIOSImpl
* source
) {
425 DCHECK_CURRENTLY_ON_WEB_THREAD(WebThread::IO
);
426 DataSourceMap::iterator i
= data_sources_
.find(source
->source_name());
427 if (i
!= data_sources_
.end()) {
428 if (!source
->source()->ShouldReplaceExistingSource())
430 i
->second
->backend_
= NULL
;
432 data_sources_
[source
->source_name()] = source
;
433 source
->backend_
= this;
436 bool URLDataManagerIOSBackend::HasPendingJob(URLRequestChromeJob
* job
) const {
437 for (PendingRequestMap::const_iterator i
= pending_requests_
.begin();
438 i
!= pending_requests_
.end();
440 if (i
->second
== job
)
446 bool URLDataManagerIOSBackend::StartRequest(const net::URLRequest
* request
,
447 URLRequestChromeJob
* job
) {
448 if (!CheckURLIsValid(request
->url()))
451 URLDataSourceIOSImpl
* source
= GetDataSourceFromURL(request
->url());
455 if (!source
->source()->ShouldServiceRequest(request
))
459 URLToRequestPath(request
->url(), &path
);
460 source
->source()->WillServiceRequest(request
, &path
);
462 // Save this request so we know where to send the data.
463 RequestID request_id
= next_request_id_
++;
464 pending_requests_
.insert(std::make_pair(request_id
, job
));
466 job
->set_allow_caching(source
->source()->AllowCaching());
467 job
->set_add_content_security_policy(true);
468 job
->set_content_security_policy_object_source(
469 source
->source()->GetContentSecurityPolicyObjectSrc());
470 job
->set_content_security_policy_frame_source("frame-src 'none';");
471 job
->set_deny_xframe_options(source
->source()->ShouldDenyXFrameOptions());
472 job
->set_send_content_type_header(false);
474 // Forward along the request to the data source.
475 // URLRequestChromeJob should receive mime type before data. This
476 // is guaranteed because request for mime type is placed in the
477 // message loop before request for data. And correspondingly their
478 // replies are put on the IO thread in the same order.
479 base::MessageLoop
* target_message_loop
=
480 web::WebThread::UnsafeGetMessageLoopForThread(web::WebThread::UI
);
481 target_message_loop
->PostTask(
483 base::Bind(&GetMimeTypeOnUI
,
484 scoped_refptr
<URLDataSourceIOSImpl
>(source
),
486 job
->weak_factory_
.GetWeakPtr()));
488 target_message_loop
->PostTask(
490 base::Bind(&URLDataManagerIOSBackend::CallStartRequest
,
491 make_scoped_refptr(source
),
497 URLDataSourceIOSImpl
* URLDataManagerIOSBackend::GetDataSourceFromURL(
499 // The input usually looks like: chrome://source_name/extra_bits?foo
500 // so do a lookup using the host of the URL.
501 DataSourceMap::iterator i
= data_sources_
.find(url
.host());
502 if (i
!= data_sources_
.end())
503 return i
->second
.get();
505 // No match using the host of the URL, so do a lookup using the scheme for
506 // URLs on the form source_name://extra_bits/foo .
507 i
= data_sources_
.find(url
.scheme() + "://");
508 if (i
!= data_sources_
.end())
509 return i
->second
.get();
511 // No matches found, so give up.
515 void URLDataManagerIOSBackend::CallStartRequest(
516 scoped_refptr
<URLDataSourceIOSImpl
> source
,
517 const std::string
& path
,
519 source
->source()->StartDataRequest(
521 base::Bind(&URLDataSourceIOSImpl::SendResponse
, source
, request_id
));
524 void URLDataManagerIOSBackend::RemoveRequest(URLRequestChromeJob
* job
) {
525 // Remove the request from our list of pending requests.
526 // If/when the source sends the data that was requested, the data will just
528 for (PendingRequestMap::iterator i
= pending_requests_
.begin();
529 i
!= pending_requests_
.end();
531 if (i
->second
== job
) {
532 pending_requests_
.erase(i
);
538 void URLDataManagerIOSBackend::DataAvailable(RequestID request_id
,
539 base::RefCountedMemory
* bytes
) {
540 // Forward this data on to the pending net::URLRequest, if it exists.
541 PendingRequestMap::iterator i
= pending_requests_
.find(request_id
);
542 if (i
!= pending_requests_
.end()) {
543 URLRequestChromeJob
* job(i
->second
);
544 pending_requests_
.erase(i
);
545 job
->DataAvailable(bytes
);