| OLD | NEW |
| 1 // Copyright (c) 2008, Google Inc. | 1 // Copyright (c) 2008, Google Inc. |
| 2 // All rights reserved. | 2 // All rights reserved. |
| 3 // | 3 // |
| 4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
| 5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
| 6 // met: | 6 // met: |
| 7 // | 7 // |
| 8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
| 9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
| 10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
| 11 // copyright notice, this list of conditions and the following disclaimer | 11 // copyright notice, this list of conditions and the following disclaimer |
| 12 // in the documentation and/or other materials provided with the | 12 // in the documentation and/or other materials provided with the |
| 13 // distribution. | 13 // distribution. |
| 14 // * Neither the name of Google Inc. nor the names of its | 14 // * Neither the name of Google Inc. nor the names of its |
| 15 // contributors may be used to endorse or promote products derived from | 15 // contributors may be used to endorse or promote products derived from |
| 16 // this software without specific prior written permission. | 16 // this software without specific prior written permission. |
| 17 // | 17 // |
| 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 v8::Context::Scope scope(context); | 76 v8::Context::Scope scope(context); |
| 77 | 77 |
| 78 // m_frame can removed by the callback function, | 78 // m_frame can removed by the callback function, |
| 79 // protect it until the callback function returns. | 79 // protect it until the callback function returns. |
| 80 RefPtr<Frame> protector(m_frame); | 80 RefPtr<Frame> protector(m_frame); |
| 81 | 81 |
| 82 IF_DEVEL(log_info(frame, "Handling DOM event", m_frame->document()->URL())); | 82 IF_DEVEL(log_info(frame, "Handling DOM event", m_frame->document()->URL())); |
| 83 | 83 |
| 84 v8::Handle<v8::Value> jsevent = V8Proxy::EventToV8Object(event); | 84 v8::Handle<v8::Value> jsevent = V8Proxy::EventToV8Object(event); |
| 85 | 85 |
| 86 // For compatibility, we store the event object as a property on the window |
| 87 // called "event". Because this is the global namespace, we save away any |
| 88 // existing "event" property, and then restore it after executing the |
| 89 // javascript handler. |
| 86 v8::Local<v8::String> event_symbol = v8::String::NewSymbol("event"); | 90 v8::Local<v8::String> event_symbol = v8::String::NewSymbol("event"); |
| 87 | 91 |
| 88 // Save the old 'event' property. | 92 // Save the old 'event' property. |
| 89 v8::Local<v8::Value> saved_evt = context->Global()->Get(event_symbol); | 93 v8::Local<v8::Value> saved_evt = context->Global()->Get(event_symbol); |
| 90 | 94 |
| 91 // Make the event available in the window object. | 95 // Make the event available in the window object. |
| 92 // | 96 // |
| 93 // TODO: This does not work as in safari if the window.event | 97 // TODO: This does not work as in safari if the window.event |
| 94 // property is already set. We need to make sure that property | 98 // property is already set. We need to make sure that property |
| 95 // access is intercepted correctly. | 99 // access is intercepted correctly. |
| 96 context->Global()->Set(event_symbol, jsevent); | 100 context->Global()->Set(event_symbol, jsevent); |
| 97 | 101 |
| 98 v8::Local<v8::Value> ret; | 102 v8::Local<v8::Value> ret; |
| 99 { | 103 { |
| 100 // Catch exceptions thrown in the event handler so they do not | 104 // Catch exceptions thrown in the event handler so they do not |
| 101 // propagate to javascript code that caused the event to fire. | 105 // propagate to javascript code that caused the event to fire. |
| 102 v8::TryCatch try_catch; | 106 v8::TryCatch try_catch; |
| 103 try_catch.SetVerbose(true); | 107 try_catch.SetVerbose(true); |
| 104 | 108 |
| 105 // Call the event handler. | 109 // Call the event handler. |
| 106 ret = CallListenerFunction(jsevent, event, isWindowEvent); | 110 ret = CallListenerFunction(jsevent, event, isWindowEvent); |
| 107 } | 111 } |
| 108 | 112 |
| 113 // Restore the old event. This must be done for all exit paths through |
| 114 // this method. |
| 115 context->Global()->Set(event_symbol, saved_evt); |
| 116 |
| 109 if (V8Proxy::HandleOutOfMemory()) | 117 if (V8Proxy::HandleOutOfMemory()) |
| 110 ASSERT(ret.IsEmpty()); | 118 ASSERT(ret.IsEmpty()); |
| 111 | 119 |
| 112 if (ret.IsEmpty()) { | 120 if (ret.IsEmpty()) { |
| 113 return; | 121 return; |
| 114 } | 122 } |
| 115 | 123 |
| 116 if (!ret.IsEmpty()) { | 124 if (!ret.IsEmpty()) { |
| 117 if (!ret->IsNull() && !ret->IsUndefined() && | 125 if (!ret->IsNull() && !ret->IsUndefined() && |
| 118 event->storesResultAsString()) { | 126 event->storesResultAsString()) { |
| 119 event->storeResult(ToWebCoreString(ret)); | 127 event->storeResult(ToWebCoreString(ret)); |
| 120 } | 128 } |
| 121 // Prevent default action if the return value is false; | 129 // Prevent default action if the return value is false; |
| 122 // TODO(fqian): example, and reference to buganizer entry | 130 // TODO(fqian): example, and reference to buganizer entry |
| 123 if (m_html) { | 131 if (m_html) { |
| 124 if (ret->IsBoolean() && !ret->BooleanValue()) { | 132 if (ret->IsBoolean() && !ret->BooleanValue()) { |
| 125 event->preventDefault(); | 133 event->preventDefault(); |
| 126 } | 134 } |
| 127 } | 135 } |
| 128 } | 136 } |
| 129 | 137 |
| 130 // Restore the old event. | |
| 131 context->Global()->Set(event_symbol, saved_evt); | |
| 132 | |
| 133 Document::updateDocumentsRendering(); | 138 Document::updateDocumentsRendering(); |
| 134 } | 139 } |
| 135 | 140 |
| 136 | 141 |
| 137 void V8AbstractEventListener::DisposeListenerObject() { | 142 void V8AbstractEventListener::DisposeListenerObject() { |
| 138 if (!m_listener.IsEmpty()) { | 143 if (!m_listener.IsEmpty()) { |
| 139 #ifndef NDEBUG | 144 #ifndef NDEBUG |
| 140 V8Proxy::UnregisterGlobalHandle(this, m_listener); | 145 V8Proxy::UnregisterGlobalHandle(this, m_listener); |
| 141 #endif | 146 #endif |
| 142 m_listener.Dispose(); | 147 m_listener.Dispose(); |
| 143 m_listener.Clear(); | 148 m_listener.Clear(); |
| 144 } | 149 } |
| 145 } | 150 } |
| 146 | 151 |
| 147 | 152 |
| 153 |
| 148 V8EventListener::V8EventListener(Frame* frame, v8::Local<v8::Object> listener, | 154 V8EventListener::V8EventListener(Frame* frame, v8::Local<v8::Object> listener, |
| 149 bool html) | 155 bool html) |
| 150 : V8AbstractEventListener(frame, html) { | 156 : V8AbstractEventListener(frame, html) { |
| 151 m_listener = v8::Persistent<v8::Object>::New(listener); | 157 m_listener = v8::Persistent<v8::Object>::New(listener); |
| 152 #ifndef NDEBUG | 158 #ifndef NDEBUG |
| 153 V8Proxy::RegisterGlobalHandle(EVENT_LISTENER, this, m_listener); | 159 V8Proxy::RegisterGlobalHandle(EVENT_LISTENER, this, m_listener); |
| 154 #endif | 160 #endif |
| 155 } | 161 } |
| 156 | 162 |
| 157 | 163 |
| (...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 463 m_wrapped_function->SetName(v8::String::New( | 469 m_wrapped_function->SetName(v8::String::New( |
| 464 FromWebCoreString(m_func_name), m_func_name.length())); | 470 FromWebCoreString(m_func_name), m_func_name.length())); |
| 465 } | 471 } |
| 466 } | 472 } |
| 467 } // end of local scope | 473 } // end of local scope |
| 468 | 474 |
| 469 return v8::Local<v8::Function>::New(m_wrapped_function); | 475 return v8::Local<v8::Function>::New(m_wrapped_function); |
| 470 } | 476 } |
| 471 | 477 |
| 472 } // namespace WebCore | 478 } // namespace WebCore |
| OLD | NEW |