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 | |
| 8 | #include "base/memory/scoped_ptr.h" |
| 9 | #include "base/memory/scoped_vector.h" |
hcarmona | 631f598 | 2015-09-11 02:01:24 | [diff] [blame] | 10 | #include "base/observer_list.h" |
hcarmona | aa431c0 | 2015-08-28 18:45:57 | [diff] [blame] | 11 | #include "base/threading/thread_checker.h" |
| 12 | #include "components/bubble/bubble_close_reason.h" |
hcarmona | 428ac3a | 2015-09-08 23:00:56 | [diff] [blame] | 13 | #include "components/bubble/bubble_reference.h" |
hcarmona | aa431c0 | 2015-08-28 18:45:57 | [diff] [blame] | 14 | |
hcarmona | aa431c0 | 2015-08-28 18:45:57 | [diff] [blame] | 15 | class BubbleDelegate; |
| 16 | |
hcarmona | aa431c0 | 2015-08-28 18:45:57 | [diff] [blame] | 17 | // Inherit from BubbleManager to show, update, and close bubbles. |
| 18 | // Any class that inherits from BubbleManager should capture any events that |
| 19 | // should dismiss a bubble or update its anchor point. |
| 20 | // This class assumes that we won't be showing a lot of bubbles simultaneously. |
| 21 | // TODO(hcarmona): Handle simultaneous bubbles. http://crbug.com/366937 |
| 22 | class BubbleManager { |
| 23 | public: |
hcarmona | 631f598 | 2015-09-11 02:01:24 | [diff] [blame] | 24 | // This interface should be used to observe the manager. This is useful when |
| 25 | // collecting metrics. |
| 26 | class BubbleManagerObserver { |
| 27 | public: |
| 28 | BubbleManagerObserver() {} |
| 29 | virtual ~BubbleManagerObserver() {} |
| 30 | |
| 31 | // Called when a bubble is asked to be displayed but never shown. |
| 32 | // ex: a bubble chained on destruction will not be shown. |
| 33 | virtual void OnBubbleNeverShown(BubbleReference bubble) = 0; |
| 34 | |
| 35 | // Called when a bubble is closed. The reason for closing is provided. |
| 36 | virtual void OnBubbleClosed(BubbleReference bubble, |
| 37 | BubbleCloseReason reason) = 0; |
| 38 | |
| 39 | private: |
| 40 | DISALLOW_COPY_AND_ASSIGN(BubbleManagerObserver); |
| 41 | }; |
| 42 | |
hcarmona | aa431c0 | 2015-08-28 18:45:57 | [diff] [blame] | 43 | // Should be instantiated on the UI thread. |
| 44 | BubbleManager(); |
| 45 | virtual ~BubbleManager(); |
| 46 | |
| 47 | // Shows a specific bubble and returns a reference to it. |
| 48 | // This reference should be used through the BubbleManager. |
| 49 | BubbleReference ShowBubble(scoped_ptr<BubbleDelegate> bubble); |
| 50 | |
| 51 | // Notify a bubble of an event that might trigger close. |
| 52 | // Returns true if the bubble was actually closed. |
| 53 | bool CloseBubble(BubbleReference bubble, BubbleCloseReason reason); |
| 54 | |
| 55 | // Notify all bubbles of an event that might trigger close. |
| 56 | void CloseAllBubbles(BubbleCloseReason reason); |
| 57 | |
| 58 | // Notify all bubbles that their anchor or parent may have changed. |
| 59 | void UpdateAllBubbleAnchors(); |
| 60 | |
hcarmona | 631f598 | 2015-09-11 02:01:24 | [diff] [blame] | 61 | // Add an observer for this BubbleManager. |
| 62 | void AddBubbleManagerObserver(BubbleManagerObserver* observer); |
| 63 | |
| 64 | // Remove an observer from this BubbleManager. |
| 65 | void RemoveBubbleManagerObserver(BubbleManagerObserver* observer); |
| 66 | |
| 67 | protected: |
| 68 | // Will close any open bubbles and prevent new ones from being shown. |
| 69 | void FinalizePendingRequests(); |
| 70 | |
hcarmona | aa431c0 | 2015-08-28 18:45:57 | [diff] [blame] | 71 | private: |
hcarmona | ffdc5b2 | 2015-10-06 20:28:55 | [diff] [blame^] | 72 | enum ManagerState { |
hcarmona | aa431c0 | 2015-08-28 18:45:57 | [diff] [blame] | 73 | SHOW_BUBBLES, |
hcarmona | aa431c0 | 2015-08-28 18:45:57 | [diff] [blame] | 74 | NO_MORE_BUBBLES, |
hcarmona | ffdc5b2 | 2015-10-06 20:28:55 | [diff] [blame^] | 75 | ITERATING_BUBBLES, |
hcarmona | aa431c0 | 2015-08-28 18:45:57 | [diff] [blame] | 76 | }; |
| 77 | |
hcarmona | 80b833ae | 2015-10-02 21:09:08 | [diff] [blame] | 78 | // All bubbles will get a close event for the specified |reason| if |match| is |
| 79 | // nullptr, otherwise only the bubble held by |match| will get a close event. |
| 80 | // Any bubble that is closed will also be deleted. |
| 81 | bool CloseAllMatchingBubbles(BubbleController* match, |
| 82 | BubbleCloseReason reason); |
| 83 | |
hcarmona | 631f598 | 2015-09-11 02:01:24 | [diff] [blame] | 84 | base::ObserverList<BubbleManagerObserver> observers_; |
| 85 | |
hcarmona | aa431c0 | 2015-08-28 18:45:57 | [diff] [blame] | 86 | // Verify that functions that affect the UI are done on the same thread. |
| 87 | base::ThreadChecker thread_checker_; |
| 88 | |
| 89 | // Determines what happens to a bubble when |ShowBubble| is called. |
hcarmona | ffdc5b2 | 2015-10-06 20:28:55 | [diff] [blame^] | 90 | ManagerState manager_state_; |
hcarmona | aa431c0 | 2015-08-28 18:45:57 | [diff] [blame] | 91 | |
| 92 | // The bubbles that are being managed. |
| 93 | ScopedVector<BubbleController> controllers_; |
| 94 | |
hcarmona | aa431c0 | 2015-08-28 18:45:57 | [diff] [blame] | 95 | DISALLOW_COPY_AND_ASSIGN(BubbleManager); |
| 96 | }; |
| 97 | |
| 98 | #endif // COMPONENTS_BUBBLE_BUBBLE_MANAGER_H_ |