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