• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "config.h"
32 
33 #if ENABLE(WORKERS)
34 
35 #include "AbstractWorker.h"
36 
37 #include "ErrorEvent.h"
38 #include "Event.h"
39 #include "EventException.h"
40 #include "EventNames.h"
41 #include "ScriptExecutionContext.h"
42 #include "SecurityOrigin.h"
43 
44 namespace WebCore {
45 
AbstractWorker(ScriptExecutionContext * context)46 AbstractWorker::AbstractWorker(ScriptExecutionContext* context)
47     : ActiveDOMObject(context, this)
48 {
49 }
50 
~AbstractWorker()51 AbstractWorker::~AbstractWorker()
52 {
53 }
54 
addEventListener(const AtomicString & eventType,PassRefPtr<EventListener> eventListener,bool)55 void AbstractWorker::addEventListener(const AtomicString& eventType, PassRefPtr<EventListener> eventListener, bool)
56 {
57     EventListenersMap::iterator iter = m_eventListeners.find(eventType);
58     if (iter == m_eventListeners.end()) {
59         ListenerVector listeners;
60         listeners.append(eventListener);
61         m_eventListeners.add(eventType, listeners);
62     } else {
63         ListenerVector& listeners = iter->second;
64         for (ListenerVector::iterator listenerIter = listeners.begin(); listenerIter != listeners.end(); ++listenerIter) {
65             if (*listenerIter == eventListener)
66                 return;
67         }
68 
69         listeners.append(eventListener);
70         m_eventListeners.add(eventType, listeners);
71     }
72 }
73 
removeEventListener(const AtomicString & eventType,EventListener * eventListener,bool)74 void AbstractWorker::removeEventListener(const AtomicString& eventType, EventListener* eventListener, bool)
75 {
76     EventListenersMap::iterator iter = m_eventListeners.find(eventType);
77     if (iter == m_eventListeners.end())
78         return;
79 
80     ListenerVector& listeners = iter->second;
81     for (ListenerVector::const_iterator listenerIter = listeners.begin(); listenerIter != listeners.end(); ++listenerIter) {
82         if (*listenerIter == eventListener) {
83             listeners.remove(listenerIter - listeners.begin());
84             return;
85         }
86     }
87 }
88 
dispatchEvent(PassRefPtr<Event> event,ExceptionCode & ec)89 bool AbstractWorker::dispatchEvent(PassRefPtr<Event> event, ExceptionCode& ec)
90 {
91     if (!event || event->type().isEmpty()) {
92         ec = EventException::UNSPECIFIED_EVENT_TYPE_ERR;
93         return true;
94     }
95 
96     ListenerVector listenersCopy = m_eventListeners.get(event->type());
97     for (ListenerVector::const_iterator listenerIter = listenersCopy.begin(); listenerIter != listenersCopy.end(); ++listenerIter) {
98         event->setTarget(this);
99         event->setCurrentTarget(this);
100         listenerIter->get()->handleEvent(event.get(), false);
101     }
102 
103     return !event->defaultPrevented();
104 }
105 
dispatchLoadErrorEvent()106 void AbstractWorker::dispatchLoadErrorEvent()
107 {
108     RefPtr<Event> evt = Event::create(eventNames().errorEvent, false, true);
109     if (m_onErrorListener) {
110         evt->setTarget(this);
111         evt->setCurrentTarget(this);
112         m_onErrorListener->handleEvent(evt.get(), true);
113     }
114 
115     ExceptionCode ec = 0;
116     dispatchEvent(evt.release(), ec);
117     ASSERT(!ec);
118 }
119 
dispatchScriptErrorEvent(const String & message,const String & sourceURL,int lineNumber)120 bool AbstractWorker::dispatchScriptErrorEvent(const String& message, const String& sourceURL, int lineNumber)
121 {
122     bool handled = false;
123     RefPtr<ErrorEvent> event = ErrorEvent::create(message, sourceURL, static_cast<unsigned>(lineNumber));
124     if (m_onErrorListener) {
125         event->setTarget(this);
126         event->setCurrentTarget(this);
127         m_onErrorListener->handleEvent(event.get(), true);
128         if (event->defaultPrevented())
129             handled = true;
130     }
131 
132     ExceptionCode ec = 0;
133     dispatchEvent(event.release(), ec);
134     ASSERT(!ec);
135 
136     return handled;
137 }
138 
resolveURL(const String & url,ExceptionCode & ec)139 KURL AbstractWorker::resolveURL(const String& url, ExceptionCode& ec)
140 {
141     if (url.isEmpty()) {
142         ec = SYNTAX_ERR;
143         return KURL();
144     }
145 
146     // FIXME: This should use the dynamic global scope (bug #27887)
147     KURL scriptURL = scriptExecutionContext()->completeURL(url);
148     if (!scriptURL.isValid()) {
149         ec = SYNTAX_ERR;
150         return KURL();
151     }
152 
153     if (!scriptExecutionContext()->securityOrigin()->canAccess(SecurityOrigin::create(scriptURL).get())) {
154         ec = SECURITY_ERR;
155         return KURL();
156     }
157     return scriptURL;
158 }
159 
160 } // namespace WebCore
161 
162 #endif // ENABLE(WORKERS)
163