Chromium Code Reviews| Index: chrome/browser/ui/autofill/save_card_bubble_controller.cc |
| diff --git a/chrome/browser/ui/autofill/save_card_bubble_controller.cc b/chrome/browser/ui/autofill/save_card_bubble_controller.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..888bfcb5c9f1b6f122ac18fcc77ccddf9e6abcc6 |
| --- /dev/null |
| +++ b/chrome/browser/ui/autofill/save_card_bubble_controller.cc |
| @@ -0,0 +1,116 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/ui/autofill/save_card_bubble_controller.h" |
| + |
| +#include "chrome/browser/ui/browser.h" |
| +#include "chrome/browser/ui/browser_finder.h" |
| +#include "chrome/browser/ui/browser_window.h" |
| +#include "chrome/browser/ui/location_bar/location_bar.h" |
| +#include "content/public/browser/navigation_details.h" |
| + |
| +namespace { |
| + |
| +// Minimal time span the bubble should survive implicit navigations. |
| +// TODO(bondd): Share with ManagePasswordsUIController. |
| +const int kBubbleMinTime = 5; |
| + |
| +} // namespace |
| + |
| +namespace autofill { |
| + |
| +SaveCardBubbleController::SaveCardBubbleController( |
| + content::WebContents* web_contents) |
| + : content::WebContentsObserver(web_contents), |
| + web_contents_(web_contents), |
|
bondd
2015/10/09 01:36:02
Is it okay to save |web_contents| off like this fo
|
| + state_(INACTIVE_STATE) { |
| + DCHECK(web_contents); |
| +} |
| + |
| +SaveCardBubbleController::~SaveCardBubbleController() {} |
| + |
| +void SaveCardBubbleController::ShowBubble( |
| + const base::Closure& save_card_callback) { |
| + save_card_callback_ = save_card_callback; |
| + state_ = BUBBLE_AVAILABLE_STATE; |
| + ReshowBubble(); |
|
Justin Donnelly
2015/10/13 16:25:53
This seems a little awkward. This API feels more l
bondd
2015/10/15 22:27:07
Done. I flip-flopped a couple of times between the
|
| +} |
| + |
| +void SaveCardBubbleController::ReshowBubble() { |
| + DCHECK_EQ(state_, BUBBLE_AVAILABLE_STATE); |
| + |
| + // Need to create location bar icon before bubble, otherwise bubble will be |
| + // unanchored. |
| + state_ = BUBBLE_VISIBLE_STATE; |
| + UpdateIcon(); |
| + |
| + Browser* browser = chrome::FindBrowserWithWebContents(web_contents_); |
| + browser->window()->ShowSaveCreditCardBubble(web_contents_, this); |
| + |
| + timer_.reset(new base::ElapsedTimer()); |
| +} |
| + |
| +void SaveCardBubbleController::OnSaveButton() { |
| + save_card_callback_.Run(); |
| + Deactivate(); |
| +} |
| + |
| +void SaveCardBubbleController::OnCancelButton() { |
| + Deactivate(); |
| +} |
| + |
| +void SaveCardBubbleController::OnBubbleClosed() { |
| + // Do nothing if bubble has already been closed because of a button press. |
| + if (state_ == INACTIVE_STATE) |
| + return; |
| + |
| + state_ = BUBBLE_AVAILABLE_STATE; |
| + UpdateIcon(); |
| +} |
| + |
| +bool SaveCardBubbleController::WantIconVisible() const { |
| + return state_ != INACTIVE_STATE; |
| +} |
| + |
| +bool SaveCardBubbleController::WantIconToggled() const { |
| + return state_ == BUBBLE_VISIBLE_STATE; |
| +} |
| + |
| +void SaveCardBubbleController::UpdateIcon() { |
| + Browser* browser = chrome::FindBrowserWithWebContents(web_contents_); |
| + LocationBar* location_bar = browser->window()->GetLocationBar(); |
| + DCHECK(location_bar); |
| + location_bar->UpdateSaveCreditCardIcon(); |
| +} |
| + |
| +void SaveCardBubbleController::Deactivate() { |
| + save_card_callback_.Reset(); |
| + state_ = INACTIVE_STATE; |
| + UpdateIcon(); |
| +} |
| + |
| +base::TimeDelta SaveCardBubbleController::Elapsed() const { |
| + return timer_ ? timer_->Elapsed() : base::TimeDelta::Max(); |
| +} |
| + |
| +void SaveCardBubbleController::DidNavigateMainFrame( |
|
bondd
2015/10/09 01:36:02
This function is based on the one in ManagePasswor
Justin Donnelly
2015/10/13 16:25:53
Is there a motivation for not resetting the timer
bondd
2015/10/15 22:27:07
Yes, I considered resetting it at the end like Man
|
| + const content::LoadCommittedDetails& details, |
| + const content::FrameNavigateParams& params) { |
| + if (state_ == INACTIVE_STATE) |
| + return; |
| + |
| + // Don't react to in-page (fragment) navigations. |
| + if (details.is_in_page) |
| + return; |
| + |
| + // Don't do anything if a navigation occurs before a user could reasonably |
| + // interact with the bubble. |
| + if (Elapsed() < base::TimeDelta::FromSeconds(kBubbleMinTime)) |
| + return; |
| + |
| + // Otherwise, make the bubble and icon inactive. |
| + Deactivate(); |
| +} |
| + |
| +} // namespace autofill |