| 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/extension_suggested_links_manager.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 using content::BrowserThread; |
| 10 |
| 11 namespace { |
| 12 |
| 13 // A functor that mathes an ExtensionSuggestedLink having a given URL. Needs an |
| 14 // implicit copy constructor to be used with std::find. |
| 15 class ExtensionSuggestedLinkMatchUrlPred { |
| 16 public: |
| 17 explicit ExtensionSuggestedLinkMatchUrlPred(const std::string* link_url) : |
| 18 link_url_(link_url) {} |
| 19 |
| 20 bool operator() (linked_ptr<ExtensionSuggestedLink> suggested_link) const { |
| 21 return suggested_link->link_url().compare(*link_url_) == 0; |
| 22 } |
| 23 |
| 24 private: |
| 25 const std::string* link_url_; |
| 26 }; |
| 27 |
| 28 void RemoveLinkFromList(const std::string& link_url, |
| 29 ExtensionSuggestedLinkList* list) { |
| 30 ExtensionSuggestedLinkMatchUrlPred pred(&link_url); |
| 31 ExtensionSuggestedLinkList::iterator found = std::find_if(list->begin(), |
| 32 list->end(), pred); |
| 33 if (found != list->end()) |
| 34 list->erase(found); |
| 35 } |
| 36 |
| 37 } |
| 38 |
| 39 ExtensionSuggestedLinksManager::ExtensionSuggestedLinksManager() {} |
| 40 |
| 41 ExtensionSuggestedLinksManager::~ExtensionSuggestedLinksManager() { |
| 42 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 43 } |
| 44 |
| 45 void ExtensionSuggestedLinksManager::Add(const std::string& extension_id, |
| 46 ExtensionSuggestedLink* item) { |
| 47 // Do this first since we own the incoming item and hence want to delete |
| 48 // it in error paths. |
| 49 linked_ptr<ExtensionSuggestedLink> linked_item(item); |
| 50 |
| 51 ExtensionSuggestedLinkList& list = GetAllInternal(extension_id); |
| 52 list.push_back(linked_item); |
| 53 } |
| 54 |
| 55 const ExtensionSuggestedLinkList* ExtensionSuggestedLinksManager::GetAll( |
| 56 const std::string& extension_id) const { |
| 57 SuggestedLinksMap::const_iterator found = suggested_links_.find(extension_id); |
| 58 if (found != suggested_links_.end()) |
| 59 return &found->second; |
| 60 return NULL; |
| 61 } |
| 62 |
| 63 void ExtensionSuggestedLinksManager::Clear(const std::string& extension_id, |
| 64 const std::string& link_url) { |
| 65 SuggestedLinksMap::iterator found = suggested_links_.find(extension_id); |
| 66 if (found != suggested_links_.end()) |
| 67 RemoveLinkFromList(link_url, &found->second); |
| 68 } |
| 69 |
| 70 void ExtensionSuggestedLinksManager::ClearAll(const std::string& extension_id) { |
| 71 SuggestedLinksMap::iterator found = suggested_links_.find(extension_id); |
| 72 if (found != suggested_links_.end()) |
| 73 suggested_links_.erase(found); |
| 74 } |
| 75 |
| 76 ExtensionSuggestedLinkList& ExtensionSuggestedLinksManager::GetAllInternal( |
| 77 const std::string& extension_id) { |
| 78 // |insert| returns the element if it's already in the map. |
| 79 SuggestedLinksMap::iterator found = suggested_links_.insert( |
| 80 SuggestedLinksMap::value_type(extension_id, |
| 81 ExtensionSuggestedLinkList())).first; |
| 82 CHECK(found != suggested_links_.end()); |
| 83 return found->second; |
| 84 } |
| OLD | NEW |