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.
4
5// An implementation of WebURLLoader in terms of ResourceLoaderBridge.
6
7#include "content/child/web_url_loader_impl.h"
8
9#include "base/bind.h"
10#include "base/files/file_path.h"
11#include "base/memory/scoped_ptr.h"
12#include "base/message_loop/message_loop.h"
13#include "base/strings/string_util.h"
14#include "base/time/time.h"
15#include "content/child/child_thread.h"
16#include "content/child/ftp_directory_listing_response_delegate.h"
17#include "content/child/request_extra_data.h"
18#include "content/child/request_info.h"
19#include "content/child/resource_dispatcher.h"
20#include "content/child/sync_load_response.h"
21#include "content/common/resource_request_body.h"
22#include "content/public/child/request_peer.h"
23#include "net/base/data_url.h"
24#include "net/base/filename_util.h"
25#include "net/base/load_flags.h"
26#include "net/base/mime_util.h"
27#include "net/base/net_errors.h"
28#include "net/http/http_response_headers.h"
29#include "net/http/http_util.h"
30#include "net/url_request/url_request.h"
31#include "third_party/WebKit/public/platform/WebHTTPHeaderVisitor.h"
32#include "third_party/WebKit/public/platform/WebHTTPLoadInfo.h"
33#include "third_party/WebKit/public/platform/WebURL.h"
34#include "third_party/WebKit/public/platform/WebURLError.h"
35#include "third_party/WebKit/public/platform/WebURLLoadTiming.h"
36#include "third_party/WebKit/public/platform/WebURLLoaderClient.h"
37#include "third_party/WebKit/public/platform/WebURLRequest.h"
38#include "third_party/WebKit/public/platform/WebURLResponse.h"
39#include "third_party/WebKit/public/web/WebSecurityPolicy.h"
40#include "webkit/child/multipart_response_delegate.h"
41#include "webkit/child/resource_loader_bridge.h"
42#include "webkit/child/weburlresponse_extradata_impl.h"
43
44using base::Time;
45using base::TimeTicks;
46using blink::WebData;
47using blink::WebHTTPBody;
48using blink::WebHTTPHeaderVisitor;
49using blink::WebHTTPLoadInfo;
50using blink::WebReferrerPolicy;
51using blink::WebSecurityPolicy;
52using blink::WebString;
53using blink::WebURL;
54using blink::WebURLError;
55using blink::WebURLLoadTiming;
56using blink::WebURLLoader;
57using blink::WebURLLoaderClient;
58using blink::WebURLRequest;
59using blink::WebURLResponse;
60using webkit_glue::MultipartResponseDelegate;
61using webkit_glue::ResourceLoaderBridge;
62using webkit_glue::WebURLResponseExtraDataImpl;
63
64namespace content {
65
66// Utilities ------------------------------------------------------------------
67
68namespace {
69
70const char kThrottledErrorDescription[] =
71    "Request throttled. Visit http://dev.chromium.org/throttling for more "
72    "information.";
73
74class HeaderFlattener : public WebHTTPHeaderVisitor {
75 public:
76  explicit HeaderFlattener() : has_accept_header_(false) {}
77
78  virtual void visitHeader(const WebString& name, const WebString& value) {
79    // Headers are latin1.
80    const std::string& name_latin1 = name.latin1();
81    const std::string& value_latin1 = value.latin1();
82
83    // Skip over referrer headers found in the header map because we already
84    // pulled it out as a separate parameter.
85    if (LowerCaseEqualsASCII(name_latin1, "referer"))
86      return;
87
88    if (LowerCaseEqualsASCII(name_latin1, "accept"))
89      has_accept_header_ = true;
90
91    if (!buffer_.empty())
92      buffer_.append("\r\n");
93    buffer_.append(name_latin1 + ": " + value_latin1);
94  }
95
96  const std::string& GetBuffer() {
97    // In some cases, WebKit doesn't add an Accept header, but not having the
98    // header confuses some web servers.  See bug 808613.
99    if (!has_accept_header_) {
100      if (!buffer_.empty())
101        buffer_.append("\r\n");
102      buffer_.append("Accept: */*");
103      has_accept_header_ = true;
104    }
105    return buffer_;
106  }
107
108 private:
109  std::string buffer_;
110  bool has_accept_header_;
111};
112
113// Extracts the information from a data: url.
114bool GetInfoFromDataURL(const GURL& url,
115                        ResourceResponseInfo* info,
116                        std::string* data,
117                        int* error_code) {
118  std::string mime_type;
119  std::string charset;
120  if (net::DataURL::Parse(url, &mime_type, &charset, data)) {
121    *error_code = net::OK;
122    // Assure same time for all time fields of data: URLs.
123    Time now = Time::Now();
124    info->load_timing.request_start = TimeTicks::Now();
125    info->load_timing.request_start_time = now;
126    info->request_time = now;
127    info->response_time = now;
128    info->headers = NULL;
129    info->mime_type.swap(mime_type);
130    info->charset.swap(charset);
131    info->security_info.clear();
132    info->content_length = data->length();
133    info->encoded_data_length = 0;
134
135    return true;
136  }
137
138  *error_code = net::ERR_INVALID_URL;
139  return false;
140}
141
142typedef ResourceDevToolsInfo::HeadersVector HeadersVector;
143
144// Converts timing data from |load_timing| to the format used by WebKit.
145void PopulateURLLoadTiming(const net::LoadTimingInfo& load_timing,
146                           WebURLLoadTiming* url_timing) {
147  DCHECK(!load_timing.request_start.is_null());
148
149  const TimeTicks kNullTicks;
150  url_timing->initialize();
151  url_timing->setRequestTime(
152      (load_timing.request_start - kNullTicks).InSecondsF());
153  url_timing->setProxyStart(
154      (load_timing.proxy_resolve_start - kNullTicks).InSecondsF());
155  url_timing->setProxyEnd(
156      (load_timing.proxy_resolve_end - kNullTicks).InSecondsF());
157  url_timing->setDNSStart(
158      (load_timing.connect_timing.dns_start - kNullTicks).InSecondsF());
159  url_timing->setDNSEnd(
160      (load_timing.connect_timing.dns_end - kNullTicks).InSecondsF());
161  url_timing->setConnectStart(
162      (load_timing.connect_timing.connect_start - kNullTicks).InSecondsF());
163  url_timing->setConnectEnd(
164      (load_timing.connect_timing.connect_end - kNullTicks).InSecondsF());
165  url_timing->setSSLStart(
166      (load_timing.connect_timing.ssl_start - kNullTicks).InSecondsF());
167  url_timing->setSSLEnd(
168      (load_timing.connect_timing.ssl_end - kNullTicks).InSecondsF());
169  url_timing->setSendStart(
170      (load_timing.send_start - kNullTicks).InSecondsF());
171  url_timing->setSendEnd(
172      (load_timing.send_end - kNullTicks).InSecondsF());
173  url_timing->setReceiveHeadersEnd(
174      (load_timing.receive_headers_end - kNullTicks).InSecondsF());
175}
176
177net::RequestPriority ConvertWebKitPriorityToNetPriority(
178    const WebURLRequest::Priority& priority) {
179  switch (priority) {
180    case WebURLRequest::PriorityVeryHigh:
181      return net::HIGHEST;
182
183    case WebURLRequest::PriorityHigh:
184      return net::MEDIUM;
185
186    case WebURLRequest::PriorityMedium:
187      return net::LOW;
188
189    case WebURLRequest::PriorityLow:
190      return net::LOWEST;
191
192    case WebURLRequest::PriorityVeryLow:
193      return net::IDLE;
194
195    case WebURLRequest::PriorityUnresolved:
196    default:
197      NOTREACHED();
198      return net::LOW;
199  }
200}
201
202}  // namespace
203
204// WebURLLoaderImpl::Context --------------------------------------------------
205
206// This inner class exists since the WebURLLoader may be deleted while inside a
207// call to WebURLLoaderClient.  The bridge requires its Peer to stay alive
208// until it receives OnCompletedRequest.
209class WebURLLoaderImpl::Context : public base::RefCounted<Context>,
210                                  public RequestPeer {
211 public:
212  explicit Context(WebURLLoaderImpl* loader);
213
214  WebURLLoaderClient* client() const { return client_; }
215  void set_client(WebURLLoaderClient* client) { client_ = client; }
216
217  void Cancel();
218  void SetDefersLoading(bool value);
219  void DidChangePriority(WebURLRequest::Priority new_priority,
220                         int intra_priority_value);
221  bool AttachThreadedDataReceiver(
222      blink::WebThreadedDataReceiver* threaded_data_receiver);
223  void Start(const WebURLRequest& request,
224             SyncLoadResponse* sync_load_response);
225
226  // RequestPeer methods:
227  virtual void OnUploadProgress(uint64 position, uint64 size) OVERRIDE;
228  virtual bool OnReceivedRedirect(const GURL& new_url,
229                                  const GURL& new_first_party_for_cookies,
230                                  const ResourceResponseInfo& info) OVERRIDE;
231  virtual void OnReceivedResponse(const ResourceResponseInfo& info) OVERRIDE;
232  virtual void OnDownloadedData(int len, int encoded_data_length) OVERRIDE;
233  virtual void OnReceivedData(const char* data,
234                              int data_length,
235                              int encoded_data_length) OVERRIDE;
236  virtual void OnReceivedCachedMetadata(const char* data, int len) OVERRIDE;
237  virtual void OnCompletedRequest(
238      int error_code,
239      bool was_ignored_by_handler,
240      bool stale_copy_in_cache,
241      const std::string& security_info,
242      const base::TimeTicks& completion_time,
243      int64 total_transfer_size) OVERRIDE;
244
245 private:
246  friend class base::RefCounted<Context>;
247  virtual ~Context() {}
248
249  // We can optimize the handling of data URLs in most cases.
250  bool CanHandleDataURL(const GURL& url) const;
251  void HandleDataURL();
252
253  WebURLLoaderImpl* loader_;
254  WebURLRequest request_;
255  WebURLLoaderClient* client_;
256  WebReferrerPolicy referrer_policy_;
257  scoped_ptr<ResourceLoaderBridge> bridge_;
258  scoped_ptr<FtpDirectoryListingResponseDelegate> ftp_listing_delegate_;
259  scoped_ptr<MultipartResponseDelegate> multipart_delegate_;
260  scoped_ptr<ResourceLoaderBridge> completed_bridge_;
261};
262
263WebURLLoaderImpl::Context::Context(WebURLLoaderImpl* loader)
264    : loader_(loader),
265      client_(NULL),
266      referrer_policy_(blink::WebReferrerPolicyDefault) {
267}
268
269void WebURLLoaderImpl::Context::Cancel() {
270  // The bridge will still send OnCompletedRequest, which will Release() us, so
271  // we don't do that here.
272  if (bridge_)
273    bridge_->Cancel();
274
275  // Ensure that we do not notify the multipart delegate anymore as it has
276  // its own pointer to the client.
277  if (multipart_delegate_)
278    multipart_delegate_->Cancel();
279  // Ditto for the ftp delegate.
280  if (ftp_listing_delegate_)
281    ftp_listing_delegate_->Cancel();
282
283  // Do not make any further calls to the client.
284  client_ = NULL;
285  loader_ = NULL;
286}
287
288void WebURLLoaderImpl::Context::SetDefersLoading(bool value) {
289  if (bridge_)
290    bridge_->SetDefersLoading(value);
291}
292
293void WebURLLoaderImpl::Context::DidChangePriority(
294    WebURLRequest::Priority new_priority, int intra_priority_value) {
295  if (bridge_)
296    bridge_->DidChangePriority(
297        ConvertWebKitPriorityToNetPriority(new_priority), intra_priority_value);
298}
299
300bool WebURLLoaderImpl::Context::AttachThreadedDataReceiver(
301    blink::WebThreadedDataReceiver* threaded_data_receiver) {
302  if (bridge_)
303    return bridge_->AttachThreadedDataReceiver(threaded_data_receiver);
304
305  return false;
306}
307
308void WebURLLoaderImpl::Context::Start(const WebURLRequest& request,
309                                      SyncLoadResponse* sync_load_response) {
310  DCHECK(!bridge_.get());
311
312  request_ = request;  // Save the request.
313
314  GURL url = request.url();
315  if (url.SchemeIs("data") && CanHandleDataURL(url)) {
316    if (sync_load_response) {
317      // This is a sync load. Do the work now.
318      sync_load_response->url = url;
319      std::string data;
320      GetInfoFromDataURL(sync_load_response->url, sync_load_response,
321                         &sync_load_response->data,
322                         &sync_load_response->error_code);
323    } else {
324      AddRef();  // Balanced in OnCompletedRequest
325      base::MessageLoop::current()->PostTask(
326          FROM_HERE, base::Bind(&Context::HandleDataURL, this));
327    }
328    return;
329  }
330
331  GURL referrer_url(
332      request.httpHeaderField(WebString::fromUTF8("Referer")).latin1());
333  const std::string& method = request.httpMethod().latin1();
334
335  int load_flags = net::LOAD_NORMAL | net::LOAD_ENABLE_LOAD_TIMING;
336  switch (request.cachePolicy()) {
337    case WebURLRequest::ReloadIgnoringCacheData:
338      // Required by LayoutTests/http/tests/misc/refresh-headers.php
339      load_flags |= net::LOAD_VALIDATE_CACHE;
340      break;
341    case WebURLRequest::ReloadBypassingCache:
342      load_flags |= net::LOAD_BYPASS_CACHE;
343      break;
344    case WebURLRequest::ReturnCacheDataElseLoad:
345      load_flags |= net::LOAD_PREFERRING_CACHE;
346      break;
347    case WebURLRequest::ReturnCacheDataDontLoad:
348      load_flags |= net::LOAD_ONLY_FROM_CACHE;
349      break;
350    case WebURLRequest::UseProtocolCachePolicy:
351      break;
352    default:
353      NOTREACHED();
354  }
355
356  if (request.reportUploadProgress())
357    load_flags |= net::LOAD_ENABLE_UPLOAD_PROGRESS;
358  if (request.reportRawHeaders())
359    load_flags |= net::LOAD_REPORT_RAW_HEADERS;
360
361  if (!request.allowStoredCredentials()) {
362    load_flags |= net::LOAD_DO_NOT_SAVE_COOKIES;
363    load_flags |= net::LOAD_DO_NOT_SEND_COOKIES;
364  }
365
366  if (!request.allowStoredCredentials())
367    load_flags |= net::LOAD_DO_NOT_SEND_AUTH_DATA;
368
369  if (request.targetType() == WebURLRequest::TargetIsXHR &&
370      (url.has_username() || url.has_password())) {
371    load_flags |= net::LOAD_DO_NOT_PROMPT_FOR_LOGIN;
372  }
373
374  HeaderFlattener flattener;
375  request.visitHTTPHeaderFields(&flattener);
376
377  // TODO(brettw) this should take parameter encoding into account when
378  // creating the GURLs.
379
380  RequestInfo request_info;
381  request_info.method = method;
382  request_info.url = url;
383  request_info.first_party_for_cookies = request.firstPartyForCookies();
384  request_info.referrer = referrer_url;
385  request_info.headers = flattener.GetBuffer();
386  request_info.load_flags = load_flags;
387  // requestor_pid only needs to be non-zero if the request originates outside
388  // the render process, so we can use requestorProcessID even for requests
389  // from in-process plugins.
390  request_info.requestor_pid = request.requestorProcessID();
391  request_info.request_type =
392      ResourceType::FromTargetType(request.targetType());
393  request_info.priority =
394      ConvertWebKitPriorityToNetPriority(request.priority());
395  request_info.appcache_host_id = request.appCacheHostID();
396  request_info.routing_id = request.requestorID();
397  request_info.download_to_file = request.downloadToFile();
398  request_info.has_user_gesture = request.hasUserGesture();
399  request_info.extra_data = request.extraData();
400  referrer_policy_ = request.referrerPolicy();
401  request_info.referrer_policy = request.referrerPolicy();
402  bridge_.reset(ChildThread::current()->resource_dispatcher()->CreateBridge(
403      request_info));
404
405  if (!request.httpBody().isNull()) {
406    // GET and HEAD requests shouldn't have http bodies.
407    DCHECK(method != "GET" && method != "HEAD");
408    const WebHTTPBody& httpBody = request.httpBody();
409    size_t i = 0;
410    WebHTTPBody::Element element;
411    scoped_refptr<ResourceRequestBody> request_body = new ResourceRequestBody;
412    while (httpBody.elementAt(i++, element)) {
413      switch (element.type) {
414        case WebHTTPBody::Element::TypeData:
415          if (!element.data.isEmpty()) {
416            // WebKit sometimes gives up empty data to append. These aren't
417            // necessary so we just optimize those out here.
418            request_body->AppendBytes(
419                element.data.data(), static_cast<int>(element.data.size()));
420          }
421          break;
422        case WebHTTPBody::Element::TypeFile:
423          if (element.fileLength == -1) {
424            request_body->AppendFileRange(
425                base::FilePath::FromUTF16Unsafe(element.filePath),
426                0, kuint64max, base::Time());
427          } else {
428            request_body->AppendFileRange(
429                base::FilePath::FromUTF16Unsafe(element.filePath),
430                static_cast<uint64>(element.fileStart),
431                static_cast<uint64>(element.fileLength),
432                base::Time::FromDoubleT(element.modificationTime));
433          }
434          break;
435        case WebHTTPBody::Element::TypeFileSystemURL: {
436          GURL file_system_url = element.fileSystemURL;
437          DCHECK(file_system_url.SchemeIsFileSystem());
438          request_body->AppendFileSystemFileRange(
439              file_system_url,
440              static_cast<uint64>(element.fileStart),
441              static_cast<uint64>(element.fileLength),
442              base::Time::FromDoubleT(element.modificationTime));
443          break;
444        }
445        case WebHTTPBody::Element::TypeBlob:
446          request_body->AppendBlob(element.blobUUID.utf8());
447          break;
448        default:
449          NOTREACHED();
450      }
451    }
452    request_body->set_identifier(request.httpBody().identifier());
453    bridge_->SetRequestBody(request_body.get());
454  }
455
456  if (sync_load_response) {
457    bridge_->SyncLoad(sync_load_response);
458    return;
459  }
460
461  if (bridge_->Start(this)) {
462    AddRef();  // Balanced in OnCompletedRequest
463  } else {
464    bridge_.reset();
465  }
466}
467
468void WebURLLoaderImpl::Context::OnUploadProgress(uint64 position, uint64 size) {
469  if (client_)
470    client_->didSendData(loader_, position, size);
471}
472
473bool WebURLLoaderImpl::Context::OnReceivedRedirect(
474    const GURL& new_url,
475    const GURL& new_first_party_for_cookies,
476    const ResourceResponseInfo& info) {
477  if (!client_)
478    return false;
479
480  WebURLResponse response;
481  response.initialize();
482  PopulateURLResponse(request_.url(), info, &response);
483
484  // TODO(darin): We lack sufficient information to construct the actual
485  // request that resulted from the redirect.
486  WebURLRequest new_request(new_url);
487  new_request.setFirstPartyForCookies(new_first_party_for_cookies);
488  new_request.setDownloadToFile(request_.downloadToFile());
489
490  WebString referrer_string = WebString::fromUTF8("Referer");
491  WebString referrer = WebSecurityPolicy::generateReferrerHeader(
492      referrer_policy_,
493      new_url,
494      request_.httpHeaderField(referrer_string));
495  if (!referrer.isEmpty())
496    new_request.setHTTPReferrer(referrer, referrer_policy_);
497
498  std::string method = request_.httpMethod().utf8();
499  std::string new_method = net::URLRequest::ComputeMethodForRedirect(
500      method, response.httpStatusCode());
501  new_request.setHTTPMethod(WebString::fromUTF8(new_method));
502  if (new_method == method)
503    new_request.setHTTPBody(request_.httpBody());
504
505  client_->willSendRequest(loader_, new_request, response);
506  request_ = new_request;
507
508  // Only follow the redirect if WebKit left the URL unmodified.
509  if (new_url == GURL(new_request.url())) {
510    // First-party cookie logic moved from DocumentLoader in Blink to
511    // CrossSiteResourceHandler in the browser. Assert that Blink didn't try to
512    // change it to something else.
513    DCHECK_EQ(new_first_party_for_cookies.spec(),
514              request_.firstPartyForCookies().string().utf8());
515    return true;
516  }
517
518  // We assume that WebKit only changes the URL to suppress a redirect, and we
519  // assume that it does so by setting it to be invalid.
520  DCHECK(!new_request.url().isValid());
521  return false;
522}
523
524void WebURLLoaderImpl::Context::OnReceivedResponse(
525    const ResourceResponseInfo& info) {
526  if (!client_)
527    return;
528
529  WebURLResponse response;
530  response.initialize();
531  PopulateURLResponse(request_.url(), info, &response);
532
533  bool show_raw_listing = (GURL(request_.url()).query() == "raw");
534
535  if (info.mime_type == "text/vnd.chromium.ftp-dir") {
536    if (show_raw_listing) {
537      // Set the MIME type to plain text to prevent any active content.
538      response.setMIMEType("text/plain");
539    } else {
540      // We're going to produce a parsed listing in HTML.
541      response.setMIMEType("text/html");
542    }
543  }
544
545  scoped_refptr<Context> protect(this);
546  client_->didReceiveResponse(loader_, response);
547
548  // We may have been cancelled after didReceiveResponse, which would leave us
549  // without a client and therefore without much need to do further handling.
550  if (!client_)
551    return;
552
553  DCHECK(!ftp_listing_delegate_.get());
554  DCHECK(!multipart_delegate_.get());
555  if (info.headers.get() && info.mime_type == "multipart/x-mixed-replace") {
556    std::string content_type;
557    info.headers->EnumerateHeader(NULL, "content-type", &content_type);
558
559    std::string mime_type;
560    std::string charset;
561    bool had_charset = false;
562    std::string boundary;
563    net::HttpUtil::ParseContentType(content_type, &mime_type, &charset,
564                                    &had_charset, &boundary);
565    base::TrimString(boundary, " \"", &boundary);
566
567    // If there's no boundary, just handle the request normally.  In the gecko
568    // code, nsMultiMixedConv::OnStartRequest throws an exception.
569    if (!boundary.empty()) {
570      multipart_delegate_.reset(
571          new MultipartResponseDelegate(client_, loader_, response, boundary));
572    }
573  } else if (info.mime_type == "text/vnd.chromium.ftp-dir" &&
574             !show_raw_listing) {
575    ftp_listing_delegate_.reset(
576        new FtpDirectoryListingResponseDelegate(client_, loader_, response));
577  }
578}
579
580void WebURLLoaderImpl::Context::OnDownloadedData(int len,
581                                                 int encoded_data_length) {
582  if (client_)
583    client_->didDownloadData(loader_, len, encoded_data_length);
584}
585
586void WebURLLoaderImpl::Context::OnReceivedData(const char* data,
587                                               int data_length,
588                                               int encoded_data_length) {
589  if (!client_)
590    return;
591
592  if (ftp_listing_delegate_) {
593    // The FTP listing delegate will make the appropriate calls to
594    // client_->didReceiveData and client_->didReceiveResponse.
595    ftp_listing_delegate_->OnReceivedData(data, data_length);
596  } else if (multipart_delegate_) {
597    // The multipart delegate will make the appropriate calls to
598    // client_->didReceiveData and client_->didReceiveResponse.
599    multipart_delegate_->OnReceivedData(data, data_length, encoded_data_length);
600  } else {
601    client_->didReceiveData(loader_, data, data_length, encoded_data_length);
602  }
603}
604
605void WebURLLoaderImpl::Context::OnReceivedCachedMetadata(
606    const char* data, int len) {
607  if (client_)
608    client_->didReceiveCachedMetadata(loader_, data, len);
609}
610
611void WebURLLoaderImpl::Context::OnCompletedRequest(
612    int error_code,
613    bool was_ignored_by_handler,
614    bool stale_copy_in_cache,
615    const std::string& security_info,
616    const base::TimeTicks& completion_time,
617    int64 total_transfer_size) {
618  if (ftp_listing_delegate_) {
619    ftp_listing_delegate_->OnCompletedRequest();
620    ftp_listing_delegate_.reset(NULL);
621  } else if (multipart_delegate_) {
622    multipart_delegate_->OnCompletedRequest();
623    multipart_delegate_.reset(NULL);
624  }
625
626  // Prevent any further IPC to the browser now that we're complete, but
627  // don't delete it to keep any downloaded temp files alive.
628  DCHECK(!completed_bridge_.get());
629  completed_bridge_.swap(bridge_);
630
631  if (client_) {
632    if (error_code != net::OK) {
633      client_->didFail(loader_, CreateError(request_.url(),
634                                            stale_copy_in_cache,
635                                            error_code));
636    } else {
637      client_->didFinishLoading(
638          loader_, (completion_time - TimeTicks()).InSecondsF(),
639          total_transfer_size);
640    }
641  }
642
643  // We are done with the bridge now, and so we need to release the reference
644  // to ourselves that we took on behalf of the bridge.  This may cause our
645  // destruction.
646  Release();
647}
648
649bool WebURLLoaderImpl::Context::CanHandleDataURL(const GURL& url) const {
650  DCHECK(url.SchemeIs("data"));
651
652  // Optimize for the case where we can handle a data URL locally.  We must
653  // skip this for data URLs targetted at frames since those could trigger a
654  // download.
655  //
656  // NOTE: We special case MIME types we can render both for performance
657  // reasons as well as to support unit tests, which do not have an underlying
658  // ResourceLoaderBridge implementation.
659
660#if defined(OS_ANDROID)
661  // For compatibility reasons on Android we need to expose top-level data://
662  // to the browser.
663  if (request_.targetType() == WebURLRequest::TargetIsMainFrame)
664    return false;
665#endif
666
667  if (request_.targetType() != WebURLRequest::TargetIsMainFrame &&
668      request_.targetType() != WebURLRequest::TargetIsSubframe)
669    return true;
670
671  std::string mime_type, unused_charset;
672  if (net::DataURL::Parse(url, &mime_type, &unused_charset, NULL) &&
673      net::IsSupportedMimeType(mime_type))
674    return true;
675
676  return false;
677}
678
679void WebURLLoaderImpl::Context::HandleDataURL() {
680  ResourceResponseInfo info;
681  int error_code;
682  std::string data;
683
684  if (GetInfoFromDataURL(request_.url(), &info, &data, &error_code)) {
685    OnReceivedResponse(info);
686    if (!data.empty())
687      OnReceivedData(data.data(), data.size(), 0);
688  }
689
690  OnCompletedRequest(error_code, false, false, info.security_info,
691                     base::TimeTicks::Now(), 0);
692}
693
694// WebURLLoaderImpl -----------------------------------------------------------
695
696WebURLLoaderImpl::WebURLLoaderImpl()
697    : context_(new Context(this)) {
698}
699
700WebURLLoaderImpl::~WebURLLoaderImpl() {
701  cancel();
702}
703
704WebURLError WebURLLoaderImpl::CreateError(const WebURL& unreachable_url,
705                                          bool stale_copy_in_cache,
706                                          int reason) {
707  WebURLError error;
708  error.domain = WebString::fromUTF8(net::kErrorDomain);
709  error.reason = reason;
710  error.unreachableURL = unreachable_url;
711  error.staleCopyInCache = stale_copy_in_cache;
712  if (reason == net::ERR_ABORTED) {
713    error.isCancellation = true;
714  } else if (reason == net::ERR_TEMPORARILY_THROTTLED) {
715    error.localizedDescription = WebString::fromUTF8(
716        kThrottledErrorDescription);
717  } else {
718    error.localizedDescription = WebString::fromUTF8(
719        net::ErrorToString(reason));
720  }
721  return error;
722}
723
724void WebURLLoaderImpl::PopulateURLResponse(const GURL& url,
725                                           const ResourceResponseInfo& info,
726                                           WebURLResponse* response) {
727  response->setURL(url);
728  response->setResponseTime(info.response_time.ToDoubleT());
729  response->setMIMEType(WebString::fromUTF8(info.mime_type));
730  response->setTextEncodingName(WebString::fromUTF8(info.charset));
731  response->setExpectedContentLength(info.content_length);
732  response->setSecurityInfo(info.security_info);
733  response->setAppCacheID(info.appcache_id);
734  response->setAppCacheManifestURL(info.appcache_manifest_url);
735  response->setWasCached(!info.load_timing.request_start_time.is_null() &&
736      info.response_time < info.load_timing.request_start_time);
737  response->setRemoteIPAddress(
738      WebString::fromUTF8(info.socket_address.host()));
739  response->setRemotePort(info.socket_address.port());
740  response->setConnectionID(info.load_timing.socket_log_id);
741  response->setConnectionReused(info.load_timing.socket_reused);
742  response->setDownloadFilePath(info.download_file_path.AsUTF16Unsafe());
743  WebURLResponseExtraDataImpl* extra_data =
744      new WebURLResponseExtraDataImpl(info.npn_negotiated_protocol);
745  response->setExtraData(extra_data);
746  extra_data->set_was_fetched_via_spdy(info.was_fetched_via_spdy);
747  extra_data->set_was_npn_negotiated(info.was_npn_negotiated);
748  extra_data->set_was_alternate_protocol_available(
749      info.was_alternate_protocol_available);
750  extra_data->set_connection_info(info.connection_info);
751  extra_data->set_was_fetched_via_proxy(info.was_fetched_via_proxy);
752
753  // If there's no received headers end time, don't set load timing.  This is
754  // the case for non-HTTP requests, requests that don't go over the wire, and
755  // certain error cases.
756  if (!info.load_timing.receive_headers_end.is_null()) {
757    WebURLLoadTiming timing;
758    PopulateURLLoadTiming(info.load_timing, &timing);
759    response->setLoadTiming(timing);
760  }
761
762  if (info.devtools_info.get()) {
763    WebHTTPLoadInfo load_info;
764
765    load_info.setHTTPStatusCode(info.devtools_info->http_status_code);
766    load_info.setHTTPStatusText(WebString::fromLatin1(
767        info.devtools_info->http_status_text));
768    load_info.setEncodedDataLength(info.encoded_data_length);
769
770    load_info.setRequestHeadersText(WebString::fromLatin1(
771        info.devtools_info->request_headers_text));
772    load_info.setResponseHeadersText(WebString::fromLatin1(
773        info.devtools_info->response_headers_text));
774    const HeadersVector& request_headers = info.devtools_info->request_headers;
775    for (HeadersVector::const_iterator it = request_headers.begin();
776         it != request_headers.end(); ++it) {
777      load_info.addRequestHeader(WebString::fromLatin1(it->first),
778          WebString::fromLatin1(it->second));
779    }
780    const HeadersVector& response_headers =
781        info.devtools_info->response_headers;
782    for (HeadersVector::const_iterator it = response_headers.begin();
783         it != response_headers.end(); ++it) {
784      load_info.addResponseHeader(WebString::fromLatin1(it->first),
785          WebString::fromLatin1(it->second));
786    }
787    response->setHTTPLoadInfo(load_info);
788  }
789
790  const net::HttpResponseHeaders* headers = info.headers.get();
791  if (!headers)
792    return;
793
794  WebURLResponse::HTTPVersion version = WebURLResponse::Unknown;
795  if (headers->GetHttpVersion() == net::HttpVersion(0, 9))
796    version = WebURLResponse::HTTP_0_9;
797  else if (headers->GetHttpVersion() == net::HttpVersion(1, 0))
798    version = WebURLResponse::HTTP_1_0;
799  else if (headers->GetHttpVersion() == net::HttpVersion(1, 1))
800    version = WebURLResponse::HTTP_1_1;
801  response->setHTTPVersion(version);
802  response->setHTTPStatusCode(headers->response_code());
803  response->setHTTPStatusText(WebString::fromLatin1(headers->GetStatusText()));
804
805  // TODO(darin): We should leverage HttpResponseHeaders for this, and this
806  // should be using the same code as ResourceDispatcherHost.
807  // TODO(jungshik): Figure out the actual value of the referrer charset and
808  // pass it to GetSuggestedFilename.
809  std::string value;
810  headers->EnumerateHeader(NULL, "content-disposition", &value);
811  response->setSuggestedFileName(
812      net::GetSuggestedFilename(url,
813                                value,
814                                std::string(),  // referrer_charset
815                                std::string(),  // suggested_name
816                                std::string(),  // mime_type
817                                std::string()));  // default_name
818
819  Time time_val;
820  if (headers->GetLastModifiedValue(&time_val))
821    response->setLastModifiedDate(time_val.ToDoubleT());
822
823  // Build up the header map.
824  void* iter = NULL;
825  std::string name;
826  while (headers->EnumerateHeaderLines(&iter, &name, &value)) {
827    response->addHTTPHeaderField(WebString::fromLatin1(name),
828                                 WebString::fromLatin1(value));
829  }
830}
831
832void WebURLLoaderImpl::loadSynchronously(const WebURLRequest& request,
833                                         WebURLResponse& response,
834                                         WebURLError& error,
835                                         WebData& data) {
836  SyncLoadResponse sync_load_response;
837  context_->Start(request, &sync_load_response);
838
839  const GURL& final_url = sync_load_response.url;
840
841  // TODO(tc): For file loads, we may want to include a more descriptive
842  // status code or status text.
843  int error_code = sync_load_response.error_code;
844  if (error_code != net::OK) {
845    response.setURL(final_url);
846    error.domain = WebString::fromUTF8(net::kErrorDomain);
847    error.reason = error_code;
848    error.unreachableURL = final_url;
849    return;
850  }
851
852  PopulateURLResponse(final_url, sync_load_response, &response);
853
854  data.assign(sync_load_response.data.data(),
855              sync_load_response.data.size());
856}
857
858void WebURLLoaderImpl::loadAsynchronously(const WebURLRequest& request,
859                                          WebURLLoaderClient* client) {
860  DCHECK(!context_->client());
861
862  context_->set_client(client);
863  context_->Start(request, NULL);
864}
865
866void WebURLLoaderImpl::cancel() {
867  context_->Cancel();
868}
869
870void WebURLLoaderImpl::setDefersLoading(bool value) {
871  context_->SetDefersLoading(value);
872}
873
874void WebURLLoaderImpl::didChangePriority(WebURLRequest::Priority new_priority,
875                                         int intra_priority_value) {
876  context_->DidChangePriority(new_priority, intra_priority_value);
877}
878
879bool WebURLLoaderImpl::attachThreadedDataReceiver(
880    blink::WebThreadedDataReceiver* threaded_data_receiver) {
881  return context_->AttachThreadedDataReceiver(threaded_data_receiver);
882}
883
884}  // namespace content
885