Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / components / search_engines / search_host_to_urls_map.cc
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.
4
5 #include "components/search_engines/search_host_to_urls_map.h"
6
7 #include "base/memory/scoped_ptr.h"
8 #include "components/search_engines/template_url.h"
9
10 SearchHostToURLsMap::SearchHostToURLsMap()
11     : initialized_(false) {
12 }
13
14 SearchHostToURLsMap::~SearchHostToURLsMap() {
15 }
16
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);
23 }
24
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());
30
31   const GURL url(template_url->GenerateSearchURL(search_terms_data));
32   if (!url.is_valid() || !url.has_host())
33     return;
34
35   host_to_urls_map_[url.host()].insert(template_url);
36 }
37
38 void SearchHostToURLsMap::Remove(TemplateURL* template_url) {
39   DCHECK(initialized_);
40   DCHECK(template_url);
41   DCHECK_NE(TemplateURL::OMNIBOX_API_EXTENSION, template_url->GetType());
42
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;
53     }
54   }
55 }
56
57 TemplateURL* SearchHostToURLsMap::GetTemplateURLForHost(
58     const std::string& host) {
59   DCHECK(initialized_);
60
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.
65 }
66
67 SearchHostToURLsMap::TemplateURLSet* SearchHostToURLsMap::GetURLsForHost(
68     const std::string& host) {
69   DCHECK(initialized_);
70
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;
75 }
76
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);
83 }