LCOV - code coverage report
Current view: directory - dom/workers - EventTarget.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 93 5 5.4 %
Date: 2012-06-02 Functions: 12 1 8.3 %

       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 "EventTarget.h"
      40                 : 
      41                 : #include "jsapi.h"
      42                 : #include "jsfriendapi.h"
      43                 : #include "nsTraceRefcnt.h"
      44                 : 
      45                 : // All the EventTarget subclasses have to be included here.
      46                 : #include "Worker.h"
      47                 : #include "WorkerScope.h"
      48                 : #include "XMLHttpRequest.h"
      49                 : 
      50                 : #include "WorkerInlines.h"
      51                 : 
      52                 : USING_WORKERS_NAMESPACE
      53                 : 
      54                 : using mozilla::dom::workers::events::EventTarget;
      55                 : 
      56                 : namespace {
      57                 : 
      58                 : #define DECL_EVENTTARGET_CLASS(_varname, _name) \
      59                 :   JSClass _varname = { \
      60                 :     _name, 0, \
      61                 :     JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub, \
      62                 :     JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub, \
      63                 :     JSCLASS_NO_OPTIONAL_MEMBERS \
      64                 :   };
      65                 : 
      66                 : DECL_EVENTTARGET_CLASS(gClass, "EventTarget")
      67                 : DECL_EVENTTARGET_CLASS(gMainThreadClass, "WorkerEventTarget")
      68                 : 
      69                 : #undef DECL_EVENTTARGET_CLASS
      70                 : 
      71                 : inline
      72                 : bool
      73               0 : EnsureObjectIsEventTarget(JSContext* aCx, JSObject* aObj, char* aFunctionName)
      74                 : {
      75               0 :   JSClass* classPtr = JS_GetClass(aObj);
      76               0 :   if (ClassIsWorker(classPtr) || ClassIsWorkerGlobalScope(classPtr) ||
      77               0 :       ClassIsXMLHttpRequest(classPtr)) {
      78               0 :     return true;
      79                 :   }
      80                 : 
      81                 :   JS_ReportErrorNumber(aCx, js_GetErrorMessage, NULL, JSMSG_INCOMPATIBLE_PROTO,
      82               0 :                        "EventTarget", aFunctionName, classPtr->name);
      83               0 :   return false;
      84                 : }
      85                 : 
      86                 : inline
      87                 : EventTarget*
      88               0 : GetPrivate(JSObject* aObj)
      89                 : {
      90               0 :   return GetJSPrivateSafeish<EventTarget>(aObj);
      91                 : }
      92                 : 
      93                 : JSBool
      94               0 : Construct(JSContext* aCx, unsigned aArgc, jsval* aVp)
      95                 : {
      96                 :   JS_ReportErrorNumber(aCx, js_GetErrorMessage, NULL, JSMSG_WRONG_CONSTRUCTOR,
      97               0 :                        gClass.name);
      98               0 :   return false;
      99                 : }
     100                 : 
     101                 : JSFunctionSpec gFunctions[] = {
     102                 :   JS_FN("addEventListener", EventTarget::AddEventListener, 3, JSPROP_ENUMERATE),
     103                 :   JS_FN("removeEventListener", EventTarget::RemoveEventListener, 3,
     104                 :         JSPROP_ENUMERATE),
     105                 :   JS_FN("dispatchEvent", EventTarget::DispatchEvent, 1, JSPROP_ENUMERATE),
     106                 :   JS_FS_END
     107                 : };
     108                 : 
     109                 : } // anonymous namespace
     110                 : 
     111               0 : EventTarget::EventTarget()
     112                 : {
     113               0 :   MOZ_COUNT_CTOR(mozilla::dom::workers::events::EventTarget);
     114               0 : }
     115                 : 
     116               0 : EventTarget::~EventTarget()
     117                 : {
     118               0 :   MOZ_COUNT_DTOR(mozilla::dom::workers::events::EventTarget);
     119               0 : }
     120                 : 
     121                 : bool
     122               0 : EventTarget::GetEventListenerOnEventTarget(JSContext* aCx, const char* aType,
     123                 :                                            jsval* aVp)
     124                 : {
     125               0 :   JSString* type = JS_InternString(aCx, aType);
     126               0 :   if (!type) {
     127               0 :     return false;
     128                 :   }
     129                 : 
     130               0 :   return mListenerManager.GetEventListener(aCx, type, aVp);
     131                 : }
     132                 : 
     133                 : bool
     134               0 : EventTarget::SetEventListenerOnEventTarget(JSContext* aCx, const char* aType,
     135                 :                                            jsval* aVp)
     136                 : {
     137               0 :   JSString* type = JS_InternString(aCx, aType);
     138               0 :   if (!type) {
     139               0 :     return false;
     140                 :   }
     141                 : 
     142                 :   JSObject* listenerObj;
     143               0 :   if (!JS_ValueToObject(aCx, *aVp, &listenerObj)) {
     144               0 :     return false;
     145                 :   }
     146                 : 
     147               0 :   return mListenerManager.SetEventListener(aCx, type, *aVp);
     148                 : }
     149                 : 
     150                 : // static
     151                 : EventTarget*
     152               0 : EventTarget::FromJSObject(JSObject* aObj)
     153                 : {
     154               0 :   return GetPrivate(aObj);
     155                 : }
     156                 : 
     157                 : // static
     158                 : JSBool
     159               0 : EventTarget::AddEventListener(JSContext* aCx, unsigned aArgc, jsval* aVp)
     160                 : {
     161               0 :   JSObject* obj = JS_THIS_OBJECT(aCx, aVp);
     162               0 :   if (!obj) {
     163               0 :     return true;
     164                 :   }
     165                 : 
     166               0 :   if (!EnsureObjectIsEventTarget(aCx, obj, "AddEventListener")) {
     167               0 :     return false;
     168                 :   }
     169                 : 
     170               0 :   EventTarget* self = GetPrivate(obj);
     171               0 :   if (!self) {
     172               0 :     return true;
     173                 :   }
     174                 : 
     175                 :   JSString* type;
     176                 :   JSObject* listener;
     177               0 :   JSBool capturing = false, wantsUntrusted = false;
     178               0 :   if (!JS_ConvertArguments(aCx, aArgc, JS_ARGV(aCx, aVp), "So/bb", &type,
     179               0 :                            &listener, &capturing, &wantsUntrusted)) {
     180               0 :     return false;
     181                 :   }
     182                 : 
     183               0 :   if (!listener) {
     184               0 :     return true;
     185                 :   }
     186                 : 
     187                 :   return self->mListenerManager.AddEventListener(aCx, type,
     188               0 :                                                  JS_ARGV(aCx, aVp)[1],
     189               0 :                                                  capturing, wantsUntrusted);
     190                 : }
     191                 : 
     192                 : // static
     193                 : JSBool
     194               0 : EventTarget::RemoveEventListener(JSContext* aCx, unsigned aArgc, jsval* aVp)
     195                 : {
     196               0 :   JSObject* obj = JS_THIS_OBJECT(aCx, aVp);
     197               0 :   if (!obj) {
     198               0 :     return true;
     199                 :   }
     200                 : 
     201               0 :   if (!EnsureObjectIsEventTarget(aCx, obj, "RemoveEventListener")) {
     202               0 :     return false;
     203                 :   }
     204                 : 
     205               0 :   EventTarget* self = GetPrivate(obj);
     206               0 :   if (!self) {
     207               0 :     return true;
     208                 :   }
     209                 : 
     210                 :   JSString* type;
     211                 :   JSObject* listener;
     212               0 :   JSBool capturing = false;
     213               0 :   if (!JS_ConvertArguments(aCx, aArgc, JS_ARGV(aCx, aVp), "So/b", &type,
     214               0 :                            &listener, &capturing)) {
     215               0 :     return false;
     216                 :   }
     217                 : 
     218               0 :   if (!listener) {
     219               0 :     return true;
     220                 :   }
     221                 : 
     222                 :   return self->mListenerManager.RemoveEventListener(aCx, type,
     223               0 :                                                     JS_ARGV(aCx, aVp)[1],
     224               0 :                                                     capturing);
     225                 : }
     226                 : 
     227                 : // static
     228                 : JSBool
     229               0 : EventTarget::DispatchEvent(JSContext* aCx, unsigned aArgc, jsval* aVp)
     230                 : {
     231               0 :   JSObject* obj = JS_THIS_OBJECT(aCx, aVp);
     232               0 :   if (!obj) {
     233               0 :     return true;
     234                 :   }
     235                 : 
     236               0 :   if (!EnsureObjectIsEventTarget(aCx, obj, "DispatchEvent")) {
     237               0 :     return false;
     238                 :   }
     239                 : 
     240               0 :   EventTarget* self = GetPrivate(obj);
     241               0 :   if (!self) {
     242               0 :     return true;
     243                 :   }
     244                 : 
     245                 :   JSObject* event;
     246               0 :   if (!JS_ConvertArguments(aCx, aArgc, JS_ARGV(aCx, aVp), "o", &event)) {
     247               0 :     return false;
     248                 :   }
     249                 : 
     250                 :   bool preventDefaultCalled;
     251               0 :   if (!self->mListenerManager.DispatchEvent(aCx, obj, event,
     252               0 :                                             &preventDefaultCalled)) {
     253               0 :     return false;
     254                 :   }
     255                 : 
     256               0 :   JS_SET_RVAL(aCx, aVp, BOOLEAN_TO_JSVAL(preventDefaultCalled));
     257               0 :   return true;
     258                 : }
     259                 : 
     260                 : BEGIN_WORKERS_NAMESPACE
     261                 : 
     262                 : namespace events {
     263                 : 
     264                 : JSObject*
     265             147 : InitEventTargetClass(JSContext* aCx, JSObject* aObj, bool aMainRuntime)
     266                 : {
     267             147 :   if (aMainRuntime) {
     268                 :     // XXX Hack to prevent instanceof checks failing on the main thread.
     269                 :     jsval windowEventTarget;
     270             147 :     if (!JS_GetProperty(aCx, aObj, gClass.name, &windowEventTarget)) {
     271               0 :       return NULL;
     272                 :     }
     273                 : 
     274             147 :     if (!JSVAL_IS_PRIMITIVE(windowEventTarget)) {
     275                 :       jsval protoVal;
     276               0 :       if (!JS_GetProperty(aCx, JSVAL_TO_OBJECT(windowEventTarget), "prototype",
     277               0 :                           &protoVal)) {
     278               0 :         return NULL;
     279                 :       }
     280                 : 
     281               0 :       if (!JSVAL_IS_PRIMITIVE(protoVal)) {
     282                 :         return JS_InitClass(aCx, aObj, JSVAL_TO_OBJECT(protoVal),
     283                 :                             &gMainThreadClass, Construct, 0, NULL, gFunctions,
     284               0 :                             NULL, NULL);
     285                 :       }
     286                 :     }
     287                 :   }
     288                 : 
     289                 :   return JS_InitClass(aCx, aObj, NULL, &gClass, Construct, 0, NULL,
     290             147 :                       gFunctions, NULL, NULL);
     291                 : }
     292                 : 
     293                 : } // namespace events
     294                 : 
     295                 : END_WORKERS_NAMESPACE

Generated by: LCOV version 1.7