Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 #include "extensions/test/extension_test_notification_observer.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 | |
| 9 #include "content/public/browser/browser_context.h" | |
| 10 #include "content/public/browser/notification_registrar.h" | |
|
Devlin
2016/10/06 15:52:17
trim
Rahul Chaturvedi
2016/10/11 20:12:35
Done.
| |
| 11 #include "content/public/browser/notification_service.h" | |
| 12 #include "content/public/browser/render_view_host.h" | |
| 13 #include "content/public/test/test_utils.h" | |
| 14 #include "extensions/browser/extension_registry.h" | |
| 15 #include "extensions/browser/extension_system.h" | |
| 16 #include "extensions/browser/notification_types.h" | |
| 17 #include "extensions/common/extension.h" | |
| 18 | |
| 19 using extensions::Extension; | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 // A callback that returns true if the condition has been met and takes no | |
| 24 // arguments. | |
| 25 typedef base::Callback<bool(void)> ConditionCallback; | |
|
Devlin
2016/10/06 15:52:17
using syntax
Rahul Chaturvedi
2016/10/11 20:12:35
Done.
| |
| 26 | |
| 27 const Extension* GetNonTerminatedExtensions(const std::string& id, | |
| 28 content::BrowserContext* context) { | |
| 29 return extensions::ExtensionRegistry::Get(context)->GetExtensionById( | |
| 30 id, extensions::ExtensionRegistry::EVERYTHING & | |
| 31 ~extensions::ExtensionRegistry::TERMINATED); | |
| 32 } | |
| 33 | |
| 34 } // namespace | |
| 35 | |
| 36 //////////////////////////////////////////////////////////////////////////////// | |
| 37 // ExtensionTestNotificationObserver::NotificationSet | |
| 38 | |
| 39 ExtensionTestNotificationObserver::NotificationSet::NotificationSet() | |
| 40 : process_manager_observer_(this) {} | |
| 41 ExtensionTestNotificationObserver::NotificationSet::~NotificationSet() {} | |
| 42 | |
| 43 void ExtensionTestNotificationObserver::NotificationSet::Add( | |
| 44 int type, | |
| 45 const content::NotificationSource& source) { | |
| 46 notification_registrar_.Add(this, type, source); | |
| 47 } | |
| 48 | |
| 49 void ExtensionTestNotificationObserver::NotificationSet::Add(int type) { | |
| 50 Add(type, content::NotificationService::AllSources()); | |
| 51 } | |
| 52 | |
| 53 void ExtensionTestNotificationObserver::NotificationSet:: | |
| 54 AddExtensionFrameUnregistration(extensions::ProcessManager* manager) { | |
| 55 process_manager_observer_.Add(manager); | |
| 56 } | |
| 57 | |
| 58 void ExtensionTestNotificationObserver::NotificationSet::Observe( | |
| 59 int type, | |
| 60 const content::NotificationSource& source, | |
| 61 const content::NotificationDetails& details) { | |
| 62 callback_list_.Notify(); | |
| 63 } | |
| 64 | |
| 65 void ExtensionTestNotificationObserver::NotificationSet:: | |
| 66 OnExtensionFrameUnregistered(const std::string& extension_id, | |
| 67 content::RenderFrameHost* render_frame_host) { | |
| 68 callback_list_.Notify(); | |
| 69 } | |
| 70 | |
| 71 //////////////////////////////////////////////////////////////////////////////// | |
| 72 // ExtensionTestNotificationObserver | |
| 73 | |
| 74 ExtensionTestNotificationObserver::ExtensionTestNotificationObserver( | |
| 75 content::BrowserContext* context) | |
| 76 : context_(context), | |
| 77 extension_installs_observed_(0), | |
| 78 extension_load_errors_observed_(0), | |
| 79 crx_installers_done_observed_(0) {} | |
| 80 | |
| 81 ExtensionTestNotificationObserver::~ExtensionTestNotificationObserver() {} | |
| 82 | |
| 83 void ExtensionTestNotificationObserver::WaitForNotification( | |
| 84 int notification_type) { | |
| 85 // TODO(bauerb): Using a WindowedNotificationObserver like this can break | |
| 86 // easily, if the notification we're waiting for is sent before this method. | |
| 87 // Change it so that the WindowedNotificationObserver is constructed earlier. | |
| 88 content::NotificationRegistrar registrar; | |
| 89 registrar.Add(this, notification_type, | |
| 90 content::NotificationService::AllSources()); | |
| 91 content::WindowedNotificationObserver( | |
| 92 notification_type, content::NotificationService::AllSources()) | |
| 93 .Wait(); | |
| 94 } | |
| 95 | |
| 96 bool ExtensionTestNotificationObserver::WaitForExtensionInstallError() { | |
| 97 int before = extension_installs_observed_; | |
| 98 content::WindowedNotificationObserver( | |
| 99 extensions::NOTIFICATION_EXTENSION_INSTALL_ERROR, | |
| 100 content::NotificationService::AllSources()) | |
| 101 .Wait(); | |
| 102 return extension_installs_observed_ == before; | |
| 103 } | |
| 104 | |
| 105 void ExtensionTestNotificationObserver::WaitForExtensionLoad() { | |
| 106 WaitForNotification(extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED); | |
| 107 } | |
| 108 | |
| 109 bool ExtensionTestNotificationObserver::WaitForExtensionLoadError() { | |
| 110 int before = extension_load_errors_observed_; | |
| 111 WaitForNotification(extensions::NOTIFICATION_EXTENSION_LOAD_ERROR); | |
| 112 return extension_load_errors_observed_ != before; | |
| 113 } | |
| 114 | |
| 115 bool ExtensionTestNotificationObserver::WaitForExtensionCrash( | |
| 116 const std::string& extension_id) { | |
| 117 if (!GetNonTerminatedExtensions(extension_id, context_)) { | |
| 118 // The extension is already unloaded, presumably due to a crash. | |
| 119 return true; | |
| 120 } | |
| 121 | |
| 122 content::WindowedNotificationObserver( | |
| 123 extensions::NOTIFICATION_EXTENSION_PROCESS_TERMINATED, | |
| 124 content::NotificationService::AllSources()) | |
| 125 .Wait(); | |
| 126 return (GetNonTerminatedExtensions(extension_id, context_) == NULL); | |
| 127 } | |
| 128 | |
| 129 bool ExtensionTestNotificationObserver::WaitForCrxInstallerDone() { | |
| 130 int before = crx_installers_done_observed_; | |
| 131 WaitForNotification(extensions::NOTIFICATION_CRX_INSTALLER_DONE); | |
| 132 return crx_installers_done_observed_ == (before + 1); | |
|
Devlin
2016/10/06 15:52:17
no parens
Rahul Chaturvedi
2016/10/11 20:12:35
Done.
| |
| 133 } | |
| 134 | |
| 135 void ExtensionTestNotificationObserver::Watch( | |
| 136 int type, | |
| 137 const content::NotificationSource& source) { | |
| 138 CHECK(!observer_); | |
| 139 observer_.reset(new content::WindowedNotificationObserver(type, source)); | |
| 140 registrar_.Add(this, type, source); | |
| 141 } | |
| 142 | |
| 143 void ExtensionTestNotificationObserver::Wait() { | |
| 144 observer_->Wait(); | |
| 145 | |
| 146 registrar_.RemoveAll(); | |
| 147 observer_.reset(); | |
| 148 } | |
| 149 | |
| 150 void ExtensionTestNotificationObserver::Observe( | |
| 151 int type, | |
| 152 const content::NotificationSource& source, | |
| 153 const content::NotificationDetails& details) { | |
| 154 switch (type) { | |
| 155 case extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED: | |
| 156 last_loaded_extension_id_ = | |
| 157 content::Details<const Extension>(details).ptr()->id(); | |
| 158 VLOG(1) << "Got EXTENSION_LOADED notification."; | |
| 159 break; | |
| 160 | |
| 161 case extensions::NOTIFICATION_CRX_INSTALLER_DONE: | |
| 162 VLOG(1) << "Got CRX_INSTALLER_DONE notification."; | |
| 163 { | |
| 164 const Extension* extension = | |
| 165 content::Details<const Extension>(details).ptr(); | |
| 166 if (extension) | |
| 167 last_loaded_extension_id_ = extension->id(); | |
| 168 else | |
| 169 last_loaded_extension_id_.clear(); | |
| 170 } | |
| 171 ++crx_installers_done_observed_; | |
| 172 break; | |
| 173 | |
| 174 case extensions::NOTIFICATION_EXTENSION_LOAD_ERROR: | |
| 175 VLOG(1) << "Got EXTENSION_LOAD_ERROR notification."; | |
| 176 ++extension_load_errors_observed_; | |
| 177 break; | |
| 178 | |
| 179 default: | |
| 180 NOTREACHED(); | |
| 181 break; | |
| 182 } | |
| 183 } | |
| 184 | |
| 185 void ExtensionTestNotificationObserver::WaitForCondition( | |
| 186 const ConditionCallback& condition, | |
| 187 NotificationSet* notification_set) { | |
| 188 if (condition.Run()) | |
| 189 return; | |
| 190 condition_ = condition; | |
| 191 | |
| 192 scoped_refptr<content::MessageLoopRunner> runner( | |
| 193 new content::MessageLoopRunner); | |
| 194 quit_closure_ = runner->QuitClosure(); | |
| 195 | |
| 196 std::unique_ptr<base::CallbackList<void()>::Subscription> subscription; | |
| 197 if (notification_set) { | |
| 198 subscription = notification_set->callback_list().Add(base::Bind( | |
| 199 &ExtensionTestNotificationObserver::MaybeQuit, base::Unretained(this))); | |
| 200 } | |
| 201 runner->Run(); | |
| 202 | |
| 203 condition_.Reset(); | |
| 204 quit_closure_.Reset(); | |
| 205 } | |
| 206 | |
| 207 void ExtensionTestNotificationObserver::MaybeQuit() { | |
| 208 if (condition_.Run()) | |
| 209 quit_closure_.Run(); | |
| 210 } | |
| OLD | NEW |