Bug 1688354 [wpt PR 27298] - Treat 'rem' as an absolute unit for font size, a=testonly
[gecko.git] / dom / workers / ChromeWorkerScope.cpp
blobbac6dfd32d2458cabdc532dd211595a9e537a80c
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "ChromeWorkerScope.h"
9 #include "jsapi.h"
10 #include "js/experimental/CTypes.h" // JS::InitCTypesClass, JS::CTypesCallbacks, JS::SetCTypesCallbacks
11 #include "js/MemoryFunctions.h"
13 #include "nsNativeCharsetUtils.h"
14 #include "nsString.h"
16 namespace mozilla {
17 namespace dom {
19 namespace {
21 #ifdef BUILD_CTYPES
23 char* UnicodeToNative(JSContext* aCx, const char16_t* aSource,
24 size_t aSourceLen) {
25 nsDependentSubstring unicode(aSource, aSourceLen);
27 nsAutoCString native;
28 if (NS_FAILED(NS_CopyUnicodeToNative(unicode, native))) {
29 JS_ReportErrorASCII(aCx, "Could not convert string to native charset!");
30 return nullptr;
33 char* result = static_cast<char*>(JS_malloc(aCx, native.Length() + 1));
34 if (!result) {
35 return nullptr;
38 memcpy(result, native.get(), native.Length());
39 result[native.Length()] = 0;
40 return result;
43 #endif // BUILD_CTYPES
45 } // namespace
47 bool DefineChromeWorkerFunctions(JSContext* aCx,
48 JS::Handle<JSObject*> aGlobal) {
49 // Currently ctypes is the only special property given to ChromeWorkers.
50 #ifdef BUILD_CTYPES
52 JS::Rooted<JS::Value> ctypes(aCx);
53 if (!JS::InitCTypesClass(aCx, aGlobal) ||
54 !JS_GetProperty(aCx, aGlobal, "ctypes", &ctypes)) {
55 return false;
58 static const JS::CTypesCallbacks callbacks = {UnicodeToNative};
60 JS::SetCTypesCallbacks(ctypes.toObjectOrNull(), &callbacks);
62 #endif // BUILD_CTYPES
64 return true;
67 } // namespace dom
68 } // namespace mozilla