blob: d2cda8815007c175d143d391e5bf72eb85789268 [file] [log] [blame]
hcarmonaaa431c02015-08-28 18:45:571// Copyright 2015 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 COMPONENTS_BUBBLE_BUBBLE_MANAGER_H_
6#define COMPONENTS_BUBBLE_BUBBLE_MANAGER_H_
7
dchenga0ee5fb82016-04-26 02:46:558#include <memory>
ke.heca350bd2017-01-24 03:56:419#include <vector>
dchenga0ee5fb82016-04-26 02:46:5510
avibc5337b2015-12-25 23:16:3311#include "base/macros.h"
hcarmona631f5982015-09-11 02:01:2412#include "base/observer_list.h"
hcarmonaaa431c02015-08-28 18:45:5713#include "base/threading/thread_checker.h"
14#include "components/bubble/bubble_close_reason.h"
hcarmona428ac3a2015-09-08 23:00:5615#include "components/bubble/bubble_reference.h"
hcarmonaaa431c02015-08-28 18:45:5716
hcarmonaaa431c02015-08-28 18:45:5717class BubbleDelegate;
18
jyasskin4a12e672016-02-09 22:39:0719namespace content {
20class RenderFrameHost;
21}
22
hcarmonaaa431c02015-08-28 18:45:5723// Inherit from BubbleManager to show, update, and close bubbles.
24// Any class that inherits from BubbleManager should capture any events that
25// should dismiss a bubble or update its anchor point.
26// This class assumes that we won't be showing a lot of bubbles simultaneously.
27// TODO(hcarmona): Handle simultaneous bubbles. http://crbug.com/366937
28class BubbleManager {
29 public:
hcarmona631f5982015-09-11 02:01:2430 // This interface should be used to observe the manager. This is useful when
31 // collecting metrics.
32 class BubbleManagerObserver {
33 public:
34 BubbleManagerObserver() {}
35 virtual ~BubbleManagerObserver() {}
36
37 // Called when a bubble is asked to be displayed but never shown.
38 // ex: a bubble chained on destruction will not be shown.
39 virtual void OnBubbleNeverShown(BubbleReference bubble) = 0;
40
41 // Called when a bubble is closed. The reason for closing is provided.
42 virtual void OnBubbleClosed(BubbleReference bubble,
43 BubbleCloseReason reason) = 0;
44
Suman Kancherla117b3482019-04-02 18:58:5745 // Called when a bubble is shown.
46 virtual void OnBubbleShown(BubbleReference bubble) = 0;
47
hcarmona631f5982015-09-11 02:01:2448 private:
49 DISALLOW_COPY_AND_ASSIGN(BubbleManagerObserver);
50 };
51
hcarmonaaa431c02015-08-28 18:45:5752 // Should be instantiated on the UI thread.
53 BubbleManager();
54 virtual ~BubbleManager();
55
56 // Shows a specific bubble and returns a reference to it.
57 // This reference should be used through the BubbleManager.
dchenga0ee5fb82016-04-26 02:46:5558 BubbleReference ShowBubble(std::unique_ptr<BubbleDelegate> bubble);
hcarmonaaa431c02015-08-28 18:45:5759
60 // Notify a bubble of an event that might trigger close.
61 // Returns true if the bubble was actually closed.
62 bool CloseBubble(BubbleReference bubble, BubbleCloseReason reason);
63
64 // Notify all bubbles of an event that might trigger close.
65 void CloseAllBubbles(BubbleCloseReason reason);
66
67 // Notify all bubbles that their anchor or parent may have changed.
68 void UpdateAllBubbleAnchors();
69
hcarmona631f5982015-09-11 02:01:2470 // Add an observer for this BubbleManager.
71 void AddBubbleManagerObserver(BubbleManagerObserver* observer);
72
73 // Remove an observer from this BubbleManager.
74 void RemoveBubbleManagerObserver(BubbleManagerObserver* observer);
75
Reilly Grant2c6ce192018-06-23 22:41:4776 // Returns the number of bubbles currently being managed.
77 size_t GetBubbleCountForTesting() const;
78
hcarmona631f5982015-09-11 02:01:2479 protected:
80 // Will close any open bubbles and prevent new ones from being shown.
81 void FinalizePendingRequests();
82
jyasskin4a12e672016-02-09 22:39:0783 // Closes bubbles that declare |frame| as their owner, with
84 // a reason of BUBBLE_CLOSE_FRAME_DESTROYED.
85 void CloseBubblesOwnedBy(const content::RenderFrameHost* frame);
86
hcarmonaaa431c02015-08-28 18:45:5787 private:
spqchan41368d62017-09-28 00:40:2588 friend class ExtensionInstalledBubbleBrowserTest;
89
hcarmonaffdc5b22015-10-06 20:28:5590 enum ManagerState {
hcarmonaaa431c02015-08-28 18:45:5791 SHOW_BUBBLES,
hcarmonaaa431c02015-08-28 18:45:5792 NO_MORE_BUBBLES,
hcarmonaffdc5b22015-10-06 20:28:5593 ITERATING_BUBBLES,
hcarmonaaa431c02015-08-28 18:45:5794 };
95
jyasskin4a12e672016-02-09 22:39:0796 // All matching bubbles will get a close event for the specified |reason|. Any
97 // bubble that is closed will also be deleted. Bubbles match if 1) |bubble| is
98 // null or it refers to the bubble, and 2) |owner| is null or owns the bubble.
99 // At most one can be non-null.
100 bool CloseAllMatchingBubbles(BubbleController* bubble,
101 const content::RenderFrameHost* owner,
hcarmona80b833ae2015-10-02 21:09:08102 BubbleCloseReason reason);
103
Trent Apteda250ec3ab2018-08-19 08:52:19104 base::ObserverList<BubbleManagerObserver>::Unchecked observers_;
hcarmona631f5982015-09-11 02:01:24105
hcarmonaaa431c02015-08-28 18:45:57106 // Verify that functions that affect the UI are done on the same thread.
107 base::ThreadChecker thread_checker_;
108
109 // Determines what happens to a bubble when |ShowBubble| is called.
hcarmonaffdc5b22015-10-06 20:28:55110 ManagerState manager_state_;
hcarmonaaa431c02015-08-28 18:45:57111
112 // The bubbles that are being managed.
ke.heca350bd2017-01-24 03:56:41113 std::vector<std::unique_ptr<BubbleController>> controllers_;
hcarmonaaa431c02015-08-28 18:45:57114
hcarmonaaa431c02015-08-28 18:45:57115 DISALLOW_COPY_AND_ASSIGN(BubbleManager);
116};
117
118#endif // COMPONENTS_BUBBLE_BUBBLE_MANAGER_H_