Bug 1257339 - Bring back deprecated newChannel() API on nsIIOService. r=sicking, r=mcmanus CLOSED TREE
authorChristoph Kerschbaumer <mozilla@christophkerschbaumer.com>
Fri, 18 Mar 2016 17:11:42 -0700
changeset 289432 c31503dab99df159c2ac2193c68bc1531b334ae7
parent 289431 57f5e012ebf3f88daae707a6aa05339c8c54dbf4
child 289433 b8375e4f91629ff2d645c1f916cc327e53ddde48
push id30102
push userryanvm@gmail.com
push dateSat, 19 Mar 2016 15:23:17 +0000
treeherdermozilla-central@720fb3d55e28 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssicking, mcmanus
bugs1257339
milestone48.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1257339 - Bring back deprecated newChannel() API on nsIIOService. r=sicking, r=mcmanus CLOSED TREE MozReview-Commit-ID: 8BVo3K0veND
netwerk/base/NetUtil.jsm
netwerk/base/nsIIOService.idl
netwerk/base/nsIIOService2.idl
netwerk/base/nsIOService.cpp
netwerk/locales/en-US/necko.properties
netwerk/test/unit/test_NetUtil.js
--- a/netwerk/base/NetUtil.jsm
+++ b/netwerk/base/NetUtil.jsm
@@ -262,22 +262,58 @@ this.NetUtil = {
      *            rendering is affected is the loadingPrincipal.
      *          securityFlags:
      *            The securityFlags of the channel.
      *            Any of the securityflags defined in nsILoadInfo.idl.
      *          contentPolicyType:
      *            The contentPolicyType of the channel.
      *            Any of the content types defined in nsIContentPolicy.idl.
      *        }
+     * @param aOriginCharset [deprecated]
+     *        The character set for the URI.  Only used if aWhatToLoad is a
+     *        string, which is a deprecated API.  Must be undefined otherwise.
+     *        Use NetUtil.newURI if you need to use this option.
+     * @param aBaseURI [deprecated]
+     *        The base URI for the spec.  Only used if aWhatToLoad is a string,
+     *        which is a deprecated API.  Must be undefined otherwise.  Use
+     *        NetUtil.newURI if you need to use this option.
      * @return an nsIChannel object.
      */
-    newChannel: function NetUtil_newChannel(aWhatToLoad)
+    newChannel: function NetUtil_newChannel(aWhatToLoad, aOriginCharset, aBaseURI)
     {
-        // Make sure the API is called using only the options object.
-        if (typeof aWhatToLoad != "object" || arguments.length != 1) {
+        // Check for the deprecated API first.
+        if (typeof aWhatToLoad == "string" ||
+            (aWhatToLoad instanceof Ci.nsIFile) ||
+            (aWhatToLoad instanceof Ci.nsIURI)) {
+
+            let uri = (aWhatToLoad instanceof Ci.nsIURI)
+                      ? aWhatToLoad
+                      : this.newURI(aWhatToLoad, aOriginCharset, aBaseURI);
+
+            // log deprecation warning for developers.
+            Services.console.logStringMessage(
+              "Warning: NetUtil.newChannel(uri) deprecated, please provide argument 'aWhatToLoad'");
+
+            // Provide default loadinfo arguments and call the new API.
+            let systemPrincipal =
+              Services.scriptSecurityManager.getSystemPrincipal();
+
+            return this.ioService.newChannelFromURI2(
+                     uri,
+                     null, // loadingNode
+                     systemPrincipal, // loadingPrincipal
+                     null, // triggeringPrincipal
+                     Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
+                     Ci.nsIContentPolicy.TYPE_OTHER);
+        }
+
+        // We are using the updated API, that requires only the options object.
+        if (typeof aWhatToLoad != "object" ||
+             aOriginCharset !== undefined ||
+             aBaseURI !== undefined) {
             throw new Components.Exception(
                 "newChannel requires a single object argument",
                 Cr.NS_ERROR_INVALID_ARG,
                 Components.stack.caller
             );
         }
 
         let { uri,
--- a/netwerk/base/nsIIOService.idl
+++ b/netwerk/base/nsIIOService.idl
@@ -148,16 +148,38 @@ interface nsIIOService : nsISupports
                            in nsIURI aBaseURI,
                            in nsIDOMNode aLoadingNode,
                            in nsIPrincipal aLoadingPrincipal,
                            in nsIPrincipal aTriggeringPrincipal,
                            in unsigned long aSecurityFlags,
                            in unsigned long aContentPolicyType);
 
     /**
+     * ***** DEPRECATED *****
+     * Please use NewChannelFromURI2()
+     *
+     * Creates a channel for a given URI.
+     *
+     * @param aURI nsIURI from which to make a channel
+     * @return reference to the new nsIChannel object
+     */
+    nsIChannel newChannelFromURI(in nsIURI aURI);
+
+    /**
+     * ***** DEPRECATED *****
+     * Please use newChannel2().
+     *
+     * Equivalent to newChannelFromURI(newURI(...))
+     */
+    nsIChannel newChannel(in AUTF8String aSpec,
+                          in string aOriginCharset,
+                          in nsIURI aBaseURI);
+
+
+    /**
      * Returns true if networking is in "offline" mode. When in offline mode, 
      * attempts to access the network will fail (although this does not 
      * necessarily correlate with whether there is actually a network 
      * available -- that's hard to detect without causing the dialer to 
      * come up).
      *
      * Changing this fires observer notifications ... see below.
      */
--- a/netwerk/base/nsIIOService2.idl
+++ b/netwerk/base/nsIIOService2.idl
@@ -98,9 +98,26 @@ interface nsIIOService2 : nsIIOService
                                               in nsIURI aProxyURI,
                                               in unsigned long aProxyFlags,
                                               in nsIDOMNode aLoadingNode,
                                               in nsIPrincipal aLoadingPrincipal,
                                               in nsIPrincipal aTriggeringPrincipal,
                                               in unsigned long aSecurityFlags,
                                               in unsigned long aContentPolicyType);
 
+  /**
+   * ***** DEPRECATED *****
+   * Please use newChannelFromURIWithProxyFlags2()
+   *
+   * Creates a channel for a given URI.
+   *
+   * @param aURI nsIURI from which to make a channel
+   * @param aProxyURI nsIURI to use for proxy resolution. Can be null in which
+   *        case aURI is used
+   * @param aProxyFlags flags from nsIProtocolProxyService to use
+   *        when resolving proxies for this new channel
+   * @return reference to the new nsIChannel object
+   */
+   nsIChannel newChannelFromURIWithProxyFlags(in nsIURI aURI,
+                                              in nsIURI aProxyURI,
+                                              in unsigned long aProxyFlags);
+
 };
--- a/netwerk/base/nsIOService.cpp
+++ b/netwerk/base/nsIOService.cpp
@@ -1,14 +1,15 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
 /* vim:set ts=4 sw=4 cindent et: */
 /* 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/. */
 
+#include "mozilla/ArrayUtils.h"
 #include "mozilla/DebugOnly.h"
 
 #include "nsIOService.h"
 #include "nsIDOMNode.h"
 #include "nsIProtocolHandler.h"
 #include "nsIFileProtocolHandler.h"
 #include "nscore.h"
 #include "nsIURI.h"
@@ -25,16 +26,17 @@
 #include "nsNetCID.h"
 #include "nsCRT.h"
 #include "nsSecCheckWrapChannel.h"
 #include "nsSimpleNestedURI.h"
 #include "nsTArray.h"
 #include "nsIConsoleService.h"
 #include "nsIUploadChannel2.h"
 #include "nsXULAppAPI.h"
+#include "nsIScriptError.h"
 #include "nsIScriptSecurityManager.h"
 #include "nsIProtocolProxyCallback.h"
 #include "nsICancelable.h"
 #include "nsINetworkLinkService.h"
 #include "nsPISocketTransportService.h"
 #include "nsAsyncRedirectVerifyHelper.h"
 #include "nsURLHelper.h"
 #include "nsPIDNSService.h"
@@ -46,16 +48,17 @@
 #include "mozilla/LoadInfo.h"
 #include "mozilla/net/NeckoCommon.h"
 #include "mozilla/Services.h"
 #include "mozilla/Telemetry.h"
 #include "mozilla/net/DNS.h"
 #include "CaptivePortalService.h"
 #include "ReferrerPolicy.h"
 #include "nsContentSecurityManager.h"
+#include "nsContentUtils.h"
 
 #ifdef MOZ_WIDGET_GONK
 #include "nsINetworkManager.h"
 #include "nsINetworkInterface.h"
 #endif
 
 #if defined(XP_WIN)
 #include "nsNativeConnectionHelper.h"
@@ -657,16 +660,51 @@ nsIOService::NewChannelFromURI2(nsIURI* 
                                             aLoadingNode,
                                             aLoadingPrincipal,
                                             aTriggeringPrincipal,
                                             aSecurityFlags,
                                             aContentPolicyType,
                                             result);
 }
 
+/*  ***** DEPRECATED *****
+ * please use NewChannelFromURI2 providing the right arguments for:
+ *        * aLoadingNode
+ *        * aLoadingPrincipal
+ *        * aTriggeringPrincipal
+ *        * aSecurityFlags
+ *        * aContentPolicyType
+ *
+ * See nsIIoService.idl for a detailed description of those arguments
+ */
+NS_IMETHODIMP
+nsIOService::NewChannelFromURI(nsIURI *aURI, nsIChannel **result)
+{
+  NS_ASSERTION(false, "Deprecated, use NewChannelFromURI2 providing loadInfo arguments!");
+
+  const char16_t* params[] = {
+    MOZ_UTF16("nsIOService::NewChannelFromURI()"),
+    MOZ_UTF16("nsIOService::NewChannelFromURI2()")
+  };
+  nsContentUtils::ReportToConsole(nsIScriptError::warningFlag,
+                                  NS_LITERAL_CSTRING("Security by Default"),
+                                  nullptr, // aDocument
+                                  nsContentUtils::eNECKO_PROPERTIES,
+                                  "APIDeprecationWarning",
+                                  params, ArrayLength(params));
+
+  return NewChannelFromURI2(aURI,
+                            nullptr, // aLoadingNode
+                            nsContentUtils::GetSystemPrincipal(),
+                            nullptr, // aTriggeringPrincipal
+                            nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
+                            nsIContentPolicy::TYPE_OTHER,
+                            result);
+}
+
 NS_IMETHODIMP
 nsIOService::NewChannelFromURIWithLoadInfo(nsIURI* aURI,
                                            nsILoadInfo* aLoadInfo,
                                            nsIChannel** result)
 {
   return NewChannelFromURIWithProxyFlagsInternal(aURI,
                                                  nullptr, // aProxyURI
                                                  0,       // aProxyFlags
@@ -836,16 +874,56 @@ nsIOService::NewChannelFromURIWithProxyF
     NS_ASSERTION(loadInfo, "Please pass security info when creating a channel");
     return NewChannelFromURIWithProxyFlagsInternal(aURI,
                                                    aProxyURI,
                                                    aProxyFlags,
                                                    loadInfo,
                                                    result);
 }
 
+/*  ***** DEPRECATED *****
+ * please use NewChannelFromURIWithProxyFlags2 providing the right arguments for:
+ *        * aLoadingNode
+ *        * aLoadingPrincipal
+ *        * aTriggeringPrincipal
+ *        * aSecurityFlags
+ *        * aContentPolicyType
+ *
+ * See nsIIoService.idl for a detailed description of those arguments
+ */
+NS_IMETHODIMP
+nsIOService::NewChannelFromURIWithProxyFlags(nsIURI *aURI,
+                                             nsIURI *aProxyURI,
+                                             uint32_t aProxyFlags,
+                                             nsIChannel **result)
+{
+  NS_ASSERTION(false, "Deprecated, use NewChannelFromURIWithProxyFlags2 providing loadInfo arguments!");
+
+  const char16_t* params[] = {
+    MOZ_UTF16("nsIOService::NewChannelFromURIWithProxyFlags()"),
+    MOZ_UTF16("nsIOService::NewChannelFromURIWithProxyFlags2()")
+  };
+  nsContentUtils::ReportToConsole(nsIScriptError::warningFlag,
+                                  NS_LITERAL_CSTRING("Security by Default"),
+                                  nullptr, // aDocument
+                                  nsContentUtils::eNECKO_PROPERTIES,
+                                  "APIDeprecationWarning",
+                                  params, ArrayLength(params));
+
+  return NewChannelFromURIWithProxyFlags2(aURI,
+                                          aProxyURI,
+                                          aProxyFlags,
+                                          nullptr, // aLoadingNode
+                                          nsContentUtils::GetSystemPrincipal(),
+                                          nullptr, // aTriggeringPrincipal
+                                          nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
+                                          nsIContentPolicy::TYPE_OTHER,
+                                          result);
+}
+
 NS_IMETHODIMP
 nsIOService::NewChannel2(const nsACString& aSpec,
                          const char* aCharset,
                          nsIURI* aBaseURI,
                          nsIDOMNode* aLoadingNode,
                          nsIPrincipal* aLoadingPrincipal,
                          nsIPrincipal* aTriggeringPrincipal,
                          uint32_t aSecurityFlags,
@@ -861,16 +939,54 @@ nsIOService::NewChannel2(const nsACStrin
                               aLoadingNode,
                               aLoadingPrincipal,
                               aTriggeringPrincipal,
                               aSecurityFlags,
                               aContentPolicyType,
                               result);
 }
 
+/*  ***** DEPRECATED *****
+ * please use NewChannel2 providing the right arguments for:
+ *        * aLoadingNode
+ *        * aLoadingPrincipal
+ *        * aTriggeringPrincipal
+ *        * aSecurityFlags
+ *        * aContentPolicyType
+ *
+ * See nsIIoService.idl for a detailed description of those arguments
+ */
+NS_IMETHODIMP
+nsIOService::NewChannel(const nsACString &aSpec, const char *aCharset, nsIURI *aBaseURI, nsIChannel **result)
+{
+  NS_ASSERTION(false, "Deprecated, use NewChannel2 providing loadInfo arguments!");
+
+  const char16_t* params[] = {
+    MOZ_UTF16("nsIOService::NewChannel()"),
+    MOZ_UTF16("nsIOService::NewChannel2()")
+  };
+  nsContentUtils::ReportToConsole(nsIScriptError::warningFlag,
+                                  NS_LITERAL_CSTRING("Security by Default"),
+                                  nullptr, // aDocument
+                                  nsContentUtils::eNECKO_PROPERTIES,
+                                  "APIDeprecationWarning",
+                                  params, ArrayLength(params));
+
+  // Call NewChannel2 providing default arguments for the loadInfo.
+  return NewChannel2(aSpec,
+                     aCharset,
+                     aBaseURI,
+                     nullptr, // aLoadingNode
+                     nsContentUtils::GetSystemPrincipal(), // aLoadingPrincipal
+                     nullptr, // aTriggeringPrincipal
+                     nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
+                     nsIContentPolicy::TYPE_OTHER,
+                     result);
+}
+
 bool
 nsIOService::IsLinkUp()
 {
     InitializeNetworkLinkService();
 
     if (!mNetworkLinkService) {
         // We cannot decide, assume the link is up
         return true;
--- a/netwerk/locales/en-US/necko.properties
+++ b/netwerk/locales/en-US/necko.properties
@@ -34,8 +34,12 @@ DirColMTime=Last Modified
 DirFileLabel=File: 
 
 PhishingAuth=You are about to visit "%1$S". This site may be attempting to trick you into thinking you are visiting a different site. Use extreme caution.
 PhishingAuthAccept=I understand and will be very careful
 SuperfluousAuth=You are about to log in to the site "%1$S" with the username "%2$S", but the website does not require authentication. This may be an attempt to trick you.\n\nIs "%1$S" the site you want to visit?
 AutomaticAuth=You are about to log in to the site "%1$S" with the username "%2$S".
 
 TrackingUriBlocked=The resource at "%1$S" was blocked because tracking protection is enabled.
+
+# LOCALIZATION NOTE (APIDeprecationWarning):
+# %1$S is the deprected API; %2$S is the API function that should be used.
+APIDeprecationWarning=Warning: '%1$S' deprecated, please use '%2$S'
--- a/netwerk/test/unit/test_NetUtil.js
+++ b/netwerk/test/unit/test_NetUtil.js
@@ -627,16 +627,76 @@ function test_newChannel_with_wrong_opti
       loadingPrincipal: systemPrincipal,
       loadUsingSystemPrincipal: true,
     });
   }, /does not accept 'loadUsingSystemPrincipal'/);
 
   run_next_test();
 }
 
+function test_deprecated_newChannel_API_with_string() {
+  const TEST_SPEC = "http://mozilla.org";
+  let uri = NetUtil.newURI(TEST_SPEC);
+  let oneArgChannel = NetUtil.newChannel(TEST_SPEC);
+  let threeArgChannel = NetUtil.newChannel(TEST_SPEC, null, null);
+  do_check_true(uri.equals(oneArgChannel.URI));
+  do_check_true(uri.equals(threeArgChannel.URI));
+
+  run_next_test();
+}
+
+function test_deprecated_newChannel_API_with_nsIFile()
+{
+  const TEST_DATA = "this is a test string";
+
+  // First we need a file to read from.
+  let file = Cc["@mozilla.org/file/directory_service;1"].
+             getService(Ci.nsIProperties).
+             get("ProfD", Ci.nsIFile);
+  file.append("NetUtil-deprecated-newchannel-api-test-file.tmp");
+  file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0o666);
+
+  // Write the test data to the file.
+  let ostream = Cc["@mozilla.org/network/file-output-stream;1"].
+                createInstance(Ci.nsIFileOutputStream);
+  ostream.init(file, -1, -1, 0);
+  ostream.write(TEST_DATA, TEST_DATA.length);
+
+  // Sanity check to make sure the data was written.
+  do_check_eq(TEST_DATA, getFileContents(file));
+
+  // create a channel using the file
+  let channel = NetUtil.newChannel(file);
+
+  // Create a pipe that will create our output stream that we can use once
+  // we have gotten all the data.
+  let pipe = Cc["@mozilla.org/pipe;1"].createInstance(Ci.nsIPipe);
+  pipe.init(true, true, 0, 0, null);
+
+  let listener = Cc["@mozilla.org/network/simple-stream-listener;1"].
+                   createInstance(Ci.nsISimpleStreamListener);
+  listener.init(pipe.outputStream, {
+    onStartRequest: function(aRequest, aContext) {},
+    onStopRequest: function(aRequest, aContext, aStatusCode) {
+      pipe.outputStream.close();
+      do_check_true(Components.isSuccessCode(aContext));
+
+      // Check that we got the right data.
+      do_check_eq(pipe.inputStream.available(), TEST_DATA.length);
+      let is = Cc["@mozilla.org/scriptableinputstream;1"].
+               createInstance(Ci.nsIScriptableInputStream);
+      is.init(pipe.inputStream);
+      let result = is.read(TEST_DATA.length);
+      do_check_eq(TEST_DATA, result);
+      run_next_test();
+    }
+  });
+  channel.asyncOpen2(listener);
+}
+
 function test_readInputStreamToString()
 {
   const TEST_DATA = "this is a test string\0 with an embedded null";
   let istream = Cc["@mozilla.org/io/string-input-stream;1"].
                 createInstance(Ci.nsISupportsCString);
   istream.data = TEST_DATA;
 
   do_check_eq(NetUtil.readInputStreamToString(istream, TEST_DATA.length),
@@ -783,16 +843,18 @@ function test_readInputStreamToString_in
   test_asyncFetch_with_nsIFile,
   test_asyncFetch_with_nsIInputString,
   test_asyncFetch_does_not_block,
   test_newChannel_no_specifier,
   test_newChannel_with_string,
   test_newChannel_with_nsIURI,
   test_newChannel_with_options,
   test_newChannel_with_wrong_options,
+  test_deprecated_newChannel_API_with_string,
+  test_deprecated_newChannel_API_with_nsIFile,
   test_readInputStreamToString,
   test_readInputStreamToString_no_input_stream,
   test_readInputStreamToString_no_bytes_arg,
   test_readInputStreamToString_blocking_stream,
   test_readInputStreamToString_too_many_bytes,
   test_readInputStreamToString_with_charset,
   test_readInputStreamToString_invalid_sequence,
 ].forEach(add_test);