OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.contextualsearch; |
| 6 |
| 7 import android.net.Uri; |
| 8 |
| 9 import org.chromium.chrome.browser.search_engines.TemplateUrlService; |
| 10 |
| 11 import javax.annotation.Nullable; |
| 12 |
| 13 |
| 14 /** |
| 15 * Bundles a Search Request URL with a low-priority version of the URL, helps ma
nage the |
| 16 * fall-back when the low-priority version fails, and tracks which one is in use
. |
| 17 */ |
| 18 class ContextualSearchRequest { |
| 19 |
| 20 private final Uri mLowPriorityUri; |
| 21 private final Uri mNormalPriorityUri; |
| 22 private final boolean mWasPrefetch; |
| 23 |
| 24 private boolean mIsLowPriority; |
| 25 private boolean mHasFailedLowPriorityLoad; |
| 26 |
| 27 /** |
| 28 * Creates a search request for the given search term without any alternate
term and |
| 29 * for normal-priority loading capability only. |
| 30 * @param searchTerm The resolved search term. |
| 31 */ |
| 32 ContextualSearchRequest(String searchTerm) { |
| 33 this(searchTerm, null, false); |
| 34 } |
| 35 |
| 36 /** |
| 37 * Creates a search request for the given search term with the given alterna
te term and |
| 38 * low-priority loading capability. |
| 39 * @param searchTerm The resolved search term. |
| 40 * @param alternateTerm The alternate search term. |
| 41 * @param isLowPriorityEnabled Whether the request can be made at a low prio
rity. |
| 42 */ |
| 43 ContextualSearchRequest(String searchTerm, @Nullable String alternateTerm, |
| 44 boolean isLowPriorityEnabled) { |
| 45 mWasPrefetch = isLowPriorityEnabled; |
| 46 mNormalPriorityUri = getUriTemplate(searchTerm, alternateTerm, false); |
| 47 if (isLowPriorityEnabled) { |
| 48 // TODO(donnd): Call TemplateURL once we have an API for 3rd-party p
roviders. |
| 49 Uri baseLowPriorityUri = getUriTemplate(searchTerm, alternateTerm, t
rue); |
| 50 mLowPriorityUri = baseLowPriorityUri.buildUpon() |
| 51 .path("s") |
| 52 .appendQueryParameter("sns", "1") |
| 53 .build(); |
| 54 mIsLowPriority = true; |
| 55 } else { |
| 56 mIsLowPriority = false; |
| 57 mLowPriorityUri = null; |
| 58 } |
| 59 } |
| 60 |
| 61 /** |
| 62 * Sets an indicator that the normal-priority URL should be used for this se
arch request. |
| 63 */ |
| 64 void setNormalPriority() { |
| 65 mIsLowPriority = false; |
| 66 } |
| 67 |
| 68 /** |
| 69 * @return whether the low priority URL is being used. |
| 70 */ |
| 71 boolean isUsingLowPriority() { |
| 72 return mIsLowPriority; |
| 73 } |
| 74 |
| 75 /** |
| 76 * @return whether this request started as a prefetch request. |
| 77 */ |
| 78 boolean wasPrefetch() { |
| 79 return mWasPrefetch; |
| 80 } |
| 81 |
| 82 /** |
| 83 * Sets that this search request has failed. |
| 84 */ |
| 85 void setHasFailed() { |
| 86 mHasFailedLowPriorityLoad = true; |
| 87 } |
| 88 |
| 89 /** |
| 90 * @return whether the load has failed for this search request or not. |
| 91 */ |
| 92 boolean getHasFailed() { |
| 93 return mHasFailedLowPriorityLoad; |
| 94 } |
| 95 |
| 96 /** |
| 97 * Gets the search URL for this request. |
| 98 * @return either the low-priority or normal-priority URL for this search re
quest. |
| 99 */ |
| 100 String getSearchUrl() { |
| 101 if (mIsLowPriority && mLowPriorityUri != null) { |
| 102 return mLowPriorityUri.toString(); |
| 103 } else { |
| 104 return mNormalPriorityUri.toString(); |
| 105 } |
| 106 } |
| 107 |
| 108 /** |
| 109 * Uses TemplateUrlService to generate the url for the given query |
| 110 * {@link String} for {@code query} with the contextual search version param
set. |
| 111 * @param query The search term to use as the main query in the returned sea
rch url. |
| 112 * @param alternateTerm The alternate search term to use as an alternate sug
gestion. |
| 113 * @param shouldPrefetch Whether the returned url should include a prefetch
parameter. |
| 114 * @return A {@link String} that contains the url of the default search
engine with |
| 115 * {@code query} and {@code alternateTerm} inserted as paramete
rs and contextual |
| 116 * search and prefetch parameters conditionally set. |
| 117 */ |
| 118 private Uri getUriTemplate(String query, @Nullable String alternateTerm, |
| 119 boolean shouldPrefetch) { |
| 120 return Uri.parse(TemplateUrlService.getInstance().getUrlForContextualSea
rchQuery( |
| 121 query, alternateTerm, shouldPrefetch)); |
| 122 } |
| 123 } |
OLD | NEW |