Implement navigator properties

Signed-Off-By: Vincent Sanders <vince@netsurf-browser.org>
This commit is contained in:
Vincent Sanders 2012-07-02 01:16:57 +01:00
parent 274a76d97a
commit 67648c621d
2 changed files with 163 additions and 2 deletions

View File

@ -18,7 +18,6 @@
#include "javascript/jsapi.h"
//#include "content/content.h"
#include "utils/log.h"
static JSBool JSAPI_NATIVE(debug, JSContext *cx, uintN argc, jsval *vp)
@ -65,6 +64,18 @@ static JSBool JSAPI_NATIVE(info, JSContext *cx, uintN argc, jsval *vp)
static JSBool JSAPI_NATIVE(log, JSContext *cx, uintN argc, jsval *vp)
{
unsigned int argloop;
JSString *jsstr;
unsigned long jsstrlen;
char *txt;
for (argloop = 0; argloop < argc; argloop++) {
jsstr = JS_ValueToString(cx, *JSAPI_ARGV(cx, vp + argloop));
JSString_to_char(jsstr, txt, jsstrlen);
LOG(("%s", txt));
}
JSAPI_SET_RVAL(cx, vp, JSVAL_VOID);
return JS_TRUE;
}

View File

@ -16,14 +16,164 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include "javascript/jsapi.h"
#include "desktop/netsurf.h"
#include "desktop/options.h"
#include "utils/config.h"
#include "utils/useragent.h"
#include "utils/log.h"
#include "utils/utsname.h"
/*
* navigator properties for netsurf
*
* Property | Everyone else | NetSurf | Notes
* ------------+-----------------+--------------+------------------------------
* appCodeName | "Mozilla" | "NetSurf" | This is kinda a pointless
* | | | constant as everyone returns
* | | | "Mozilla" which is dumb
* ------------+-----------------+--------------+------------------------------
* appName | "<Browsername>" | "NetSurf" | Browsers named other than
* | | | "Netscape", "Mozilla",
* | | | "Netscape Navigator",
* | | | "Microsoft Internet Explorer"
* | | | often other browser have
* | | | "(compatible with Netscape)"
* | | | append.
* ------------+-----------------+--------------+------------------------------
* appVersion | "<ver> (<type>)"| "<ver>" | Actually just the version
* | | | number e.g "3.0".
* ------------+-----------------+--------------+------------------------------
* language | "<lang>" | "<lang>" | The language the frontend is
* | | | configured for
* ------------+-----------------+--------------+------------------------------
* platform | "<krn> <hw>" | "<krn> <hw>" | Efectively uname -s -i,
* | | | eg "Linux x86_64"
* ------------+-----------------+--------------+------------------------------
* userAgent | "Mozilla/5.0 (" | "NetSurf" | The usual useragent string
* | | | with excessive lies
* ------------+-----------------+--------------+------------------------------
*/
static JSFunctionSpec jsfunctions_navigator[] = {
JS_FS_END
};
#define NAVIGATOR_APPNAME "NetSurf"
#define NAVIGATOR_APPCODENAME "NetSurf"
static JSBool JSAPI_PROPERTYGET(appName, JSContext *cx, JSObject *obj, jsval *vp)
{
JS_SET_RVAL(cx, vp, STRING_TO_JSVAL(JS_NewStringCopyZ(cx, NAVIGATOR_APPNAME)));
return JS_TRUE;
}
static JSBool JSAPI_PROPERTYSET(appName, JSContext *cx, JSObject *obj, jsval *vp)
{
assert(false);
return JS_FALSE;
}
static JSBool JSAPI_PROPERTYGET(appCodeName, JSContext *cx, JSObject *obj, jsval *vp)
{
JS_SET_RVAL(cx, vp, STRING_TO_JSVAL(JS_NewStringCopyZ(cx, NAVIGATOR_APPCODENAME)));
return JS_TRUE;
}
static JSBool JSAPI_PROPERTYSET(appCodeName, JSContext *cx, JSObject *obj, jsval *vp)
{
assert(false);
return JS_FALSE;
}
static JSBool JSAPI_PROPERTYGET(appVersion, JSContext *cx, JSObject *obj, jsval *vp)
{
JS_SET_RVAL(cx, vp, STRING_TO_JSVAL(JS_NewStringCopyZ(cx, netsurf_version)));
return JS_TRUE;
}
static JSBool JSAPI_PROPERTYSET(appVersion, JSContext *cx, JSObject *obj, jsval *vp)
{
assert(false);
return JS_FALSE;
}
static JSBool JSAPI_PROPERTYGET(language, JSContext *cx, JSObject *obj, jsval *vp)
{
const char *alang = nsoption_charp(accept_language);
if (alang != NULL) {
JS_SET_RVAL(cx, vp, STRING_TO_JSVAL(JS_NewStringCopyZ(cx, alang)));
} else {
JS_SET_RVAL(cx, vp, JSVAL_VOID);
}
return JS_TRUE;
}
static JSBool JSAPI_PROPERTYSET(language, JSContext *cx, JSObject *obj, jsval *vp)
{
assert(false);
return JS_FALSE;
}
static JSBool JSAPI_PROPERTYGET(platform, JSContext *cx, JSObject *obj, jsval *vp)
{
struct utsname *cutsname;
cutsname = malloc(sizeof(struct utsname));
if ((cutsname == NULL) || uname(cutsname) < 0) {
JS_SET_RVAL(cx, vp, JSVAL_VOID);
} else {
char *platstr;
int platstrlen;
platstrlen = strlen(cutsname->sysname) + strlen(cutsname->machine) + 2;
platstr = malloc(platstrlen);
if (platstr != NULL) {
snprintf(platstr, platstrlen, "%s %s", cutsname->sysname, cutsname->machine);
JS_SET_RVAL(cx, vp, STRING_TO_JSVAL(JS_NewStringCopyN(cx, platstr, platstrlen - 1)));
free(platstr);
} else {
JS_SET_RVAL(cx, vp, JSVAL_VOID);
}
}
return JS_TRUE;
}
static JSBool JSAPI_PROPERTYSET(platform, JSContext *cx, JSObject *obj, jsval *vp)
{
assert(false);
return JS_FALSE;
}
static JSBool JSAPI_PROPERTYGET(userAgent, JSContext *cx, JSObject *obj, jsval *vp)
{
JS_SET_RVAL(cx, vp, STRING_TO_JSVAL(JS_NewStringCopyZ(cx, user_agent_string())));
return JS_TRUE;
}
static JSBool JSAPI_PROPERTYSET(userAgent, JSContext *cx, JSObject *obj, jsval *vp)
{
assert(false);
return JS_FALSE;
}
static JSPropertySpec jsproperties_navigator[] =
{
JSAPI_PS(appName, 0, JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_SHARED),
JSAPI_PS(appCodeName, 0, JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_SHARED),
JSAPI_PS(appVersion, 0, JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_SHARED),
JSAPI_PS(language, 0, JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_SHARED),
JSAPI_PS(platform, 0, JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_SHARED),
JSAPI_PS(userAgent, 0, JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_SHARED),
JSAPI_PS_END
};
static JSClass jsclass_navigator =
{
"navigator",
@ -48,7 +198,7 @@ JSObject *jsapi_new_navigator(JSContext *cx, JSObject *parent)
&jsclass_navigator,
NULL,
0,
NULL,
jsproperties_navigator,
jsfunctions_navigator,
NULL,
NULL);