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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 namespace { | 46 namespace { |
47 | 47 |
48 // Configuration options for Embedded Search. | 48 // Configuration options for Embedded Search. |
49 // EmbeddedSearch field trials are named in such a way that we can parse out | 49 // EmbeddedSearch field trials are named in such a way that we can parse out |
50 // the experiment configuration from the trial's group name in order to give | 50 // the experiment configuration from the trial's group name in order to give |
51 // us maximum flexability in running experiments. | 51 // us maximum flexability in running experiments. |
52 // Field trial groups should be named things like "Group7 espv:2 instant:1". | 52 // Field trial groups should be named things like "Group7 espv:2 instant:1". |
53 // The first token is always GroupN for some integer N, followed by a | 53 // The first token is always GroupN for some integer N, followed by a |
54 // space-delimited list of key:value pairs which correspond to these flags: | 54 // space-delimited list of key:value pairs which correspond to these flags: |
55 const char kEmbeddedPageVersionFlagName[] = "espv"; | 55 const char kEmbeddedPageVersionFlagName[] = "espv"; |
56 #if defined(OS_IOS) || defined(OS_ANDROID) | 56 #if defined(OS_IOS) |
57 const uint64 kEmbeddedPageVersionDefault = 1; | 57 const uint64 kEmbeddedPageVersionDefault = 1; |
| 58 #elif defined(OS_ANDROID) |
| 59 const uint64 kEmbeddedPageVersionDefault = 1; |
| 60 // Use this variant to enable EmbeddedSearch SearchBox API in the results page. |
| 61 const uint64 kEmbeddedSearchEnabledVersion = 2; |
58 #else | 62 #else |
59 const uint64 kEmbeddedPageVersionDefault = 2; | 63 const uint64 kEmbeddedPageVersionDefault = 2; |
60 #endif | 64 #endif |
61 | 65 |
62 const char kHideVerbatimFlagName[] = "hide_verbatim"; | 66 const char kHideVerbatimFlagName[] = "hide_verbatim"; |
63 const char kPrefetchSearchResultsFlagName[] = "prefetch_results"; | 67 const char kPrefetchSearchResultsFlagName[] = "prefetch_results"; |
64 const char kPrefetchSearchResultsOnSRP[] = "prefetch_results_srp"; | 68 const char kPrefetchSearchResultsOnSRP[] = "prefetch_results_srp"; |
65 | 69 |
66 // Controls whether to reuse prerendered Instant Search base page to commit any | 70 // Controls whether to reuse prerendered Instant Search base page to commit any |
67 // search query. | 71 // search query. |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 const content::NavigationEntry* entry) { | 253 const content::NavigationEntry* entry) { |
250 if (!contents || !IsQueryExtractionEnabled()) | 254 if (!contents || !IsQueryExtractionEnabled()) |
251 return base::string16(); | 255 return base::string16(); |
252 | 256 |
253 // For security reasons, don't extract search terms if the page is not being | 257 // For security reasons, don't extract search terms if the page is not being |
254 // rendered in the privileged Instant renderer process. This is to protect | 258 // rendered in the privileged Instant renderer process. This is to protect |
255 // against a malicious page somehow scripting the search results page and | 259 // against a malicious page somehow scripting the search results page and |
256 // faking search terms in the URL. Random pages can't get into the Instant | 260 // faking search terms in the URL. Random pages can't get into the Instant |
257 // renderer and scripting doesn't work cross-process, so if the page is in | 261 // renderer and scripting doesn't work cross-process, so if the page is in |
258 // the Instant process, we know it isn't being exploited. | 262 // the Instant process, we know it isn't being exploited. |
259 // Since iOS and Android doesn't use the instant framework, these checks are | |
260 // disabled for the two platforms. | |
261 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); | 263 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); |
262 #if !defined(OS_IOS) && !defined(OS_ANDROID) | 264 if (IsInstantExtendedAPIEnabled() && |
263 if (!IsRenderedInInstantProcess(contents, profile) && | 265 !IsRenderedInInstantProcess(contents, profile) && |
264 ((entry == contents->GetController().GetLastCommittedEntry()) || | 266 ((entry == contents->GetController().GetLastCommittedEntry()) || |
265 !ShouldAssignURLToInstantRenderer(entry->GetURL(), profile))) | 267 !ShouldAssignURLToInstantRenderer(entry->GetURL(), profile))) |
266 return base::string16(); | 268 return base::string16(); |
267 #endif // !defined(OS_IOS) && !defined(OS_ANDROID) | 269 |
268 // Check to see if search terms have already been extracted. | 270 // Check to see if search terms have already been extracted. |
269 base::string16 search_terms = GetSearchTermsFromNavigationEntry(entry); | 271 base::string16 search_terms = GetSearchTermsFromNavigationEntry(entry); |
270 if (!search_terms.empty()) | 272 if (!search_terms.empty()) |
271 return search_terms; | 273 return search_terms; |
272 | 274 |
273 if (!IsQueryExtractionAllowedForURL(profile, entry->GetVirtualURL())) | 275 if (!IsQueryExtractionAllowedForURL(profile, entry->GetVirtualURL())) |
274 return base::string16(); | 276 return base::string16(); |
275 | 277 |
276 // Otherwise, extract from the URL. | 278 // Otherwise, extract from the URL. |
277 return ExtractSearchTermsFromURL(profile, entry->GetVirtualURL()); | 279 return ExtractSearchTermsFromURL(profile, entry->GetVirtualURL()); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 GURL url; | 338 GURL url; |
337 NewTabURLState state; | 339 NewTabURLState state; |
338 }; | 340 }; |
339 | 341 |
340 } // namespace | 342 } // namespace |
341 | 343 |
342 // Negative start-margin values prevent the "es_sm" parameter from being used. | 344 // Negative start-margin values prevent the "es_sm" parameter from being used. |
343 const int kDisableStartMargin = -1; | 345 const int kDisableStartMargin = -1; |
344 | 346 |
345 bool IsInstantExtendedAPIEnabled() { | 347 bool IsInstantExtendedAPIEnabled() { |
346 #if defined(OS_IOS) || defined(OS_ANDROID) | 348 #if defined(OS_IOS) |
347 return false; | 349 return false; |
| 350 #elif defined(OS_ANDROID) |
| 351 return EmbeddedSearchPageVersion() == kEmbeddedSearchEnabledVersion; |
348 #else | 352 #else |
349 return true; | 353 return true; |
350 #endif // defined(OS_IOS) || defined(OS_ANDROID) | 354 #endif // defined(OS_IOS) |
351 } | 355 } |
352 | 356 |
353 // Determine what embedded search page version to request from the user's | 357 // Determine what embedded search page version to request from the user's |
354 // default search provider. If 0, the embedded search UI should not be enabled. | 358 // default search provider. If 0, the embedded search UI should not be enabled. |
355 uint64 EmbeddedSearchPageVersion() { | 359 uint64 EmbeddedSearchPageVersion() { |
| 360 #if defined(OS_ANDROID) |
| 361 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 362 switches::kEnableEmbeddedSearchAPI)) { |
| 363 return kEmbeddedSearchEnabledVersion; |
| 364 } |
| 365 #endif |
| 366 |
356 FieldTrialFlags flags; | 367 FieldTrialFlags flags; |
357 if (GetFieldTrialInfo(&flags)) { | 368 if (GetFieldTrialInfo(&flags)) { |
358 return GetUInt64ValueForFlagWithDefault(kEmbeddedPageVersionFlagName, | 369 return GetUInt64ValueForFlagWithDefault(kEmbeddedPageVersionFlagName, |
359 kEmbeddedPageVersionDefault, | 370 kEmbeddedPageVersionDefault, |
360 flags); | 371 flags); |
361 } | 372 } |
362 return kEmbeddedPageVersionDefault; | 373 return kEmbeddedPageVersionDefault; |
363 } | 374 } |
364 | 375 |
365 std::string InstantExtendedEnabledParam(bool for_search) { | 376 std::string InstantExtendedEnabledParam(bool for_search) { |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
423 | 434 |
424 base::string16 GetSearchTerms(const content::WebContents* contents) { | 435 base::string16 GetSearchTerms(const content::WebContents* contents) { |
425 if (!contents) | 436 if (!contents) |
426 return base::string16(); | 437 return base::string16(); |
427 | 438 |
428 const content::NavigationEntry* entry = | 439 const content::NavigationEntry* entry = |
429 contents->GetController().GetVisibleEntry(); | 440 contents->GetController().GetVisibleEntry(); |
430 if (!entry) | 441 if (!entry) |
431 return base::string16(); | 442 return base::string16(); |
432 | 443 |
433 #if !defined(OS_IOS) && !defined(OS_ANDROID) | 444 if (IsInstantExtendedAPIEnabled()) { |
434 // iOS and Android doesn't use the Instant framework, disable this check for | 445 InstantSupportState state = |
435 // the two platforms. | 446 GetInstantSupportStateFromNavigationEntry(*entry); |
436 InstantSupportState state = GetInstantSupportStateFromNavigationEntry(*entry); | 447 if (state == INSTANT_SUPPORT_NO) |
437 if (state == INSTANT_SUPPORT_NO) | 448 return base::string16(); |
438 return base::string16(); | 449 } |
439 #endif // !defined(OS_IOS) && !defined(OS_ANDROID) | |
440 | 450 |
441 return GetSearchTermsImpl(contents, entry); | 451 return GetSearchTermsImpl(contents, entry); |
442 } | 452 } |
443 | 453 |
444 bool ShouldAssignURLToInstantRenderer(const GURL& url, Profile* profile) { | 454 bool ShouldAssignURLToInstantRenderer(const GURL& url, Profile* profile) { |
445 return url.is_valid() && | 455 return url.is_valid() && |
446 profile && | 456 profile && |
447 IsInstantExtendedAPIEnabled() && | 457 IsInstantExtendedAPIEnabled() && |
448 (url.SchemeIs(chrome::kChromeSearchScheme) || | 458 (url.SchemeIs(chrome::kChromeSearchScheme) || |
449 IsInstantURL(url, profile)); | 459 IsInstantURL(url, profile)); |
(...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
829 | 839 |
830 // Given a FieldTrialFlags object, returns the boolean value of the provided | 840 // Given a FieldTrialFlags object, returns the boolean value of the provided |
831 // flag. | 841 // flag. |
832 bool GetBoolValueForFlagWithDefault(const std::string& flag, | 842 bool GetBoolValueForFlagWithDefault(const std::string& flag, |
833 bool default_value, | 843 bool default_value, |
834 const FieldTrialFlags& flags) { | 844 const FieldTrialFlags& flags) { |
835 return !!GetUInt64ValueForFlagWithDefault(flag, default_value ? 1 : 0, flags); | 845 return !!GetUInt64ValueForFlagWithDefault(flag, default_value ? 1 : 0, flags); |
836 } | 846 } |
837 | 847 |
838 } // namespace chrome | 848 } // namespace chrome |
OLD | NEW |