| 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 26 matching lines...) Expand all Loading... |
| 143 | 143 |
| 144 registrar_.RemoveAll(); | 144 registrar_.RemoveAll(); |
| 145 observer_.reset(); | 145 observer_.reset(); |
| 146 } | 146 } |
| 147 | 147 |
| 148 void ExtensionTestNotificationObserver::Observe( | 148 void ExtensionTestNotificationObserver::Observe( |
| 149 int type, | 149 int type, |
| 150 const content::NotificationSource& source, | 150 const content::NotificationSource& source, |
| 151 const content::NotificationDetails& details) { | 151 const content::NotificationDetails& details) { |
| 152 switch (type) { | 152 switch (type) { |
| 153 case NOTIFICATION_EXTENSION_LOADED_DEPRECATED: | |
| 154 last_loaded_extension_id_ = | |
| 155 content::Details<const Extension>(details).ptr()->id(); | |
| 156 VLOG(1) << "Got EXTENSION_LOADED notification."; | |
| 157 break; | |
| 158 | |
| 159 case NOTIFICATION_CRX_INSTALLER_DONE: | 153 case NOTIFICATION_CRX_INSTALLER_DONE: |
| 160 VLOG(1) << "Got CRX_INSTALLER_DONE notification."; | 154 VLOG(1) << "Got CRX_INSTALLER_DONE notification."; |
| 161 { | 155 { |
| 162 const Extension* extension = | 156 const Extension* extension = |
| 163 content::Details<const Extension>(details).ptr(); | 157 content::Details<const Extension>(details).ptr(); |
| 164 if (extension) | 158 if (extension) |
| 165 last_loaded_extension_id_ = extension->id(); | 159 last_loaded_extension_id_ = extension->id(); |
| 166 else | 160 else |
| 167 last_loaded_extension_id_.clear(); | 161 last_loaded_extension_id_.clear(); |
| 168 } | 162 } |
| 169 ++crx_installers_done_observed_; | 163 ++crx_installers_done_observed_; |
| 170 break; | 164 break; |
| 171 | 165 |
| 172 case NOTIFICATION_EXTENSION_LOAD_ERROR: | 166 case NOTIFICATION_EXTENSION_LOAD_ERROR: |
| 173 VLOG(1) << "Got EXTENSION_LOAD_ERROR notification."; | 167 VLOG(1) << "Got EXTENSION_LOAD_ERROR notification."; |
| 174 ++extension_load_errors_observed_; | 168 ++extension_load_errors_observed_; |
| 175 break; | 169 break; |
| 176 | 170 |
| 177 default: | 171 default: |
| 178 NOTREACHED(); | 172 NOTREACHED(); |
| 179 break; | 173 break; |
| 180 } | 174 } |
| 181 } | 175 } |
| 182 | 176 |
| 177 void ExtensionTestNotificationObserver::OnExtensionLoaded( |
| 178 content::BrowserContext* browser_context, |
| 179 const Extension* extension) { |
| 180 last_loaded_extension_id_ = extension->id(); |
| 181 VLOG(1) << "Got EXTENSION_LOADED notification."; |
| 182 } |
| 183 |
| 184 void ExtensionTestNotificationObserver::OnShutdown( |
| 185 ExtensionRegistry* registry) { |
| 186 registry_observer_.RemoveAll(); |
| 187 } |
| 188 |
| 183 void ExtensionTestNotificationObserver::WaitForCondition( | 189 void ExtensionTestNotificationObserver::WaitForCondition( |
| 184 const ConditionCallback& condition, | 190 const ConditionCallback& condition, |
| 185 NotificationSet* notification_set) { | 191 NotificationSet* notification_set) { |
| 186 if (condition.Run()) | 192 if (condition.Run()) |
| 187 return; | 193 return; |
| 188 condition_ = condition; | 194 condition_ = condition; |
| 189 | 195 |
| 190 scoped_refptr<content::MessageLoopRunner> runner( | 196 scoped_refptr<content::MessageLoopRunner> runner( |
| 191 new content::MessageLoopRunner); | 197 new content::MessageLoopRunner); |
| 192 quit_closure_ = runner->QuitClosure(); | 198 quit_closure_ = runner->QuitClosure(); |
| 193 | 199 |
| 194 std::unique_ptr<base::CallbackList<void()>::Subscription> subscription; | 200 std::unique_ptr<base::CallbackList<void()>::Subscription> subscription; |
| 195 if (notification_set) { | 201 if (notification_set) { |
| 196 subscription = notification_set->callback_list().Add(base::Bind( | 202 subscription = notification_set->callback_list().Add(base::Bind( |
| 197 &ExtensionTestNotificationObserver::MaybeQuit, base::Unretained(this))); | 203 &ExtensionTestNotificationObserver::MaybeQuit, base::Unretained(this))); |
| 198 } | 204 } |
| 199 runner->Run(); | 205 runner->Run(); |
| 200 | 206 |
| 201 condition_.Reset(); | 207 condition_.Reset(); |
| 202 quit_closure_.Reset(); | 208 quit_closure_.Reset(); |
| 203 } | 209 } |
| 204 | 210 |
| 205 void ExtensionTestNotificationObserver::MaybeQuit() { | 211 void ExtensionTestNotificationObserver::MaybeQuit() { |
| 206 if (condition_.Run()) | 212 if (condition_.Run()) |
| 207 quit_closure_.Run(); | 213 quit_closure_.Run(); |
| 208 } | 214 } |
| 209 | 215 |
| 210 } // namespace extensions | 216 } // namespace extensions |
| OLD | NEW |