Home | History | Annotate | Download | only in autofill
      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 #ifndef CHROME_BROWSER_UI_AUTOFILL_GENERATED_CREDIT_CARD_BUBBLE_CONTROLLER_H_
      6 #define CHROME_BROWSER_UI_AUTOFILL_GENERATED_CREDIT_CARD_BUBBLE_CONTROLLER_H_
      7 
      8 #include <vector>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/compiler_specific.h"
     12 #include "base/memory/weak_ptr.h"
     13 #include "base/strings/string16.h"
     14 #include "content/public/browser/web_contents_observer.h"
     15 #include "content/public/browser/web_contents_user_data.h"
     16 #include "ui/gfx/image/image.h"
     17 #include "ui/gfx/range/range.h"
     18 
     19 namespace content {
     20 class WebContents;
     21 }
     22 
     23 namespace user_prefs {
     24 class PrefRegistrySyncable;
     25 }
     26 
     27 namespace autofill {
     28 
     29 class GeneratedCreditCardBubbleView;
     30 
     31 // A simple struct of text highlighting range information. If |is_link| is true
     32 // this portion of the text should be clickable (and trigger |OnLinkClicked()|).
     33 // If |is_link| is false, the text denoted by |range| should be bolded.
     34 struct TextRange {
     35   // The range of text this TextRange applies to (start and end).
     36   gfx::Range range;
     37   // Whether this text range should be styled like a link (e.g. clickable).
     38   bool is_link;
     39   // An equality operator for testing.
     40   bool operator==(const TextRange& other) const;
     41 };
     42 
     43 ////////////////////////////////////////////////////////////////////////////////
     44 //
     45 // GeneratedCreditCardBubbleController
     46 //
     47 //  A class to control showing and hiding a bubble after a credit card is
     48 //  generated.
     49 //
     50 ////////////////////////////////////////////////////////////////////////////////
     51 class GeneratedCreditCardBubbleController
     52     : public content::WebContentsObserver,
     53       public content::WebContentsUserData<GeneratedCreditCardBubbleController> {
     54  public:
     55   virtual ~GeneratedCreditCardBubbleController();
     56 
     57   // Registers preferences this class cares about.
     58   static void RegisterUserPrefs(user_prefs::PrefRegistrySyncable* registry);
     59 
     60   // Show a bubble to educate the user about generated (fronting) cards and how
     61   // they are used to bill their original (backing) card.
     62   static void Show(content::WebContents* contents,
     63                    const base::string16& fronting_card_name,
     64                    const base::string16& backing_card_name);
     65 
     66   // content::WebContentsObserver:
     67   virtual void DidNavigateMainFrame(
     68       const content::LoadCommittedDetails& details,
     69       const content::FrameNavigateParams& params) OVERRIDE;
     70 
     71   // Returns whether |bubble_| is currently in the process of hiding.
     72   bool IsHiding() const;
     73 
     74   // Returns the image that should be shown as an icon in the omnibox.
     75   gfx::Image AnchorIcon() const;
     76 
     77   // The title of the bubble.
     78   const base::string16& TitleText() const;
     79 
     80   // Text in the contents of the bubble.
     81   const base::string16& ContentsText() const;
     82 
     83   // Ranges of text styles in the bubble's main content.
     84   const std::vector<TextRange>& ContentsTextRanges() const;
     85 
     86   // Called when the anchor for this bubble is clicked. Shows a new bubble.
     87   void OnAnchorClicked();
     88 
     89   // Called when the link at the bottom of the bubble is clicked. Opens and
     90   // navigates a new tab to an informational page and hides the bubble.
     91   void OnLinkClicked();
     92 
     93   // The web contents that successfully submitted the Autofill dialog (causing
     94   // this bubble to show).
     95   content::WebContents* web_contents() { return web_contents_; }
     96   const content::WebContents* web_contents() const { return web_contents_; }
     97 
     98  protected:
     99   // Creates a bubble connected to |web_contents|.
    100   explicit GeneratedCreditCardBubbleController(content::WebContents* contents);
    101 
    102   // Returns a base::WeakPtr that references |this|. Exposed for testing.
    103   base::WeakPtr<GeneratedCreditCardBubbleController> GetWeakPtr();
    104 
    105   // Creates and returns an Autofill credit card bubble. Exposed for testing.
    106   virtual base::WeakPtr<GeneratedCreditCardBubbleView> CreateBubble();
    107 
    108   // Returns a weak reference to |bubble_|. May be invalid/NULL.
    109   virtual base::WeakPtr<GeneratedCreditCardBubbleView> bubble();
    110 
    111   // Returns whether the bubble can currently show itself.
    112   virtual bool CanShow() const;
    113 
    114   // Whether the generated card bubble should be shown initially when showing
    115   // the anchor icon. This does not affect whether the generated card's icon
    116   // will show in the omnibox.
    117   bool ShouldDisplayBubbleInitially() const;
    118 
    119   // Exposed for testing.
    120   base::string16 fronting_card_name() const { return fronting_card_name_; }
    121   base::string16 backing_card_name() const { return backing_card_name_; }
    122 
    123   // Generates the correct bubble text and text highlighting ranges and shows a
    124   // bubble to educate the user about generated (fronting) cards and how they
    125   // are used to bill their original (backing) card. Exposed for testing.
    126   virtual void SetupAndShow(const base::string16& fronting_card_name,
    127                             const base::string16& backing_card_name);
    128 
    129  private:
    130   friend class
    131       content::WebContentsUserData<GeneratedCreditCardBubbleController>;
    132 
    133   // An internal helper to show the bubble.
    134   void Show(bool was_anchor_click);
    135 
    136   // Updates the omnibox icon that |bubble_| is anchored to.
    137   void UpdateAnchor();
    138 
    139   // Hides |bubble_| (if it exists and isn't already hiding).
    140   void Hide();
    141 
    142   // The web contents associated with this bubble.
    143   content::WebContents* const web_contents_;
    144 
    145   // The generated credit card number and associated backing card.
    146   base::string16 fronting_card_name_;
    147   base::string16 backing_card_name_;
    148 
    149   // The title text of the bubble.
    150   const base::string16 title_text_;
    151 
    152   // Strings and ranges generated based on |backing_card_name_| and
    153   // |fronting_card_name_|.
    154   base::string16 contents_text_;
    155   std::vector<TextRange> contents_text_ranges_;
    156 
    157   // A bubble view that's created by calling either |Show*()| method; owned by
    158   // the native widget/hierarchy, not this class (though this class must outlive
    159   // |bubble_|). NULL in many cases.
    160   base::WeakPtr<GeneratedCreditCardBubbleView> bubble_;
    161 
    162   // Whether the anchor should currently be showing.
    163   bool should_show_anchor_;
    164 
    165   // A weak pointer factory for |Create()|.
    166   base::WeakPtrFactory<GeneratedCreditCardBubbleController> weak_ptr_factory_;
    167 
    168   DISALLOW_COPY_AND_ASSIGN(GeneratedCreditCardBubbleController);
    169 };
    170 
    171 }  // namespace autofill
    172 
    173 #endif  // CHROME_BROWSER_UI_AUTOFILL_GENERATED_CREDIT_CARD_BUBBLE_CONTROLLER_H_
    174