Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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/autofill/save_card_bubble_controller_impl.h" | |
| 6 | |
| 7 #include "chrome/browser/ui/autofill/save_card_bubble_view.h" | |
| 8 #include "chrome/browser/ui/browser.h" | |
| 9 #include "chrome/browser/ui/browser_finder.h" | |
| 10 #include "chrome/browser/ui/browser_window.h" | |
| 11 #include "chrome/browser/ui/location_bar/location_bar.h" | |
| 12 #include "content/public/browser/navigation_details.h" | |
| 13 | |
| 14 DEFINE_WEB_CONTENTS_USER_DATA_KEY(autofill::SaveCardBubbleControllerImpl); | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 // Number of seconds the bubble and icon will survive navigations, starting | |
| 19 // from when the bubble is shown. | |
| 20 // TODO(bondd): Share with ManagePasswordsUIController. | |
| 21 const int kSurviveNavigationSeconds = 5; | |
| 22 | |
| 23 } // namespace | |
| 24 | |
| 25 namespace autofill { | |
| 26 | |
| 27 SaveCardBubbleControllerImpl::SaveCardBubbleControllerImpl( | |
| 28 content::WebContents* web_contents) | |
| 29 : content::WebContentsObserver(web_contents), | |
| 30 save_card_bubble_view_(nullptr) { | |
| 31 DCHECK(web_contents); | |
| 32 } | |
| 33 | |
| 34 SaveCardBubbleControllerImpl::~SaveCardBubbleControllerImpl() { | |
| 35 if (save_card_bubble_view_) | |
| 36 save_card_bubble_view_->ControllerGone(); | |
| 37 } | |
| 38 | |
| 39 void SaveCardBubbleControllerImpl::SetCallback( | |
| 40 const base::Closure& save_card_callback) { | |
| 41 save_card_callback_ = save_card_callback; | |
| 42 } | |
| 43 | |
| 44 void SaveCardBubbleControllerImpl::ShowBubble() { | |
| 45 DCHECK(!save_card_callback_.is_null()); | |
| 46 | |
| 47 // Need to create location bar icon before bubble, otherwise bubble will be | |
| 48 // unanchored. | |
| 49 UpdateIcon(); | |
| 50 | |
| 51 Browser* browser = chrome::FindBrowserWithWebContents(web_contents()); | |
| 52 save_card_bubble_view_ = | |
| 53 browser->window()->ShowSaveCreditCardBubble(web_contents(), this); | |
| 54 DCHECK(save_card_bubble_view_); | |
| 55 | |
| 56 // Update icon after creating |save_card_bubble_view_| so that icon will show | |
| 57 // its "toggled on" state. | |
| 58 UpdateIcon(); | |
| 59 | |
| 60 timer_.reset(new base::ElapsedTimer()); | |
| 61 } | |
| 62 | |
| 63 bool SaveCardBubbleControllerImpl::IsIconVisible() const { | |
| 64 return !save_card_callback_.is_null(); | |
| 65 } | |
| 66 | |
| 67 bool SaveCardBubbleControllerImpl::IsIconToggled() const { | |
| 68 return !!save_card_bubble_view_; | |
| 69 } | |
| 70 | |
| 71 SaveCardBubbleView* SaveCardBubbleControllerImpl::save_card_bubble_view() | |
| 72 const { | |
| 73 return save_card_bubble_view_; | |
| 74 } | |
| 75 | |
| 76 void SaveCardBubbleControllerImpl::OnSaveButton() { | |
| 77 save_card_callback_.Run(); | |
| 78 save_card_callback_.Reset(); | |
| 79 } | |
| 80 | |
| 81 void SaveCardBubbleControllerImpl::OnCancelButton() { | |
| 82 save_card_callback_.Reset(); | |
| 83 } | |
| 84 | |
| 85 void SaveCardBubbleControllerImpl::OnBubbleClosed() { | |
| 86 save_card_bubble_view_ = nullptr; | |
|
Evan Stade
2015/10/23 21:56:30
reset the callback here instead of above in save a
bondd
2015/10/23 22:58:28
Pressing the cancel button causes different behavi
| |
| 87 UpdateIcon(); | |
| 88 } | |
| 89 | |
| 90 void SaveCardBubbleControllerImpl::UpdateIcon() { | |
| 91 Browser* browser = chrome::FindBrowserWithWebContents(web_contents()); | |
| 92 LocationBar* location_bar = browser->window()->GetLocationBar(); | |
| 93 location_bar->UpdateSaveCreditCardIcon(); | |
| 94 } | |
| 95 | |
| 96 void SaveCardBubbleControllerImpl::DidNavigateMainFrame( | |
| 97 const content::LoadCommittedDetails& details, | |
| 98 const content::FrameNavigateParams& params) { | |
| 99 // Nothing to do if there's no bubble available. | |
| 100 if (save_card_callback_.is_null()) | |
| 101 return; | |
| 102 | |
| 103 // Don't react to in-page (fragment) navigations. | |
| 104 if (details.is_in_page) | |
| 105 return; | |
| 106 | |
| 107 // Don't do anything if a navigation occurs before a user could reasonably | |
| 108 // interact with the bubble. | |
| 109 if (timer_->Elapsed() < | |
| 110 base::TimeDelta::FromSeconds(kSurviveNavigationSeconds)) | |
| 111 return; | |
| 112 | |
| 113 // Otherwise, get rid of the bubble and icon. | |
| 114 if (save_card_bubble_view_) { | |
| 115 save_card_bubble_view_->Close(); | |
| 116 save_card_bubble_view_ = nullptr; | |
|
Evan Stade
2015/10/23 21:56:30
won't the above Close() call already null this out
bondd
2015/10/23 22:58:28
You are correct about "save_card_bubble_view_ = nu
| |
| 117 } | |
| 118 save_card_callback_.Reset(); | |
| 119 UpdateIcon(); | |
| 120 } | |
| 121 | |
| 122 } // namespace autofill | |
| OLD | NEW |