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 3 // found in the LICENSE file. 4 5 #include "chrome/browser/ui/passwords/manage_passwords_bubble_ui_controller.h" 6 7 #include "chrome/browser/chrome_notification_types.h" 8 #include "chrome/browser/ui/browser_finder.h" 9 #include "chrome/browser/ui/browser_window.h" 10 #include "chrome/browser/ui/omnibox/location_bar.h" 11 #include "content/public/browser/notification_service.h" 12 13 using autofill::PasswordFormMap; 14 15 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ManagePasswordsBubbleUIController); 16 17 ManagePasswordsBubbleUIController::ManagePasswordsBubbleUIController( 18 content::WebContents* web_contents) 19 : content::WebContentsObserver(web_contents), 20 manage_passwords_icon_to_be_shown_(false), 21 password_to_be_saved_(false), 22 manage_passwords_bubble_needs_showing_(false), 23 password_submitted_(false) {} 24 25 ManagePasswordsBubbleUIController::~ManagePasswordsBubbleUIController() {} 26 27 void ManagePasswordsBubbleUIController::UpdateBubbleAndIconVisibility() { 28 #if !defined(OS_ANDROID) 29 Browser* browser = chrome::FindBrowserWithWebContents(web_contents()); 30 if (!browser) 31 return; 32 LocationBar* location_bar = browser->window()->GetLocationBar(); 33 DCHECK(location_bar); 34 location_bar->UpdateManagePasswordsIconAndBubble(); 35 #endif 36 } 37 38 void ManagePasswordsBubbleUIController::OnPasswordSubmitted( 39 PasswordFormManager* form_manager) { 40 form_manager_.reset(form_manager); 41 password_form_map_ = form_manager_->best_matches(); 42 manage_passwords_icon_to_be_shown_ = true; 43 password_to_be_saved_ = true; 44 manage_passwords_bubble_needs_showing_ = true; 45 password_submitted_ = true; 46 UpdateBubbleAndIconVisibility(); 47 } 48 49 void ManagePasswordsBubbleUIController::OnPasswordAutofilled( 50 const PasswordFormMap& password_form_map) { 51 password_form_map_ = password_form_map; 52 manage_passwords_icon_to_be_shown_ = true; 53 password_to_be_saved_ = false; 54 manage_passwords_bubble_needs_showing_ = false; 55 password_submitted_ = false; 56 UpdateBubbleAndIconVisibility(); 57 } 58 59 void ManagePasswordsBubbleUIController::RemoveFromBestMatches( 60 autofill::PasswordForm password_form) { 61 password_form_map_.erase(password_form.username_value); 62 } 63 64 void ManagePasswordsBubbleUIController::OnBubbleShown() { 65 unset_manage_passwords_bubble_needs_showing(); 66 } 67 68 void ManagePasswordsBubbleUIController::SavePassword() { 69 DCHECK(form_manager_.get()); 70 form_manager_->Save(); 71 } 72 73 void ManagePasswordsBubbleUIController::DidNavigateMainFrame( 74 const content::LoadCommittedDetails& details, 75 const content::FrameNavigateParams& params) { 76 if (details.is_in_page) 77 return; 78 // Reset password states for next page. 79 manage_passwords_icon_to_be_shown_ = false; 80 password_to_be_saved_ = false; 81 manage_passwords_bubble_needs_showing_ = false; 82 password_submitted_ = false; 83 UpdateBubbleAndIconVisibility(); 84 } 85