1 /* ECMAScript browser scripting module */
9 #include "ecmascript/spidermonkey/util.h"
10 #include "network/connection.h"
11 #include "protocol/uri.h"
12 #include "scripting/smjs/core.h"
13 #include "scripting/smjs/cache_object.h"
14 #include "scripting/smjs/elinks_object.h"
15 #include "session/download.h"
18 struct smjs_load_uri_hop
{
24 smjs_loading_callback(struct download
*download
, void *data
)
26 struct session
*saved_smjs_ses
= smjs_ses
;
27 struct smjs_load_uri_hop
*hop
= data
;
29 JSObject
*cache_entry_object
;
31 if (is_in_progress_state(download
->state
)) return;
33 if (!download
->cached
) goto end
;
35 /* download->cached->object.refcount is typically 0 here
36 * because no struct document uses the cache entry. Because
37 * the connection is no longer using the cache entry either,
38 * it can be garbage collected. Don't let that happen while
39 * the script is using it. */
40 object_lock(download
->cached
);
42 assert(hop
->callback
);
46 cache_entry_object
= smjs_get_cache_entry_object(download
->cached
);
47 if (!cache_entry_object
) goto end
;
49 args
[0] = OBJECT_TO_JSVAL(cache_entry_object
);
51 JS_CallFunction(smjs_ctx
, NULL
, hop
->callback
, 1, args
, &rval
);
55 object_unlock(download
->cached
);
56 mem_free(download
->data
);
59 smjs_ses
= saved_smjs_ses
;
63 smjs_load_uri(JSContext
*ctx
, JSObject
*obj
, uintN argc
, jsval
*argv
,
66 struct smjs_load_uri_hop
*hop
;
67 struct download
*download
;
69 unsigned char *uri_string
;
72 if (argc
< 2) return JS_FALSE
;
74 jsstr
= JS_ValueToString(smjs_ctx
, argv
[0]);
75 uri_string
= JS_GetStringBytes(jsstr
);
77 uri
= get_uri(uri_string
, 0);
78 if (!uri
) return JS_FALSE
;
80 download
= mem_alloc(sizeof(*download
));
86 hop
= mem_alloc(sizeof(*hop
));
93 hop
->callback
= JS_ValueToFunction(smjs_ctx
, argv
[1]);
97 download
->callback
= (download_callback_T
*) smjs_loading_callback
;
99 load_uri(uri
, NULL
, download
, PRI_MAIN
, CACHE_MODE_NORMAL
, -1);
107 smjs_init_load_uri_interface(void)
109 if (!smjs_ctx
|| !smjs_elinks_object
)
112 JS_DefineFunction(smjs_ctx
, smjs_elinks_object
, "load_uri",
113 &smjs_load_uri
, 2, 0);