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 "Navigator.h"
40 :
41 : #include "jsapi.h"
42 : #include "jsfriendapi.h"
43 :
44 : #include "nsTraceRefcnt.h"
45 :
46 : #include "RuntimeService.h"
47 :
48 : #define PROPERTY_FLAGS \
49 : (JSPROP_ENUMERATE | JSPROP_SHARED)
50 :
51 : USING_WORKERS_NAMESPACE
52 :
53 : namespace {
54 :
55 : class Navigator
56 : {
57 : static JSClass sClass;
58 : static JSPropertySpec sProperties[];
59 :
60 : enum SLOT {
61 : SLOT_appName = 0,
62 : SLOT_appVersion,
63 : SLOT_platform,
64 : SLOT_userAgent,
65 :
66 : SLOT_COUNT
67 : };
68 :
69 : public:
70 : static JSObject*
71 0 : InitClass(JSContext* aCx, JSObject* aObj)
72 : {
73 : return JS_InitClass(aCx, aObj, NULL, &sClass, Construct, 0, sProperties,
74 0 : NULL, NULL, NULL);
75 : }
76 :
77 : static JSObject*
78 0 : Create(JSContext* aCx)
79 : {
80 0 : RuntimeService* rts = RuntimeService::GetService();
81 0 : JS_ASSERT(rts);
82 :
83 : const RuntimeService::NavigatorStrings& strings =
84 0 : rts->GetNavigatorStrings();
85 :
86 : JSString* appName, *version, *platform, *userAgent;
87 :
88 : #define COPY_STRING(_jsstr, _str) \
89 : if (strings. _str .IsEmpty()) { \
90 : _jsstr = NULL; \
91 : } \
92 : else if (!(_jsstr = JS_NewUCStringCopyN(aCx, strings. _str .get(), \
93 : strings. _str .Length()))) { \
94 : return NULL; \
95 : }
96 :
97 0 : COPY_STRING(appName, mAppName);
98 0 : COPY_STRING(version, mAppVersion);
99 0 : COPY_STRING(platform, mPlatform);
100 0 : COPY_STRING(userAgent, mUserAgent);
101 :
102 : #undef COPY_STRING
103 :
104 0 : JSObject* obj = JS_NewObject(aCx, &sClass, NULL, NULL);
105 0 : if (!obj) {
106 0 : return NULL;
107 : }
108 :
109 0 : jsval empty = JS_GetEmptyStringValue(aCx);
110 :
111 : JS_SetReservedSlot(obj, SLOT_appName,
112 0 : appName ? STRING_TO_JSVAL(appName) : empty);
113 : JS_SetReservedSlot(obj, SLOT_appVersion,
114 0 : version ? STRING_TO_JSVAL(version) : empty);
115 : JS_SetReservedSlot(obj, SLOT_platform,
116 0 : platform ? STRING_TO_JSVAL(platform) : empty);
117 : JS_SetReservedSlot(obj, SLOT_userAgent,
118 0 : userAgent ? STRING_TO_JSVAL(userAgent) : empty);
119 :
120 0 : Navigator* priv = new Navigator();
121 0 : JS_SetPrivate(obj, priv);
122 :
123 0 : return obj;
124 : }
125 :
126 : private:
127 0 : Navigator()
128 : {
129 0 : MOZ_COUNT_CTOR(mozilla::dom::workers::Navigator);
130 0 : }
131 :
132 0 : ~Navigator()
133 : {
134 0 : MOZ_COUNT_DTOR(mozilla::dom::workers::Navigator);
135 0 : }
136 :
137 : static JSBool
138 0 : Construct(JSContext* aCx, unsigned aArgc, jsval* aVp)
139 : {
140 : JS_ReportErrorNumber(aCx, js_GetErrorMessage, NULL, JSMSG_WRONG_CONSTRUCTOR,
141 0 : sClass.name);
142 0 : return false;
143 : }
144 :
145 : static void
146 0 : Finalize(JSContext* aCx, JSObject* aObj)
147 : {
148 0 : JS_ASSERT(JS_GetClass(aObj) == &sClass);
149 0 : delete static_cast<Navigator*>(JS_GetPrivate(aObj));
150 0 : }
151 :
152 : static JSBool
153 0 : GetProperty(JSContext* aCx, JSObject* aObj, jsid aIdval, jsval* aVp)
154 : {
155 0 : JSClass* classPtr = JS_GetClass(aObj);
156 0 : if (classPtr != &sClass) {
157 : JS_ReportErrorNumber(aCx, js_GetErrorMessage, NULL,
158 : JSMSG_INCOMPATIBLE_PROTO, sClass.name, "GetProperty",
159 0 : classPtr->name);
160 0 : return false;
161 : }
162 :
163 0 : JS_ASSERT(JSID_IS_INT(aIdval));
164 0 : JS_ASSERT(JSID_TO_INT(aIdval) >= 0 && JSID_TO_INT(aIdval) < SLOT_COUNT);
165 :
166 0 : *aVp = JS_GetReservedSlot(aObj, JSID_TO_INT(aIdval));
167 0 : return true;
168 : }
169 : };
170 :
171 : JSClass Navigator::sClass = {
172 : "WorkerNavigator",
173 : JSCLASS_HAS_PRIVATE | JSCLASS_HAS_RESERVED_SLOTS(SLOT_COUNT),
174 : JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
175 : JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, Finalize,
176 : JSCLASS_NO_OPTIONAL_MEMBERS
177 : };
178 :
179 : JSPropertySpec Navigator::sProperties[] = {
180 : { "appName", SLOT_appName, PROPERTY_FLAGS, GetProperty,
181 : js_GetterOnlyPropertyStub },
182 : { "appVersion", SLOT_appVersion, PROPERTY_FLAGS, GetProperty,
183 : js_GetterOnlyPropertyStub },
184 : { "platform", SLOT_platform, PROPERTY_FLAGS, GetProperty,
185 : js_GetterOnlyPropertyStub },
186 : { "userAgent", SLOT_userAgent, PROPERTY_FLAGS, GetProperty,
187 : js_GetterOnlyPropertyStub },
188 : { 0, 0, 0, NULL, NULL }
189 : };
190 :
191 : } // anonymous namespace
192 :
193 : BEGIN_WORKERS_NAMESPACE
194 :
195 : namespace navigator {
196 :
197 : bool
198 0 : InitClass(JSContext* aCx, JSObject* aGlobal)
199 : {
200 0 : return !!Navigator::InitClass(aCx, aGlobal);
201 : }
202 :
203 : JSObject*
204 0 : Create(JSContext* aCx)
205 : {
206 0 : return Navigator::Create(aCx);
207 : }
208 :
209 : } // namespace navigator
210 :
211 : END_WORKERS_NAMESPACE
|