Skip to content

Commit e27ef6c

Browse files
committed
Bug 713550 - Move Base64 code on nsXPConnect to XPCOM / xpcpublic.h; r=bholley+khuey
1 parent 0576a76 commit e27ef6c

File tree

11 files changed

+150
-141
lines changed

11 files changed

+150
-141
lines changed

content/base/src/nsContentUtils.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ static NS_DEFINE_CID(kXTFServiceCID, NS_XTFSERVICE_CID);
193193

194194
#include "mozAutoDocUpdate.h"
195195
#include "imgICache.h"
196-
#include "xpcprivate.h"
196+
#include "xpcprivate.h" // nsXPConnect
197197
#include "nsScriptSecurityManager.h"
198198
#include "nsIChannelPolicy.h"
199199
#include "nsChannelPolicy.h"
@@ -207,6 +207,7 @@ static NS_DEFINE_CID(kXTFServiceCID, NS_XTFSERVICE_CID);
207207
#include "nsIContentViewer.h"
208208
#include "nsIObjectLoadingContent.h"
209209

210+
#include "mozilla/Base64.h"
210211
#include "mozilla/Preferences.h"
211212

212213
#include "nsWrapperCacheInlines.h"
@@ -633,7 +634,7 @@ nsContentUtils::Btoa(const nsAString& aBinaryData,
633634
return NS_ERROR_DOM_INVALID_CHARACTER_ERR;
634635
}
635636

636-
return nsXPConnect::Base64Encode(aBinaryData, aAsciiBase64String);
637+
return Base64Encode(aBinaryData, aAsciiBase64String);
637638
}
638639

639640
nsresult
@@ -645,7 +646,7 @@ nsContentUtils::Atob(const nsAString& aAsciiBase64String,
645646
return NS_ERROR_DOM_INVALID_CHARACTER_ERR;
646647
}
647648

648-
nsresult rv = nsXPConnect::Base64Decode(aAsciiBase64String, aBinaryData);
649+
nsresult rv = Base64Decode(aAsciiBase64String, aBinaryData);
649650
if (NS_FAILED(rv) && rv == NS_ERROR_INVALID_ARG) {
650651
return NS_ERROR_DOM_INVALID_CHARACTER_ERR;
651652
}

dom/base/nsStructuredCloneContainer.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@
4545
#include "nsIVariant.h"
4646
#include "nsServiceManagerUtils.h"
4747
#include "nsContentUtils.h"
48-
#include "xpcprivate.h"
48+
49+
#include "mozilla/Base64.h"
50+
51+
using namespace mozilla;
4952

5053
NS_IMPL_ADDREF(nsStructuredCloneContainer)
5154
NS_IMPL_RELEASE(nsStructuredCloneContainer)
@@ -124,7 +127,7 @@ nsStructuredCloneContainer::InitFromBase64(const nsAString &aData,
124127
NS_ConvertUTF16toUTF8 data(aData);
125128

126129
nsCAutoString binaryData;
127-
nsresult rv = nsXPConnect::Base64Decode(data, binaryData);
130+
nsresult rv = Base64Decode(data, binaryData);
128131
NS_ENSURE_SUCCESS(rv, rv);
129132

130133
// Copy the string's data into our own buffer.
@@ -171,7 +174,7 @@ nsStructuredCloneContainer::GetDataAsBase64(nsAString &aOut)
171174

172175
nsCAutoString binaryData(reinterpret_cast<char*>(mData), mSize);
173176
nsCAutoString base64Data;
174-
nsresult rv = nsXPConnect::Base64Encode(binaryData, base64Data);
177+
nsresult rv = Base64Encode(binaryData, base64Data);
175178
NS_ENSURE_SUCCESS(rv, rv);
176179

177180
aOut.Assign(NS_ConvertASCIItoUTF16(base64Data));

dom/workers/FileReaderSync.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
#include "jscntxt.h"
4747
#include "jstypedarray.h"
4848
#include "nsJSUtils.h"
49-
#include "xpcprivate.h"
5049

5150
#include "Exceptions.h"
5251
#include "File.h"

dom/workers/Makefile.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ LOCAL_INCLUDES = \
7777
-I$(topsrcdir)/content/base/src \
7878
-I$(topsrcdir)/content/events/src \
7979
-I$(topsrcdir)/dom/base \
80-
-I$(topsrcdir)/js/xpconnect/src \
8180
-I$(topsrcdir)/xpcom/build \
8281
$(NULL)
8382

dom/workers/WorkerScope.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@
4242
#include "WorkerScope.h"
4343

4444
#include "jsapi.h"
45+
#include "jsdbgapi.h"
4546
#include "jscntxt.h"
4647

4748
#include "nsTraceRefcnt.h"
48-
#include "xpcprivate.h"
49+
#include "xpcpublic.h"
4950

5051
#include "ChromeWorkerScope.h"
5152
#include "Events.h"
@@ -548,7 +549,7 @@ class WorkerGlobalScope : public events::EventTarget
548549
}
549550

550551
jsval result;
551-
if (!nsXPConnect::Base64Decode(aCx, string, &result)) {
552+
if (!xpc::Base64Decode(aCx, string, &result)) {
552553
return false;
553554
}
554555

@@ -574,7 +575,7 @@ class WorkerGlobalScope : public events::EventTarget
574575
}
575576

576577
jsval result;
577-
if (!nsXPConnect::Base64Encode(aCx, binary, &result)) {
578+
if (!xpc::Base64Encode(aCx, binary, &result)) {
578579
return false;
579580
}
580581

js/xpconnect/loader/mozJSComponentLoader.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
#include "nsILocalFileWin.h"
9191
#endif
9292
#include "xpcprivate.h"
93+
#include "xpcpublic.h"
9394
#include "nsIResProtocolHandler.h"
9495

9596
#include "mozilla/scache/StartupCache.h"
@@ -235,7 +236,7 @@ Atob(JSContext *cx, uintN argc, jsval *vp)
235236
if (!argc)
236237
return true;
237238

238-
return nsXPConnect::Base64Decode(cx, JS_ARGV(cx, vp)[0], &JS_RVAL(cx, vp));
239+
return xpc::Base64Decode(cx, JS_ARGV(cx, vp)[0], &JS_RVAL(cx, vp));
239240
}
240241

241242
static JSBool
@@ -244,7 +245,7 @@ Btoa(JSContext *cx, uintN argc, jsval *vp)
244245
if (!argc)
245246
return true;
246247

247-
return nsXPConnect::Base64Encode(cx, JS_ARGV(cx, vp)[0], &JS_RVAL(cx, vp));
248+
return xpc::Base64Encode(cx, JS_ARGV(cx, vp)[0], &JS_RVAL(cx, vp));
248249
}
249250

250251
static JSBool

js/xpconnect/src/nsXPConnect.cpp

Lines changed: 19 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
#include "nsNullPrincipal.h"
5555
#include "nsIURI.h"
5656
#include "nsJSEnvironment.h"
57-
#include "plbase64.h"
5857

5958
#include "XrayWrapper.h"
6059
#include "WrapperFactory.h"
@@ -64,6 +63,10 @@
6463

6564
#include "XPCQuickStubs.h"
6665
#include "dombindings.h"
66+
67+
#include "mozilla/Assertions.h"
68+
#include "mozilla/Base64.h"
69+
6770
#include "nsWrapperCacheInlines.h"
6871

6972
NS_IMPL_THREADSAFE_ISUPPORTS7(nsXPConnect,
@@ -2797,65 +2800,22 @@ nsXPConnect::GetCaller(JSContext **aJSContext, JSObject **aObj)
27972800
*aObj = ccx->GetFlattenedJSObject();
27982801
}
27992802

2800-
// static
2801-
nsresult
2802-
nsXPConnect::Base64Encode(const nsACString &aBinaryData, nsACString &aString)
2803-
{
2804-
// Check for overflow.
2805-
if (aBinaryData.Length() > (PR_UINT32_MAX / 4) * 3)
2806-
return NS_ERROR_FAILURE;
2807-
2808-
PRUint32 stringLen = ((aBinaryData.Length() + 2) / 3) * 4;
2809-
2810-
char *buffer;
2811-
2812-
// Add one byte for null termination.
2813-
if (aString.SetCapacity(stringLen + 1) &&
2814-
(buffer = aString.BeginWriting()) &&
2815-
PL_Base64Encode(aBinaryData.BeginReading(), aBinaryData.Length(), buffer)) {
2816-
// PL_Base64Encode doesn't null terminate the buffer for us when we pass
2817-
// the buffer in. Do that manually.
2818-
buffer[stringLen] = '\0';
2819-
2820-
aString.SetLength(stringLen);
2821-
return NS_OK;
2822-
}
2823-
2824-
aString.Truncate();
2825-
return NS_ERROR_INVALID_ARG;
2826-
}
2827-
2828-
// static
2829-
nsresult
2830-
nsXPConnect::Base64Encode(const nsAString &aString, nsAString &aBinaryData)
2831-
{
2832-
NS_LossyConvertUTF16toASCII string(aString);
2833-
nsCAutoString binaryData;
2834-
2835-
nsresult rv = Base64Encode(string, binaryData);
2836-
if (NS_SUCCEEDED(rv))
2837-
CopyASCIItoUTF16(binaryData, aBinaryData);
2838-
else
2839-
aBinaryData.Truncate();
2840-
2841-
return rv;
2842-
}
2803+
namespace xpc {
28432804

2844-
// static
2845-
JSBool
2846-
nsXPConnect::Base64Encode(JSContext *cx, jsval val, jsval *out)
2805+
bool
2806+
Base64Encode(JSContext *cx, JS::Value val, JS::Value *out)
28472807
{
2848-
NS_ASSERTION(cx, "Null context!");
2849-
NS_ASSERTION(out, "Null jsval pointer!");
2808+
MOZ_ASSERT(cx);
2809+
MOZ_ASSERT(out);
28502810

2851-
jsval root = val;
2811+
JS::Value root = val;
28522812
xpc_qsACString encodedString(cx, root, &root, xpc_qsACString::eNull,
28532813
xpc_qsACString::eStringify);
28542814
if (!encodedString.IsValid())
28552815
return false;
28562816

28572817
nsCAutoString result;
2858-
if (NS_FAILED(nsXPConnect::Base64Encode(encodedString, result))) {
2818+
if (NS_FAILED(mozilla::Base64Encode(encodedString, result))) {
28592819
JS_ReportError(cx, "Failed to encode base64 data!");
28602820
return false;
28612821
}
@@ -2868,72 +2828,20 @@ nsXPConnect::Base64Encode(JSContext *cx, jsval val, jsval *out)
28682828
return true;
28692829
}
28702830

2871-
// static
2872-
nsresult
2873-
nsXPConnect::Base64Decode(const nsACString &aString, nsACString &aBinaryData)
2874-
{
2875-
// Check for overflow.
2876-
if (aString.Length() > PR_UINT32_MAX / 3)
2877-
return NS_ERROR_FAILURE;
2878-
2879-
PRUint32 binaryDataLen = ((aString.Length() * 3) / 4);
2880-
2881-
char *buffer;
2882-
2883-
// Add one byte for null termination.
2884-
if (aBinaryData.SetCapacity(binaryDataLen + 1) &&
2885-
(buffer = aBinaryData.BeginWriting()) &&
2886-
PL_Base64Decode(aString.BeginReading(), aString.Length(), buffer)) {
2887-
// PL_Base64Decode doesn't null terminate the buffer for us when we pass
2888-
// the buffer in. Do that manually, taking into account the number of '='
2889-
// characters we were passed.
2890-
if (!aString.IsEmpty() && aString[aString.Length() - 1] == '=') {
2891-
if (aString.Length() > 1 && aString[aString.Length() - 2] == '=')
2892-
binaryDataLen -= 2;
2893-
else
2894-
binaryDataLen -= 1;
2895-
}
2896-
buffer[binaryDataLen] = '\0';
2897-
2898-
aBinaryData.SetLength(binaryDataLen);
2899-
return NS_OK;
2900-
}
2901-
2902-
aBinaryData.Truncate();
2903-
return NS_ERROR_INVALID_ARG;
2904-
}
2905-
2906-
// static
2907-
nsresult
2908-
nsXPConnect::Base64Decode(const nsAString &aBinaryData, nsAString &aString)
2909-
{
2910-
NS_LossyConvertUTF16toASCII binaryData(aBinaryData);
2911-
nsCAutoString string;
2912-
2913-
nsresult rv = Base64Decode(binaryData, string);
2914-
if (NS_SUCCEEDED(rv))
2915-
CopyASCIItoUTF16(string, aString);
2916-
else
2917-
aString.Truncate();
2918-
2919-
return rv;
2920-
}
2921-
2922-
// static
2923-
JSBool
2924-
nsXPConnect::Base64Decode(JSContext *cx, jsval val, jsval *out)
2831+
bool
2832+
Base64Decode(JSContext *cx, JS::Value val, JS::Value *out)
29252833
{
2926-
NS_ASSERTION(cx, "Null context!");
2927-
NS_ASSERTION(out, "Null jsval pointer!");
2834+
MOZ_ASSERT(cx);
2835+
MOZ_ASSERT(out);
29282836

2929-
jsval root = val;
2837+
JS::Value root = val;
29302838
xpc_qsACString encodedString(cx, root, &root, xpc_qsACString::eNull,
29312839
xpc_qsACString::eNull);
29322840
if (!encodedString.IsValid())
29332841
return false;
29342842

29352843
nsCAutoString result;
2936-
if (NS_FAILED(nsXPConnect::Base64Decode(encodedString, result))) {
2844+
if (NS_FAILED(mozilla::Base64Decode(encodedString, result))) {
29372845
JS_ReportError(cx, "Failed to decode base64 string!");
29382846
return false;
29392847
}
@@ -2946,6 +2854,8 @@ nsXPConnect::Base64Decode(JSContext *cx, jsval val, jsval *out)
29462854
return true;
29472855
}
29482856

2857+
} // namespace xpc
2858+
29492859
NS_IMETHODIMP
29502860
nsXPConnect::SetDebugModeWhenPossible(bool mode, bool allowSyncDisable)
29512861
{

js/xpconnect/src/xpcprivate.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -532,24 +532,6 @@ class nsXPConnect : public nsIXPConnect,
532532
nsresult GetInfoForIID(const nsIID * aIID, nsIInterfaceInfo** info);
533533
nsresult GetInfoForName(const char * name, nsIInterfaceInfo** info);
534534

535-
static nsresult Base64Encode(const nsACString &aString,
536-
nsACString &aBinary);
537-
538-
static nsresult Base64Encode(const nsAString &aString,
539-
nsAString &aBinaryData);
540-
541-
// If this returns false then an exception will be set on cx.
542-
static JSBool Base64Encode(JSContext *cx, jsval val, jsval *out);
543-
544-
static nsresult Base64Decode(const nsACString &aBinaryData,
545-
nsACString &aString);
546-
547-
static nsresult Base64Decode(const nsAString &aBinaryData,
548-
nsAString &aString);
549-
550-
// If this returns false then an exception will be set on cx.
551-
static JSBool Base64Decode(JSContext *cx, jsval val, jsval *out);
552-
553535
// nsCycleCollectionParticipant
554536
NS_IMETHOD Root(void *p);
555537
NS_IMETHOD Unlink(void *p);

js/xpconnect/src/xpcpublic.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ xpc_ActivateDebugMode();
196196

197197
namespace xpc {
198198

199+
// If these functions return false, then an exception will be set on cx.
200+
bool Base64Encode(JSContext *cx, JS::Value val, JS::Value *out);
201+
bool Base64Decode(JSContext *cx, JS::Value val, JS::Value *out);
202+
199203
/**
200204
* Convert an nsString to jsval, returning true on success.
201205
* Note, the ownership of the string buffer may be moved from str to rval.

0 commit comments

Comments
 (0)