<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/login/login_prompt.h"

#include <string>

#include "base/json/json_reader.h"
#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
#include "chrome/browser/ui/webui/chrome_url_data_manager.h"
#include "chrome/browser/ui/webui/constrained_html_ui.h"
#include "chrome/browser/ui/webui/html_dialog_ui.h"
#include "chrome/common/jstemplate_builder.h"
#include "chrome/common/url_constants.h"
#include "content/browser/tab_contents/tab_contents.h"
#include "grit/browser_resources.h"
#include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/size.h"

#if defined(USE_AURA) && !defined(OS_CHROMEOS) && !defined(TOUCH_UI)
// TODO(saintlou): This is another big fat hack like in the FileSelector.
namespace chrome {
const char kChromeUIHttpAuthHost[] = "http-auth";
const char kChromeUIHttpAuthURL[] = "chrome://http-auth/";
} // namespace chrome
#endif

class LoginHandlerSource : public ChromeURLDataManager::DataSource {
 public:
  LoginHandlerSource()
      : DataSource(chrome::kChromeUIHttpAuthHost, MessageLoop::current()) {}

  virtual void StartDataRequest(const std::string& path,
                                bool is_off_the_record,
                                int request_id) OVERRIDE {
    DictionaryValue dict;
    dict.SetString("username",
                   l10n_util::GetStringUTF16(IDS_LOGIN_DIALOG_USERNAME_FIELD));
    dict.SetString("password",
                   l10n_util::GetStringUTF16(IDS_LOGIN_DIALOG_PASSWORD_FIELD));
    dict.SetString("signin",
                   l10n_util::GetStringUTF16(IDS_LOGIN_DIALOG_OK_BUTTON_LABEL));
    dict.SetString("cancel", l10n_util::GetStringUTF16(IDS_CANCEL));

    SetFontAndTextDirection(&dict);

    const base::StringPiece html(
        ResourceBundle::GetSharedInstance().GetRawDataResource(
            IDR_HTTP_AUTH_HTML));
    std::string response = jstemplate_builder::GetI18nTemplateHtml(html, &dict);

    SendResponse(request_id, base::RefCountedString::TakeString(&response));
  }

  virtual std::string GetMimeType(const std::string& path) const OVERRIDE {
    return "text/html";
  }

  static void RegisterDataSource(Profile *profile) {
    ChromeURLDataManager* url_manager = profile->GetChromeURLDataManager();
    LoginHandlerSource *source = new LoginHandlerSource();
    url_manager->AddDataSource(source);
  }

 private:
  virtual ~LoginHandlerSource() {}

  DISALLOW_COPY_AND_ASSIGN(LoginHandlerSource);
};

class LoginHandlerHtml;

class LoginHandlerHtmlDelegate : public HtmlDialogUIDelegate,
                                 public WebUIMessageHandler {
 public:
  LoginHandlerHtmlDelegate(LoginHandlerHtml *login_handler,
                           const string16 explanation)
      : login_handler_(login_handler),
        explanation_(UTF16ToUTF8(explanation)),
        closed_(false),
        has_autofill_(false),
        ready_for_autofill_(false) {
  }

  // HtmlDialogUIDelegate methods:
  virtual bool IsDialogModal() const OVERRIDE {
    return true;
  }

  virtual string16 GetDialogTitle() const OVERRIDE {
    return l10n_util::GetStringUTF16(IDS_LOGIN_DIALOG_TITLE);
  }

  virtual GURL GetDialogContentURL() const OVERRIDE {
    return GURL(chrome::kChromeUIHttpAuthURL);
  }

  virtual void GetWebUIMessageHandlers(
      std::vector<WebUIMessageHandler*>* handlers) const OVERRIDE {
    const WebUIMessageHandler* handler = this;
    handlers->push_back(const_cast<WebUIMessageHandler*>(handler));
  }

  virtual void GetDialogSize(gfx::Size* size) const OVERRIDE {
    size->set_width(kDialogWidth);
    size->set_height(kDialogHeight);
  }

  virtual std::string GetDialogArgs() const OVERRIDE {
    return explanation_;
  }

  virtual void OnDialogClosed(const std::string& json_retval) OVERRIDE;

  virtual void OnCloseContents(TabContents* source,
                               bool* out_close_dialog) OVERRIDE {}

  virtual bool ShouldShowDialogTitle() const OVERRIDE {
    return true;
  }

  // WebUIMessageHandler method:
  virtual void RegisterMessages() OVERRIDE {
    web_ui_->RegisterMessageCallback(
        "GetAutofill",
        NewCallback(this, &LoginHandlerHtmlDelegate::GetAutofill));
  }

  void ShowAutofillData(const std::wstring& username,
                        const std::wstring& password);

 private:
  // Send autofill data to HTML once the dialog is ready and the data is
  // available.
  void SendAutofillData();<commit_msg>[webui] Migrate login_prompt_ui.cc to base::Bind().<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/login/login_prompt.h"

#include <string>

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/json/json_reader.h"
#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
#include "chrome/browser/ui/webui/chrome_url_data_manager.h"
#include "chrome/browser/ui/webui/constrained_html_ui.h"
#include "chrome/browser/ui/webui/html_dialog_ui.h"
#include "chrome/common/jstemplate_builder.h"
#include "chrome/common/url_constants.h"
#include "content/browser/tab_contents/tab_contents.h"
#include "grit/browser_resources.h"
#include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/size.h"

#if defined(USE_AURA) && !defined(OS_CHROMEOS) && !defined(TOUCH_UI)
// TODO(saintlou): This is another big fat hack like in the FileSelector.
namespace chrome {
const char kChromeUIHttpAuthHost[] = "http-auth";
const char kChromeUIHttpAuthURL[] = "chrome://http-auth/";
} // namespace chrome
#endif

class LoginHandlerSource : public ChromeURLDataManager::DataSource {
 public:
  LoginHandlerSource()
      : DataSource(chrome::kChromeUIHttpAuthHost, MessageLoop::current()) {}

  virtual void StartDataRequest(const std::string& path,
                                bool is_off_the_record,
                                int request_id) OVERRIDE {
    DictionaryValue dict;
    dict.SetString("username",
                   l10n_util::GetStringUTF16(IDS_LOGIN_DIALOG_USERNAME_FIELD));
    dict.SetString("password",
                   l10n_util::GetStringUTF16(IDS_LOGIN_DIALOG_PASSWORD_FIELD));
    dict.SetString("signin",
                   l10n_util::GetStringUTF16(IDS_LOGIN_DIALOG_OK_BUTTON_LABEL));
    dict.SetString("cancel", l10n_util::GetStringUTF16(IDS_CANCEL));

    SetFontAndTextDirection(&dict);

    const base::StringPiece html(
        ResourceBundle::GetSharedInstance().GetRawDataResource(
            IDR_HTTP_AUTH_HTML));
    std::string response = jstemplate_builder::GetI18nTemplateHtml(html, &dict);

    SendResponse(request_id, base::RefCountedString::TakeString(&response));
  }

  virtual std::string GetMimeType(const std::string& path) const OVERRIDE {
    return "text/html";
  }

  static void RegisterDataSource(Profile *profile) {
    ChromeURLDataManager* url_manager = profile->GetChromeURLDataManager();
    LoginHandlerSource *source = new LoginHandlerSource();
    url_manager->AddDataSource(source);
  }

 private:
  virtual ~LoginHandlerSource() {}

  DISALLOW_COPY_AND_ASSIGN(LoginHandlerSource);
};

class LoginHandlerHtml;

class LoginHandlerHtmlDelegate : public HtmlDialogUIDelegate,
                                 public WebUIMessageHandler {
 public:
  LoginHandlerHtmlDelegate(LoginHandlerHtml *login_handler,
                           const string16 explanation)
      : login_handler_(login_handler),
        explanation_(UTF16ToUTF8(explanation)),
        closed_(false),
        has_autofill_(false),
        ready_for_autofill_(false) {
  }

  // HtmlDialogUIDelegate methods:
  virtual bool IsDialogModal() const OVERRIDE {
    return true;
  }

  virtual string16 GetDialogTitle() const OVERRIDE {
    return l10n_util::GetStringUTF16(IDS_LOGIN_DIALOG_TITLE);
  }

  virtual GURL GetDialogContentURL() const OVERRIDE {
    return GURL(chrome::kChromeUIHttpAuthURL);
  }

  virtual void GetWebUIMessageHandlers(
      std::vector<WebUIMessageHandler*>* handlers) const OVERRIDE {
    const WebUIMessageHandler* handler = this;
    handlers->push_back(const_cast<WebUIMessageHandler*>(handler));
  }

  virtual void GetDialogSize(gfx::Size* size) const OVERRIDE {
    size->set_width(kDialogWidth);
    size->set_height(kDialogHeight);
  }

  virtual std::string GetDialogArgs() const OVERRIDE {
    return explanation_;
  }

  virtual void OnDialogClosed(const std::string& json_retval) OVERRIDE;

  virtual void OnCloseContents(TabContents* source,
                               bool* out_close_dialog) OVERRIDE {}

  virtual bool ShouldShowDialogTitle() const OVERRIDE {
    return true;
  }

  // WebUIMessageHandler method:
  virtual void RegisterMessages() OVERRIDE {
    web_ui_->RegisterMessageCallback(
        "GetAutofill",
        base::Bind(&LoginHandlerHtmlDelegate::GetAutofill,
                   base::Unretained(this)));
  }

  void ShowAutofillData(const std::wstring& username,
                        const std::wstring& password);

 private:
  // Send autofill data to HTML once the dialog is ready and the data is
  // available.
  void SendAutofillData();
{'fraction_non_alphanumeric': 0.1030353086929589, 'fraction_numerical': 0.007639892628536031, 'mean_word_length': 4.008790072388831, 'pattern_counts': {'":': 0, '<': 9, '<?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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '401204', 'n_tokens_mistral': 3053, 'n_tokens_neox': 2947, 'n_words': 571}