Lazily build the app list model.
[chromium-blink-merge.git] / components / search_engines / search_host_to_urls_map.cc
blob1db68aa803e0d07ac9ade8d7822a910c32922577
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
3 // found in the LICENSE file.
5 #include "components/search_engines/search_host_to_urls_map.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "components/search_engines/template_url.h"
10 SearchHostToURLsMap::SearchHostToURLsMap()
11 : initialized_(false) {
14 SearchHostToURLsMap::~SearchHostToURLsMap() {
17 void SearchHostToURLsMap::Init(
18 const TemplateURLService::TemplateURLVector& template_urls,
19 const SearchTermsData& search_terms_data) {
20 DCHECK(!initialized_);
21 initialized_ = true; // Set here so Add doesn't assert.
22 Add(template_urls, search_terms_data);
25 void SearchHostToURLsMap::Add(TemplateURL* template_url,
26 const SearchTermsData& search_terms_data) {
27 DCHECK(initialized_);
28 DCHECK(template_url);
29 DCHECK_NE(TemplateURL::OMNIBOX_API_EXTENSION, template_url->GetType());
31 const GURL url(template_url->GenerateSearchURL(search_terms_data));
32 if (!url.is_valid() || !url.has_host())
33 return;
35 host_to_urls_map_[url.host()].insert(template_url);
38 void SearchHostToURLsMap::Remove(TemplateURL* template_url) {
39 DCHECK(initialized_);
40 DCHECK(template_url);
41 DCHECK_NE(TemplateURL::OMNIBOX_API_EXTENSION, template_url->GetType());
43 for (HostToURLsMap::iterator i = host_to_urls_map_.begin();
44 i != host_to_urls_map_.end(); ++i) {
45 TemplateURLSet::iterator url_set_iterator = i->second.find(template_url);
46 if (url_set_iterator != i->second.end()) {
47 i->second.erase(url_set_iterator);
48 if (i->second.empty())
49 host_to_urls_map_.erase(i);
50 // A given TemplateURL only occurs once in the map. As soon as we find the
51 // entry, stop.
52 return;
57 TemplateURL* SearchHostToURLsMap::GetTemplateURLForHost(
58 const std::string& host) {
59 DCHECK(initialized_);
61 HostToURLsMap::const_iterator iter = host_to_urls_map_.find(host);
62 if (iter == host_to_urls_map_.end() || iter->second.empty())
63 return NULL;
64 return *(iter->second.begin()); // Return the 1st element.
67 SearchHostToURLsMap::TemplateURLSet* SearchHostToURLsMap::GetURLsForHost(
68 const std::string& host) {
69 DCHECK(initialized_);
71 HostToURLsMap::iterator urls_for_host = host_to_urls_map_.find(host);
72 if (urls_for_host == host_to_urls_map_.end() || urls_for_host->second.empty())
73 return NULL;
74 return &urls_for_host->second;
77 void SearchHostToURLsMap::Add(
78 const TemplateURLService::TemplateURLVector& template_urls,
79 const SearchTermsData& search_terms_data) {
80 for (TemplateURLService::TemplateURLVector::const_iterator i(
81 template_urls.begin()); i != template_urls.end(); ++i)
82 Add(*i, search_terms_data);