hcarmona | aa431c0 | 2015-08-28 18:45:57 | [diff] [blame] | 1 | // 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 | |
dcheng | a0ee5fb8 | 2016-04-26 02:46:55 | [diff] [blame] | 8 | #include <memory> |
ke.he | ca350bd | 2017-01-24 03:56:41 | [diff] [blame] | 9 | #include <vector> |
dcheng | a0ee5fb8 | 2016-04-26 02:46:55 | [diff] [blame] | 10 | |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 11 | #include "base/macros.h" |
hcarmona | 631f598 | 2015-09-11 02:01:24 | [diff] [blame] | 12 | #include "base/observer_list.h" |
hcarmona | aa431c0 | 2015-08-28 18:45:57 | [diff] [blame] | 13 | #include "base/threading/thread_checker.h" |
| 14 | #include "components/bubble/bubble_close_reason.h" |
hcarmona | 428ac3a | 2015-09-08 23:00:56 | [diff] [blame] | 15 | #include "components/bubble/bubble_reference.h" |
hcarmona | aa431c0 | 2015-08-28 18:45:57 | [diff] [blame] | 16 | |
hcarmona | aa431c0 | 2015-08-28 18:45:57 | [diff] [blame] | 17 | class BubbleDelegate; |
| 18 | |
jyasskin | 4a12e67 | 2016-02-09 22:39:07 | [diff] [blame] | 19 | namespace content { |
| 20 | class RenderFrameHost; |
| 21 | } |
| 22 | |
hcarmona | aa431c0 | 2015-08-28 18:45:57 | [diff] [blame] | 23 | // 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 |
| 28 | class BubbleManager { |
| 29 | public: |
hcarmona | 631f598 | 2015-09-11 02:01:24 | [diff] [blame] | 30 | // 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 Kancherla | 117b348 | 2019-04-02 18:58:57 | [diff] [blame] | 45 | // Called when a bubble is shown. |
| 46 | virtual void OnBubbleShown(BubbleReference bubble) = 0; |
| 47 | |
hcarmona | 631f598 | 2015-09-11 02:01:24 | [diff] [blame] | 48 | private: |
| 49 | DISALLOW_COPY_AND_ASSIGN(BubbleManagerObserver); |
| 50 | }; |
| 51 | |
hcarmona | aa431c0 | 2015-08-28 18:45:57 | [diff] [blame] | 52 | // 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. |
dcheng | a0ee5fb8 | 2016-04-26 02:46:55 | [diff] [blame] | 58 | BubbleReference ShowBubble(std::unique_ptr<BubbleDelegate> bubble); |
hcarmona | aa431c0 | 2015-08-28 18:45:57 | [diff] [blame] | 59 | |
| 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 | |
hcarmona | 631f598 | 2015-09-11 02:01:24 | [diff] [blame] | 70 | // 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 Grant | 2c6ce19 | 2018-06-23 22:41:47 | [diff] [blame] | 76 | // Returns the number of bubbles currently being managed. |
| 77 | size_t GetBubbleCountForTesting() const; |
| 78 | |
hcarmona | 631f598 | 2015-09-11 02:01:24 | [diff] [blame] | 79 | protected: |
| 80 | // Will close any open bubbles and prevent new ones from being shown. |
| 81 | void FinalizePendingRequests(); |
| 82 | |
jyasskin | 4a12e67 | 2016-02-09 22:39:07 | [diff] [blame] | 83 | // 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 | |
hcarmona | aa431c0 | 2015-08-28 18:45:57 | [diff] [blame] | 87 | private: |
spqchan | 41368d6 | 2017-09-28 00:40:25 | [diff] [blame] | 88 | friend class ExtensionInstalledBubbleBrowserTest; |
| 89 | |
hcarmona | ffdc5b2 | 2015-10-06 20:28:55 | [diff] [blame] | 90 | enum ManagerState { |
hcarmona | aa431c0 | 2015-08-28 18:45:57 | [diff] [blame] | 91 | SHOW_BUBBLES, |
hcarmona | aa431c0 | 2015-08-28 18:45:57 | [diff] [blame] | 92 | NO_MORE_BUBBLES, |
hcarmona | ffdc5b2 | 2015-10-06 20:28:55 | [diff] [blame] | 93 | ITERATING_BUBBLES, |
hcarmona | aa431c0 | 2015-08-28 18:45:57 | [diff] [blame] | 94 | }; |
| 95 | |
jyasskin | 4a12e67 | 2016-02-09 22:39:07 | [diff] [blame] | 96 | // 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, |
hcarmona | 80b833ae | 2015-10-02 21:09:08 | [diff] [blame] | 102 | BubbleCloseReason reason); |
| 103 | |
Trent Apted | a250ec3ab | 2018-08-19 08:52:19 | [diff] [blame] | 104 | base::ObserverList<BubbleManagerObserver>::Unchecked observers_; |
hcarmona | 631f598 | 2015-09-11 02:01:24 | [diff] [blame] | 105 | |
hcarmona | aa431c0 | 2015-08-28 18:45:57 | [diff] [blame] | 106 | // 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. |
hcarmona | ffdc5b2 | 2015-10-06 20:28:55 | [diff] [blame] | 110 | ManagerState manager_state_; |
hcarmona | aa431c0 | 2015-08-28 18:45:57 | [diff] [blame] | 111 | |
| 112 | // The bubbles that are being managed. |
ke.he | ca350bd | 2017-01-24 03:56:41 | [diff] [blame] | 113 | std::vector<std::unique_ptr<BubbleController>> controllers_; |
hcarmona | aa431c0 | 2015-08-28 18:45:57 | [diff] [blame] | 114 | |
hcarmona | aa431c0 | 2015-08-28 18:45:57 | [diff] [blame] | 115 | DISALLOW_COPY_AND_ASSIGN(BubbleManager); |
| 116 | }; |
| 117 | |
| 118 | #endif // COMPONENTS_BUBBLE_BUBBLE_MANAGER_H_ |