// Copyright (c) 2012 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/browser/extensions/api/declarative/rules_registry_service.h" #include "base/bind.h" #include "base/logging.h" #include "chrome/browser/extensions/api/declarative/initializing_rules_registry.h" #include "chrome/common/chrome_notification_types.h" #include "chrome/common/extensions/extension.h" #include "content/public/browser/notification_details.h" #include "content/public/browser/notification_source.h" namespace extensions { RulesRegistryService::RulesRegistryService(Profile* profile) { if (profile) { registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, content::Source(profile)); } } RulesRegistryService::~RulesRegistryService() {} void RulesRegistryService::RegisterRulesRegistry( const std::string& event_name, scoped_refptr rule_registry) { DCHECK(rule_registries_.find(event_name) == rule_registries_.end()); rule_registries_[event_name] = make_scoped_refptr(new InitializingRulesRegistry(rule_registry)); } scoped_refptr RulesRegistryService::GetRulesRegistry( const std::string& event_name) const { RulesRegistryMap::const_iterator i = rule_registries_.find(event_name); if (i == rule_registries_.end()) return scoped_refptr(); return i->second; } void RulesRegistryService::SimulateExtensionUnloaded( const std::string& extension_id) { OnExtensionUnloaded(extension_id); } void RulesRegistryService::OnExtensionUnloaded( const std::string& extension_id) { RulesRegistryMap::iterator i; for (i = rule_registries_.begin(); i != rule_registries_.end(); ++i) { scoped_refptr registry = i->second; if (content::BrowserThread::CurrentlyOn(registry->GetOwnerThread())) { registry->OnExtensionUnloaded(extension_id); } else { content::BrowserThread::PostTask( registry->GetOwnerThread(), FROM_HERE, base::Bind(&RulesRegistry::OnExtensionUnloaded, registry, extension_id)); } } } void RulesRegistryService::Observe( int type, const content::NotificationSource& source, const content::NotificationDetails& details) { switch (type) { case chrome::NOTIFICATION_EXTENSION_UNLOADED: { const Extension* extension = content::Details(details)->extension; OnExtensionUnloaded(extension->id()); break; } default: NOTREACHED(); break; } } } // namespace extensions