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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
// Copyright 2013 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 "chrome/browser/extensions/extension_test_notification_observer.h"
#include "base/callback_list.h"
#include "chrome/browser/extensions/extension_action_test_util.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_util.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "content/public/browser/notification_registrar.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/test/test_utils.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/process_manager.h"
#include "extensions/common/extension.h"
using extensions::Extension;
namespace {
// A callback that returns true if the condition has been met and takes no
// arguments.
typedef base::Callback<bool(void)> ConditionCallback;
bool HasPageActionVisibilityReachedTarget(
Browser* browser, size_t target_visible_page_action_count) {
return extensions::extension_action_test_util::GetVisiblePageActionCount(
browser->tab_strip_model()->GetActiveWebContents()) ==
target_visible_page_action_count;
}
bool HaveAllExtensionRenderViewHostsFinishedLoading(
extensions::ProcessManager* manager) {
extensions::ProcessManager::ViewSet all_views = manager->GetAllViews();
for (extensions::ProcessManager::ViewSet::const_iterator iter =
all_views.begin();
iter != all_views.end(); ++iter) {
if ((*iter)->IsLoading())
return false;
}
return true;
}
bool IsExtensionNotIdle(const std::string& extension_id,
content::BrowserContext* context) {
return !extensions::util::IsExtensionIdle(extension_id, context);
}
} // namespace
////////////////////////////////////////////////////////////////////////////////
// ExtensionTestNotificationObserver::NotificationSet
class ExtensionTestNotificationObserver::NotificationSet
: public content::NotificationObserver {
public:
void Add(int type, const content::NotificationSource& source);
void Add(int type);
// Notified any time an Add()ed notification is received.
// The details of the notification are dropped.
base::CallbackList<void()>& callback_list() {
return callback_list_;
}
private:
// content::NotificationObserver:
void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) override;
content::NotificationRegistrar notification_registrar_;
base::CallbackList<void()> callback_list_;
};
void ExtensionTestNotificationObserver::NotificationSet::Add(
int type,
const content::NotificationSource& source) {
notification_registrar_.Add(this, type, source);
}
void ExtensionTestNotificationObserver::NotificationSet::Add(int type) {
Add(type, content::NotificationService::AllSources());
}
void ExtensionTestNotificationObserver::NotificationSet::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
callback_list_.Notify();
}
////////////////////////////////////////////////////////////////////////////////
// ExtensionTestNotificationObserver
ExtensionTestNotificationObserver::ExtensionTestNotificationObserver(
Browser* browser)
: browser_(browser),
profile_(NULL),
extension_installs_observed_(0),
extension_load_errors_observed_(0),
crx_installers_done_observed_(0) {
}
ExtensionTestNotificationObserver::~ExtensionTestNotificationObserver() {}
Profile* ExtensionTestNotificationObserver::GetProfile() {
if (!profile_) {
if (browser_)
profile_ = browser_->profile();
else
profile_ = ProfileManager::GetActiveUserProfile();
}
return profile_;
}
void ExtensionTestNotificationObserver::WaitForNotification(
int notification_type) {
// TODO(bauerb): Using a WindowedNotificationObserver like this can break
// easily, if the notification we're waiting for is sent before this method.
// Change it so that the WindowedNotificationObserver is constructed earlier.
content::NotificationRegistrar registrar;
registrar.Add(
this, notification_type, content::NotificationService::AllSources());
content::WindowedNotificationObserver(
notification_type, content::NotificationService::AllSources()).Wait();
}
bool ExtensionTestNotificationObserver::WaitForPageActionVisibilityChangeTo(
int count) {
extensions::ExtensionActionAPI::Get(GetProfile())->AddObserver(this);
WaitForCondition(
base::Bind(&HasPageActionVisibilityReachedTarget, browser_, count),
NULL);
extensions::ExtensionActionAPI::Get(GetProfile())->
RemoveObserver(this);
return true;
}
bool ExtensionTestNotificationObserver::WaitForExtensionViewsToLoad() {
extensions::ProcessManager* manager =
extensions::ProcessManager::Get(GetProfile());
NotificationSet notification_set;
notification_set.Add(content::NOTIFICATION_WEB_CONTENTS_DESTROYED);
notification_set.Add(content::NOTIFICATION_LOAD_STOP);
WaitForCondition(
base::Bind(&HaveAllExtensionRenderViewHostsFinishedLoading, manager),
¬ification_set);
return true;
}
bool ExtensionTestNotificationObserver::WaitForExtensionIdle(
const std::string& extension_id) {
NotificationSet notification_set;
notification_set.Add(content::NOTIFICATION_RENDERER_PROCESS_TERMINATED);
WaitForCondition(base::Bind(&extensions::util::IsExtensionIdle, extension_id,
GetProfile()),
¬ification_set);
return true;
}
bool ExtensionTestNotificationObserver::WaitForExtensionNotIdle(
const std::string& extension_id) {
NotificationSet notification_set;
notification_set.Add(content::NOTIFICATION_LOAD_STOP);
WaitForCondition(base::Bind(&IsExtensionNotIdle, extension_id, GetProfile()),
¬ification_set);
return true;
}
bool ExtensionTestNotificationObserver::WaitForExtensionInstall() {
int before = extension_installs_observed_;
WaitForNotification(
extensions::NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED);
return extension_installs_observed_ == (before + 1);
}
bool ExtensionTestNotificationObserver::WaitForExtensionInstallError() {
int before = extension_installs_observed_;
content::WindowedNotificationObserver(
extensions::NOTIFICATION_EXTENSION_INSTALL_ERROR,
content::NotificationService::AllSources()).Wait();
return extension_installs_observed_ == before;
}
void ExtensionTestNotificationObserver::WaitForExtensionLoad() {
WaitForNotification(extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED);
}
void ExtensionTestNotificationObserver::WaitForExtensionAndViewLoad() {
this->WaitForExtensionLoad();
WaitForExtensionViewsToLoad();
}
bool ExtensionTestNotificationObserver::WaitForExtensionLoadError() {
int before = extension_load_errors_observed_;
WaitForNotification(extensions::NOTIFICATION_EXTENSION_LOAD_ERROR);
return extension_load_errors_observed_ != before;
}
bool ExtensionTestNotificationObserver::WaitForExtensionCrash(
const std::string& extension_id) {
ExtensionService* service = extensions::ExtensionSystem::Get(
GetProfile())->extension_service();
if (!service->GetExtensionById(extension_id, true)) {
// The extension is already unloaded, presumably due to a crash.
return true;
}
content::WindowedNotificationObserver(
extensions::NOTIFICATION_EXTENSION_PROCESS_TERMINATED,
content::NotificationService::AllSources()).Wait();
return (service->GetExtensionById(extension_id, true) == NULL);
}
bool ExtensionTestNotificationObserver::WaitForCrxInstallerDone() {
int before = crx_installers_done_observed_;
WaitForNotification(extensions::NOTIFICATION_CRX_INSTALLER_DONE);
return crx_installers_done_observed_ == (before + 1);
}
void ExtensionTestNotificationObserver::Watch(
int type,
const content::NotificationSource& source) {
CHECK(!observer_);
observer_.reset(new content::WindowedNotificationObserver(type, source));
registrar_.Add(this, type, source);
}
void ExtensionTestNotificationObserver::Wait() {
observer_->Wait();
registrar_.RemoveAll();
observer_.reset();
}
void ExtensionTestNotificationObserver::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
switch (type) {
case extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED:
last_loaded_extension_id_ =
content::Details<const Extension>(details).ptr()->id();
VLOG(1) << "Got EXTENSION_LOADED notification.";
break;
case extensions::NOTIFICATION_CRX_INSTALLER_DONE:
VLOG(1) << "Got CRX_INSTALLER_DONE notification.";
{
const Extension* extension =
content::Details<const Extension>(details).ptr();
if (extension)
last_loaded_extension_id_ = extension->id();
else
last_loaded_extension_id_.clear();
}
++crx_installers_done_observed_;
break;
case extensions::NOTIFICATION_EXTENSION_WILL_BE_INSTALLED_DEPRECATED:
VLOG(1) << "Got EXTENSION_INSTALLED notification.";
++extension_installs_observed_;
break;
case extensions::NOTIFICATION_EXTENSION_LOAD_ERROR:
VLOG(1) << "Got EXTENSION_LOAD_ERROR notification.";
++extension_load_errors_observed_;
break;
default:
NOTREACHED();
break;
}
}
void ExtensionTestNotificationObserver::OnPageActionsUpdated(
content::WebContents* web_contents) {
MaybeQuit();
}
void ExtensionTestNotificationObserver::WaitForCondition(
const ConditionCallback& condition,
NotificationSet* notification_set) {
if (condition.Run())
return;
condition_ = condition;
scoped_refptr<content::MessageLoopRunner> runner(
new content::MessageLoopRunner);
quit_closure_ = runner->QuitClosure();
scoped_ptr<base::CallbackList<void()>::Subscription> subscription;
if (notification_set) {
subscription = notification_set->callback_list().Add(
base::Bind(&ExtensionTestNotificationObserver::MaybeQuit,
base::Unretained(this)));
}
runner->Run();
condition_.Reset();
quit_closure_.Reset();
}
void ExtensionTestNotificationObserver::MaybeQuit() {
if (condition_.Run())
quit_closure_.Run();
}
|