Bug 1449821 - 1. Remove legacy Messaging.jsm APIs; r?esawin draft
authorJim Chen <nchen@mozilla.com>
Thu, 05 Apr 2018 18:50:11 -0400
changeset 778191 2906de78ca9699f2edb72f93bcb5c3b4aaccb45e
parent 778190 c87c2502deeb71745724ca0b0293cdf504ea1c48
child 778192 a2a10096d5c4fa85d7b55c3e770426f0ed833156
push id105421
push userbmo:nchen@mozilla.com
push dateThu, 05 Apr 2018 22:53:25 +0000
reviewersesawin
bugs1449821
milestone61.0a1
Bug 1449821 - 1. Remove legacy Messaging.jsm APIs; r?esawin There are some legacy APIs in Messaging.jsm that we can remove, now that we no longer have to support legacy add-ons. MozReview-Commit-ID: 3Gfg12hmkN7
mobile/android/modules/geckoview/Messaging.jsm
toolkit/content/aboutSupport.js
widget/android/AndroidBridge.cpp
widget/android/nsIAndroidBridge.idl
--- a/mobile/android/modules/geckoview/Messaging.jsm
+++ b/mobile/android/modules/geckoview/Messaging.jsm
@@ -1,39 +1,25 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 "use strict";
 
 ChromeUtils.import("resource://gre/modules/Services.jsm");
 ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
 
-var EXPORTED_SYMBOLS = ["sendMessageToJava", "Messaging", "EventDispatcher"];
-
-ChromeUtils.defineModuleGetter(this, "Task", "resource://gre/modules/Task.jsm");
+var EXPORTED_SYMBOLS = ["EventDispatcher"];
 
 XPCOMUtils.defineLazyServiceGetter(this, "UUIDGen",
                                    "@mozilla.org/uuid-generator;1",
                                    "nsIUUIDGenerator");
 
 const IS_PARENT_PROCESS = (Services.appinfo.processType ==
                            Services.appinfo.PROCESS_TYPE_DEFAULT);
 
-function sendMessageToJava(aMessage, aCallback) {
-  Cu.reportError("sendMessageToJava is deprecated. Use EventDispatcher instead.");
-
-  if (aCallback) {
-    EventDispatcher.instance.sendRequestForResult(aMessage)
-      .then(result => aCallback(result, null),
-            error => aCallback(null, error));
-  } else {
-    EventDispatcher.instance.sendRequest(aMessage);
-  }
-}
-
 function DispatcherDelegate(aDispatcher, aMessageManager) {
   this._dispatcher = aDispatcher;
   this._messageManager = aMessageManager;
 }
 
 DispatcherDelegate.prototype = {
   /**
    * Register a listener to be notified of event(s).
@@ -98,20 +84,16 @@ DispatcherDelegate.prototype = {
         }
       });
     }
 
     mm.sendAsyncMessage("GeckoView:Messaging", forwardData);
   },
 
   /**
-   * Implementations of Messaging APIs for backwards compatibility.
-   */
-
-  /**
    * Sends a request to Java.
    *
    * @param msg Message to send; must be an object with a "type" property
    */
   sendRequest: function(msg, callback) {
     let type = msg.type;
     msg.type = undefined;
     this.dispatch(type, msg, callback);
@@ -129,91 +111,16 @@ DispatcherDelegate.prototype = {
       msg.type = undefined;
 
       this.dispatch(type, msg, {
         onSuccess: resolve,
         onError: reject,
       });
     });
   },
-
-  /**
-   * Add a listener for the given event.
-   *
-   * Only one request listener can be registered for a given event.
-   *
-   * Example usage:
-   *   // aData is data sent from Java with the request. The return value is
-   *   // used to respond to the request. The return type *must* be an instance
-   *   // of Object.
-   *   let listener = function (aData) {
-   *     if (aData == "foo") {
-   *       return { response: "bar" };
-   *     }
-   *     return {};
-   *   };
-   *   EventDispatcher.instance.addListener(listener, "Demo:Request");
-   *
-   * The listener may also be a generator function, useful for performing a
-   * task asynchronously. For example:
-   *   let listener = function* (aData) {
-   *     // Respond with "bar" after 2 seconds.
-   *     yield new Promise(resolve => setTimeout(resolve, 2000));
-   *     return { response: "bar" };
-   *   };
-   *   EventDispatcher.instance.addListener(listener, "Demo:Request");
-   *
-   * @param listener Listener callback taking a single data parameter
-   *                 (see example usage above).
-   * @param event    Event name that this listener should observe.
-   */
-  addListener: function(listener, event) {
-    if (this._requestHandler.listeners[event]) {
-      throw new Error("Error in addListener: A listener already exists for event " + event);
-    }
-    if (typeof listener !== "function") {
-      throw new Error("Error in addListener: Listener must be a function for event " + event);
-    }
-
-    this._requestHandler.listeners[event] = listener;
-    this.registerListener(this._requestHandler, event);
-  },
-
-  /**
-   * Removes a listener for a given event.
-   *
-   * @param event The event to stop listening for.
-   */
-  removeListener: function(event) {
-    if (!this._requestHandler.listeners[event]) {
-      throw new Error("Error in removeListener: There is no listener for event " + event);
-    }
-
-    this._requestHandler.listeners[event] = undefined;
-    this.unregisterListener(this._requestHandler, event);
-  },
-
-  _requestHandler: {
-    listeners: {},
-
-    onEvent: function(event, data, callback) {
-      let self = this;
-      Task.spawn(function* () {
-        return yield self.listeners[event](data.data);
-      }).then(response => {
-        callback.onSuccess(response);
-      }, e => {
-        Cu.reportError("Error in Messaging handler for " + event + ": " + e);
-        callback.onError({
-          message: e.message || (e && e.toString()),
-          stack: e.stack || Components.stack.formattedStack,
-        });
-      });
-    },
-  },
 };
 
 var EventDispatcher = {
   instance: new DispatcherDelegate(IS_PARENT_PROCESS ? Services.androidBridge : undefined),
 
   /**
    * Return an EventDispatcher instance for a chrome DOM window. In a content
    * process, return a proxy through the message manager that automatically
@@ -278,38 +185,8 @@ var EventDispatcher = {
     dispatcher.dispatch(aMsg.data.event, aMsg.data.data, callback);
   },
 };
 
 if (IS_PARENT_PROCESS) {
   Services.mm.addMessageListener("GeckoView:Messaging", EventDispatcher);
   Services.ppmm.addMessageListener("GeckoView:Messaging", EventDispatcher);
 }
-
-// For backwards compatibility.
-var Messaging = {};
-
-function _addMessagingGetter(name) {
-  Messaging[name] = function() {
-    Cu.reportError("Messaging." + name + " is deprecated. " +
-                   "Use EventDispatcher object instead.");
-
-    // Try global dispatcher first.
-    let ret = EventDispatcher.instance[name].apply(EventDispatcher.instance, arguments);
-    if (ret) {
-      // For sendRequestForResult, return the global dispatcher promise.
-      return ret;
-    }
-
-    // Now try the window dispatcher.
-    let window = Services.wm.getMostRecentWindow("navigator:browser");
-    let dispatcher = window && window.WindowEventDispatcher;
-    let func = dispatcher && dispatcher[name];
-    if (typeof func === "function") {
-      return func.apply(dispatcher, arguments);
-    }
-  };
-}
-
-_addMessagingGetter("sendRequest");
-_addMessagingGetter("sendRequestForResult");
-_addMessagingGetter("addListener");
-_addMessagingGetter("removeListener");
--- a/toolkit/content/aboutSupport.js
+++ b/toolkit/content/aboutSupport.js
@@ -907,23 +907,20 @@ function copyRawDataToClipboard(button) 
       str.data = JSON.stringify(snapshot, undefined, 2);
       let transferable = Cc["@mozilla.org/widget/transferable;1"].
                          createInstance(Ci.nsITransferable);
       transferable.init(getLoadContext());
       transferable.addDataFlavor("text/unicode");
       transferable.setTransferData("text/unicode", str, str.data.length * 2);
       Services.clipboard.setData(transferable, null, Ci.nsIClipboard.kGlobalClipboard);
       if (AppConstants.platform == "android") {
-        // Present a toast notification.
-        let message = {
-          type: "Toast:Show",
-          message: stringBundle().GetStringFromName("rawDataCopied"),
-          duration: "short"
-        };
-        Services.androidBridge.handleGeckoMessage(message);
+        // Present a snackbar notification.
+        ChromeUtils.import("resource://gre/modules/Snackbars.jsm");
+        Snackbars.show(stringBundle().GetStringFromName("rawDataCopied"),
+                       Snackbars.LENGTH_SHORT);
       }
     });
   } catch (err) {
     if (button)
       button.disabled = false;
     throw err;
   }
 }
@@ -958,23 +955,20 @@ function copyContentsToClipboard() {
   transferable.addDataFlavor("text/unicode");
   ssText.data = dataText;
   transferable.setTransferData("text/unicode", ssText, dataText.length * 2);
 
   // Store the data into the clipboard.
   Services.clipboard.setData(transferable, null, Services.clipboard.kGlobalClipboard);
 
   if (AppConstants.platform == "android") {
-    // Present a toast notification.
-    let message = {
-      type: "Toast:Show",
-      message: stringBundle().GetStringFromName("textCopied"),
-      duration: "short"
-    };
-    Services.androidBridge.handleGeckoMessage(message);
+    // Present a snackbar notification.
+    ChromeUtils.import("resource://gre/modules/Snackbars.jsm");
+    Snackbars.show(stringBundle().GetStringFromName("textCopied"),
+                   Snackbars.LENGTH_SHORT);
   }
 }
 
 // Return the plain text representation of an element.  Do a little bit
 // of pretty-printing to make it human-readable.
 function createTextForElement(elem) {
   let serializer = new Serializer();
   let text = serializer.serialize(elem);
--- a/widget/android/AndroidBridge.cpp
+++ b/widget/android/AndroidBridge.cpp
@@ -672,56 +672,16 @@ nsAndroidBridge::nsAndroidBridge() :
 
   AddObservers();
 }
 
 nsAndroidBridge::~nsAndroidBridge()
 {
 }
 
-NS_IMETHODIMP nsAndroidBridge::HandleGeckoMessage(JS::HandleValue val,
-                                                  JSContext *cx)
-{
-    // Spit out a warning before sending the message.
-    nsContentUtils::ReportToConsoleNonLocalized(
-        NS_LITERAL_STRING("Use of handleGeckoMessage is deprecated. "
-                          "Please use EventDispatcher from Messaging.jsm."),
-        nsIScriptError::warningFlag,
-        NS_LITERAL_CSTRING("nsIAndroidBridge"),
-        nullptr);
-
-    JS::RootedValue jsonVal(cx);
-
-    if (val.isObject()) {
-        jsonVal = val;
-
-    } else {
-        // Handle legacy JSON messages.
-        if (!val.isString()) {
-            return NS_ERROR_INVALID_ARG;
-        }
-        JS::RootedString jsonStr(cx, val.toString());
-
-        if (!JS_ParseJSON(cx, jsonStr, &jsonVal) || !jsonVal.isObject()) {
-            JS_ClearPendingException(cx);
-            return NS_ERROR_INVALID_ARG;
-        }
-    }
-
-    JS::RootedObject jsonObj(cx, &jsonVal.toObject());
-    JS::RootedValue typeVal(cx);
-
-    if (!JS_GetProperty(cx, jsonObj, "type", &typeVal)) {
-        JS_ClearPendingException(cx);
-        return NS_ERROR_INVALID_ARG;
-    }
-
-    return Dispatch(typeVal, jsonVal, /* callback */ nullptr, cx);
-}
-
 NS_IMETHODIMP nsAndroidBridge::ContentDocumentChanged(mozIDOMWindowProxy* aWindow)
 {
     AndroidBridge::Bridge()->ContentDocumentChanged(aWindow);
     return NS_OK;
 }
 
 NS_IMETHODIMP nsAndroidBridge::IsContentDocumentDisplayed(mozIDOMWindowProxy* aWindow,
                                                           bool *aRet)
--- a/widget/android/nsIAndroidBridge.idl
+++ b/widget/android/nsIAndroidBridge.idl
@@ -65,13 +65,12 @@ interface nsIAndroidEventDispatcher : ns
 interface nsIAndroidView : nsIAndroidEventDispatcher
 {
   [implicit_jscontext] readonly attribute jsval settings;
 };
 
 [scriptable, uuid(1beb70d3-70f3-4742-98cc-a3d301b26c0c)]
 interface nsIAndroidBridge : nsIAndroidEventDispatcher
 {
-  [implicit_jscontext] void handleGeckoMessage(in jsval message);
   attribute nsIAndroidBrowserApp browserApp;
   void contentDocumentChanged(in mozIDOMWindowProxy window);
   boolean isContentDocumentDisplayed(in mozIDOMWindowProxy window);
 };