| 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 |
| 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::TemplateURLVector& template_urls, | 19 const TemplateURLService::TemplateURLVector& 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(!template_url->IsExtensionKeyword()); |
| 30 | 31 |
| 31 const GURL url(TemplateURLService::GenerateSearchURLUsingTermsData( | 32 const GURL url(TemplateURLService::GenerateSearchURLUsingTermsData( |
| 32 template_url, search_terms_data)); | 33 template_url, search_terms_data)); |
| 33 if (!url.is_valid() || !url.has_host()) | 34 if (!url.is_valid() || !url.has_host()) |
| 34 return; | 35 return; |
| 35 | 36 |
| 36 host_to_urls_map_[url.host()].insert(template_url); | 37 host_to_urls_map_[url.host()].insert(template_url); |
| 37 } | 38 } |
| 38 | 39 |
| 39 void SearchHostToURLsMap::Remove(TemplateURL* template_url) { | 40 void SearchHostToURLsMap::Remove(TemplateURL* template_url) { |
| 40 DCHECK(initialized_); | 41 DCHECK(initialized_); |
| 41 DCHECK(template_url); | 42 DCHECK(template_url); |
| 43 DCHECK(!template_url->IsExtensionKeyword()); |
| 42 | 44 |
| 43 const GURL url(TemplateURLService::GenerateSearchURL(template_url)); | 45 const GURL url(TemplateURLService::GenerateSearchURL(template_url)); |
| 44 if (!url.is_valid() || !url.has_host()) | 46 if (!url.is_valid() || !url.has_host()) |
| 45 return; | 47 return; |
| 46 | 48 |
| 47 const std::string host(url.host()); | 49 const std::string host(url.host()); |
| 48 DCHECK(host_to_urls_map_.find(host) != host_to_urls_map_.end()); | 50 DCHECK(host_to_urls_map_.find(host) != host_to_urls_map_.end()); |
| 49 | 51 |
| 50 TemplateURLSet& urls = host_to_urls_map_[host]; | 52 TemplateURLSet& urls = host_to_urls_map_[host]; |
| 51 DCHECK(urls.find(template_url) != urls.end()); | 53 DCHECK(urls.find(template_url) != urls.end()); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 if (url_set_iterator != i->second.end()) { | 115 if (url_set_iterator != i->second.end()) { |
| 114 i->second.erase(url_set_iterator); | 116 i->second.erase(url_set_iterator); |
| 115 if (i->second.empty()) | 117 if (i->second.empty()) |
| 116 host_to_urls_map_.erase(i); | 118 host_to_urls_map_.erase(i); |
| 117 // A given TemplateURL only occurs once in the map. As soon as we find the | 119 // A given TemplateURL only occurs once in the map. As soon as we find the |
| 118 // entry, stop. | 120 // entry, stop. |
| 119 return; | 121 return; |
| 120 } | 122 } |
| 121 } | 123 } |
| 122 } | 124 } |
| OLD | NEW |