Bug 1589337 - Use nsIClassInfoImpl in DefaultURI r=necko-reviewers,xpcom-reviewers,kershaw,sg

Differential Revision: https://phabricator.services.mozilla.com/D90254
This commit is contained in:
Valentin Gosu 2020-09-29 11:40:28 +00:00
Родитель 810c1b9f3d
Коммит 32d406eed2
5 изменённых файлов: 101 добавлений и 47 удалений

Просмотреть файл

@ -3,9 +3,9 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "DefaultURI.h"
#include "nsIClassInfoImpl.h"
#include "nsIObjectInputStream.h"
#include "nsIObjectOutputStream.h"
namespace mozilla {
namespace net {
@ -25,11 +25,25 @@ namespace net {
static NS_DEFINE_CID(kDefaultURICID, NS_DEFAULTURI_CID);
//----------------------------------------------------------------------------
// nsIClassInfo
//----------------------------------------------------------------------------
NS_IMPL_CLASSINFO(DefaultURI, nullptr, nsIClassInfo::THREADSAFE,
NS_DEFAULTURI_CID)
// Empty CI getter. We only need nsIClassInfo for Serialization
NS_IMPL_CI_INTERFACE_GETTER0(DefaultURI)
//----------------------------------------------------------------------------
// nsISupports
//----------------------------------------------------------------------------
NS_IMPL_ADDREF(DefaultURI)
NS_IMPL_RELEASE(DefaultURI)
NS_INTERFACE_TABLE_HEAD(DefaultURI)
NS_INTERFACE_TABLE(DefaultURI, nsIURI, nsISerializable, nsIClassInfo)
NS_INTERFACE_TABLE(DefaultURI, nsIURI, nsISerializable)
NS_INTERFACE_TABLE_TO_MAP_SEGUE
NS_IMPL_QUERY_CLASSINFO(DefaultURI)
if (aIID.Equals(kDefaultURICID)) {
foundInterface = static_cast<nsIURI*>(this);
} else
@ -50,45 +64,6 @@ NS_IMETHODIMP DefaultURI::Write(nsIObjectOutputStream* aOutputStream) {
return aOutputStream->WriteStringZ(spec.get());
}
//----------------------------------------------------------------------------
// nsIClassInfo
//----------------------------------------------------------------------------
NS_IMETHODIMP DefaultURI::GetInterfaces(nsTArray<nsIID>& aInterfaces) {
aInterfaces.Clear();
return NS_OK;
}
NS_IMETHODIMP DefaultURI::GetScriptableHelper(nsIXPCScriptable** _retval) {
*_retval = nullptr;
return NS_OK;
}
NS_IMETHODIMP DefaultURI::GetContractID(nsACString& aContractID) {
aContractID.SetIsVoid(true);
return NS_OK;
}
NS_IMETHODIMP DefaultURI::GetClassDescription(nsACString& aClassDescription) {
aClassDescription.SetIsVoid(true);
return NS_OK;
}
NS_IMETHODIMP DefaultURI::GetClassID(nsCID** aClassID) {
*aClassID = (nsCID*)moz_xmalloc(sizeof(nsCID));
return GetClassIDNoAlloc(*aClassID);
}
NS_IMETHODIMP DefaultURI::GetFlags(uint32_t* aFlags) {
*aFlags = nsIClassInfo::MAIN_THREAD_ONLY;
return NS_OK;
}
NS_IMETHODIMP DefaultURI::GetClassIDNoAlloc(nsCID* aClassIDNoAlloc) {
*aClassIDNoAlloc = kDefaultURICID;
return NS_OK;
}
//----------------------------------------------------------------------------
// nsISizeOf
//----------------------------------------------------------------------------

Просмотреть файл

@ -7,7 +7,6 @@
#include "nsIURI.h"
#include "nsISerializable.h"
#include "nsIClassInfo.h"
#include "nsISizeOf.h"
#include "nsIURIMutator.h"
#include "mozilla/net/MozURL.h"
@ -15,15 +14,11 @@
namespace mozilla {
namespace net {
class DefaultURI : public nsIURI,
public nsISerializable,
public nsIClassInfo,
public nsISizeOf {
class DefaultURI : public nsIURI, public nsISerializable, public nsISizeOf {
public:
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSIURI
NS_DECL_NSISERIALIZABLE
NS_DECL_NSICLASSINFO
virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const override;
virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override;

Просмотреть файл

@ -299,3 +299,55 @@ function addCertFromFile(certdb, filename, trustString) {
.replace(/[\r\n]/g, "");
certdb.addCertFromBase64(pem, trustString);
}
// Helper code to test nsISerializable
function serialize_to_escaped_string(obj) {
let objectOutStream = Cc["@mozilla.org/binaryoutputstream;1"].createInstance(
Ci.nsIObjectOutputStream
);
let pipe = Cc["@mozilla.org/pipe;1"].createInstance(Ci.nsIPipe);
pipe.init(false, false, 0, 0xffffffff, null);
objectOutStream.setOutputStream(pipe.outputStream);
objectOutStream.writeCompoundObject(obj, Ci.nsISupports, true);
objectOutStream.close();
let objectInStream = Cc["@mozilla.org/binaryinputstream;1"].createInstance(
Ci.nsIObjectInputStream
);
objectInStream.setInputStream(pipe.inputStream);
let data = [];
// This reads all the data from the stream until an error occurs.
while (true) {
try {
let bytes = objectInStream.readByteArray(1);
data.push(String.fromCharCode.apply(null, bytes));
} catch (e) {
break;
}
}
return escape(data.join(""));
}
function deserialize_from_escaped_string(str) {
let payload = unescape(str);
let data = [];
let i = 0;
while (i < payload.length) {
data.push(payload.charCodeAt(i++));
}
let objectOutStream = Cc["@mozilla.org/binaryoutputstream;1"].createInstance(
Ci.nsIObjectOutputStream
);
let pipe = Cc["@mozilla.org/pipe;1"].createInstance(Ci.nsIPipe);
pipe.init(false, false, 0, 0xffffffff, null);
objectOutStream.setOutputStream(pipe.outputStream);
objectOutStream.writeByteArray(data);
objectOutStream.close();
let objectInStream = Cc["@mozilla.org/binaryinputstream;1"].createInstance(
Ci.nsIObjectInputStream
);
objectInStream.setInputStream(pipe.inputStream);
return objectInStream.readObject(true);
}

Просмотреть файл

@ -155,3 +155,31 @@ add_task(function test_ipv6() {
// Hopefully this will change after bug 1603199.
equal(uri.host, "2001::1");
});
add_task(function test_serialization() {
let uri = stringToDefaultURI("http://example.org/path");
let str = serialize_to_escaped_string(uri);
let other = deserialize_from_escaped_string(str).QueryInterface(Ci.nsIURI);
equal(other.spec, uri.spec);
});
// This test assumes the serialization never changes, which might not be true.
// It's OK to change the test if we ever make changes to the serialization
// code and this starts failing.
add_task(function test_deserialize_from_string() {
let payload =
"%04DZ%A0%FD%27L%99%BDAk%E61%8A%E9%2C%00%00%00%00%00%00%00" +
"%00%C0%00%00%00%00%00%00F%00%00%00%13scheme%3Astuff/to/say";
equal(
deserialize_from_escaped_string(payload).QueryInterface(Ci.nsIURI).spec,
stringToDefaultURI("scheme:stuff/to/say").spec
);
let payload2 =
"%04DZ%A0%FD%27L%99%BDAk%E61%8A%E9%2C%00%00%00%00%00%00%00" +
"%00%C0%00%00%00%00%00%00F%00%00%00%17http%3A//example.org/path";
equal(
deserialize_from_escaped_string(payload2).QueryInterface(Ci.nsIURI).spec,
stringToDefaultURI("http://example.org/path").spec
);
});

Просмотреть файл

@ -155,6 +155,10 @@ class GenericClassInfo : public nsIClassInfo {
MOZ_FOR_EACH(NS_CLASSINFO_HELPER_ENTRY, (), (__VA_ARGS__)) \
NS_CLASSINFO_HELPER_END
#define NS_IMPL_CI_INTERFACE_GETTER0(aClass) \
NS_CLASSINFO_HELPER_BEGIN(aClass, 0) \
NS_CLASSINFO_HELPER_END
#define NS_IMPL_QUERY_INTERFACE_CI(aClass, ...) \
static_assert(MOZ_ARG_COUNT(__VA_ARGS__) > 0, \
"Need more arguments to NS_IMPL_QUERY_INTERFACE_CI"); \