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