Chromium Code Reviews| Index: chrome/browser/extensions/chrome_extension_test_notification_observer.cc |
| diff --git a/chrome/browser/extensions/extension_test_notification_observer.cc b/chrome/browser/extensions/chrome_extension_test_notification_observer.cc |
| similarity index 21% |
| copy from chrome/browser/extensions/extension_test_notification_observer.cc |
| copy to chrome/browser/extensions/chrome_extension_test_notification_observer.cc |
| index a9f3d8f2084d5afddb962c7af15634102ca6f833..cf5f3d0d6f945b24a6765c6f7365c0243b537ff5 100644 |
| --- a/chrome/browser/extensions/extension_test_notification_observer.cc |
| +++ b/chrome/browser/extensions/chrome_extension_test_notification_observer.cc |
| @@ -1,27 +1,17 @@ |
| -// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Copyright 2016 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 <stddef.h> |
| - |
| -#include "base/callback_list.h" |
| +#include <chrome/browser/extensions/chrome_extension_test_notification_observer.h> |
| #include "base/scoped_observer.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 "content/public/browser/browser_context.h" |
| +#include "extensions/browser/notification_types.h" |
| #include "extensions/browser/process_manager.h" |
| -#include "extensions/browser/process_manager_observer.h" |
| #include "extensions/common/extension.h" |
| using extensions::Extension; |
| @@ -30,13 +20,14 @@ namespace { |
| // A callback that returns true if the condition has been met and takes no |
| // arguments. |
| -typedef base::Callback<bool(void)> ConditionCallback; |
| +using ConditionCallback = base::Callback<bool(void)>; |
| bool HasPageActionVisibilityReachedTarget( |
| - Browser* browser, size_t target_visible_page_action_count) { |
| + 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; |
| + browser->tab_strip_model()->GetActiveWebContents()) == |
| + target_visible_page_action_count; |
| } |
| bool HaveAllExtensionRenderFrameHostsFinishedLoading( |
| @@ -49,128 +40,50 @@ bool HaveAllExtensionRenderFrameHostsFinishedLoading( |
| 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 extensions::ProcessManagerObserver { |
| - public: |
| - NotificationSet() : process_manager_observer_(this) {} |
| - ~NotificationSet() override {} |
| - |
| - void Add(int type, const content::NotificationSource& source); |
| - void Add(int type); |
| - void AddExtensionFrameUnregistration(extensions::ProcessManager* manager); |
| - |
| - // 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; |
| - |
| - // extensions::ProcessManagerObserver: |
| - void OnExtensionFrameUnregistered( |
| - const std::string& extension_id, |
| - content::RenderFrameHost* render_frame_host) override; |
| - |
| - content::NotificationRegistrar notification_registrar_; |
| - base::CallbackList<void()> callback_list_; |
| - ScopedObserver<extensions::ProcessManager, extensions::ProcessManagerObserver> |
| - process_manager_observer_; |
| -}; |
| - |
| -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:: |
| - AddExtensionFrameUnregistration(extensions::ProcessManager* manager) { |
| - process_manager_observer_.Add(manager); |
| -} |
| - |
| -void ExtensionTestNotificationObserver::NotificationSet::Observe( |
| - int type, |
| - const content::NotificationSource& source, |
| - const content::NotificationDetails& details) { |
| - callback_list_.Notify(); |
| -} |
| - |
| -void ExtensionTestNotificationObserver::NotificationSet:: |
| - OnExtensionFrameUnregistered(const std::string& extension_id, |
| - content::RenderFrameHost* render_frame_host) { |
| - 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_) { |
| +ChromeExtensionTestNotificationObserver:: |
| + ChromeExtensionTestNotificationObserver(Browser* browser) |
| + : ExtensionTestNotificationObserver( |
| + [browser]() -> content::BrowserContext* { |
|
Devlin
2016/10/11 22:53:45
I can't find anywhere we create one of these that
Rahul Chaturvedi
2016/10/12 20:25:53
Done.
|
| + if (browser) |
| + return browser->profile(); |
| + else |
| + return ProfileManager::GetActiveUserProfile(); |
| + }()), |
| + browser_(browser) {} |
| + |
| +ChromeExtensionTestNotificationObserver:: |
| + ~ChromeExtensionTestNotificationObserver() {} |
| + |
| +content::BrowserContext* |
| +ChromeExtensionTestNotificationObserver::GetBrowserContext() { |
|
Devlin
2016/10/11 22:53:45
no longer needed, right? (Should always be contex
Rahul Chaturvedi
2016/10/12 20:25:53
Done.
|
| + if (!context_) { |
| if (browser_) |
| - profile_ = browser_->profile(); |
| + context_ = browser_->profile(); |
| else |
| - profile_ = ProfileManager::GetActiveUserProfile(); |
| + context_ = ProfileManager::GetActiveUserProfile(); |
| } |
| - return profile_; |
| + return context_; |
| } |
| -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); |
| +bool ChromeExtensionTestNotificationObserver:: |
| + WaitForPageActionVisibilityChangeTo(int count) { |
| + ScopedObserver<extensions::ExtensionActionAPI, |
| + extensions::ExtensionActionAPI::Observer> |
| + observer(this); |
| + observer.Add(extensions::ExtensionActionAPI::Get(GetBrowserContext())); |
| WaitForCondition( |
| - base::Bind(&HasPageActionVisibilityReachedTarget, browser_, count), |
| - NULL); |
| - extensions::ExtensionActionAPI::Get(GetProfile())-> |
| - RemoveObserver(this); |
| + base::Bind(&HasPageActionVisibilityReachedTarget, browser_, count), NULL); |
| return true; |
| } |
| -bool ExtensionTestNotificationObserver::WaitForExtensionViewsToLoad() { |
| +bool ChromeExtensionTestNotificationObserver::WaitForExtensionViewsToLoad() { |
| extensions::ProcessManager* manager = |
| - extensions::ProcessManager::Get(GetProfile()); |
| + extensions::ProcessManager::Get(GetBrowserContext()); |
| NotificationSet notification_set; |
| notification_set.Add(content::NOTIFICATION_WEB_CONTENTS_DESTROYED); |
| notification_set.Add(content::NOTIFICATION_LOAD_STOP); |
| @@ -181,148 +94,37 @@ bool ExtensionTestNotificationObserver::WaitForExtensionViewsToLoad() { |
| return true; |
| } |
| -bool ExtensionTestNotificationObserver::WaitForExtensionIdle( |
| +bool ChromeExtensionTestNotificationObserver::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()), |
| + GetBrowserContext()), |
| ¬ification_set); |
| return true; |
| } |
| -bool ExtensionTestNotificationObserver::WaitForExtensionNotIdle( |
| +bool ChromeExtensionTestNotificationObserver::WaitForExtensionNotIdle( |
| const std::string& extension_id) { |
| NotificationSet notification_set; |
| notification_set.Add(content::NOTIFICATION_LOAD_STOP); |
| - WaitForCondition(base::Bind(&IsExtensionNotIdle, extension_id, GetProfile()), |
| + WaitForCondition(base::Bind( |
| + [](const std::string& extension_id, |
| + content::BrowserContext* context) -> bool { |
| + return !extensions::util::IsExtensionIdle(extension_id, |
| + context); |
| + }, |
| + extension_id, GetBrowserContext()), |
| ¬ification_set); |
| return true; |
| } |
| -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(); |
| +void ChromeExtensionTestNotificationObserver::WaitForExtensionAndViewLoad() { |
| + 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_LOAD_ERROR: |
| - VLOG(1) << "Got EXTENSION_LOAD_ERROR notification."; |
| - ++extension_load_errors_observed_; |
| - break; |
| - |
| - default: |
| - NOTREACHED(); |
| - break; |
| - } |
| -} |
| - |
| -void ExtensionTestNotificationObserver::OnPageActionsUpdated( |
| +void ChromeExtensionTestNotificationObserver::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(); |
| - |
| - std::unique_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(); |
| -} |