Chromium Code Reviews| Index: chrome/browser/extensions/api/discovery/extension_suggested_links_manager.cc |
| diff --git a/chrome/browser/extensions/api/discovery/extension_suggested_links_manager.cc b/chrome/browser/extensions/api/discovery/extension_suggested_links_manager.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..262e0bedcdac462269a73bd17942aedddbe0cde1 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/discovery/extension_suggested_links_manager.cc |
| @@ -0,0 +1,81 @@ |
| +// 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/discovery/extension_suggested_links_manager.h" |
| + |
| +#include <algorithm> |
| + |
| +namespace { |
| + |
| +// A functor that mathes an ExtensionSuggestedLink having a given URL. Needs an |
|
Yoyo Zhou
2012/05/15 00:38:04
typo: matches
beaudoin
2012/05/16 16:56:16
Ack. (Gone.)
|
| +// implicit copy constructor to be used with std::find. |
|
Aaron Boodman
2012/05/15 00:07:43
Blech, std::find. Not worth the trouble. Just loop
beaudoin
2012/05/16 16:56:16
Done.
|
| +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() { |
| +} |
| + |
| +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. |
|
Aaron Boodman
2012/05/15 00:07:43
Cool trick, never seen that one.
beaudoin
2012/05/16 16:56:16
Ack.
|
| + SuggestedLinksMap::iterator found = suggested_links_.insert( |
| + SuggestedLinksMap::value_type(extension_id, |
| + ExtensionSuggestedLinkList())).first; |
| + CHECK(found != suggested_links_.end()); |
| + return found->second; |
| +} |