<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.
//
// Implementation of the SafeBrowsingBlockingPage class.

#include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h"

#include "chrome/app/locales/locale_settings.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browser_resources.h"
#include "chrome/browser/dom_operation_notification_details.h"
#include "chrome/browser/google_util.h"
#include "chrome/browser/navigation_controller.h"
#include "chrome/browser/navigation_entry.h"
#include "chrome/browser/tab_util.h"
#include "chrome/browser/web_contents.h"
#include "chrome/common/jstemplate_builder.h"
#include "chrome/common/l10n_util.h"
#include "chrome/common/resource_bundle.h"
#include "generated_resources.h"
#include "net/base/escape.h"


// For malware interstitial pages, we link the problematic URL to Google's
// diagnostic page.
#if defined(GOOGLE_CHROME_BUILD)
static const char* const kSbDiagnosticUrl =
    "http://safebrowsing.clients.google.com/safebrowsing/diagnostic?site=%ls&client=googlechrome";
#else
static const char* const kSbDiagnosticUrl =
    "http://safebrowsing.clients.google.com/safebrowsing/diagnostic?site=%ls&client=chromium";
#endif

static const char* const kSbReportPhishingUrl =
    "http://www.google.com/safebrowsing/report_error/";

static const wchar_t* const kSbDiagnosticHtml =
    L"<a href=\"\" onClick=\"sendCommand(4); return false;\">%ls</a>";

// Created on the io_thread.
SafeBrowsingBlockingPage::SafeBrowsingBlockingPage(
    SafeBrowsingService* sb_service,
    SafeBrowsingService::Client* client,
    int render_process_host_id,
    int render_view_id,
    const GURL& url,
    ResourceType::Type resource_type,
    SafeBrowsingService::UrlCheckResult result)
    : sb_service_(sb_service),
      client_(client),
      render_process_host_id_(render_process_host_id),
      render_view_id_(render_view_id),
      url_(url),
      result_(result),
      proceed_(false),
      tab_(NULL),
      controller_(NULL),
      delete_pending_(false),
      is_main_frame_(resource_type == ResourceType::MAIN_FRAME),
      created_temporary_entry_(false) {
}

// Deleted on the io_thread.
SafeBrowsingBlockingPage::~SafeBrowsingBlockingPage() {
}

void SafeBrowsingBlockingPage::DisplayBlockingPage() {
  TabContents* tab = tab_util::GetTabContentsByID(render_process_host_id_,
                                                  render_view_id_);
  if (!tab || tab->type() != TAB_CONTENTS_WEB) {
    NotifyDone();
    return;
  }

  tab_ = tab;
  controller_ = tab->controller();

  // Register for notifications of events from this tab.
  NotificationService* ns = NotificationService::current();
  DCHECK(ns);
  ns->AddObserver(this, NOTIFY_TAB_CLOSING,
                  Source<NavigationController>(controller_));
  ns->AddObserver(this, NOTIFY_DOM_OPERATION_RESPONSE,
                  Source<TabContents>(tab_));

  // Hold an extra reference to ourself until the interstitial is gone.
  AddRef();

  WebContents* web_contents = tab->AsWebContents();

  // Load the HTML page and create the template components.
  DictionaryValue strings;
  ResourceBundle& rb = ResourceBundle::GetSharedInstance();
  std::string html;

  if (result_ == SafeBrowsingService::URL_MALWARE) {
    std::wstring link = StringPrintf(kSbDiagnosticHtml,
        l10n_util::GetString(IDS_SAFE_BROWSING_MALWARE_DIAGNOSTIC_PAGE).c_str());

    strings.SetString(L"badURL", UTF8ToWide(url_.host()));
    strings.SetString(L"title",
                      l10n_util::GetString(IDS_SAFE_BROWSING_MALWARE_TITLE));
    strings.SetString(L"headLine",
                      l10n_util::GetString(IDS_SAFE_BROWSING_MALWARE_HEADLINE));

    // Check to see if we're blocking the main page, or a sub-resource on the
    // main page.
    GURL top_url = tab_->GetURL();
    if (top_url == url_) {
      strings.SetString(L"description1",
          l10n_util::GetStringF(IDS_SAFE_BROWSING_MALWARE_DESCRIPTION1,
                                UTF8ToWide(url_.host())));
      strings.SetString(L"description2",
          l10n_util::GetStringF(IDS_SAFE_BROWSING_MALWARE_DESCRIPTION2,
                                link,
                                UTF8ToWide(url_.host())));
    } else {
      strings.SetString(L"description1",
          l10n_util::GetStringF(IDS_SAFE_BROWSING_MALWARE_DESCRIPTION4,
                                UTF8ToWide(top_url.host()),
                                UTF8ToWide(url_.host())));
      strings.SetString(L"description2",<commit_msg>If you click a link to a malware page or if a page redirects to malware, the malware blocking page is shown but it says that the malware page is a resource of the originating page. This is because the blocking page was using the page URL to determine if we are dealing with the main frame, and in these cases the URL was not set yet.<commit_after>// 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.
//
// Implementation of the SafeBrowsingBlockingPage class.

#include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h"

#include "chrome/app/locales/locale_settings.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browser_resources.h"
#include "chrome/browser/dom_operation_notification_details.h"
#include "chrome/browser/google_util.h"
#include "chrome/browser/navigation_controller.h"
#include "chrome/browser/navigation_entry.h"
#include "chrome/browser/tab_util.h"
#include "chrome/browser/web_contents.h"
#include "chrome/common/jstemplate_builder.h"
#include "chrome/common/l10n_util.h"
#include "chrome/common/resource_bundle.h"
#include "generated_resources.h"
#include "net/base/escape.h"


// For malware interstitial pages, we link the problematic URL to Google's
// diagnostic page.
#if defined(GOOGLE_CHROME_BUILD)
static const char* const kSbDiagnosticUrl =
    "http://safebrowsing.clients.google.com/safebrowsing/diagnostic?site=%ls&client=googlechrome";
#else
static const char* const kSbDiagnosticUrl =
    "http://safebrowsing.clients.google.com/safebrowsing/diagnostic?site=%ls&client=chromium";
#endif

static const char* const kSbReportPhishingUrl =
    "http://www.google.com/safebrowsing/report_error/";

static const wchar_t* const kSbDiagnosticHtml =
    L"<a href=\"\" onClick=\"sendCommand(4); return false;\">%ls</a>";

// Created on the io_thread.
SafeBrowsingBlockingPage::SafeBrowsingBlockingPage(
    SafeBrowsingService* sb_service,
    SafeBrowsingService::Client* client,
    int render_process_host_id,
    int render_view_id,
    const GURL& url,
    ResourceType::Type resource_type,
    SafeBrowsingService::UrlCheckResult result)
    : sb_service_(sb_service),
      client_(client),
      render_process_host_id_(render_process_host_id),
      render_view_id_(render_view_id),
      url_(url),
      result_(result),
      proceed_(false),
      tab_(NULL),
      controller_(NULL),
      delete_pending_(false),
      is_main_frame_(resource_type == ResourceType::MAIN_FRAME),
      created_temporary_entry_(false) {
}

// Deleted on the io_thread.
SafeBrowsingBlockingPage::~SafeBrowsingBlockingPage() {
}

void SafeBrowsingBlockingPage::DisplayBlockingPage() {
  TabContents* tab = tab_util::GetTabContentsByID(render_process_host_id_,
                                                  render_view_id_);
  if (!tab || tab->type() != TAB_CONTENTS_WEB) {
    NotifyDone();
    return;
  }

  tab_ = tab;
  controller_ = tab->controller();

  // Register for notifications of events from this tab.
  NotificationService* ns = NotificationService::current();
  DCHECK(ns);
  ns->AddObserver(this, NOTIFY_TAB_CLOSING,
                  Source<NavigationController>(controller_));
  ns->AddObserver(this, NOTIFY_DOM_OPERATION_RESPONSE,
                  Source<TabContents>(tab_));

  // Hold an extra reference to ourself until the interstitial is gone.
  AddRef();

  WebContents* web_contents = tab->AsWebContents();

  // Load the HTML page and create the template components.
  DictionaryValue strings;
  ResourceBundle& rb = ResourceBundle::GetSharedInstance();
  std::string html;

  if (result_ == SafeBrowsingService::URL_MALWARE) {
    std::wstring link = StringPrintf(kSbDiagnosticHtml,
        l10n_util::GetString(IDS_SAFE_BROWSING_MALWARE_DIAGNOSTIC_PAGE).c_str());

    strings.SetString(L"badURL", UTF8ToWide(url_.host()));
    strings.SetString(L"title",
                      l10n_util::GetString(IDS_SAFE_BROWSING_MALWARE_TITLE));
    strings.SetString(L"headLine",
                      l10n_util::GetString(IDS_SAFE_BROWSING_MALWARE_HEADLINE));

    // Check to see if we're blocking the main page, or a sub-resource on the
    // main page.
    if (is_main_frame_) {
      strings.SetString(L"description1",
          l10n_util::GetStringF(IDS_SAFE_BROWSING_MALWARE_DESCRIPTION1,
                                UTF8ToWide(url_.host())));
      strings.SetString(L"description2",
          l10n_util::GetStringF(IDS_SAFE_BROWSING_MALWARE_DESCRIPTION2,
                                link,
                                UTF8ToWide(url_.host())));
    } else {
      strings.SetString(L"description1",
          l10n_util::GetStringF(IDS_SAFE_BROWSING_MALWARE_DESCRIPTION4,
                                UTF8ToWide(tab_->GetURL().host()),
                                UTF8ToWide(url_.host())));
      strings.SetString(L"description2",
{'fraction_non_alphanumeric': 0.10595200997195388, 'fraction_numerical': 0.007271216370624286, 'mean_word_length': 3.6942954656265234, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '414894', 'n_tokens_mistral': 3094, 'n_tokens_neox': 2944, 'n_words': 581}