1116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Copyright 2014 The Chromium Authors. All rights reserved.
2116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Use of this source code is governed by a BSD-style license that can be
3116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// found in the LICENSE file.
4116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
5116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "extensions/browser/test_extension_registry_observer.h"
6116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
7116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "content/public/test/test_utils.h"
8116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "extensions/browser/extension_registry.h"
9116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
10116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace extensions {
11116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
12116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass TestExtensionRegistryObserver::Waiter {
13116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch public:
141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  explicit Waiter(const std::string& extension_id)
151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      : observed_(false), runner_(NULL) {}
16116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
17116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void Wait() {
18116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    if (observed_)
19116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      return;
20116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
21116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    runner_ = new content::MessageLoopRunner();
22116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    runner_->Run();
23116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
24116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
25116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void OnObserved() {
26116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    observed_ = true;
27116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    if (runner_.get()) {
29116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      runner_->Quit();
30116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      runner_ = NULL;
31116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    }
32116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
33116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
34116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch private:
35116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  bool observed_;
36116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  scoped_refptr<content::MessageLoopRunner> runner_;
37116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
38116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  DISALLOW_COPY_AND_ASSIGN(Waiter);
39116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch};
40116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
41116680a4aac90f2aa7413d9095a592090648e557Ben MurdochTestExtensionRegistryObserver::TestExtensionRegistryObserver(
42116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    ExtensionRegistry* registry,
43116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const std::string& extension_id)
44116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    : will_be_installed_waiter_(new Waiter(extension_id)),
45116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      uninstalled_waiter_(new Waiter(extension_id)),
46116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      loaded_waiter_(new Waiter(extension_id)),
47116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      unloaded_waiter_(new Waiter(extension_id)),
48116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      extension_registry_observer_(this),
49116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      extension_id_(extension_id) {
50116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  extension_registry_observer_.Add(registry);
51116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
52116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
53116680a4aac90f2aa7413d9095a592090648e557Ben MurdochTestExtensionRegistryObserver::~TestExtensionRegistryObserver() {
54116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
55116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
56116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid TestExtensionRegistryObserver::WaitForExtensionUninstalled() {
57116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  uninstalled_waiter_->Wait();
58116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
59116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
60116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid TestExtensionRegistryObserver::WaitForExtensionWillBeInstalled() {
61116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  will_be_installed_waiter_->Wait();
62116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
63116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
64116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid TestExtensionRegistryObserver::WaitForExtensionLoaded() {
65116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  loaded_waiter_->Wait();
66116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
67116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
68116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid TestExtensionRegistryObserver::WaitForExtensionUnloaded() {
69116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  unloaded_waiter_->Wait();
70116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
71116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
72116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid TestExtensionRegistryObserver::OnExtensionWillBeInstalled(
73116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    content::BrowserContext* browser_context,
74116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const Extension* extension,
75116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    bool is_update,
76116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    bool from_ephemeral,
77116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const std::string& old_name) {
78116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (extension->id() == extension_id_)
79116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    will_be_installed_waiter_->OnObserved();
80116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
81116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
82116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid TestExtensionRegistryObserver::OnExtensionUninstalled(
83116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    content::BrowserContext* browser_context,
845f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    const Extension* extension,
855f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    extensions::UninstallReason reason) {
86116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (extension->id() == extension_id_)
87116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    uninstalled_waiter_->OnObserved();
88116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
89116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
90116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid TestExtensionRegistryObserver::OnExtensionLoaded(
91116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    content::BrowserContext* browser_context,
92116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const Extension* extension) {
93116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (extension->id() == extension_id_)
94116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    loaded_waiter_->OnObserved();
95116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
96116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
97116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid TestExtensionRegistryObserver::OnExtensionUnloaded(
98116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    content::BrowserContext* browser_context,
99116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const Extension* extension,
100116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    UnloadedExtensionInfo::Reason reason) {
101116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (extension->id() == extension_id_)
102116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    unloaded_waiter_->OnObserved();
103116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
104116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
105116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}  // namespace extensions
106