Skip to content

Commit

Permalink
[ULP] Add LanguageProfileController
Browse files Browse the repository at this point in the history
This CL adds a LanguageProfileController to manage the
LanguageProfileDelegate and handle logging while getting preferred
languages.

To get preferred languages from a background thread call:
LanguageProfileController.getLanguagePreferences(accountName)

Bug: 1258255
Change-Id: Ibab5ab54eec7240d9b5d74452af9e4081ac3c3fc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3314393
Reviewed-by: Josh Simmons <jds@google.com>
Reviewed-by: Anthony Cui <cuianthony@chromium.org>
Commit-Queue: Trevor Perrier <perrier@chromium.org>
Cr-Commit-Position: refs/heads/main@{#949627}
  • Loading branch information
Trevor Perrier authored and Chromium LUCI CQ committed Dec 8, 2021
1 parent 61b3ace commit a08d163
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
2 changes: 2 additions & 0 deletions components/language/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ android_library("java") {
sources = [
"java/src/org/chromium/components/language/AndroidLanguageMetricsBridge.java",
"java/src/org/chromium/components/language/GeoLanguageProviderBridge.java",
"java/src/org/chromium/components/language/LanguageProfileController.java",
]

deps = [
":ulp_delegate_java",
"//base:base_java",
"//third_party/androidx:androidx_annotation_annotation_java",
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.components.language;

import org.chromium.base.Log;
import org.chromium.base.ThreadUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeoutException;

/**
* Controller to manage getting language preferences from device.
*/
public final class LanguageProfileController {
private static final String TAG = "ULP";
private static final int TIMEOUT_IN_SECONDS = 60;

private LanguageProfileDelegate mDelegate;

/**
* @param delegate LanguageProfileDelegate to use.
*/
public LanguageProfileController(LanguageProfileDelegate delegate) {
mDelegate = delegate;
}

/**
* Get the preferred languages for user. The list is empty if an error occurs.
* This method is blocking and must be called on a background thread.
* @param accountName Account to get profile or null if the default profile should be returned.
* @return A list of language tags ordered by preference for |accountName|
*/
public List<String> getLanguagePreferences(String accountName) {
ThreadUtils.assertOnBackgroundThread();
if (!mDelegate.isULPAvailable()) {
// (TODO:https://crbug.com/1258261) Add initiation histogram.
Log.d(TAG, "ULP not available");
return new ArrayList<String>();
}
try {
return mDelegate.getLanguagePreferences(accountName, TIMEOUT_IN_SECONDS);
} catch (TimeoutException e) {
// (TODO:https://crbug.com/1258261) Add initiation histogram.
Log.d(TAG, "ULP getLanguagePreferences timed out");
} catch (Exception e) {
// (TODO:https://crbug.com/1258261) Add initiation histogram.
Log.d(TAG, "ULP getLanguagePreferences threw exception:", e);
}
return new ArrayList<String>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package org.chromium.components.language;

import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

/**
* Interface to get language profile data for device.
Expand All @@ -17,7 +19,9 @@ public interface LanguageProfileDelegate {

/**
* @param accountName Account to get profile or null if the default profile should be returned.
* @param timeoutInSeconds Seconds to wait before timing out on call to device.
* @return A list of language tags ordered by preference for |accountName|
*/
public List<String> getLanguagePreferences(String accountName);
public List<String> getLanguagePreferences(String accountName, int timeoutInSeconds)
throws ExecutionException, InterruptedException, TimeoutException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ public boolean isULPAvailable() {

/**
* @param accountName Account to get profile or null if the default profile should be returned.
* @param timeoutInSeconds Seconds to wait before timing out on call to device.
* @return A list of language tags ordered by preference for |accountName|
*/
@Override
public List<String> getLanguagePreferences(String accountName) {
public List<String> getLanguagePreferences(String accountName, int timeoutInSeconds) {
// The default implementation always returns an empty list.
return new ArrayList<String>();
}
Expand Down

0 comments on commit a08d163

Please sign in to comment.