Bug 1395118 - Replace use of nsIUnicharStreamLoader in calendar. r=philipp
authorMakeMyDay <makemyday@gmx-topmail.de>
Thu, 15 Nov 2018 15:41:41 +0100
changeset 33722 465c90ec3466efeac4e52f3696d889069f23ca2a
parent 33721 1a1cb39d41c20a8e8a0c5a3dee4c583c7ef35a04
child 33723 e19bd82baf3c10ea00b1fef5754b3e2021646609
push id388
push userclokep@gmail.com
push dateMon, 28 Jan 2019 20:54:56 +0000
reviewersphilipp
bugs1395118
Bug 1395118 - Replace use of nsIUnicharStreamLoader in calendar. r=philipp
calendar/base/content/calendar-dnd-listener.js
calendar/providers/wcap/calWcapRequest.js
--- a/calendar/base/content/calendar-dnd-listener.js
+++ b/calendar/base/content/calendar-dnd-listener.js
@@ -286,53 +286,44 @@ calDNDBaseObserver.prototype = {
                     inputStream.close();
                 }
 
                 break;
             }
             case "application/x-moz-file-promise":
             case "text/x-moz-url": {
                 let uri = Services.io.newURI(data.toString());
-                let loader = Cc["@mozilla.org/network/unichar-stream-loader;1"]
-                             .createInstance(Ci.nsIUnicharStreamLoader);
+                let loader = cal.provider.createStreamLoader();
                 let channel = Services.io.newChannelFromURI2(
                     uri,
                     null,
                     Services.scriptSecurityManager.getSystemPrincipal(),
                     null,
                     Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
                     Ci.nsIContentPolicy.TYPE_OTHER
                 );
                 channel.loadFlags |= Ci.nsIRequest.LOAD_BYPASS_CACHE;
 
                 let self = this;
 
                 let listener = {
-                    // nsIUnicharStreamLoaderObserver:
-                    onDetermineCharset: function(aLoader, context, firstSegment, aLength) {
-                        let charset = null;
-                        if (aLoader && aLoader.channel) {
-                            charset = channel.contentCharset;
-                        }
-                        if (!charset || charset.length == 0) {
-                            charset = "UTF-8";
-                        }
-                        return charset;
-                    },
-
-                    onStreamComplete: function(aLoader, context, status, unicharString) {
+                    onStreamComplete: function(aLoader, aContext, aStatus, aResultLength, aResult) {
                         let parser = Cc["@mozilla.org/calendar/ics-parser;1"]
                                      .createInstance(Ci.calIIcsParser);
-                        parser.parseString(unicharString);
+                        let encoding = channel.contentCharset || "utf-8";
+                        let result = aResultLength
+                                   ? new TextDecoder(encoding).decode(Uint8Array.from(aResult))
+                                   : "";
+                        parser.parseString(result);
                         self.onDropItems(parser.getItems({}).concat(parser.getParentlessItems({})));
                     }
                 };
 
                 try {
-                    loader.init(listener, Ci.nsIUnicharStreamLoader.DEFAULT_SEGMENT_SIZE);
+                    loader.init(listener);
                     channel.asyncOpen(loader, null);
                 } catch (e) {
                     cal.ERROR(e);
                 }
                 break;
             }
             case "text/x-moz-message":
                 this.onDropMessage(messenger.msgHdrFromURI(data));
--- a/calendar/providers/wcap/calWcapRequest.js
+++ b/calendar/providers/wcap/calWcapRequest.js
@@ -188,17 +188,16 @@ function calWcapNetworkRequest(url, resp
     this.wrappedJSObject = this;
     this.m_id = generateRequestId();
     this.m_url = url;
     this.m_respFunc = respFunc;
     this.m_bLogging = (bLogging === undefined ? true : bLogging);
 }
 var calWcapNetworkRequestClassID = Components.ID("{e3c62b37-83cf-41ec-9872-0af9f952430a}");
 var calWcapNetworkRequestInterfaces = [
-    Components.interfaces.nsIUnicharStreamLoaderObserver,
     Components.interfaces.nsIInterfaceRequestor,
     Components.interfaces.nsIChannelEventSink,
     Components.interfaces.calIOperation,
 ];
 calWcapNetworkRequest.prototype = {
     m_id: 0,
     m_url: null,
     m_loader: null,
@@ -237,64 +236,47 @@ calWcapNetworkRequest.prototype = {
      * @see nsIChannelEventSink
      */
     asyncOnChannelRedirect: function(aOldChannel, aNewChannel, aFlags, aCallback) {
         // all we need to do to the new channel is the basic preparation
         this.prepareChannel(aNewChannel);
         aCallback.onRedirectVerifyCallback(Components.results.NS_OK);
     },
 
-    /**
-     * @see nsIUnicharStreamLoaderObserver
-     */
-    onDetermineCharset: function(loader, context, firstSegment, length) {
-        let channel = null;
-        if (loader) {
-            channel = loader.channel;
-        }
-        let charset = null;
-        if (channel) {
-            charset = channel.contentCharset;
-        }
-        if (!charset || charset.length == 0) {
-            charset = "UTF-8";
-        }
-        return charset;
-    },
-
-    /**
-     * @see nsIUnicharStreamLoaderObserver
-     */
-    onStreamComplete: function(aLoader, aContext, aStatus, unicharData) {
+    onStreamComplete: function(aLoader, aContext, aStatus, aResultLength, aResult) {
         this.m_loader = null;
 
         if (LOG_LEVEL > 0 && this.m_bLogging) {
             log("status: " + errorToString(aStatus), this);
         }
         if (aStatus != Components.results.NS_OK) {
             this.execRespFunc(aStatus);
             return;
         }
 
+        let httpChannel = aLoader.request.QueryInterface(Ci.nsIHttpChannel);
+        let encoding = httpChannel.contentCharset || "utf-8";
+        let result = aResultLength
+                   ? new TextDecoder(encoding).decode(Uint8Array.from(aResult))
+                   : "";
+
         if (LOG_LEVEL > 2 && this.m_bLogging) {
-            log("contentCharset = " + aLoader.charset + "\nrequest result:\n" + unicharData, this);
+            log("contentCharset = " + encoding + "\nrequest result:\n" + result, this);
         }
-
-        let httpChannel = aLoader.channel.QueryInterface(Components.interfaces.nsIHttpChannel);
         switch (httpChannel.responseStatus / 100) {
             case 2: /* 2xx codes */
                 // Everything worked out, we are done
-                this.execRespFunc(aStatus, unicharData);
+                this.execRespFunc(aStatus, aResult);
                 break;
             default: {
                 // Something else went wrong
                 let error = "A request Error Occurred. Status Code: " +
                             httpChannel.responseStatus + " " +
                             httpChannel.responseStatusText + " Body: " +
-                            unicharData;
+                            result;
                 this.execRespFunc(Components.Exception(error, NS_BINDING_FAILED));
                 break;
             }
         }
     },
 
     toString: function() {
         let ret = "calWcapNetworkRequest id=" + this.id +
@@ -402,23 +384,21 @@ function issueNetworkRequest(parentReque
                                                      Services.scriptSecurityManager.getSystemPrincipal(),
                                                      null,
                                                      Components.interfaces.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
                                                      Components.interfaces.nsIContentPolicy.TYPE_OTHER);
         netRequest.prepareChannel(channel);
         channel = channel.QueryInterface(Components.interfaces.nsIHttpChannel);
         channel.redirectionLimit = 3;
         channel.notificationCallbacks = netRequest;
-        let loader = Components.classes["@mozilla.org/network/unichar-stream-loader;1"]
-                               .createInstance(Components.interfaces.nsIUnicharStreamLoader);
+        let loader = cal.provider.createStreamLoader();
         netRequest.m_loader = loader;
 
         log("opening channel.", netRequest);
-        loader.init(netRequest,
-                    Components.interfaces.nsIUnicharStreamLoader.DEFAULT_SEGMENT_SIZE);
+        loader.init(netRequest);
         channel.asyncOpen(loader, null);
     } catch (exc) {
         netRequest.execRespFunc(exc);
     }
 }
 
 function getWcapRequestStatusString(xml) {
     let str = "request status: ";