| 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" | |
| 10 | 9 |
| 11 SearchHostToURLsMap::SearchHostToURLsMap() | 10 SearchHostToURLsMap::SearchHostToURLsMap() |
| 12 : initialized_(false) { | 11 : initialized_(false) { |
| 13 } | 12 } |
| 14 | 13 |
| 15 SearchHostToURLsMap::~SearchHostToURLsMap() { | 14 SearchHostToURLsMap::~SearchHostToURLsMap() { |
| 16 } | 15 } |
| 17 | 16 |
| 18 void SearchHostToURLsMap::Init( | 17 void SearchHostToURLsMap::Init( |
| 19 const TemplateURLService::TemplateURLVector& template_urls, | 18 const TemplateURLService::TemplateURLVector& template_urls, |
| 20 const SearchTermsData& search_terms_data) { | 19 const SearchTermsData& search_terms_data) { |
| 21 DCHECK(!initialized_); | 20 DCHECK(!initialized_); |
| 22 initialized_ = true; // Set here so Add doesn't assert. | 21 initialized_ = true; // Set here so Add doesn't assert. |
| 23 Add(template_urls, search_terms_data); | 22 Add(template_urls, search_terms_data); |
| 24 } | 23 } |
| 25 | 24 |
| 26 void SearchHostToURLsMap::Add(TemplateURL* template_url, | 25 void SearchHostToURLsMap::Add(TemplateURL* template_url, |
| 27 const SearchTermsData& search_terms_data) { | 26 const SearchTermsData& search_terms_data) { |
| 28 DCHECK(initialized_); | 27 DCHECK(initialized_); |
| 29 DCHECK(template_url); | 28 DCHECK(template_url); |
| 30 | 29 |
| 31 const GURL url(TemplateURLService::GenerateSearchURL( | 30 const GURL url(template_url->GenerateSearchURL(search_terms_data)); |
| 32 template_url, search_terms_data)); | |
| 33 if (!url.is_valid() || !url.has_host()) | 31 if (!url.is_valid() || !url.has_host()) |
| 34 return; | 32 return; |
| 35 | 33 |
| 36 host_to_urls_map_[url.host()].insert(template_url); | 34 host_to_urls_map_[url.host()].insert(template_url); |
| 37 } | 35 } |
| 38 | 36 |
| 39 void SearchHostToURLsMap::Remove(TemplateURL* template_url, | 37 void SearchHostToURLsMap::Remove(TemplateURL* template_url, |
| 40 const SearchTermsData& search_terms_data) { | 38 const SearchTermsData& search_terms_data) { |
| 41 DCHECK(initialized_); | 39 DCHECK(initialized_); |
| 42 DCHECK(template_url); | 40 DCHECK(template_url); |
| 43 | 41 |
| 44 const GURL url(TemplateURLService::GenerateSearchURL( | 42 const GURL url(template_url->GenerateSearchURL(search_terms_data)); |
| 45 template_url, search_terms_data)); | |
| 46 if (!url.is_valid() || !url.has_host()) | 43 if (!url.is_valid() || !url.has_host()) |
| 47 return; | 44 return; |
| 48 | 45 |
| 49 const std::string host(url.host()); | 46 const std::string host(url.host()); |
| 50 DCHECK(host_to_urls_map_.find(host) != host_to_urls_map_.end()); | 47 DCHECK(host_to_urls_map_.find(host) != host_to_urls_map_.end()); |
| 51 | 48 |
| 52 TemplateURLSet& urls = host_to_urls_map_[host]; | 49 TemplateURLSet& urls = host_to_urls_map_[host]; |
| 53 DCHECK(urls.find(template_url) != urls.end()); | 50 DCHECK(urls.find(template_url) != urls.end()); |
| 54 | 51 |
| 55 urls.erase(urls.find(template_url)); | 52 urls.erase(urls.find(template_url)); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 if (url_set_iterator != i->second.end()) { | 89 if (url_set_iterator != i->second.end()) { |
| 93 i->second.erase(url_set_iterator); | 90 i->second.erase(url_set_iterator); |
| 94 if (i->second.empty()) | 91 if (i->second.empty()) |
| 95 host_to_urls_map_.erase(i); | 92 host_to_urls_map_.erase(i); |
| 96 // A given TemplateURL only occurs once in the map. As soon as we find the | 93 // A given TemplateURL only occurs once in the map. As soon as we find the |
| 97 // entry, stop. | 94 // entry, stop. |
| 98 return; | 95 return; |
| 99 } | 96 } |
| 100 } | 97 } |
| 101 } | 98 } |
| OLD | NEW |