dom/workers/ChromeWorkerScope.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "ChromeWorkerScope.h"
michael@0 7
michael@0 8 #include "jsapi.h"
michael@0 9
michael@0 10 #include "nsXPCOM.h"
michael@0 11 #include "nsNativeCharsetUtils.h"
michael@0 12 #include "nsString.h"
michael@0 13
michael@0 14 #include "WorkerPrivate.h"
michael@0 15
michael@0 16 using namespace mozilla::dom;
michael@0 17 USING_WORKERS_NAMESPACE
michael@0 18
michael@0 19 namespace {
michael@0 20
michael@0 21 #ifdef BUILD_CTYPES
michael@0 22
michael@0 23 char*
michael@0 24 UnicodeToNative(JSContext* aCx, const jschar* aSource, size_t aSourceLen)
michael@0 25 {
michael@0 26 nsDependentString unicode(aSource, aSourceLen);
michael@0 27
michael@0 28 nsAutoCString native;
michael@0 29 if (NS_FAILED(NS_CopyUnicodeToNative(unicode, native))) {
michael@0 30 JS_ReportError(aCx, "Could not convert string to native charset!");
michael@0 31 return nullptr;
michael@0 32 }
michael@0 33
michael@0 34 char* result = static_cast<char*>(JS_malloc(aCx, native.Length() + 1));
michael@0 35 if (!result) {
michael@0 36 return nullptr;
michael@0 37 }
michael@0 38
michael@0 39 memcpy(result, native.get(), native.Length());
michael@0 40 result[native.Length()] = 0;
michael@0 41 return result;
michael@0 42 }
michael@0 43
michael@0 44 #endif // BUILD_CTYPES
michael@0 45
michael@0 46 } // anonymous namespace
michael@0 47
michael@0 48 BEGIN_WORKERS_NAMESPACE
michael@0 49
michael@0 50 bool
michael@0 51 DefineChromeWorkerFunctions(JSContext* aCx, JS::Handle<JSObject*> aGlobal)
michael@0 52 {
michael@0 53 // Currently ctypes is the only special property given to ChromeWorkers.
michael@0 54 #ifdef BUILD_CTYPES
michael@0 55 {
michael@0 56 JS::Rooted<JS::Value> ctypes(aCx);
michael@0 57 if (!JS_InitCTypesClass(aCx, aGlobal) ||
michael@0 58 !JS_GetProperty(aCx, aGlobal, "ctypes", &ctypes)) {
michael@0 59 return false;
michael@0 60 }
michael@0 61
michael@0 62 static JSCTypesCallbacks callbacks = {
michael@0 63 UnicodeToNative
michael@0 64 };
michael@0 65
michael@0 66 JS_SetCTypesCallbacks(JSVAL_TO_OBJECT(ctypes), &callbacks);
michael@0 67 }
michael@0 68 #endif // BUILD_CTYPES
michael@0 69
michael@0 70 return true;
michael@0 71 }
michael@0 72
michael@0 73 END_WORKERS_NAMESPACE

mercurial