Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 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/browser/extensions/api/discovery/extension_suggested_links_mana ger.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 namespace { | |
| 10 | |
| 11 // 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.)
| |
| 12 // 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.
| |
| 13 class ExtensionSuggestedLinkMatchUrlPred { | |
| 14 public: | |
| 15 explicit ExtensionSuggestedLinkMatchUrlPred(const std::string* link_url) : | |
| 16 link_url_(link_url) {} | |
| 17 | |
| 18 bool operator() (linked_ptr<ExtensionSuggestedLink> suggested_link) const { | |
| 19 return suggested_link->link_url().compare(*link_url_) == 0; | |
| 20 } | |
| 21 | |
| 22 private: | |
| 23 const std::string* link_url_; | |
| 24 }; | |
| 25 | |
| 26 void RemoveLinkFromList(const std::string& link_url, | |
| 27 ExtensionSuggestedLinkList* list) { | |
| 28 ExtensionSuggestedLinkMatchUrlPred pred(&link_url); | |
| 29 ExtensionSuggestedLinkList::iterator found = std::find_if(list->begin(), | |
| 30 list->end(), pred); | |
| 31 if (found != list->end()) | |
| 32 list->erase(found); | |
| 33 } | |
| 34 | |
| 35 } | |
| 36 | |
| 37 ExtensionSuggestedLinksManager::ExtensionSuggestedLinksManager() {} | |
| 38 | |
| 39 ExtensionSuggestedLinksManager::~ExtensionSuggestedLinksManager() { | |
| 40 } | |
| 41 | |
| 42 void ExtensionSuggestedLinksManager::Add(const std::string& extension_id, | |
| 43 ExtensionSuggestedLink* item) { | |
| 44 // Do this first since we own the incoming item and hence want to delete | |
| 45 // it in error paths. | |
| 46 linked_ptr<ExtensionSuggestedLink> linked_item(item); | |
| 47 | |
| 48 ExtensionSuggestedLinkList& list = GetAllInternal(extension_id); | |
| 49 list.push_back(linked_item); | |
| 50 } | |
| 51 | |
| 52 const ExtensionSuggestedLinkList* ExtensionSuggestedLinksManager::GetAll( | |
| 53 const std::string& extension_id) const { | |
| 54 SuggestedLinksMap::const_iterator found = suggested_links_.find(extension_id); | |
| 55 if (found != suggested_links_.end()) | |
| 56 return &found->second; | |
| 57 return NULL; | |
| 58 } | |
| 59 | |
| 60 void ExtensionSuggestedLinksManager::Clear(const std::string& extension_id, | |
| 61 const std::string& link_url) { | |
| 62 SuggestedLinksMap::iterator found = suggested_links_.find(extension_id); | |
| 63 if (found != suggested_links_.end()) | |
| 64 RemoveLinkFromList(link_url, &found->second); | |
| 65 } | |
| 66 | |
| 67 void ExtensionSuggestedLinksManager::ClearAll(const std::string& extension_id) { | |
| 68 SuggestedLinksMap::iterator found = suggested_links_.find(extension_id); | |
| 69 if (found != suggested_links_.end()) | |
| 70 suggested_links_.erase(found); | |
| 71 } | |
| 72 | |
| 73 ExtensionSuggestedLinkList& ExtensionSuggestedLinksManager::GetAllInternal( | |
| 74 const std::string& extension_id) { | |
| 75 // |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.
| |
| 76 SuggestedLinksMap::iterator found = suggested_links_.insert( | |
| 77 SuggestedLinksMap::value_type(extension_id, | |
| 78 ExtensionSuggestedLinkList())).first; | |
| 79 CHECK(found != suggested_links_.end()); | |
| 80 return found->second; | |
| 81 } | |
| OLD | NEW |