blob: 935a8b08e02392b7fcf0a2ae7580694dde1c7b2e [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
45 private:
46 DISALLOW_COPY_AND_ASSIGN(BubbleManagerObserver);
47 };
48
hcarmonaaa431c02015-08-28 18:45:5749 // Should be instantiated on the UI thread.
50 BubbleManager();
51 virtual ~BubbleManager();
52
53 // Shows a specific bubble and returns a reference to it.
54 // This reference should be used through the BubbleManager.
dchenga0ee5fb82016-04-26 02:46:5555 BubbleReference ShowBubble(std::unique_ptr<BubbleDelegate> bubble);
hcarmonaaa431c02015-08-28 18:45:5756
57 // Notify a bubble of an event that might trigger close.
58 // Returns true if the bubble was actually closed.
59 bool CloseBubble(BubbleReference bubble, BubbleCloseReason reason);
60
61 // Notify all bubbles of an event that might trigger close.
62 void CloseAllBubbles(BubbleCloseReason reason);
63
64 // Notify all bubbles that their anchor or parent may have changed.
65 void UpdateAllBubbleAnchors();
66
hcarmona631f5982015-09-11 02:01:2467 // Add an observer for this BubbleManager.
68 void AddBubbleManagerObserver(BubbleManagerObserver* observer);
69
70 // Remove an observer from this BubbleManager.
71 void RemoveBubbleManagerObserver(BubbleManagerObserver* observer);
72
Reilly Grant2c6ce192018-06-23 22:41:4773 // Returns the number of bubbles currently being managed.
74 size_t GetBubbleCountForTesting() const;
75
hcarmona631f5982015-09-11 02:01:2476 protected:
77 // Will close any open bubbles and prevent new ones from being shown.
78 void FinalizePendingRequests();
79
jyasskin4a12e672016-02-09 22:39:0780 // Closes bubbles that declare |frame| as their owner, with
81 // a reason of BUBBLE_CLOSE_FRAME_DESTROYED.
82 void CloseBubblesOwnedBy(const content::RenderFrameHost* frame);
83
hcarmonaaa431c02015-08-28 18:45:5784 private:
spqchan41368d62017-09-28 00:40:2585 friend class ExtensionInstalledBubbleBrowserTest;
86
hcarmonaffdc5b22015-10-06 20:28:5587 enum ManagerState {
hcarmonaaa431c02015-08-28 18:45:5788 SHOW_BUBBLES,
hcarmonaaa431c02015-08-28 18:45:5789 NO_MORE_BUBBLES,
hcarmonaffdc5b22015-10-06 20:28:5590 ITERATING_BUBBLES,
hcarmonaaa431c02015-08-28 18:45:5791 };
92
jyasskin4a12e672016-02-09 22:39:0793 // All matching bubbles will get a close event for the specified |reason|. Any
94 // bubble that is closed will also be deleted. Bubbles match if 1) |bubble| is
95 // null or it refers to the bubble, and 2) |owner| is null or owns the bubble.
96 // At most one can be non-null.
97 bool CloseAllMatchingBubbles(BubbleController* bubble,
98 const content::RenderFrameHost* owner,
hcarmona80b833ae2015-10-02 21:09:0899 BubbleCloseReason reason);
100
hcarmona631f5982015-09-11 02:01:24101 base::ObserverList<BubbleManagerObserver> observers_;
102
hcarmonaaa431c02015-08-28 18:45:57103 // Verify that functions that affect the UI are done on the same thread.
104 base::ThreadChecker thread_checker_;
105
106 // Determines what happens to a bubble when |ShowBubble| is called.
hcarmonaffdc5b22015-10-06 20:28:55107 ManagerState manager_state_;
hcarmonaaa431c02015-08-28 18:45:57108
109 // The bubbles that are being managed.
ke.heca350bd2017-01-24 03:56:41110 std::vector<std::unique_ptr<BubbleController>> controllers_;
hcarmonaaa431c02015-08-28 18:45:57111
hcarmonaaa431c02015-08-28 18:45:57112 DISALLOW_COPY_AND_ASSIGN(BubbleManager);
113};
114
115#endif // COMPONENTS_BUBBLE_BUBBLE_MANAGER_H_