Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(105)

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/contextualsearch/ContextualSearchRequest.java

Issue 1354763003: [Contextual Search] Trigger the translation one-box. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Updated comment about test infrastructure needing to be updated when shifting to new API. Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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.net.Uri; 7 import android.net.Uri;
8 import android.text.TextUtils;
8 9
10 import org.chromium.base.VisibleForTesting;
9 import org.chromium.chrome.browser.search_engines.TemplateUrlService; 11 import org.chromium.chrome.browser.search_engines.TemplateUrlService;
10 12
11 import java.net.MalformedURLException; 13 import java.net.MalformedURLException;
12 import java.net.URL; 14 import java.net.URL;
15 import java.util.Locale;
13 16
14 import javax.annotation.Nullable; 17 import javax.annotation.Nullable;
15 18
16 19
17 /** 20 /**
18 * Bundles a Search Request URL with a low-priority version of the URL, helps ma nage the 21 * Bundles a Search Request URL with a low-priority version of the URL, helps ma nage the
19 * fall-back when the low-priority version fails, and tracks which one is in use . 22 * fall-back when the low-priority version fails, and tracks which one is in use .
20 */ 23 */
21 class ContextualSearchRequest { 24 class ContextualSearchRequest {
25 private final boolean mWasPrefetch;
22 26
23 private final Uri mLowPriorityUri; 27 private Uri mLowPriorityUri;
24 private final Uri mNormalPriorityUri; 28 private Uri mNormalPriorityUri;
25 private final boolean mWasPrefetch;
26 29
27 private boolean mIsLowPriority; 30 private boolean mIsLowPriority;
28 private boolean mHasFailedLowPriorityLoad; 31 private boolean mHasFailedLowPriorityLoad;
32 private boolean mIsTranslationForced;
29 33
34 private static final String GWS_LOW_PRIORITY_SEARCH_PATH = "s";
35 private static final String GWS_SEARCH_NO_SUGGESTIONS_PARAM = "sns";
36 private static final String GWS_SEARCH_NO_SUGGESTIONS_PARAM_VALUE = "1";
37 private static final String GWS_QUERY_PARAM = "q";
30 private static final String CTXS_PARAM_PATTERN = "(ctxs=[^&]+)"; 38 private static final String CTXS_PARAM_PATTERN = "(ctxs=[^&]+)";
31 private static final String CTXR_PARAM = "ctxr"; 39 private static final String CTXR_PARAM = "ctxr";
40 @VisibleForTesting static final String TLITE_SOURCE_LANGUAGE_PARAM = "tlites l";
41 private static final String TLITE_TARGET_LANGUAGE_PARAM = "tlitetl";
42 private static final String TLITE_QUERY_PARAM = "tlitetxt";
32 43
33 /** 44 /**
34 * Creates a search request for the given search term without any alternate term and 45 * Creates a search request for the given search term without any alternate term and
35 * for normal-priority loading capability only. 46 * for normal-priority loading capability only.
36 * @param searchTerm The resolved search term. 47 * @param searchTerm The resolved search term.
37 */ 48 */
38 ContextualSearchRequest(String searchTerm) { 49 ContextualSearchRequest(String searchTerm) {
39 this(searchTerm, null, false); 50 this(searchTerm, null, false);
40 } 51 }
41 52
42 /** 53 /**
43 * Creates a search request for the given search term with the given alterna te term and 54 * Creates a search request for the given search term with the given alterna te term and
44 * low-priority loading capability. 55 * low-priority loading capability.
45 * @param searchTerm The resolved search term. 56 * @param searchTerm The resolved search term.
46 * @param alternateTerm The alternate search term. 57 * @param alternateTerm The alternate search term.
47 * @param isLowPriorityEnabled Whether the request can be made at a low prio rity. 58 * @param isLowPriorityEnabled Whether the request can be made at a low prio rity.
48 */ 59 */
49 ContextualSearchRequest(String searchTerm, @Nullable String alternateTerm, 60 ContextualSearchRequest(String searchTerm, @Nullable String alternateTerm,
50 boolean isLowPriorityEnabled) { 61 boolean isLowPriorityEnabled) {
51 mWasPrefetch = isLowPriorityEnabled; 62 mWasPrefetch = isLowPriorityEnabled;
52 mNormalPriorityUri = getUriTemplate(searchTerm, alternateTerm, false); 63 mNormalPriorityUri = getUriTemplate(searchTerm, alternateTerm, false);
53 if (isLowPriorityEnabled) { 64 if (isLowPriorityEnabled) {
54 // TODO(donnd): Call TemplateURL once we have an API for 3rd-party p roviders. 65 // TODO(donnd): Call TemplateURL once we have an API for 3rd-party p roviders.
55 Uri baseLowPriorityUri = getUriTemplate(searchTerm, alternateTerm, t rue); 66 Uri baseLowPriorityUri = getUriTemplate(searchTerm, alternateTerm, t rue);
56 mLowPriorityUri = baseLowPriorityUri.buildUpon() 67 mLowPriorityUri = makeLowPriorityUri(baseLowPriorityUri);
57 .path("s")
58 .appendQueryParameter("sns", "1")
59 .build();
60 mIsLowPriority = true; 68 mIsLowPriority = true;
61 } else { 69 } else {
62 mIsLowPriority = false; 70 mIsLowPriority = false;
63 mLowPriorityUri = null; 71 mLowPriorityUri = null;
64 } 72 }
65 } 73 }
66 74
67 /** 75 /**
68 * Sets an indicator that the normal-priority URL should be used for this se arch request. 76 * Sets an indicator that the normal-priority URL should be used for this se arch request.
69 */ 77 */
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 try { 141 try {
134 url = new URL(searchUrl.replaceAll(CTXS_PARAM_PATTERN, CTXR_PARAM)); 142 url = new URL(searchUrl.replaceAll(CTXS_PARAM_PATTERN, CTXR_PARAM));
135 } catch (MalformedURLException e) { 143 } catch (MalformedURLException e) {
136 url = null; 144 url = null;
137 } 145 }
138 146
139 return url != null ? url.toString() : null; 147 return url != null ? url.toString() : null;
140 } 148 }
141 149
142 /** 150 /**
151 * Adds translation parameters.
152 * @param sourceLanguage The language of the original search term.
153 * @param targetLanguage The language the that the user prefers.
154 */
155 void forceTranslation(String sourceLanguage, String targetLanguage) {
156 mIsTranslationForced = true;
157 if (mLowPriorityUri != null) {
158 mLowPriorityUri = makeTranslateUri(mLowPriorityUri, sourceLanguage, targetLanguage);
159 }
160 mNormalPriorityUri = makeTranslateUri(mNormalPriorityUri, sourceLanguage , targetLanguage);
161 }
162
163 /**
164 * @return Whether translation was forced for this request.
165 */
166 @VisibleForTesting
167 boolean isTranslationForced() {
168 return mIsTranslationForced;
169 }
170
171 /**
143 * Uses TemplateUrlService to generate the url for the given query 172 * Uses TemplateUrlService to generate the url for the given query
144 * {@link String} for {@code query} with the contextual search version param set. 173 * {@link String} for {@code query} with the contextual search version param set.
145 * @param query The search term to use as the main query in the returned sea rch url. 174 * @param query The search term to use as the main query in the returned sea rch url.
146 * @param alternateTerm The alternate search term to use as an alternate sug gestion. 175 * @param alternateTerm The alternate search term to use as an alternate sug gestion.
147 * @param shouldPrefetch Whether the returned url should include a prefetch parameter. 176 * @param shouldPrefetch Whether the returned url should include a prefetch parameter.
148 * @return A {@link String} that contains the url of the default search engine with 177 * @return A {@link Uri} that contains the url of the default search engine with
149 * {@code query} and {@code alternateTerm} inserted as paramete rs and contextual 178 * {@code query} and {@code alternateTerm} inserted as parameters an d contextual
150 * search and prefetch parameters conditionally set. 179 * search and prefetch parameters conditionally set.
151 */ 180 */
152 private Uri getUriTemplate(String query, @Nullable String alternateTerm, 181 private Uri getUriTemplate(String query, @Nullable String alternateTerm,
153 boolean shouldPrefetch) { 182 boolean shouldPrefetch) {
154 return Uri.parse(TemplateUrlService.getInstance().getUrlForContextualSea rchQuery( 183 return Uri.parse(TemplateUrlService.getInstance().getUrlForContextualSea rchQuery(
155 query, alternateTerm, shouldPrefetch)); 184 query, alternateTerm, shouldPrefetch));
156 } 185 }
186
187 /**
188 * @return a low-priority {@code Uri} from the given base {@code Uri}.
189 */
190 private Uri makeLowPriorityUri(Uri baseUri) {
191 return baseUri.buildUpon()
192 .path(GWS_LOW_PRIORITY_SEARCH_PATH)
193 .appendQueryParameter(
194 GWS_SEARCH_NO_SUGGESTIONS_PARAM, GWS_SEARCH_NO_SUGGESTIO NS_PARAM_VALUE)
195 .build();
196 }
197
198 /**
199 * Makes the given {@code Uri} into a similar Uri that triggers a Translate one-box.
200 * @param baseUri The base Uri to build off of.
201 * @param sourceLanguage The language of the original search term.
202 * @param targetLanguage The language the that the user prefers.
203 * @return A {@link Uri} that has additional parameters for Translate approp riately set.
204 */
205 private Uri makeTranslateUri(Uri baseUri, String sourceLanguage, String targ etLanguage) {
206 // TODO(donnd): update to work for non-English. See also getTranslateQu ery.
207 if (!TextUtils.equals(targetLanguage, Locale.ENGLISH.getLanguage())) ret urn baseUri;
208 Uri resultUri = baseUri;
209 if (!sourceLanguage.isEmpty() || !targetLanguage.isEmpty()) {
210 // We must replace the q= param, and there seems to be no good way o ther than clearing
211 // all the query params and adding them all back in, changing q=.
212 Uri.Builder builder = baseUri.buildUpon().clearQuery();
213 String query = null;
214 for (String param : baseUri.getQueryParameterNames()) {
215 String value = baseUri.getQueryParameter(param);
216 if (TextUtils.equals(param, GWS_QUERY_PARAM)) {
217 query = value;
218 value = getTranslateQuery();
219 }
220 builder.appendQueryParameter(param, value);
221 }
222 if (!sourceLanguage.isEmpty()) {
223 builder.appendQueryParameter(TLITE_SOURCE_LANGUAGE_PARAM, source Language);
224 }
225 if (!targetLanguage.isEmpty()) {
226 builder.appendQueryParameter(TLITE_TARGET_LANGUAGE_PARAM, target Language);
227 }
228 builder.appendQueryParameter(TLITE_QUERY_PARAM, query);
229 resultUri = builder.build();
230 }
231 return resultUri;
232 }
233
234 /**
235 * TODO(donnd): This translate API is evolving. Update this code!
236 * TODO(donnd): As of Oct 2015 this will only work on production GWS to tran slate into English.
237 */
238 private String getTranslateQuery() {
239 return "Translate";
240 }
157 } 241 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698