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 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 return false; | 143 return false; |
144 | 144 |
145 const InstantService* instant_service = | 145 const InstantService* instant_service = |
146 InstantServiceFactory::GetForProfile(profile); | 146 InstantServiceFactory::GetForProfile(profile); |
147 if (!instant_service) | 147 if (!instant_service) |
148 return false; | 148 return false; |
149 | 149 |
150 return instant_service->IsInstantProcess(process_host->GetID()); | 150 return instant_service->IsInstantProcess(process_host->GetID()); |
151 } | 151 } |
152 | 152 |
| 153 // Returns true if |url| passes some basic checks that must succeed for it to be |
| 154 // usable as an instant URL: |
| 155 // (1) It contains the search terms replacement key of |template_url|, which is |
| 156 // expected to be the TemplateURL* for the default search provider. |
| 157 // (2) It has a secure scheme. |
| 158 bool IsSuitableURLForInstant(const GURL& url, const TemplateURL* template_url) { |
| 159 return template_url->HasSearchTermsReplacementKey(url) && |
| 160 url.SchemeIsSecure(); |
| 161 } |
| 162 |
153 // Returns true if |url| can be used as an Instant URL for |profile|. | 163 // Returns true if |url| can be used as an Instant URL for |profile|. |
154 bool IsInstantURL(const GURL& url, Profile* profile) { | 164 bool IsInstantURL(const GURL& url, Profile* profile) { |
| 165 if (!url.is_valid()) |
| 166 return false; |
| 167 |
155 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); | 168 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); |
156 if (!template_url) | 169 if (!template_url) |
157 return false; | 170 return false; |
158 | 171 |
159 const TemplateURLRef& instant_url_ref = template_url->instant_url_ref(); | |
160 const bool extended_api_enabled = IsInstantExtendedAPIEnabled(); | 172 const bool extended_api_enabled = IsInstantExtendedAPIEnabled(); |
161 | 173 if (extended_api_enabled && !IsSuitableURLForInstant(url, template_url)) |
162 if (!url.is_valid()) | |
163 return false; | 174 return false; |
164 | 175 |
165 if (extended_api_enabled && !url.SchemeIsSecure()) | 176 const TemplateURLRef& instant_url_ref = template_url->instant_url_ref(); |
166 return false; | |
167 | |
168 if (extended_api_enabled && !template_url->HasSearchTermsReplacementKey(url)) | |
169 return false; | |
170 | |
171 const GURL instant_url = | 177 const GURL instant_url = |
172 TemplateURLRefToGURL(instant_url_ref, kDisableStartMargin); | 178 TemplateURLRefToGURL(instant_url_ref, kDisableStartMargin); |
173 if (!instant_url.is_valid()) | 179 return instant_url.is_valid() && |
174 return false; | 180 (MatchesOriginAndPath(url, instant_url) || |
175 | 181 (extended_api_enabled && MatchesAnySearchURL(url, template_url))); |
176 if (MatchesOriginAndPath(url, instant_url)) | |
177 return true; | |
178 | |
179 if (extended_api_enabled && MatchesAnySearchURL(url, template_url)) | |
180 return true; | |
181 | |
182 return false; | |
183 } | 182 } |
184 | 183 |
185 string16 GetSearchTermsImpl(const content::WebContents* contents, | 184 string16 GetSearchTermsImpl(const content::WebContents* contents, |
186 const content::NavigationEntry* entry) { | 185 const content::NavigationEntry* entry) { |
187 if (!IsQueryExtractionEnabled()) | 186 if (!IsQueryExtractionEnabled()) |
188 return string16(); | 187 return string16(); |
189 | 188 |
190 // For security reasons, don't extract search terms if the page is not being | 189 // For security reasons, don't extract search terms if the page is not being |
191 // rendered in the privileged Instant renderer process. This is to protect | 190 // rendered in the privileged Instant renderer process. This is to protect |
192 // against a malicious page somehow scripting the search results page and | 191 // against a malicious page somehow scripting the search results page and |
193 // faking search terms in the URL. Random pages can't get into the Instant | 192 // faking search terms in the URL. Random pages can't get into the Instant |
194 // renderer and scripting doesn't work cross-process, so if the page is in | 193 // renderer and scripting doesn't work cross-process, so if the page is in |
195 // the Instant process, we know it isn't being exploited. | 194 // the Instant process, we know it isn't being exploited. |
196 // Since iOS and Android doesn't use the instant framework, these checks are | 195 // Since iOS and Android doesn't use the instant framework, these checks are |
197 // disabled for the two platforms. | 196 // disabled for the two platforms. |
198 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); | 197 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); |
199 #if !defined(OS_IOS) && !defined(OS_ANDROID) | 198 #if !defined(OS_IOS) && !defined(OS_ANDROID) |
200 if (!IsRenderedInInstantProcess(contents, profile) && | 199 if (!IsRenderedInInstantProcess(contents, profile) && |
201 (contents->GetController().GetLastCommittedEntry() == entry || | 200 ((entry == contents->GetController().GetLastCommittedEntry()) || |
202 !ShouldAssignURLToInstantRenderer(entry->GetURL(), profile))) | 201 !ShouldAssignURLToInstantRenderer(entry->GetURL(), profile))) |
203 return string16(); | 202 return string16(); |
204 #endif // !defined(OS_IOS) && !defined(OS_ANDROID) | 203 #endif // !defined(OS_IOS) && !defined(OS_ANDROID) |
205 // Check to see if search terms have already been extracted. | 204 // Check to see if search terms have already been extracted. |
206 string16 search_terms = GetSearchTermsFromNavigationEntry(entry); | 205 string16 search_terms = GetSearchTermsFromNavigationEntry(entry); |
207 if (!search_terms.empty()) | 206 if (!search_terms.empty()) |
208 return search_terms; | 207 return search_terms; |
209 | 208 |
210 // Otherwise, extract from the URL. | 209 // Otherwise, extract from the URL. |
211 return GetSearchTermsFromURL(profile, entry->GetVirtualURL()); | 210 return GetSearchTermsFromURL(profile, entry->GetVirtualURL()); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 } | 252 } |
254 return kEmbeddedPageVersionDisabled; | 253 return kEmbeddedPageVersionDisabled; |
255 } | 254 } |
256 | 255 |
257 bool IsQueryExtractionEnabled() { | 256 bool IsQueryExtractionEnabled() { |
258 return EmbeddedSearchPageVersion() != kEmbeddedPageVersionDisabled; | 257 return EmbeddedSearchPageVersion() != kEmbeddedPageVersionDisabled; |
259 } | 258 } |
260 | 259 |
261 string16 GetSearchTermsFromURL(Profile* profile, const GURL& url) { | 260 string16 GetSearchTermsFromURL(Profile* profile, const GURL& url) { |
262 string16 search_terms; | 261 string16 search_terms; |
263 | |
264 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); | 262 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); |
265 if (!template_url) | 263 if (template_url && IsSuitableURLForInstant(url, template_url)) |
266 return string16(); | |
267 | |
268 if (url.SchemeIsSecure() && template_url->HasSearchTermsReplacementKey(url)) | |
269 template_url->ExtractSearchTermsFromURL(url, &search_terms); | 264 template_url->ExtractSearchTermsFromURL(url, &search_terms); |
270 | |
271 return search_terms; | 265 return search_terms; |
272 } | 266 } |
273 | 267 |
274 string16 GetSearchTermsFromNavigationEntry( | 268 string16 GetSearchTermsFromNavigationEntry( |
275 const content::NavigationEntry* entry) { | 269 const content::NavigationEntry* entry) { |
276 string16 search_terms; | 270 string16 search_terms; |
277 if (entry) | 271 if (entry) |
278 entry->GetExtraData(sessions::kSearchTermsKey, &search_terms); | 272 entry->GetExtraData(sessions::kSearchTermsKey, &search_terms); |
279 return search_terms; | 273 return search_terms; |
280 } | 274 } |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
413 return l10n_util::GetStringUTF16(IDS_INSTANT_CHECKBOX_PREDICTION_DISABLED); | 407 return l10n_util::GetStringUTF16(IDS_INSTANT_CHECKBOX_PREDICTION_DISABLED); |
414 | 408 |
415 DCHECK(IsInstantCheckboxEnabled(profile)); | 409 DCHECK(IsInstantCheckboxEnabled(profile)); |
416 return l10n_util::GetStringUTF16(IDS_INSTANT_CHECKBOX_ENABLED); | 410 return l10n_util::GetStringUTF16(IDS_INSTANT_CHECKBOX_ENABLED); |
417 } | 411 } |
418 | 412 |
419 GURL GetInstantURL(Profile* profile, int start_margin) { | 413 GURL GetInstantURL(Profile* profile, int start_margin) { |
420 if (!IsInstantCheckboxEnabled(profile)) | 414 if (!IsInstantCheckboxEnabled(profile)) |
421 return GURL(); | 415 return GURL(); |
422 | 416 |
| 417 // In non-extended mode, the checkbox must be checked. |
423 const bool extended_api_enabled = IsInstantExtendedAPIEnabled(); | 418 const bool extended_api_enabled = IsInstantExtendedAPIEnabled(); |
424 | |
425 // In non-extended mode, the checkbox must be checked. | |
426 if (!extended_api_enabled && !IsInstantCheckboxChecked(profile)) | 419 if (!extended_api_enabled && !IsInstantCheckboxChecked(profile)) |
427 return GURL(); | 420 return GURL(); |
428 | 421 |
429 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); | 422 TemplateURL* template_url = GetDefaultSearchProviderTemplateURL(profile); |
430 GURL instant_url = | 423 GURL instant_url = |
431 TemplateURLRefToGURL(template_url->instant_url_ref(), start_margin); | 424 TemplateURLRefToGURL(template_url->instant_url_ref(), start_margin); |
432 if (extended_api_enabled && !instant_url.SchemeIsSecure()) { | |
433 // Extended mode requires HTTPS. Force it if necessary. | |
434 const std::string secure_scheme = chrome::kHttpsScheme; | |
435 GURL::Replacements replacements; | |
436 replacements.SetSchemeStr(secure_scheme); | |
437 instant_url = instant_url.ReplaceComponents(replacements); | |
438 } | |
439 | 425 |
440 return instant_url; | 426 // Extended mode requires HTTPS. Force it. |
| 427 if (!extended_api_enabled || instant_url.SchemeIsSecure()) |
| 428 return instant_url; |
| 429 GURL::Replacements replacements; |
| 430 const std::string secure_scheme(chrome::kHttpsScheme); |
| 431 replacements.SetSchemeStr(secure_scheme); |
| 432 return instant_url.ReplaceComponents(replacements); |
441 } | 433 } |
442 | 434 |
443 GURL GetLocalInstantURL(Profile* profile) { | 435 GURL GetLocalInstantURL(Profile* profile) { |
444 const TemplateURL* default_provider = | 436 const TemplateURL* default_provider = |
445 GetDefaultSearchProviderTemplateURL(profile); | 437 GetDefaultSearchProviderTemplateURL(profile); |
446 | 438 |
447 if (default_provider && | 439 if (default_provider && |
448 (TemplateURLPrepopulateData::GetEngineType(default_provider->url()) == | 440 (TemplateURLPrepopulateData::GetEngineType(default_provider->url()) == |
449 SEARCH_ENGINE_GOOGLE)) { | 441 SEARCH_ENGINE_GOOGLE)) { |
450 return GURL(chrome::kChromeSearchLocalGoogleNtpUrl); | 442 return GURL(chrome::kChromeSearchLocalGoogleNtpUrl); |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
658 return instant_url.is_valid() && | 650 return instant_url.is_valid() && |
659 (!IsInstantExtendedAPIEnabled() || | 651 (!IsInstantExtendedAPIEnabled() || |
660 template_url->HasSearchTermsReplacementKey(instant_url)); | 652 template_url->HasSearchTermsReplacementKey(instant_url)); |
661 } | 653 } |
662 | 654 |
663 void ResetInstantExtendedOptInStateGateForTest() { | 655 void ResetInstantExtendedOptInStateGateForTest() { |
664 instant_extended_opt_in_state_gate = false; | 656 instant_extended_opt_in_state_gate = false; |
665 } | 657 } |
666 | 658 |
667 } // namespace chrome | 659 } // namespace chrome |
OLD | NEW |