browser_urlrequest_impl.cc 17.5 KB
Newer Older
1 2 3 4 5 6 7 8 9
// Copyright (c) 2012 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that can
// be found in the LICENSE file.

#include "libcef/browser/browser_urlrequest_impl.h"

#include <string>

#include "libcef/browser/browser_context.h"
10
#include "libcef/browser/content_browser_client.h"
11
#include "libcef/browser/request_context_impl.h"
12
#include "libcef/browser/thread_util.h"
13
#include "libcef/browser/url_request_user_data.h"
14 15 16 17 18
#include "libcef/common/http_header_utils.h"
#include "libcef/common/request_impl.h"
#include "libcef/common/response_impl.h"

#include "base/logging.h"
19
#include "base/message_loop/message_loop.h"
20
#include "base/strings/string_util.h"
21
#include "content/public/browser/browser_thread.h"
22
#include "content/public/common/url_fetcher.h"
23
#include "net/base/io_buffer.h"
24
#include "net/base/load_flags.h"
25
#include "net/base/net_errors.h"
26
#include "net/http/http_request_headers.h"
27
#include "net/http/http_response_headers.h"
28
#include "net/url_request/url_fetcher.h"
29
#include "net/url_request/url_fetcher_delegate.h"
30
#include "net/url_request/url_fetcher_response_writer.h"
31 32
#include "net/url_request/url_request_status.h"

33
using content::BrowserThread;
34 35 36 37 38 39 40

namespace {

class CefURLFetcherDelegate : public net::URLFetcherDelegate {
 public:
  CefURLFetcherDelegate(CefBrowserURLRequest::Context* context,
                        int request_flags);
41
  ~CefURLFetcherDelegate() override;
42 43

  // net::URLFetcherDelegate methods.
44 45 46 47 48
  void OnURLFetchComplete(const net::URLFetcher* source) override;
  void OnURLFetchDownloadProgress(const net::URLFetcher* source,
                                  int64 current, int64 total) override;
  void OnURLFetchUploadProgress(const net::URLFetcher* source,
                                int64 current, int64 total) override;
49 50 51 52 53 54 55

 private:
  // The context_ pointer will outlive this object.
  CefBrowserURLRequest::Context* context_;
  int request_flags_;
};

56 57 58 59 60
class NET_EXPORT CefURLFetcherResponseWriter :
    public net::URLFetcherResponseWriter {
 public:
  CefURLFetcherResponseWriter(
      CefRefPtr<CefBrowserURLRequest> url_request,
61
      scoped_refptr<base::SequencedTaskRunner> task_runner)
62
      : url_request_(url_request),
63
        task_runner_(task_runner) {
64 65 66
  }

  // net::URLFetcherResponseWriter methods.
67
  int Initialize(const net::CompletionCallback& callback) override {
68 69 70
    return net::OK;
  }

71 72 73
  int Write(net::IOBuffer* buffer,
            int num_bytes,
            const net::CompletionCallback& callback) override {
74
    if (url_request_.get()) {
75
      task_runner_->PostTask(FROM_HERE,
76
          base::Bind(&CefURLFetcherResponseWriter::WriteOnClientThread,
77 78
                     url_request_, scoped_refptr<net::IOBuffer>(buffer),
                     num_bytes, callback,
79
                     base::MessageLoop::current()->task_runner()));
80 81 82 83 84
      return net::ERR_IO_PENDING;
    }
    return num_bytes;
  }

85
  int Finish(const net::CompletionCallback& callback) override {
86
    if (url_request_.get())
87 88 89 90 91 92 93
      url_request_ = NULL;
    return net::OK;
  }

 private:
  static void WriteOnClientThread(
      CefRefPtr<CefBrowserURLRequest> url_request,
94
      scoped_refptr<net::IOBuffer> buffer,
95 96
      int num_bytes,
      const net::CompletionCallback& callback,
97
      scoped_refptr<base::SequencedTaskRunner> source_message_loop_proxy) {
98
    CefRefPtr<CefURLRequestClient> client = url_request->GetClient();
99
    if (client.get())
100 101 102 103 104 105 106 107 108 109 110 111 112 113
      client->OnDownloadData(url_request.get(), buffer->data(), num_bytes);

    source_message_loop_proxy->PostTask(FROM_HERE,
        base::Bind(&CefURLFetcherResponseWriter::ContinueOnSourceThread,
                   num_bytes, callback));
  }

  static void ContinueOnSourceThread(
      int num_bytes,
      const net::CompletionCallback& callback) {
    callback.Run(num_bytes);
  }

  CefRefPtr<CefBrowserURLRequest> url_request_;
114
  scoped_refptr<base::SequencedTaskRunner> task_runner_;
115 116 117 118

  DISALLOW_COPY_AND_ASSIGN(CefURLFetcherResponseWriter);
};

119 120 121 122 123
base::SupportsUserData::Data* CreateURLRequestUserData(
    CefRefPtr<CefURLRequestClient> client) {
  return new CefURLRequestUserData(client);
}

124 125 126 127 128 129 130 131 132 133
}  // namespace


// CefBrowserURLRequest::Context ----------------------------------------------

class CefBrowserURLRequest::Context
    : public base::RefCountedThreadSafe<CefBrowserURLRequest::Context> {
 public:
  Context(CefRefPtr<CefBrowserURLRequest> url_request,
          CefRefPtr<CefRequest> request,
134 135
          CefRefPtr<CefURLRequestClient> client,
          CefRefPtr<CefRequestContext> request_context)
136 137 138
    : url_request_(url_request),
      request_(request),
      client_(client),
139
      request_context_(request_context),
140
      task_runner_(base::MessageLoop::current()->task_runner()),
141 142 143 144 145 146 147 148 149
    status_(UR_IO_PENDING),
    error_code_(ERR_NONE),
    upload_data_size_(0),
    got_upload_progress_complete_(false) {
    // Mark the request as read-only.
    static_cast<CefRequestImpl*>(request_.get())->SetReadOnly(true);
  }

  inline bool CalledOnValidThread() {
150
    return task_runner_->RunsTasksOnCurrentThread();
151 152 153 154 155 156 157 158 159 160
  }

  bool Start() {
    DCHECK(CalledOnValidThread());

    GURL url = GURL(request_->GetURL().ToString());
    if (!url.is_valid())
      return false;

    std::string method = request_->GetMethod();
161
    base::StringToLowerASCII(&method);
162
    net::URLFetcher::RequestType request_type = net::URLFetcher::GET;
163 164
    if (base::LowerCaseEqualsASCII(method, "get")) {
    } else if (base::LowerCaseEqualsASCII(method, "post")) {
165
      request_type = net::URLFetcher::POST;
166
    } else if (base::LowerCaseEqualsASCII(method, "head")) {
167
      request_type = net::URLFetcher::HEAD;
168
    } else if (base::LowerCaseEqualsASCII(method, "delete")) {
169
      request_type = net::URLFetcher::DELETE_REQUEST;
170
    } else if (base::LowerCaseEqualsASCII(method, "put")) {
171 172 173 174 175 176
      request_type = net::URLFetcher::PUT;
    } else {
      NOTREACHED() << "invalid request type";
      return false;
    }

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    BrowserThread::PostTaskAndReply(
      BrowserThread::UI,
      FROM_HERE,
      base::Bind(&CefBrowserURLRequest::Context::GetRequestContextOnUIThread,
                 this),
      base::Bind(&CefBrowserURLRequest::Context::ContinueOnOriginatingThread,
                 this, url, request_type));

    return true;
  }

  void GetRequestContextOnUIThread() {
    CEF_REQUIRE_UIT();

    // Get or create the request context and browser context.
    CefRefPtr<CefRequestContextImpl> request_context_impl =
        CefRequestContextImpl::GetForRequestContext(request_context_);
    DCHECK(request_context_impl.get());
    scoped_refptr<CefBrowserContext> browser_context =
        request_context_impl->GetBrowserContext();
    DCHECK(browser_context.get());

    if (!request_context_.get())
      request_context_ = request_context_impl.get();

    // The request context is created on the UI thread but accessed and
    // destroyed on the IO thread.
    url_request_getter_ = browser_context->GetRequestContext();
  }

  void ContinueOnOriginatingThread(const GURL& url,
                                   net::URLFetcher::RequestType request_type) {
    DCHECK(CalledOnValidThread());

211 212 213
    fetcher_delegate_.reset(
        new CefURLFetcherDelegate(this, request_->GetFlags()));

214 215
    fetcher_ = net::URLFetcher::Create(url, request_type,
                                       fetcher_delegate_.get());
216 217 218

    DCHECK(url_request_getter_.get());
    fetcher_->SetRequestContext(url_request_getter_.get());
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254

    CefRequest::HeaderMap headerMap;
    request_->GetHeaderMap(headerMap);

    // Extract the Referer header value.
    {
      CefString referrerStr;
      referrerStr.FromASCII(net::HttpRequestHeaders::kReferer);
      CefRequest::HeaderMap::iterator it = headerMap.find(referrerStr);
      if (it == headerMap.end()) {
        fetcher_->SetReferrer("");
      } else {
        fetcher_->SetReferrer(it->second);
        headerMap.erase(it);
      }
    }

    std::string content_type;

    // Extract the Content-Type header value.
    {
      CefString contentTypeStr;
      contentTypeStr.FromASCII(net::HttpRequestHeaders::kContentType);
      CefRequest::HeaderMap::iterator it = headerMap.find(contentTypeStr);
      if (it != headerMap.end()) {
        content_type = it->second;
        headerMap.erase(it);
      }
    }

    int64 upload_data_size = 0;

    CefRefPtr<CefPostData> post_data = request_->GetPostData();
    if (post_data.get()) {
      CefPostData::ElementVector elements;
      post_data->GetElements(elements);
255
      if (elements.size() == 1) {
256 257 258 259
        // Default to URL encoding if not specified.
        if (content_type.empty())
          content_type = "application/x-www-form-urlencoded";

260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
        CefPostDataElementImpl* impl =
            static_cast<CefPostDataElementImpl*>(elements[0].get());

        switch (elements[0]->GetType())
          case PDE_TYPE_BYTES: {
            upload_data_size = impl->GetBytesCount();
            fetcher_->SetUploadData(content_type,
                std::string(static_cast<char*>(impl->GetBytes()),
                            upload_data_size));
            break;
          case PDE_TYPE_FILE:
            fetcher_->SetUploadFilePath(
                content_type, 
                base::FilePath(impl->GetFile()),
                0, kuint64max,
                content::BrowserThread::GetMessageLoopProxyForThread(
                    content::BrowserThread::FILE).get());
            break;
          case PDE_TYPE_EMPTY:
            break;
        }
      } else if (elements.size() > 1) {
        NOTIMPLEMENTED() << " multi-part form data is not supported";
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
      }
    }

    std::string first_party_for_cookies = request_->GetFirstPartyForCookies();
    if (!first_party_for_cookies.empty())
      fetcher_->SetFirstPartyForCookies(GURL(first_party_for_cookies));

    int cef_flags = request_->GetFlags();

    if (cef_flags & UR_FLAG_NO_RETRY_ON_5XX)
      fetcher_->SetAutomaticallyRetryOn5xx(false);

    int load_flags = 0;

    if (cef_flags & UR_FLAG_SKIP_CACHE)
      load_flags |= net::LOAD_BYPASS_CACHE;

300
    if (!(cef_flags & UR_FLAG_ALLOW_CACHED_CREDENTIALS)) {
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
      load_flags |= net::LOAD_DO_NOT_SEND_AUTH_DATA;
      load_flags |= net::LOAD_DO_NOT_SEND_COOKIES;
      load_flags |= net::LOAD_DO_NOT_SAVE_COOKIES;
    }

    if (cef_flags & UR_FLAG_REPORT_UPLOAD_PROGRESS) {
      upload_data_size_ = upload_data_size;
    }

    if (cef_flags & UR_FLAG_REPORT_RAW_HEADERS)
      load_flags |= net::LOAD_REPORT_RAW_HEADERS;

    fetcher_->SetLoadFlags(load_flags);

    fetcher_->SetExtraRequestHeaders(
        HttpHeaderUtils::GenerateHeaders(headerMap));

318 319 320 321
    fetcher_->SetURLRequestUserData(
        CefURLRequestUserData::kUserDataKey,
        base::Bind(&CreateURLRequestUserData, client_));

322 323 324 325 326
    scoped_ptr<net::URLFetcherResponseWriter> response_writer;
    if (cef_flags & UR_FLAG_NO_DOWNLOAD_DATA) {
      response_writer.reset(new CefURLFetcherResponseWriter(NULL, NULL));
    } else {
      response_writer.reset(
327
          new CefURLFetcherResponseWriter(url_request_, task_runner_));
328 329 330
    }
    fetcher_->SaveResponseWithWriter(response_writer.Pass());

331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
    fetcher_->Start();
  }

  void Cancel() {
    DCHECK(CalledOnValidThread());

    // The request may already be complete.
    if (!fetcher_.get())
      return;

    // Cancel the fetch by deleting the fetcher.
    fetcher_.reset(NULL);

    status_ = UR_CANCELED;
    error_code_ = ERR_ABORTED;
    OnComplete();
  }

  void OnComplete() {
    DCHECK(CalledOnValidThread());

    if (fetcher_.get()) {
      const net::URLRequestStatus& status = fetcher_->GetStatus();

      if (status.is_success())
        NotifyUploadProgressIfNecessary();

      switch (status.status()) {
        case net::URLRequestStatus::SUCCESS:
          status_ = UR_SUCCESS;
          break;
        case net::URLRequestStatus::IO_PENDING:
          status_ = UR_IO_PENDING;
          break;
        case net::URLRequestStatus::CANCELED:
          status_ = UR_CANCELED;
          break;
        case net::URLRequestStatus::FAILED:
          status_ = UR_FAILED;
          break;
      }

      error_code_ = static_cast<CefURLRequest::ErrorCode>(status.error());

375 376
      if(!response_.get())
        OnResponse();
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
    }

    DCHECK(url_request_.get());
    client_->OnRequestComplete(url_request_.get());

    if (fetcher_.get())
      fetcher_.reset(NULL);

    // This may result in the Context object being deleted.
    url_request_ = NULL;
  }

  void OnDownloadProgress(int64 current, int64 total) {
    DCHECK(CalledOnValidThread());
    DCHECK(url_request_.get());

393 394 395
    if(!response_.get())
      OnResponse();

396 397 398 399 400 401 402 403
    NotifyUploadProgressIfNecessary();

    client_->OnDownloadProgress(url_request_.get(), current, total);
  }

  void OnDownloadData(scoped_ptr<std::string> download_data) {
    DCHECK(CalledOnValidThread());
    DCHECK(url_request_.get());
404 405 406 407

    if(!response_.get())
      OnResponse();

408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
    client_->OnDownloadData(url_request_.get(), download_data->c_str(),
        download_data->length());
  }

  void OnUploadProgress(int64 current, int64 total) {
    DCHECK(CalledOnValidThread());
    DCHECK(url_request_.get());
    if (current == total)
      got_upload_progress_complete_ = true;
    client_->OnUploadProgress(url_request_.get(), current, total);
  }

  CefRefPtr<CefRequest> request() { return request_; }
  CefRefPtr<CefURLRequestClient> client() { return client_; }
  CefURLRequest::Status status() { return status_; }
  CefURLRequest::ErrorCode error_code() { return error_code_; }
  CefRefPtr<CefResponse> response() { return response_; }

 private:
427 428 429 430 431
  friend class base::RefCountedThreadSafe<CefBrowserURLRequest::Context>;

  ~Context() {
    if (fetcher_.get()) {
      // Delete the fetcher object on the thread that created it.
432
      task_runner_->DeleteSoon(FROM_HERE, fetcher_.release());
433 434 435
    }
  }

436 437 438 439 440 441 442 443 444 445 446
  void NotifyUploadProgressIfNecessary() {
    if (!got_upload_progress_complete_ && upload_data_size_ > 0) {
      // URLFetcher sends upload notifications using a timer and will not send
      // a notification if the request completes too quickly. We therefore
      // send the notification here if necessary.
      client_->OnUploadProgress(url_request_.get(), upload_data_size_,
                                upload_data_size_);
      got_upload_progress_complete_ = true;
    }
  }

447 448 449 450 451 452 453 454 455 456 457 458 459 460
  void OnResponse() {
    if (fetcher_.get()) {  
      response_ = new CefResponseImpl();
      CefResponseImpl* responseImpl =
          static_cast<CefResponseImpl*>(response_.get());

      net::HttpResponseHeaders* headers = fetcher_->GetResponseHeaders();
      if (headers)
        responseImpl->SetResponseHeaders(*headers);

      responseImpl->SetReadOnly(true);
    }
  }

461 462 463 464
  // Members only accessed on the initialization thread.
  CefRefPtr<CefBrowserURLRequest> url_request_;
  CefRefPtr<CefRequest> request_;
  CefRefPtr<CefURLRequestClient> client_;
465
  CefRefPtr<CefRequestContext> request_context_;
466
  scoped_refptr<base::SequencedTaskRunner> task_runner_;
467 468 469 470 471 472 473
  scoped_ptr<net::URLFetcher> fetcher_;
  scoped_ptr<CefURLFetcherDelegate> fetcher_delegate_;
  CefURLRequest::Status status_;
  CefURLRequest::ErrorCode error_code_;
  CefRefPtr<CefResponse> response_;
  int64 upload_data_size_;
  bool got_upload_progress_complete_;
474 475

  scoped_refptr<net::URLRequestContextGetter> url_request_getter_;
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
};


// CefURLFetcherDelegate ------------------------------------------------------

namespace {

CefURLFetcherDelegate::CefURLFetcherDelegate(
    CefBrowserURLRequest::Context* context, int request_flags)
  : context_(context),
    request_flags_(request_flags) {
}

CefURLFetcherDelegate::~CefURLFetcherDelegate() {
}

void CefURLFetcherDelegate::OnURLFetchComplete(
    const net::URLFetcher* source) {
494 495 496 497
  // Complete asynchronously so as not to delete the URLFetcher while it's still
  // in the call stack.
  base::MessageLoop::current()->PostTask(FROM_HERE,
      base::Bind(&CefBrowserURLRequest::Context::OnComplete, context_));
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
}

void CefURLFetcherDelegate::OnURLFetchDownloadProgress(
    const net::URLFetcher* source,
    int64 current, int64 total) {
  context_->OnDownloadProgress(current, total);
}

void CefURLFetcherDelegate::OnURLFetchUploadProgress(
    const net::URLFetcher* source,
    int64 current, int64 total) {
  if (request_flags_ & UR_FLAG_REPORT_UPLOAD_PROGRESS)
    context_->OnUploadProgress(current, total);
}

}  // namespace


// CefBrowserURLRequest -------------------------------------------------------

CefBrowserURLRequest::CefBrowserURLRequest(
    CefRefPtr<CefRequest> request,
520 521 522
    CefRefPtr<CefURLRequestClient> client,
    CefRefPtr<CefRequestContext> request_context) {
  context_ = new Context(this, request, client, request_context);
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
}

CefBrowserURLRequest::~CefBrowserURLRequest() {
}

bool CefBrowserURLRequest::Start() {
  if (!VerifyContext())
    return false;
  return context_->Start();
}

CefRefPtr<CefRequest> CefBrowserURLRequest::GetRequest() {
  if (!VerifyContext())
    return NULL;
  return context_->request();
}

CefRefPtr<CefURLRequestClient> CefBrowserURLRequest::GetClient() {
  if (!VerifyContext())
    return NULL;
  return context_->client();
}

CefURLRequest::Status CefBrowserURLRequest::GetRequestStatus() {
  if (!VerifyContext())
    return UR_UNKNOWN;
  return context_->status();
}

CefURLRequest::ErrorCode CefBrowserURLRequest::GetRequestError() {
  if (!VerifyContext())
    return ERR_NONE;
  return context_->error_code();
}

CefRefPtr<CefResponse> CefBrowserURLRequest::GetResponse() {
  if (!VerifyContext())
    return NULL;
  return context_->response();
}

void CefBrowserURLRequest::Cancel() {
  if (!VerifyContext())
    return;
  return context_->Cancel();
}

bool CefBrowserURLRequest::VerifyContext() {
  DCHECK(context_.get());
  if (!context_->CalledOnValidThread()) {
    NOTREACHED() << "called on invalid thread";
    return false;
  }

  return true;
}