• 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 #include "ExceptionCode.h"
34 #include "MessagePort.h"
35 #include "V8Binding.h"
36 #include "V8CustomBinding.h"
37 #include "V8ObjectEventListener.h"
38 #include "V8Proxy.h"
39 #include "V8Utilities.h"
40 #include "WorkerContextExecutionProxy.h"
41 
42 namespace WebCore {
43 
getEventListener(MessagePort * messagePort,v8::Local<v8::Value> value,bool findOnly,bool createObjectEventListener)44 PassRefPtr<EventListener> getEventListener(MessagePort* messagePort, v8::Local<v8::Value> value, bool findOnly, bool createObjectEventListener)
45 {
46     V8Proxy* proxy = V8Proxy::retrieve(messagePort->scriptExecutionContext());
47     if (proxy) {
48         V8EventListenerList* list = proxy->objectListeners();
49         return findOnly ? list->findWrapper(value, false) : list->findOrCreateWrapper<V8ObjectEventListener>(proxy->frame(), value, false);
50     }
51 
52 #if ENABLE(WORKERS)
53     WorkerContextExecutionProxy* workerContextProxy = WorkerContextExecutionProxy::retrieve();
54     if (workerContextProxy)
55         return workerContextProxy->findOrCreateEventListenerHelper(value, false, findOnly, createObjectEventListener);
56 #endif
57 
58     return PassRefPtr<EventListener>();
59 }
60 
ACCESSOR_GETTER(MessagePortOnmessage)61 ACCESSOR_GETTER(MessagePortOnmessage)
62 {
63     INC_STATS("DOM.MessagePort.onmessage._get");
64     MessagePort* messagePort = V8DOMWrapper::convertToNativeObject<MessagePort>(V8ClassIndex::MESSAGEPORT, info.Holder());
65     return V8DOMWrapper::convertEventListenerToV8Object(messagePort->onmessage());
66 }
67 
ACCESSOR_SETTER(MessagePortOnmessage)68 ACCESSOR_SETTER(MessagePortOnmessage)
69 {
70     INC_STATS("DOM.MessagePort.onmessage._set");
71     MessagePort* messagePort = V8DOMWrapper::convertToNativeObject<MessagePort>(V8ClassIndex::MESSAGEPORT, info.Holder());
72     if (value->IsNull()) {
73         if (messagePort->onmessage()) {
74             V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(messagePort->onmessage());
75             removeHiddenDependency(info.Holder(), listener->getListenerObject(), V8Custom::kMessagePortRequestCacheIndex);
76         }
77 
78         // Clear the listener.
79         messagePort->setOnmessage(0);
80 
81     } else {
82         RefPtr<EventListener> listener = getEventListener(messagePort, value, false, false);
83         if (listener) {
84             messagePort->setOnmessage(listener);
85             createHiddenDependency(info.Holder(), value, V8Custom::kMessagePortRequestCacheIndex);
86         }
87     }
88 }
89 
CALLBACK_FUNC_DECL(MessagePortAddEventListener)90 CALLBACK_FUNC_DECL(MessagePortAddEventListener)
91 {
92     INC_STATS("DOM.MessagePort.addEventListener()");
93     MessagePort* messagePort = V8DOMWrapper::convertToNativeObject<MessagePort>(V8ClassIndex::MESSAGEPORT, args.Holder());
94     RefPtr<EventListener> listener = getEventListener(messagePort, args[1], false, true);
95     if (listener) {
96         String type = toWebCoreString(args[0]);
97         bool useCapture = args[2]->BooleanValue();
98         messagePort->addEventListener(type, listener, useCapture);
99 
100         createHiddenDependency(args.Holder(), args[1], V8Custom::kMessagePortRequestCacheIndex);
101     }
102     return v8::Undefined();
103 }
104 
CALLBACK_FUNC_DECL(MessagePortRemoveEventListener)105 CALLBACK_FUNC_DECL(MessagePortRemoveEventListener)
106 {
107     INC_STATS("DOM.MessagePort.removeEventListener()");
108     MessagePort* messagePort = V8DOMWrapper::convertToNativeObject<MessagePort>(V8ClassIndex::MESSAGEPORT, args.Holder());
109     RefPtr<EventListener> listener = getEventListener(messagePort, args[1], true, true);
110     if (listener) {
111         String type = toWebCoreString(args[0]);
112         bool useCapture = args[2]->BooleanValue();
113         messagePort->removeEventListener(type, listener.get(), useCapture);
114 
115         removeHiddenDependency(args.Holder(), args[1], V8Custom::kMessagePortRequestCacheIndex);
116     }
117 
118     return v8::Undefined();
119 }
120 
121 } // namespace WebCore
122