Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /* Binding to generate window interface
  2.  *
  3.  * Copyright 2012 Vincent Sanders <vince@netsurf-browser.org>
  4.  *
  5.  * This file is part of NetSurf, http://www.netsurf-browser.org/
  6.  *
  7.  * Released under the terms of the MIT License,
  8.  *         http://www.opensource.org/licenses/mit-license
  9.  */
  10.  
  11.  
  12. webidlfile "html.idl";
  13. webidlfile "dom.idl";
  14.  
  15. hdrcomment "Copyright 2012 Vincent Sanders <vince@netsurf-browser.org>";
  16. hdrcomment "This file is part of NetSurf, http://www.netsurf-browser.org/";
  17. hdrcomment "Released under the terms of the MIT License,";
  18. hdrcomment "        http://www.opensource.org/licenses/mit-license";
  19.  
  20. preamble %{
  21.  
  22. #include <dom/dom.h>
  23.  
  24. #include "utils/config.h"
  25. #include "utils/log.h"
  26. #include "utils/corestrings.h"
  27. #include "render/html_internal.h"
  28. #include "javascript/jsapi.h"
  29.  
  30. #include "console.h"
  31. #include "navigator.h"
  32. #include "event.h"
  33. #include "node.h"
  34. #include "htmlcollection.h"
  35. #include "nodelist.h"
  36. #include "htmldocument.h"
  37. #include "text.h"
  38. #include "comment.h"
  39. #include "htmlelement.h"
  40. #include "window.h"
  41. #include "location.h"
  42.  
  43. %}
  44.  
  45. binding window {
  46.         type js_libdom; /* the binding type */
  47.  
  48.         interface Window; /* Web IDL interface to generate */
  49.  
  50.         private "struct browser_window *" bw;
  51.         private "struct html_content *" htmlc;
  52.  
  53.         internal "JSObject *" document;
  54.         internal "JSObject *" navigator;
  55.         internal "JSObject *" console;
  56.  
  57.         property unshared type EventHandler;
  58. }
  59.  
  60. api mark %{
  61.         if (private != NULL) {
  62.                 if (private->document != NULL) {
  63.                         JSAPI_GCMARK(private->document);
  64.                 }
  65.                 if (private->navigator != NULL) {
  66.                         JSAPI_GCMARK(private->navigator);
  67.                 }
  68.                 if (private->console != NULL) {
  69.                         JSAPI_GCMARK(private->console);
  70.                 }
  71.         }
  72. %}
  73.  
  74. api global %{
  75. %}
  76.  
  77. api init %{
  78.         JSObject *user_proto;
  79.  
  80.         prototype = JS_NewCompartmentAndGlobalObject(cx, &JSClass_Window, NULL);
  81.         if (prototype == NULL) {
  82.                 return NULL;
  83.         }
  84.  
  85.         /** @todo reconsider global object handling. future
  86.          * editions of spidermonkey appear to be removing the
  87.          * idea of a global so we probably need to handle
  88.          * global object references internally
  89.          */
  90.  
  91.         /* set the contexts global */
  92.         JS_SetGlobalObject(cx, prototype);
  93.  
  94.         /* Populate the global object with the standard globals, like
  95.          *  Object and Array.
  96.          */
  97.         if (!JS_InitStandardClasses(cx, prototype)) {
  98.                 return NULL;
  99.         }
  100.  
  101.         /* add functions to prototype */
  102.         if (!JS_DefineFunctions(cx, prototype, jsclass_functions)) {
  103.                 return NULL;
  104.         }
  105.  
  106.         /* add properties to prototype */
  107.         if (!JS_DefineProperties(cx, prototype, jsclass_properties))
  108.                 return NULL;
  109.  
  110.         /* Initialises all the user javascript classes to make their
  111.          * prototypes available.
  112.          */
  113.         /** @todo should we be managing these prototype objects ourselves */
  114.         user_proto = jsapi_InitClass_Document(cx, prototype);
  115.         if (user_proto == NULL) {
  116.                 return NULL;
  117.         }
  118.  
  119.         user_proto = jsapi_InitClass_Navigator(cx, prototype);
  120.         if (user_proto == NULL) {
  121.                 return NULL;
  122.         }
  123.  
  124.         user_proto = jsapi_InitClass_Location(cx, prototype);
  125.         if (user_proto == NULL) {
  126.                 return NULL;
  127.         }
  128.  
  129.         user_proto = jsapi_InitClass_Console(cx, prototype);
  130.         if (user_proto == NULL) {
  131.                 return NULL;
  132.         }
  133.  
  134.         user_proto = jsapi_InitClass_HTMLElement(cx, prototype);
  135.         if (user_proto == NULL) {
  136.                 return NULL;
  137.         }
  138.  
  139.         user_proto = jsapi_InitClass_HTMLCollection(cx, prototype);
  140.         if (user_proto == NULL) {
  141.                 return NULL;
  142.         }
  143.  
  144.         user_proto = jsapi_InitClass_NodeList(cx, prototype);
  145.         if (user_proto == NULL) {
  146.                 return NULL;
  147.         }
  148.  
  149.         user_proto = jsapi_InitClass_Text(cx, prototype);
  150.         if (user_proto == NULL) {
  151.                 return NULL;
  152.         }
  153.  
  154.         user_proto = jsapi_InitClass_Comment(cx, prototype);
  155.         if (user_proto == NULL) {
  156.                 return NULL;
  157.         }
  158.  
  159.         user_proto = jsapi_InitClass_Node(cx, prototype);
  160.         if (user_proto == NULL) {
  161.                 return NULL;
  162.         }
  163.  
  164.         user_proto = jsapi_InitClass_Event(cx, prototype);
  165.         if (user_proto == NULL) {
  166.                 return NULL;
  167.         }
  168.  
  169. %}
  170.  
  171. api new %{
  172.         /* @todo sort out windows that are not globals */
  173.         assert(parent == NULL);
  174.  
  175.         /* the window object is the global so its prototype *is* the instance */
  176.         newobject = prototype;
  177.  
  178.         /* instantiate the subclasses off the window global */
  179.         private->document = jsapi_new_Document(cx,
  180.                                                NULL,
  181.                                                newobject,
  182.                                                (dom_document *)dom_node_ref(htmlc->document),
  183.                                                htmlc);
  184.         if (private->document == NULL) {
  185.                 free(private);
  186.                 return NULL;
  187.         }
  188.  
  189.         private->navigator = jsapi_new_Navigator(cx, NULL, newobject);
  190.         if (private->navigator == NULL) {
  191.                 free(private);
  192.                 return NULL;
  193.         }
  194.  
  195.         private->console = jsapi_new_Console(cx, NULL, newobject);
  196.         if (private->console == NULL) {
  197.                 free(private);
  198.                 return NULL;
  199.         }
  200.  
  201.         /** @todo forms, history */
  202.  
  203.         LOG(("Created new window object %p", newobject));
  204. %}
  205.  
  206. operation confirm %{
  207.         warn_user(message, NULL);
  208. %}
  209.  
  210. operation alert %{
  211.         warn_user(message, NULL);
  212. %}
  213.  
  214. operation prompt %{
  215.         warn_user(message, NULL);
  216. %}
  217.  
  218. /* boolean dispatchEvent(Event event); */
  219. operation dispatchEvent %{
  220.         /* this implementation is unique to the window object as it is
  221.          * not a "real" dom node.
  222.          */
  223.  
  224.         /* caution, this must match the struct generated from event.bnd */
  225.         struct {
  226.                 dom_event *event;
  227.         } *event_private;
  228.         dom_string *type_dom = NULL;
  229.         dom_exception exc;
  230.         jsval eventval = JSVAL_VOID;
  231.         jsval event_argv[1];
  232.         jsval event_rval;
  233.  
  234.         event_private = JS_GetInstancePrivate(cx, event, &JSClass_Event, NULL);
  235.         if (event_private->event == NULL) {
  236.                 /** @todo type error? */
  237.                 jsret = JS_FALSE;
  238.         } else {
  239.                 exc = dom_event_get_type(event_private->event, &type_dom);
  240.                 if (exc == DOM_NO_ERR) {
  241.  
  242.                         if (dom_string_isequal(type_dom, corestring_dom_load)) {
  243.                                 JS_GetProperty(cx, JSAPI_THIS_OBJECT(cx, vp), "onload", &eventval);
  244.                         }
  245.  
  246.                         if (!JSVAL_IS_VOID(eventval)) {
  247.                                 event_argv[0] = eventval;
  248.                                 jsret = JS_CallFunctionValue(cx, NULL, eventval, 1, event_argv, &event_rval);
  249.                         }
  250.                 }
  251.         }
  252. %}
  253.  
  254. getter location %{
  255.         jsval loc;
  256.         JS_GetProperty(cx, private->document, "location", &loc);
  257.         jsret = JSVAL_TO_OBJECT(loc);
  258. %}
  259.  
  260. getter window %{
  261.         jsret = obj;
  262. %}
  263.  
  264. getter self %{
  265.         jsret = obj;
  266. %}
  267.  
  268. getter EventHandler %{
  269.         /* this implementation is unique to the window object as it is
  270.          * not a dom node.
  271.          */
  272.         JSLOG("propname[%d]=\"%s\"",
  273.               tinyid,
  274.               jsclass_properties[tinyid].name);
  275. %}
  276.  
  277. setter EventHandler %{
  278.         /* this implementation is unique to the window object as it is
  279.          * not a dom node.
  280.          */
  281.         JSLOG("propname[%d]=\"%s\"",
  282.               tinyid,
  283.               jsclass_properties[tinyid].name);
  284. %}
  285.