summaryrefslogtreecommitdiffstats
path: root/chrome/test/base/extension_load_waiter_one_shot.h
blob: 15cae651a97b8fad4600d65d82647af8887f59d0 (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
// 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.

#ifndef CHROME_TEST_BASE_EXTENSION_LOAD_WAITER_ONE_SHOT_H_
#define CHROME_TEST_BASE_EXTENSION_LOAD_WAITER_ONE_SHOT_H_

#include "base/callback_forward.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "content/public/test/test_utils.h"

namespace content {
class BrowserContext;
}  // namespace content

// A class used to wait for an extension to load. Callers provide a load
// callback and can block until the extension loads.
class ExtensionLoadWaiterOneShot : public content::NotificationObserver {
 public:
  ExtensionLoadWaiterOneShot();
  ~ExtensionLoadWaiterOneShot() override;

  // Waits for extension with |extension_id| to load. The id should be a pointer
  // to a static char array.
  void WaitForExtension(const char* extension_id, const base::Closure& load_cb);

  // content::NotificationObserver overrides.
  void Observe(int type,
               const content::NotificationSource& source,
               const content::NotificationDetails& details) override;

  // Get the browser context associated with the loaded extension. Returns
  // NULL if |WaitForExtension| was not previously called.
  content::BrowserContext* browser_context() { return browser_context_; }

  // Get the id of the loaded extension.
  const char* extension_id() { return extension_id_; }

 private:
  content::NotificationRegistrar registrar_;
  scoped_refptr<content::MessageLoopRunner> load_looper_;
  const char* extension_id_;
  content::BrowserContext* browser_context_;

  DISALLOW_COPY_AND_ASSIGN(ExtensionLoadWaiterOneShot);
};

#endif  // CHROME_TEST_BASE_EXTENSION_LOAD_WAITER_ONE_SHOT_H_