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

Unified Diff: remoting/android/java/src/org/chromium/chromoting/OAuthTokenConsumer.java

Issue 1976853002: [Remoting Android] Refactor OAuth Token Fetching Code (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Reviewer's Feedback Created 4 years, 7 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « remoting/android/java/src/org/chromium/chromoting/HostListManager.java ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: remoting/android/java/src/org/chromium/chromoting/OAuthTokenConsumer.java
diff --git a/remoting/android/java/src/org/chromium/chromoting/OAuthTokenConsumer.java b/remoting/android/java/src/org/chromium/chromoting/OAuthTokenConsumer.java
new file mode 100644
index 0000000000000000000000000000000000000000..1e3771eef52df1c3730e3c3092c2f770dfed46d7
--- /dev/null
+++ b/remoting/android/java/src/org/chromium/chromoting/OAuthTokenConsumer.java
@@ -0,0 +1,75 @@
+// Copyright 2016 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.chromoting;
+
+import android.app.Activity;
+
+import org.chromium.chromoting.base.OAuthTokenFetcher;
+
+/**
+ * This helper guards same auth token requesting task shouldn't be run more than once at the same
+ * time.
+ */
+public class OAuthTokenConsumer {
+ private Activity mActivity;
+ private String mTokenScope;
+ private boolean mWaitingForAuthToken;
+ private String mLatestToken;
+
+ /**
+ * @param activity The Chromoting activity.
+ * @param tokenScope Scope to use when fetching the OAuth token.
+ */
+ public OAuthTokenConsumer(Activity activity, String tokenScope) {
+ mActivity = activity;
+ mTokenScope = tokenScope;
+ mWaitingForAuthToken = false;
+ }
+
+ /**
+ * Retrieves the auth token and call the callback when it is done. callback.onTokenFetched()
+ * will be called if the retrieval succeeds, otherwise callback.onError() will be called.
+ * The callback will not be run if the task is already running and false will be returned in
+ * that case.
+ * Each OAuthTokenConsumer is supposed to work for one specific task. It is the caller's
+ * responsibility to supply equivalent callbacks (variables being captured can vary) for the
+ * same consumer.
+ * @param account User's account name (email).
+ * @param callback the callback to be called
+ * @return true if the consumer will run |callback| when the token is fetched and false
+ * otherwise (meaning a previous callback is waiting to be run).
+ */
+ public boolean consume(String account, final OAuthTokenFetcher.Callback callback) {
+ if (mWaitingForAuthToken) {
+ return false;
+ }
+ mWaitingForAuthToken = true;
+
+ new OAuthTokenFetcher(mActivity, account, mTokenScope, new OAuthTokenFetcher.Callback() {
+ @Override
+ public void onTokenFetched(String token) {
+ mWaitingForAuthToken = false;
+ mLatestToken = token;
+ callback.onTokenFetched(token);
+ }
+
+ @Override
+ public void onError(OAuthTokenFetcher.Error error) {
+ mWaitingForAuthToken = false;
+ callback.onError(error);
+ }
+ }).fetch();
+ return true;
+ }
+
+ /**
+ * @return The latest auth token fetched by calling consume(). This should be used right after
+ * the callback passed to consume() is run. The token may become invalid after some
+ * amount of time.
+ */
+ public String getLatestToken() {
+ return mLatestToken;
+ }
+}
« no previous file with comments | « remoting/android/java/src/org/chromium/chromoting/HostListManager.java ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698