| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/api/discovery/suggested_links_registry.h" | 5 #include "chrome/browser/extensions/api/discovery/suggested_links_registry.h" |
| 6 | 6 |
| 7 namespace { | 7 namespace { |
| 8 | 8 |
| 9 typedef extensions::SuggestedLinksRegistry::SuggestedLinkList SuggestedLinkList; | 9 typedef extensions::SuggestedLinksRegistry::SuggestedLinkList SuggestedLinkList; |
| 10 | 10 |
| 11 void RemoveLinkFromList(const std::string& link_url, SuggestedLinkList* list) { | 11 SuggestedLinkList::iterator FindUrlInList(const std::string& link_url, |
| 12 SuggestedLinkList* list) { |
| 12 SuggestedLinkList::iterator found = list->begin(); | 13 SuggestedLinkList::iterator found = list->begin(); |
| 13 for (; found != list->end(); ++found) | 14 for (; found != list->end(); ++found) |
| 14 if (link_url.compare((*found)->link_url()) == 0) break; | 15 if (link_url.compare((*found)->link_url()) == 0) break; |
| 16 return found; |
| 17 } |
| 18 |
| 19 void RemoveLinkFromList(const std::string& link_url, SuggestedLinkList* list) { |
| 20 SuggestedLinkList::iterator found = FindUrlInList(link_url, list); |
| 15 if (found != list->end()) | 21 if (found != list->end()) |
| 16 list->erase(found); | 22 list->erase(found); |
| 17 } | 23 } |
| 18 | 24 |
| 19 } // namespace | 25 } // namespace |
| 20 | 26 |
| 21 namespace extensions { | 27 namespace extensions { |
| 22 | 28 |
| 23 SuggestedLinksRegistry::SuggestedLinksRegistry() {} | 29 SuggestedLinksRegistry::SuggestedLinksRegistry() {} |
| 24 | 30 |
| 25 SuggestedLinksRegistry::~SuggestedLinksRegistry() { | 31 SuggestedLinksRegistry::~SuggestedLinksRegistry() { |
| 26 } | 32 } |
| 27 | 33 |
| 28 void SuggestedLinksRegistry::Add(const std::string& extension_id, | 34 void SuggestedLinksRegistry::Add(const std::string& extension_id, |
| 29 scoped_ptr<extensions::SuggestedLink> item) { | 35 scoped_ptr<extensions::SuggestedLink> item) { |
| 30 SuggestedLinkList& list = GetAllInternal(extension_id); | 36 SuggestedLinkList& list = GetAllInternal(extension_id); |
| 31 list.push_back(linked_ptr<extensions::SuggestedLink>(item.release())); | 37 SuggestedLinkList::iterator found = FindUrlInList(item->link_url(), &list); |
| 38 linked_ptr<extensions::SuggestedLink> new_item(item.release()); |
| 39 if (found != list.end()) |
| 40 *found = new_item; |
| 41 else |
| 42 list.push_back(new_item); |
| 32 } | 43 } |
| 33 | 44 |
| 34 const SuggestedLinkList* SuggestedLinksRegistry::GetAll( | 45 const SuggestedLinkList* SuggestedLinksRegistry::GetAll( |
| 35 const std::string& extension_id) const { | 46 const std::string& extension_id) const { |
| 36 SuggestedLinksMap::const_iterator found = suggested_links_.find(extension_id); | 47 SuggestedLinksMap::const_iterator found = suggested_links_.find(extension_id); |
| 37 if (found != suggested_links_.end()) | 48 if (found != suggested_links_.end()) |
| 38 return &found->second; | 49 return &found->second; |
| 39 return NULL; | 50 return &empty_list_; |
| 40 } | 51 } |
| 41 | 52 |
| 42 void SuggestedLinksRegistry::Remove(const std::string& extension_id, | 53 void SuggestedLinksRegistry::Remove(const std::string& extension_id, |
| 43 const std::string& link_url) { | 54 const std::string& link_url) { |
| 44 SuggestedLinksMap::iterator found = suggested_links_.find(extension_id); | 55 SuggestedLinksMap::iterator found = suggested_links_.find(extension_id); |
| 45 if (found != suggested_links_.end()) | 56 if (found != suggested_links_.end()) |
| 46 RemoveLinkFromList(link_url, &found->second); | 57 RemoveLinkFromList(link_url, &found->second); |
| 47 } | 58 } |
| 48 | 59 |
| 49 void SuggestedLinksRegistry::ClearAll(const std::string& extension_id) { | 60 void SuggestedLinksRegistry::ClearAll(const std::string& extension_id) { |
| 50 SuggestedLinksMap::iterator found = suggested_links_.find(extension_id); | 61 SuggestedLinksMap::iterator found = suggested_links_.find(extension_id); |
| 51 if (found != suggested_links_.end()) | 62 if (found != suggested_links_.end()) |
| 52 suggested_links_.erase(found); | 63 suggested_links_.erase(found); |
| 53 } | 64 } |
| 54 | 65 |
| 55 SuggestedLinkList& SuggestedLinksRegistry::GetAllInternal( | 66 SuggestedLinkList& SuggestedLinksRegistry::GetAllInternal( |
| 56 const std::string& extension_id) { | 67 const std::string& extension_id) { |
| 57 // |insert| returns the element if it's already in the map. | 68 // |insert| returns the element if it's already in the map. |
| 58 SuggestedLinksMap::iterator found = suggested_links_.insert( | 69 SuggestedLinksMap::iterator found = suggested_links_.insert( |
| 59 SuggestedLinksMap::value_type(extension_id, SuggestedLinkList())).first; | 70 SuggestedLinksMap::value_type(extension_id, SuggestedLinkList())).first; |
| 60 CHECK(found != suggested_links_.end()); | 71 CHECK(found != suggested_links_.end()); |
| 61 return found->second; | 72 return found->second; |
| 62 } | 73 } |
| 63 | 74 |
| 64 } // namespace extensions | 75 } // namespace extensions |
| OLD | NEW |