// Copyright (c) 2009 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/extension_toolbar_model.h" #include "chrome/browser/extensions/extensions_service.h" #include "chrome/common/extensions/extension.h" #include "chrome/common/notification_service.h" ExtensionToolbarModel::ExtensionToolbarModel(ExtensionsService* service) : service_(service) { DCHECK(service_); registrar_.Add(this, NotificationType::EXTENSION_LOADED, Source(service_->profile())); registrar_.Add(this, NotificationType::EXTENSION_UNLOADED, Source(service_->profile())); registrar_.Add(this, NotificationType::EXTENSION_UNLOADED_DISABLED, Source(service_->profile())); registrar_.Add(this, NotificationType::EXTENSIONS_READY, Source(service_->profile())); } ExtensionToolbarModel::~ExtensionToolbarModel() { } void ExtensionToolbarModel::AddObserver(Observer* observer) { observers_.AddObserver(observer); } void ExtensionToolbarModel::RemoveObserver(Observer* observer) { observers_.RemoveObserver(observer); } void ExtensionToolbarModel::MoveBrowserAction(Extension* extension, int index) { ExtensionList::iterator pos = std::find(begin(), end(), extension); if (pos == end()) { NOTREACHED(); return; } toolitems_.erase(pos); int i = 0; bool inserted = false; for (ExtensionList::iterator iter = begin(); iter != end(); ++iter, ++i) { if (i == index) { toolitems_.insert(pos, extension); inserted = true; break; } } if (!inserted) { DCHECK_EQ(index, static_cast(toolitems_.size())); index = toolitems_.size(); toolitems_.push_back(extension); } FOR_EACH_OBSERVER(Observer, observers_, BrowserActionMoved(extension, index)); } void ExtensionToolbarModel::Observe(NotificationType type, const NotificationSource& source, const NotificationDetails& details) { if (type == NotificationType::EXTENSIONS_READY) { for (size_t i = 0; i < service_->extensions()->size(); ++i) { Extension* extension = service_->GetExtensionById( service_->extensions()->at(i)->id(), false); AddExtension(extension); } return; } if (!service_->is_ready()) return; Extension* extension = Details(details).ptr(); if (type == NotificationType::EXTENSION_LOADED) { AddExtension(extension); } else if (type == NotificationType::EXTENSION_UNLOADED || type == NotificationType::EXTENSION_UNLOADED_DISABLED) { RemoveExtension(extension); } else { NOTREACHED() << "Received unexpected notification"; } } void ExtensionToolbarModel::AddExtension(Extension* extension) { // We only care about extensions with browser actions. if (!extension->browser_action()) return; toolitems_.push_back(extension); FOR_EACH_OBSERVER(Observer, observers_, BrowserActionAdded(extension, toolitems_.size() - 1)); } void ExtensionToolbarModel::RemoveExtension(Extension* extension) { ExtensionList::iterator pos = std::find(begin(), end(), extension); if (pos != end()) { toolitems_.erase(pos); FOR_EACH_OBSERVER(Observer, observers_, BrowserActionRemoved(extension)); } }