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 chrome::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 chrome::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(this, 45 chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING, 46 content::NotificationService::AllSources()); 47 load_looper_->Quit(); 48 } 49 break; 50 } 51 } 52 } 53