1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef EXTENSIONS_TEST_EXTENSION_TEST_NOTIFICATION_OBSERVER_H_
#define EXTENSIONS_TEST_EXTENSION_TEST_NOTIFICATION_OBSERVER_H_
#include <map>
#include <memory>
#include <string>
#include "base/callback_list.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/scoped_observation.h"
#include "content/public/test/browser_test_utils.h"
#include "extensions/browser/process_manager.h"
#include "extensions/browser/process_manager_observer.h"
#include "extensions/common/extension_id.h"
namespace content {
class BrowserContext;
class WebContents;
}
namespace extensions {
// Test helper class for observing extension-related events.
class ExtensionTestNotificationObserver {
public:
explicit ExtensionTestNotificationObserver(content::BrowserContext* context);
ExtensionTestNotificationObserver(const ExtensionTestNotificationObserver&) =
delete;
ExtensionTestNotificationObserver& operator=(
const ExtensionTestNotificationObserver&) = delete;
virtual ~ExtensionTestNotificationObserver();
// Waits for all extension views to load.
bool WaitForExtensionViewsToLoad();
// Waits for the extension associated with the given `extension_id` to be
// idle.
bool WaitForExtensionIdle(const ExtensionId& extension_id);
// Waits for the extension associated with the given `extension_id` to not
// be considered idle.
bool WaitForExtensionNotIdle(const ExtensionId& extension_id);
protected:
class NotificationSet : public extensions::ProcessManagerObserver {
public:
explicit NotificationSet(ProcessManager* manager);
NotificationSet(const NotificationSet&) = delete;
NotificationSet& operator=(const NotificationSet&) = delete;
~NotificationSet() override;
// Notified any time a notification is received.
// The details of the notification are dropped.
base::RepeatingClosureList& closure_list() { return closure_list_; }
private:
class ForwardingWebContentsObserver;
// extensions::ProcessManagerObserver:
void OnExtensionFrameUnregistered(
const ExtensionId& extension_id,
content::RenderFrameHost* render_frame_host) override;
void OnWebContentsCreated(content::WebContents* web_contents);
void StartObservingWebContents(content::WebContents* web_contents);
void DidStopLoading(content::WebContents* web_contents);
void WebContentsDestroyed(content::WebContents* web_contents);
base::RepeatingClosureList closure_list_;
base::ScopedObservation<extensions::ProcessManager,
extensions::ProcessManagerObserver>
process_manager_observation_{this};
base::CallbackListSubscription web_contents_creation_subscription_ =
content::RegisterWebContentsCreationCallback(
base::BindRepeating(&NotificationSet::OnWebContentsCreated,
base::Unretained(this)));
std::map<content::WebContents*,
std::unique_ptr<ForwardingWebContentsObserver>>
web_contents_observers_;
};
// A callback that returns true if the condition has been met and takes no
// arguments.
using ConditionCallback = base::RepeatingCallback<bool(void)>;
// Wait for `condition_` to be met. `notification_set` is the set of
// notifications to wait for and to check `condition` when observing. This
// can be NULL if we are instead waiting for a different observer method, like
// OnPageActionsUpdated().
void WaitForCondition(const ConditionCallback& condition,
NotificationSet* notification_set);
// Quits the message loop if `condition_` is met.
void MaybeQuit();
raw_ptr<content::BrowserContext, AcrossTasksDanglingUntriaged> context_;
private:
// The condition for which we are waiting. This should be checked in any
// observing methods that could trigger it.
ConditionCallback condition_;
// The closure to quit the currently-running message loop.
base::OnceClosure quit_closure_;
};
} // namespace extensions
#endif // EXTENSIONS_TEST_EXTENSION_TEST_NOTIFICATION_OBSERVER_H_
|