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
3 // found in the LICENSE file.
5 #include "chrome/browser/search_engines/search_host_to_urls_map.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "chrome/browser/search_engines/template_url.h"
9 #include "chrome/browser/search_engines/template_url_service.h"
11 SearchHostToURLsMap::SearchHostToURLsMap()
12 : initialized_(false) {
15 SearchHostToURLsMap::~SearchHostToURLsMap() {
18 void SearchHostToURLsMap::Init(
19 const TemplateURLService::TemplateURLVector
& template_urls
,
20 const SearchTermsData
& search_terms_data
) {
21 DCHECK(!initialized_
);
22 initialized_
= true; // Set here so Add doesn't assert.
23 Add(template_urls
, search_terms_data
);
26 void SearchHostToURLsMap::Add(TemplateURL
* template_url
,
27 const SearchTermsData
& search_terms_data
) {
31 const GURL
url(TemplateURLService::GenerateSearchURLUsingTermsData(
32 template_url
, search_terms_data
));
33 if (!url
.is_valid() || !url
.has_host())
36 host_to_urls_map_
[url
.host()].insert(template_url
);
39 void SearchHostToURLsMap::Remove(TemplateURL
* template_url
,
40 const SearchTermsData
& search_terms_data
) {
44 const GURL
url(TemplateURLService::GenerateSearchURLUsingTermsData(
45 template_url
, search_terms_data
));
46 if (!url
.is_valid() || !url
.has_host())
49 const std::string
host(url
.host());
50 DCHECK(host_to_urls_map_
.find(host
) != host_to_urls_map_
.end());
52 TemplateURLSet
& urls
= host_to_urls_map_
[host
];
53 DCHECK(urls
.find(template_url
) != urls
.end());
55 urls
.erase(urls
.find(template_url
));
57 host_to_urls_map_
.erase(host_to_urls_map_
.find(host
));
60 TemplateURL
* SearchHostToURLsMap::GetTemplateURLForHost(
61 const std::string
& host
) {
64 HostToURLsMap::const_iterator iter
= host_to_urls_map_
.find(host
);
65 if (iter
== host_to_urls_map_
.end() || iter
->second
.empty())
67 return *(iter
->second
.begin()); // Return the 1st element.
70 SearchHostToURLsMap::TemplateURLSet
* SearchHostToURLsMap::GetURLsForHost(
71 const std::string
& host
) {
74 HostToURLsMap::iterator urls_for_host
= host_to_urls_map_
.find(host
);
75 if (urls_for_host
== host_to_urls_map_
.end() || urls_for_host
->second
.empty())
77 return &urls_for_host
->second
;
80 void SearchHostToURLsMap::Add(
81 const TemplateURLService::TemplateURLVector
& template_urls
,
82 const SearchTermsData
& search_terms_data
) {
83 for (TemplateURLService::TemplateURLVector::const_iterator
i(
84 template_urls
.begin()); i
!= template_urls
.end(); ++i
)
85 Add(*i
, search_terms_data
);
88 void SearchHostToURLsMap::RemoveByPointer(TemplateURL
* template_url
) {
89 for (HostToURLsMap::iterator i
= host_to_urls_map_
.begin();
90 i
!= host_to_urls_map_
.end(); ++i
) {
91 TemplateURLSet::iterator url_set_iterator
= i
->second
.find(template_url
);
92 if (url_set_iterator
!= i
->second
.end()) {
93 i
->second
.erase(url_set_iterator
);
94 if (i
->second
.empty())
95 host_to_urls_map_
.erase(i
);
96 // A given TemplateURL only occurs once in the map. As soon as we find the