172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch// Use of this source code is governed by a BSD-style license that can be
3201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch// found in the LICENSE file.
4201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch
5201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch#include "chrome/browser/password_manager_delegate_impl.h"
6201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch
7ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "base/memory/singleton.h"
8201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch#include "base/metrics/histogram.h"
9201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch#include "chrome/browser/password_manager/password_form_manager.h"
10201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch#include "chrome/browser/password_manager/password_manager.h"
11dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include "chrome/browser/tab_contents/confirm_infobar_delegate.h"
1272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen#include "chrome/common/autofill_messages.h"
13dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include "content/browser/renderer_host/render_view_host.h"
14dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include "content/browser/tab_contents/tab_contents.h"
15201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch#include "grit/chromium_strings.h"
16201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch#include "grit/generated_resources.h"
17201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch#include "grit/theme_resources.h"
1872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen#include "ui/base/l10n/l10n_util.h"
1972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen#include "ui/base/resource/resource_bundle.h"
20201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch#include "webkit/glue/password_form.h"
21201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch
22201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch// After a successful *new* login attempt, we take the PasswordFormManager in
23201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch// provisional_save_manager_ and move it to a SavePasswordInfoBarDelegate while
24201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch// the user makes up their mind with the "save password" infobar. Note if the
25201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch// login is one we already know about, the end of the line is
26201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch// provisional_save_manager_ because we just update it on success and so such
27201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch// forms never end up in an infobar.
28201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdochclass SavePasswordInfoBarDelegate : public ConfirmInfoBarDelegate {
29201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch public:
30201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  SavePasswordInfoBarDelegate(TabContents* tab_contents,
3172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen                              PasswordFormManager* form_to_save);
32201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch
33201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch private:
34201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  enum ResponseType {
35201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch    NO_RESPONSE = 0,
36201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch    REMEMBER_PASSWORD,
37201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch    DONT_REMEMBER_PASSWORD,
38201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch    NUM_RESPONSE_TYPES,
39201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  };
4072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
4172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  virtual ~SavePasswordInfoBarDelegate();
4272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
4372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // ConfirmInfoBarDelegate
4472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  virtual void InfoBarClosed();
4572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  virtual SkBitmap* GetIcon() const;
4672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  virtual Type GetInfoBarType() const;
4772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  virtual string16 GetMessageText() const;
4872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  virtual string16 GetButtonLabel(InfoBarButton button) const;
4972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  virtual bool Accept();
5072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  virtual bool Cancel();
5172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
5272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // The PasswordFormManager managing the form we're asking the user about,
5372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // and should update as per her decision.
5472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  scoped_ptr<PasswordFormManager> form_to_save_;
5572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
5672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // Used to track the results we get from the info bar.
57201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  ResponseType infobar_response_;
58201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch
59201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  DISALLOW_COPY_AND_ASSIGN(SavePasswordInfoBarDelegate);
60201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch};
61201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch
6272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian MonsenSavePasswordInfoBarDelegate::SavePasswordInfoBarDelegate(
6372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen    TabContents* tab_contents,
6472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen    PasswordFormManager* form_to_save)
6572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen    : ConfirmInfoBarDelegate(tab_contents),
6672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen      form_to_save_(form_to_save),
6772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen      infobar_response_(NO_RESPONSE) {
6872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen}
6972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
7072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian MonsenSavePasswordInfoBarDelegate::~SavePasswordInfoBarDelegate() {
7172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen}
7272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
7372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenvoid SavePasswordInfoBarDelegate::InfoBarClosed() {
7472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  UMA_HISTOGRAM_ENUMERATION("PasswordManager.InfoBarResponse",
7572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen                            infobar_response_, NUM_RESPONSE_TYPES);
7672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  delete this;
7772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen}
7872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
7972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian MonsenSkBitmap* SavePasswordInfoBarDelegate::GetIcon() const {
8072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  return ResourceBundle::GetSharedInstance().GetBitmapNamed(
8172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen      IDR_INFOBAR_SAVE_PASSWORD);
8272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen}
8372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
8472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian MonsenInfoBarDelegate::Type SavePasswordInfoBarDelegate::GetInfoBarType() const {
8572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  return PAGE_ACTION_TYPE;
8672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen}
8772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
8872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenstring16 SavePasswordInfoBarDelegate::GetMessageText() const {
8972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  return l10n_util::GetStringUTF16(IDS_PASSWORD_MANAGER_SAVE_PASSWORD_PROMPT);
9072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen}
9172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
9272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenstring16 SavePasswordInfoBarDelegate::GetButtonLabel(
9372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen    InfoBarButton button) const {
9472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  return l10n_util::GetStringUTF16((button == BUTTON_OK) ?
9572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen      IDS_PASSWORD_MANAGER_SAVE_BUTTON : IDS_PASSWORD_MANAGER_BLACKLIST_BUTTON);
9672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen}
9772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
9872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenbool SavePasswordInfoBarDelegate::Accept() {
9972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  DCHECK(form_to_save_.get());
10072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  form_to_save_->Save();
10172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  infobar_response_ = REMEMBER_PASSWORD;
10272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  return true;
10372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen}
10472a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
10572a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsenbool SavePasswordInfoBarDelegate::Cancel() {
10672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  DCHECK(form_to_save_.get());
10772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  form_to_save_->PermanentlyBlacklist();
10872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  infobar_response_ = DONT_REMEMBER_PASSWORD;
10972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  return true;
11072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen}
11172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
11272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen
11372a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// PasswordManagerDelegateImpl ------------------------------------------------
114201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch
115201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdochvoid PasswordManagerDelegateImpl::FillPasswordForm(
116201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch    const webkit_glue::PasswordFormFillData& form_data) {
117ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  tab_contents_->render_view_host()->Send(new AutofillMsg_FillPasswordForm(
11872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen      tab_contents_->render_view_host()->routing_id(), form_data));
119201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch}
120201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch
121201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdochvoid PasswordManagerDelegateImpl::AddSavePasswordInfoBar(
122201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch    PasswordFormManager* form_to_save) {
123201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  tab_contents_->AddInfoBar(
124201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch      new SavePasswordInfoBarDelegate(tab_contents_, form_to_save));
125201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch}
126201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch
127201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben MurdochProfile* PasswordManagerDelegateImpl::GetProfileForPasswordManager() {
128201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  return tab_contents_->profile();
129201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch}
130201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch
131201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdochbool PasswordManagerDelegateImpl::DidLastPageLoadEncounterSSLErrors() {
132201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  return tab_contents_->controller().ssl_manager()->
133201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch      ProcessedSSLErrorFromRequest();
134201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch}
135