1 : /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Web Workers.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * The Mozilla Foundation.
19 : * Portions created by the Initial Developer are Copyright (C) 2011
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Ben Turner <bent.mozilla@gmail.com> (Original Author)
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #include "ChromeWorkerScope.h"
40 :
41 : #include "jsapi.h"
42 :
43 : #include "nsXPCOM.h"
44 : #include "nsNativeCharsetUtils.h"
45 : #include "nsStringGlue.h"
46 :
47 : #include "WorkerPrivate.h"
48 :
49 : #define CTYPES_STR "ctypes"
50 :
51 : USING_WORKERS_NAMESPACE
52 :
53 : namespace {
54 :
55 : #ifdef BUILD_CTYPES
56 : char*
57 0 : UnicodeToNative(JSContext* aCx, const jschar* aSource, size_t aSourceLen)
58 : {
59 0 : nsDependentString unicode(aSource, aSourceLen);
60 :
61 0 : nsCAutoString native;
62 0 : if (NS_FAILED(NS_CopyUnicodeToNative(unicode, native))) {
63 0 : JS_ReportError(aCx, "Could not convert string to native charset!");
64 0 : return nsnull;
65 : }
66 :
67 0 : char* result = static_cast<char*>(JS_malloc(aCx, native.Length() + 1));
68 0 : if (!result) {
69 0 : return nsnull;
70 : }
71 :
72 0 : memcpy(result, native.get(), native.Length());
73 0 : result[native.Length()] = 0;
74 0 : return result;
75 : }
76 :
77 : JSCTypesCallbacks gCTypesCallbacks = {
78 : UnicodeToNative
79 : };
80 :
81 : JSBool
82 0 : CTypesLazyGetter(JSContext* aCx, JSObject* aObj, jsid aId, jsval* aVp)
83 : {
84 0 : NS_ASSERTION(JS_GetGlobalObject(aCx) == aObj, "Not a global object!");
85 0 : NS_ASSERTION(JSID_IS_STRING(aId), "Bad id!");
86 0 : NS_ASSERTION(JS_FlatStringEqualsAscii(JSID_TO_FLAT_STRING(aId), CTYPES_STR),
87 : "Bad id!");
88 :
89 0 : WorkerPrivate* worker = GetWorkerPrivateFromContext(aCx);
90 0 : NS_ASSERTION(worker->IsChromeWorker(), "This should always be true!");
91 :
92 0 : if (!worker->DisableMemoryReporter()) {
93 0 : return false;
94 : }
95 :
96 : jsval ctypes;
97 0 : if (!JS_DeletePropertyById(aCx, aObj, aId) ||
98 0 : !JS_InitCTypesClass(aCx, aObj) ||
99 0 : !JS_GetPropertyById(aCx, aObj, aId, &ctypes)) {
100 0 : return false;
101 : }
102 0 : JS_SetCTypesCallbacks(JSVAL_TO_OBJECT(ctypes), &gCTypesCallbacks);
103 0 : return JS_GetPropertyById(aCx, aObj, aId, aVp);
104 : }
105 : #endif
106 :
107 : inline bool
108 0 : DefineCTypesLazyGetter(JSContext* aCx, JSObject* aGlobal)
109 : {
110 : #ifdef BUILD_CTYPES
111 : {
112 0 : JSString* ctypesStr = JS_InternString(aCx, CTYPES_STR);
113 0 : if (!ctypesStr) {
114 0 : return false;
115 : }
116 :
117 0 : jsid ctypesId = INTERNED_STRING_TO_JSID(aCx, ctypesStr);
118 :
119 : // We use a lazy getter here to let us unregister the blocking memory
120 : // reporter since ctypes can easily block the worker thread and we can
121 : // deadlock. Remove once bug 673323 is fixed.
122 0 : if (!JS_DefinePropertyById(aCx, aGlobal, ctypesId, JSVAL_VOID,
123 0 : CTypesLazyGetter, NULL, 0)) {
124 0 : return false;
125 : }
126 : }
127 : #endif
128 :
129 0 : return true;
130 : }
131 :
132 : } // anonymous namespace
133 :
134 : BEGIN_WORKERS_NAMESPACE
135 :
136 : namespace chromeworker {
137 :
138 : bool
139 0 : DefineChromeWorkerFunctions(JSContext* aCx, JSObject* aGlobal)
140 : {
141 : // Currently ctypes is the only special property given to ChromeWorkers.
142 0 : return DefineCTypesLazyGetter(aCx, aGlobal);
143 : }
144 :
145 : } // namespace chromeworker
146 :
147 : END_WORKERS_NAMESPACE
|