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

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: Disable for non-English users, for now. Created 5 years, 2 months 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 {
22
23 private final Uri mLowPriorityUri;
24 private final Uri mNormalPriorityUri;
25 private final boolean mWasPrefetch; 25 private final boolean mWasPrefetch;
26 private Uri mLowPriorityUri;
27 private Uri mNormalPriorityUri;
26 28
27 private boolean mIsLowPriority; 29 private boolean mIsLowPriority;
28 private boolean mHasFailedLowPriorityLoad; 30 private boolean mHasFailedLowPriorityLoad;
29 31
32 private static final String GWS_LOW_PRIORITY_SEARCH_PATH = "s";
33 private static final String GWS_SEARCH_NO_SUGGESTIONS_PARAM = "sns";
34 private static final String GWS_SEARCH_NO_SUGGESTIONS_PARAM_VALUE = "1";
35 private static final String GWS_QUERY_PARAM = "q";
30 private static final String CTXS_PARAM_PATTERN = "(ctxs=[^&]+)"; 36 private static final String CTXS_PARAM_PATTERN = "(ctxs=[^&]+)";
31 private static final String CTXR_PARAM = "ctxr"; 37 private static final String CTXR_PARAM = "ctxr";
38 @VisibleForTesting static final String TLITE_SOURCE_LANGUAGE_PARAM = "tlites l";
39 private static final String TLITE_TARGET_LANGUAGE_PARAM = "tlitetl";
40 private static final String TLITE_QUERY_PARAM = "tlitetxt";
32 41
33 /** 42 /**
34 * Creates a search request for the given search term without any alternate term and 43 * Creates a search request for the given search term without any alternate term and
35 * for normal-priority loading capability only. 44 * for normal-priority loading capability only.
36 * @param searchTerm The resolved search term. 45 * @param searchTerm The resolved search term.
37 */ 46 */
38 ContextualSearchRequest(String searchTerm) { 47 ContextualSearchRequest(String searchTerm) {
39 this(searchTerm, null, false); 48 this(searchTerm, null, false);
40 } 49 }
41 50
42 /** 51 /**
43 * Creates a search request for the given search term with the given alterna te term and 52 * Creates a search request for the given search term with the given alterna te term and
44 * low-priority loading capability. 53 * low-priority loading capability.
45 * @param searchTerm The resolved search term. 54 * @param searchTerm The resolved search term.
46 * @param alternateTerm The alternate search term. 55 * @param alternateTerm The alternate search term.
47 * @param isLowPriorityEnabled Whether the request can be made at a low prio rity. 56 * @param isLowPriorityEnabled Whether the request can be made at a low prio rity.
48 */ 57 */
49 ContextualSearchRequest(String searchTerm, @Nullable String alternateTerm, 58 ContextualSearchRequest(String searchTerm, @Nullable String alternateTerm,
50 boolean isLowPriorityEnabled) { 59 boolean isLowPriorityEnabled) {
51 mWasPrefetch = isLowPriorityEnabled; 60 mWasPrefetch = isLowPriorityEnabled;
52 mNormalPriorityUri = getUriTemplate(searchTerm, alternateTerm, false); 61 mNormalPriorityUri = getUriTemplate(searchTerm, alternateTerm, false);
53 if (isLowPriorityEnabled) { 62 if (isLowPriorityEnabled) {
54 // TODO(donnd): Call TemplateURL once we have an API for 3rd-party p roviders. 63 // TODO(donnd): Call TemplateURL once we have an API for 3rd-party p roviders.
55 Uri baseLowPriorityUri = getUriTemplate(searchTerm, alternateTerm, t rue); 64 Uri baseLowPriorityUri = getUriTemplate(searchTerm, alternateTerm, t rue);
56 mLowPriorityUri = baseLowPriorityUri.buildUpon() 65 mLowPriorityUri = makeLowPriorityUri(baseLowPriorityUri);
57 .path("s")
58 .appendQueryParameter("sns", "1")
59 .build();
60 mIsLowPriority = true; 66 mIsLowPriority = true;
61 } else { 67 } else {
62 mIsLowPriority = false; 68 mIsLowPriority = false;
63 mLowPriorityUri = null; 69 mLowPriorityUri = null;
64 } 70 }
65 } 71 }
66 72
67 /** 73 /**
68 * Sets an indicator that the normal-priority URL should be used for this se arch request. 74 * Sets an indicator that the normal-priority URL should be used for this se arch request.
69 */ 75 */
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 try { 139 try {
134 url = new URL(searchUrl.replaceAll(CTXS_PARAM_PATTERN, CTXR_PARAM)); 140 url = new URL(searchUrl.replaceAll(CTXS_PARAM_PATTERN, CTXR_PARAM));
135 } catch (MalformedURLException e) { 141 } catch (MalformedURLException e) {
136 url = null; 142 url = null;
137 } 143 }
138 144
139 return url != null ? url.toString() : null; 145 return url != null ? url.toString() : null;
140 } 146 }
141 147
142 /** 148 /**
149 * Adds translation parameters.
150 * @param sourceLanguage The language of the original search term.
151 * @param targetLanguage The language the that the user prefers.
152 */
153 void forceTranslation(String sourceLanguage, String targetLanguage) {
154 if (mLowPriorityUri != null) {
155 mLowPriorityUri = makeTranslateUri(mLowPriorityUri, sourceLanguage, targetLanguage);
156 }
157 mNormalPriorityUri = makeTranslateUri(mNormalPriorityUri, sourceLanguage , targetLanguage);
158 }
159
pedro (no code reviews) 2015/10/23 08:50:36 There should be a public boolean isTranlationForce
Donn Denman 2015/10/28 22:17:15 Done.
160 /**
143 * Uses TemplateUrlService to generate the url for the given query 161 * Uses TemplateUrlService to generate the url for the given query
144 * {@link String} for {@code query} with the contextual search version param set. 162 * {@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. 163 * @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. 164 * @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. 165 * @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 166 * @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 167 * {@code query} and {@code alternateTerm} inserted as parameters an d contextual
150 * search and prefetch parameters conditionally set. 168 * search and prefetch parameters conditionally set.
151 */ 169 */
152 private Uri getUriTemplate(String query, @Nullable String alternateTerm, 170 private Uri getUriTemplate(String query, @Nullable String alternateTerm,
153 boolean shouldPrefetch) { 171 boolean shouldPrefetch) {
154 return Uri.parse(TemplateUrlService.getInstance().getUrlForContextualSea rchQuery( 172 return Uri.parse(TemplateUrlService.getInstance().getUrlForContextualSea rchQuery(
155 query, alternateTerm, shouldPrefetch)); 173 query, alternateTerm, shouldPrefetch));
156 } 174 }
175
176 /**
177 * @return a low-priority {@code Uri} from the given base {@code Uri}.
178 */
179 private Uri makeLowPriorityUri(Uri baseUri) {
180 return baseUri.buildUpon()
181 .path(GWS_LOW_PRIORITY_SEARCH_PATH)
182 .appendQueryParameter(
183 GWS_SEARCH_NO_SUGGESTIONS_PARAM, GWS_SEARCH_NO_SUGGESTIO NS_PARAM_VALUE)
184 .build();
185 }
186
187 /**
188 * Makes the given {@code Uri} into a similar Uri that triggers a Translate one-box.
189 * @param baseUri The base Uri to build off of.
190 * @param sourceLanguage The language of the original search term.
191 * @param targetLanguage The language the that the user prefers.
192 * @return A {@link Uri} that has additional parameters for Translate approp riately set.
193 */
194 private Uri makeTranslateUri(Uri baseUri, String sourceLanguage, String targ etLanguage) {
195 // TODO(donnd): update to work for non-English. See also getTranslateQu ery.
196 if (!TextUtils.equals(targetLanguage, Locale.ENGLISH.getLanguage())) ret urn baseUri;
197 Uri resultUri = baseUri;
198 if (!sourceLanguage.isEmpty() || !targetLanguage.isEmpty()) {
199 // We must replace the q= param, and there seems to be no good way o ther than clearing
200 // all the query params and adding them all back in, changing q=.
201 Uri.Builder builder = baseUri.buildUpon().clearQuery();
202 String query = null;
203 for (String param : baseUri.getQueryParameterNames()) {
204 String value = baseUri.getQueryParameter(param);
205 if (TextUtils.equals(param, GWS_QUERY_PARAM)) {
206 query = value;
207 value = getTranslateQuery();
208 }
209 builder.appendQueryParameter(param, value);
210 }
211 if (!sourceLanguage.isEmpty()) {
212 builder.appendQueryParameter(TLITE_SOURCE_LANGUAGE_PARAM, source Language);
213 }
214 if (!targetLanguage.isEmpty()) {
215 builder.appendQueryParameter(TLITE_TARGET_LANGUAGE_PARAM, target Language);
216 }
217 builder.appendQueryParameter(TLITE_QUERY_PARAM, query);
218 resultUri = builder.build();
219 }
220 return resultUri;
221 }
222
223 /**
224 * TODO(donnd): This translate API is evolving. Update this code!
225 * TODO(donnd): As of Oct '15 this will only work on production GWS to trans late into English.
226 */
227 private String getTranslateQuery() {
228 return "Translate";
229 }
157 } 230 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698