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 // This file contains the Search autocomplete provider. This provider is | 5 // This file contains the Search autocomplete provider. This provider is |
6 // responsible for all non-keyword autocomplete entries that start with | 6 // responsible for all non-keyword autocomplete entries that start with |
7 // "Search <engine> for ...", including searching for the current input string, | 7 // "Search <engine> for ...", including searching for the current input string, |
8 // search history, and search suggestions. An instance of it gets created and | 8 // search history, and search suggestions. An instance of it gets created and |
9 // managed by the autocomplete controller. | 9 // managed by the autocomplete controller. |
10 // | 10 // |
(...skipping 12 matching lines...) Expand all Loading... | |
23 #include "base/memory/scoped_ptr.h" | 23 #include "base/memory/scoped_ptr.h" |
24 #include "base/time.h" | 24 #include "base/time.h" |
25 #include "chrome/browser/autocomplete/autocomplete.h" | 25 #include "chrome/browser/autocomplete/autocomplete.h" |
26 #include "chrome/browser/autocomplete/autocomplete_match.h" | 26 #include "chrome/browser/autocomplete/autocomplete_match.h" |
27 #include "chrome/browser/history/history_types.h" | 27 #include "chrome/browser/history/history_types.h" |
28 #include "chrome/browser/search_engines/template_url.h" | 28 #include "chrome/browser/search_engines/template_url.h" |
29 #include "chrome/browser/search_engines/template_url_id.h" | 29 #include "chrome/browser/search_engines/template_url_id.h" |
30 #include "content/public/common/url_fetcher_delegate.h" | 30 #include "content/public/common/url_fetcher_delegate.h" |
31 | 31 |
32 class Profile; | 32 class Profile; |
33 class TemplateURLService; | |
33 | 34 |
34 namespace base { | 35 namespace base { |
35 class Value; | 36 class Value; |
36 } | 37 } |
37 | 38 |
38 // Autocomplete provider for searches and suggestions from a search engine. | 39 // Autocomplete provider for searches and suggestions from a search engine. |
39 // | 40 // |
40 // After construction, the autocomplete controller repeatedly calls Start() | 41 // After construction, the autocomplete controller repeatedly calls Start() |
41 // with some user input, each time expecting to receive a small set of the best | 42 // with some user input, each time expecting to receive a small set of the best |
42 // matches (either synchronously or asynchronously). | 43 // matches (either synchronously or asynchronously). |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
84 virtual ~SearchProvider(); | 85 virtual ~SearchProvider(); |
85 | 86 |
86 // Manages the providers (TemplateURLs) used by SearchProvider. Two providers | 87 // Manages the providers (TemplateURLs) used by SearchProvider. Two providers |
87 // may be used: | 88 // may be used: |
88 // . The default provider. This corresponds to the user's default search | 89 // . The default provider. This corresponds to the user's default search |
89 // engine. This is always used, except for the rare case of no default | 90 // engine. This is always used, except for the rare case of no default |
90 // engine. | 91 // engine. |
91 // . The keyword provider. This is used if the user has typed in a keyword. | 92 // . The keyword provider. This is used if the user has typed in a keyword. |
92 class Providers { | 93 class Providers { |
93 public: | 94 public: |
94 explicit Providers(Profile* profile) | 95 explicit Providers(TemplateURLService* template_url_service); |
95 : cached_default_provider_(profile, TemplateURLData()), | 96 |
96 cached_keyword_provider_(profile, TemplateURLData()), | 97 // Returns true if the specified providers match the two providers cached |
97 default_provider_(NULL), | 98 // by this class. |
98 keyword_provider_(NULL) { | 99 bool equal(const string16& default_provider, |
100 const string16& keyword_provider) const { | |
101 return (default_provider == default_provider_) && | |
102 (keyword_provider == keyword_provider_); | |
99 } | 103 } |
100 | 104 |
101 // Returns true if the specified providers match the two providers managed | 105 // Resets the cached providers. |
102 // by this class. | 106 void set(const string16& default_provider, |
103 bool equals(const TemplateURL* default_provider, | 107 const string16& keyword_provider) { |
104 const TemplateURL* keyword_provider) { | 108 default_provider_ = default_provider; |
105 return (default_provider == default_provider_ && | 109 keyword_provider_ = keyword_provider; |
106 keyword_provider == keyword_provider_); | |
107 } | 110 } |
108 | 111 |
109 // Resets the providers. | 112 TemplateURLService* template_url_service() { return template_url_service_; } |
110 void Set(const TemplateURL* default_provider, | 113 string16 default_provider() const { return default_provider_; } |
sky
2012/05/08 19:55:37
const string16& (same for keyword_provider()).
| |
111 const TemplateURL* keyword_provider); | 114 string16 keyword_provider() const { return keyword_provider_; } |
112 | 115 |
113 TemplateURL* default_provider() { | 116 // NOTE: These may return NULL even if the provider members are nonempty! |
114 DCHECK(valid_default_provider()); | 117 const TemplateURL* GetDefaultProviderURL() const; |
115 return &cached_default_provider_; | 118 const TemplateURL* GetKeywordProviderURL() const; |
116 } | |
117 | 119 |
118 TemplateURL* keyword_provider() { | 120 // Returns true if |from_keyword_provider| is true, or the keyword provider |
119 DCHECK(valid_keyword_provider()); | 121 // is not valid. |
120 return &cached_keyword_provider_; | |
121 } | |
122 | |
123 // Returns true if the default provider is valid. | |
124 bool valid_default_provider() const { return !!default_provider_; } | |
125 | |
126 // Returns true if the default provider is valid and has a valid suggest | |
127 // url. | |
128 bool valid_suggest_for_default_provider() const { | |
129 return default_provider_ && | |
130 !cached_default_provider_.suggestions_url().empty(); | |
131 } | |
132 | |
133 // Returns true if the keyword provider is valid. | |
134 bool valid_keyword_provider() const { return !!keyword_provider_; } | |
135 | |
136 // Returns true if the keyword provider is valid and has a valid suggest | |
137 // url. | |
138 bool valid_suggest_for_keyword_provider() const { | |
139 return keyword_provider_ && | |
140 !cached_keyword_provider_.suggestions_url().empty(); | |
141 } | |
142 | |
143 // Returns true if |from_keyword_provider| is true, or | |
144 // the keyword provider is not valid. | |
145 bool is_primary_provider(bool from_keyword_provider) const { | 122 bool is_primary_provider(bool from_keyword_provider) const { |
146 return from_keyword_provider || !valid_keyword_provider(); | 123 return from_keyword_provider || keyword_provider_.empty(); |
147 } | 124 } |
148 | 125 |
149 private: | 126 private: |
127 TemplateURLService* template_url_service_; | |
128 | |
150 // Cached across the life of a query so we behave consistently even if the | 129 // Cached across the life of a query so we behave consistently even if the |
151 // user changes their default while the query is running. | 130 // user changes their default while the query is running. |
152 TemplateURL cached_default_provider_; | 131 string16 default_provider_; |
153 TemplateURL cached_keyword_provider_; | 132 string16 keyword_provider_; |
154 | |
155 // TODO(pkasting): http://b/1162970 We shouldn't need these. | |
156 const TemplateURL* default_provider_; | |
157 const TemplateURL* keyword_provider_; | |
158 | 133 |
159 DISALLOW_COPY_AND_ASSIGN(Providers); | 134 DISALLOW_COPY_AND_ASSIGN(Providers); |
160 }; | 135 }; |
161 | 136 |
162 struct NavigationResult { | 137 struct NavigationResult { |
163 NavigationResult(const GURL& url, const string16& site_name) | 138 NavigationResult(const GURL& url, const string16& site_name) |
164 : url(url), | 139 : url(url), |
165 site_name(site_name) { | 140 site_name(site_name) { |
166 } | 141 } |
167 | 142 |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
340 // Has FinalizeInstantQuery been invoked since the last |Start|? | 315 // Has FinalizeInstantQuery been invoked since the last |Start|? |
341 bool instant_finalized_; | 316 bool instant_finalized_; |
342 | 317 |
343 // The |suggest_text| parameter passed to FinalizeInstantQuery. | 318 // The |suggest_text| parameter passed to FinalizeInstantQuery. |
344 string16 default_provider_suggest_text_; | 319 string16 default_provider_suggest_text_; |
345 | 320 |
346 DISALLOW_COPY_AND_ASSIGN(SearchProvider); | 321 DISALLOW_COPY_AND_ASSIGN(SearchProvider); |
347 }; | 322 }; |
348 | 323 |
349 #endif // CHROME_BROWSER_AUTOCOMPLETE_SEARCH_PROVIDER_H_ | 324 #endif // CHROME_BROWSER_AUTOCOMPLETE_SEARCH_PROVIDER_H_ |
OLD | NEW |