summaryrefslogtreecommitdiffstats
path: root/chrome/test/base/extension_load_waiter_one_shot.cc
blob: ae052c691f565714a98c89b210efc0982f5b7a21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright 2014 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/test/base/extension_load_waiter_one_shot.h"

#include "base/callback.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/common/extensions/extension_constants.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/notification_service.h"
#include "extensions/browser/extension_host.h"

ExtensionLoadWaiterOneShot::ExtensionLoadWaiterOneShot() : extension_id_(NULL),
                                             browser_context_(NULL) {
}

ExtensionLoadWaiterOneShot::~ExtensionLoadWaiterOneShot() {
}

void ExtensionLoadWaiterOneShot::WaitForExtension(const char* extension_id,
                                  const base::Closure& load_cb) {
  CHECK(!extension_id_) <<
      "ExtensionLoadWaiterOneShot should only be used once.";
  extension_id_ = extension_id;
  load_looper_ = new content::MessageLoopRunner();
  registrar_.Add(this,
                 extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_FIRST_LOAD,
                 content::NotificationService::AllSources());
  load_cb.Run();
  load_looper_->Run();
}

void ExtensionLoadWaiterOneShot::Observe(int type,
                                  const content::NotificationSource& source,
                                  const content::NotificationDetails& details) {
  switch (type) {
    case extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_FIRST_LOAD: {
      extensions::ExtensionHost* host =
          content::Details<extensions::ExtensionHost>(details).ptr();
      if (host->extension_id() == extension_id_) {
        browser_context_ = host->browser_context();
        CHECK(browser_context_);
        registrar_.Remove(
            this, extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_FIRST_LOAD,
            content::NotificationService::AllSources());
        load_looper_->Quit();
      }
      break;
    }
  }
}