OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/passwords/manage_passwords_bubble_ui_controller.h" | 5 #include "chrome/browser/ui/passwords/manage_passwords_bubble_ui_controller.h" |
6 | 6 |
7 #include "chrome/browser/chrome_notification_types.h" | 7 #include "chrome/browser/chrome_notification_types.h" |
| 8 #include "chrome/browser/password_manager/password_store_factory.h" |
8 #include "chrome/browser/ui/browser_finder.h" | 9 #include "chrome/browser/ui/browser_finder.h" |
9 #include "chrome/browser/ui/browser_window.h" | 10 #include "chrome/browser/ui/browser_window.h" |
10 #include "chrome/browser/ui/omnibox/location_bar.h" | 11 #include "chrome/browser/ui/omnibox/location_bar.h" |
| 12 #include "components/password_manager/core/browser/password_store.h" |
11 #include "content/public/browser/notification_service.h" | 13 #include "content/public/browser/notification_service.h" |
12 | 14 |
13 using autofill::PasswordFormMap; | 15 using autofill::PasswordFormMap; |
14 using password_manager::PasswordFormManager; | 16 using password_manager::PasswordFormManager; |
15 | 17 |
| 18 namespace { |
| 19 |
| 20 password_manager::PasswordStore* GetPasswordStore( |
| 21 content::WebContents* web_contents) { |
| 22 return PasswordStoreFactory::GetForProfile( |
| 23 Profile::FromBrowserContext(web_contents->GetBrowserContext()), |
| 24 Profile::EXPLICIT_ACCESS).get(); |
| 25 } |
| 26 |
| 27 } // namespace |
| 28 |
16 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ManagePasswordsBubbleUIController); | 29 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ManagePasswordsBubbleUIController); |
17 | 30 |
18 ManagePasswordsBubbleUIController::ManagePasswordsBubbleUIController( | 31 ManagePasswordsBubbleUIController::ManagePasswordsBubbleUIController( |
19 content::WebContents* web_contents) | 32 content::WebContents* web_contents) |
20 : content::WebContentsObserver(web_contents), | 33 : content::WebContentsObserver(web_contents), |
21 manage_passwords_icon_to_be_shown_(false), | 34 manage_passwords_icon_to_be_shown_(false), |
22 password_to_be_saved_(false), | 35 password_to_be_saved_(false), |
23 manage_passwords_bubble_needs_showing_(false), | 36 manage_passwords_bubble_needs_showing_(false), |
24 password_submitted_(false), | 37 autofill_blocked_(false) { |
25 autofill_blocked_(false) {} | 38 password_manager::PasswordStore* password_store = |
| 39 GetPasswordStore(web_contents); |
| 40 if (password_store) |
| 41 password_store->AddObserver(this); |
| 42 } |
26 | 43 |
27 ManagePasswordsBubbleUIController::~ManagePasswordsBubbleUIController() {} | 44 ManagePasswordsBubbleUIController::~ManagePasswordsBubbleUIController() {} |
28 | 45 |
29 void ManagePasswordsBubbleUIController::UpdateBubbleAndIconVisibility() { | 46 void ManagePasswordsBubbleUIController::UpdateBubbleAndIconVisibility() { |
30 #if !defined(OS_ANDROID) | 47 #if !defined(OS_ANDROID) |
31 Browser* browser = chrome::FindBrowserWithWebContents(web_contents()); | 48 Browser* browser = chrome::FindBrowserWithWebContents(web_contents()); |
32 if (!browser) | 49 if (!browser) |
33 return; | 50 return; |
34 LocationBar* location_bar = browser->window()->GetLocationBar(); | 51 LocationBar* location_bar = browser->window()->GetLocationBar(); |
35 DCHECK(location_bar); | 52 DCHECK(location_bar); |
36 location_bar->UpdateManagePasswordsIconAndBubble(); | 53 location_bar->UpdateManagePasswordsIconAndBubble(); |
37 #endif | 54 #endif |
38 } | 55 } |
39 | 56 |
40 void ManagePasswordsBubbleUIController::OnPasswordSubmitted( | 57 void ManagePasswordsBubbleUIController::OnPasswordSubmitted( |
41 PasswordFormManager* form_manager) { | 58 PasswordFormManager* form_manager) { |
42 form_manager_.reset(form_manager); | 59 form_manager_.reset(form_manager); |
43 password_form_map_ = form_manager_->best_matches(); | 60 password_form_map_ = form_manager_->best_matches(); |
| 61 origin_ = pending_credentials().origin; |
44 manage_passwords_icon_to_be_shown_ = true; | 62 manage_passwords_icon_to_be_shown_ = true; |
45 password_to_be_saved_ = true; | 63 password_to_be_saved_ = true; |
46 manage_passwords_bubble_needs_showing_ = true; | 64 manage_passwords_bubble_needs_showing_ = true; |
47 password_submitted_ = true; | |
48 autofill_blocked_ = false; | 65 autofill_blocked_ = false; |
49 UpdateBubbleAndIconVisibility(); | 66 UpdateBubbleAndIconVisibility(); |
50 } | 67 } |
51 | 68 |
52 void ManagePasswordsBubbleUIController::OnPasswordAutofilled( | 69 void ManagePasswordsBubbleUIController::OnPasswordAutofilled( |
53 const PasswordFormMap& password_form_map) { | 70 const PasswordFormMap& password_form_map) { |
54 password_form_map_ = password_form_map; | 71 password_form_map_ = password_form_map; |
| 72 origin_ = password_form_map_.begin()->second->origin; |
55 manage_passwords_icon_to_be_shown_ = true; | 73 manage_passwords_icon_to_be_shown_ = true; |
56 password_to_be_saved_ = false; | 74 password_to_be_saved_ = false; |
57 manage_passwords_bubble_needs_showing_ = false; | 75 manage_passwords_bubble_needs_showing_ = false; |
58 password_submitted_ = false; | |
59 autofill_blocked_ = false; | 76 autofill_blocked_ = false; |
60 UpdateBubbleAndIconVisibility(); | 77 UpdateBubbleAndIconVisibility(); |
61 } | 78 } |
62 | 79 |
63 void ManagePasswordsBubbleUIController::OnBlacklistBlockedAutofill() { | 80 void ManagePasswordsBubbleUIController::OnBlacklistBlockedAutofill() { |
64 manage_passwords_icon_to_be_shown_ = true; | 81 manage_passwords_icon_to_be_shown_ = true; |
65 password_to_be_saved_ = false; | 82 password_to_be_saved_ = false; |
66 manage_passwords_bubble_needs_showing_ = false; | 83 manage_passwords_bubble_needs_showing_ = false; |
67 password_submitted_ = false; | |
68 autofill_blocked_ = true; | 84 autofill_blocked_ = true; |
69 UpdateBubbleAndIconVisibility(); | 85 UpdateBubbleAndIconVisibility(); |
70 } | 86 } |
71 | 87 |
72 void ManagePasswordsBubbleUIController::RemoveFromBestMatches( | 88 void ManagePasswordsBubbleUIController::WebContentsDestroyed( |
73 autofill::PasswordForm password_form) { | 89 content::WebContents* web_contents) { |
74 password_form_map_.erase(password_form.username_value); | 90 password_manager::PasswordStore* password_store = |
| 91 GetPasswordStore(web_contents); |
| 92 if (password_store) |
| 93 password_store->RemoveObserver(this); |
| 94 } |
| 95 |
| 96 void ManagePasswordsBubbleUIController::OnLoginsChanged( |
| 97 const password_manager::PasswordStoreChangeList& changes) { |
| 98 for (password_manager::PasswordStoreChangeList::const_iterator it = |
| 99 changes.begin(); |
| 100 it != changes.end(); |
| 101 it++) { |
| 102 const autofill::PasswordForm& changed_form = it->form(); |
| 103 if (changed_form.origin != origin_) |
| 104 continue; |
| 105 |
| 106 if (it->type() == password_manager::PasswordStoreChange::REMOVE) { |
| 107 password_form_map_.erase(changed_form.username_value); |
| 108 } else { |
| 109 autofill::PasswordForm* new_form = |
| 110 new autofill::PasswordForm(changed_form); |
| 111 password_form_map_[changed_form.username_value] = new_form; |
| 112 } |
| 113 } |
75 } | 114 } |
76 | 115 |
77 void ManagePasswordsBubbleUIController::OnBubbleShown() { | 116 void ManagePasswordsBubbleUIController::OnBubbleShown() { |
78 unset_manage_passwords_bubble_needs_showing(); | 117 unset_manage_passwords_bubble_needs_showing(); |
79 } | 118 } |
80 | 119 |
81 void ManagePasswordsBubbleUIController::SavePassword() { | 120 void ManagePasswordsBubbleUIController::SavePassword() { |
82 DCHECK(form_manager_.get()); | 121 DCHECK(form_manager_.get()); |
83 form_manager_->Save(); | 122 form_manager_->Save(); |
84 } | 123 } |
85 | 124 |
86 void ManagePasswordsBubbleUIController::NeverSavePassword() { | 125 void ManagePasswordsBubbleUIController::NeverSavePassword() { |
87 DCHECK(form_manager_.get()); | 126 DCHECK(form_manager_.get()); |
88 form_manager_->PermanentlyBlacklist(); | 127 form_manager_->PermanentlyBlacklist(); |
89 } | 128 } |
90 | 129 |
91 void ManagePasswordsBubbleUIController::DidNavigateMainFrame( | 130 void ManagePasswordsBubbleUIController::DidNavigateMainFrame( |
92 const content::LoadCommittedDetails& details, | 131 const content::LoadCommittedDetails& details, |
93 const content::FrameNavigateParams& params) { | 132 const content::FrameNavigateParams& params) { |
94 if (details.is_in_page) | 133 if (details.is_in_page) |
95 return; | 134 return; |
96 // Reset password states for next page. | 135 // Reset password states for next page. |
97 manage_passwords_icon_to_be_shown_ = false; | 136 manage_passwords_icon_to_be_shown_ = false; |
98 password_to_be_saved_ = false; | 137 password_to_be_saved_ = false; |
99 manage_passwords_bubble_needs_showing_ = false; | 138 manage_passwords_bubble_needs_showing_ = false; |
100 password_submitted_ = false; | |
101 UpdateBubbleAndIconVisibility(); | 139 UpdateBubbleAndIconVisibility(); |
102 } | 140 } |
OLD | NEW |