Chromium Code Reviews| 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.physicalweb; | 5 package org.chromium.chrome.browser.physicalweb; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.graphics.Bitmap; | 8 import android.graphics.Bitmap; |
| 9 import android.os.AsyncTask; | 9 import android.os.AsyncTask; |
| 10 import android.os.Build; | 10 import android.os.Build; |
| 11 import android.text.TextUtils; | |
| 11 | 12 |
| 13 import org.json.JSONArray; | |
| 14 import org.json.JSONException; | |
| 15 import org.json.JSONObject; | |
| 16 | |
| 17 import org.chromium.base.LocaleUtils; | |
| 12 import org.chromium.base.Log; | 18 import org.chromium.base.Log; |
| 13 import org.chromium.base.ThreadUtils; | 19 import org.chromium.base.ThreadUtils; |
| 14 import org.chromium.base.VisibleForTesting; | 20 import org.chromium.base.VisibleForTesting; |
| 15 import org.chromium.chrome.GoogleAPIKeys; | 21 import org.chromium.chrome.GoogleAPIKeys; |
| 16 import org.chromium.chrome.R; | 22 import org.chromium.chrome.R; |
| 17 import org.chromium.chrome.browser.ChromeVersionInfo; | 23 import org.chromium.chrome.browser.ChromeVersionInfo; |
| 18 import org.chromium.chrome.browser.physicalweb.PwsClient.FetchIconCallback; | 24 import org.chromium.chrome.browser.physicalweb.PwsClient.FetchIconCallback; |
| 19 import org.chromium.chrome.browser.physicalweb.PwsClient.ResolveScanCallback; | 25 import org.chromium.chrome.browser.physicalweb.PwsClient.ResolveScanCallback; |
| 20 | 26 |
| 21 import org.json.JSONArray; | |
| 22 import org.json.JSONException; | |
| 23 import org.json.JSONObject; | |
| 24 | |
| 25 import java.net.MalformedURLException; | 27 import java.net.MalformedURLException; |
| 26 import java.util.ArrayList; | 28 import java.util.ArrayList; |
| 27 import java.util.Collection; | 29 import java.util.Collection; |
| 30 import java.util.Collections; | |
| 28 import java.util.Formatter; | 31 import java.util.Formatter; |
| 29 import java.util.Locale; | 32 import java.util.HashSet; |
| 30 | 33 |
| 31 /** | 34 /** |
| 32 * This class sends requests to the Physical Web Service. | 35 * This class sends requests to the Physical Web Service. |
| 33 */ | 36 */ |
| 34 class PwsClientImpl implements PwsClient { | 37 class PwsClientImpl implements PwsClient { |
| 35 private static final String TAG = "PhysicalWeb"; | 38 private static final String TAG = "PhysicalWeb"; |
| 36 private static final String ENDPOINT_URL = | 39 private static final String ENDPOINT_URL = |
| 37 "https://physicalweb.googleapis.com/v1alpha1/urls:resolve"; | 40 "https://physicalweb.googleapis.com/v1alpha1/urls:resolve"; |
| 38 | 41 |
| 39 // Format strings for creating the User-Agent string. It should somewhat res emble the Chrome for | 42 // Format strings for creating the User-Agent string. It should somewhat res emble the Chrome for |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 216 sUserAgent = String.format(USER_AGENT_FORMAT, osInfo, product); | 219 sUserAgent = String.format(USER_AGENT_FORMAT, osInfo, product); |
| 217 } | 220 } |
| 218 return sUserAgent; | 221 return sUserAgent; |
| 219 } | 222 } |
| 220 | 223 |
| 221 /** | 224 /** |
| 222 * Construct the Accept-Language string based on the current locale. | 225 * Construct the Accept-Language string based on the current locale. |
| 223 * @return An Accept-Language string. | 226 * @return An Accept-Language string. |
| 224 */ | 227 */ |
| 225 @VisibleForTesting | 228 @VisibleForTesting |
| 226 String getAcceptLanguage() { | 229 String getAcceptLanguage() { |
|
ksk1
2016/10/11 09:58:08
Can we rename this to getAcceptLanguages or someth
Yirui Huang
2016/10/11 12:17:32
Done.
| |
| 227 String defaultLocale = Locale.getDefault().toString(); | 230 String defaultLocale = LocaleUtils.getDefaultLocale(); |
| 228 if (sDefaultLocale == null || !sDefaultLocale.equals(defaultLocale)) { | 231 if (sDefaultLocale == null || !sDefaultLocale.equals(defaultLocale)) { |
| 229 String acceptLanguages = mContext.getResources().getString(R.string. accept_languages); | 232 String acceptLanguages = mContext.getResources().getString(R.string. accept_languages); |
| 230 acceptLanguages = prependToAcceptLanguagesIfNecessary(defaultLocale, acceptLanguages); | 233 acceptLanguages = prependToAcceptLanguagesIfNecessary(defaultLocale, acceptLanguages); |
| 231 sAcceptLanguage = generateAcceptLanguageHeader(acceptLanguages); | 234 sAcceptLanguage = generateAcceptLanguageHeader(acceptLanguages); |
| 232 sDefaultLocale = defaultLocale; | 235 sDefaultLocale = defaultLocale; |
| 233 } | 236 } |
| 234 return sAcceptLanguage; | 237 return sAcceptLanguage; |
| 235 } | 238 } |
| 236 | 239 |
| 237 /** | 240 /** |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 256 isoLanguage = language; | 259 isoLanguage = language; |
| 257 } | 260 } |
| 258 | 261 |
| 259 return isoLanguage + "-" + region; | 262 return isoLanguage + "-" + region; |
| 260 } | 263 } |
| 261 | 264 |
| 262 /** | 265 /** |
| 263 * Get the language code for the default locale and prepend it to the Accept -Language string if | 266 * Get the language code for the default locale and prepend it to the Accept -Language string if |
| 264 * it isn't already present. The logic should match PrependToAcceptLanguages IfNecessary in | 267 * it isn't already present. The logic should match PrependToAcceptLanguages IfNecessary in |
| 265 * chrome/browser/android/preferences/pref_service_bridge.cc | 268 * chrome/browser/android/preferences/pref_service_bridge.cc |
| 266 * @param locale A string representing the default locale. | 269 * @param locales A string representing a default language tag or a list of default language |
| 270 * tags. | |
| 267 * @param acceptLanguages The default language list for the language of the user's locale. | 271 * @param acceptLanguages The default language list for the language of the user's locale. |
| 268 * @return An updated language list. | 272 * @return An updated language list. |
| 269 */ | 273 */ |
| 270 @VisibleForTesting | 274 @VisibleForTesting |
| 271 static String prependToAcceptLanguagesIfNecessary(String locale, String acce ptLanguages) | 275 static String prependToAcceptLanguagesIfNecessary(String locales, String acc eptLanguages) { |
|
ksk1
2016/10/11 09:58:08
I suspect that this CL contains your previous CL.
Yirui Huang
2016/10/11 12:17:32
Done.
| |
| 272 { | 276 String localeString = locales + "," + acceptLanguages; |
| 273 if (locale.length() != 5 || locale.charAt(2) != '_') { | 277 String[] localeList = localeString.split(","); |
| 274 return acceptLanguages; | 278 |
| 279 HashSet<String> seenLocales = new HashSet<>(); | |
| 280 ArrayList<String> uniqueList = new ArrayList<>(); | |
| 281 for (String locale : localeList) { | |
| 282 String language; | |
| 283 String country; | |
| 284 if (locale.length() == 2 || locale.length() == 3) { | |
| 285 language = locale; | |
| 286 country = ""; | |
| 287 } else if (locale.charAt(2) == '-') { | |
| 288 language = locale.substring(0, 2); | |
| 289 country = locale.substring(3); | |
| 290 } else if (locale.charAt(3) == '-') { | |
| 291 language = locale.substring(0, 3); | |
| 292 country = locale.substring(4); | |
| 293 } else { | |
| 294 continue; | |
| 295 } | |
| 296 | |
| 297 String languageTag = makeLanguageTag(language, country); | |
| 298 | |
| 299 if (seenLocales.contains(languageTag)) { | |
| 300 continue; | |
| 301 } | |
| 302 if (country != "") { | |
| 303 seenLocales.add(languageTag); | |
| 304 } | |
| 305 uniqueList.add(languageTag); | |
| 275 } | 306 } |
| 276 | 307 |
| 277 String language = locale.substring(0, 2); | 308 // If language is not in the accept languages list, also add language co de. |
| 278 String region = locale.substring(3); | 309 // A language code should only be inserted after the last languageTag th at |
| 279 String languageTag = makeLanguageTag(language, region); | 310 // contains that language. |
| 311 // This will work with the IDS_ACCEPT_LANGUAGE localized strings bundled | |
| 312 // with Chrome but may fail on arbitrary lists of language tags due to | |
| 313 // differences in case and whitespace. | |
| 314 HashSet<String> seenLanguages = new HashSet<>(); | |
| 315 ArrayList<String> outputList = new ArrayList<>(); | |
| 316 for (int i = uniqueList.size() - 1; i >= 0; i--) { | |
| 317 String localeAdd = uniqueList.get(i); | |
| 318 int lastChar = localeAdd.length() - 1; | |
| 319 String languageAdd; | |
| 320 if (localeAdd.charAt(lastChar) == '-') { | |
| 321 languageAdd = localeAdd.substring(0, lastChar); | |
| 322 } else if (localeAdd.charAt(2) == '-') { | |
| 323 languageAdd = localeAdd.substring(0, 2); | |
| 324 } else { // length of the language code is 3. | |
| 325 languageAdd = localeAdd.substring(0, 3); | |
| 326 } | |
| 280 | 327 |
| 281 if (acceptLanguages.contains(languageTag)) { | 328 if (!seenLanguages.contains(languageAdd)) { |
| 282 return acceptLanguages; | 329 seenLanguages.add(languageAdd); |
| 330 outputList.add(languageAdd); | |
| 331 } | |
| 332 if (lastChar != languageAdd.length()) { | |
| 333 outputList.add(localeAdd); | |
| 334 } | |
| 283 } | 335 } |
| 284 | 336 Collections.reverse(outputList); |
| 285 Formatter parts = new Formatter(); | 337 return TextUtils.join(",", outputList); |
| 286 parts.format("%s,", languageTag); | |
| 287 // If language is not in the accept languages list, also add language co de. | |
| 288 // This will work with the IDS_ACCEPT_LANGUAGES localized strings bundle d with Chrome but | |
| 289 // may fail on arbitrary lists of language tags due to differences in ca se and whitespace. | |
| 290 if (!acceptLanguages.contains(language + ",") && !acceptLanguages.endsWi th(language)) { | |
| 291 parts.format("%s,", language); | |
| 292 } | |
| 293 parts.format("%s", acceptLanguages); | |
| 294 return parts.toString(); | |
| 295 } | 338 } |
| 296 | 339 |
| 297 /** | 340 /** |
| 298 * Given a list of comma-delimited language codes in decreasing order of pre ference, insert | 341 * Given a list of comma-delimited language codes in decreasing order of pre ference, insert |
| 299 * q-values to represent the relative quality/precedence of each language. T he logic should | 342 * q-values to represent the relative quality/precedence of each language. T he logic should |
| 300 * match GenerateAcceptLanguageHeader in net/http/http_util.cc. | 343 * match GenerateAcceptLanguageHeader in net/http/http_util.cc. |
| 301 * @param languageList A comma-delimited list of language codes containing n o whitespace. | 344 * @param languageList A comma-delimited list of language codes containing n o whitespace. |
| 302 * @return An Accept-Language header with q-values. | 345 * @return An Accept-Language header with q-values. |
| 303 */ | 346 */ |
| 304 @VisibleForTesting | 347 @VisibleForTesting |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 317 langListWithQ.format(",%s;q=0.%d", language, qvalue10); | 360 langListWithQ.format(",%s;q=0.%d", language, qvalue10); |
| 318 } | 361 } |
| 319 // It does not make sense to have 'q=0'. | 362 // It does not make sense to have 'q=0'. |
| 320 if (qvalue10 > kQvalueDecrement10) { | 363 if (qvalue10 > kQvalueDecrement10) { |
| 321 qvalue10 -= kQvalueDecrement10; | 364 qvalue10 -= kQvalueDecrement10; |
| 322 } | 365 } |
| 323 } | 366 } |
| 324 return langListWithQ.toString(); | 367 return langListWithQ.toString(); |
| 325 } | 368 } |
| 326 } | 369 } |
| OLD | NEW |