blob: 5dc1000383bc6deb5fa883b0b4731420bda4eaa2 [file] [log] [blame]
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include <vector>
#include "base/macros.h"
#include "base/observer_list.h"
#include "base/threading/thread_checker.h"
#include "components/bubble/bubble_close_reason.h"
#include "components/bubble/bubble_reference.h"
class BubbleDelegate;
namespace content {
class RenderFrameHost;
// Inherit from BubbleManager to show, update, and close bubbles.
// Any class that inherits from BubbleManager should capture any events that
// should dismiss a bubble or update its anchor point.
// This class assumes that we won't be showing a lot of bubbles simultaneously.
// TODO(hcarmona): Handle simultaneous bubbles.
class BubbleManager {
// This interface should be used to observe the manager. This is useful when
// collecting metrics.
class BubbleManagerObserver {
BubbleManagerObserver() {}
virtual ~BubbleManagerObserver() {}
// Called when a bubble is asked to be displayed but never shown.
// ex: a bubble chained on destruction will not be shown.
virtual void OnBubbleNeverShown(BubbleReference bubble) = 0;
// Called when a bubble is closed. The reason for closing is provided.
virtual void OnBubbleClosed(BubbleReference bubble,
BubbleCloseReason reason) = 0;
// Should be instantiated on the UI thread.
virtual ~BubbleManager();
// Shows a specific bubble and returns a reference to it.
// This reference should be used through the BubbleManager.
BubbleReference ShowBubble(std::unique_ptr<BubbleDelegate> bubble);
// Notify a bubble of an event that might trigger close.
// Returns true if the bubble was actually closed.
bool CloseBubble(BubbleReference bubble, BubbleCloseReason reason);
// Notify all bubbles of an event that might trigger close.
void CloseAllBubbles(BubbleCloseReason reason);
// Notify all bubbles that their anchor or parent may have changed.
void UpdateAllBubbleAnchors();
// Add an observer for this BubbleManager.
void AddBubbleManagerObserver(BubbleManagerObserver* observer);
// Remove an observer from this BubbleManager.
void RemoveBubbleManagerObserver(BubbleManagerObserver* observer);
// Will close any open bubbles and prevent new ones from being shown.
void FinalizePendingRequests();
// Closes bubbles that declare |frame| as their owner, with
void CloseBubblesOwnedBy(const content::RenderFrameHost* frame);
friend class ExtensionInstalledBubbleBrowserTest;
enum ManagerState {
// All matching bubbles will get a close event for the specified |reason|. Any
// bubble that is closed will also be deleted. Bubbles match if 1) |bubble| is
// null or it refers to the bubble, and 2) |owner| is null or owns the bubble.
// At most one can be non-null.
bool CloseAllMatchingBubbles(BubbleController* bubble,
const content::RenderFrameHost* owner,
BubbleCloseReason reason);
base::ObserverList<BubbleManagerObserver> observers_;
// Verify that functions that affect the UI are done on the same thread.
base::ThreadChecker thread_checker_;
// Determines what happens to a bubble when |ShowBubble| is called.
ManagerState manager_state_;
// The bubbles that are being managed.
std::vector<std::unique_ptr<BubbleController>> controllers_;