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.
5 #ifndef CHROME_BROWSER_UI_AUTOFILL_GENERATED_CREDIT_CARD_BUBBLE_CONTROLLER_H_
6 #define CHROME_BROWSER_UI_AUTOFILL_GENERATED_CREDIT_CARD_BUBBLE_CONTROLLER_H_
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"
23 namespace user_prefs
{
24 class PrefRegistrySyncable
;
29 class GeneratedCreditCardBubbleView
;
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.
35 // The range of text this TextRange applies to (start and end).
37 // Whether this text range should be styled like a link (e.g. clickable).
39 // An equality operator for testing.
40 bool operator==(const TextRange
& other
) const;
43 ////////////////////////////////////////////////////////////////////////////////
45 // GeneratedCreditCardBubbleController
47 // A class to control showing and hiding a bubble after a credit card is
50 ////////////////////////////////////////////////////////////////////////////////
51 class GeneratedCreditCardBubbleController
52 : public content::WebContentsObserver
,
53 public content::WebContentsUserData
<GeneratedCreditCardBubbleController
> {
55 ~GeneratedCreditCardBubbleController() override
;
57 // Registers preferences this class cares about.
58 static void RegisterUserPrefs(user_prefs::PrefRegistrySyncable
* registry
);
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
);
66 // content::WebContentsObserver:
67 void DidNavigateMainFrame(
68 const content::LoadCommittedDetails
& details
,
69 const content::FrameNavigateParams
& params
) override
;
71 // Returns whether |bubble_| is currently in the process of hiding.
72 bool IsHiding() const;
74 // Returns the image that should be shown as an icon in the omnibox.
75 gfx::Image
AnchorIcon() const;
77 // The title of the bubble.
78 const base::string16
& TitleText() const;
80 // Text in the contents of the bubble.
81 const base::string16
& ContentsText() const;
83 // Ranges of text styles in the bubble's main content.
84 const std::vector
<TextRange
>& ContentsTextRanges() const;
86 // Called when the anchor for this bubble is clicked. Shows a new bubble.
87 void OnAnchorClicked();
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.
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_
; }
99 // Creates a bubble connected to |web_contents|.
100 explicit GeneratedCreditCardBubbleController(content::WebContents
* contents
);
102 // Returns a base::WeakPtr that references |this|. Exposed for testing.
103 base::WeakPtr
<GeneratedCreditCardBubbleController
> GetWeakPtr();
105 // Creates and returns an Autofill credit card bubble. Exposed for testing.
106 virtual base::WeakPtr
<GeneratedCreditCardBubbleView
> CreateBubble();
108 // Returns a weak reference to |bubble_|. May be invalid/NULL.
109 virtual base::WeakPtr
<GeneratedCreditCardBubbleView
> bubble();
111 // Returns whether the bubble can currently show itself.
112 virtual bool CanShow() const;
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;
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_
; }
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
);
131 content::WebContentsUserData
<GeneratedCreditCardBubbleController
>;
133 // An internal helper to show the bubble.
134 void Show(bool was_anchor_click
);
136 // Updates the omnibox icon that |bubble_| is anchored to.
139 // Hides |bubble_| (if it exists and isn't already hiding).
142 // The web contents associated with this bubble.
143 content::WebContents
* const web_contents_
;
145 // The generated credit card number and associated backing card.
146 base::string16 fronting_card_name_
;
147 base::string16 backing_card_name_
;
149 // The title text of the bubble.
150 const base::string16 title_text_
;
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_
;
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_
;
162 // Whether the anchor should currently be showing.
163 bool should_show_anchor_
;
165 // A weak pointer factory for |Create()|.
166 base::WeakPtrFactory
<GeneratedCreditCardBubbleController
> weak_ptr_factory_
;
168 DISALLOW_COPY_AND_ASSIGN(GeneratedCreditCardBubbleController
);
171 } // namespace autofill
173 #endif // CHROME_BROWSER_UI_AUTOFILL_GENERATED_CREDIT_CARD_BUBBLE_CONTROLLER_H_