1 // Copyright 2017 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_VIEWS_PASSWORDS_PASSWORD_BUBBLE_VIEW_BASE_H_
6 #define CHROME_BROWSER_UI_VIEWS_PASSWORDS_PASSWORD_BUBBLE_VIEW_BASE_H_
7 
8 #include <memory>
9 
10 #include "base/macros.h"
11 #include "build/build_config.h"
12 #include "chrome/browser/ui/views/location_bar/location_bar_bubble_delegate_view.h"
13 
14 namespace content {
15 class WebContents;
16 }
17 
18 namespace password_manager {
19 struct PasswordForm;
20 }  // namespace password_manager
21 
22 namespace views {
23 class Label;
24 }
25 
26 class FeaturePromoControllerViews;
27 class PasswordBubbleControllerBase;
28 
29 // Base class for all manage-passwords bubbles. Provides static methods for
30 // creating and showing these dialogs. Also used to access the web contents
31 // related to the dialog.
32 // These bubbles remove themselves as globals on destruction.
33 // TODO(pbos): Remove static global usage and move dialog ownership to
34 // TabDialog instances. Consider removing access to GetWebContents() through
35 // this class when ownership has moved to TabDialog instances, as it's hopefully
36 // no longer relevant for checking dialog ownership. These two work items should
37 // make this base class significantly smaller.
38 class PasswordBubbleViewBase : public LocationBarBubbleDelegateView {
39  public:
40   // Returns a pointer to the bubble.
manage_password_bubble()41   static PasswordBubbleViewBase* manage_password_bubble() {
42     return g_manage_passwords_bubble_;
43   }
44 
45   // Shows an appropriate bubble on the toolkit-views Browser window containing
46   // |web_contents|.
47   static void ShowBubble(content::WebContents* web_contents,
48                          DisplayReason reason);
49   // Creates and returns the passwords manager bubble UI appropriate for the
50   // current password_manager::ui::State value for the provided |web_contents|.
51   static PasswordBubbleViewBase* CreateBubble(
52       content::WebContents* web_contents,
53       views::View* anchor_view,
54       DisplayReason reason,
55       FeaturePromoControllerViews* promo_controller);
56 
57   // Closes the existing bubble.
58   static void CloseCurrentBubble();
59 
60   // Makes the bubble the foreground window.
61   static void ActivateBubble();
62 
63   const content::WebContents* GetWebContents() const;
64 
65   // Returns the PasswordBubbleController used by the view. Returns nullptr if
66   // the view is still using the ManagerPasswordBubbleModel instead of a
67   // PasswordBubbleController.
68   virtual PasswordBubbleControllerBase* GetController() = 0;
69   virtual const PasswordBubbleControllerBase* GetController() const = 0;
70 
71  protected:
72   // The |easily_dismissable| flag indicates if the bubble should close upon
73   // a click in the content area of the browser.
74   PasswordBubbleViewBase(content::WebContents* web_contents,
75                          views::View* anchor_view,
76                          bool easily_dismissable);
77 
78   ~PasswordBubbleViewBase() override;
79 
80   static std::unique_ptr<views::Label> CreateUsernameLabel(
81       const password_manager::PasswordForm& form);
82   static std::unique_ptr<views::Label> CreatePasswordLabel(
83       const password_manager::PasswordForm& form);
84 
85  private:
86   // views::BubbleDialogDelegateView:
87   void Init() override;
88 
89   // WidgetObserver:
90   void OnWidgetClosing(views::Widget* widget) override;
91 
92   // Singleton instance of the Password bubble.The instance is owned by the
93   // Bubble and will be deleted when the bubble closes.
94   static PasswordBubbleViewBase* g_manage_passwords_bubble_;
95 
96   // Listens for WebContentsView events and closes the bubble so the bubble gets
97   // dismissed when users keep using the web page.
98   std::unique_ptr<WebContentMouseHandler> mouse_handler_;
99 
100   DISALLOW_COPY_AND_ASSIGN(PasswordBubbleViewBase);
101 };
102 
103 #endif  // CHROME_BROWSER_UI_VIEWS_PASSWORDS_PASSWORD_BUBBLE_VIEW_BASE_H_
104