<commit_before>// Copyright (c) 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 "app/gfx/font.h"
#include "app/l10n_util.h"
#include "app/win_util.h"
#include "chrome/browser/browser_list.h"
#include "chrome/browser/browser_window.h"
#include "chrome/browser/chrome_thread.h"
#include "chrome/browser/extensions/extensions_ui.h"
#include "chrome/browser/extensions/pack_extension_job.h"
#include "chrome/browser/shell_dialogs.h"
#include "grit/generated_resources.h"
#include "views/controls/label.h"
#include "views/controls/button/native_button.h"
#include "views/controls/textfield/textfield.h"
#include "views/standard_layout.h"
#include "views/view.h"
#include "views/window/dialog_client_view.h"
#include "views/window/dialog_delegate.h"
#include "views/window/window.h"

namespace {

// Puts up the the pack dialog, which has this basic layout:
//
// Select the extension to pack.
//
// Extension root:      [            ] [browse]
// Extension key file:  [            ] [browse]
//
//                                [ok] [cancel]
class PackDialogContent
  : public views::View,
    public PackExtensionJob::Client,
    public SelectFileDialog::Listener,
    public views::ButtonListener,
    public views::DialogDelegate {
 public:
  PackDialogContent() : ok_button_(NULL) {
    using views::GridLayout;

    // Setup the layout.
    views::GridLayout* layout = CreatePanelGridLayout(this);
    SetLayoutManager(layout);

    views::ColumnSet* columns = layout->AddColumnSet(0);
    columns->AddColumn(GridLayout::LEADING, GridLayout::FILL, 1,
                       GridLayout::USE_PREF, 0, 0);
    columns->AddPaddingColumn(0, kRelatedControlHorizontalSpacing);
    columns->AddColumn(GridLayout::LEADING, GridLayout::CENTER, 1,
                       GridLayout::USE_PREF, 0, 0);
    columns->AddPaddingColumn(0, kRelatedControlHorizontalSpacing);
    columns->AddColumn(GridLayout::LEADING, GridLayout::FILL, 1,
                       GridLayout::USE_PREF, 0, 0);

    layout->StartRow(0, 0);
    views::Label* heading = new views::Label(
        l10n_util::GetString(IDS_EXTENSION_PACK_DIALOG_HEADING));
    heading->SetMultiLine(true);
    heading->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
    layout->AddView(heading, 5, 1, GridLayout::FILL, GridLayout::LEADING);

    layout->AddPaddingRow(0, kUnrelatedControlVerticalSpacing);

    layout->StartRow(0, 0);
    layout->AddView(new views::Label(l10n_util::GetString(
        IDS_EXTENSION_PACK_DIALOG_ROOT_DIRECTORY_LABEL)));
    extension_root_textbox_ = new views::Textfield();
    extension_root_textbox_->set_default_width_in_chars(32);
    layout->AddView(extension_root_textbox_);
    extension_root_button_ = new views::NativeButton(this, l10n_util::GetString(
        IDS_EXTENSION_PACK_DIALOG_BROWSE));
    layout->AddView(extension_root_button_);

    layout->AddPaddingRow(0, kRelatedControlVerticalSpacing);

    layout->StartRow(0, 0);
    layout->AddView(new views::Label(l10n_util::GetString(
        IDS_EXTENSION_PACK_DIALOG_PRIVATE_KEY_LABEL)));
    private_key_textbox_ = new views::Textfield();
    private_key_textbox_->set_default_width_in_chars(32);
    layout->AddView(private_key_textbox_);
    private_key_button_ = new views::NativeButton(this, l10n_util::GetString(
      IDS_EXTENSION_PACK_DIALOG_BROWSE));
    layout->AddView(private_key_button_);
  }

  // PackExtensionJob::Client
  virtual void OnPackSuccess(const FilePath& crx_file,
                             const FilePath& pem_file) {
    ok_button_->SetEnabled(true);
    std::wstring message;
    if (!pem_file.empty()) {
      message = l10n_util::GetStringF(
          IDS_EXTENSION_PACK_DIALOG_SUCCESS_BODY_NEW,
          crx_file.ToWStringHack().c_str(),
          pem_file.ToWStringHack().c_str());
    } else {
      message = l10n_util::GetStringF(
          IDS_EXTENSION_PACK_DIALOG_SUCCESS_BODY_UPDATE,
          crx_file.ToWStringHack().c_str());
    }
    win_util::MessageBox(GetWindow()->GetNativeWindow(), message,
        l10n_util::GetString(IDS_EXTENSION_PACK_DIALOG_SUCCESS_TITLE),
        MB_OK | MB_SETFOREGROUND);
    GetWindow()->Close();
  }

  void OnPackFailure(const std::wstring& error) {
    ok_button_->SetEnabled(true);
    win_util::MessageBox(GetWindow()->GetNativeWindow(), error,
        l10n_util::GetString(IDS_EXTENSION_PACK_DIALOG_FAILURE_TITLE),
        MB_OK | MB_SETFOREGROUND);
  }

 private:
  // DialogDelegate
  virtual bool Accept() {
    FilePath root_directory = FilePath::FromWStringHack(
        extension_root_textbox_->text());
    FilePath key_file = FilePath::FromWStringHack(private_key_textbox_->text());

    if (root_directory.empty()) {
      if (extension_root_textbox_->text().empty()) {
        OnPackFailure(l10n_util::GetString(<commit_msg>Land review 193091: Avoid unnecessary conversions between wchar_t and wstring.<commit_after>// Copyright (c) 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 "app/gfx/font.h"
#include "app/l10n_util.h"
#include "app/win_util.h"
#include "chrome/browser/browser_list.h"
#include "chrome/browser/browser_window.h"
#include "chrome/browser/chrome_thread.h"
#include "chrome/browser/extensions/extensions_ui.h"
#include "chrome/browser/extensions/pack_extension_job.h"
#include "chrome/browser/shell_dialogs.h"
#include "grit/generated_resources.h"
#include "views/controls/label.h"
#include "views/controls/button/native_button.h"
#include "views/controls/textfield/textfield.h"
#include "views/standard_layout.h"
#include "views/view.h"
#include "views/window/dialog_client_view.h"
#include "views/window/dialog_delegate.h"
#include "views/window/window.h"

namespace {

// Puts up the the pack dialog, which has this basic layout:
//
// Select the extension to pack.
//
// Extension root:      [            ] [browse]
// Extension key file:  [            ] [browse]
//
//                                [ok] [cancel]
class PackDialogContent
  : public views::View,
    public PackExtensionJob::Client,
    public SelectFileDialog::Listener,
    public views::ButtonListener,
    public views::DialogDelegate {
 public:
  PackDialogContent() : ok_button_(NULL) {
    using views::GridLayout;

    // Setup the layout.
    views::GridLayout* layout = CreatePanelGridLayout(this);
    SetLayoutManager(layout);

    views::ColumnSet* columns = layout->AddColumnSet(0);
    columns->AddColumn(GridLayout::LEADING, GridLayout::FILL, 1,
                       GridLayout::USE_PREF, 0, 0);
    columns->AddPaddingColumn(0, kRelatedControlHorizontalSpacing);
    columns->AddColumn(GridLayout::LEADING, GridLayout::CENTER, 1,
                       GridLayout::USE_PREF, 0, 0);
    columns->AddPaddingColumn(0, kRelatedControlHorizontalSpacing);
    columns->AddColumn(GridLayout::LEADING, GridLayout::FILL, 1,
                       GridLayout::USE_PREF, 0, 0);

    layout->StartRow(0, 0);
    views::Label* heading = new views::Label(
        l10n_util::GetString(IDS_EXTENSION_PACK_DIALOG_HEADING));
    heading->SetMultiLine(true);
    heading->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
    layout->AddView(heading, 5, 1, GridLayout::FILL, GridLayout::LEADING);

    layout->AddPaddingRow(0, kUnrelatedControlVerticalSpacing);

    layout->StartRow(0, 0);
    layout->AddView(new views::Label(l10n_util::GetString(
        IDS_EXTENSION_PACK_DIALOG_ROOT_DIRECTORY_LABEL)));
    extension_root_textbox_ = new views::Textfield();
    extension_root_textbox_->set_default_width_in_chars(32);
    layout->AddView(extension_root_textbox_);
    extension_root_button_ = new views::NativeButton(this, l10n_util::GetString(
        IDS_EXTENSION_PACK_DIALOG_BROWSE));
    layout->AddView(extension_root_button_);

    layout->AddPaddingRow(0, kRelatedControlVerticalSpacing);

    layout->StartRow(0, 0);
    layout->AddView(new views::Label(l10n_util::GetString(
        IDS_EXTENSION_PACK_DIALOG_PRIVATE_KEY_LABEL)));
    private_key_textbox_ = new views::Textfield();
    private_key_textbox_->set_default_width_in_chars(32);
    layout->AddView(private_key_textbox_);
    private_key_button_ = new views::NativeButton(this, l10n_util::GetString(
      IDS_EXTENSION_PACK_DIALOG_BROWSE));
    layout->AddView(private_key_button_);
  }

  // PackExtensionJob::Client
  virtual void OnPackSuccess(const FilePath& crx_file,
                             const FilePath& pem_file) {
    ok_button_->SetEnabled(true);
    std::wstring message;
    if (!pem_file.empty()) {
      message = l10n_util::GetStringF(
          IDS_EXTENSION_PACK_DIALOG_SUCCESS_BODY_NEW,
          crx_file.ToWStringHack(),
          pem_file.ToWStringHack());
    } else {
      message = l10n_util::GetStringF(
          IDS_EXTENSION_PACK_DIALOG_SUCCESS_BODY_UPDATE,
          crx_file.ToWStringHack());
    }
    win_util::MessageBox(GetWindow()->GetNativeWindow(), message,
        l10n_util::GetString(IDS_EXTENSION_PACK_DIALOG_SUCCESS_TITLE),
        MB_OK | MB_SETFOREGROUND);
    GetWindow()->Close();
  }

  void OnPackFailure(const std::wstring& error) {
    ok_button_->SetEnabled(true);
    win_util::MessageBox(GetWindow()->GetNativeWindow(), error,
        l10n_util::GetString(IDS_EXTENSION_PACK_DIALOG_FAILURE_TITLE),
        MB_OK | MB_SETFOREGROUND);
  }

 private:
  // DialogDelegate
  virtual bool Accept() {
    FilePath root_directory = FilePath::FromWStringHack(
        extension_root_textbox_->text());
    FilePath key_file = FilePath::FromWStringHack(private_key_textbox_->text());

    if (root_directory.empty()) {
      if (extension_root_textbox_->text().empty()) {
        OnPackFailure(l10n_util::GetString(
{'fraction_non_alphanumeric': 0.11955967791254714, 'fraction_numerical': 0.011211905004586688, 'mean_word_length': 4.1210855949895615, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 63, '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': '7246235', 'n_tokens_mistral': 3244, 'n_tokens_neox': 3130, 'n_words': 502}