| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 package org.chromium.chrome.browser.contextualsearch; | 5 package org.chromium.chrome.browser.contextualsearch; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.text.TextUtils; | 8 import android.text.TextUtils; |
| 9 | 9 |
| 10 import org.chromium.base.VisibleForTesting; | 10 import org.chromium.base.VisibleForTesting; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 import javax.annotation.Nullable; | 23 import javax.annotation.Nullable; |
| 24 | 24 |
| 25 | 25 |
| 26 /** | 26 /** |
| 27 * Handles policy decisions for the {@code ContextualSearchManager}. | 27 * Handles policy decisions for the {@code ContextualSearchManager}. |
| 28 */ | 28 */ |
| 29 class ContextualSearchPolicy { | 29 class ContextualSearchPolicy { |
| 30 private static final Pattern CONTAINS_WHITESPACE_PATTERN = Pattern.compile("
\\s"); | 30 private static final Pattern CONTAINS_WHITESPACE_PATTERN = Pattern.compile("
\\s"); |
| 31 private static final int REMAINING_NOT_APPLICABLE = -1; | 31 private static final int REMAINING_NOT_APPLICABLE = -1; |
| 32 private static final int ONE_DAY_IN_MILLIS = 24 * 60 * 60 * 1000; | 32 private static final int ONE_DAY_IN_MILLIS = 24 * 60 * 60 * 1000; |
| 33 private static final int TAP_RESOLVE_LIMIT_FOR_DECIDED = 50; |
| 34 private static final int TAP_PREFETCH_LIMIT_FOR_DECIDED = 50; |
| 35 private static final int TAP_RESOLVE_LIMIT_FOR_UNDECIDED = 20; |
| 36 private static final int TAP_PREFETCH_LIMIT_FOR_UNDECIDED = 20; |
| 33 | 37 |
| 34 private static ContextualSearchPolicy sInstance; | 38 private static ContextualSearchPolicy sInstance; |
| 35 | 39 |
| 36 private final ChromePreferenceManager mPreferenceManager; | 40 private final ChromePreferenceManager mPreferenceManager; |
| 37 | 41 |
| 38 // Members used only for testing purposes. | 42 // Members used only for testing purposes. |
| 39 private boolean mDidOverrideDecidedStateForTesting; | 43 private boolean mDidOverrideDecidedStateForTesting; |
| 40 private boolean mDecidedStateForTesting; | 44 private boolean mDecidedStateForTesting; |
| 41 private boolean mDidResetCounters; | 45 private boolean mDidResetCounters; |
| 46 private Integer mTapResolveLimitForDecided; |
| 47 private Integer mTapPrefetchLimitForDecided; |
| 48 private Integer mTapResolveLimitForUndecided; |
| 49 private Integer mTapPrefetchLimitForUndecided; |
| 42 | 50 |
| 43 public static ContextualSearchPolicy getInstance(Context context) { | 51 public static ContextualSearchPolicy getInstance(Context context) { |
| 44 if (sInstance == null) { | 52 if (sInstance == null) { |
| 45 sInstance = new ContextualSearchPolicy(context); | 53 sInstance = new ContextualSearchPolicy(context); |
| 46 } | 54 } |
| 47 return sInstance; | 55 return sInstance; |
| 48 } | 56 } |
| 49 | 57 |
| 50 /** | 58 /** |
| 51 * Private constructor -- use {@link #getInstance} to get the singleton inst
ance. | 59 * Private constructor -- use {@link #getInstance} to get the singleton inst
ance. |
| (...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 422 } | 430 } |
| 423 } | 431 } |
| 424 | 432 |
| 425 /** | 433 /** |
| 426 * @return Whether translation should be enabled or not. | 434 * @return Whether translation should be enabled or not. |
| 427 */ | 435 */ |
| 428 boolean isTranslationEnabled() { | 436 boolean isTranslationEnabled() { |
| 429 return ContextualSearchFieldTrial.isTranslationOneboxEnabled(); | 437 return ContextualSearchFieldTrial.isTranslationOneboxEnabled(); |
| 430 } | 438 } |
| 431 | 439 |
| 440 @VisibleForTesting |
| 441 void setTapResolveLimitForDecidedForTesting(int limit) { |
| 442 mTapResolveLimitForDecided = limit; |
| 443 } |
| 444 |
| 445 @VisibleForTesting |
| 446 void setTapPrefetchLimitForDecidedForTesting(int limit) { |
| 447 mTapPrefetchLimitForDecided = limit; |
| 448 } |
| 449 |
| 450 @VisibleForTesting |
| 451 void setTapPrefetchLimitForUndecidedForTesting(int limit) { |
| 452 mTapPrefetchLimitForUndecided = limit; |
| 453 } |
| 454 |
| 455 @VisibleForTesting |
| 456 void setTapResolveLimitForUndecidedForTesting(int limit) { |
| 457 mTapResolveLimitForUndecided = limit; |
| 458 } |
| 459 |
| 432 // -------------------------------------------------------------------------
------------------- | 460 // -------------------------------------------------------------------------
------------------- |
| 433 // Private helpers. | 461 // Private helpers. |
| 434 // -------------------------------------------------------------------------
------------------- | 462 // -------------------------------------------------------------------------
------------------- |
| 435 | 463 |
| 436 /** | 464 /** |
| 437 * @return Whether a promo is needed because the user is still undecided | 465 * @return Whether a promo is needed because the user is still undecided |
| 438 * on enabling or disabling the feature. | 466 * on enabling or disabling the feature. |
| 439 */ | 467 */ |
| 440 private boolean isUserUndecided() { | 468 private boolean isUserUndecided() { |
| 441 // TODO(donnd) use dependency injection for the PrefServiceBridge instea
d! | 469 // TODO(donnd) use dependency injection for the PrefServiceBridge instea
d! |
| 442 if (mDidOverrideDecidedStateForTesting) return !mDecidedStateForTesting; | 470 if (mDidOverrideDecidedStateForTesting) return !mDecidedStateForTesting; |
| 443 | 471 |
| 444 return PrefServiceBridge.getInstance().isContextualSearchUninitialized()
; | 472 return PrefServiceBridge.getInstance().isContextualSearchUninitialized()
; |
| 445 } | 473 } |
| 446 | 474 |
| 447 /** | 475 /** |
| 448 * @param url The URL of the base page. | 476 * @param url The URL of the base page. |
| 449 * @return Whether the given content view is for an HTTP page. | 477 * @return Whether the given content view is for an HTTP page. |
| 450 */ | 478 */ |
| 451 private boolean isBasePageHTTP(@Nullable URL url) { | 479 private boolean isBasePageHTTP(@Nullable URL url) { |
| 452 return url != null && "http".equals(url.getProtocol()); | 480 return url != null && "http".equals(url.getProtocol()); |
| 453 } | 481 } |
| 454 | 482 |
| 455 /** | 483 /** |
| 456 * @return Whether the tap resolve limit has been exceeded. | 484 * @return Whether the tap resolve limit has been exceeded. |
| 457 */ | 485 */ |
| 458 private boolean isTapResolveBeyondTheLimit() { | 486 private boolean isTapResolveBeyondTheLimit() { |
| 459 return isTapResolveLimited() && getTapCount() > getTapResolveLimit(); | 487 return getTapCount() > getTapResolveLimit(); |
| 460 } | 488 } |
| 461 | 489 |
| 462 /** | 490 /** |
| 463 * @return Whether the tap resolve limit has been exceeded. | 491 * @return Whether the tap resolve limit has been exceeded. |
| 464 */ | 492 */ |
| 465 private boolean isTapPrefetchBeyondTheLimit() { | 493 private boolean isTapPrefetchBeyondTheLimit() { |
| 466 return isTapPrefetchLimited() && getTapCount() > getTapPrefetchLimit(); | 494 return getTapCount() > getTapPrefetchLimit(); |
| 467 } | 495 } |
| 468 | 496 |
| 469 /** | 497 /** |
| 470 * @return Whether a tap gesture is resolve-limited. | |
| 471 */ | |
| 472 private boolean isTapResolveLimited() { | |
| 473 return isUserUndecided() | |
| 474 ? ContextualSearchFieldTrial.isTapResolveLimitedForUndecided() | |
| 475 : ContextualSearchFieldTrial.isTapResolveLimitedForDecided(); | |
| 476 } | |
| 477 | |
| 478 /** | |
| 479 * @return Whether a tap gesture is resolve-limited. | |
| 480 */ | |
| 481 private boolean isTapPrefetchLimited() { | |
| 482 return isUserUndecided() | |
| 483 ? ContextualSearchFieldTrial.isTapPrefetchLimitedForUndecided() | |
| 484 : ContextualSearchFieldTrial.isTapPrefetchLimitedForDecided(); | |
| 485 } | |
| 486 | |
| 487 /** | |
| 488 * @return The limit of the number of taps to prefetch. | 498 * @return The limit of the number of taps to prefetch. |
| 489 */ | 499 */ |
| 490 private int getTapPrefetchLimit() { | 500 private int getTapPrefetchLimit() { |
| 491 return isUserUndecided() | 501 return isUserUndecided() |
| 492 ? ContextualSearchFieldTrial.getTapPrefetchLimitForUndecided() | 502 ? getTapPrefetchLimitForUndecided() |
| 493 : ContextualSearchFieldTrial.getTapPrefetchLimitForDecided(); | 503 : getTapPrefetchLimitForDecided(); |
| 494 } | 504 } |
| 495 | 505 |
| 496 /** | 506 /** |
| 497 * @return The limit of the number of taps to resolve using search term reso
lution. | 507 * @return The limit of the number of taps to resolve using search term reso
lution. |
| 498 */ | 508 */ |
| 499 private int getTapResolveLimit() { | 509 private int getTapResolveLimit() { |
| 500 return isUserUndecided() | 510 return isUserUndecided() |
| 501 ? ContextualSearchFieldTrial.getTapResolveLimitForUndecided() | 511 ? getTapResolveLimitForUndecided() |
| 502 : ContextualSearchFieldTrial.getTapResolveLimitForDecided(); | 512 : getTapResolveLimitForDecided(); |
| 513 } |
| 514 |
| 515 private int getTapPrefetchLimitForDecided() { |
| 516 if (mTapPrefetchLimitForDecided != null) return mTapPrefetchLimitForDeci
ded.intValue(); |
| 517 return TAP_PREFETCH_LIMIT_FOR_DECIDED; |
| 518 } |
| 519 |
| 520 private int getTapResolveLimitForDecided() { |
| 521 if (mTapResolveLimitForDecided != null) return mTapResolveLimitForDecide
d.intValue(); |
| 522 return TAP_RESOLVE_LIMIT_FOR_DECIDED; |
| 523 } |
| 524 |
| 525 private int getTapPrefetchLimitForUndecided() { |
| 526 if (mTapPrefetchLimitForUndecided != null) return mTapPrefetchLimitForUn
decided.intValue(); |
| 527 return TAP_PREFETCH_LIMIT_FOR_UNDECIDED; |
| 528 } |
| 529 |
| 530 private int getTapResolveLimitForUndecided() { |
| 531 if (mTapResolveLimitForUndecided != null) return mTapResolveLimitForUnde
cided.intValue(); |
| 532 return TAP_RESOLVE_LIMIT_FOR_UNDECIDED; |
| 503 } | 533 } |
| 504 } | 534 } |
| OLD | NEW |