| 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.annotation.SuppressLint; | 7 import android.annotation.SuppressLint; |
| 8 import android.os.Handler; | 8 import android.os.Handler; |
| 9 import android.text.TextUtils; | 9 import android.text.TextUtils; |
| 10 import android.view.View; | 10 import android.view.View; |
| (...skipping 413 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 424 | 424 |
| 425 String selection = mSelectionController.getSelectedText(); | 425 String selection = mSelectionController.getSelectedText(); |
| 426 boolean isTap = mSelectionController.getSelectionType() == SelectionType
.TAP; | 426 boolean isTap = mSelectionController.getSelectionType() == SelectionType
.TAP; |
| 427 if (isTap) { | 427 if (isTap) { |
| 428 // If the user action was not a long-press, we should not delay befo
re loading content. | 428 // If the user action was not a long-press, we should not delay befo
re loading content. |
| 429 mShouldLoadDelayedSearch = false; | 429 mShouldLoadDelayedSearch = false; |
| 430 } | 430 } |
| 431 if (isTap && mPolicy.shouldPreviousTapResolve()) { | 431 if (isTap && mPolicy.shouldPreviousTapResolve()) { |
| 432 // Cache the native translate data, so JNI calls won't be made when
time-critical. | 432 // Cache the native translate data, so JNI calls won't be made when
time-critical. |
| 433 mTranslateController.cacheNativeTranslateData(); | 433 mTranslateController.cacheNativeTranslateData(); |
| 434 } else { | 434 } else if (!TextUtils.isEmpty(selection)) { |
| 435 boolean shouldPrefetch = mPolicy.shouldPrefetchSearchResult(); | 435 boolean shouldPrefetch = mPolicy.shouldPrefetchSearchResult(); |
| 436 mSearchRequest = createContextualSearchRequest(selection, null, null
, shouldPrefetch); | 436 mSearchRequest = new ContextualSearchRequest(selection, null, null,
shouldPrefetch); |
| 437 mTranslateController.forceAutoDetectTranslateUnlessDisabled(mSearchR
equest); | 437 mTranslateController.forceAutoDetectTranslateUnlessDisabled(mSearchR
equest); |
| 438 mDidStartLoadingResolvedSearchRequest = false; | 438 mDidStartLoadingResolvedSearchRequest = false; |
| 439 mSearchPanel.setSearchTerm(selection); | 439 mSearchPanel.setSearchTerm(selection); |
| 440 if (shouldPrefetch) loadSearchUrl(); | 440 if (shouldPrefetch) loadSearchUrl(); |
| 441 | 441 |
| 442 // Record metrics for manual refinement of the search term from long
-press. | 442 // Record metrics for manual refinement of the search term from long
-press. |
| 443 // TODO(donnd): remove this section once metrics have been analyzed. | 443 // TODO(donnd): remove this section once metrics have been analyzed. |
| 444 if (!isTap && mSearchPanel.isPeeking()) { | 444 if (!isTap && mSearchPanel.isPeeking()) { |
| 445 boolean isSingleWord = | 445 boolean isSingleWord = |
| 446 !CONTAINS_WHITESPACE_PATTERN.matcher(selection.trim()).f
ind(); | 446 !CONTAINS_WHITESPACE_PATTERN.matcher(selection.trim()).f
ind(); |
| 447 RecordUserAction.record(isSingleWord ? "ContextualSearch.ManualR
efineSingleWord" | 447 RecordUserAction.record(isSingleWord ? "ContextualSearch.ManualR
efineSingleWord" |
| 448 : "ContextualSearch.ManualR
efineMultiWord"); | 448 : "ContextualSearch.ManualR
efineMultiWord"); |
| 449 } | 449 } |
| 450 } else { |
| 451 // The selection is no longer valid, so we can't build a request. D
on't show the UX. |
| 452 hideContextualSearch(StateChangeReason.UNKNOWN); |
| 453 return; |
| 450 } | 454 } |
| 451 mWereSearchResultsSeen = false; | 455 mWereSearchResultsSeen = false; |
| 452 | 456 |
| 453 // Show the Peek Promo only when the Panel wasn't previously visible, pr
ovided | 457 // Show the Peek Promo only when the Panel wasn't previously visible, pr
ovided |
| 454 // the policy allows it. | 458 // the policy allows it. |
| 455 if (!mSearchPanel.isShowing()) { | 459 if (!mSearchPanel.isShowing()) { |
| 456 mWouldShowPeekPromo = mPolicy.isPeekPromoConditionSatisfied(); | 460 mWouldShowPeekPromo = mPolicy.isPeekPromoConditionSatisfied(); |
| 457 mIsShowingPeekPromo = mPolicy.isPeekPromoAvailable(); | 461 mIsShowingPeekPromo = mPolicy.isPeekPromoAvailable(); |
| 458 if (mIsShowingPeekPromo) { | 462 if (mIsShowingPeekPromo) { |
| 459 mSearchPanel.showPeekPromo(); | 463 mSearchPanel.showPeekPromo(); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 496 WebContents baseWebContents = getBaseWebContents(); | 500 WebContents baseWebContents = getBaseWebContents(); |
| 497 if (baseWebContents == null) return null; | 501 if (baseWebContents == null) return null; |
| 498 | 502 |
| 499 try { | 503 try { |
| 500 return new URL(baseWebContents.getUrl()); | 504 return new URL(baseWebContents.getUrl()); |
| 501 } catch (MalformedURLException e) { | 505 } catch (MalformedURLException e) { |
| 502 return null; | 506 return null; |
| 503 } | 507 } |
| 504 } | 508 } |
| 505 | 509 |
| 506 /** | |
| 507 * A method that can override the creation of a standard search request. Thi
s should only be | |
| 508 * used for testing. | |
| 509 * @param term The search term to create the request with. | |
| 510 * @param altTerm An alternate search term. | |
| 511 * @param isLowPriorityEnabled Whether the request can be made at low priori
ty. | |
| 512 */ | |
| 513 protected ContextualSearchRequest createContextualSearchRequest( | |
| 514 String term, String altTerm, String mid, boolean isLowPriorityEnable
d) { | |
| 515 return new ContextualSearchRequest(term, altTerm, mid, isLowPriorityEnab
led); | |
| 516 } | |
| 517 | |
| 518 /** Accessor for the {@code InfoBarContainer} currently attached to the {@co
de Tab}. */ | 510 /** Accessor for the {@code InfoBarContainer} currently attached to the {@co
de Tab}. */ |
| 519 private InfoBarContainer getInfoBarContainer() { | 511 private InfoBarContainer getInfoBarContainer() { |
| 520 Tab tab = mActivity.getActivityTab(); | 512 Tab tab = mActivity.getActivityTab(); |
| 521 return tab == null ? null : tab.getInfoBarContainer(); | 513 return tab == null ? null : tab.getInfoBarContainer(); |
| 522 } | 514 } |
| 523 | 515 |
| 524 /** Listens for notifications that should hide the Contextual Search bar. */ | 516 /** Listens for notifications that should hide the Contextual Search bar. */ |
| 525 private void listenForTabModelSelectorNotifications() { | 517 private void listenForTabModelSelectorNotifications() { |
| 526 TabModelSelector selector = mActivity.getTabModelSelector(); | 518 TabModelSelector selector = mActivity.getTabModelSelector(); |
| 527 | 519 |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 703 if (doLiteralSearch) { | 695 if (doLiteralSearch) { |
| 704 searchTerm = mSelectionController.getSelectedText(); | 696 searchTerm = mSelectionController.getSelectedText(); |
| 705 alternateTerm = null; | 697 alternateTerm = null; |
| 706 doPreventPreload = true; | 698 doPreventPreload = true; |
| 707 } | 699 } |
| 708 if (!TextUtils.isEmpty(searchTerm)) { | 700 if (!TextUtils.isEmpty(searchTerm)) { |
| 709 // TODO(donnd): Instead of preloading, we should prefetch (ie the UR
L should not | 701 // TODO(donnd): Instead of preloading, we should prefetch (ie the UR
L should not |
| 710 // appear in the user's history until the user views it). See crbug
.com/406446. | 702 // appear in the user's history until the user views it). See crbug
.com/406446. |
| 711 boolean shouldPreload = !doPreventPreload && mPolicy.shouldPrefetchS
earchResult(); | 703 boolean shouldPreload = !doPreventPreload && mPolicy.shouldPrefetchS
earchResult(); |
| 712 mSearchRequest = | 704 mSearchRequest = |
| 713 createContextualSearchRequest(searchTerm, alternateTerm, mid
, shouldPreload); | 705 new ContextualSearchRequest(searchTerm, alternateTerm, mid,
shouldPreload); |
| 714 // Trigger translation, if enabled. | 706 // Trigger translation, if enabled. |
| 715 mTranslateController.forceTranslateIfNeeded(mSearchRequest, contextL
anguage); | 707 mTranslateController.forceTranslateIfNeeded(mSearchRequest, contextL
anguage); |
| 716 mDidStartLoadingResolvedSearchRequest = false; | 708 mDidStartLoadingResolvedSearchRequest = false; |
| 717 if (mSearchPanel.isContentShowing()) { | 709 if (mSearchPanel.isContentShowing()) { |
| 718 mSearchRequest.setNormalPriority(); | 710 mSearchRequest.setNormalPriority(); |
| 719 } | 711 } |
| 720 if (mSearchPanel.isContentShowing() || shouldPreload) { | 712 if (mSearchPanel.isContentShowing() || shouldPreload) { |
| 721 loadSearchUrl(); | 713 loadSearchUrl(); |
| 722 } | 714 } |
| 723 mPolicy.logSearchTermResolutionDetails(searchTerm); | 715 mPolicy.logSearchTermResolutionDetails(searchTerm); |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 941 public void onContentLoadStarted(String url) { | 933 public void onContentLoadStarted(String url) { |
| 942 mDidPromoteSearchNavigation = false; | 934 mDidPromoteSearchNavigation = false; |
| 943 } | 935 } |
| 944 | 936 |
| 945 @Override | 937 @Override |
| 946 public void onVisibilityChanged(boolean isVisible) { | 938 public void onVisibilityChanged(boolean isVisible) { |
| 947 if (isVisible) { | 939 if (isVisible) { |
| 948 mWereSearchResultsSeen = true; | 940 mWereSearchResultsSeen = true; |
| 949 // If there's no current request, then either a search term reso
lution | 941 // If there's no current request, then either a search term reso
lution |
| 950 // is in progress or we should do a verbatim search now. | 942 // is in progress or we should do a verbatim search now. |
| 951 if (mSearchRequest == null && mPolicy.shouldCreateVerbatimReques
t()) { | 943 if (mSearchRequest == null && mPolicy.shouldCreateVerbatimReques
t() |
| 952 mSearchRequest = createContextualSearchRequest( | 944 && !TextUtils.isEmpty(mSelectionController.getSelectedTe
xt())) { |
| 945 mSearchRequest = new ContextualSearchRequest( |
| 953 mSelectionController.getSelectedText(), null, null,
false); | 946 mSelectionController.getSelectedText(), null, null,
false); |
| 954 mDidStartLoadingResolvedSearchRequest = false; | 947 mDidStartLoadingResolvedSearchRequest = false; |
| 955 } | 948 } |
| 956 if (mSearchRequest != null | 949 if (mSearchRequest != null |
| 957 && (!mDidStartLoadingResolvedSearchRequest || mShouldLoa
dDelayedSearch)) { | 950 && (!mDidStartLoadingResolvedSearchRequest || mShouldLoa
dDelayedSearch)) { |
| 958 // mShouldLoadDelayedSearch is used in the long-press case t
o load content. | 951 // mShouldLoadDelayedSearch is used in the long-press case t
o load content. |
| 959 // Since content is now created and destroyed for each reque
st, was impossible | 952 // Since content is now created and destroyed for each reque
st, was impossible |
| 960 // to know if content was already loaded or recently needed
to be; this is for | 953 // to know if content was already loaded or recently needed
to be; this is for |
| 961 // the case where it needed to be. | 954 // the case where it needed to be. |
| 962 mSearchRequest.setNormalPriority(); | 955 mSearchRequest.setNormalPriority(); |
| (...skipping 674 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1637 private native void nativeStartSearchTermResolutionRequest(long nativeContex
tualSearchManager, | 1630 private native void nativeStartSearchTermResolutionRequest(long nativeContex
tualSearchManager, |
| 1638 ContextualSearchContext contextualSearchContext, WebContents baseWeb
Contents); | 1631 ContextualSearchContext contextualSearchContext, WebContents baseWeb
Contents); |
| 1639 protected native void nativeGatherSurroundingText(long nativeContextualSearc
hManager, | 1632 protected native void nativeGatherSurroundingText(long nativeContextualSearc
hManager, |
| 1640 ContextualSearchContext contextualSearchContext, WebContents baseWeb
Contents); | 1633 ContextualSearchContext contextualSearchContext, WebContents baseWeb
Contents); |
| 1641 private native void nativeEnableContextualSearchJsApiForOverlay( | 1634 private native void nativeEnableContextualSearchJsApiForOverlay( |
| 1642 long nativeContextualSearchManager, WebContents overlayWebContents); | 1635 long nativeContextualSearchManager, WebContents overlayWebContents); |
| 1643 // Don't call these directly, instead call the private methods that cache th
e results. | 1636 // Don't call these directly, instead call the private methods that cache th
e results. |
| 1644 private native String nativeGetTargetLanguage(long nativeContextualSearchMan
ager); | 1637 private native String nativeGetTargetLanguage(long nativeContextualSearchMan
ager); |
| 1645 private native String nativeGetAcceptLanguages(long nativeContextualSearchMa
nager); | 1638 private native String nativeGetAcceptLanguages(long nativeContextualSearchMa
nager); |
| 1646 } | 1639 } |
| OLD | NEW |