From 93b9588643c0afcc14ea6241a9acbff8313be231 Mon Sep 17 00:00:00 2001 From: Collin Jackson Date: Tue, 26 Jan 2016 16:26:04 -0800 Subject: [PATCH] Android implementation of the remaining ::firebase::Firebase Mojo APIs --- sky/services/firebase/firebase.mojom | 18 +- .../org/domokit/firebase/FirebaseImpl.java | 175 ++++++++++++++++-- 2 files changed, 174 insertions(+), 19 deletions(-) diff --git a/sky/services/firebase/firebase.mojom b/sky/services/firebase/firebase.mojom index 0f483b4ac37..b7c8f26ab41 100644 --- a/sky/services/firebase/firebase.mojom +++ b/sky/services/firebase/firebase.mojom @@ -45,12 +45,24 @@ interface ChildEventListener { [ServiceName="firebase::Firebase"] interface Firebase { InitWithUrl(string url); - GetRoot(Firebase& root); - GetChild(string path, Firebase& child); AddValueEventListener(ValueEventListener listener); AddChildEventListener(ChildEventListener listener); ObserveSingleEventOfType(EventType eventType) => (DataSnapshot snapshot); + AuthWithCustomToken(string token) => (Error? error, AuthData? authData); + AuthAnonymously() => (Error? error, AuthData? authData); AuthWithOAuthToken(string provider, string credentials) => (Error? error, AuthData? authData); AuthWithPassword(string email, string password) => (Error? error, AuthData? authData); - SetValue(string jsonValue) => (Error? error); + Unauth() => (Error? error); + GetChild(string path, Firebase& child); + GetParent(Firebase& parent); + GetRoot(Firebase& root); + SetValue(string jsonValue, int32 priority, bool hasPriority) => (Error? error); + RemoveValue() => (Error? error); + Push(Firebase& child) => (string key); + SetPriority(int32 priority) => (Error? error); + CreateUser(string email, string password) => (Error? error, string? jsonValue); + ChangeEmail(string oldEmail, string password, string newEmail) => (Error? error); + ChangePassword(string newPassword, string email, string oldPassword) => (Error? error); + RemoveUser(string email, string password) => (Error? error); + ResetPassword(string email) => (Error? error); }; diff --git a/sky/services/firebase/src/org/domokit/firebase/FirebaseImpl.java b/sky/services/firebase/src/org/domokit/firebase/FirebaseImpl.java index fc45dd98c60..291c37a0f76 100644 --- a/sky/services/firebase/src/org/domokit/firebase/FirebaseImpl.java +++ b/sky/services/firebase/src/org/domokit/firebase/FirebaseImpl.java @@ -11,6 +11,8 @@ import com.firebase.client.AuthData; import com.firebase.client.FirebaseError; import com.firebase.client.Firebase.AuthResultHandler; import com.firebase.client.Firebase.CompletionListener; +import com.firebase.client.Firebase.ResultHandler; +import com.firebase.client.Firebase.ValueResultHandler; import java.io.IOException; import java.util.ArrayList; @@ -59,20 +61,6 @@ public class FirebaseImpl implements org.chromium.mojom.firebase.Firebase { mClient = new com.firebase.client.Firebase(url); } - @Override - public void getRoot(InterfaceRequest request) { - FirebaseImpl root = new FirebaseImpl(mContext); - root.mClient = mClient.getRoot(); - Firebase.MANAGER.bind(root, request); - } - - @Override - public void getChild(String path, InterfaceRequest request) { - FirebaseImpl child = new FirebaseImpl(mContext); - child.mClient = mClient.child(path); - Firebase.MANAGER.bind(child, request); - } - @Override public void addValueEventListener(org.chromium.mojom.firebase.ValueEventListener listener) { final org.chromium.mojom.firebase.ValueEventListener.Proxy proxy = @@ -148,6 +136,32 @@ public class FirebaseImpl implements org.chromium.mojom.firebase.Firebase { }); } + @Override + public void authWithCustomToken(String token, final AuthWithCustomTokenResponse response) { + mClient.authWithCustomToken(token, new AuthResultHandler() { + @Override + public void onAuthenticated(AuthData authData) { + response.call(null, toMojoAuthData(authData)); + } + public void onAuthenticationError(FirebaseError error) { + response.call(toMojoError(error), null); + } + }); + } + + @Override + public void authAnonymously(final AuthAnonymouslyResponse response) { + mClient.authAnonymously(new AuthResultHandler() { + @Override + public void onAuthenticated(AuthData authData) { + response.call(null, toMojoAuthData(authData)); + } + public void onAuthenticationError(FirebaseError error) { + response.call(toMojoError(error), null); + } + }); + } + @Override public void authWithOAuthToken(String provider, String credentials, final AuthWithOAuthTokenResponse response) { mClient.authWithOAuthToken(provider, credentials, new AuthResultHandler() { @@ -175,11 +189,42 @@ public class FirebaseImpl implements org.chromium.mojom.firebase.Firebase { } @Override - public void setValue(String jsonValue, final SetValueResponse response) { + public void unauth(final UnauthResponse response) { + mClient.unauth(new CompletionListener() { + @Override + public void onComplete(FirebaseError error, com.firebase.client.Firebase ref) { + response.call(toMojoError(error)); + } + }); + } + + @Override + public void getChild(String path, InterfaceRequest request) { + FirebaseImpl child = new FirebaseImpl(mContext); + child.mClient = mClient.child(path); + Firebase.MANAGER.bind(child, request); + } + + @Override + public void getParent(InterfaceRequest request) { + FirebaseImpl parent = new FirebaseImpl(mContext); + parent.mClient = mClient.getParent(); + Firebase.MANAGER.bind(parent, request); + } + + @Override + public void getRoot(InterfaceRequest request) { + FirebaseImpl root = new FirebaseImpl(mContext); + root.mClient = mClient.getRoot(); + Firebase.MANAGER.bind(root, request); + } + + @Override + public void setValue(String jsonValue, int priority, boolean hasPriority, final SetValueResponse response) { try { JSONObject root = new JSONObject(jsonValue); Object value = toMap(root).get("value"); - mClient.setValue(value, null, new CompletionListener() { + mClient.setValue(value, hasPriority ? priority : null, new CompletionListener() { @Override public void onComplete(FirebaseError error, com.firebase.client.Firebase ref) { response.call(toMojoError(error)); @@ -195,6 +240,104 @@ public class FirebaseImpl implements org.chromium.mojom.firebase.Firebase { } } + @Override + public void removeValue(final RemoveValueResponse response) { + mClient.removeValue(new CompletionListener() { + @Override + public void onComplete(FirebaseError error, com.firebase.client.Firebase ref) { + response.call(toMojoError(error)); + } + }); + } + + @Override + public void push(InterfaceRequest request, final PushResponse response) { + FirebaseImpl child = new FirebaseImpl(mContext); + child.mClient = mClient.push(); + Firebase.MANAGER.bind(child, request); + response.call(child.mClient.getKey()); + } + + @Override + public void setPriority(int priority, final SetPriorityResponse response) { + mClient.setPriority(priority, new CompletionListener() { + @Override + public void onComplete(FirebaseError error, com.firebase.client.Firebase ref) { + response.call(toMojoError(error)); + } + }); + } + + @Override + public void createUser(String email, String password, final CreateUserResponse response) { + mClient.createUser(email, password, new ValueResultHandler>() { + @Override + public void onError(FirebaseError error) { + response.call(toMojoError(error), null); + } + @Override + public void onSuccess(Map result) { + response.call(null, new JSONObject(result).toString()); + } + }); + } + + @Override + public void changeEmail(String oldEmail, String password, String newExample, final ChangeEmailResponse response) { + mClient.changeEmail(oldEmail, password, newExample, new ResultHandler() { + @Override + public void onError(FirebaseError error) { + response.call(toMojoError(error)); + } + @Override + public void onSuccess() { + response.call(null); + } + }); + } + + @Override + public void changePassword(String newPassword, String email, String oldPassword, final ChangePasswordResponse response) { + mClient.changePassword(newPassword, email, oldPassword, new ResultHandler() { + @Override + public void onError(FirebaseError error) { + response.call(toMojoError(error)); + } + @Override + public void onSuccess() { + response.call(null); + } + }); + } + + @Override + public void removeUser(String email, String password, final RemoveUserResponse response) { + mClient.removeUser(email, password, new ResultHandler() { + @Override + public void onError(FirebaseError error) { + response.call(toMojoError(error)); + } + @Override + public void onSuccess() { + response.call(null); + } + }); + } + + @Override + public void resetPassword(String email, final ResetPasswordResponse response) { + mClient.resetPassword(email, new ResultHandler() { + @Override + public void onError(FirebaseError error) { + response.call(toMojoError(error)); + } + @Override + public void onSuccess() { + response.call(null); + } + }); + } + DataSnapshot toMojoSnapshot(com.firebase.client.DataSnapshot snapshot) { DataSnapshot mojoSnapshot = new DataSnapshot(); mojoSnapshot.key = snapshot.getKey();