| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/search_engines/search_host_to_urls_map.h" | 5 #include "components/search_engines/search_host_to_urls_map.h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "components/search_engines/template_url.h" | 8 #include "components/search_engines/template_url.h" |
| 9 | 9 |
| 10 SearchHostToURLsMap::SearchHostToURLsMap() | 10 SearchHostToURLsMap::SearchHostToURLsMap() |
| 11 : initialized_(false) { | 11 : initialized_(false) { |
| 12 } | 12 } |
| 13 | 13 |
| 14 SearchHostToURLsMap::~SearchHostToURLsMap() { | 14 SearchHostToURLsMap::~SearchHostToURLsMap() { |
| 15 } | 15 } |
| 16 | 16 |
| 17 void SearchHostToURLsMap::Init( | 17 void SearchHostToURLsMap::Init( |
| 18 const TemplateURLService::TemplateURLVector& template_urls, | 18 const TemplateURLService::TemplateURLVector& template_urls, |
| 19 const SearchTermsData& search_terms_data) { | 19 const SearchTermsData& search_terms_data) { |
| 20 DCHECK(!initialized_); | 20 DCHECK(!initialized_); |
| 21 initialized_ = true; // Set here so Add doesn't assert. | 21 initialized_ = true; // Set here so Add doesn't assert. |
| 22 Add(template_urls, search_terms_data); | 22 Add(template_urls, search_terms_data); |
| 23 } | 23 } |
| 24 | 24 |
| 25 void SearchHostToURLsMap::Add(TemplateURL* template_url, | 25 void SearchHostToURLsMap::Add(TemplateURL* template_url, |
| 26 const SearchTermsData& search_terms_data) { | 26 const SearchTermsData& search_terms_data) { |
| 27 DCHECK(initialized_); | 27 DCHECK(initialized_); |
| 28 DCHECK(template_url); | 28 DCHECK(template_url); |
| 29 DCHECK_NE(TemplateURL::OMNIBOX_API_EXTENSION, template_url->GetType()); |
| 29 | 30 |
| 30 const GURL url(template_url->GenerateSearchURL(search_terms_data)); | 31 const GURL url(template_url->GenerateSearchURL(search_terms_data)); |
| 31 if (!url.is_valid() || !url.has_host()) | 32 if (!url.is_valid() || !url.has_host()) |
| 32 return; | 33 return; |
| 33 | 34 |
| 34 host_to_urls_map_[url.host()].insert(template_url); | 35 host_to_urls_map_[url.host()].insert(template_url); |
| 35 } | 36 } |
| 36 | 37 |
| 37 void SearchHostToURLsMap::Remove(TemplateURL* template_url) { | 38 void SearchHostToURLsMap::Remove(TemplateURL* template_url) { |
| 38 DCHECK(initialized_); | 39 DCHECK(initialized_); |
| 39 DCHECK(template_url); | 40 DCHECK(template_url); |
| 41 DCHECK_NE(TemplateURL::OMNIBOX_API_EXTENSION, template_url->GetType()); |
| 40 | 42 |
| 41 for (HostToURLsMap::iterator i = host_to_urls_map_.begin(); | 43 for (HostToURLsMap::iterator i = host_to_urls_map_.begin(); |
| 42 i != host_to_urls_map_.end(); ++i) { | 44 i != host_to_urls_map_.end(); ++i) { |
| 43 TemplateURLSet::iterator url_set_iterator = i->second.find(template_url); | 45 TemplateURLSet::iterator url_set_iterator = i->second.find(template_url); |
| 44 if (url_set_iterator != i->second.end()) { | 46 if (url_set_iterator != i->second.end()) { |
| 45 i->second.erase(url_set_iterator); | 47 i->second.erase(url_set_iterator); |
| 46 if (i->second.empty()) | 48 if (i->second.empty()) |
| 47 host_to_urls_map_.erase(i); | 49 host_to_urls_map_.erase(i); |
| 48 // A given TemplateURL only occurs once in the map. As soon as we find the | 50 // A given TemplateURL only occurs once in the map. As soon as we find the |
| 49 // entry, stop. | 51 // entry, stop. |
| (...skipping 22 matching lines...) Expand all Loading... |
| 72 return &urls_for_host->second; | 74 return &urls_for_host->second; |
| 73 } | 75 } |
| 74 | 76 |
| 75 void SearchHostToURLsMap::Add( | 77 void SearchHostToURLsMap::Add( |
| 76 const TemplateURLService::TemplateURLVector& template_urls, | 78 const TemplateURLService::TemplateURLVector& template_urls, |
| 77 const SearchTermsData& search_terms_data) { | 79 const SearchTermsData& search_terms_data) { |
| 78 for (TemplateURLService::TemplateURLVector::const_iterator i( | 80 for (TemplateURLService::TemplateURLVector::const_iterator i( |
| 79 template_urls.begin()); i != template_urls.end(); ++i) | 81 template_urls.begin()); i != template_urls.end(); ++i) |
| 80 Add(*i, search_terms_data); | 82 Add(*i, search_terms_data); |
| 81 } | 83 } |
| OLD | NEW |