1 // Copyright 2020 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_LOCATION_BAR_PERMISSION_CHIP_H_
6 #define CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_PERMISSION_CHIP_H_
7 
8 #include "base/timer/timer.h"
9 #include "components/permissions/permission_prompt.h"
10 #include "components/permissions/permission_request.h"
11 #include "ui/gfx/animation/animation_delegate.h"
12 #include "ui/gfx/animation/slide_animation.h"
13 #include "ui/views/animation/animation_delegate_views.h"
14 #include "ui/views/controls/button/md_text_button.h"
15 #include "ui/views/widget/widget_observer.h"
16 
17 class Browser;
18 class PermissionPromptBubbleView;
19 
20 namespace views {
21 class Widget;
22 }  // namespace views
23 
24 class BubbleOwnerDelegate {
25  public:
26   virtual bool IsBubbleShowing() const = 0;
27 };
28 
29 // A chip view shown in the location bar to notify user about a permission
30 // request. Shows a permission bubble on click.
31 class PermissionChip : public views::View,
32                        public views::AnimationDelegateViews,
33                        public views::WidgetObserver,
34                        public BubbleOwnerDelegate {
35  public:
36   explicit PermissionChip(Browser* browser);
37   PermissionChip(const PermissionChip& mask_layer) = delete;
38   PermissionChip& operator=(const PermissionChip& mask_layer) = delete;
39 
40   ~PermissionChip() override;
41 
42   void Show(permissions::PermissionPrompt::Delegate* delegate);
43   void Hide();
44 
button()45   views::Button* button() { return chip_button_; }
46 
47   // views::AnimationDelegateViews:
48   void AnimationEnded(const gfx::Animation* animation) override;
49   void AnimationProgressed(const gfx::Animation* animation) override;
50 
51   // views::View:
52   gfx::Size CalculatePreferredSize() const override;
53   void OnMouseEntered(const ui::MouseEvent& event) override;
54   void OnThemeChanged() override;
55 
56   // views::WidgetObserver:
57   void OnWidgetDestroying(views::Widget* widget) override;
58 
59   // BubbleOwnerDelegate:
60   bool IsBubbleShowing() const override;
61 
62  private:
63   void ChipButtonPressed();
64   void Collapse();
65   void StartCollapseTimer();
66   int GetIconSize() const;
67   void UpdatePermissionIconAndTextColor();
68   base::string16 GetPermissionMessage();
69   const gfx::VectorIcon& GetPermissionIconId();
70 
71   Browser* browser_ = nullptr;
72   permissions::PermissionPrompt::Delegate* delegate_ = nullptr;
73   PermissionPromptBubbleView* prompt_bubble_ = nullptr;
74 
75   // An animation used for expanding and collapsing the chip.
76   std::unique_ptr<gfx::SlideAnimation> animation_;
77 
78   // A timer used to collapse the chip after a delay.
79   base::OneShotTimer timer_;
80 
81   // The button that displays the icon and text.
82   views::MdTextButton* chip_button_;
83 
84   // The time when the permission was requested.
85   base::TimeTicks requested_time_;
86 
87   // If uma metric was already recorded on the button click.
88   bool already_recorded_interaction_ = false;
89 };
90 
91 #endif  // CHROME_BROWSER_UI_VIEWS_LOCATION_BAR_PERMISSION_CHIP_H_
92