| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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.blimp; | 5 package org.chromium.chrome.browser.blimp; |
| 6 | 6 |
| 7 import org.chromium.base.ContextUtils; | 7 import android.content.Context; |
| 8 |
| 8 import org.chromium.base.annotations.CalledByNative; | 9 import org.chromium.base.annotations.CalledByNative; |
| 9 import org.chromium.blimp_public.BlimpClientContext; | 10 import org.chromium.blimp_public.BlimpClientContext; |
| 10 import org.chromium.blimp_public.BlimpClientContextDelegate; | 11 import org.chromium.blimp_public.BlimpClientContextDelegate; |
| 12 import org.chromium.chrome.browser.ApplicationLifetime; |
| 11 import org.chromium.chrome.browser.profiles.Profile; | 13 import org.chromium.chrome.browser.profiles.Profile; |
| 12 import org.chromium.chrome.browser.signin.AccountTrackerService; | 14 import org.chromium.chrome.browser.signin.AccountSigninActivity; |
| 15 import org.chromium.chrome.browser.signin.SigninAccessPoint; |
| 13 | 16 |
| 14 /** | 17 /** |
| 15 * The ChromeBlimpClientContextDelegate for //chrome which provides the necessar
y functionality | 18 * The ChromeBlimpClientContextDelegate for //chrome which provides the necessar
y functionality |
| 16 * required to run Blimp. This is the Java counterparty to the C++ | 19 * required to run Blimp. This is the Java counterparty to the C++ |
| 17 * ChromeBlimpClientContextDelegateAndroid. | 20 * ChromeBlimpClientContextDelegateAndroid. |
| 18 * | 21 * |
| 19 * There can only be a single delegate for any given BlimpClientContext. To crea
te one and attach | 22 * There can only be a single delegate for any given BlimpClientContext. To crea
te one and attach |
| 20 * it, call {@link ChromeBlimpClientContextDelegate#createAndSetDelegateForConte
xt(Profile)}. | 23 * it, call {@link ChromeBlimpClientContextDelegate#createAndSetDelegateForConte
xt(Profile)}. |
| 21 * When the delegate should be deleted, a call to {@link #destroy} is required. | 24 * When the delegate should be deleted, a call to {@link #destroy} is required. |
| 22 */ | 25 */ |
| 23 public class ChromeBlimpClientContextDelegate | 26 public class ChromeBlimpClientContextDelegate implements BlimpClientContextDeleg
ate { |
| 24 implements BlimpClientContextDelegate, | |
| 25 AccountTrackerService.OnSystemAccountsSeededListener { | |
| 26 /** | 27 /** |
| 27 * {@link BlimpClientContext} associated with this delegate. | 28 * {@link BlimpClientContext} associated with this delegate. |
| 28 */ | 29 */ |
| 29 private BlimpClientContext mBlimpClientContext; | 30 private BlimpClientContext mBlimpClientContext; |
| 30 | 31 |
| 31 /** | 32 /** |
| 32 * Creates a new ChromeBlimpClientContextDelegate that is owned by the calle
r. It automatically | 33 * Creates a new ChromeBlimpClientContextDelegate that is owned by the calle
r. It automatically |
| 33 * attaches itself as the sole delegate for the BlimpClientContext attached
to the given | 34 * attaches itself as the sole delegate for the BlimpClientContext attached
to the given |
| 34 * profile. When the delegate should be deleted, the caller of this method m
ust call | 35 * profile. When the delegate should be deleted, the caller of this method m
ust call |
| 35 * {@link #destroy()} to ensure that the native counterparts are cleaned up.
The call to | 36 * {@link #destroy()} to ensure that the native counterparts are cleaned up.
The call to |
| 36 * {@link #destroy()} also removes the delegate from the BlimpClientContext
by clearing the | 37 * {@link #destroy()} also removes the delegate from the BlimpClientContext
by clearing the |
| 37 * pointer for the delegate by calling BlimpClientContext::SetDelegate(nullp
tr). | 38 * pointer for the delegate by calling BlimpClientContext::SetDelegate(nullp
tr). |
| 38 * | 39 * |
| 39 * @param profile The profile to use to look for the BlimpClientContext. | 40 * @param profile The profile to use to look for the BlimpClientContext. |
| 40 * @return The newly created delegate, owned by the caller. | 41 * @return The newly created delegate, owned by the caller. |
| 41 */ | 42 */ |
| 42 public static ChromeBlimpClientContextDelegate createAndSetDelegateForContex
t(Profile profile) { | 43 public static ChromeBlimpClientContextDelegate createAndSetDelegateForContex
t(Profile profile) { |
| 43 return new ChromeBlimpClientContextDelegate(profile); | 44 return new ChromeBlimpClientContextDelegate(profile); |
| 44 } | 45 } |
| 45 | 46 |
| 46 /** | 47 /** |
| 47 * @return {@link BlimpClientContext} object this delegate belongs to. | 48 * @return {@link BlimpClientContext} object this delegate belongs to. |
| 48 */ | 49 */ |
| 49 public BlimpClientContext getBlimpClientContext() { | 50 public BlimpClientContext getBlimpClientContext() { |
| 50 return mBlimpClientContext; | 51 return mBlimpClientContext; |
| 51 } | 52 } |
| 52 | 53 |
| 53 @Override | 54 @Override |
| 54 public void onSystemAccountsSeedingComplete() { | 55 public void restartBrowser() { |
| 55 AccountTrackerService.get(ContextUtils.getApplicationContext()) | 56 ApplicationLifetime.terminate(true); |
| 56 .removeSystemAccountsSeededListener(this); | |
| 57 | |
| 58 // Start authentication. | |
| 59 // Must request OAuth2 token after account seeded, or native ProfileOAut
h2TokenService might | |
| 60 // revoke the refresh token during request, making token retrieval flaky
. | |
| 61 mBlimpClientContext.connect(); | |
| 62 } | 57 } |
| 63 | 58 |
| 64 @Override | 59 @Override |
| 65 public void onSystemAccountsChanged() {} | 60 public void startUserSignInFlow(Context context) { |
| 61 // TODO(xingliu): Figure out if Blimp should have its own SigninAccessPo
int. |
| 62 AccountSigninActivity.startAccountSigninActivity(context, SigninAccessPo
int.SETTINGS); |
| 63 } |
| 66 | 64 |
| 67 /** | 65 /** |
| 68 * The pointer to the ChromeBlimpClientContextDelegateAndroid JNI bridge. | 66 * The pointer to the ChromeBlimpClientContextDelegateAndroid JNI bridge. |
| 69 */ | 67 */ |
| 70 private long mNativeChromeBlimpClientContextDelegateAndroid; | 68 private long mNativeChromeBlimpClientContextDelegateAndroid; |
| 71 | 69 |
| 72 private ChromeBlimpClientContextDelegate(Profile profile) { | 70 private ChromeBlimpClientContextDelegate(Profile profile) { |
| 73 // Create native delegate object. | 71 // Create native delegate object. |
| 74 mNativeChromeBlimpClientContextDelegateAndroid = nativeInit(profile); | 72 mNativeChromeBlimpClientContextDelegateAndroid = nativeInit(profile); |
| 75 | 73 |
| 76 BlimpClientContext context = | 74 BlimpClientContext context = |
| 77 BlimpClientContextFactory.getBlimpClientContextForProfile(profil
e); | 75 BlimpClientContextFactory.getBlimpClientContextForProfile(profil
e); |
| 78 mBlimpClientContext = context; | 76 mBlimpClientContext = context; |
| 79 | 77 |
| 80 // Set ourselves as the Java delegate object. | 78 // Set ourselves as the Java delegate object. |
| 81 mBlimpClientContext.setDelegate(this); | 79 mBlimpClientContext.setDelegate(this); |
| 82 | 80 |
| 83 // Connect after account seeding finished, if account seeding is already
done, listener will | 81 // Connect to engine on start up. |
| 84 // immediately get called. | 82 if (mBlimpClientContext.isBlimpEnabled()) { |
| 85 AccountTrackerService.get(ContextUtils.getApplicationContext()) | 83 mBlimpClientContext.connect(); |
| 86 .addSystemAccountsSeededListener(this); | 84 } |
| 87 } | 85 } |
| 88 | 86 |
| 89 @CalledByNative | 87 @CalledByNative |
| 90 private void clearNativePtr() { | 88 private void clearNativePtr() { |
| 91 mNativeChromeBlimpClientContextDelegateAndroid = 0; | 89 mNativeChromeBlimpClientContextDelegateAndroid = 0; |
| 92 } | 90 } |
| 93 | 91 |
| 94 public void destroy() { | 92 public void destroy() { |
| 95 assert mNativeChromeBlimpClientContextDelegateAndroid != 0; | 93 assert mNativeChromeBlimpClientContextDelegateAndroid != 0; |
| 96 | 94 |
| 97 AccountTrackerService.get(ContextUtils.getApplicationContext()) | |
| 98 .removeSystemAccountsSeededListener(this); | |
| 99 | |
| 100 nativeDestroy(mNativeChromeBlimpClientContextDelegateAndroid); | 95 nativeDestroy(mNativeChromeBlimpClientContextDelegateAndroid); |
| 101 } | 96 } |
| 102 | 97 |
| 103 private native long nativeInit(Profile profile); | 98 private native long nativeInit(Profile profile); |
| 104 private native void nativeDestroy(long nativeChromeBlimpClientContextDelegat
eAndroid); | 99 private native void nativeDestroy(long nativeChromeBlimpClientContextDelegat
eAndroid); |
| 105 } | 100 } |
| OLD | NEW |