<commit_before>// Copyright (c) 2011 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/prerender/prerender_resource_handler.h"

#include "chrome/browser/net/chrome_url_request_context.h"
#include "content/common/resource_response.h"
#include "net/base/load_flags.h"
#include "net/http/http_response_headers.h"
#include "net/url_request/url_request.h"

namespace prerender {

namespace {

bool ShouldPrerenderURL(const GURL& url) {
  if (!url.is_valid())
    return false;
  if (!url.SchemeIs("http")) {
    RecordFinalStatus(FINAL_STATUS_HTTPS);
    return false;
  }
  return true;
}

bool ValidateAliasURLs(const std::vector<GURL>& urls) {
  for (std::vector<GURL>::const_iterator it = urls.begin();
       it != urls.end();
       ++it) {
    if (!ShouldPrerenderURL(*it))
      return false;
  }
  return true;
}

bool ShouldPrerender(const ResourceResponse* response) {
  if (!response)
    return false;
  const ResourceResponseHead& rrh = response->response_head;
  if (!rrh.headers)
    return false;
  if (rrh.mime_type != "text/html")
    return false;
  if (rrh.headers->response_code() != 200)
    return false;
  return true;
}

}  // namespace

PrerenderResourceHandler* PrerenderResourceHandler::MaybeCreate(
    const net::URLRequest& request,
    ChromeURLRequestContext* context,
    ResourceHandler* next_handler) {
  if (!context || !context->prerender_manager())
    return NULL;
  if (request.load_flags() & net::LOAD_PRERENDER) {
    RecordFinalStatus(FINAL_STATUS_NESTED);
    return NULL;
  }
  if (!(request.load_flags() & net::LOAD_PREFETCH))
    return NULL;
  if (!ShouldPrerenderURL(request.url()))
    return NULL;
  if (request.method() != "GET")
    return NULL;
  return new PrerenderResourceHandler(request,
                                      next_handler,
                                      context->prerender_manager());
}

PrerenderResourceHandler::PrerenderResourceHandler(
    const net::URLRequest& request,
    ResourceHandler* next_handler,
    PrerenderManager* prerender_manager)
    : next_handler_(next_handler),
      prerender_manager_(prerender_manager),
      ALLOW_THIS_IN_INITIALIZER_LIST(
          prerender_callback_(NewCallback(
              this, &PrerenderResourceHandler::StartPrerender))),
      request_(request) {<commit_msg>Move FINAL_STATUS_NESTED later so it is not counted too heavily.<commit_after>// Copyright (c) 2011 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/prerender/prerender_resource_handler.h"

#include "chrome/browser/net/chrome_url_request_context.h"
#include "content/common/resource_response.h"
#include "net/base/load_flags.h"
#include "net/http/http_response_headers.h"
#include "net/url_request/url_request.h"

namespace prerender {

namespace {

bool ShouldPrerenderURL(const GURL& url) {
  if (!url.is_valid())
    return false;
  if (!url.SchemeIs("http")) {
    RecordFinalStatus(FINAL_STATUS_HTTPS);
    return false;
  }
  return true;
}

bool ValidateAliasURLs(const std::vector<GURL>& urls) {
  for (std::vector<GURL>::const_iterator it = urls.begin();
       it != urls.end();
       ++it) {
    if (!ShouldPrerenderURL(*it))
      return false;
  }
  return true;
}

bool ShouldPrerender(const ResourceResponse* response) {
  if (!response)
    return false;
  const ResourceResponseHead& rrh = response->response_head;
  if (!rrh.headers)
    return false;
  if (rrh.mime_type != "text/html")
    return false;
  if (rrh.headers->response_code() != 200)
    return false;
  return true;
}

}  // namespace

PrerenderResourceHandler* PrerenderResourceHandler::MaybeCreate(
    const net::URLRequest& request,
    ChromeURLRequestContext* context,
    ResourceHandler* next_handler) {
  if (!context || !context->prerender_manager())
    return NULL;
  if (!(request.load_flags() & net::LOAD_PREFETCH))
    return NULL;
  if (!ShouldPrerenderURL(request.url()))
    return NULL;
  if (request.method() != "GET")
    return NULL;
  if (request.load_flags() & net::LOAD_PRERENDER) {
    RecordFinalStatus(FINAL_STATUS_NESTED);
    return NULL;
  }
  return new PrerenderResourceHandler(request,
                                      next_handler,
                                      context->prerender_manager());
}

PrerenderResourceHandler::PrerenderResourceHandler(
    const net::URLRequest& request,
    ResourceHandler* next_handler,
    PrerenderManager* prerender_manager)
    : next_handler_(next_handler),
      prerender_manager_(prerender_manager),
      ALLOW_THIS_IN_INITIALIZER_LIST(
          prerender_callback_(NewCallback(
              this, &PrerenderResourceHandler::StartPrerender))),
      request_(request) {
{'fraction_non_alphanumeric': 0.11274810722324534, 'fraction_numerical': 0.002864743196234909, 'mean_word_length': 3.741028128031038, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3430934', 'n_tokens_mistral': 1552, 'n_tokens_neox': 1486, 'n_words': 327}