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