1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/password_manager_delegate_impl.h"
6
7 #include "base/memory/singleton.h"
8 #include "base/metrics/histogram.h"
9 #include "chrome/browser/password_manager/password_form_manager.h"
10 #include "chrome/browser/password_manager/password_manager.h"
11 #include "chrome/browser/tab_contents/confirm_infobar_delegate.h"
12 #include "chrome/common/autofill_messages.h"
13 #include "content/browser/renderer_host/render_view_host.h"
14 #include "content/browser/tab_contents/tab_contents.h"
15 #include "grit/chromium_strings.h"
16 #include "grit/generated_resources.h"
17 #include "grit/theme_resources.h"
18 #include "ui/base/l10n/l10n_util.h"
19 #include "ui/base/resource/resource_bundle.h"
20 #include "webkit/glue/password_form.h"
21
22 // After a successful *new* login attempt, we take the PasswordFormManager in
23 // provisional_save_manager_ and move it to a SavePasswordInfoBarDelegate while
24 // the user makes up their mind with the "save password" infobar. Note if the
25 // login is one we already know about, the end of the line is
26 // provisional_save_manager_ because we just update it on success and so such
27 // forms never end up in an infobar.
28 class SavePasswordInfoBarDelegate : public ConfirmInfoBarDelegate {
29 public:
30 SavePasswordInfoBarDelegate(TabContents* tab_contents,
31 PasswordFormManager* form_to_save);
32
33 private:
34 enum ResponseType {
35 NO_RESPONSE = 0,
36 REMEMBER_PASSWORD,
37 DONT_REMEMBER_PASSWORD,
38 NUM_RESPONSE_TYPES,
39 };
40
41 virtual ~SavePasswordInfoBarDelegate();
42
43 // ConfirmInfoBarDelegate
44 virtual void InfoBarClosed();
45 virtual SkBitmap* GetIcon() const;
46 virtual Type GetInfoBarType() const;
47 virtual string16 GetMessageText() const;
48 virtual string16 GetButtonLabel(InfoBarButton button) const;
49 virtual bool Accept();
50 virtual bool Cancel();
51
52 // The PasswordFormManager managing the form we're asking the user about,
53 // and should update as per her decision.
54 scoped_ptr<PasswordFormManager> form_to_save_;
55
56 // Used to track the results we get from the info bar.
57 ResponseType infobar_response_;
58
59 DISALLOW_COPY_AND_ASSIGN(SavePasswordInfoBarDelegate);
60 };
61
SavePasswordInfoBarDelegate(TabContents * tab_contents,PasswordFormManager * form_to_save)62 SavePasswordInfoBarDelegate::SavePasswordInfoBarDelegate(
63 TabContents* tab_contents,
64 PasswordFormManager* form_to_save)
65 : ConfirmInfoBarDelegate(tab_contents),
66 form_to_save_(form_to_save),
67 infobar_response_(NO_RESPONSE) {
68 }
69
~SavePasswordInfoBarDelegate()70 SavePasswordInfoBarDelegate::~SavePasswordInfoBarDelegate() {
71 }
72
InfoBarClosed()73 void SavePasswordInfoBarDelegate::InfoBarClosed() {
74 UMA_HISTOGRAM_ENUMERATION("PasswordManager.InfoBarResponse",
75 infobar_response_, NUM_RESPONSE_TYPES);
76 delete this;
77 }
78
GetIcon() const79 SkBitmap* SavePasswordInfoBarDelegate::GetIcon() const {
80 return ResourceBundle::GetSharedInstance().GetBitmapNamed(
81 IDR_INFOBAR_SAVE_PASSWORD);
82 }
83
GetInfoBarType() const84 InfoBarDelegate::Type SavePasswordInfoBarDelegate::GetInfoBarType() const {
85 return PAGE_ACTION_TYPE;
86 }
87
GetMessageText() const88 string16 SavePasswordInfoBarDelegate::GetMessageText() const {
89 return l10n_util::GetStringUTF16(IDS_PASSWORD_MANAGER_SAVE_PASSWORD_PROMPT);
90 }
91
GetButtonLabel(InfoBarButton button) const92 string16 SavePasswordInfoBarDelegate::GetButtonLabel(
93 InfoBarButton button) const {
94 return l10n_util::GetStringUTF16((button == BUTTON_OK) ?
95 IDS_PASSWORD_MANAGER_SAVE_BUTTON : IDS_PASSWORD_MANAGER_BLACKLIST_BUTTON);
96 }
97
Accept()98 bool SavePasswordInfoBarDelegate::Accept() {
99 DCHECK(form_to_save_.get());
100 form_to_save_->Save();
101 infobar_response_ = REMEMBER_PASSWORD;
102 return true;
103 }
104
Cancel()105 bool SavePasswordInfoBarDelegate::Cancel() {
106 DCHECK(form_to_save_.get());
107 form_to_save_->PermanentlyBlacklist();
108 infobar_response_ = DONT_REMEMBER_PASSWORD;
109 return true;
110 }
111
112
113 // PasswordManagerDelegateImpl ------------------------------------------------
114
FillPasswordForm(const webkit_glue::PasswordFormFillData & form_data)115 void PasswordManagerDelegateImpl::FillPasswordForm(
116 const webkit_glue::PasswordFormFillData& form_data) {
117 tab_contents_->render_view_host()->Send(new AutofillMsg_FillPasswordForm(
118 tab_contents_->render_view_host()->routing_id(), form_data));
119 }
120
AddSavePasswordInfoBar(PasswordFormManager * form_to_save)121 void PasswordManagerDelegateImpl::AddSavePasswordInfoBar(
122 PasswordFormManager* form_to_save) {
123 tab_contents_->AddInfoBar(
124 new SavePasswordInfoBarDelegate(tab_contents_, form_to_save));
125 }
126
GetProfileForPasswordManager()127 Profile* PasswordManagerDelegateImpl::GetProfileForPasswordManager() {
128 return tab_contents_->profile();
129 }
130
DidLastPageLoadEncounterSSLErrors()131 bool PasswordManagerDelegateImpl::DidLastPageLoadEncounterSSLErrors() {
132 return tab_contents_->controller().ssl_manager()->
133 ProcessedSSLErrorFromRequest();
134 }
135