| Index: chrome/browser/extensions/extension_suggested_links_manager.cc
|
| diff --git a/chrome/browser/extensions/extension_suggested_links_manager.cc b/chrome/browser/extensions/extension_suggested_links_manager.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..9eef9ca3970b4467c9e26f63ed1f41d6d50480d8
|
| --- /dev/null
|
| +++ b/chrome/browser/extensions/extension_suggested_links_manager.cc
|
| @@ -0,0 +1,84 @@
|
| +// 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/extension_suggested_links_manager.h"
|
| +
|
| +#include <algorithm>
|
| +
|
| +using content::BrowserThread;
|
| +
|
| +namespace {
|
| +
|
| +// A functor that mathes an ExtensionSuggestedLink having a given URL. Needs an
|
| +// implicit copy constructor to be used with std::find.
|
| +class ExtensionSuggestedLinkMatchUrlPred {
|
| + public:
|
| + explicit ExtensionSuggestedLinkMatchUrlPred(const std::string* link_url) :
|
| + link_url_(link_url) {}
|
| +
|
| + bool operator() (linked_ptr<ExtensionSuggestedLink> suggested_link) const {
|
| + return suggested_link->link_url().compare(*link_url_) == 0;
|
| + }
|
| +
|
| + private:
|
| + const std::string* link_url_;
|
| +};
|
| +
|
| +void RemoveLinkFromList(const std::string& link_url,
|
| + ExtensionSuggestedLinkList* list) {
|
| + ExtensionSuggestedLinkMatchUrlPred pred(&link_url);
|
| + ExtensionSuggestedLinkList::iterator found = std::find_if(list->begin(),
|
| + list->end(), pred);
|
| + if (found != list->end())
|
| + list->erase(found);
|
| +}
|
| +
|
| +}
|
| +
|
| +ExtensionSuggestedLinksManager::ExtensionSuggestedLinksManager() {}
|
| +
|
| +ExtensionSuggestedLinksManager::~ExtensionSuggestedLinksManager() {
|
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
|
| +}
|
| +
|
| +void ExtensionSuggestedLinksManager::Add(const std::string& extension_id,
|
| + ExtensionSuggestedLink* item) {
|
| + // Do this first since we own the incoming item and hence want to delete
|
| + // it in error paths.
|
| + linked_ptr<ExtensionSuggestedLink> linked_item(item);
|
| +
|
| + ExtensionSuggestedLinkList& list = GetAllInternal(extension_id);
|
| + list.push_back(linked_item);
|
| +}
|
| +
|
| +const ExtensionSuggestedLinkList* ExtensionSuggestedLinksManager::GetAll(
|
| + const std::string& extension_id) const {
|
| + SuggestedLinksMap::const_iterator found = suggested_links_.find(extension_id);
|
| + if (found != suggested_links_.end())
|
| + return &found->second;
|
| + return NULL;
|
| +}
|
| +
|
| +void ExtensionSuggestedLinksManager::Clear(const std::string& extension_id,
|
| + const std::string& link_url) {
|
| + SuggestedLinksMap::iterator found = suggested_links_.find(extension_id);
|
| + if (found != suggested_links_.end())
|
| + RemoveLinkFromList(link_url, &found->second);
|
| +}
|
| +
|
| +void ExtensionSuggestedLinksManager::ClearAll(const std::string& extension_id) {
|
| + SuggestedLinksMap::iterator found = suggested_links_.find(extension_id);
|
| + if (found != suggested_links_.end())
|
| + suggested_links_.erase(found);
|
| +}
|
| +
|
| +ExtensionSuggestedLinkList& ExtensionSuggestedLinksManager::GetAllInternal(
|
| + const std::string& extension_id) {
|
| + // |insert| returns the element if it's already in the map.
|
| + SuggestedLinksMap::iterator found = suggested_links_.insert(
|
| + SuggestedLinksMap::value_type(extension_id,
|
| + ExtensionSuggestedLinkList())).first;
|
| + CHECK(found != suggested_links_.end());
|
| + return found->second;
|
| +}
|
|
|