<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/ui/views/infobars/infobar_view.h"

#include "base/utf_string_conversions.h"
#include "chrome/browser/tab_contents/infobar_delegate.h"
#include "chrome/browser/ui/views/infobars/infobar_background.h"
#include "chrome/browser/ui/views/infobars/infobar_button_border.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "grit/theme_resources_standard.h"
#include "third_party/skia/include/effects/SkGradientShader.h"
#include "ui/base/accessibility/accessible_view_state.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/canvas_skia_paint.h"
#include "ui/gfx/image/image.h"
#include "views/controls/button/image_button.h"
#include "views/controls/button/menu_button.h"
#include "views/controls/button/text_button.h"
#include "views/controls/image_view.h"
#include "views/controls/label.h"
#include "views/controls/link.h"
#include "views/focus/external_focus_tracker.h"
#include "views/widget/widget.h"
#include "views/window/non_client_view.h"

#if defined(OS_WIN)
#include <shellapi.h>

#include "base/win/win_util.h"
#include "base/win/windows_version.h"
#include "ui/base/win/hwnd_util.h"
#include "ui/gfx/icon_util.h"
#endif

// static
const int InfoBar::kSeparatorLineHeight =
    views::NonClientFrameView::kClientEdgeThickness;
const int InfoBar::kDefaultArrowTargetHeight = 9;
const int InfoBar::kMaximumArrowTargetHeight = 24;
const int InfoBar::kDefaultArrowTargetHalfWidth = kDefaultArrowTargetHeight;
const int InfoBar::kMaximumArrowTargetHalfWidth = 14;

#ifdef TOUCH_UI
const int InfoBar::kDefaultBarTargetHeight = 75;
#else
const int InfoBar::kDefaultBarTargetHeight = 36;
#endif

const int InfoBarView::kButtonButtonSpacing = 10;
const int InfoBarView::kEndOfLabelSpacing = 16;
const int InfoBarView::kHorizontalPadding = 6;

InfoBarView::InfoBarView(TabContentsWrapper* owner, InfoBarDelegate* delegate)
    : InfoBar(owner, delegate),
      icon_(NULL),
      close_button_(NULL) {
  set_parent_owned(false);  // InfoBar deletes itself at the appropriate time.
  set_background(new InfoBarBackground(delegate->GetInfoBarType()));
}

InfoBarView::~InfoBarView() {
}

// static
views::Label* InfoBarView::CreateLabel(const string16& text) {
  views::Label* label = new views::Label(UTF16ToWideHack(text),
      ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::MediumFont));
  label->SetColor(SK_ColorBLACK);
  label->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
  return label;
}

// static
views::Link* InfoBarView::CreateLink(const string16& text,
                                     views::LinkListener* listener,
                                     const SkColor& background_color) {
  views::Link* link = new views::Link;
  link->SetText(UTF16ToWideHack(text));
  link->SetFont(
      ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::MediumFont));
  link->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
  link->set_listener(listener);
  link->MakeReadableOverBackgroundColor(background_color);
  return link;
}

// static
views::MenuButton* InfoBarView::CreateMenuButton(
    const string16& text,
    views::ViewMenuDelegate* menu_delegate) {
  views::MenuButton* menu_button =
      new views::MenuButton(NULL, UTF16ToWideHack(text), menu_delegate, true);
  menu_button->set_border(new InfoBarButtonBorder);
  menu_button->set_animate_on_state_change(false);
  ResourceBundle& rb = ResourceBundle::GetSharedInstance();
  menu_button->set_menu_marker(
      rb.GetBitmapNamed(IDR_INFOBARBUTTON_MENU_DROPARROW));
  menu_button->SetEnabledColor(SK_ColorBLACK);
  menu_button->SetHighlightColor(SK_ColorBLACK);
  menu_button->SetHoverColor(SK_ColorBLACK);
  menu_button->SetFont(rb.GetFont(ResourceBundle::MediumFont));
  return menu_button;
}

// static
views::TextButton* InfoBarView::CreateTextButton(
    views::ButtonListener* listener,
    const string16& text,
    bool needs_elevation) {
  views::TextButton* text_button =
      new views::TextButton(listener, UTF16ToWideHack(text));
  text_button->set_border(new InfoBarButtonBorder);
  text_button->set_animate_on_state_change(false);
  text_button->SetEnabledColor(SK_ColorBLACK);
  text_button->SetHighlightColor(SK_ColorBLACK);
  text_button->SetHoverColor(SK_ColorBLACK);
  ResourceBundle& rb = ResourceBundle::GetSharedInstance();
  text_button->SetFont(rb.GetFont(ResourceBundle::MediumFont));
#if defined(OS_WIN)
  if (needs_elevation &&
      (base::win::GetVersion() >= base::win::VERSION_VISTA) &&
      base::win::UserAccountControlIsEnabled()) {
    SHSTOCKICONINFO icon_info = { sizeof SHSTOCKICONINFO };
    // Even with the runtime guard above, we have to use GetProcAddress() here,
    // because otherwise the loader will try to resolve the function address on
    // startup, which will break on XP.
    typedef HRESULT (STDAPICALLTYPE *GetStockIconInfo)(SHSTOCKICONID, UINT,
                                                       SHSTOCKICONINFO*);
    GetStockIconInfo func = reinterpret_cast<GetStockIconInfo>(
        GetProcAddress(GetModuleHandle(L"shell32.dll"), "SHGetStockIconInfo"));
    (*func)(SIID_SHIELD, SHGSI_ICON | SHGSI_SMALLICON, &icon_info);
    text_button->SetIcon(*IconUtil::CreateSkBitmapFromHICON(icon_info.hIcon,
        gfx::Size(GetSystemMetrics(SM_CXSMICON),
                  GetSystemMetrics(SM_CYSMICON))));
  }
#endif
  return text_button;
}

void InfoBarView::Layout() {
  // Calculate the fill and stroke paths.  We do this here, rather than in
  // PlatformSpecificRecalculateHeight(), because this is also reached when our
  // width is changed, which affects both paths.
  stroke_path_.rewind();
  fill_path_.rewind();
  const InfoBarContainer::Delegate* delegate = container_delegate();
  if (delegate) {
    static_cast<InfoBarBackground*>(background())->set_separator_color(
        delegate->GetInfoBarSeparatorColor());
    int arrow_x;<commit_msg>Fix a crash and a few leaks.<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/ui/views/infobars/infobar_view.h"

#include "base/utf_string_conversions.h"
#include "base/scoped_ptr.h"
#include "chrome/browser/tab_contents/infobar_delegate.h"
#include "chrome/browser/ui/views/infobars/infobar_background.h"
#include "chrome/browser/ui/views/infobars/infobar_button_border.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "grit/theme_resources_standard.h"
#include "third_party/skia/include/effects/SkGradientShader.h"
#include "ui/base/accessibility/accessible_view_state.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/canvas_skia_paint.h"
#include "ui/gfx/image/image.h"
#include "views/controls/button/image_button.h"
#include "views/controls/button/menu_button.h"
#include "views/controls/button/text_button.h"
#include "views/controls/image_view.h"
#include "views/controls/label.h"
#include "views/controls/link.h"
#include "views/focus/external_focus_tracker.h"
#include "views/widget/widget.h"
#include "views/window/non_client_view.h"

#if defined(OS_WIN)
#include <shellapi.h>

#include "base/win/win_util.h"
#include "base/win/windows_version.h"
#include "ui/base/win/hwnd_util.h"
#include "ui/gfx/icon_util.h"
#endif

// static
const int InfoBar::kSeparatorLineHeight =
    views::NonClientFrameView::kClientEdgeThickness;
const int InfoBar::kDefaultArrowTargetHeight = 9;
const int InfoBar::kMaximumArrowTargetHeight = 24;
const int InfoBar::kDefaultArrowTargetHalfWidth = kDefaultArrowTargetHeight;
const int InfoBar::kMaximumArrowTargetHalfWidth = 14;

#ifdef TOUCH_UI
const int InfoBar::kDefaultBarTargetHeight = 75;
#else
const int InfoBar::kDefaultBarTargetHeight = 36;
#endif

const int InfoBarView::kButtonButtonSpacing = 10;
const int InfoBarView::kEndOfLabelSpacing = 16;
const int InfoBarView::kHorizontalPadding = 6;

InfoBarView::InfoBarView(TabContentsWrapper* owner, InfoBarDelegate* delegate)
    : InfoBar(owner, delegate),
      icon_(NULL),
      close_button_(NULL) {
  set_parent_owned(false);  // InfoBar deletes itself at the appropriate time.
  set_background(new InfoBarBackground(delegate->GetInfoBarType()));
}

InfoBarView::~InfoBarView() {
}

// static
views::Label* InfoBarView::CreateLabel(const string16& text) {
  views::Label* label = new views::Label(UTF16ToWideHack(text),
      ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::MediumFont));
  label->SetColor(SK_ColorBLACK);
  label->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
  return label;
}

// static
views::Link* InfoBarView::CreateLink(const string16& text,
                                     views::LinkListener* listener,
                                     const SkColor& background_color) {
  views::Link* link = new views::Link;
  link->SetText(UTF16ToWideHack(text));
  link->SetFont(
      ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::MediumFont));
  link->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
  link->set_listener(listener);
  link->MakeReadableOverBackgroundColor(background_color);
  return link;
}

// static
views::MenuButton* InfoBarView::CreateMenuButton(
    const string16& text,
    views::ViewMenuDelegate* menu_delegate) {
  views::MenuButton* menu_button =
      new views::MenuButton(NULL, UTF16ToWideHack(text), menu_delegate, true);
  menu_button->set_border(new InfoBarButtonBorder);
  menu_button->set_animate_on_state_change(false);
  ResourceBundle& rb = ResourceBundle::GetSharedInstance();
  menu_button->set_menu_marker(
      rb.GetBitmapNamed(IDR_INFOBARBUTTON_MENU_DROPARROW));
  menu_button->SetEnabledColor(SK_ColorBLACK);
  menu_button->SetHighlightColor(SK_ColorBLACK);
  menu_button->SetHoverColor(SK_ColorBLACK);
  menu_button->SetFont(rb.GetFont(ResourceBundle::MediumFont));
  return menu_button;
}

// static
views::TextButton* InfoBarView::CreateTextButton(
    views::ButtonListener* listener,
    const string16& text,
    bool needs_elevation) {
  views::TextButton* text_button =
      new views::TextButton(listener, UTF16ToWideHack(text));
  text_button->set_border(new InfoBarButtonBorder);
  text_button->set_animate_on_state_change(false);
  text_button->SetEnabledColor(SK_ColorBLACK);
  text_button->SetHighlightColor(SK_ColorBLACK);
  text_button->SetHoverColor(SK_ColorBLACK);
  ResourceBundle& rb = ResourceBundle::GetSharedInstance();
  text_button->SetFont(rb.GetFont(ResourceBundle::MediumFont));
#if defined(OS_WIN)
  if (needs_elevation &&
      (base::win::GetVersion() >= base::win::VERSION_VISTA) &&
      base::win::UserAccountControlIsEnabled()) {
    SHSTOCKICONINFO icon_info = { sizeof SHSTOCKICONINFO };
    // Even with the runtime guard above, we have to use GetProcAddress() here,
    // because otherwise the loader will try to resolve the function address on
    // startup, which will break on XP.
    typedef HRESULT (STDAPICALLTYPE *GetStockIconInfo)(SHSTOCKICONID, UINT,
                                                       SHSTOCKICONINFO*);
    GetStockIconInfo func = reinterpret_cast<GetStockIconInfo>(
        GetProcAddress(GetModuleHandle(L"shell32.dll"), "SHGetStockIconInfo"));
    if (SUCCEEDED((*func)(SIID_SHIELD, SHGSI_ICON | SHGSI_SMALLICON,
                          &icon_info))) {
      scoped_ptr<SkBitmap> icon(IconUtil::CreateSkBitmapFromHICON(
          icon_info.hIcon, gfx::Size(GetSystemMetrics(SM_CXSMICON),
                                     GetSystemMetrics(SM_CYSMICON))));
      if (icon.get())
        text_button->SetIcon(*icon);
      DestroyIcon(icon_info.hIcon);
    }
  }
#endif
  return text_button;
}

void InfoBarView::Layout() {
  // Calculate the fill and stroke paths.  We do this here, rather than in
  // PlatformSpecificRecalculateHeight(), because this is also reached when our
  // width is changed, which affects both paths.
  stroke_path_.rewind();
  fill_path_.rewind();
  const InfoBarContainer::Delegate* delegate = container_delegate();
  if (delegate) {
    static_cast<InfoBarBackground*>(background())->set_separator_color(
        delegate->GetInfoBarSeparatorColor());
    int arrow_x;
{'fraction_non_alphanumeric': 0.11696141479099678, 'fraction_numerical': 0.006430868167202572, 'mean_word_length': 5.165014866204163, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2005444', 'n_tokens_mistral': 4040, 'n_tokens_neox': 3926, 'n_words': 706}