blob: 5dc1000383bc6deb5fa883b0b4731420bda4eaa2 [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
73 protected:
74 // Will close any open bubbles and prevent new ones from being shown.
75 void FinalizePendingRequests();
76
jyasskin4a12e672016-02-09 22:39:0777 // Closes bubbles that declare |frame| as their owner, with
78 // a reason of BUBBLE_CLOSE_FRAME_DESTROYED.
79 void CloseBubblesOwnedBy(const content::RenderFrameHost* frame);
80
hcarmonaaa431c02015-08-28 18:45:5781 private:
spqchan41368d62017-09-28 00:40:2582 friend class ExtensionInstalledBubbleBrowserTest;
83
hcarmonaffdc5b22015-10-06 20:28:5584 enum ManagerState {
hcarmonaaa431c02015-08-28 18:45:5785 SHOW_BUBBLES,
hcarmonaaa431c02015-08-28 18:45:5786 NO_MORE_BUBBLES,
hcarmonaffdc5b22015-10-06 20:28:5587 ITERATING_BUBBLES,
hcarmonaaa431c02015-08-28 18:45:5788 };
89
jyasskin4a12e672016-02-09 22:39:0790 // All matching bubbles will get a close event for the specified |reason|. Any
91 // bubble that is closed will also be deleted. Bubbles match if 1) |bubble| is
92 // null or it refers to the bubble, and 2) |owner| is null or owns the bubble.
93 // At most one can be non-null.
94 bool CloseAllMatchingBubbles(BubbleController* bubble,
95 const content::RenderFrameHost* owner,
hcarmona80b833ae2015-10-02 21:09:0896 BubbleCloseReason reason);
97
hcarmona631f5982015-09-11 02:01:2498 base::ObserverList<BubbleManagerObserver> observers_;
99
hcarmonaaa431c02015-08-28 18:45:57100 // Verify that functions that affect the UI are done on the same thread.
101 base::ThreadChecker thread_checker_;
102
103 // Determines what happens to a bubble when |ShowBubble| is called.
hcarmonaffdc5b22015-10-06 20:28:55104 ManagerState manager_state_;
hcarmonaaa431c02015-08-28 18:45:57105
106 // The bubbles that are being managed.
ke.heca350bd2017-01-24 03:56:41107 std::vector<std::unique_ptr<BubbleController>> controllers_;
hcarmonaaa431c02015-08-28 18:45:57108
hcarmonaaa431c02015-08-28 18:45:57109 DISALLOW_COPY_AND_ASSIGN(BubbleManager);
110};
111
112#endif // COMPONENTS_BUBBLE_BUBBLE_MANAGER_H_