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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
// 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.
#include "extensions/test/extension_test_notification_observer.h"
#include <memory>
#include <set>
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/test/test_utils.h"
#include "extensions/browser/extension_util.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_id.h"
namespace extensions {
namespace {
bool HaveAllExtensionRenderFrameHostsFinishedLoading(ProcessManager* manager) {
for (content::RenderFrameHost* host : manager->GetAllFrames()) {
if (content::WebContents::FromRenderFrameHost(host)->IsLoading()) {
return false;
}
}
return true;
}
} // namespace
////////////////////////////////////////////////////////////////////////////////
// NotificationSet::ForwardingWebContentsObserver
class ExtensionTestNotificationObserver::NotificationSet::
ForwardingWebContentsObserver : public content::WebContentsObserver {
public:
ForwardingWebContentsObserver(
content::WebContents* contents,
ExtensionTestNotificationObserver::NotificationSet* owner)
: WebContentsObserver(contents), owner_(owner) {}
private:
// content::WebContentsObserver
void DidStopLoading() override { owner_->DidStopLoading(web_contents()); }
void WebContentsDestroyed() override {
// Do not add code after this line, deletes `this`.
owner_->WebContentsDestroyed(web_contents());
}
raw_ptr<ExtensionTestNotificationObserver::NotificationSet> owner_;
};
////////////////////////////////////////////////////////////////////////////////
// ExtensionTestNotificationObserver::NotificationSet
ExtensionTestNotificationObserver::NotificationSet::NotificationSet(
ProcessManager* manager) {
process_manager_observation_.Observe(manager);
std::set<content::WebContents*> initial_extension_contents;
for (content::RenderFrameHost* rfh : manager->GetAllFrames()) {
initial_extension_contents.insert(
content::WebContents::FromRenderFrameHost(rfh));
}
for (content::WebContents* web_contents : initial_extension_contents) {
StartObservingWebContents(web_contents);
}
}
ExtensionTestNotificationObserver::NotificationSet::~NotificationSet() =
default;
void ExtensionTestNotificationObserver::NotificationSet::
OnExtensionFrameUnregistered(const ExtensionId& extension_id,
content::RenderFrameHost* render_frame_host) {
closure_list_.Notify();
}
void ExtensionTestNotificationObserver::NotificationSet::OnWebContentsCreated(
content::WebContents* web_contents) {
StartObservingWebContents(web_contents);
}
void ExtensionTestNotificationObserver::NotificationSet::
StartObservingWebContents(content::WebContents* web_contents) {
CHECK(!base::Contains(web_contents_observers_, web_contents));
web_contents_observers_[web_contents] =
std::make_unique<ForwardingWebContentsObserver>(web_contents, this);
}
void ExtensionTestNotificationObserver::NotificationSet::DidStopLoading(
content::WebContents* web_contents) {
closure_list_.Notify();
}
void ExtensionTestNotificationObserver::NotificationSet::WebContentsDestroyed(
content::WebContents* web_contents) {
web_contents_observers_.erase(web_contents);
closure_list_.Notify();
}
////////////////////////////////////////////////////////////////////////////////
// ExtensionTestNotificationObserver
ExtensionTestNotificationObserver::ExtensionTestNotificationObserver(
content::BrowserContext* context)
: context_(context) {}
ExtensionTestNotificationObserver::~ExtensionTestNotificationObserver() =
default;
bool ExtensionTestNotificationObserver::WaitForExtensionViewsToLoad() {
// Some views might not be created yet. This call may become insufficient if
// e.g. implementation of ExtensionHostQueue changes.
base::RunLoop().RunUntilIdle();
ProcessManager* manager = ProcessManager::Get(context_);
NotificationSet notification_set(manager);
WaitForCondition(
base::BindRepeating(&HaveAllExtensionRenderFrameHostsFinishedLoading,
manager),
¬ification_set);
return true;
}
bool ExtensionTestNotificationObserver::WaitForExtensionIdle(
const ExtensionId& extension_id) {
ProcessManager* manager = ProcessManager::Get(context_);
NotificationSet notification_set(manager);
WaitForCondition(
base::BindRepeating(&util::IsExtensionIdle, extension_id, context_),
¬ification_set);
return true;
}
bool ExtensionTestNotificationObserver::WaitForExtensionNotIdle(
const ExtensionId& extension_id) {
ProcessManager* manager = ProcessManager::Get(context_);
NotificationSet notification_set(manager);
WaitForCondition(base::BindRepeating(
[](const ExtensionId& extension_id,
content::BrowserContext* context) -> bool {
return !util::IsExtensionIdle(extension_id, context);
},
extension_id, context_),
¬ification_set);
return true;
}
void ExtensionTestNotificationObserver::WaitForCondition(
const ConditionCallback& condition,
NotificationSet* notification_set) {
if (condition.Run())
return;
condition_ = condition;
base::RunLoop run_loop;
quit_closure_ = run_loop.QuitClosure();
base::CallbackListSubscription subscription;
if (notification_set) {
subscription = notification_set->closure_list().Add(base::BindRepeating(
&ExtensionTestNotificationObserver::MaybeQuit, base::Unretained(this)));
}
run_loop.Run();
condition_.Reset();
quit_closure_.Reset();
}
void ExtensionTestNotificationObserver::MaybeQuit() {
// We can be called synchronously from any of the events being observed,
// so return immediately if the closure has already been run.
if (quit_closure_.is_null())
return;
if (condition_.Run())
std::move(quit_closure_).Run();
}
} // namespace extensions
|