Home | History | Annotate | Download | only in autofill
      1 // Copyright (c) 2012 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 #ifndef CHROME_BROWSER_UI_AUTOFILL_AUTOFILL_DIALOG_CONTROLLER_IMPL_H_
      6 #define CHROME_BROWSER_UI_AUTOFILL_AUTOFILL_DIALOG_CONTROLLER_IMPL_H_
      7 
      8 #include <set>
      9 #include <vector>
     10 
     11 #include "base/callback.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "base/memory/weak_ptr.h"
     14 #include "base/strings/string16.h"
     15 #include "base/time/time.h"
     16 #include "chrome/browser/ui/autofill/account_chooser_model.h"
     17 #include "chrome/browser/ui/autofill/autofill_dialog_controller.h"
     18 #include "chrome/browser/ui/autofill/autofill_dialog_models.h"
     19 #include "chrome/browser/ui/autofill/autofill_dialog_types.h"
     20 #include "chrome/browser/ui/autofill/autofill_dialog_view_delegate.h"
     21 #include "chrome/browser/ui/autofill/autofill_popup_controller_impl.h"
     22 #include "chrome/browser/ui/autofill/country_combobox_model.h"
     23 #include "components/autofill/content/browser/wallet/wallet_client.h"
     24 #include "components/autofill/content/browser/wallet/wallet_client_delegate.h"
     25 #include "components/autofill/content/browser/wallet/wallet_items.h"
     26 #include "components/autofill/content/browser/wallet/wallet_signin_helper_delegate.h"
     27 #include "components/autofill/core/browser/autofill_manager_delegate.h"
     28 #include "components/autofill/core/browser/autofill_metrics.h"
     29 #include "components/autofill/core/browser/autofill_popup_delegate.h"
     30 #include "components/autofill/core/browser/field_types.h"
     31 #include "components/autofill/core/browser/form_structure.h"
     32 #include "components/autofill/core/browser/personal_data_manager.h"
     33 #include "components/autofill/core/browser/personal_data_manager_observer.h"
     34 #include "content/public/browser/notification_observer.h"
     35 #include "content/public/browser/notification_registrar.h"
     36 #include "content/public/browser/web_contents_observer.h"
     37 #include "content/public/common/ssl_status.h"
     38 #include "third_party/skia/include/core/SkColor.h"
     39 #include "ui/base/models/simple_menu_model.h"
     40 #include "ui/base/ui_base_types.h"
     41 #include "ui/gfx/animation/animation_delegate.h"
     42 #include "ui/gfx/animation/linear_animation.h"
     43 #include "url/gurl.h"
     44 
     45 class Profile;
     46 
     47 namespace content {
     48 class WebContents;
     49 }
     50 
     51 namespace autofill {
     52 
     53 class AutofillDataModel;
     54 class AutofillDialogView;
     55 class AutofillPopupControllerImpl;
     56 class DataModelWrapper;
     57 class TestableAutofillDialogView;
     58 
     59 namespace risk {
     60 class Fingerprint;
     61 }
     62 
     63 namespace wallet {
     64 class WalletSigninHelper;
     65 }
     66 
     67 // This class drives the dialog that appears when a site uses the imperative
     68 // autocomplete API to fill out a form.
     69 class AutofillDialogControllerImpl : public AutofillDialogViewDelegate,
     70                                      public AutofillDialogController,
     71                                      public AutofillPopupDelegate,
     72                                      public content::NotificationObserver,
     73                                      public content::WebContentsObserver,
     74                                      public SuggestionsMenuModelDelegate,
     75                                      public wallet::WalletClientDelegate,
     76                                      public wallet::WalletSigninHelperDelegate,
     77                                      public PersonalDataManagerObserver,
     78                                      public AccountChooserModelDelegate,
     79                                      public gfx::AnimationDelegate {
     80  public:
     81   virtual ~AutofillDialogControllerImpl();
     82 
     83   static base::WeakPtr<AutofillDialogControllerImpl> Create(
     84       content::WebContents* contents,
     85       const FormData& form_structure,
     86       const GURL& source_url,
     87       const base::Callback<void(const FormStructure*)>& callback);
     88 
     89   // AutofillDialogController implementation.
     90   virtual void Show() OVERRIDE;
     91   virtual void Hide() OVERRIDE;
     92   virtual void TabActivated() OVERRIDE;
     93 
     94   // Returns |view_| as a testable version of itself (if |view_| exists and
     95   // actually implements |AutofillDialogView::GetTestableView()|).
     96   TestableAutofillDialogView* GetTestableView();
     97 
     98   // AutofillDialogViewDelegate implementation.
     99   virtual base::string16 DialogTitle() const OVERRIDE;
    100   virtual base::string16 AccountChooserText() const OVERRIDE;
    101   virtual base::string16 SignInLinkText() const OVERRIDE;
    102   virtual base::string16 SpinnerText() const OVERRIDE;
    103   virtual base::string16 EditSuggestionText() const OVERRIDE;
    104   virtual base::string16 CancelButtonText() const OVERRIDE;
    105   virtual base::string16 ConfirmButtonText() const OVERRIDE;
    106   virtual base::string16 SaveLocallyText() const OVERRIDE;
    107   virtual base::string16 SaveLocallyTooltip() const OVERRIDE;
    108   virtual base::string16 LegalDocumentsText() OVERRIDE;
    109   virtual bool ShouldShowSpinner() const OVERRIDE;
    110   virtual bool ShouldShowAccountChooser() const OVERRIDE;
    111   virtual bool ShouldShowSignInWebView() const OVERRIDE;
    112   virtual GURL SignInUrl() const OVERRIDE;
    113   virtual bool ShouldOfferToSaveInChrome() const OVERRIDE;
    114   virtual bool ShouldSaveInChrome() const OVERRIDE;
    115   virtual ui::MenuModel* MenuModelForAccountChooser() OVERRIDE;
    116   virtual gfx::Image AccountChooserImage() OVERRIDE;
    117   virtual gfx::Image ButtonStripImage() const OVERRIDE;
    118   virtual int GetDialogButtons() const OVERRIDE;
    119   virtual bool IsDialogButtonEnabled(ui::DialogButton button) const OVERRIDE;
    120   virtual DialogOverlayState GetDialogOverlay() OVERRIDE;
    121   virtual const std::vector<gfx::Range>& LegalDocumentLinks() OVERRIDE;
    122   virtual bool SectionIsActive(DialogSection section) const OVERRIDE;
    123   virtual const DetailInputs& RequestedFieldsForSection(DialogSection section)
    124       const OVERRIDE;
    125   virtual ui::ComboboxModel* ComboboxModelForAutofillType(
    126       ServerFieldType type) OVERRIDE;
    127   virtual ui::MenuModel* MenuModelForSection(DialogSection section) OVERRIDE;
    128   virtual base::string16 LabelForSection(DialogSection section) const OVERRIDE;
    129   virtual SuggestionState SuggestionStateForSection(
    130       DialogSection section) OVERRIDE;
    131   virtual FieldIconMap IconsForFields(const FieldValueMap& user_inputs)
    132       const OVERRIDE;
    133   virtual bool FieldControlsIcons(ServerFieldType type) const OVERRIDE;
    134   virtual base::string16 TooltipForField(ServerFieldType type) const OVERRIDE;
    135   virtual bool InputIsEditable(const DetailInput& input, DialogSection section)
    136       OVERRIDE;
    137   virtual base::string16 InputValidityMessage(DialogSection section,
    138                                         ServerFieldType type,
    139                                         const base::string16& value) OVERRIDE;
    140   virtual ValidityMessages InputsAreValid(
    141       DialogSection section, const FieldValueMap& inputs) OVERRIDE;
    142   virtual void UserEditedOrActivatedInput(DialogSection section,
    143                                           ServerFieldType type,
    144                                           gfx::NativeView parent_view,
    145                                           const gfx::Rect& content_bounds,
    146                                           const base::string16& field_contents,
    147                                           bool was_edit) OVERRIDE;
    148   virtual bool HandleKeyPressEventInInput(
    149       const content::NativeWebKeyboardEvent& event) OVERRIDE;
    150   virtual void FocusMoved() OVERRIDE;
    151   virtual bool ShouldShowErrorBubble() const OVERRIDE;
    152   virtual void ViewClosed() OVERRIDE;
    153   virtual std::vector<DialogNotification> CurrentNotifications() OVERRIDE;
    154   virtual void LinkClicked(const GURL& url) OVERRIDE;
    155   virtual void SignInLinkClicked() OVERRIDE;
    156   virtual void NotificationCheckboxStateChanged(DialogNotification::Type type,
    157                                                 bool checked) OVERRIDE;
    158   virtual void LegalDocumentLinkClicked(const gfx::Range& range) OVERRIDE;
    159   virtual bool OnCancel() OVERRIDE;
    160   virtual bool OnAccept() OVERRIDE;
    161   virtual Profile* profile() OVERRIDE;
    162   virtual content::WebContents* GetWebContents() OVERRIDE;
    163 
    164   // AutofillPopupDelegate implementation.
    165   virtual void OnPopupShown() OVERRIDE;
    166   virtual void OnPopupHidden() OVERRIDE;
    167   virtual bool ShouldRepostEvent(const ui::MouseEvent& event) OVERRIDE;
    168   virtual void DidSelectSuggestion(int identifier) OVERRIDE;
    169   virtual void DidAcceptSuggestion(const base::string16& value,
    170                                    int identifier) OVERRIDE;
    171   virtual void RemoveSuggestion(const base::string16& value,
    172                                 int identifier) OVERRIDE;
    173   virtual void ClearPreviewedForm() OVERRIDE;
    174 
    175   // content::NotificationObserver implementation.
    176   virtual void Observe(int type,
    177                        const content::NotificationSource& source,
    178                        const content::NotificationDetails& details) OVERRIDE;
    179 
    180   // SuggestionsMenuModelDelegate implementation.
    181   virtual void SuggestionsMenuWillShow() OVERRIDE;
    182   virtual void SuggestionItemSelected(SuggestionsMenuModel* model,
    183                                       size_t index) OVERRIDE;
    184 
    185   // wallet::WalletClientDelegate implementation.
    186   virtual const AutofillMetrics& GetMetricLogger() const OVERRIDE;
    187   virtual std::string GetRiskData() const OVERRIDE;
    188   virtual std::string GetWalletCookieValue() const OVERRIDE;
    189   virtual bool IsShippingAddressRequired() const OVERRIDE;
    190   virtual void OnDidAcceptLegalDocuments() OVERRIDE;
    191   virtual void OnDidAuthenticateInstrument(bool success) OVERRIDE;
    192   virtual void OnDidGetFullWallet(
    193       scoped_ptr<wallet::FullWallet> full_wallet) OVERRIDE;
    194   virtual void OnDidGetWalletItems(
    195       scoped_ptr<wallet::WalletItems> wallet_items) OVERRIDE;
    196   virtual void OnDidSaveToWallet(
    197       const std::string& instrument_id,
    198       const std::string& address_id,
    199       const std::vector<wallet::RequiredAction>& required_actions,
    200       const std::vector<wallet::FormFieldError>& form_field_errors) OVERRIDE;
    201   virtual void OnWalletError(
    202       wallet::WalletClient::ErrorType error_type) OVERRIDE;
    203 
    204   // PersonalDataManagerObserver implementation.
    205   virtual void OnPersonalDataChanged() OVERRIDE;
    206 
    207   // AccountChooserModelDelegate implementation.
    208   virtual void AccountChooserWillShow() OVERRIDE;
    209   virtual void AccountChoiceChanged() OVERRIDE;
    210   virtual void AddAccount() OVERRIDE;
    211   virtual void UpdateAccountChooserView() OVERRIDE;
    212 
    213   // wallet::WalletSigninHelperDelegate implementation.
    214   virtual void OnPassiveSigninSuccess() OVERRIDE;
    215   virtual void OnPassiveSigninFailure(
    216       const GoogleServiceAuthError& error) OVERRIDE;
    217   virtual void OnDidFetchWalletCookieValue(
    218       const std::string& cookie_value) OVERRIDE;
    219 
    220   // gfx::AnimationDelegate implementation.
    221   virtual void AnimationEnded(const gfx::Animation* animation) OVERRIDE;
    222   virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE;
    223 
    224  protected:
    225   enum DialogSignedInState {
    226     NOT_CHECKED,
    227     REQUIRES_RESPONSE,
    228     REQUIRES_SIGN_IN,
    229     REQUIRES_PASSIVE_SIGN_IN,
    230     SIGNED_IN,
    231     SIGN_IN_DISABLED,
    232   };
    233 
    234   // Exposed for testing.
    235   AutofillDialogControllerImpl(
    236       content::WebContents* contents,
    237       const FormData& form_structure,
    238       const GURL& source_url,
    239       const base::Callback<void(const FormStructure*)>& callback);
    240 
    241   // Exposed for testing.
    242   AutofillDialogView* view() { return view_.get(); }
    243   virtual AutofillDialogView* CreateView();
    244   const DetailInput* input_showing_popup() const {
    245     return input_showing_popup_;
    246   }
    247 
    248   // Returns the PersonalDataManager for |profile_|.
    249   virtual PersonalDataManager* GetManager() const;
    250 
    251   // Returns the WalletClient* this class uses to talk to Online Wallet. Exposed
    252   // for testing.
    253   const wallet::WalletClient* GetWalletClient() const;
    254   virtual wallet::WalletClient* GetWalletClient();
    255 
    256   // Call to disable communication to Online Wallet for this dialog.
    257   // Exposed for testing.
    258   void DisableWallet(wallet::WalletClient::ErrorType error_type);
    259 
    260   // Returns whether Wallet is the current data source. Exposed for testing.
    261   virtual bool IsPayingWithWallet() const;
    262 
    263   // Asks risk module to asynchronously load fingerprint data. Data will be
    264   // returned via |OnDidLoadRiskFingerprintData()|. Exposed for testing.
    265   virtual void LoadRiskFingerprintData();
    266   virtual void OnDidLoadRiskFingerprintData(
    267       scoped_ptr<risk::Fingerprint> fingerprint);
    268 
    269   // Opens the given URL in a new foreground tab.
    270   virtual void OpenTabWithUrl(const GURL& url);
    271 
    272   // Whether |section| was sent into edit mode based on existing data. This
    273   // happens when a user clicks "Edit" or a suggestion is invalid.
    274   virtual bool IsEditingExistingData(DialogSection section) const;
    275 
    276   // Whether the user has chosen to enter all new data in |section|. This
    277   // happens via choosing "Add a new X..." from a section's suggestion menu.
    278   bool IsManuallyEditingSection(DialogSection section) const;
    279 
    280   // Should be called on the Wallet sign-in error.
    281   virtual void OnWalletSigninError();
    282 
    283   // Whether submission is currently waiting for |action| to be handled.
    284   bool IsSubmitPausedOn(wallet::RequiredAction action) const;
    285 
    286   // Shows a new credit card saved bubble and passes ownership of |new_card| and
    287   // |billing_profile| to the bubble. Exposed for testing.
    288   virtual void ShowNewCreditCardBubble(
    289       scoped_ptr<CreditCard> new_card,
    290       scoped_ptr<AutofillProfile> billing_profile);
    291 
    292   // Called when there's nothing left to accept, update, save, or authenticate
    293   // in order to fill |form_structure_| and pass data back to the invoking page.
    294   void DoFinishSubmit();
    295 
    296   // Delays enabling submit button for a short period of time. Exposed for
    297   // testing.
    298   virtual void SubmitButtonDelayBegin();
    299 
    300   // Ends the delay for enabling the submit button. Called only from tests.
    301   // Without this method, the tests would have to wait for the delay timer to
    302   // finish, which would be flaky.
    303   void SubmitButtonDelayEndForTesting();
    304 
    305   // Resets |last_wallet_items_fetch_timestamp_| for testing.
    306   void ClearLastWalletItemsFetchTimestampForTesting();
    307 
    308   // Allows tests to inspect the state of the account chooser.
    309   AccountChooserModel* AccountChooserModelForTesting();
    310 
    311   // Returns whether |url| matches the sign in continue URL. If so, also fills
    312   // in |user_index| with the index of the user account that just signed in.
    313   virtual bool IsSignInContinueUrl(const GURL& url, size_t* user_index) const;
    314 
    315   // Whether the user is known to be signed in.
    316   DialogSignedInState SignedInState() const;
    317 
    318  private:
    319   // Initializes or updates |suggested_cc_| et al.
    320   void SuggestionsUpdated();
    321 
    322   // Starts fetching the wallet items from Online Wallet.
    323   void GetWalletItems();
    324 
    325   // Stop showing sign in flow.
    326   void HideSignIn();
    327 
    328   // Handles the SignedInState() on Wallet or sign-in state update.
    329   // Triggers the user name fetch and passive sign-in.
    330   void SignedInStateUpdated();
    331 
    332   // Refreshes the model on Wallet or sign-in state update.
    333   void OnWalletOrSigninUpdate();
    334 
    335   // Called when a Save or Update call to Wallet has validation errors.
    336   void OnWalletFormFieldError(
    337       const std::vector<wallet::FormFieldError>& form_field_errors);
    338 
    339   // Calculates |legal_documents_text_| and |legal_document_link_ranges_|.
    340   void ConstructLegalDocumentsText();
    341 
    342   // Clears previously entered manual input and removes |section| from
    343   // |section_editing_state_|. Does not update the view.
    344   void ResetSectionInput(DialogSection section);
    345 
    346   // Force |section| into edit mode if the current suggestion is invalid.
    347   void ShowEditUiIfBadSuggestion(DialogSection section);
    348 
    349   // Whether the |value| of |input| should be preserved on account change.
    350   bool InputWasEdited(ServerFieldType type,
    351                       const base::string16& value);
    352 
    353   // Takes a snapshot of the newly inputted user data in |view_| (if it exists).
    354   FieldValueMap TakeUserInputSnapshot();
    355 
    356   // Fills the detail inputs from a previously taken user input snapshot. Does
    357   // not update the view.
    358   void RestoreUserInputFromSnapshot(const FieldValueMap& snapshot);
    359 
    360   // Tells the view to update |section|.
    361   void UpdateSection(DialogSection section);
    362 
    363   // Tells |view_| to update the validity status of its detail inputs (if
    364   // |view_| is non-null). Currently this is used solely for highlighting
    365   // invalid suggestions, so if no sections are based on existing data,
    366   // |view_->UpdateForErrors()| is not called.
    367   void UpdateForErrors();
    368 
    369   // Renders and returns one frame of the generated card animation.
    370   gfx::Image GetGeneratedCardImage(const base::string16& card_number,
    371                                    const base::string16& name,
    372                                    const SkColor& gradient_top,
    373                                    const SkColor& gradient_bottom);
    374 
    375   // Kicks off |card_scrambling_refresher_|.
    376   void StartCardScramblingRefresher();
    377 
    378   // Changes |scrambled_card_number_| and pushes an update to the view.
    379   void RefreshCardScramblingOverlay();
    380 
    381   // Tells the view to update the overlay.
    382   void PushOverlayUpdate();
    383 
    384   // Creates a DataModelWrapper item for the item that's checked in the
    385   // suggestion model for |section|. This may represent Autofill
    386   // data or Wallet data, depending on whether Wallet is currently enabled.
    387   scoped_ptr<DataModelWrapper> CreateWrapper(DialogSection section);
    388 
    389   // Helper to return the current Wallet instrument or address. If the dialog
    390   // isn't using Wallet or the user is adding a new instrument or address, NULL
    391   // will be returned.
    392   const wallet::WalletItems::MaskedInstrument* ActiveInstrument() const;
    393   const wallet::Address* ActiveShippingAddress() const;
    394 
    395   // Fills in |section|-related fields in |output_| according to the state of
    396   // |view_|.
    397   void FillOutputForSection(DialogSection section);
    398   // As above, but uses |compare| to determine whether a DetailInput matches
    399   // a field. Saves any new Autofill data to the PersonalDataManager.
    400   void FillOutputForSectionWithComparator(DialogSection section,
    401                                           const InputFieldComparator& compare);
    402 
    403   // Returns whether |form_structure|_| has any fields that match the fieldset
    404   // represented by |section|.
    405   bool FormStructureCaresAboutSection(DialogSection section) const;
    406 
    407   // Finds all fields of the given |type| in |form_structure_|, if any, and sets
    408   // each field's value to |output|.
    409   void SetOutputForFieldsOfType(ServerFieldType type,
    410                                 const base::string16& output);
    411 
    412   // Gets the value for |type| in |section|, whether it comes from manual user
    413   // input or the active suggestion.
    414   base::string16 GetValueFromSection(DialogSection section,
    415                                ServerFieldType type);
    416 
    417   // Gets the SuggestionsMenuModel for |section|.
    418   SuggestionsMenuModel* SuggestionsMenuModelForSection(DialogSection section);
    419   const SuggestionsMenuModel* SuggestionsMenuModelForSection(
    420       DialogSection section) const;
    421   // And the reverse.
    422   DialogSection SectionForSuggestionsMenuModel(
    423       const SuggestionsMenuModel& model);
    424 
    425   // Suggested text and icons for sections. Suggestion text is used to show an
    426   // abridged overview of the currently used suggestion. Extra text is used when
    427   // part of a section is suggested but part must be manually input (e.g. during
    428   // a CVC challenge or when using Autofill's CC section [never stores CVC]).
    429   bool SuggestionTextForSection(DialogSection section,
    430                                 base::string16* vertically_compact,
    431                                 base::string16* horizontally_compact);
    432   base::string16 RequiredActionTextForSection(DialogSection section) const;
    433   gfx::Image SuggestionIconForSection(DialogSection section);
    434   base::string16 ExtraSuggestionTextForSection(DialogSection section) const;
    435   gfx::Image ExtraSuggestionIconForSection(DialogSection section);
    436 
    437   // Loads profiles that can suggest data for |type|. |field_contents| is the
    438   // part the user has already typed. |inputs| is the rest of section.
    439   // Identifying info is loaded into the last three outparams as well as
    440   // |popup_guids_|.
    441   void GetProfileSuggestions(
    442       ServerFieldType type,
    443       const base::string16& field_contents,
    444       const DetailInputs& inputs,
    445       std::vector<base::string16>* popup_values,
    446       std::vector<base::string16>* popup_labels,
    447       std::vector<base::string16>* popup_icons);
    448 
    449   // Like RequestedFieldsForSection, but returns a pointer.
    450   DetailInputs* MutableRequestedFieldsForSection(DialogSection section);
    451 
    452   // Hides |popup_controller_|'s popup view, if it exists.
    453   void HidePopup();
    454 
    455   // Set whether the currently editing |section| was originally based on
    456   // existing Wallet or Autofill data.
    457   void SetEditingExistingData(DialogSection section, bool editing);
    458 
    459   // Whether the user has chosen to enter all new data in at least one section.
    460   bool IsManuallyEditingAnySection() const;
    461 
    462   // Returns validity message for a given credit card number.
    463   base::string16 CreditCardNumberValidityMessage(
    464       const base::string16& number) const;
    465 
    466   // Whether all of the input fields currently showing in the dialog have valid
    467   // contents. This validates only by checking "sure" messages, i.e. messages
    468   // that would have been displayed to the user during editing, as opposed to
    469   // submission.
    470   bool AllSectionsAreValid();
    471 
    472   // Whether all of the input fields currently showing in the given |section| of
    473   // the dialog have valid contents. This validates only by checking "sure"
    474   // messages - see AllSectionsAreValid.
    475   bool SectionIsValid(DialogSection section);
    476 
    477   // Whether the currently active credit card expiration date is valid.
    478   bool IsCreditCardExpirationValid(const base::string16& year,
    479                                    const base::string16& month) const;
    480 
    481   // Returns true if |key| refers to a suggestion, as opposed to some control
    482   // menu item.
    483   bool IsASuggestionItemKey(const std::string& key) const;
    484 
    485   // Whether the billing section should be used to fill in the shipping details.
    486   bool ShouldUseBillingForShipping();
    487 
    488   // Whether the user wishes to save information locally to Autofill.
    489   bool ShouldSaveDetailsLocally();
    490 
    491   // Change whether the controller is currently submitting details to Autofill
    492   // or Online Wallet (|is_submitting_|) and update the view.
    493   void SetIsSubmitting(bool submitting);
    494 
    495   // Whether the user has accepted all the current legal documents' terms.
    496   bool AreLegalDocumentsCurrent() const;
    497 
    498   // Accepts any pending legal documents now that the user has pressed Submit.
    499   void AcceptLegalTerms();
    500 
    501   // Start the submit proccess to interact with Online Wallet (might do various
    502   // things like accept documents, save details, update details, respond to
    503   // required actions, etc.).
    504   void SubmitWithWallet();
    505 
    506   // Creates an instrument based on |views_|' contents.
    507   scoped_ptr<wallet::Instrument> CreateTransientInstrument();
    508 
    509   // Creates an address based on the contents of |view_|.
    510   scoped_ptr<wallet::Address> CreateTransientAddress();
    511 
    512   // Gets a full wallet from Online Wallet so the user can purchase something.
    513   // This information is decoded to reveal a fronting (proxy) card.
    514   void GetFullWallet();
    515 
    516   // Updates the state of the controller and |view_| based on any required
    517   // actions returned by Save or Update calls to Wallet.
    518   void HandleSaveOrUpdateRequiredActions(
    519       const std::vector<wallet::RequiredAction>& required_actions);
    520 
    521   // Shows a card generation overlay if necessary, then calls DoFinishSubmit.
    522   void FinishSubmit();
    523 
    524   // Writes to prefs the choice of AutofillDataModel for |section|.
    525   void PersistAutofillChoice(DialogSection section,
    526                              const std::string& guid);
    527 
    528   // Sets the outparams to the default AutofillDataModel for |section| (which is
    529   // the first one in the menu that is a suggestion item).
    530   void GetDefaultAutofillChoice(DialogSection section,
    531                                 std::string* guid);
    532 
    533   // Reads from prefs the choice of AutofillDataModel for |section|. Returns
    534   // whether there was a setting to read.
    535   bool GetAutofillChoice(DialogSection section,
    536                          std::string* guid);
    537 
    538   // Logs metrics when the dialog is submitted.
    539   void LogOnFinishSubmitMetrics();
    540 
    541   // Logs metrics when the dialog is canceled.
    542   void LogOnCancelMetrics();
    543 
    544   // Logs metrics when the edit ui is shown for the given |section|.
    545   void LogEditUiShownMetric(DialogSection section);
    546 
    547   // Logs metrics when a suggestion item from the given |model| is selected.
    548   void LogSuggestionItemSelectedMetric(const SuggestionsMenuModel& model);
    549 
    550   // Logs the time elapsed from when the dialog was shown to when the user could
    551   // interact with it.
    552   void LogDialogLatencyToShow();
    553 
    554   // Returns the metric corresponding to the user's initial state when
    555   // interacting with this dialog.
    556   AutofillMetrics::DialogInitialUserStateMetric GetInitialUserState() const;
    557 
    558   // Shows an educational bubble if a new credit card was saved or the first few
    559   // times an Online Wallet fronting card was generated.
    560   void MaybeShowCreditCardBubble();
    561 
    562   // Called when the delay for enabling the submit button ends.
    563   void OnSubmitButtonDelayEnd();
    564 
    565   // Gets the user's current Wallet cookie (gdToken) from the cookie jar.
    566   void FetchWalletCookie();
    567 
    568   // The |profile| for |contents_|.
    569   Profile* const profile_;
    570 
    571   // For logging UMA metrics.
    572   const AutofillMetrics metric_logger_;
    573   base::Time dialog_shown_timestamp_;
    574   AutofillMetrics::DialogInitialUserStateMetric initial_user_state_;
    575 
    576   FormStructure form_structure_;
    577 
    578   // Whether the URL visible to the user when this dialog was requested to be
    579   // invoked is the same as |source_url_|.
    580   bool invoked_from_same_origin_;
    581 
    582   // The URL of the invoking site.
    583   GURL source_url_;
    584 
    585   // The callback via which we return the collected data.
    586   base::Callback<void(const FormStructure*)> callback_;
    587 
    588   // The AccountChooserModel acts as the MenuModel for the account chooser,
    589   // and also tracks which data source the dialog is using.
    590   scoped_ptr<AccountChooserModel> account_chooser_model_;
    591 
    592   // The sign-in helper to fetch the user's Wallet cookie and to perform passive
    593   // sign-in. The helper is set only during fetch/sign-in, and NULL otherwise.
    594   scoped_ptr<wallet::WalletSigninHelper> signin_helper_;
    595 
    596   // A client to talk to the Online Wallet API.
    597   wallet::WalletClient wallet_client_;
    598 
    599   // True if |this| has ever called GetWalletItems().
    600   bool wallet_items_requested_;
    601 
    602   // True when the user has clicked the "Use Wallet" link and we're waiting to
    603   // figure out whether we need to ask them to actively sign in.
    604   bool handling_use_wallet_link_click_;
    605 
    606   // True when the current WalletItems has a passive auth action which was
    607   // attempted and failed.
    608   bool passive_failed_;
    609 
    610   // Recently received items retrieved via |wallet_client_|.
    611   scoped_ptr<wallet::WalletItems> wallet_items_;
    612   scoped_ptr<wallet::FullWallet> full_wallet_;
    613 
    614   // The default active instrument and shipping address object IDs as of the
    615   // last time Wallet items were fetched. These variables are only set
    616   // (i.e. non-empty) when the Wallet items are being re-fetched.
    617   std::string previous_default_instrument_id_;
    618   std::string previous_default_shipping_address_id_;
    619   // The last active instrument and shipping address object IDs. These
    620   // variables are only set (i.e. non-empty) when the Wallet items are being
    621   // re-fetched.
    622   std::string previously_selected_instrument_id_;
    623   std::string previously_selected_shipping_address_id_;
    624 
    625   // When the Wallet items were last fetched.
    626   base::TimeTicks last_wallet_items_fetch_timestamp_;
    627 
    628   // Local machine signals to pass along on each request to trigger (or
    629   // discourage) risk challenges; sent if the user is up to date on legal docs.
    630   std::string risk_data_;
    631 
    632   // The text to display when the user is accepting new terms of service, etc.
    633   base::string16 legal_documents_text_;
    634   // The ranges within |legal_documents_text_| to linkify.
    635   std::vector<gfx::Range> legal_document_link_ranges_;
    636 
    637   // The instrument and address IDs from the Online Wallet server to be used
    638   // when getting a full wallet.
    639   std::string active_instrument_id_;
    640   std::string active_address_id_;
    641 
    642   // The fields for billing and shipping which the page has actually requested.
    643   DetailInputs requested_cc_fields_;
    644   DetailInputs requested_billing_fields_;
    645   DetailInputs requested_cc_billing_fields_;
    646   DetailInputs requested_shipping_fields_;
    647 
    648   // Models for the credit card expiration inputs.
    649   MonthComboboxModel cc_exp_month_combobox_model_;
    650   YearComboboxModel cc_exp_year_combobox_model_;
    651 
    652   // Model for the country input.
    653   CountryComboboxModel country_combobox_model_;
    654 
    655   // Models for the suggestion views.
    656   SuggestionsMenuModel suggested_cc_;
    657   SuggestionsMenuModel suggested_billing_;
    658   SuggestionsMenuModel suggested_cc_billing_;
    659   SuggestionsMenuModel suggested_shipping_;
    660 
    661   // |DialogSection|s that are in edit mode that are based on existing data.
    662   std::set<DialogSection> section_editing_state_;
    663 
    664   // Whether |form_structure_| has asked for any details that would indicate
    665   // we should show a shipping section.
    666   bool cares_about_shipping_;
    667 
    668   // The GUIDs for the currently showing unverified profiles popup.
    669   std::vector<PersonalDataManager::GUIDPair> popup_guids_;
    670 
    671   // The controller for the currently showing popup (which helps users when
    672   // they're manually filling the dialog).
    673   base::WeakPtr<AutofillPopupControllerImpl> popup_controller_;
    674 
    675   // The input for which |popup_controller_| is currently showing a popup
    676   // (if any).
    677   const DetailInput* input_showing_popup_;
    678 
    679   scoped_ptr<AutofillDialogView> view_;
    680 
    681   // A NotificationRegistrar for tracking the completion of sign-in.
    682   content::NotificationRegistrar signin_registrar_;
    683 
    684   base::WeakPtrFactory<AutofillDialogControllerImpl> weak_ptr_factory_;
    685 
    686   // Set to true when the user presses the sign in link, until we're ready to
    687   // show the normal dialog again. This is used to hide the buttons while
    688   // the spinner is showing after an explicit sign in.
    689   bool waiting_for_explicit_sign_in_response_;
    690 
    691   // Whether a user accepted legal documents while this dialog is running.
    692   bool has_accepted_legal_documents_;
    693 
    694   // True after the user first accepts the dialog and presses "Submit". May
    695   // continue to be true while processing required actions.
    696   bool is_submitting_;
    697 
    698   // True if the last call to |GetFullWallet()| returned a
    699   // CHOOSE_ANOTHER_INSTRUMENT_OR_ADDRESS required action, indicating that the
    700   // selected instrument or address had become invalid since it was originally
    701   // returned in |GetWalletItems()|.
    702   bool choose_another_instrument_or_address_;
    703 
    704   // Whether or not the server side validation errors returned by Wallet were
    705   // recoverable.
    706   bool wallet_server_validation_recoverable_;
    707 
    708   // Whether |callback_| was Run() with a filled |form_structure_|.
    709   bool data_was_passed_back_;
    710 
    711   typedef std::map<ServerFieldType,
    712       std::pair<base::string16, base::string16> > TypeErrorInputMap;
    713   typedef std::map<DialogSection, TypeErrorInputMap> WalletValidationErrors;
    714   // Wallet validation errors. section->type->(error_msg, input_value).
    715   WalletValidationErrors wallet_errors_;
    716 
    717   // The notification that describes the current wallet error, if any.
    718   scoped_ptr<DialogNotification> wallet_error_notification_;
    719 
    720   // Whether the latency to display to the UI was logged to UMA yet.
    721   bool was_ui_latency_logged_;
    722 
    723   // The Google Wallet cookie value, set as an authorization header on requests
    724   // to Wallet.
    725   std::string wallet_cookie_value_;
    726 
    727   // A map from dialog sections to the GUID of a newly saved Autofill data
    728   // models for that section. No entries present that don't have newly saved
    729   // data models.
    730   std::map<DialogSection, std::string> newly_saved_data_model_guids_;
    731 
    732   // Populated if the user chose to save a newly inputted credit card. Used to
    733   // show a bubble as the dialog closes to confirm a user's new card info was
    734   // saved. Never populated while incognito (as nothing's actually saved).
    735   scoped_ptr<CreditCard> newly_saved_card_;
    736 
    737   // The last four digits of the backing card used for the current run of the
    738   // dialog. Only applies to Wallet and is populated on submit.
    739   base::string16 backing_card_last_four_;
    740 
    741   // The timer that delays enabling submit button for a short period of time on
    742   // startup.
    743   base::OneShotTimer<AutofillDialogControllerImpl> submit_button_delay_timer_;
    744 
    745   // The card scrambling animation displays a random number in place of an
    746   // actual credit card number. This is that random number.
    747   base::string16 scrambled_card_number_;
    748 
    749   // Two timers to deal with the card scrambling animation. The first provides
    750   // a one second delay before the numbers start scrambling. The second controls
    751   // the rate of refresh for the number scrambling.
    752   base::OneShotTimer<AutofillDialogControllerImpl> card_scrambling_delay_;
    753   base::RepeatingTimer<AutofillDialogControllerImpl> card_scrambling_refresher_;
    754 
    755   // An animation which controls the background fade when the card is done
    756   // scrambling.
    757   gfx::LinearAnimation card_generated_animation_;
    758 
    759   // A username string we display in the card scrambling/generated overlay.
    760   base::string16 submitted_cardholder_name_;
    761 
    762   DISALLOW_COPY_AND_ASSIGN(AutofillDialogControllerImpl);
    763 };
    764 
    765 }  // namespace autofill
    766 
    767 #endif  // CHROME_BROWSER_UI_AUTOFILL_AUTOFILL_DIALOG_CONTROLLER_IMPL_H_
    768