Chromium Code Reviews| Index: services/authentication/src/org/chromium/mojo/authentication/AuthenticationServiceImpl.java |
| diff --git a/services/authentication/src/org/chromium/mojo/authentication/AuthenticationServiceImpl.java b/services/authentication/src/org/chromium/mojo/authentication/AuthenticationServiceImpl.java |
| index 928a8cdd5e57b7a0d3c83309b74d57dff5059cc3..b9e1c9d261244fb82b9befa657b958c01ac27cfe 100644 |
| --- a/services/authentication/src/org/chromium/mojo/authentication/AuthenticationServiceImpl.java |
| +++ b/services/authentication/src/org/chromium/mojo/authentication/AuthenticationServiceImpl.java |
| @@ -16,21 +16,37 @@ import com.google.android.gms.auth.UserRecoverableAuthException; |
| import com.google.android.gms.common.AccountPicker; |
| import org.chromium.mojo.application.ShellHelper; |
| +import org.chromium.mojo.bindings.DeserializationException; |
| +import org.chromium.mojo.bindings.Message; |
| import org.chromium.mojo.bindings.SideEffectFreeCloseable; |
| import org.chromium.mojo.intent.IntentReceiver; |
| import org.chromium.mojo.intent.IntentReceiverManager; |
| import org.chromium.mojo.intent.IntentReceiverManager.RegisterActivityResultReceiverResponse; |
| import org.chromium.mojo.system.Core; |
| +import org.chromium.mojo.system.Handle; |
| import org.chromium.mojo.system.MojoException; |
| import org.chromium.mojom.mojo.Shell; |
| +import java.io.File; |
| +import java.io.FileInputStream; |
| +import java.io.FileOutputStream; |
| import java.io.IOException; |
| +import java.nio.channels.FileChannel; |
| +import java.nio.channels.FileChannel.MapMode; |
| +import java.util.ArrayList; |
| +import java.util.HashMap; |
| /** |
| * Implementation of AuthenticationService from services/authentication/authentication.mojom |
| */ |
| public class AuthenticationServiceImpl |
| extends SideEffectFreeCloseable implements AuthenticationService { |
| + // The current version of the database. |
|
ppi
2015/06/03 17:11:17
When this should be updated / incremented? Could y
qsr
2015/06/04 09:16:15
Done.
|
| + private static final int VERSION = 0; |
| + |
| + // Type of google accounts. |
| + private static final String GOOGLE_ACCOUNT_TYPE = "com.google"; |
|
ppi
2015/06/03 17:11:17
Should we put "private static final String[] sAcco
qsr
2015/06/04 09:16:15
Done.
|
| + |
| /** |
| * An callback that takes a serialized intent, add the intent the shell needs to send and start |
| * the container intent. |
| @@ -38,8 +54,7 @@ public class AuthenticationServiceImpl |
| private final class RegisterActivityResultReceiverCallback |
| implements RegisterActivityResultReceiverResponse { |
| /** |
| - * The intent that the requesting application needs to be run by shell on its |
| -behalf. |
| + * The intent that the requesting application needs to be run by shell on its behalf. |
| */ |
| private final Intent mIntent; |
| @@ -61,6 +76,8 @@ behalf. |
| private final Activity mContext; |
| private final String mConsumerURL; |
| private final IntentReceiverManager mIntentReceiverManager; |
| + private Db mDb = null; |
| + private File mDbFile = null; |
| public AuthenticationServiceImpl(Context context, Core core, String consumerURL, Shell shell) { |
| mContext = (Activity) context; |
| @@ -136,11 +153,24 @@ behalf. |
| } |
| /** |
| - * @see AuthenticationService#selectAccount(AuthenticationService.SelectAccountResponse) |
| + * @see AuthenticationService#selectAccount(boolean, |
| + * AuthenticationService.SelectAccountResponse) |
| */ |
| @Override |
| - public void selectAccount(final SelectAccountResponse callback) { |
| - String[] accountTypes = new String[] {"com.google"}; |
| + public void selectAccount(boolean returnLastSelected, final SelectAccountResponse callback) { |
| + if (returnLastSelected) { |
| + Db db = getDb(); |
| + String username = db.lastSelectedAccounts.get(mConsumerURL); |
| + if (username != null) { |
| + try { |
| + GoogleAuthUtil.getAccountId(mContext, username); |
| + callback.call(username, null); |
| + return; |
| + } catch (final GoogleAuthException | IOException e) { |
| + } |
| + } |
| + } |
|
ppi
2015/06/03 17:11:17
nit: maybe add a blank line afterwards?
qsr
2015/06/04 09:16:15
Done.
|
| + String[] accountTypes = new String[] {GOOGLE_ACCOUNT_TYPE}; |
| String message = null; |
| if (mConsumerURL.equals("")) { |
| message = "Select an account to use with mojo shell"; |
| @@ -175,6 +205,7 @@ behalf. |
| } |
| mPendingCallback.call(username, error); |
| mPendingCallback = null; |
| + updateDb(username); |
| } |
| }, new RegisterActivityResultReceiverCallback(accountPickerIntent)); |
| } |
| @@ -197,4 +228,62 @@ behalf. |
| p.setDataPosition(0); |
| return Intent.CREATOR.createFromParcel(p); |
| } |
| + |
| + private File getDbFile() { |
| + if (mDbFile != null) { |
| + return mDbFile; |
| + } |
| + File home = new File(System.getenv("HOME")); |
| + File configDir = new File(home, ".mojo_authentication"); |
| + configDir.mkdirs(); |
| + mDbFile = new File(configDir, "db"); |
| + return mDbFile; |
| + } |
| + |
| + private Db getDb() { |
| + if (mDb != null) { |
| + return mDb; |
| + } |
| + File dbFile = getDbFile(); |
| + if (dbFile.exists()) { |
| + try { |
| + int size = (int) dbFile.length(); |
| + try (FileInputStream stream = new FileInputStream(dbFile); |
| + FileChannel channel = stream.getChannel()) { |
| + Db db = Db.deserialize(new Message( |
| + channel.map(MapMode.READ_ONLY, 0, size), new ArrayList<Handle>())); |
| + if (db.version == VERSION) { |
| + mDb = db; |
| + return mDb; |
| + } |
| + } catch (DeserializationException e) { |
| + } |
| + dbFile.delete(); |
| + } catch (IOException e) { |
| + } |
| + } |
| + mDb = new Db(); |
| + mDb.version = VERSION; |
| + mDb.lastSelectedAccounts = new HashMap<>(); |
| + return mDb; |
| + } |
| + |
| + private void updateDb(String username) { |
| + try { |
| + Db db = getDb(); |
| + if (username == null) { |
| + db.lastSelectedAccounts.remove(mConsumerURL); |
| + } else { |
| + db.lastSelectedAccounts.put(mConsumerURL, username); |
| + } |
| + Message m = db.serialize(null); |
|
ppi
2015/06/03 17:11:17
At the first sight, it is puzzling to see Message
qsr
2015/06/04 09:16:15
Done.
|
| + File dbFile = getDbFile(); |
| + dbFile.delete(); |
| + try (FileOutputStream stream = new FileOutputStream(dbFile); |
| + FileChannel channel = stream.getChannel()) { |
| + channel.write(m.getData()); |
| + } |
| + } catch (IOException e) { |
| + } |
| + } |
| } |