OLD | NEW |
---|---|
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 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
93 } | 93 } |
94 | 94 |
95 bool MatchesOrigin(const GURL& my_url, const GURL& other_url) { | 95 bool MatchesOrigin(const GURL& my_url, const GURL& other_url) { |
96 return my_url.host() == other_url.host() && | 96 return my_url.host() == other_url.host() && |
97 my_url.port() == other_url.port() && | 97 my_url.port() == other_url.port() && |
98 (my_url.scheme() == other_url.scheme() || | 98 (my_url.scheme() == other_url.scheme() || |
99 (my_url.SchemeIs(chrome::kHttpsScheme) && | 99 (my_url.SchemeIs(chrome::kHttpsScheme) && |
100 other_url.SchemeIs(chrome::kHttpScheme))); | 100 other_url.SchemeIs(chrome::kHttpScheme))); |
101 } | 101 } |
102 | 102 |
103 bool IsCommandLineInstantURL(const GURL& url) { | |
104 const CommandLine* cl = CommandLine::ForCurrentProcess(); | |
105 const GURL instant_url(cl->GetSwitchValueASCII(switches::kInstantURL)); | |
106 return instant_url.is_valid() && MatchesOrigin(url, instant_url); | |
107 } | |
108 | |
109 bool MatchesAnySearchURL(const GURL& url, TemplateURL* template_url) { | 103 bool MatchesAnySearchURL(const GURL& url, TemplateURL* template_url) { |
110 GURL search_url = | 104 GURL search_url = |
111 TemplateURLRefToGURL(template_url->url_ref(), kDisableStartMargin); | 105 TemplateURLRefToGURL(template_url->url_ref(), kDisableStartMargin); |
112 if (search_url.is_valid() && MatchesOriginAndPath(url, search_url)) | 106 if (search_url.is_valid() && MatchesOriginAndPath(url, search_url)) |
113 return true; | 107 return true; |
114 | 108 |
115 // "URLCount() - 1" because we already tested url_ref above. | 109 // "URLCount() - 1" because we already tested url_ref above. |
116 for (size_t i = 0; i < template_url->URLCount() - 1; ++i) { | 110 for (size_t i = 0; i < template_url->URLCount() - 1; ++i) { |
117 TemplateURLRef ref(template_url, i); | 111 TemplateURLRef ref(template_url, i); |
118 search_url = TemplateURLRefToGURL(ref, kDisableStartMargin); | 112 search_url = TemplateURLRefToGURL(ref, kDisableStartMargin); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
189 const InstantService* instant_service = | 183 const InstantService* instant_service = |
190 InstantServiceFactory::GetForProfile(profile); | 184 InstantServiceFactory::GetForProfile(profile); |
191 if (!instant_service) | 185 if (!instant_service) |
192 return false; | 186 return false; |
193 | 187 |
194 return instant_service->IsInstantProcess(process_host->GetID()); | 188 return instant_service->IsInstantProcess(process_host->GetID()); |
195 } | 189 } |
196 | 190 |
197 // Returns true if |url| can be used as an Instant URL for |profile|. | 191 // Returns true if |url| can be used as an Instant URL for |profile|. |
198 bool IsInstantURL(const GURL& url, Profile* profile) { | 192 bool IsInstantURL(const GURL& url, Profile* profile) { |
193 if (!url.is_valid()) | |
194 return false; | |
195 | |
199 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); | 196 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); |
200 if (!template_url) | 197 if (!template_url) |
201 return false; | 198 return false; |
202 | 199 |
203 const TemplateURLRef& instant_url_ref = template_url->instant_url_ref(); | |
204 const bool extended_api_enabled = IsInstantExtendedAPIEnabled(); | 200 const bool extended_api_enabled = IsInstantExtendedAPIEnabled(); |
205 GURL effective_url = url; | 201 if (extended_api_enabled && !url.SchemeIsSecure()) |
samarth
2013/06/17 20:39:37
I ran into two issues while trying out your patch:
| |
206 | |
207 if (IsCommandLineInstantURL(url)) | |
208 effective_url = CoerceCommandLineURLToTemplateURL(url, instant_url_ref, | |
209 kDisableStartMargin); | |
210 | |
211 if (!effective_url.is_valid()) | |
212 return false; | |
213 | |
214 if (extended_api_enabled && !effective_url.SchemeIsSecure()) | |
215 return false; | 202 return false; |
216 | 203 |
217 if (extended_api_enabled && | 204 if (extended_api_enabled && |
218 !template_url->HasSearchTermsReplacementKey(effective_url)) | 205 !template_url->HasSearchTermsReplacementKey(url)) |
219 return false; | 206 return false; |
220 | 207 |
208 const TemplateURLRef& instant_url_ref = template_url->instant_url_ref(); | |
221 const GURL instant_url = | 209 const GURL instant_url = |
222 TemplateURLRefToGURL(instant_url_ref, kDisableStartMargin); | 210 TemplateURLRefToGURL(instant_url_ref, kDisableStartMargin); |
223 if (!instant_url.is_valid()) | 211 if (!instant_url.is_valid()) |
224 return false; | 212 return false; |
225 | 213 |
226 if (MatchesOriginAndPath(effective_url, instant_url)) | 214 if (MatchesOriginAndPath(url, instant_url)) |
227 return true; | 215 return true; |
228 | 216 |
229 if (extended_api_enabled && MatchesAnySearchURL(effective_url, template_url)) | 217 if (extended_api_enabled && MatchesAnySearchURL(url, template_url)) |
230 return true; | 218 return true; |
231 | 219 |
232 return false; | 220 return false; |
233 } | 221 } |
234 | 222 |
235 string16 GetSearchTermsImpl(const content::WebContents* contents, | 223 string16 GetSearchTermsImpl(const content::WebContents* contents, |
236 const content::NavigationEntry* entry) { | 224 const content::NavigationEntry* entry) { |
237 // For security reasons, don't extract search terms if the page is not being | 225 // For security reasons, don't extract search terms if the page is not being |
238 // rendered in the privileged Instant renderer process. This is to protect | 226 // rendered in the privileged Instant renderer process. This is to protect |
239 // against a malicious page somehow scripting the search results page and | 227 // against a malicious page somehow scripting the search results page and |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
305 | 293 |
306 FieldTrialFlags flags; | 294 FieldTrialFlags flags; |
307 if (GetFieldTrialInfo( | 295 if (GetFieldTrialInfo( |
308 base::FieldTrialList::FindFullName(kInstantExtendedFieldTrialName), | 296 base::FieldTrialList::FindFullName(kInstantExtendedFieldTrialName), |
309 &flags, NULL)) { | 297 &flags, NULL)) { |
310 return GetBoolValueForFlagWithDefault(kLocalOnlyFlagName, false, flags); | 298 return GetBoolValueForFlagWithDefault(kLocalOnlyFlagName, false, flags); |
311 } | 299 } |
312 return false; | 300 return false; |
313 } | 301 } |
314 | 302 |
315 string16 GetSearchTermsFromURL(Profile* profile, const GURL& in_url) { | 303 string16 GetSearchTermsFromURL(Profile* profile, const GURL& url) { |
316 GURL url(in_url); | |
317 string16 search_terms; | 304 string16 search_terms; |
318 | |
319 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); | 305 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); |
320 if (!template_url) | 306 if (template_url && url.SchemeIsSecure() && |
321 return string16(); | 307 template_url->HasSearchTermsReplacementKey(url)) |
322 | |
323 if (IsCommandLineInstantURL(url)) | |
324 url = CoerceCommandLineURLToTemplateURL(url, template_url->url_ref(), | |
325 kDisableStartMargin); | |
326 | |
327 if (url.SchemeIsSecure() && template_url->HasSearchTermsReplacementKey(url)) | |
328 template_url->ExtractSearchTermsFromURL(url, &search_terms); | 308 template_url->ExtractSearchTermsFromURL(url, &search_terms); |
329 | |
330 return search_terms; | 309 return search_terms; |
331 } | 310 } |
332 | 311 |
333 string16 GetSearchTermsFromNavigationEntry( | 312 string16 GetSearchTermsFromNavigationEntry( |
334 const content::NavigationEntry* entry) { | 313 const content::NavigationEntry* entry) { |
335 string16 search_terms; | 314 string16 search_terms; |
336 if (entry) | 315 if (entry) |
337 entry->GetExtraData(sessions::kSearchTermsKey, &search_terms); | 316 entry->GetExtraData(sessions::kSearchTermsKey, &search_terms); |
338 return search_terms; | 317 return search_terms; |
339 } | 318 } |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
483 if (!IsInstantCheckboxEnabled(profile)) | 462 if (!IsInstantCheckboxEnabled(profile)) |
484 return GURL(); | 463 return GURL(); |
485 | 464 |
486 const bool extended_api_enabled = IsInstantExtendedAPIEnabled(); | 465 const bool extended_api_enabled = IsInstantExtendedAPIEnabled(); |
487 | 466 |
488 // In non-extended mode, the checkbox must be checked. | 467 // In non-extended mode, the checkbox must be checked. |
489 if (!extended_api_enabled && !IsInstantCheckboxChecked(profile)) | 468 if (!extended_api_enabled && !IsInstantCheckboxChecked(profile)) |
490 return GURL(); | 469 return GURL(); |
491 | 470 |
492 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); | 471 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); |
493 CommandLine* cl = CommandLine::ForCurrentProcess(); | |
494 if (cl->HasSwitch(switches::kInstantURL)) { | |
495 GURL instant_url(cl->GetSwitchValueASCII(switches::kInstantURL)); | |
496 if (extended_api_enabled) { | |
497 // Extended mode won't work if the search terms replacement key is absent. | |
498 GURL coerced_url = CoerceCommandLineURLToTemplateURL( | |
499 instant_url, template_url->instant_url_ref(), start_margin); | |
500 if (!template_url->HasSearchTermsReplacementKey(coerced_url)) | |
501 return GURL(); | |
502 } | |
503 return instant_url; | |
504 } | |
505 | |
506 GURL instant_url = | 472 GURL instant_url = |
507 TemplateURLRefToGURL(template_url->instant_url_ref(), start_margin); | 473 TemplateURLRefToGURL(template_url->instant_url_ref(), start_margin); |
508 if (extended_api_enabled && !instant_url.SchemeIsSecure()) { | 474 if (extended_api_enabled && !instant_url.SchemeIsSecure()) { |
509 // Extended mode requires HTTPS. Force it if necessary. | 475 // Extended mode requires HTTPS. Force it if necessary. |
510 const std::string secure_scheme = chrome::kHttpsScheme; | 476 const std::string secure_scheme = chrome::kHttpsScheme; |
511 GURL::Replacements replacements; | 477 GURL::Replacements replacements; |
512 replacements.SetSchemeStr(secure_scheme); | 478 replacements.SetSchemeStr(secure_scheme); |
513 instant_url = instant_url.ReplaceComponents(replacements); | 479 instant_url = instant_url.ReplaceComponents(replacements); |
514 } | 480 } |
515 | 481 |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
700 } | 666 } |
701 | 667 |
702 // Given a FieldTrialFlags object, returns the boolean value of the provided | 668 // Given a FieldTrialFlags object, returns the boolean value of the provided |
703 // flag. | 669 // flag. |
704 bool GetBoolValueForFlagWithDefault(const std::string& flag, | 670 bool GetBoolValueForFlagWithDefault(const std::string& flag, |
705 bool default_value, | 671 bool default_value, |
706 const FieldTrialFlags& flags) { | 672 const FieldTrialFlags& flags) { |
707 return !!GetUInt64ValueForFlagWithDefault(flag, default_value ? 1 : 0, flags); | 673 return !!GetUInt64ValueForFlagWithDefault(flag, default_value ? 1 : 0, flags); |
708 } | 674 } |
709 | 675 |
710 // Coerces the commandline Instant URL to look like a template URL, so that we | |
711 // can extract search terms from it. | |
712 GURL CoerceCommandLineURLToTemplateURL(const GURL& instant_url, | |
713 const TemplateURLRef& ref, | |
714 int start_margin) { | |
715 GURL search_url = TemplateURLRefToGURL(ref, start_margin); | |
716 | |
717 // NOTE(samarth): GURL returns temporaries which we must save because | |
718 // GURL::Replacements expects the replacements to live until | |
719 // ReplaceComponents is called. | |
720 const std::string search_scheme = chrome::kHttpsScheme; | |
721 const std::string search_host = search_url.host(); | |
722 const std::string search_port = search_url.port(); | |
723 | |
724 GURL::Replacements replacements; | |
725 replacements.SetSchemeStr(search_scheme); | |
726 replacements.SetHostStr(search_host); | |
727 replacements.SetPortStr(search_port); | |
728 return instant_url.ReplaceComponents(replacements); | |
729 } | |
730 | |
731 bool DefaultSearchProviderSupportsInstant(Profile* profile) { | 676 bool DefaultSearchProviderSupportsInstant(Profile* profile) { |
732 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); | 677 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); |
733 if (!template_url) | 678 if (!template_url) |
734 return false; | 679 return false; |
735 | 680 |
736 GURL instant_url = TemplateURLRefToGURL(template_url->instant_url_ref(), | 681 GURL instant_url = TemplateURLRefToGURL(template_url->instant_url_ref(), |
737 kDisableStartMargin); | 682 kDisableStartMargin); |
738 // Extended mode instant requires a search terms replacement key. | 683 // Extended mode instant requires a search terms replacement key. |
739 return instant_url.is_valid() && | 684 return instant_url.is_valid() && |
740 (!IsInstantExtendedAPIEnabled() || | 685 (!IsInstantExtendedAPIEnabled() || |
(...skipping 13 matching lines...) Expand all Loading... | |
754 for (size_t i = 0; i < items_b.size(); ++i) { | 699 for (size_t i = 0; i < items_b.size(); ++i) { |
755 if (items_b[i].url != items_a[i].url || | 700 if (items_b[i].url != items_a[i].url || |
756 items_b[i].title != items_a[i].title) { | 701 items_b[i].title != items_a[i].title) { |
757 return false; | 702 return false; |
758 } | 703 } |
759 } | 704 } |
760 return true; | 705 return true; |
761 } | 706 } |
762 | 707 |
763 } // namespace chrome | 708 } // namespace chrome |
OLD | NEW |