michael@0: /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "ChromeWorkerScope.h" michael@0: michael@0: #include "jsapi.h" michael@0: michael@0: #include "nsXPCOM.h" michael@0: #include "nsNativeCharsetUtils.h" michael@0: #include "nsString.h" michael@0: michael@0: #include "WorkerPrivate.h" michael@0: michael@0: using namespace mozilla::dom; michael@0: USING_WORKERS_NAMESPACE michael@0: michael@0: namespace { michael@0: michael@0: #ifdef BUILD_CTYPES michael@0: michael@0: char* michael@0: UnicodeToNative(JSContext* aCx, const jschar* aSource, size_t aSourceLen) michael@0: { michael@0: nsDependentString unicode(aSource, aSourceLen); michael@0: michael@0: nsAutoCString native; michael@0: if (NS_FAILED(NS_CopyUnicodeToNative(unicode, native))) { michael@0: JS_ReportError(aCx, "Could not convert string to native charset!"); michael@0: return nullptr; michael@0: } michael@0: michael@0: char* result = static_cast(JS_malloc(aCx, native.Length() + 1)); michael@0: if (!result) { michael@0: return nullptr; michael@0: } michael@0: michael@0: memcpy(result, native.get(), native.Length()); michael@0: result[native.Length()] = 0; michael@0: return result; michael@0: } michael@0: michael@0: #endif // BUILD_CTYPES michael@0: michael@0: } // anonymous namespace michael@0: michael@0: BEGIN_WORKERS_NAMESPACE michael@0: michael@0: bool michael@0: DefineChromeWorkerFunctions(JSContext* aCx, JS::Handle aGlobal) michael@0: { michael@0: // Currently ctypes is the only special property given to ChromeWorkers. michael@0: #ifdef BUILD_CTYPES michael@0: { michael@0: JS::Rooted ctypes(aCx); michael@0: if (!JS_InitCTypesClass(aCx, aGlobal) || michael@0: !JS_GetProperty(aCx, aGlobal, "ctypes", &ctypes)) { michael@0: return false; michael@0: } michael@0: michael@0: static JSCTypesCallbacks callbacks = { michael@0: UnicodeToNative michael@0: }; michael@0: michael@0: JS_SetCTypesCallbacks(JSVAL_TO_OBJECT(ctypes), &callbacks); michael@0: } michael@0: #endif // BUILD_CTYPES michael@0: michael@0: return true; michael@0: } michael@0: michael@0: END_WORKERS_NAMESPACE