blob: b89cba88c21d273a54612e6572c719a413ecd870 [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
8#include "base/memory/scoped_ptr.h"
9#include "base/memory/scoped_vector.h"
hcarmona631f5982015-09-11 02:01:2410#include "base/observer_list.h"
hcarmonaaa431c02015-08-28 18:45:5711#include "base/threading/thread_checker.h"
12#include "components/bubble/bubble_close_reason.h"
hcarmona428ac3a2015-09-08 23:00:5613#include "components/bubble/bubble_reference.h"
hcarmonaaa431c02015-08-28 18:45:5714
hcarmonaaa431c02015-08-28 18:45:5715class BubbleDelegate;
16
hcarmonaaa431c02015-08-28 18:45:5717// 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
22class BubbleManager {
23 public:
hcarmona631f5982015-09-11 02:01:2424 // 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
hcarmonaaa431c02015-08-28 18:45:5743 // 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
hcarmona631f5982015-09-11 02:01:2461 // 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
hcarmonaaa431c02015-08-28 18:45:5771 private:
hcarmonaffdc5b22015-10-06 20:28:5572 enum ManagerState {
hcarmonaaa431c02015-08-28 18:45:5773 SHOW_BUBBLES,
hcarmonaaa431c02015-08-28 18:45:5774 NO_MORE_BUBBLES,
hcarmonaffdc5b22015-10-06 20:28:5575 ITERATING_BUBBLES,
hcarmonaaa431c02015-08-28 18:45:5776 };
77
hcarmona80b833ae2015-10-02 21:09:0878 // 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
hcarmona631f5982015-09-11 02:01:2484 base::ObserverList<BubbleManagerObserver> observers_;
85
hcarmonaaa431c02015-08-28 18:45:5786 // 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.
hcarmonaffdc5b22015-10-06 20:28:5590 ManagerState manager_state_;
hcarmonaaa431c02015-08-28 18:45:5791
92 // The bubbles that are being managed.
93 ScopedVector<BubbleController> controllers_;
94
hcarmonaaa431c02015-08-28 18:45:5795 DISALLOW_COPY_AND_ASSIGN(BubbleManager);
96};
97
98#endif // COMPONENTS_BUBBLE_BUBBLE_MANAGER_H_