| 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" |
| 11 #include "content/public/browser/render_frame_host.h" | 11 #include "content/public/browser/render_frame_host.h" |
| 12 #include "content/public/test/test_utils.h" | 12 #include "content/public/test/test_utils.h" |
| 13 #include "extensions/browser/extension_registry.h" | 13 #include "extensions/browser/extension_registry.h" |
| 14 #include "extensions/browser/notification_types.h" | 14 #include "extensions/browser/notification_types.h" |
| 15 #include "extensions/browser/process_manager.h" | 15 #include "extensions/browser/process_manager.h" |
| 16 #include "extensions/common/extension.h" | 16 #include "extensions/common/extension.h" |
| 17 | 17 |
| 18 using extensions::Extension; | 18 namespace extensions { |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 // A callback that returns true if the condition has been met and takes no | 22 // A callback that returns true if the condition has been met and takes no |
| 23 // arguments. | 23 // arguments. |
| 24 using ConditionCallback = base::Callback<bool(void)>; | 24 using ConditionCallback = base::Callback<bool(void)>; |
| 25 | 25 |
| 26 const Extension* GetNonTerminatedExtensions(const std::string& id, | 26 const Extension* GetNonTerminatedExtensions(const std::string& id, |
| 27 content::BrowserContext* context) { | 27 content::BrowserContext* context) { |
| 28 return extensions::ExtensionRegistry::Get(context)->GetExtensionById( | 28 return ExtensionRegistry::Get(context)->GetExtensionById( |
| 29 id, extensions::ExtensionRegistry::EVERYTHING & | 29 id, ExtensionRegistry::EVERYTHING & ~ExtensionRegistry::TERMINATED); |
| 30 ~extensions::ExtensionRegistry::TERMINATED); | |
| 31 } | 30 } |
| 32 | 31 |
| 33 } // namespace | 32 } // namespace |
| 34 | 33 |
| 35 //////////////////////////////////////////////////////////////////////////////// | 34 //////////////////////////////////////////////////////////////////////////////// |
| 36 // ExtensionTestNotificationObserver::NotificationSet | 35 // ExtensionTestNotificationObserver::NotificationSet |
| 37 | 36 |
| 38 ExtensionTestNotificationObserver::NotificationSet::NotificationSet() | 37 ExtensionTestNotificationObserver::NotificationSet::NotificationSet() |
| 39 : process_manager_observer_(this) {} | 38 : process_manager_observer_(this) {} |
| 40 ExtensionTestNotificationObserver::NotificationSet::~NotificationSet() {} | 39 ExtensionTestNotificationObserver::NotificationSet::~NotificationSet() {} |
| 41 | 40 |
| 42 void ExtensionTestNotificationObserver::NotificationSet::Add( | 41 void ExtensionTestNotificationObserver::NotificationSet::Add( |
| 43 int type, | 42 int type, |
| 44 const content::NotificationSource& source) { | 43 const content::NotificationSource& source) { |
| 45 notification_registrar_.Add(this, type, source); | 44 notification_registrar_.Add(this, type, source); |
| 46 } | 45 } |
| 47 | 46 |
| 48 void ExtensionTestNotificationObserver::NotificationSet::Add(int type) { | 47 void ExtensionTestNotificationObserver::NotificationSet::Add(int type) { |
| 49 Add(type, content::NotificationService::AllSources()); | 48 Add(type, content::NotificationService::AllSources()); |
| 50 } | 49 } |
| 51 | 50 |
| 52 void ExtensionTestNotificationObserver::NotificationSet:: | 51 void ExtensionTestNotificationObserver::NotificationSet:: |
| 53 AddExtensionFrameUnregistration(extensions::ProcessManager* manager) { | 52 AddExtensionFrameUnregistration(ProcessManager* manager) { |
| 54 process_manager_observer_.Add(manager); | 53 process_manager_observer_.Add(manager); |
| 55 } | 54 } |
| 56 | 55 |
| 57 void ExtensionTestNotificationObserver::NotificationSet::Observe( | 56 void ExtensionTestNotificationObserver::NotificationSet::Observe( |
| 58 int type, | 57 int type, |
| 59 const content::NotificationSource& source, | 58 const content::NotificationSource& source, |
| 60 const content::NotificationDetails& details) { | 59 const content::NotificationDetails& details) { |
| 61 callback_list_.Notify(); | 60 callback_list_.Notify(); |
| 62 } | 61 } |
| 63 | 62 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 88 registrar.Add(this, notification_type, | 87 registrar.Add(this, notification_type, |
| 89 content::NotificationService::AllSources()); | 88 content::NotificationService::AllSources()); |
| 90 content::WindowedNotificationObserver( | 89 content::WindowedNotificationObserver( |
| 91 notification_type, content::NotificationService::AllSources()) | 90 notification_type, content::NotificationService::AllSources()) |
| 92 .Wait(); | 91 .Wait(); |
| 93 } | 92 } |
| 94 | 93 |
| 95 bool ExtensionTestNotificationObserver::WaitForExtensionInstallError() { | 94 bool ExtensionTestNotificationObserver::WaitForExtensionInstallError() { |
| 96 int before = extension_installs_observed_; | 95 int before = extension_installs_observed_; |
| 97 content::WindowedNotificationObserver( | 96 content::WindowedNotificationObserver( |
| 98 extensions::NOTIFICATION_EXTENSION_INSTALL_ERROR, | 97 NOTIFICATION_EXTENSION_INSTALL_ERROR, |
| 99 content::NotificationService::AllSources()) | 98 content::NotificationService::AllSources()) |
| 100 .Wait(); | 99 .Wait(); |
| 101 return extension_installs_observed_ == before; | 100 return extension_installs_observed_ == before; |
| 102 } | 101 } |
| 103 | 102 |
| 104 void ExtensionTestNotificationObserver::WaitForExtensionLoad() { | 103 void ExtensionTestNotificationObserver::WaitForExtensionLoad() { |
| 105 WaitForNotification(extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED); | 104 WaitForNotification(NOTIFICATION_EXTENSION_LOADED_DEPRECATED); |
| 106 } | 105 } |
| 107 | 106 |
| 108 bool ExtensionTestNotificationObserver::WaitForExtensionLoadError() { | 107 bool ExtensionTestNotificationObserver::WaitForExtensionLoadError() { |
| 109 int before = extension_load_errors_observed_; | 108 int before = extension_load_errors_observed_; |
| 110 WaitForNotification(extensions::NOTIFICATION_EXTENSION_LOAD_ERROR); | 109 WaitForNotification(NOTIFICATION_EXTENSION_LOAD_ERROR); |
| 111 return extension_load_errors_observed_ != before; | 110 return extension_load_errors_observed_ != before; |
| 112 } | 111 } |
| 113 | 112 |
| 114 bool ExtensionTestNotificationObserver::WaitForExtensionCrash( | 113 bool ExtensionTestNotificationObserver::WaitForExtensionCrash( |
| 115 const std::string& extension_id) { | 114 const std::string& extension_id) { |
| 116 if (!GetNonTerminatedExtensions(extension_id, context_)) { | 115 if (!GetNonTerminatedExtensions(extension_id, context_)) { |
| 117 // The extension is already unloaded, presumably due to a crash. | 116 // The extension is already unloaded, presumably due to a crash. |
| 118 return true; | 117 return true; |
| 119 } | 118 } |
| 120 | 119 |
| 121 content::WindowedNotificationObserver( | 120 content::WindowedNotificationObserver( |
| 122 extensions::NOTIFICATION_EXTENSION_PROCESS_TERMINATED, | 121 NOTIFICATION_EXTENSION_PROCESS_TERMINATED, |
| 123 content::NotificationService::AllSources()) | 122 content::NotificationService::AllSources()) |
| 124 .Wait(); | 123 .Wait(); |
| 125 return (GetNonTerminatedExtensions(extension_id, context_) == NULL); | 124 return (GetNonTerminatedExtensions(extension_id, context_) == NULL); |
| 126 } | 125 } |
| 127 | 126 |
| 128 bool ExtensionTestNotificationObserver::WaitForCrxInstallerDone() { | 127 bool ExtensionTestNotificationObserver::WaitForCrxInstallerDone() { |
| 129 int before = crx_installers_done_observed_; | 128 int before = crx_installers_done_observed_; |
| 130 WaitForNotification(extensions::NOTIFICATION_CRX_INSTALLER_DONE); | 129 WaitForNotification(NOTIFICATION_CRX_INSTALLER_DONE); |
| 131 return crx_installers_done_observed_ == before + 1; | 130 return crx_installers_done_observed_ == before + 1; |
| 132 } | 131 } |
| 133 | 132 |
| 134 void ExtensionTestNotificationObserver::Watch( | 133 void ExtensionTestNotificationObserver::Watch( |
| 135 int type, | 134 int type, |
| 136 const content::NotificationSource& source) { | 135 const content::NotificationSource& source) { |
| 137 CHECK(!observer_); | 136 CHECK(!observer_); |
| 138 observer_.reset(new content::WindowedNotificationObserver(type, source)); | 137 observer_.reset(new content::WindowedNotificationObserver(type, source)); |
| 139 registrar_.Add(this, type, source); | 138 registrar_.Add(this, type, source); |
| 140 } | 139 } |
| 141 | 140 |
| 142 void ExtensionTestNotificationObserver::Wait() { | 141 void ExtensionTestNotificationObserver::Wait() { |
| 143 observer_->Wait(); | 142 observer_->Wait(); |
| 144 | 143 |
| 145 registrar_.RemoveAll(); | 144 registrar_.RemoveAll(); |
| 146 observer_.reset(); | 145 observer_.reset(); |
| 147 } | 146 } |
| 148 | 147 |
| 149 void ExtensionTestNotificationObserver::Observe( | 148 void ExtensionTestNotificationObserver::Observe( |
| 150 int type, | 149 int type, |
| 151 const content::NotificationSource& source, | 150 const content::NotificationSource& source, |
| 152 const content::NotificationDetails& details) { | 151 const content::NotificationDetails& details) { |
| 153 switch (type) { | 152 switch (type) { |
| 154 case extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED: | 153 case NOTIFICATION_EXTENSION_LOADED_DEPRECATED: |
| 155 last_loaded_extension_id_ = | 154 last_loaded_extension_id_ = |
| 156 content::Details<const Extension>(details).ptr()->id(); | 155 content::Details<const Extension>(details).ptr()->id(); |
| 157 VLOG(1) << "Got EXTENSION_LOADED notification."; | 156 VLOG(1) << "Got EXTENSION_LOADED notification."; |
| 158 break; | 157 break; |
| 159 | 158 |
| 160 case extensions::NOTIFICATION_CRX_INSTALLER_DONE: | 159 case NOTIFICATION_CRX_INSTALLER_DONE: |
| 161 VLOG(1) << "Got CRX_INSTALLER_DONE notification."; | 160 VLOG(1) << "Got CRX_INSTALLER_DONE notification."; |
| 162 { | 161 { |
| 163 const Extension* extension = | 162 const Extension* extension = |
| 164 content::Details<const Extension>(details).ptr(); | 163 content::Details<const Extension>(details).ptr(); |
| 165 if (extension) | 164 if (extension) |
| 166 last_loaded_extension_id_ = extension->id(); | 165 last_loaded_extension_id_ = extension->id(); |
| 167 else | 166 else |
| 168 last_loaded_extension_id_.clear(); | 167 last_loaded_extension_id_.clear(); |
| 169 } | 168 } |
| 170 ++crx_installers_done_observed_; | 169 ++crx_installers_done_observed_; |
| 171 break; | 170 break; |
| 172 | 171 |
| 173 case extensions::NOTIFICATION_EXTENSION_LOAD_ERROR: | 172 case NOTIFICATION_EXTENSION_LOAD_ERROR: |
| 174 VLOG(1) << "Got EXTENSION_LOAD_ERROR notification."; | 173 VLOG(1) << "Got EXTENSION_LOAD_ERROR notification."; |
| 175 ++extension_load_errors_observed_; | 174 ++extension_load_errors_observed_; |
| 176 break; | 175 break; |
| 177 | 176 |
| 178 default: | 177 default: |
| 179 NOTREACHED(); | 178 NOTREACHED(); |
| 180 break; | 179 break; |
| 181 } | 180 } |
| 182 } | 181 } |
| 183 | 182 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 200 runner->Run(); | 199 runner->Run(); |
| 201 | 200 |
| 202 condition_.Reset(); | 201 condition_.Reset(); |
| 203 quit_closure_.Reset(); | 202 quit_closure_.Reset(); |
| 204 } | 203 } |
| 205 | 204 |
| 206 void ExtensionTestNotificationObserver::MaybeQuit() { | 205 void ExtensionTestNotificationObserver::MaybeQuit() { |
| 207 if (condition_.Run()) | 206 if (condition_.Run()) |
| 208 quit_closure_.Run(); | 207 quit_closure_.Run(); |
| 209 } | 208 } |
| 209 |
| 210 } // namespace extensions |
| OLD | NEW |