Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(486)

Side by Side Diff: chrome/browser/search/search.cc

Issue 396583002: Stop depending on InstantService from BaseSearchProvider (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/search/search.h ('k') | chrome/browser/search/search_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 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/search.h" 5 #include "chrome/browser/search/search.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/metrics/field_trial.h" 8 #include "base/metrics/field_trial.h"
9 #include "base/metrics/histogram.h" 9 #include "base/metrics/histogram.h"
10 #include "base/prefs/pref_service.h" 10 #include "base/prefs/pref_service.h"
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 TemplateURLService* template_url_service = 163 TemplateURLService* template_url_service =
164 TemplateURLServiceFactory::GetForProfile(profile); 164 TemplateURLServiceFactory::GetForProfile(profile);
165 if (template_url_service) 165 if (template_url_service)
166 return template_url_service->GetDefaultSearchProvider(); 166 return template_url_service->GetDefaultSearchProvider();
167 } 167 }
168 return NULL; 168 return NULL;
169 } 169 }
170 170
171 GURL TemplateURLRefToGURL(const TemplateURLRef& ref, 171 GURL TemplateURLRefToGURL(const TemplateURLRef& ref,
172 const SearchTermsData& search_terms_data, 172 const SearchTermsData& search_terms_data,
173 int start_margin,
174 bool append_extra_query_params, 173 bool append_extra_query_params,
175 bool force_instant_results) { 174 bool force_instant_results) {
176 TemplateURLRef::SearchTermsArgs search_terms_args = 175 TemplateURLRef::SearchTermsArgs search_terms_args =
177 TemplateURLRef::SearchTermsArgs(base::string16()); 176 TemplateURLRef::SearchTermsArgs(base::string16());
178 search_terms_args.omnibox_start_margin = start_margin;
179 search_terms_args.append_extra_query_params = append_extra_query_params; 177 search_terms_args.append_extra_query_params = append_extra_query_params;
180 search_terms_args.force_instant_results = force_instant_results; 178 search_terms_args.force_instant_results = force_instant_results;
181 return GURL(ref.ReplaceSearchTerms(search_terms_args, search_terms_data)); 179 return GURL(ref.ReplaceSearchTerms(search_terms_args, search_terms_data));
182 } 180 }
183 181
184 bool MatchesAnySearchURL(const GURL& url, 182 bool MatchesAnySearchURL(const GURL& url,
185 TemplateURL* template_url, 183 TemplateURL* template_url,
186 const SearchTermsData& search_terms_data) { 184 const SearchTermsData& search_terms_data) {
187 GURL search_url = 185 GURL search_url = TemplateURLRefToGURL(template_url->url_ref(),
188 TemplateURLRefToGURL(template_url->url_ref(), search_terms_data, 186 search_terms_data, false, false);
189 kDisableStartMargin, false, false);
190 if (search_url.is_valid() && 187 if (search_url.is_valid() &&
191 search::MatchesOriginAndPath(url, search_url)) 188 search::MatchesOriginAndPath(url, search_url))
192 return true; 189 return true;
193 190
194 // "URLCount() - 1" because we already tested url_ref above. 191 // "URLCount() - 1" because we already tested url_ref above.
195 for (size_t i = 0; i < template_url->URLCount() - 1; ++i) { 192 for (size_t i = 0; i < template_url->URLCount() - 1; ++i) {
196 TemplateURLRef ref(template_url, i); 193 TemplateURLRef ref(template_url, i);
197 search_url = TemplateURLRefToGURL(ref, search_terms_data, 194 search_url = TemplateURLRefToGURL(ref, search_terms_data, false, false);
198 kDisableStartMargin, false, false);
199 if (search_url.is_valid() && 195 if (search_url.is_valid() &&
200 search::MatchesOriginAndPath(url, search_url)) 196 search::MatchesOriginAndPath(url, search_url))
201 return true; 197 return true;
202 } 198 }
203 199
204 return false; 200 return false;
205 } 201 }
206 202
207 203
208 204
(...skipping 22 matching lines...) Expand all
231 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); 227 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile);
232 if (!template_url) 228 if (!template_url)
233 return false; 229 return false;
234 230
235 if (!IsSuitableURLForInstant(url, template_url)) 231 if (!IsSuitableURLForInstant(url, template_url))
236 return false; 232 return false;
237 233
238 const TemplateURLRef& instant_url_ref = template_url->instant_url_ref(); 234 const TemplateURLRef& instant_url_ref = template_url->instant_url_ref();
239 UIThreadSearchTermsData search_terms_data(profile); 235 UIThreadSearchTermsData search_terms_data(profile);
240 const GURL instant_url = TemplateURLRefToGURL( 236 const GURL instant_url = TemplateURLRefToGURL(
241 instant_url_ref, search_terms_data, kDisableStartMargin, false, false); 237 instant_url_ref, search_terms_data, false, false);
242 if (!instant_url.is_valid()) 238 if (!instant_url.is_valid())
243 return false; 239 return false;
244 240
245 if (search::MatchesOriginAndPath(url, instant_url)) 241 if (search::MatchesOriginAndPath(url, instant_url))
246 return true; 242 return true;
247 243
248 return IsQueryExtractionEnabled() && 244 return IsQueryExtractionEnabled() &&
249 MatchesAnySearchURL(url, template_url, search_terms_data); 245 MatchesAnySearchURL(url, template_url, search_terms_data);
250 } 246 }
251 247
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 : url(url), state(state) {} 310 : url(url), state(state) {}
315 311
316 static NewTabURLDetails ForProfile(Profile* profile) { 312 static NewTabURLDetails ForProfile(Profile* profile) {
317 const GURL local_url(chrome::kChromeSearchLocalNtpUrl); 313 const GURL local_url(chrome::kChromeSearchLocalNtpUrl);
318 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); 314 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile);
319 if (!profile || !template_url) 315 if (!profile || !template_url)
320 return NewTabURLDetails(local_url, NEW_TAB_URL_BAD); 316 return NewTabURLDetails(local_url, NEW_TAB_URL_BAD);
321 317
322 GURL search_provider_url = TemplateURLRefToGURL( 318 GURL search_provider_url = TemplateURLRefToGURL(
323 template_url->new_tab_url_ref(), UIThreadSearchTermsData(profile), 319 template_url->new_tab_url_ref(), UIThreadSearchTermsData(profile),
324 kDisableStartMargin, false, false); 320 false, false);
325 NewTabURLState state = IsValidNewTabURL(profile, search_provider_url); 321 NewTabURLState state = IsValidNewTabURL(profile, search_provider_url);
326 switch (state) { 322 switch (state) {
327 case NEW_TAB_URL_VALID: 323 case NEW_TAB_URL_VALID:
328 // We can use the search provider's page. 324 // We can use the search provider's page.
329 return NewTabURLDetails(search_provider_url, state); 325 return NewTabURLDetails(search_provider_url, state);
330 case NEW_TAB_URL_INCOGNITO: 326 case NEW_TAB_URL_INCOGNITO:
331 // Incognito has its own New Tab. 327 // Incognito has its own New Tab.
332 return NewTabURLDetails(GURL(), state); 328 return NewTabURLDetails(GURL(), state);
333 default: 329 default:
334 // Use the local New Tab otherwise. 330 // Use the local New Tab otherwise.
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
520 GURL new_tab_url(GetNewTabPageURL(profile)); 516 GURL new_tab_url(GetNewTabPageURL(profile));
521 return new_tab_url.is_valid() && 517 return new_tab_url.is_valid() &&
522 search::MatchesOriginAndPath(entry->GetURL(), new_tab_url); 518 search::MatchesOriginAndPath(entry->GetURL(), new_tab_url);
523 } 519 }
524 520
525 bool IsSuggestPrefEnabled(Profile* profile) { 521 bool IsSuggestPrefEnabled(Profile* profile) {
526 return profile && !profile->IsOffTheRecord() && profile->GetPrefs() && 522 return profile && !profile->IsOffTheRecord() && profile->GetPrefs() &&
527 profile->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled); 523 profile->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled);
528 } 524 }
529 525
530 GURL GetInstantURL(Profile* profile, int start_margin, 526 GURL GetInstantURL(Profile* profile, bool force_instant_results) {
531 bool force_instant_results) {
532 if (!IsInstantExtendedAPIEnabled() || !IsSuggestPrefEnabled(profile)) 527 if (!IsInstantExtendedAPIEnabled() || !IsSuggestPrefEnabled(profile))
533 return GURL(); 528 return GURL();
534 529
535 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); 530 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile);
536 if (!template_url) 531 if (!template_url)
537 return GURL(); 532 return GURL();
538 533
539 GURL instant_url = TemplateURLRefToGURL( 534 GURL instant_url = TemplateURLRefToGURL(
540 template_url->instant_url_ref(), UIThreadSearchTermsData(profile), 535 template_url->instant_url_ref(), UIThreadSearchTermsData(profile),
541 start_margin, true, force_instant_results); 536 true, force_instant_results);
542 if (!instant_url.is_valid() || 537 if (!instant_url.is_valid() ||
543 !template_url->HasSearchTermsReplacementKey(instant_url)) 538 !template_url->HasSearchTermsReplacementKey(instant_url))
544 return GURL(); 539 return GURL();
545 540
546 // Extended mode requires HTTPS. Force it unless the base URL was overridden 541 // Extended mode requires HTTPS. Force it unless the base URL was overridden
547 // on the command line, in which case we allow HTTP (see comments on 542 // on the command line, in which case we allow HTTP (see comments on
548 // IsSuitableURLForInstant()). 543 // IsSuitableURLForInstant()).
549 if (!instant_url.SchemeIsSecure() && 544 if (!instant_url.SchemeIsSecure() &&
550 !google_util::StartsWithCommandLineGoogleBaseURL(instant_url)) { 545 !google_util::StartsWithCommandLineGoogleBaseURL(instant_url)) {
551 GURL::Replacements replacements; 546 GURL::Replacements replacements;
(...skipping 19 matching lines...) Expand all
571 566
572 // Returns URLs associated with the default search engine for |profile|. 567 // Returns URLs associated with the default search engine for |profile|.
573 std::vector<GURL> GetSearchURLs(Profile* profile) { 568 std::vector<GURL> GetSearchURLs(Profile* profile) {
574 std::vector<GURL> result; 569 std::vector<GURL> result;
575 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); 570 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile);
576 if (!template_url) 571 if (!template_url)
577 return result; 572 return result;
578 for (size_t i = 0; i < template_url->URLCount(); ++i) { 573 for (size_t i = 0; i < template_url->URLCount(); ++i) {
579 TemplateURLRef ref(template_url, i); 574 TemplateURLRef ref(template_url, i);
580 result.push_back(TemplateURLRefToGURL(ref, UIThreadSearchTermsData(profile), 575 result.push_back(TemplateURLRefToGURL(ref, UIThreadSearchTermsData(profile),
581 kDisableStartMargin, false, false)); 576 false, false));
582 } 577 }
583 return result; 578 return result;
584 } 579 }
585 580
586 GURL GetNewTabPageURL(Profile* profile) { 581 GURL GetNewTabPageURL(Profile* profile) {
587 return NewTabURLDetails::ForProfile(profile).url; 582 return NewTabURLDetails::ForProfile(profile).url;
588 } 583 }
589 584
590 GURL GetSearchResultPrefetchBaseURL(Profile* profile) { 585 GURL GetSearchResultPrefetchBaseURL(Profile* profile) {
591 return ShouldPrefetchSearchResults() ? 586 return ShouldPrefetchSearchResults() ? GetInstantURL(profile, true) : GURL();
592 GetInstantURL(profile, kDisableStartMargin, true) : GURL();
593 } 587 }
594 588
595 bool ShouldPrefetchSearchResults() { 589 bool ShouldPrefetchSearchResults() {
596 if (!IsInstantExtendedAPIEnabled()) 590 if (!IsInstantExtendedAPIEnabled())
597 return false; 591 return false;
598 592
599 if (CommandLine::ForCurrentProcess()->HasSwitch( 593 if (CommandLine::ForCurrentProcess()->HasSwitch(
600 switches::kPrefetchSearchResults)) { 594 switches::kPrefetchSearchResults)) {
601 return true; 595 return true;
602 } 596 }
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after
859 return !!GetUInt64ValueForFlagWithDefault(flag, default_value ? 1 : 0, flags); 853 return !!GetUInt64ValueForFlagWithDefault(flag, default_value ? 1 : 0, flags);
860 } 854 }
861 855
862 bool ShouldUseAltInstantURL() { 856 bool ShouldUseAltInstantURL() {
863 FieldTrialFlags flags; 857 FieldTrialFlags flags;
864 return GetFieldTrialInfo(&flags) && GetBoolValueForFlagWithDefault( 858 return GetFieldTrialInfo(&flags) && GetBoolValueForFlagWithDefault(
865 kUseAltInstantURL, false, flags); 859 kUseAltInstantURL, false, flags);
866 } 860 }
867 861
868 } // namespace chrome 862 } // namespace chrome
OLDNEW
« no previous file with comments | « chrome/browser/search/search.h ('k') | chrome/browser/search/search_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698