Bug 1550519 - Show a translucent parent highlight when a subgrid is highlighted....
[gecko.git] / dom / workers / ChromeWorkerScope.cpp
blob073b1b2325b6824e8586f1038149da3259328c99
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/MemoryFunctions.h"
12 #include "nsXPCOM.h"
13 #include "nsNativeCharsetUtils.h"
14 #include "nsString.h"
16 #include "WorkerPrivate.h"
18 namespace mozilla {
19 namespace dom {
21 namespace {
23 #ifdef BUILD_CTYPES
25 char* UnicodeToNative(JSContext* aCx, const char16_t* aSource,
26 size_t aSourceLen) {
27 nsDependentString unicode(aSource, aSourceLen);
29 nsAutoCString native;
30 if (NS_FAILED(NS_CopyUnicodeToNative(unicode, native))) {
31 JS_ReportErrorASCII(aCx, "Could not convert string to native charset!");
32 return nullptr;
35 char* result = static_cast<char*>(JS_malloc(aCx, native.Length() + 1));
36 if (!result) {
37 return nullptr;
40 memcpy(result, native.get(), native.Length());
41 result[native.Length()] = 0;
42 return result;
45 #endif // BUILD_CTYPES
47 } // namespace
49 bool DefineChromeWorkerFunctions(JSContext* aCx,
50 JS::Handle<JSObject*> aGlobal) {
51 // Currently ctypes is the only special property given to ChromeWorkers.
52 #ifdef BUILD_CTYPES
54 JS::Rooted<JS::Value> ctypes(aCx);
55 if (!JS_InitCTypesClass(aCx, aGlobal) ||
56 !JS_GetProperty(aCx, aGlobal, "ctypes", &ctypes)) {
57 return false;
60 static const JSCTypesCallbacks callbacks = {UnicodeToNative};
62 JS_SetCTypesCallbacks(ctypes.toObjectOrNull(), &callbacks);
64 #endif // BUILD_CTYPES
66 return true;
69 } // namespace dom
70 } // namespace mozilla