Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / test / base / extension_load_waiter_one_shot.cc
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 "chrome/test/base/extension_load_waiter_one_shot.h"
6
7 #include "base/callback.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/common/extensions/extension_constants.h"
10 #include "content/public/browser/browser_context.h"
11 #include "content/public/browser/notification_service.h"
12 #include "extensions/browser/extension_host.h"
13
14 ExtensionLoadWaiterOneShot::ExtensionLoadWaiterOneShot() : extension_id_(NULL),
15                                              browser_context_(NULL) {
16 }
17
18 ExtensionLoadWaiterOneShot::~ExtensionLoadWaiterOneShot() {
19 }
20
21 void ExtensionLoadWaiterOneShot::WaitForExtension(const char* extension_id,
22                                   const base::Closure& load_cb) {
23   CHECK(!extension_id_) <<
24       "ExtensionLoadWaiterOneShot should only be used once.";
25   extension_id_ = extension_id;
26   load_looper_ = new content::MessageLoopRunner();
27   registrar_.Add(this,
28                  extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING,
29                  content::NotificationService::AllSources());
30   load_cb.Run();
31   load_looper_->Run();
32 }
33
34 void ExtensionLoadWaiterOneShot::Observe(int type,
35                                   const content::NotificationSource& source,
36                                   const content::NotificationDetails& details) {
37   switch (type) {
38     case extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING: {
39       extensions::ExtensionHost* host =
40           content::Details<extensions::ExtensionHost>(details).ptr();
41       if (host->extension_id() == extension_id_) {
42         browser_context_ = host->browser_context();
43         CHECK(browser_context_);
44         registrar_.Remove(
45             this,
46             extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING,
47             content::NotificationService::AllSources());
48         load_looper_->Quit();
49       }
50       break;
51     }
52   }
53 }