Line data Source code
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/. */
6 :
7 : #include "ChromeWorkerScope.h"
8 :
9 : #include "jsapi.h"
10 :
11 : #include "nsXPCOM.h"
12 : #include "nsNativeCharsetUtils.h"
13 : #include "nsString.h"
14 :
15 : #include "WorkerPrivate.h"
16 :
17 : using namespace mozilla::dom;
18 : USING_WORKERS_NAMESPACE
19 :
20 : namespace {
21 :
22 : #ifdef BUILD_CTYPES
23 :
24 : char*
25 0 : UnicodeToNative(JSContext* aCx, const char16_t* aSource, size_t aSourceLen)
26 : {
27 0 : nsDependentString unicode(aSource, aSourceLen);
28 :
29 0 : nsAutoCString native;
30 0 : if (NS_FAILED(NS_CopyUnicodeToNative(unicode, native))) {
31 0 : JS_ReportErrorASCII(aCx, "Could not convert string to native charset!");
32 0 : return nullptr;
33 : }
34 :
35 0 : char* result = static_cast<char*>(JS_malloc(aCx, native.Length() + 1));
36 0 : if (!result) {
37 0 : return nullptr;
38 : }
39 :
40 0 : memcpy(result, native.get(), native.Length());
41 0 : result[native.Length()] = 0;
42 0 : return result;
43 : }
44 :
45 : #endif // BUILD_CTYPES
46 :
47 : } // namespace
48 :
49 : BEGIN_WORKERS_NAMESPACE
50 :
51 : bool
52 1 : DefineChromeWorkerFunctions(JSContext* aCx, JS::Handle<JSObject*> aGlobal)
53 : {
54 : // Currently ctypes is the only special property given to ChromeWorkers.
55 : #ifdef BUILD_CTYPES
56 : {
57 2 : JS::Rooted<JS::Value> ctypes(aCx);
58 4 : if (!JS_InitCTypesClass(aCx, aGlobal) ||
59 4 : !JS_GetProperty(aCx, aGlobal, "ctypes", &ctypes)) {
60 0 : return false;
61 : }
62 :
63 : static const JSCTypesCallbacks callbacks = {
64 : UnicodeToNative
65 : };
66 :
67 1 : JS_SetCTypesCallbacks(ctypes.toObjectOrNull(), &callbacks);
68 : }
69 : #endif // BUILD_CTYPES
70 :
71 1 : return true;
72 : }
73 :
74 : END_WORKERS_NAMESPACE
|