<commit_before>// Copyright (c) 2006-2008 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 "chrome/browser/renderer_host/download_throttling_resource_handler.h"

#include "base/logging.h"
#include "chrome/browser/renderer_host/download_resource_handler.h"
#include "chrome/browser/renderer_host/resource_dispatcher_host.h"
#include "chrome/common/resource_response.h"
#include "net/base/io_buffer.h"

DownloadThrottlingResourceHandler::DownloadThrottlingResourceHandler(
    ResourceDispatcherHost* host,
    URLRequest* request,
    const GURL& url,
    int render_process_host_id,
    int render_view_id,
    int request_id,
    bool in_complete)
    : host_(host),
      request_(request),
      url_(url),
      render_process_host_id_(render_process_host_id),
      render_view_id_(render_view_id),
      request_id_(request_id),
      tmp_buffer_length_(0),
      ignore_on_read_complete_(in_complete) {
  // Pause the request.
  host_->PauseRequest(render_process_host_id_, request_id_, true);
  host_->download_request_manager()->CanDownloadOnIOThread(
      render_process_host_id_, render_view_id, this);
 }

DownloadThrottlingResourceHandler::~DownloadThrottlingResourceHandler() {
}

bool DownloadThrottlingResourceHandler::OnUploadProgress(int request_id,
                                                         uint64 position,
                                                         uint64 size) {
  if (download_handler_.get())
    return download_handler_->OnUploadProgress(request_id, position, size);
  return true;
}

bool DownloadThrottlingResourceHandler::OnRequestRedirected(
    int request_id,
    const GURL& url,
    ResourceResponse* response,
    bool* defer) {
  if (download_handler_.get()) {
    return download_handler_->OnRequestRedirected(
        request_id, url, response, defer);
  }
  url_ = url;
  return true;
}

bool DownloadThrottlingResourceHandler::OnResponseStarted(
    int request_id,
    ResourceResponse* response) {
  if (download_handler_.get())
    return download_handler_->OnResponseStarted(request_id, response);
  response_ = response;
  return true;
}

bool DownloadThrottlingResourceHandler::OnWillStart(int request_id,
                                                    const GURL& url,
                                                    bool* defer) {
  if (download_handler_.get())
    return download_handler_->OnWillStart(request_id, url, defer);
  return true;
}

bool DownloadThrottlingResourceHandler::OnWillRead(int request_id,
                                                   net::IOBuffer** buf,
                                                   int* buf_size,
                                                   int min_size) {
  if (download_handler_.get())
    return download_handler_->OnWillRead(request_id, buf, buf_size, min_size);

  // We should only have this invoked once, as such we only deal with one
  // tmp buffer.
  DCHECK(!tmp_buffer_.get());
  if (min_size < 0)
    min_size = 1024;
  tmp_buffer_ = new net::IOBuffer(min_size);
  *buf = tmp_buffer_.get();
  // TODO(willchan): Remove after debugging bug 16371.
  CHECK((*buf)->data());
  *buf_size = min_size;
  return true;
}

bool DownloadThrottlingResourceHandler::OnReadCompleted(int request_id,
                                                        int* bytes_read) {
  if (ignore_on_read_complete_) {
    // See comments above definition for details on this.
    ignore_on_read_complete_ = false;
    return true;
  }
  if (!*bytes_read)
    return true;

  if (tmp_buffer_.get()) {
    DCHECK(!tmp_buffer_length_);
    tmp_buffer_length_ = *bytes_read;
    if (download_handler_.get())
      CopyTmpBufferToDownloadHandler();
    return true;
  }<commit_msg>Fixing a constant which was the hardcoded value of 2*kNumBytesToSniff. When I  changed kNumBytesToSniff in 41687, it was left behind. <commit_after>// Copyright (c) 2006-2010 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 "chrome/browser/renderer_host/download_throttling_resource_handler.h"

#include "base/logging.h"
#include "chrome/browser/renderer_host/download_resource_handler.h"
#include "chrome/browser/renderer_host/resource_dispatcher_host.h"
#include "chrome/common/resource_response.h"
#include "net/base/io_buffer.h"
#include "net/base/mime_sniffer.h"

DownloadThrottlingResourceHandler::DownloadThrottlingResourceHandler(
    ResourceDispatcherHost* host,
    URLRequest* request,
    const GURL& url,
    int render_process_host_id,
    int render_view_id,
    int request_id,
    bool in_complete)
    : host_(host),
      request_(request),
      url_(url),
      render_process_host_id_(render_process_host_id),
      render_view_id_(render_view_id),
      request_id_(request_id),
      tmp_buffer_length_(0),
      ignore_on_read_complete_(in_complete) {
  // Pause the request.
  host_->PauseRequest(render_process_host_id_, request_id_, true);
  host_->download_request_manager()->CanDownloadOnIOThread(
      render_process_host_id_, render_view_id, this);
 }

DownloadThrottlingResourceHandler::~DownloadThrottlingResourceHandler() {
}

bool DownloadThrottlingResourceHandler::OnUploadProgress(int request_id,
                                                         uint64 position,
                                                         uint64 size) {
  if (download_handler_.get())
    return download_handler_->OnUploadProgress(request_id, position, size);
  return true;
}

bool DownloadThrottlingResourceHandler::OnRequestRedirected(
    int request_id,
    const GURL& url,
    ResourceResponse* response,
    bool* defer) {
  if (download_handler_.get()) {
    return download_handler_->OnRequestRedirected(
        request_id, url, response, defer);
  }
  url_ = url;
  return true;
}

bool DownloadThrottlingResourceHandler::OnResponseStarted(
    int request_id,
    ResourceResponse* response) {
  if (download_handler_.get())
    return download_handler_->OnResponseStarted(request_id, response);
  response_ = response;
  return true;
}

bool DownloadThrottlingResourceHandler::OnWillStart(int request_id,
                                                    const GURL& url,
                                                    bool* defer) {
  if (download_handler_.get())
    return download_handler_->OnWillStart(request_id, url, defer);
  return true;
}

bool DownloadThrottlingResourceHandler::OnWillRead(int request_id,
                                                   net::IOBuffer** buf,
                                                   int* buf_size,
                                                   int min_size) {
  if (download_handler_.get())
    return download_handler_->OnWillRead(request_id, buf, buf_size, min_size);

  // We should only have this invoked once, as such we only deal with one
  // tmp buffer.
  DCHECK(!tmp_buffer_.get());
  // If the caller passed a negative |min_size| then chose an appropriate
  // default. The BufferedResourceHandler requires this to be at least 2 times
  // the size required for mime detection.
  if (min_size < 0)
    min_size = 2 * net::kMaxBytesToSniff;
  tmp_buffer_ = new net::IOBuffer(min_size);
  *buf = tmp_buffer_.get();
  // TODO(willchan): Remove after debugging bug 16371.
  CHECK((*buf)->data());
  *buf_size = min_size;
  return true;
}

bool DownloadThrottlingResourceHandler::OnReadCompleted(int request_id,
                                                        int* bytes_read) {
  if (ignore_on_read_complete_) {
    // See comments above definition for details on this.
    ignore_on_read_complete_ = false;
    return true;
  }
  if (!*bytes_read)
    return true;

  if (tmp_buffer_.get()) {
    DCHECK(!tmp_buffer_length_);
    tmp_buffer_length_ = *bytes_read;
    if (download_handler_.get())
      CopyTmpBufferToDownloadHandler();
    return true;
  }
{'fraction_non_alphanumeric': 0.08748284038437538, 'fraction_numerical': 0.006239860227130912, 'mean_word_length': 2.8381226053639845, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 84, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6272819', 'n_tokens_mistral': 2269, 'n_tokens_neox': 2240, 'n_words': 515}