Bug 892214 - atob and btoa for Sandbox. r=bholley
authorGabor Krizsanits <gkrizsanits@mozilla.com>
Sat, 21 Sep 2013 15:25:47 +0200
changeset 148122 d5e7dc7d33c38b5251c4d1f6ba9ce3f139630f99
parent 148121 2d42170cc6cce200e8c05a4a68ce00c956b5b257
child 148123 a8adb2b75a14082fc2eb168f05b0d4630e3dc238
push id2777
push usergkrizsanits@mozilla.com
push dateSat, 21 Sep 2013 13:27:27 +0000
treeherderfx-team@d5e7dc7d33c3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbholley
bugs892214
milestone27.0a1
Bug 892214 - atob and btoa for Sandbox. r=bholley
js/xpconnect/src/Sandbox.cpp
js/xpconnect/src/xpcprivate.h
js/xpconnect/tests/unit/test_sandbox_atob.js
js/xpconnect/tests/unit/xpcshell.ini
--- a/js/xpconnect/src/Sandbox.cpp
+++ b/js/xpconnect/src/Sandbox.cpp
@@ -886,16 +886,20 @@ xpc::SandboxOptions::GlobalProperties::P
         char *name = JS_EncodeString(cx, nameValue.toString());
         NS_ENSURE_TRUE(name, false);
         if (!strcmp(name, "XMLHttpRequest")) {
             XMLHttpRequest = true;
         } else if (!strcmp(name, "TextEncoder")) {
             TextEncoder = true;
         } else if (!strcmp(name, "TextDecoder")) {
             TextDecoder = true;
+        } else if (!strcmp(name, "atob")) {
+            atob = true;
+        } else if (!strcmp(name, "btoa")) {
+            btoa = true;
         } else {
             // Reporting error, if one of the global property names is unknown.
             return false;
         }
     }
     return true;
 }
 
@@ -909,16 +913,24 @@ xpc::SandboxOptions::GlobalProperties::D
     if (TextEncoder &&
         !dom::TextEncoderBinding::GetConstructorObject(cx, obj))
         return false;
 
     if (TextDecoder &&
         !dom::TextDecoderBinding::GetConstructorObject(cx, obj))
         return false;
 
+    if (atob &&
+        !JS_DefineFunction(cx, obj, "atob", Atob, 1, 0))
+        return false;
+
+    if (btoa &&
+        !JS_DefineFunction(cx, obj, "btoa", Btoa, 1, 0))
+        return false;
+
     return true;
 }
 
 nsresult
 xpc::CreateSandboxObject(JSContext *cx, jsval *vp, nsISupports *prinOrSop, SandboxOptions& options)
 {
     // Create the sandbox global object
     nsresult rv;
--- a/js/xpconnect/src/xpcprivate.h
+++ b/js/xpconnect/src/xpcprivate.h
@@ -3596,16 +3596,18 @@ IsSandbox(JSObject *obj);
 struct SandboxOptions {
     struct GlobalProperties {
         GlobalProperties() { mozilla::PodZero(this); }
         bool Parse(JSContext* cx, JS::HandleObject obj);
         bool Define(JSContext* cx, JS::HandleObject obj);
         bool XMLHttpRequest;
         bool TextDecoder;
         bool TextEncoder;
+        bool atob;
+        bool btoa;
     };
 
     SandboxOptions(JSContext *cx)
         : wantXrays(true)
         , wantComponents(true)
         , wantExportHelpers(false)
         , proto(xpc_GetSafeJSContext())
         , sameZoneAs(xpc_GetSafeJSContext())
new file mode 100644
--- /dev/null
+++ b/js/xpconnect/tests/unit/test_sandbox_atob.js
@@ -0,0 +1,10 @@
+function run_test() {
+  var Cu = Components.utils;
+  sb = new Cu.Sandbox('http://www.example.com',
+                      { wantGlobalProperties: ["atob", "btoa"] });
+  sb.do_check_eq = do_check_eq;
+  Cu.evalInSandbox('var dummy = "Dummy test.";' +
+                   'do_check_eq(dummy, atob(btoa(dummy)));' +
+                   'do_check_eq(btoa("budapest"), "YnVkYXBlc3Q=");',
+                   sb);
+}
--- a/js/xpconnect/tests/unit/xpcshell.ini
+++ b/js/xpconnect/tests/unit/xpcshell.ini
@@ -49,16 +49,17 @@ fail-if = os == "android"
 [test_want_components.js]
 [test_components.js]
 [test_allowedDomains.js]
 [test_allowedDomainsXHR.js]
 [test_nuke_sandbox.js]
 [test_sandbox_metadata.js]
 [test_exportFunction.js]
 [test_textDecoder.js]
+[test_sandbox_atob.js]
 [test_watchdog_enable.js]
 head = head_watchdog.js
 [test_watchdog_disable.js]
 head = head_watchdog.js
 [test_watchdog_toggle.js]
 head = head_watchdog.js
 [test_watchdog_default.js]
 head = head_watchdog.js