This source file includes following definitions.
- weak_factory_
- SetUpload
- SetExtraRequestHeaders
- SetPriority
- Kill
- DetachRequest
- Read
- StopCaching
- GetFullRequestHeaders
- GetTotalReceivedBytes
- GetLoadState
- GetUploadProgress
- GetCharset
- GetResponseInfo
- GetLoadTimingInfo
- GetResponseCookies
- SetupFilter
- IsRedirectResponse
- CopyFragmentOnRedirect
- IsSafeRedirect
- NeedsAuth
- GetAuthChallengeInfo
- SetAuth
- CancelAuth
- ContinueWithCertificate
- ContinueDespiteLastError
- FollowDeferredRedirect
- ResumeNetworkStart
- GetMimeType
- GetResponseCode
- GetSocketAddress
- OnSuspend
- NotifyURLRequestDestroyed
- NotifyCertificateRequested
- NotifySSLCertificateError
- CanGetCookies
- CanSetCookie
- CanEnablePrivacyMode
- GetCookieStore
- NotifyBeforeNetworkStart
- NotifyHeadersComplete
- NotifyReadComplete
- NotifyStartError
- NotifyDone
- CompleteNotifyDone
- NotifyCanceled
- NotifyRestartRequired
- OnCallToDelegate
- OnCallToDelegateComplete
- ReadRawData
- DoneReading
- DoneReadingRedirectResponse
- FilteredDataRead
- ReadFilteredData
- DestroyFilters
- GetStatus
- SetStatus
- ReadRawDataForFilter
- ReadRawDataHelper
- FollowRedirect
- OnRawReadComplete
- RecordBytesRead
- FilterHasData
- UpdatePacketReadTimes
#include "net/url_request/url_request_job.h"
#include "base/bind.h"
#include "base/compiler_specific.h"
#include "base/message_loop/message_loop.h"
#include "base/power_monitor/power_monitor.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "net/base/auth.h"
#include "net/base/host_port_pair.h"
#include "net/base/io_buffer.h"
#include "net/base/load_states.h"
#include "net/base/net_errors.h"
#include "net/base/network_delegate.h"
#include "net/filter/filter.h"
#include "net/http/http_response_headers.h"
#include "net/url_request/url_request.h"
namespace net {
URLRequestJob::URLRequestJob(URLRequest* request,
                             NetworkDelegate* network_delegate)
    : request_(request),
      done_(false),
      prefilter_bytes_read_(0),
      postfilter_bytes_read_(0),
      filter_input_byte_count_(0),
      filter_needs_more_output_space_(false),
      filtered_read_buffer_len_(0),
      has_handled_response_(false),
      expected_content_size_(-1),
      deferred_redirect_status_code_(-1),
      network_delegate_(network_delegate),
      weak_factory_(this) {
  base::PowerMonitor* power_monitor = base::PowerMonitor::Get();
  if (power_monitor)
    power_monitor->AddObserver(this);
}
void URLRequestJob::SetUpload(UploadDataStream* upload) {
}
void URLRequestJob::SetExtraRequestHeaders(const HttpRequestHeaders& headers) {
}
void URLRequestJob::SetPriority(RequestPriority priority) {
}
void URLRequestJob::Kill() {
  weak_factory_.InvalidateWeakPtrs();
  
  
  if (request_)
    NotifyCanceled();
}
void URLRequestJob::DetachRequest() {
  request_ = NULL;
}
bool URLRequestJob::Read(IOBuffer* buf, int buf_size, int *bytes_read) {
  bool rv = false;
  DCHECK_LT(buf_size, 1000000);  
  DCHECK(buf);
  DCHECK(bytes_read);
  DCHECK(filtered_read_buffer_.get() == NULL);
  DCHECK_EQ(0, filtered_read_buffer_len_);
  *bytes_read = 0;
  
  if (!filter_.get()) {
    rv = ReadRawDataHelper(buf, buf_size, bytes_read);
  } else {
    
    
    filtered_read_buffer_ = buf;
    filtered_read_buffer_len_ = buf_size;
    if (ReadFilteredData(bytes_read)) {
      rv = true;   
      
      
      
      if (*bytes_read == 0)
        DoneReading();
    } else {
      rv = false;  
    }
  }
  if (rv && *bytes_read == 0)
    NotifyDone(URLRequestStatus());
  return rv;
}
void URLRequestJob::StopCaching() {
  
}
bool URLRequestJob::GetFullRequestHeaders(HttpRequestHeaders* headers) const {
  
  return false;
}
int64 URLRequestJob::GetTotalReceivedBytes() const {
  return 0;
}
LoadState URLRequestJob::GetLoadState() const {
  return LOAD_STATE_IDLE;
}
UploadProgress URLRequestJob::GetUploadProgress() const {
  return UploadProgress();
}
bool URLRequestJob::GetCharset(std::string* charset) {
  return false;
}
void URLRequestJob::GetResponseInfo(HttpResponseInfo* info) {
}
void URLRequestJob::GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const {
  
}
bool URLRequestJob::GetResponseCookies(std::vector<std::string>* cookies) {
  return false;
}
Filter* URLRequestJob::SetupFilter() const {
  return NULL;
}
bool URLRequestJob::IsRedirectResponse(GURL* location,
                                       int* http_status_code) {
  
  HttpResponseHeaders* headers = request_->response_headers();
  if (!headers)
    return false;
  std::string value;
  if (!headers->IsRedirect(&value))
    return false;
  *location = request_->url().Resolve(value);
  *http_status_code = headers->response_code();
  return true;
}
bool URLRequestJob::CopyFragmentOnRedirect(const GURL& location) const {
  return true;
}
bool URLRequestJob::IsSafeRedirect(const GURL& location) {
  return true;
}
bool URLRequestJob::NeedsAuth() {
  return false;
}
void URLRequestJob::GetAuthChallengeInfo(
    scoped_refptr<AuthChallengeInfo>* auth_info) {
  
  
  NOTREACHED();
}
void URLRequestJob::SetAuth(const AuthCredentials& credentials) {
  
  
  NOTREACHED();
}
void URLRequestJob::CancelAuth() {
  
  
  NOTREACHED();
}
void URLRequestJob::ContinueWithCertificate(
    X509Certificate* client_cert) {
  
  NOTREACHED();
}
void URLRequestJob::ContinueDespiteLastError() {
  
  
  
  NOTREACHED();
}
void URLRequestJob::FollowDeferredRedirect() {
  DCHECK(deferred_redirect_status_code_ != -1);
  
  
  
  
  
  GURL redirect_url = deferred_redirect_url_;
  int redirect_status_code = deferred_redirect_status_code_;
  deferred_redirect_url_ = GURL();
  deferred_redirect_status_code_ = -1;
  FollowRedirect(redirect_url, redirect_status_code);
}
void URLRequestJob::ResumeNetworkStart() {
  
  
  NOTREACHED();
}
bool URLRequestJob::GetMimeType(std::string* mime_type) const {
  return false;
}
int URLRequestJob::GetResponseCode() const {
  return -1;
}
HostPortPair URLRequestJob::GetSocketAddress() const {
  return HostPortPair();
}
void URLRequestJob::OnSuspend() {
  Kill();
}
void URLRequestJob::NotifyURLRequestDestroyed() {
}
URLRequestJob::~URLRequestJob() {
  base::PowerMonitor* power_monitor = base::PowerMonitor::Get();
  if (power_monitor)
    power_monitor->RemoveObserver(this);
}
void URLRequestJob::NotifyCertificateRequested(
    SSLCertRequestInfo* cert_request_info) {
  if (!request_)
    return;  
  request_->NotifyCertificateRequested(cert_request_info);
}
void URLRequestJob::NotifySSLCertificateError(const SSLInfo& ssl_info,
                                              bool fatal) {
  if (!request_)
    return;  
  request_->NotifySSLCertificateError(ssl_info, fatal);
}
bool URLRequestJob::CanGetCookies(const CookieList& cookie_list) const {
  if (!request_)
    return false;  
  return request_->CanGetCookies(cookie_list);
}
bool URLRequestJob::CanSetCookie(const std::string& cookie_line,
                                 CookieOptions* options) const {
  if (!request_)
    return false;  
  return request_->CanSetCookie(cookie_line, options);
}
bool URLRequestJob::CanEnablePrivacyMode() const {
  if (!request_)
    return false;  
  return request_->CanEnablePrivacyMode();
}
CookieStore* URLRequestJob::GetCookieStore() const {
  DCHECK(request_);
  return request_->cookie_store();
}
void URLRequestJob::NotifyBeforeNetworkStart(bool* defer) {
  if (!request_)
    return;
  request_->NotifyBeforeNetworkStart(defer);
}
void URLRequestJob::NotifyHeadersComplete() {
  if (!request_ || !request_->has_delegate())
    return;  
  if (has_handled_response_)
    return;
  DCHECK(!request_->status().is_io_pending());
  
  
  
  request_->response_info_.response_time = base::Time::Now();
  GetResponseInfo(&request_->response_info_);
  
  
  
  
  
  scoped_refptr<URLRequestJob> self_preservation(this);
  if (request_)
    request_->OnHeadersComplete();
  GURL new_location;
  int http_status_code;
  if (IsRedirectResponse(&new_location, &http_status_code)) {
    
    
    DoneReadingRedirectResponse();
    const GURL& url = request_->url();
    
    
    if (url.is_valid() && url.has_ref() && !new_location.has_ref() &&
        CopyFragmentOnRedirect(new_location)) {
      GURL::Replacements replacements;
      
      
      replacements.SetRef(url.spec().data(),
                          url.parsed_for_possibly_invalid_spec().ref);
      new_location = new_location.ReplaceComponents(replacements);
    }
    bool defer_redirect = false;
    request_->NotifyReceivedRedirect(new_location, &defer_redirect);
    
    
    if (!request_ || !request_->has_delegate())
      return;
    
    if (request_->status().is_success()) {
      if (defer_redirect) {
        deferred_redirect_url_ = new_location;
        deferred_redirect_status_code_ = http_status_code;
      } else {
        FollowRedirect(new_location, http_status_code);
      }
      return;
    }
  } else if (NeedsAuth()) {
    scoped_refptr<AuthChallengeInfo> auth_info;
    GetAuthChallengeInfo(&auth_info);
    
    
    if (auth_info.get()) {
      request_->NotifyAuthRequired(auth_info.get());
      
      return;
    }
  }
  has_handled_response_ = true;
  if (request_->status().is_success())
    filter_.reset(SetupFilter());
  if (!filter_.get()) {
    std::string content_length;
    request_->GetResponseHeaderByName("content-length", &content_length);
    if (!content_length.empty())
      base::StringToInt64(content_length, &expected_content_size_);
  }
  request_->NotifyResponseStarted();
}
void URLRequestJob::NotifyReadComplete(int bytes_read) {
  if (!request_ || !request_->has_delegate())
    return;  
  
  
  
  
  DCHECK(has_handled_response_);
  OnRawReadComplete(bytes_read);
  
  if (!request_->status().is_success())
    return;
  
  
  
  
  
  scoped_refptr<URLRequestJob> self_preservation(this);
  if (filter_.get()) {
    
    FilteredDataRead(bytes_read);
    
    int filter_bytes_read = 0;
    if (ReadFilteredData(&filter_bytes_read)) {
      if (!filter_bytes_read)
        DoneReading();
      request_->NotifyReadCompleted(filter_bytes_read);
    }
  } else {
    request_->NotifyReadCompleted(bytes_read);
  }
  DVLOG(1) << __FUNCTION__ << "() "
           << "\"" << (request_ ? request_->url().spec() : "???") << "\""
           << " pre bytes read = " << bytes_read
           << " pre total = " << prefilter_bytes_read_
           << " post total = " << postfilter_bytes_read_;
}
void URLRequestJob::NotifyStartError(const URLRequestStatus &status) {
  DCHECK(!has_handled_response_);
  has_handled_response_ = true;
  if (request_) {
    
    
    GetResponseInfo(&request_->response_info_);
    request_->set_status(status);
    request_->NotifyResponseStarted();
    
  }
}
void URLRequestJob::NotifyDone(const URLRequestStatus &status) {
  DCHECK(!done_) << "Job sending done notification twice";
  if (done_)
    return;
  done_ = true;
  
  
  DCHECK(has_handled_response_ || !status.is_success());
  
  
  if (request_) {
    request_->set_is_pending(false);
    
    
    
    
    
    
    if (request_->status().is_success()) {
      if (status.status() == URLRequestStatus::FAILED) {
        request_->net_log().AddEventWithNetErrorCode(NetLog::TYPE_FAILED,
                                                     status.error());
      }
      request_->set_status(status);
    }
  }
  
  
  base::MessageLoop::current()->PostTask(
      FROM_HERE,
      base::Bind(&URLRequestJob::CompleteNotifyDone,
                 weak_factory_.GetWeakPtr()));
}
void URLRequestJob::CompleteNotifyDone() {
  
  if (request_ &&
      !request_->status().is_success() &&
      request_->has_delegate()) {
    
    
    if (has_handled_response_) {
      
      request_->NotifyReadCompleted(-1);
    } else {
      has_handled_response_ = true;
      request_->NotifyResponseStarted();
    }
  }
}
void URLRequestJob::NotifyCanceled() {
  if (!done_) {
    NotifyDone(URLRequestStatus(URLRequestStatus::CANCELED, ERR_ABORTED));
  }
}
void URLRequestJob::NotifyRestartRequired() {
  DCHECK(!has_handled_response_);
  if (GetStatus().status() != URLRequestStatus::CANCELED)
    request_->Restart();
}
void URLRequestJob::OnCallToDelegate() {
  request_->OnCallToDelegate();
}
void URLRequestJob::OnCallToDelegateComplete() {
  request_->OnCallToDelegateComplete();
}
bool URLRequestJob::ReadRawData(IOBuffer* buf, int buf_size,
                                int *bytes_read) {
  DCHECK(bytes_read);
  *bytes_read = 0;
  return true;
}
void URLRequestJob::DoneReading() {
  
}
void URLRequestJob::DoneReadingRedirectResponse() {
}
void URLRequestJob::FilteredDataRead(int bytes_read) {
  DCHECK(filter_.get());  
  filter_->FlushStreamBuffer(bytes_read);
}
bool URLRequestJob::ReadFilteredData(int* bytes_read) {
  DCHECK(filter_.get());  
  DCHECK(filtered_read_buffer_.get() !=
         NULL);                             
  DCHECK_GT(filtered_read_buffer_len_, 0);  
  DCHECK_LT(filtered_read_buffer_len_, 1000000);  
  DCHECK(raw_read_buffer_.get() ==
         NULL);  
  bool rv = false;
  *bytes_read = 0;
  if (is_done())
    return true;
  if (!filter_needs_more_output_space_ && !filter_->stream_data_len()) {
    
    
    int filtered_data_read;
    if (ReadRawDataForFilter(&filtered_data_read)) {
      if (filtered_data_read > 0) {
        filter_->FlushStreamBuffer(filtered_data_read);  
      } else {
        return true;  
      }
    } else {
      return false;  
    }
  }
  if ((filter_->stream_data_len() || filter_needs_more_output_space_)
      && !is_done()) {
    
    int filtered_data_len = filtered_read_buffer_len_;
    Filter::FilterStatus status;
    int output_buffer_size = filtered_data_len;
    status = filter_->ReadData(filtered_read_buffer_->data(),
                               &filtered_data_len);
    if (filter_needs_more_output_space_ && 0 == filtered_data_len) {
      
      
      
      filter_needs_more_output_space_ = false;
      return ReadFilteredData(bytes_read);
    }
    switch (status) {
      case Filter::FILTER_DONE: {
        filter_needs_more_output_space_ = false;
        *bytes_read = filtered_data_len;
        postfilter_bytes_read_ += filtered_data_len;
        rv = true;
        break;
      }
      case Filter::FILTER_NEED_MORE_DATA: {
        filter_needs_more_output_space_ =
            (filtered_data_len == output_buffer_size);
        
        
        
        
        
        
        if (filtered_data_len > 0) {
          *bytes_read = filtered_data_len;
          postfilter_bytes_read_ += filtered_data_len;
          rv = true;
        } else {
          
          
          rv = ReadFilteredData(bytes_read);
        }
        break;
      }
      case Filter::FILTER_OK: {
        filter_needs_more_output_space_ =
            (filtered_data_len == output_buffer_size);
        *bytes_read = filtered_data_len;
        postfilter_bytes_read_ += filtered_data_len;
        rv = true;
        break;
      }
      case Filter::FILTER_ERROR: {
        DVLOG(1) << __FUNCTION__ << "() "
                 << "\"" << (request_ ? request_->url().spec() : "???") << "\""
                 << " Filter Error";
        filter_needs_more_output_space_ = false;
        NotifyDone(URLRequestStatus(URLRequestStatus::FAILED,
                   ERR_CONTENT_DECODING_FAILED));
        rv = false;
        break;
      }
      default: {
        NOTREACHED();
        filter_needs_more_output_space_ = false;
        rv = false;
        break;
      }
    }
    DVLOG(2) << __FUNCTION__ << "() "
             << "\"" << (request_ ? request_->url().spec() : "???") << "\""
             << " rv = " << rv
             << " post bytes read = " << filtered_data_len
             << " pre total = " << prefilter_bytes_read_
             << " post total = "
             << postfilter_bytes_read_;
    
    if (rv && request() && request()->net_log().IsLoggingBytes() &&
        filtered_data_len > 0) {
      request()->net_log().AddByteTransferEvent(
          NetLog::TYPE_URL_REQUEST_JOB_FILTERED_BYTES_READ,
          filtered_data_len, filtered_read_buffer_->data());
    }
  } else {
    
    rv = true;
  }
  if (rv) {
    
    
    filtered_read_buffer_ = NULL;
    filtered_read_buffer_len_ = 0;
  }
  return rv;
}
void URLRequestJob::DestroyFilters() {
  filter_.reset();
}
const URLRequestStatus URLRequestJob::GetStatus() {
  if (request_)
    return request_->status();
  
  return URLRequestStatus(URLRequestStatus::CANCELED,
                          ERR_ABORTED);
}
void URLRequestJob::SetStatus(const URLRequestStatus &status) {
  if (request_)
    request_->set_status(status);
}
bool URLRequestJob::ReadRawDataForFilter(int* bytes_read) {
  bool rv = false;
  DCHECK(bytes_read);
  DCHECK(filter_.get());
  *bytes_read = 0;
  
  
  
  if (!filter_->stream_data_len() && !is_done()) {
    IOBuffer* stream_buffer = filter_->stream_buffer();
    int stream_buffer_size = filter_->stream_buffer_size();
    rv = ReadRawDataHelper(stream_buffer, stream_buffer_size, bytes_read);
  }
  return rv;
}
bool URLRequestJob::ReadRawDataHelper(IOBuffer* buf, int buf_size,
                                      int* bytes_read) {
  DCHECK(!request_->status().is_io_pending());
  DCHECK(raw_read_buffer_.get() == NULL);
  
  
  
  raw_read_buffer_ = buf;
  bool rv = ReadRawData(buf, buf_size, bytes_read);
  if (!request_->status().is_io_pending()) {
    
    
    
    OnRawReadComplete(*bytes_read);
  }
  return rv;
}
void URLRequestJob::FollowRedirect(const GURL& location, int http_status_code) {
  int rv = request_->Redirect(location, http_status_code);
  if (rv != OK)
    NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv));
}
void URLRequestJob::OnRawReadComplete(int bytes_read) {
  DCHECK(raw_read_buffer_.get());
  
  if (!filter_.get() && request() && request()->net_log().IsLoggingBytes() &&
      bytes_read > 0) {
    request()->net_log().AddByteTransferEvent(
        NetLog::TYPE_URL_REQUEST_JOB_BYTES_READ,
        bytes_read, raw_read_buffer_->data());
  }
  if (bytes_read > 0) {
    RecordBytesRead(bytes_read);
  }
  raw_read_buffer_ = NULL;
}
void URLRequestJob::RecordBytesRead(int bytes_read) {
  filter_input_byte_count_ += bytes_read;
  prefilter_bytes_read_ += bytes_read;
  if (!filter_.get())
    postfilter_bytes_read_ += bytes_read;
  DVLOG(2) << __FUNCTION__ << "() "
           << "\"" << (request_ ? request_->url().spec() : "???") << "\""
           << " pre bytes read = " << bytes_read
           << " pre total = " << prefilter_bytes_read_
           << " post total = " << postfilter_bytes_read_;
  UpdatePacketReadTimes();  
  if (network_delegate_)
    network_delegate_->NotifyRawBytesRead(*request_, bytes_read);
}
bool URLRequestJob::FilterHasData() {
    return filter_.get() && filter_->stream_data_len();
}
void URLRequestJob::UpdatePacketReadTimes() {
}
}