Bug 1257339 - Bring back deprecated newChannel() API on nsIIOService. r=sicking, r=mcmanus CLOSED TREE

MozReview-Commit-ID: 8BVo3K0veND
This commit is contained in:
Christoph Kerschbaumer 2016-03-18 17:11:42 -07:00
parent d19c0a23f3
commit 830e36b012
6 changed files with 260 additions and 3 deletions

View File

@ -267,12 +267,48 @@ this.NetUtil = {
* 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,

View File

@ -152,6 +152,28 @@ interface nsIIOService : nsISupports
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

View File

@ -103,4 +103,21 @@ interface nsIIOService2 : nsIIOService
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);
};

View File

@ -4,6 +4,7 @@
* 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"
@ -30,6 +31,7 @@
#include "nsIConsoleService.h"
#include "nsIUploadChannel2.h"
#include "nsXULAppAPI.h"
#include "nsIScriptError.h"
#include "nsIScriptSecurityManager.h"
#include "nsIProtocolProxyCallback.h"
#include "nsICancelable.h"
@ -51,6 +53,7 @@
#include "CaptivePortalService.h"
#include "ReferrerPolicy.h"
#include "nsContentSecurityManager.h"
#include "nsContentUtils.h"
#ifdef MOZ_WIDGET_GONK
#include "nsINetworkManager.h"
@ -662,6 +665,41 @@ nsIOService::NewChannelFromURI2(nsIURI* aURI,
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,
@ -841,6 +879,46 @@ nsIOService::NewChannelFromURIWithProxyFlags2(nsIURI* aURI,
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,
@ -866,6 +944,44 @@ nsIOService::NewChannel2(const nsACString& aSpec,
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()
{

View File

@ -39,3 +39,7 @@ SuperfluousAuth=You are about to log in to the site "%1$S" with the username "%2
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'

View File

@ -632,6 +632,66 @@ function test_newChannel_with_wrong_options()
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";
@ -788,6 +848,8 @@ function test_readInputStreamToString_invalid_sequence()
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,