Bug 1468048 - Make GeckoSession.saveState() return a GeckoResult r=droeh,esawin draft
authorJames Willcox <snorp@snorp.net>
Sat, 09 Jun 2018 20:24:46 -0500
changeset 814639 99559b0b43994eadb47c574bb559d7d3ac8c74d7
parent 814638 0b17d1e111679869d1cbc2eac3e59d70d63e7c42
child 814640 956d802e22f04eca979c66f3c5593387ee57902d
push id115291
push userbmo:snorp@snorp.net
push dateThu, 05 Jul 2018 20:44:33 +0000
reviewersdroeh, esawin
bugs1468048
milestone63.0a1
Bug 1468048 - Make GeckoSession.saveState() return a GeckoResult r=droeh,esawin MozReview-Commit-ID: Ck6EZcaUeV2
mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoSession.java
--- a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoSession.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoSession.java
@@ -1142,32 +1142,27 @@ public class GeckoSession extends LayerS
     }
 
     /**
      * Save the current browsing session state of this GeckoSession. This session state
      * includes the history, scroll position, zoom, and any form data that has been entered,
      * but does not include information pertaining to the GeckoSession itself (for example,
      * this does not include settings on the GeckoSession).
      *
-     * @param response This is a response which will be called with the state once it has been
-     *                 saved. Can be null if we fail to save the state for any reason.
+     * @return A {@link GeckoResult} containing the {@link SessionState}
      */
-    public void saveState(final GeckoResponse<SessionState> response) {
-        mEventDispatcher.dispatch("GeckoView:SaveState", null, new EventCallback() {
+    public GeckoResult<SessionState> saveState() {
+        CallbackResult<SessionState> result = new CallbackResult<SessionState>() {
             @Override
-            public void sendSuccess(final Object result) {
-                response.respond(new SessionState((String) result));
+            public void sendSuccess(final Object value) {
+                complete(new SessionState((String)value));
             }
-
-            @Override
-            public void sendError(final Object result) {
-                Log.w(LOGTAG, "Failed to save state, as another save is already in progress.");
-                response.respond(null);
-            }
-        });
+        };
+        mEventDispatcher.dispatch("GeckoView:SaveState", null, result);
+        return result;
     }
 
     /**
      * Restore a saved state to this GeckoSession; only data that is saved (history, scroll
      * position, zoom, and form data) will be restored. These will overwrite the corresponding
      * state of this GeckoSession.
      *
      * @param state A saved session state; this should originate from GeckoSession.saveState().
@@ -3021,9 +3016,20 @@ public class GeckoSession extends LayerS
          * {@link android.view.inputmethod.InputConnection#requestCursorUpdates}.
          * Consequently, this method is <i>not</i> called in viewless mode.
          *
          * @param session Session instance.
          * @param info Cursor-anchor information.
          */
         void updateCursorAnchorInfo(@NonNull GeckoSession session, @NonNull CursorAnchorInfo info);
     }
+
+    private static abstract class CallbackResult<T> extends GeckoResult<T> implements EventCallback {
+
+        @Override
+        public abstract void sendSuccess(Object response);
+
+        @Override
+        public void sendError(Object response) {
+            completeExceptionally(new Exception(response.toString()));
+        }
+    }
 }