Bug 684327 - Misc fixes to xpctest_attributes. r=khuey

This patch is best reviewed while listening to http://www.youtube.com/watch?v=MK6TXMsvgQg

NB - The code here would make a great "find the bugs in this code" interview screen question.
This commit is contained in:
Bobby Holley 2011-09-23 14:50:27 -07:00
parent 4252ab2c8a
commit 91964a9e10
4 changed files with 57 additions and 194 deletions

View File

@ -38,47 +38,16 @@
*
* ***** END LICENSE BLOCK ***** */
#include "xpctest_attributes.h"
#include "nsISupports.h"
#include "xpctest_private.h"
#define NS_IXPCTESTOBJECTREADONLY_IID \
{0x1364941e, 0x4462, 0x11d3, \
{ 0x82, 0xee, 0x00, 0x60, 0xb0, 0xeb, 0x59, 0x6f }}
class xpcTestObjectReadOnly : public nsIXPCTestObjectReadOnly {
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIXPCTESTOBJECTREADONLY
xpcTestObjectReadOnly();
private:
PRBool boolProperty;
PRInt16 shortProperty;
PRInt32 longProperty;
float floatProperty;
char charProperty;
char *stringID;
};
NS_IMPL_ISUPPORTS1(xpcTestObjectReadOnly, nsIXPCTestObjectReadOnly)
xpcTestObjectReadOnly :: xpcTestObjectReadOnly() {
NS_ADDREF_THIS();
boolProperty = PR_TRUE;
shortProperty = 32767;
longProperty = 2147483647;
floatProperty = 5.5f;
charProperty = 'X';
const char _id[] = "a68cc6a6-6552-11d3-82ef-0060b0eb596f";
stringID = (char*) nsMemory::Clone(_id, sizeof(char)*(strlen(_id)+1));
}
NS_IMETHODIMP xpcTestObjectReadOnly :: GetID(char **_retval) {
*_retval= (char*) nsMemory::Clone(stringID,
sizeof(char)*(strlen(stringID)+1));
return *_retval? NS_OK : NS_ERROR_OUT_OF_MEMORY;
}
NS_IMETHODIMP xpcTestObjectReadOnly :: GetStrReadOnly(char * *aStrReadOnly){
@ -86,8 +55,8 @@ NS_IMETHODIMP xpcTestObjectReadOnly :: GetStrReadOnly(char * *aStrReadOnly){
if(!aStrReadOnly)
return NS_ERROR_NULL_POINTER;
*aStrReadOnly = (char*) nsMemory::Clone(aStrReadOnly,
sizeof(char)*(strlen(aString)+1));
*aStrReadOnly = (char*) nsMemory::Clone(aString,
sizeof(char)*(strlen(aString)+1));
return *aStrReadOnly ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
}
@ -111,68 +80,22 @@ NS_IMETHODIMP xpcTestObjectReadOnly :: GetCharReadOnly(char *aCharReadOnly){
*aCharReadOnly = charProperty;
return NS_OK;
}
nsresult
xpctest::ConstructXPCTestObjectReadOnly(nsISupports *aOuter, REFNSIID aIID, void **aResult)
{
nsresult rv;
NS_ASSERTION(aOuter == nsnull, "no aggregation");
xpcTestObjectReadOnly *obj = new xpcTestObjectReadOnly();
if(obj)
{
rv = obj->QueryInterface(aIID, aResult);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to find correct interface");
NS_RELEASE(obj);
}
else
{
*aResult = nsnull;
rv = NS_ERROR_OUT_OF_MEMORY;
}
return rv;
}
/****************************************************************************/
/* starting interface: nsIXPCTestObjectReadWrite */
/* {3b9b1d38-491a-11d3-82ef-0060b0eb596f} */
/*
#define NS_IXPCTESTOBJECTREADWRITE_IID_STR "3b9b1d38-491a-11d3-82ef-0060b0eb596f"
*/
#define NS_IXPCTESTOBJECTREADWRITE_IID \
{0x3b9b1d38, 0x491a, 0x11d3, \
{ 0x82, 0xef, 0x00, 0x60, 0xb0, 0xeb, 0x59, 0x6f }}
class xpcTestObjectReadWrite : public nsIXPCTestObjectReadWrite {
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIXPCTESTOBJECTREADWRITE
xpcTestObjectReadWrite();
private:
PRBool boolProperty;
PRInt16 shortProperty;
PRInt32 longProperty;
float floatProperty;
char charProperty;
const char *stringProperty;
};
NS_IMPL_ISUPPORTS1(xpcTestObjectReadWrite, nsIXPCTestObjectReadWrite)
xpcTestObjectReadWrite :: xpcTestObjectReadWrite() {
NS_ADDREF_THIS();
const char s[] = "XPConnect Read-Writable String";
stringProperty = (char*) nsMemory::Clone(s, sizeof(char)*(strlen(s)+1));
boolProperty = PR_TRUE;
shortProperty = 32767;
longProperty = 2147483647;
floatProperty = 5.5f;
charProperty = 'X';
}
const char s[] = "XPConnect Read-Writable String";
stringProperty = (char*) nsMemory::Clone(s,
sizeof(char)*(strlen(s)+1));
xpcTestObjectReadWrite :: ~xpcTestObjectReadWrite()
{
nsMemory::Free(stringProperty);
}
NS_IMETHODIMP xpcTestObjectReadWrite :: GetStringProperty(char * *aStringProperty) {
@ -184,7 +107,9 @@ NS_IMETHODIMP xpcTestObjectReadWrite :: GetStringProperty(char * *aStringPropert
}
NS_IMETHODIMP xpcTestObjectReadWrite :: SetStringProperty(const char * aStringProperty) {
stringProperty = aStringProperty;
nsMemory::Free(stringProperty);
stringProperty = (char*) nsMemory::Clone(aStringProperty,
sizeof(char)*(strlen(aStringProperty)+1));
return NS_OK;
}
@ -193,6 +118,8 @@ NS_IMETHODIMP xpcTestObjectReadWrite :: GetBooleanProperty(PRBool *aBooleanPrope
return NS_OK;
}
NS_IMETHODIMP xpcTestObjectReadWrite :: SetBooleanProperty(PRBool aBooleanProperty) {
NS_ENSURE_TRUE(aBooleanProperty == PR_TRUE || aBooleanProperty == PR_FALSE,
NS_ERROR_INVALID_ARG);
boolProperty = aBooleanProperty;
return NS_OK;
}
@ -228,78 +155,3 @@ NS_IMETHODIMP xpcTestObjectReadWrite :: SetCharProperty(char aCharProperty) {
charProperty = aCharProperty;
return NS_OK;
}
nsresult
xpctest::ConstructXPCTestObjectReadWrite(nsISupports *aOuter, REFNSIID aIID, void **aResult)
{
nsresult rv;
NS_ASSERTION(aOuter == nsnull, "no aggregation");
xpcTestObjectReadWrite *obj = new xpcTestObjectReadWrite();
if(obj)
{
rv = obj->QueryInterface(aIID, aResult);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to find correct interface");
NS_RELEASE(obj);
}
else
{
*aResult = nsnull;
rv = NS_ERROR_OUT_OF_MEMORY;
}
return rv;
}
/****************************************************************************/
/*
class xpcTestAttributes : public nsIXPCTestObjectReadWrite,
public nsIXPCTestObjectReadOnly
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIXPCTESTOBJECTREADONLY
NS_DECL_NSIXPCTESTOBJECTREADWRITE
NS_IMETHOD GetName(char * *aString);
NS_IMETHOD SetName(char * aString );
private:
char *name;
};
NS_IMPL_ISUPPORTS2(xpcTestAttributes, nsIXPCTestObjectReadWrite, nsIXPCTestObjectReadOnly)
NS_IMETHODIMP xpcTestAttributes ::GetName(char * *aString) {
if(!aString)
return NS_ERROR_NULL_POINTER;
*aString = (char*) nsMemory::Clone(name,
sizeof(char)*(strlen(name)+1));
return *aString ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
}
NS_IMETHODIMP xpcTestAttributes ::SetName(char * aString) {
name = aString;
return NS_OK;
}
nsresult
xpctest::ConstructXPCTestAttributes(nsISupports *aOuter, REFNSIID aIID, void **aResult)
{
nsresult rv;
NS_ASSERTION(aOuter == nsnull, "no aggregation");
xpcTestAttributes *obj = new xpcTestAttributes();
if(obj)
{
rv = obj->QueryInterface(aIID, aResult);
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to find correct interface");
NS_RELEASE(obj);
}
else
{
*aResult = nsnull;
rv = NS_ERROR_OUT_OF_MEMORY;
}
return rv;
}
*/

View File

@ -40,19 +40,29 @@
/* module registration and factory code. */
#include "xpctest_private.h"
#include "nsCOMPtr.h"
#include "nsIModule.h"
#include "mozilla/ModuleUtils.h"
#include "nsCRT.h"
#include "nsIClassInfoImpl.h"
#include "xpctest_private.h"
#define NS_XPCTESTOBJECTREADONLY_CID \
{ 0x492609a7, 0x2582, 0x436b, \
{ 0xb0, 0xef, 0x92, 0xe2, 0x9b, 0xb9, 0xe1, 0x43 } }
#define NS_XPCTESTOBJECTREADWRITE_CID \
{ 0x8f37f760, 0x3686, 0x4dbb, \
{ 0xb1, 0x21, 0x96, 0x93, 0xba, 0x81, 0x3f, 0x8f } }
NS_GENERIC_FACTORY_CONSTRUCTOR(xpcTestObjectReadOnly);
NS_GENERIC_FACTORY_CONSTRUCTOR(xpcTestObjectReadWrite);
NS_DEFINE_NAMED_CID(NS_XPCTESTOBJECTREADONLY_CID);
NS_DEFINE_NAMED_CID(NS_XPCTESTOBJECTREADWRITE_CID);
static const mozilla::Module::CIDEntry kXPCTestCIDs[] = {
{ &kNS_XPCTESTOBJECTREADONLY_CID, false, NULL, xpctest::ConstructXPCTestObjectReadOnly },
{ &kNS_XPCTESTOBJECTREADWRITE_CID, false, NULL, xpctest::ConstructXPCTestObjectReadWrite },
{ &kNS_XPCTESTOBJECTREADONLY_CID, false, NULL, xpcTestObjectReadOnlyConstructor },
{ &kNS_XPCTESTOBJECTREADWRITE_CID, false, NULL, xpcTestObjectReadWriteConstructor },
{ NULL }
};

View File

@ -44,36 +44,39 @@
#define xpctest_private_h___
#include "nsISupports.h"
#include "nsIFactory.h"
#include "nsMemory.h"
#include "nsIXPConnect.h"
#include "nsIServiceManager.h"
#include "nsIComponentManager.h"
#include "mozilla/ModuleUtils.h"
#include "nscore.h"
#include "nsCOMPtr.h"
#include "nsStringGlue.h"
#include <stdio.h>
#include "jsapi.h"
#include "xpctest_attributes.h"
#define NS_XPCTESTOBJECTREADONLY_CID \
{0x1364941e, 0x4462, 0x11d3, \
{ 0x82, 0xee, 0x00, 0x60, 0xb0, 0xeb, 0x59, 0x6f }}
class xpcTestObjectReadOnly : public nsIXPCTestObjectReadOnly {
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIXPCTESTOBJECTREADONLY
xpcTestObjectReadOnly();
private:
PRBool boolProperty;
PRInt16 shortProperty;
PRInt32 longProperty;
float floatProperty;
char charProperty;
};
#define NS_XPCTESTOBJECTREADWRITE_CID \
{0x3b9b1d38, 0x491a, 0x11d3, \
{ 0x82, 0xef, 0x00, 0x60, 0xb0, 0xeb, 0x59, 0x6f }}
class xpcTestObjectReadWrite : public nsIXPCTestObjectReadWrite {
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIXPCTESTOBJECTREADWRITE
// 'namespace' class
class xpctest
{
public:
static nsresult ConstructXPCTestObjectReadOnly(nsISupports *aOuter, REFNSIID aIID, void **aResult);
static nsresult ConstructXPCTestObjectReadWrite(nsISupports *aOuter, REFNSIID aIID, void **aResult);
xpcTestObjectReadWrite();
~xpcTestObjectReadWrite();
private:
xpctest(); // not implemented
private:
PRBool boolProperty;
PRInt16 shortProperty;
PRInt32 longProperty;
float floatProperty;
char charProperty;
char *stringProperty;
};
#endif /* xpctest_private_h___ */

View File

@ -44,7 +44,7 @@
*
*/
[scriptable, uuid(1364941e-4462-11d3-82ee-0060b0eb596f)]
[scriptable, uuid(29e950a0-0134-44bc-b947-5e0ee95c8f7e)]
interface nsIXPCTestObjectReadOnly : nsISupports {
readonly attribute string strReadOnly;
readonly attribute boolean boolReadOnly;
@ -52,11 +52,9 @@ interface nsIXPCTestObjectReadOnly : nsISupports {
readonly attribute long longReadOnly;
readonly attribute float floatReadOnly;
readonly attribute char charReadOnly;
string getID();
};
[scriptable, uuid(3b9b1d38-491a-11d3-82ef-0060b0eb596f)]
[scriptable, uuid(492609a7-2582-436b-b0ef-92e29bb9e143)]
interface nsIXPCTestObjectReadWrite : nsISupports {
attribute string stringProperty;
attribute boolean booleanProperty;