<commit_before>// Copyright (c) 2012 The Chromium Embedded Framework Authors.
// Portions copyright (c) 2006-2009 The Chromium 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/resource_request_job.h"

#include <map>
#include <vector>

#include "include/cef_callback.h"
#include "libcef/browser/thread_util.h"
#include "libcef/common/request_impl.h"
#include "libcef/common/response_impl.h"

#include "base/logging.h"
#include "net/base/io_buffer.h"
#include "net/base/load_flags.h"
#include "net/http/http_response_headers.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_context.h"

using net::URLRequestStatus;

// Client callback for asynchronous response continuation.
class CefResourceRequestJobCallback : public CefCallback {
 public:
  enum Type {
    HEADERS_AVAILABLE,
    BYTES_AVAILABLE,
  };

  explicit CefResourceRequestJobCallback(CefResourceRequestJob* job, Type type)
      : job_(job),
        type_(type),
        dest_(NULL),
        dest_size_(0) {}

  virtual void Continue() OVERRIDE {
    if (CEF_CURRENTLY_ON_IOT()) {
      // Currently on IO thread.
      // Return early if the callback has already been detached.
      if (!job_)
        return;

      if (type_ == HEADERS_AVAILABLE) {
        // Callback for headers available.
        if (!job_->has_response_started()) {
          // Send header information.
          job_->SendHeaders();
        }

        // This type of callback only ever needs to be called once.
        Detach();
      } else if (type_ == BYTES_AVAILABLE) {
        // Callback for bytes available.
        if (job_->has_response_started() &&
            job_->GetStatus().is_io_pending()) {
          // Read the bytes. They should be available but, if not, wait again.
          int bytes_read = 0;
          if (job_->ReadRawData(dest_, dest_size_, &bytes_read)) {
            if (bytes_read > 0) {
              // Clear the IO_PENDING status.
              job_->SetStatus(URLRequestStatus());

              // Notify about the available bytes.
              job_->NotifyReadComplete(bytes_read);

              dest_ = NULL;
              dest_size_ = 0;
            }
          } else {
            // All done.
            job_->NotifyDone(URLRequestStatus());
            Detach();
          }
        }
      }
    } else {
      // Execute this method on the IO thread.
      CEF_POST_TASK(CEF_IOT,
          base::Bind(&CefResourceRequestJobCallback::Continue, this));
    }
  }

  virtual void Cancel() OVERRIDE {
    if (CEF_CURRENTLY_ON_IOT()) {
      // Currently on IO thread.
      if (job_)
        job_->Kill();<commit_msg>Fix usage of ReadRawData return value in ResourceRequestJob implementation (issue #534).<commit_after>// Copyright (c) 2012 The Chromium Embedded Framework Authors.
// Portions copyright (c) 2006-2009 The Chromium 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/resource_request_job.h"

#include <map>
#include <vector>

#include "include/cef_callback.h"
#include "libcef/browser/thread_util.h"
#include "libcef/common/request_impl.h"
#include "libcef/common/response_impl.h"

#include "base/logging.h"
#include "net/base/io_buffer.h"
#include "net/base/load_flags.h"
#include "net/http/http_response_headers.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_context.h"

using net::URLRequestStatus;

// Client callback for asynchronous response continuation.
class CefResourceRequestJobCallback : public CefCallback {
 public:
  enum Type {
    HEADERS_AVAILABLE,
    BYTES_AVAILABLE,
  };

  explicit CefResourceRequestJobCallback(CefResourceRequestJob* job, Type type)
      : job_(job),
        type_(type),
        dest_(NULL),
        dest_size_(0) {}

  virtual void Continue() OVERRIDE {
    if (CEF_CURRENTLY_ON_IOT()) {
      // Currently on IO thread.
      // Return early if the callback has already been detached.
      if (!job_)
        return;

      if (type_ == HEADERS_AVAILABLE) {
        // Callback for headers available.
        if (!job_->has_response_started()) {
          // Send header information.
          job_->SendHeaders();
        }

        // This type of callback only ever needs to be called once.
        Detach();
      } else if (type_ == BYTES_AVAILABLE) {
        // Callback for bytes available.
        if (job_->has_response_started() &&
            job_->GetStatus().is_io_pending()) {
          // Read the bytes. They should be available but, if not, wait again.
          int bytes_read = 0;
          if (job_->ReadRawData(dest_, dest_size_, &bytes_read)) {
            if (bytes_read > 0) {
              // Clear the IO_PENDING status.
              job_->SetStatus(URLRequestStatus());

              // Notify about the available bytes.
              job_->NotifyReadComplete(bytes_read);

              dest_ = NULL;
              dest_size_ = 0;
            } else {
              // All done.
              job_->NotifyDone(URLRequestStatus());
              Detach();
            }
          } else if (!job_->GetStatus().is_io_pending()) {
            // Failed due to an error.
            NOTREACHED() <<
                "ReadRawData returned false without setting IO as pending";
            job_->NotifyDone(URLRequestStatus());
            Detach();
          }
        }
      }
    } else {
      // Execute this method on the IO thread.
      CEF_POST_TASK(CEF_IOT,
          base::Bind(&CefResourceRequestJobCallback::Continue, this));
    }
  }

  virtual void Cancel() OVERRIDE {
    if (CEF_CURRENTLY_ON_IOT()) {
      // Currently on IO thread.
      if (job_)
        job_->Kill();
{'fraction_non_alphanumeric': 0.10778443113772455, 'fraction_numerical': 0.005988023952095809, 'mean_word_length': 2.3348545350827155, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7574150', 'n_tokens_mistral': 1781, 'n_tokens_neox': 1706, 'n_words': 456}