1 // Copyright 2014 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/browser/test_extension_registry_observer.h" 6 7 #include "content/public/test/test_utils.h" 8 #include "extensions/browser/extension_registry.h" 9 10 namespace extensions { 11 12 class TestExtensionRegistryObserver::Waiter { 13 public: 14 explicit Waiter(const std::string& extension_id) 15 : observed_(false), runner_(NULL) {} 16 17 void Wait() { 18 if (observed_) 19 return; 20 21 runner_ = new content::MessageLoopRunner(); 22 runner_->Run(); 23 } 24 25 void OnObserved() { 26 observed_ = true; 27 28 if (runner_.get()) { 29 runner_->Quit(); 30 runner_ = NULL; 31 } 32 } 33 34 private: 35 bool observed_; 36 scoped_refptr<content::MessageLoopRunner> runner_; 37 38 DISALLOW_COPY_AND_ASSIGN(Waiter); 39 }; 40 41 TestExtensionRegistryObserver::TestExtensionRegistryObserver( 42 ExtensionRegistry* registry, 43 const std::string& extension_id) 44 : will_be_installed_waiter_(new Waiter(extension_id)), 45 uninstalled_waiter_(new Waiter(extension_id)), 46 loaded_waiter_(new Waiter(extension_id)), 47 unloaded_waiter_(new Waiter(extension_id)), 48 extension_registry_observer_(this), 49 extension_id_(extension_id) { 50 extension_registry_observer_.Add(registry); 51 } 52 53 TestExtensionRegistryObserver::~TestExtensionRegistryObserver() { 54 } 55 56 void TestExtensionRegistryObserver::WaitForExtensionUninstalled() { 57 uninstalled_waiter_->Wait(); 58 } 59 60 void TestExtensionRegistryObserver::WaitForExtensionWillBeInstalled() { 61 will_be_installed_waiter_->Wait(); 62 } 63 64 void TestExtensionRegistryObserver::WaitForExtensionLoaded() { 65 loaded_waiter_->Wait(); 66 } 67 68 void TestExtensionRegistryObserver::WaitForExtensionUnloaded() { 69 unloaded_waiter_->Wait(); 70 } 71 72 void TestExtensionRegistryObserver::OnExtensionWillBeInstalled( 73 content::BrowserContext* browser_context, 74 const Extension* extension, 75 bool is_update, 76 bool from_ephemeral, 77 const std::string& old_name) { 78 if (extension->id() == extension_id_) 79 will_be_installed_waiter_->OnObserved(); 80 } 81 82 void TestExtensionRegistryObserver::OnExtensionUninstalled( 83 content::BrowserContext* browser_context, 84 const Extension* extension, 85 extensions::UninstallReason reason) { 86 if (extension->id() == extension_id_) 87 uninstalled_waiter_->OnObserved(); 88 } 89 90 void TestExtensionRegistryObserver::OnExtensionLoaded( 91 content::BrowserContext* browser_context, 92 const Extension* extension) { 93 if (extension->id() == extension_id_) 94 loaded_waiter_->OnObserved(); 95 } 96 97 void TestExtensionRegistryObserver::OnExtensionUnloaded( 98 content::BrowserContext* browser_context, 99 const Extension* extension, 100 UnloadedExtensionInfo::Reason reason) { 101 if (extension->id() == extension_id_) 102 unloaded_waiter_->OnObserved(); 103 } 104 105 } // namespace extensions 106