| 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/search_engines/search_host_to_urls_map.h" | 5 #include "chrome/browser/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 "chrome/browser/search_engines/template_url.h" | 8 #include "chrome/browser/search_engines/template_url.h" |
| 9 #include "chrome/browser/search_engines/template_url_service.h" | 9 #include "chrome/browser/search_engines/template_url_service.h" |
| 10 | 10 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 DCHECK(!template_url->IsExtensionKeyword()); | 30 DCHECK(!template_url->IsExtensionKeyword()); |
| 31 | 31 |
| 32 const GURL url(TemplateURLService::GenerateSearchURLUsingTermsData( | 32 const GURL url(TemplateURLService::GenerateSearchURLUsingTermsData( |
| 33 template_url, search_terms_data)); | 33 template_url, search_terms_data)); |
| 34 if (!url.is_valid() || !url.has_host()) | 34 if (!url.is_valid() || !url.has_host()) |
| 35 return; | 35 return; |
| 36 | 36 |
| 37 host_to_urls_map_[url.host()].insert(template_url); | 37 host_to_urls_map_[url.host()].insert(template_url); |
| 38 } | 38 } |
| 39 | 39 |
| 40 void SearchHostToURLsMap::Remove(TemplateURL* template_url) { | 40 void SearchHostToURLsMap::Remove(TemplateURL* template_url, |
| 41 const SearchTermsData& search_terms_data) { |
| 41 DCHECK(initialized_); | 42 DCHECK(initialized_); |
| 42 DCHECK(template_url); | 43 DCHECK(template_url); |
| 43 DCHECK(!template_url->IsExtensionKeyword()); | 44 DCHECK(!template_url->IsExtensionKeyword()); |
| 44 | 45 |
| 45 const GURL url(TemplateURLService::GenerateSearchURL(template_url)); | 46 const GURL url(TemplateURLService::GenerateSearchURLUsingTermsData( |
| 47 template_url, search_terms_data)); |
| 46 if (!url.is_valid() || !url.has_host()) | 48 if (!url.is_valid() || !url.has_host()) |
| 47 return; | 49 return; |
| 48 | 50 |
| 49 const std::string host(url.host()); | 51 const std::string host(url.host()); |
| 50 DCHECK(host_to_urls_map_.find(host) != host_to_urls_map_.end()); | 52 DCHECK(host_to_urls_map_.find(host) != host_to_urls_map_.end()); |
| 51 | 53 |
| 52 TemplateURLSet& urls = host_to_urls_map_[host]; | 54 TemplateURLSet& urls = host_to_urls_map_[host]; |
| 53 DCHECK(urls.find(template_url) != urls.end()); | 55 DCHECK(urls.find(template_url) != urls.end()); |
| 54 | 56 |
| 55 urls.erase(urls.find(template_url)); | 57 urls.erase(urls.find(template_url)); |
| 56 if (urls.empty()) | 58 if (urls.empty()) |
| 57 host_to_urls_map_.erase(host_to_urls_map_.find(host)); | 59 host_to_urls_map_.erase(host_to_urls_map_.find(host)); |
| 58 } | 60 } |
| 59 | 61 |
| 60 void SearchHostToURLsMap::UpdateGoogleBaseURLs( | |
| 61 const SearchTermsData& search_terms_data) { | |
| 62 DCHECK(initialized_); | |
| 63 | |
| 64 // Create a list of the the TemplateURLs to update. | |
| 65 TemplateURLService::TemplateURLVector t_urls_using_base_url; | |
| 66 for (HostToURLsMap::iterator i(host_to_urls_map_.begin()); | |
| 67 i != host_to_urls_map_.end(); ++i) { | |
| 68 for (TemplateURLSet::const_iterator j(i->second.begin()); | |
| 69 j != i->second.end(); ++j) { | |
| 70 if ((*j)->url_ref().HasGoogleBaseURLs() || | |
| 71 (*j)->suggestions_url_ref().HasGoogleBaseURLs()) | |
| 72 t_urls_using_base_url.push_back(*j); | |
| 73 } | |
| 74 } | |
| 75 | |
| 76 for (TemplateURLService::TemplateURLVector::const_iterator i( | |
| 77 t_urls_using_base_url.begin()); i != t_urls_using_base_url.end(); ++i) | |
| 78 RemoveByPointer(*i); | |
| 79 | |
| 80 Add(t_urls_using_base_url, search_terms_data); | |
| 81 } | |
| 82 | |
| 83 TemplateURL* SearchHostToURLsMap::GetTemplateURLForHost( | 62 TemplateURL* SearchHostToURLsMap::GetTemplateURLForHost( |
| 84 const std::string& host) { | 63 const std::string& host) { |
| 85 DCHECK(initialized_); | 64 DCHECK(initialized_); |
| 86 | 65 |
| 87 HostToURLsMap::const_iterator iter = host_to_urls_map_.find(host); | 66 HostToURLsMap::const_iterator iter = host_to_urls_map_.find(host); |
| 88 if (iter == host_to_urls_map_.end() || iter->second.empty()) | 67 if (iter == host_to_urls_map_.end() || iter->second.empty()) |
| 89 return NULL; | 68 return NULL; |
| 90 return *(iter->second.begin()); // Return the 1st element. | 69 return *(iter->second.begin()); // Return the 1st element. |
| 91 } | 70 } |
| 92 | 71 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 115 if (url_set_iterator != i->second.end()) { | 94 if (url_set_iterator != i->second.end()) { |
| 116 i->second.erase(url_set_iterator); | 95 i->second.erase(url_set_iterator); |
| 117 if (i->second.empty()) | 96 if (i->second.empty()) |
| 118 host_to_urls_map_.erase(i); | 97 host_to_urls_map_.erase(i); |
| 119 // A given TemplateURL only occurs once in the map. As soon as we find the | 98 // A given TemplateURL only occurs once in the map. As soon as we find the |
| 120 // entry, stop. | 99 // entry, stop. |
| 121 return; | 100 return; |
| 122 } | 101 } |
| 123 } | 102 } |
| 124 } | 103 } |
| OLD | NEW |