| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "extensions/test/extension_test_notification_observer.h" | 5 #include "extensions/test/extension_test_notification_observer.h" |
| 6 | 6 |
| 7 #include "content/public/browser/browser_context.h" | 7 #include "content/public/browser/browser_context.h" |
| 8 #include "content/public/browser/notification_details.h" | 8 #include "content/public/browser/notification_details.h" |
| 9 #include "content/public/browser/notification_registrar.h" | 9 #include "content/public/browser/notification_registrar.h" |
| 10 #include "content/public/browser/notification_service.h" | 10 #include "content/public/browser/notification_service.h" |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 } | 67 } |
| 68 | 68 |
| 69 //////////////////////////////////////////////////////////////////////////////// | 69 //////////////////////////////////////////////////////////////////////////////// |
| 70 // ExtensionTestNotificationObserver | 70 // ExtensionTestNotificationObserver |
| 71 | 71 |
| 72 ExtensionTestNotificationObserver::ExtensionTestNotificationObserver( | 72 ExtensionTestNotificationObserver::ExtensionTestNotificationObserver( |
| 73 content::BrowserContext* context) | 73 content::BrowserContext* context) |
| 74 : context_(context), | 74 : context_(context), |
| 75 extension_installs_observed_(0), | 75 extension_installs_observed_(0), |
| 76 extension_load_errors_observed_(0), | 76 extension_load_errors_observed_(0), |
| 77 crx_installers_done_observed_(0) {} | 77 crx_installers_done_observed_(0), |
| 78 registry_observer_(this) { |
| 79 if (context_) |
| 80 registry_observer_.Add(ExtensionRegistry::Get(context_)); |
| 81 } |
| 78 | 82 |
| 79 ExtensionTestNotificationObserver::~ExtensionTestNotificationObserver() {} | 83 ExtensionTestNotificationObserver::~ExtensionTestNotificationObserver() {} |
| 80 | 84 |
| 81 void ExtensionTestNotificationObserver::WaitForNotification( | 85 void ExtensionTestNotificationObserver::WaitForNotification( |
| 82 int notification_type) { | 86 int notification_type) { |
| 83 // TODO(bauerb): Using a WindowedNotificationObserver like this can break | 87 // TODO(bauerb): Using a WindowedNotificationObserver like this can break |
| 84 // easily, if the notification we're waiting for is sent before this method. | 88 // easily, if the notification we're waiting for is sent before this method. |
| 85 // Change it so that the WindowedNotificationObserver is constructed earlier. | 89 // Change it so that the WindowedNotificationObserver is constructed earlier. |
| 86 content::NotificationRegistrar registrar; | 90 content::NotificationRegistrar registrar; |
| 87 registrar.Add(this, notification_type, | 91 registrar.Add(this, notification_type, |
| 88 content::NotificationService::AllSources()); | 92 content::NotificationService::AllSources()); |
| 89 content::WindowedNotificationObserver( | 93 content::WindowedNotificationObserver( |
| 90 notification_type, content::NotificationService::AllSources()) | 94 notification_type, content::NotificationService::AllSources()) |
| 91 .Wait(); | 95 .Wait(); |
| 92 } | 96 } |
| 93 | 97 |
| 94 bool ExtensionTestNotificationObserver::WaitForExtensionInstallError() { | 98 bool ExtensionTestNotificationObserver::WaitForExtensionInstallError() { |
| 95 int before = extension_installs_observed_; | 99 int before = extension_installs_observed_; |
| 96 content::WindowedNotificationObserver( | 100 content::WindowedNotificationObserver( |
| 97 NOTIFICATION_EXTENSION_INSTALL_ERROR, | 101 NOTIFICATION_EXTENSION_INSTALL_ERROR, |
| 98 content::NotificationService::AllSources()) | 102 content::NotificationService::AllSources()) |
| 99 .Wait(); | 103 .Wait(); |
| 100 return extension_installs_observed_ == before; | 104 return extension_installs_observed_ == before; |
| 101 } | 105 } |
| 102 | 106 |
| 103 void ExtensionTestNotificationObserver::WaitForExtensionLoad() { | |
| 104 WaitForNotification(NOTIFICATION_EXTENSION_LOADED_DEPRECATED); | |
| 105 } | |
| 106 | |
| 107 bool ExtensionTestNotificationObserver::WaitForExtensionLoadError() { | 107 bool ExtensionTestNotificationObserver::WaitForExtensionLoadError() { |
| 108 int before = extension_load_errors_observed_; | 108 int before = extension_load_errors_observed_; |
| 109 WaitForNotification(NOTIFICATION_EXTENSION_LOAD_ERROR); | 109 WaitForNotification(NOTIFICATION_EXTENSION_LOAD_ERROR); |
| 110 return extension_load_errors_observed_ != before; | 110 return extension_load_errors_observed_ != before; |
| 111 } | 111 } |
| 112 | 112 |
| 113 bool ExtensionTestNotificationObserver::WaitForExtensionCrash( | 113 bool ExtensionTestNotificationObserver::WaitForExtensionCrash( |
| 114 const std::string& extension_id) { | 114 const std::string& extension_id) { |
| 115 if (!GetNonTerminatedExtensions(extension_id, context_)) { | 115 if (!GetNonTerminatedExtensions(extension_id, context_)) { |
| 116 // The extension is already unloaded, presumably due to a crash. | 116 // The extension is already unloaded, presumably due to a crash. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 144 | 144 |
| 145 registrar_.RemoveAll(); | 145 registrar_.RemoveAll(); |
| 146 observer_.reset(); | 146 observer_.reset(); |
| 147 } | 147 } |
| 148 | 148 |
| 149 void ExtensionTestNotificationObserver::Observe( | 149 void ExtensionTestNotificationObserver::Observe( |
| 150 int type, | 150 int type, |
| 151 const content::NotificationSource& source, | 151 const content::NotificationSource& source, |
| 152 const content::NotificationDetails& details) { | 152 const content::NotificationDetails& details) { |
| 153 switch (type) { | 153 switch (type) { |
| 154 case NOTIFICATION_EXTENSION_LOADED_DEPRECATED: | |
| 155 last_loaded_extension_id_ = | |
| 156 content::Details<const Extension>(details).ptr()->id(); | |
| 157 VLOG(1) << "Got EXTENSION_LOADED notification."; | |
| 158 break; | |
| 159 | |
| 160 case NOTIFICATION_CRX_INSTALLER_DONE: | 154 case NOTIFICATION_CRX_INSTALLER_DONE: |
| 161 VLOG(1) << "Got CRX_INSTALLER_DONE notification."; | 155 VLOG(1) << "Got CRX_INSTALLER_DONE notification."; |
| 162 { | 156 { |
| 163 const Extension* extension = | 157 const Extension* extension = |
| 164 content::Details<const Extension>(details).ptr(); | 158 content::Details<const Extension>(details).ptr(); |
| 165 if (extension) | 159 if (extension) |
| 166 last_loaded_extension_id_ = extension->id(); | 160 last_loaded_extension_id_ = extension->id(); |
| 167 else | 161 else |
| 168 last_loaded_extension_id_.clear(); | 162 last_loaded_extension_id_.clear(); |
| 169 } | 163 } |
| 170 ++crx_installers_done_observed_; | 164 ++crx_installers_done_observed_; |
| 171 break; | 165 break; |
| 172 | 166 |
| 173 case NOTIFICATION_EXTENSION_LOAD_ERROR: | 167 case NOTIFICATION_EXTENSION_LOAD_ERROR: |
| 174 VLOG(1) << "Got EXTENSION_LOAD_ERROR notification."; | 168 VLOG(1) << "Got EXTENSION_LOAD_ERROR notification."; |
| 175 ++extension_load_errors_observed_; | 169 ++extension_load_errors_observed_; |
| 176 break; | 170 break; |
| 177 | 171 |
| 178 default: | 172 default: |
| 179 NOTREACHED(); | 173 NOTREACHED(); |
| 180 break; | 174 break; |
| 181 } | 175 } |
| 182 } | 176 } |
| 183 | 177 |
| 178 void ExtensionTestNotificationObserver::OnExtensionLoaded( |
| 179 content::BrowserContext* browser_context, |
| 180 const Extension* extension) { |
| 181 last_loaded_extension_id_ = extension->id(); |
| 182 VLOG(1) << "Got EXTENSION_LOADED notification."; |
| 183 } |
| 184 |
| 185 void ExtensionTestNotificationObserver::OnShutdown( |
| 186 ExtensionRegistry* registry) { |
| 187 registry_observer_.RemoveAll(); |
| 188 } |
| 189 |
| 184 void ExtensionTestNotificationObserver::WaitForCondition( | 190 void ExtensionTestNotificationObserver::WaitForCondition( |
| 185 const ConditionCallback& condition, | 191 const ConditionCallback& condition, |
| 186 NotificationSet* notification_set) { | 192 NotificationSet* notification_set) { |
| 187 if (condition.Run()) | 193 if (condition.Run()) |
| 188 return; | 194 return; |
| 189 condition_ = condition; | 195 condition_ = condition; |
| 190 | 196 |
| 191 scoped_refptr<content::MessageLoopRunner> runner( | 197 scoped_refptr<content::MessageLoopRunner> runner( |
| 192 new content::MessageLoopRunner); | 198 new content::MessageLoopRunner); |
| 193 quit_closure_ = runner->QuitClosure(); | 199 quit_closure_ = runner->QuitClosure(); |
| 194 | 200 |
| 195 std::unique_ptr<base::CallbackList<void()>::Subscription> subscription; | 201 std::unique_ptr<base::CallbackList<void()>::Subscription> subscription; |
| 196 if (notification_set) { | 202 if (notification_set) { |
| 197 subscription = notification_set->callback_list().Add(base::Bind( | 203 subscription = notification_set->callback_list().Add(base::Bind( |
| 198 &ExtensionTestNotificationObserver::MaybeQuit, base::Unretained(this))); | 204 &ExtensionTestNotificationObserver::MaybeQuit, base::Unretained(this))); |
| 199 } | 205 } |
| 200 runner->Run(); | 206 runner->Run(); |
| 201 | 207 |
| 202 condition_.Reset(); | 208 condition_.Reset(); |
| 203 quit_closure_.Reset(); | 209 quit_closure_.Reset(); |
| 204 } | 210 } |
| 205 | 211 |
| 206 void ExtensionTestNotificationObserver::MaybeQuit() { | 212 void ExtensionTestNotificationObserver::MaybeQuit() { |
| 207 if (condition_.Run()) | 213 if (condition_.Run()) |
| 208 quit_closure_.Run(); | 214 quit_closure_.Run(); |
| 209 } | 215 } |
| 210 | 216 |
| 211 } // namespace extensions | 217 } // namespace extensions |
| OLD | NEW |