forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Navigator.cpp
178 lines (140 loc) · 4.88 KB
/
Navigator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "Navigator.h"
#include "jsapi.h"
#include "jsfriendapi.h"
#include "nsTraceRefcnt.h"
#include "RuntimeService.h"
#define PROPERTY_FLAGS \
(JSPROP_ENUMERATE | JSPROP_SHARED)
USING_WORKERS_NAMESPACE
namespace {
class Navigator
{
static JSClass sClass;
static const JSPropertySpec sProperties[];
enum SLOT {
SLOT_appName = 0,
SLOT_appVersion,
SLOT_platform,
SLOT_userAgent,
SLOT_COUNT
};
public:
static JSObject*
InitClass(JSContext* aCx, JSObject* aObj)
{
return JS_InitClass(aCx, aObj, NULL, &sClass, Construct, 0, sProperties,
NULL, NULL, NULL);
}
static JSObject*
Create(JSContext* aCx)
{
RuntimeService* rts = RuntimeService::GetService();
JS_ASSERT(rts);
const RuntimeService::NavigatorStrings& strings =
rts->GetNavigatorStrings();
JS::Rooted<JSString*> appName(aCx), version(aCx), platform(aCx), userAgent(aCx);
#define COPY_STRING(_jsstr, _str) \
if (strings. _str .IsEmpty()) { \
_jsstr = NULL; \
} \
else if (!(_jsstr = JS_NewUCStringCopyN(aCx, strings. _str .get(), \
strings. _str .Length()))) { \
return NULL; \
}
COPY_STRING(appName, mAppName);
COPY_STRING(version, mAppVersion);
COPY_STRING(platform, mPlatform);
COPY_STRING(userAgent, mUserAgent);
#undef COPY_STRING
JSObject* obj = JS_NewObject(aCx, &sClass, NULL, NULL);
if (!obj) {
return NULL;
}
jsval empty = JS_GetEmptyStringValue(aCx);
JS_SetReservedSlot(obj, SLOT_appName,
appName ? STRING_TO_JSVAL(appName) : empty);
JS_SetReservedSlot(obj, SLOT_appVersion,
version ? STRING_TO_JSVAL(version) : empty);
JS_SetReservedSlot(obj, SLOT_platform,
platform ? STRING_TO_JSVAL(platform) : empty);
JS_SetReservedSlot(obj, SLOT_userAgent,
userAgent ? STRING_TO_JSVAL(userAgent) : empty);
Navigator* priv = new Navigator();
JS_SetPrivate(obj, priv);
return obj;
}
private:
Navigator()
{
MOZ_COUNT_CTOR(mozilla::dom::workers::Navigator);
}
~Navigator()
{
MOZ_COUNT_DTOR(mozilla::dom::workers::Navigator);
}
static JSBool
Construct(JSContext* aCx, unsigned aArgc, jsval* aVp)
{
JS_ReportErrorNumber(aCx, js_GetErrorMessage, NULL, JSMSG_WRONG_CONSTRUCTOR,
sClass.name);
return false;
}
static void
Finalize(JSFreeOp* aFop, JSObject* aObj)
{
JS_ASSERT(JS_GetClass(aObj) == &sClass);
delete static_cast<Navigator*>(JS_GetPrivate(aObj));
}
static JSBool
GetProperty(JSContext* aCx, JS::Handle<JSObject*> aObj, JS::Handle<jsid> aIdval,
JS::MutableHandle<JS::Value> aVp)
{
JSClass* classPtr = JS_GetClass(aObj);
if (classPtr != &sClass) {
JS_ReportErrorNumber(aCx, js_GetErrorMessage, NULL,
JSMSG_INCOMPATIBLE_PROTO, sClass.name, "GetProperty",
classPtr->name);
return false;
}
JS_ASSERT(JSID_IS_INT(aIdval));
JS_ASSERT(JSID_TO_INT(aIdval) >= 0 && JSID_TO_INT(aIdval) < SLOT_COUNT);
aVp.set(JS_GetReservedSlot(aObj, JSID_TO_INT(aIdval)));
return true;
}
};
JSClass Navigator::sClass = {
"WorkerNavigator",
JSCLASS_HAS_PRIVATE | JSCLASS_HAS_RESERVED_SLOTS(SLOT_COUNT),
JS_PropertyStub, JS_DeletePropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, Finalize
};
const JSPropertySpec Navigator::sProperties[] = {
{ "appName", SLOT_appName, PROPERTY_FLAGS, JSOP_WRAPPER(GetProperty),
JSOP_WRAPPER(js_GetterOnlyPropertyStub) },
{ "appVersion", SLOT_appVersion, PROPERTY_FLAGS, JSOP_WRAPPER(GetProperty),
JSOP_WRAPPER(js_GetterOnlyPropertyStub) },
{ "platform", SLOT_platform, PROPERTY_FLAGS, JSOP_WRAPPER(GetProperty),
JSOP_WRAPPER(js_GetterOnlyPropertyStub) },
{ "userAgent", SLOT_userAgent, PROPERTY_FLAGS, JSOP_WRAPPER(GetProperty),
JSOP_WRAPPER(js_GetterOnlyPropertyStub) },
{ 0, 0, 0, JSOP_NULLWRAPPER, JSOP_NULLWRAPPER }
};
} // anonymous namespace
BEGIN_WORKERS_NAMESPACE
namespace navigator {
bool
InitClass(JSContext* aCx, JSObject* aGlobal)
{
return !!Navigator::InitClass(aCx, aGlobal);
}
JSObject*
Create(JSContext* aCx)
{
return Navigator::Create(aCx);
}
} // namespace navigator
END_WORKERS_NAMESPACE