diff options
Diffstat (limited to 'WebCore/bindings/v8')
100 files changed, 4417 insertions, 2305 deletions
diff --git a/WebCore/bindings/v8/ChildThreadDOMData.h b/WebCore/bindings/v8/ChildThreadDOMData.h index ab90e5b..173a5e8 100644 --- a/WebCore/bindings/v8/ChildThreadDOMData.h +++ b/WebCore/bindings/v8/ChildThreadDOMData.h @@ -39,7 +39,9 @@ namespace WebCore { class ChildThreadDOMData : public DOMData { public: ChildThreadDOMData(); +#if PLATFORM(ANDROID) virtual ~ChildThreadDOMData() { } +#endif DOMDataStore& getStore(); diff --git a/WebCore/bindings/v8/DOMData.h b/WebCore/bindings/v8/DOMData.h index 07e14e5..e67adb4 100644 --- a/WebCore/bindings/v8/DOMData.h +++ b/WebCore/bindings/v8/DOMData.h @@ -45,7 +45,9 @@ namespace WebCore { class DOMData : public Noncopyable { public: DOMData(); +#if PLATFORM(ANDROID) virtual ~DOMData() { } +#endif static DOMData* getCurrent(); static DOMData* getCurrentMainThread(); // Caller must be on the main thread. diff --git a/WebCore/bindings/v8/DOMObjectsInclude.h b/WebCore/bindings/v8/DOMObjectsInclude.h index 6f36f3b..fb7ba81 100644 --- a/WebCore/bindings/v8/DOMObjectsInclude.h +++ b/WebCore/bindings/v8/DOMObjectsInclude.h @@ -31,13 +31,31 @@ #ifndef DOMObjectsInclude_h #define DOMObjectsInclude_h -#include "AbstractWorker.h" #include "BarInfo.h" +#include "BeforeLoadEvent.h" +#include "CanvasArray.h" +#include "CanvasArrayBuffer.h" +#include "CanvasBuffer.h" +#include "CanvasByteArray.h" +#include "CanvasFloatArray.h" +#include "CanvasFramebuffer.h" #include "CanvasGradient.h" +#include "CanvasIntArray.h" +#include "CanvasObject.h" #include "CanvasPattern.h" #include "CanvasPixelArray.h" +#include "CanvasProgram.h" +#include "CanvasRenderbuffer.h" +#include "CanvasRenderingContext.h" #include "CanvasRenderingContext2D.h" +#include "CanvasRenderingContext3D.h" +#include "CanvasShader.h" +#include "CanvasShortArray.h" +#include "CanvasUnsignedByteArray.h" +#include "CanvasUnsignedIntArray.h" +#include "CanvasUnsignedShortArray.h" #include "CanvasStyle.h" +#include "CanvasTexture.h" #include "CharacterData.h" #include "ClientRect.h" #include "ClientRectList.h" @@ -57,7 +75,6 @@ #include "CSSValueList.h" #include "CSSVariablesDeclaration.h" #include "CSSVariablesRule.h" -#include "Database.h" #include "DocumentType.h" #include "DocumentFragment.h" #include "DOMCoreException.h" @@ -86,7 +103,6 @@ #include "HTMLSelectElement.h" #include "HTMLOptionsCollection.h" #include "ImageData.h" -#include "InspectorBackend.h" #include "KeyboardEvent.h" #include "Location.h" #include "Media.h" @@ -106,6 +122,7 @@ #include "NodeIterator.h" #include "OverflowEvent.h" #include "Page.h" +#include "PageTransitionEvent.h" #include "Plugin.h" #include "PluginArray.h" #include "ProcessingInstruction.h" @@ -118,11 +135,6 @@ #include "ScriptExecutionContext.h" #include "SecurityOrigin.h" #include "Settings.h" -#include "SharedWorker.h" -#include "SharedWorkerContext.h" -#include "SQLTransaction.h" -#include "SQLResultSet.h" -#include "SQLResultSetRowList.h" #include "StyleSheet.h" #include "StyleSheetList.h" #include "TextEvent.h" @@ -135,9 +147,7 @@ #include "V8HTMLElement.h" #include "V8LazyEventListener.h" #include "V8NodeFilterCondition.h" -#include "V8ObjectEventListener.h" #include "ValidityState.h" -#include "VoidCallback.h" #include "WebKitAnimationEvent.h" #include "WebKitCSSKeyframeRule.h" #include "WebKitCSSKeyframesRule.h" @@ -151,16 +161,18 @@ #include "XMLHttpRequestProgressEvent.h" #include "XMLHttpRequestUpload.h" #include "XMLSerializer.h" -#include "XPathException.h" -#include "XPathExpression.h" -#include "XPathNSResolver.h" -#include "XPathResult.h" -#include "XSLTProcessor.h" #if ENABLE(OFFLINE_WEB_APPLICATIONS) #include "DOMApplicationCache.h" #endif +#if ENABLE(DATABASE) +#include "Database.h" +#include "SQLTransaction.h" +#include "SQLResultSet.h" +#include "SQLResultSetRowList.h" +#endif // DATABASE + #if ENABLE(DATAGRID) #include "DataGridColumn.h" #include "DataGridColumnList.h" @@ -215,13 +227,21 @@ #include "V8SVGPODTypeWrapper.h" #endif // SVG +#if PLATFORM(ANDROID) +// TODO: Upstream TOUCH_EVENTS guard. #if ENABLE(TOUCH_EVENTS) #include "Touch.h" #include "TouchList.h" #include "TouchEvent.h" #endif +#endif + +#if ENABLE(WEB_SOCKETS) +#include "WebSocket.h" +#endif #if ENABLE(WORKERS) +#include "AbstractWorker.h" #include "DedicatedWorkerContext.h" #include "Worker.h" #include "WorkerContext.h" @@ -229,10 +249,32 @@ #include "WorkerNavigator.h" #endif // WORKERS +#if ENABLE(SHARED_WORKERS) +#include "SharedWorker.h" +#include "SharedWorkerContext.h" +#endif // SHARED_WORKERS + +#if ENABLE(NOTIFICATIONS) +#include "Notification.h" +#include "NotificationCenter.h" +#endif // NOTIFICATIONS + #if ENABLE(XPATH) #include "XPathEvaluator.h" +#include "XPathException.h" +#include "XPathExpression.h" +#include "XPathNSResolver.h" +#include "XPathResult.h" #endif // XPATH +#if ENABLE(XSLT) +#include "XSLTProcessor.h" +#endif // XSLT + +#if ENABLE(INSPECTOR) +#include "InspectorBackend.h" +#endif // INSPECTOR + namespace WebCore { // A helper class for undetectable document.all diff --git a/WebCore/bindings/v8/DateExtension.cpp b/WebCore/bindings/v8/DateExtension.cpp new file mode 100644 index 0000000..778a15a --- /dev/null +++ b/WebCore/bindings/v8/DateExtension.cpp @@ -0,0 +1,123 @@ +/* + * Copyright (C) 2009 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "DateExtension.h" + +#include "V8Proxy.h" +#include "V8HiddenPropertyName.h" + +namespace WebCore { + +DateExtension* DateExtension::extension; + +static const char* dateExtensionName = "v8/DateExtension"; +static const char* dateExtensionScript = + "(function () {" + " var counter;" + " var orig_getTime;" + " function getTimeOverride() {" + " if (++counter > 1000)" + " OnSleepDetected();" + " return orig_getTime.call(this);" + " };" + " function enableSleepDetection(enable) {" + " if (enable) {" + " counter = 0;" + " orig_getTime = Date.prototype.getTime;" + " Date.prototype.getTime = getTimeOverride;" + " } else {" + " Date.prototype.getTime = orig_getTime;" + " }" + " };" + " native function Setup();" + " native function OnSleepDetected();" + " Setup(Date, enableSleepDetection);" + "})()"; + +DateExtension::DateExtension() : v8::Extension(dateExtensionName, dateExtensionScript) +{ +} + +DateExtension* DateExtension::get() +{ + if (!extension) + extension = new DateExtension(); + return extension; +} + +void DateExtension::setAllowSleep(bool allow) +{ + v8::Local<v8::Value> result = V8Proxy::retrieve()->context()->Global()->Get(v8::String::New("Date")); + if (result.IsEmpty()) + return; + + v8::Handle<v8::Object> dateObject = v8::Handle<v8::Object>::Cast(result); + if (dateObject.IsEmpty()) + return; + + v8::Local<v8::Value> sleepFunctionHandle = dateObject->GetHiddenValue(V8HiddenPropertyName::sleepFunction()); + if (sleepFunctionHandle.IsEmpty() || !sleepFunctionHandle->IsFunction()) + return; + + v8::Handle<v8::Value> argv[1]; + argv[0] = v8::String::New(allow ? "false" : "true"); + v8::Handle<v8::Function>::Cast(sleepFunctionHandle)->Call(v8::Object::New(), 1, argv); +} + +v8::Handle<v8::FunctionTemplate> DateExtension::GetNativeFunction(v8::Handle<v8::String> name) +{ + if (name->Equals(v8::String::New("Setup"))) + return v8::FunctionTemplate::New(Setup); + if (name->Equals(v8::String::New("OnSleepDetected"))) + return v8::FunctionTemplate::New(OnSleepDetected); + + return v8::Handle<v8::FunctionTemplate>(); +} + +v8::Handle<v8::Value> DateExtension::Setup(const v8::Arguments& args) +{ + if (args.Length() != 2 || !args[0]->IsObject() || !args[1]->IsFunction()) + return v8::Undefined(); + + v8::Handle<v8::Object> dateObject = v8::Handle<v8::Object>::Cast(args[0]); + v8::Handle<v8::Function> enableSleepDetectionFunction = v8::Handle<v8::Function>::Cast(args[1]); + + dateObject->SetHiddenValue(V8HiddenPropertyName::sleepFunction(), enableSleepDetectionFunction); + return v8::Undefined(); +} + +v8::Handle<v8::Value> DateExtension::OnSleepDetected(const v8::Arguments&) +{ + v8::V8::TerminateExecution(); + return v8::Undefined(); +} + +} // namespace WebCore diff --git a/WebCore/bindings/v8/DateExtension.h b/WebCore/bindings/v8/DateExtension.h new file mode 100644 index 0000000..2bccac4 --- /dev/null +++ b/WebCore/bindings/v8/DateExtension.h @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2009 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef DateExtension_h +#define DateExtension_h + +#include <v8.h> + +namespace WebCore { + +// Prevent "sleep" calls in unload handlers. +class DateExtension : public v8::Extension { +public: + static DateExtension* get(); + void setAllowSleep(bool allow); + +private: + DateExtension(); + virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction(v8::Handle<v8::String>); + static v8::Handle<v8::Value> Setup(const v8::Arguments&); + static v8::Handle<v8::Value> OnSleepDetected(const v8::Arguments&); + + static DateExtension* extension; +}; + +} // namespace WebCore + +#endif // DateExtension_h diff --git a/WebCore/bindings/v8/DerivedSourcesAllInOne.cpp b/WebCore/bindings/v8/DerivedSourcesAllInOne.cpp index e52a505..98832f3 100644 --- a/WebCore/bindings/v8/DerivedSourcesAllInOne.cpp +++ b/WebCore/bindings/v8/DerivedSourcesAllInOne.cpp @@ -33,10 +33,28 @@ #include "bindings/V8Attr.cpp" #include "bindings/V8BarInfo.cpp" +#include "bindings/V8BeforeLoadEvent.cpp" +#include "bindings/V8CanvasArray.cpp" +#include "bindings/V8CanvasArrayBuffer.cpp" +#include "bindings/V8CanvasBuffer.cpp" +#include "bindings/V8CanvasByteArray.cpp" +#include "bindings/V8CanvasFloatArray.cpp" +#include "bindings/V8CanvasFramebuffer.cpp" #include "bindings/V8CanvasGradient.cpp" +#include "bindings/V8CanvasIntArray.cpp" #include "bindings/V8CanvasPattern.cpp" #include "bindings/V8CanvasPixelArray.cpp" +#include "bindings/V8CanvasProgram.cpp" +#include "bindings/V8CanvasRenderbuffer.cpp" +#include "bindings/V8CanvasRenderingContext.cpp" #include "bindings/V8CanvasRenderingContext2D.cpp" +#include "bindings/V8CanvasRenderingContext3D.cpp" +#include "bindings/V8CanvasShader.cpp" +#include "bindings/V8CanvasShortArray.cpp" +#include "bindings/V8CanvasTexture.cpp" +#include "bindings/V8CanvasUnsignedByteArray.cpp" +#include "bindings/V8CanvasUnsignedIntArray.cpp" +#include "bindings/V8CanvasUnsignedShortArray.cpp" #include "bindings/V8CDATASection.cpp" #include "bindings/V8CharacterData.cpp" #include "bindings/V8ClientRect.cpp" @@ -150,11 +168,6 @@ #include "bindings/V8HTMLUListElement.cpp" #include "bindings/V8HTMLVideoElement.cpp" #include "bindings/V8ImageData.cpp" -#if PLATFORM(ANDROID) -// No inspector for now -#else -#include "bindings/V8InspectorBackend.cpp" -#endif #include "bindings/V8KeyboardEvent.cpp" #include "bindings/V8Location.cpp" #include "bindings/V8Media.cpp" @@ -175,6 +188,7 @@ #include "bindings/V8NodeList.cpp" #include "bindings/V8Notation.cpp" #include "bindings/V8OverflowEvent.cpp" +#include "bindings/V8PageTransitionEvent.cpp" #include "bindings/V8Plugin.cpp" #include "bindings/V8PluginArray.cpp" #include "bindings/V8ProcessingInstruction.cpp" @@ -207,6 +221,18 @@ #include "bindings/V8XMLHttpRequestUpload.cpp" #include "bindings/V8XMLSerializer.cpp" +#if ENABLE(OFFLINE_WEB_APPLICATIONS) +#include "bindings/V8DOMApplicationCache.cpp" +#endif + +#if ENABLE(DOM_STORAGE) +#include "bindings/V8Storage.cpp" +#include "bindings/V8StorageEvent.cpp" +#endif + +#if ENABLE(WEB_SOCKETS) +#include "bindings/V8WebSocket.cpp" +#endif #if ENABLE(DATABASE) #include "bindings/V8Database.cpp" @@ -216,14 +242,17 @@ #include "bindings/V8SQLTransaction.cpp" #endif - -#if ENABLE(OFFLINE_WEB_APPLICATIONS) -#include "bindings/V8DOMApplicationCache.cpp" +#if ENABLE(WORKERS) +#include "bindings/V8AbstractWorker.cpp" +#include "bindings/V8DedicatedWorkerContext.cpp" +#include "bindings/V8Worker.cpp" +#include "bindings/V8WorkerContext.cpp" +#include "bindings/V8WorkerLocation.cpp" +#include "bindings/V8WorkerNavigator.cpp" #endif -#if ENABLE(DOM_STORAGE) -#include "bindings/V8Storage.cpp" -#include "bindings/V8StorageEvent.cpp" +#if ENABLE(SHARED_WORKERS) +#include "bindings/V8SharedWorker.cpp" #endif #if ENABLE(SVG) @@ -251,7 +280,6 @@ #include "bindings/V8SVGClipPathElement.cpp" #include "bindings/V8SVGColor.cpp" #include "bindings/V8SVGCursorElement.cpp" -#include "bindings/V8SVGDefinitionSrcElement.cpp" #include "bindings/V8SVGDefsElement.cpp" #include "bindings/V8SVGDescElement.cpp" #include "bindings/V8SVGDocument.cpp" @@ -339,16 +367,6 @@ #include "bindings/V8SVGZoomEvent.cpp" #endif -#if ENABLE(WORKERS) -#include "bindings/V8AbstractWorker.cpp" -#include "bindings/V8DedicatedWorkerContext.cpp" -#include "bindings/V8SharedWorker.cpp" -#include "bindings/V8Worker.cpp" -#include "bindings/V8WorkerContext.cpp" -#include "bindings/V8WorkerLocation.cpp" -#include "bindings/V8WorkerNavigator.cpp" -#endif - #if ENABLE(XPATH) #include "bindings/V8XPathEvaluator.cpp" #include "bindings/V8XPathException.cpp" @@ -360,3 +378,15 @@ #if ENABLE(XSLT) #include "bindings/V8XSLTProcessor.cpp" #endif + +#if ENABLE(INSPECTOR) +#include "bindings/V8InspectorBackend.cpp" +#endif + +#if PLATFORM(ANDROID) +// TODO: Upstream NOTIFICATIONS guard. +#if ENABLE(NOTIFICATIONS) +#include "bindings/V8Notification.cpp" +#include "bindings/V8NotificationCenter.cpp" +#endif +#endif diff --git a/WebCore/bindings/v8/NPV8Object.cpp b/WebCore/bindings/v8/NPV8Object.cpp index cb2a6c1..13403ec 100644 --- a/WebCore/bindings/v8/NPV8Object.cpp +++ b/WebCore/bindings/v8/NPV8Object.cpp @@ -276,7 +276,7 @@ bool _NPN_EvaluateHelper(NPP npp, bool popupsAllowed, NPObject* npObject, NPStri filename = "npscript"; WebCore::String script = WebCore::String::fromUTF8(npScript->UTF8Characters, npScript->UTF8Length); - v8::Local<v8::Value> v8result = proxy->evaluate(WebCore::ScriptSourceCode(script, WebCore::KURL(filename)), 0); + v8::Local<v8::Value> v8result = proxy->evaluate(WebCore::ScriptSourceCode(script, WebCore::KURL(WebCore::ParsedURLString, filename)), 0); if (v8result.IsEmpty()) return false; @@ -413,8 +413,12 @@ bool _NPN_HasMethod(NPP npp, NPObject* npObject, NPIdentifier methodName) void _NPN_SetException(NPObject* npObject, const NPUTF8 *message) { - if (npObject->_class != npScriptObjectClass) + if (npObject->_class != npScriptObjectClass) { + // We won't be able to find a proper scope for this exception, so just throw it. + // This is consistent with JSC, which throws a global exception all the time. + V8Proxy::throwError(V8Proxy::GeneralError, message); return; + } v8::HandleScope handleScope; v8::Handle<v8::Context> context = toV8Context(0, npObject); if (context.IsEmpty()) diff --git a/WebCore/bindings/v8/OwnHandle.h b/WebCore/bindings/v8/OwnHandle.h index 6580674..17c551c 100644 --- a/WebCore/bindings/v8/OwnHandle.h +++ b/WebCore/bindings/v8/OwnHandle.h @@ -45,15 +45,13 @@ namespace WebCore { v8::Handle<T> get() const { return m_handle; } void set(v8::Handle<T> handle) { clear(); m_handle = v8::Persistent<T>::New(handle); } - // FIXME: What if we release a weak handle? Won't the callback do the wrong thing? - v8::Persistent<T> release() { v8::Persistent<T> result = m_handle; m_handle.Clear(); return result; } - void adopt(v8::Persistent<T> handle) { clear(); m_handle = handle; } - // Note: This is clear in the OwnPtr sense, not the v8::Handle sense. void clear() { if (m_handle.IsEmpty()) return; + if (m_handle.IsWeak()) + m_handle.ClearWeak(); m_handle.Dispose(); m_handle.Clear(); } diff --git a/WebCore/bindings/v8/ScheduledAction.cpp b/WebCore/bindings/v8/ScheduledAction.cpp index 44e8a37..268e177 100644 --- a/WebCore/bindings/v8/ScheduledAction.cpp +++ b/WebCore/bindings/v8/ScheduledAction.cpp @@ -44,8 +44,9 @@ namespace WebCore { -ScheduledAction::ScheduledAction(v8::Handle<v8::Function> func, int argc, v8::Handle<v8::Value> argv[]) - : m_code(String(), KURL(), 0) +ScheduledAction::ScheduledAction(v8::Handle<v8::Context> context, v8::Handle<v8::Function> func, int argc, v8::Handle<v8::Value> argv[]) + : m_context(context) + , m_code(String(), KURL(), 0) { m_function = v8::Persistent<v8::Function>::New(func); @@ -107,7 +108,7 @@ void ScheduledAction::execute(V8Proxy* proxy) LOCK_V8; v8::HandleScope handleScope; - v8::Local<v8::Context> v8Context = proxy->context(); + v8::Handle<v8::Context> v8Context = v8::Local<v8::Context>::New(m_context.get()); if (v8Context.IsEmpty()) return; // JS may not be enabled. @@ -136,7 +137,7 @@ void ScheduledAction::execute(WorkerContext* workerContext) if (!m_function.IsEmpty() && m_function->IsFunction()) { LOCK_V8; v8::HandleScope handleScope; - v8::Local<v8::Context> v8Context = scriptController->proxy()->context(); + v8::Handle<v8::Context> v8Context = v8::Local<v8::Context>::New(m_context.get()); ASSERT(!v8Context.IsEmpty()); v8::Context::Scope scope(v8Context); m_function->Call(v8Context->Global(), m_argc, m_argv); diff --git a/WebCore/bindings/v8/ScheduledAction.h b/WebCore/bindings/v8/ScheduledAction.h index 53694c7..003885f 100644 --- a/WebCore/bindings/v8/ScheduledAction.h +++ b/WebCore/bindings/v8/ScheduledAction.h @@ -31,7 +31,9 @@ #ifndef ScheduledAction_h #define ScheduledAction_h +#include "OwnHandle.h" #include "ScriptSourceCode.h" +#include "V8GCController.h" #include <v8.h> @@ -44,9 +46,10 @@ namespace WebCore { class ScheduledAction { public: - ScheduledAction(v8::Handle<v8::Function>, int argc, v8::Handle<v8::Value> argv[]); - explicit ScheduledAction(const WebCore::String& code, const KURL& url = KURL()) - : m_argc(0) + ScheduledAction(v8::Handle<v8::Context>, v8::Handle<v8::Function>, int argc, v8::Handle<v8::Value> argv[]); + explicit ScheduledAction(v8::Handle<v8::Context> context, const WebCore::String& code, const KURL& url = KURL()) + : m_context(context) + , m_argc(0) , m_argv(0) , m_code(code, url) { @@ -61,6 +64,7 @@ namespace WebCore { void execute(WorkerContext*); #endif + OwnHandle<v8::Context> m_context; v8::Persistent<v8::Function> m_function; int m_argc; v8::Persistent<v8::Value>* m_argv; diff --git a/WebCore/bindings/v8/ScriptCallFrame.cpp b/WebCore/bindings/v8/ScriptCallFrame.cpp index ab30862..4c29814 100644 --- a/WebCore/bindings/v8/ScriptCallFrame.cpp +++ b/WebCore/bindings/v8/ScriptCallFrame.cpp @@ -42,7 +42,7 @@ namespace WebCore { ScriptCallFrame::ScriptCallFrame(const String& functionName, const String& urlString, int lineNumber, const v8::Arguments& arguments, unsigned skipArgumentCount) : m_functionName(functionName) - , m_sourceURL(urlString) + , m_sourceURL(ParsedURLString, urlString) , m_lineNumber(lineNumber) { for (int i = 0; i < arguments.Length(); ++i) diff --git a/WebCore/bindings/v8/ScriptController.cpp b/WebCore/bindings/v8/ScriptController.cpp index 48c5115..85af072 100644 --- a/WebCore/bindings/v8/ScriptController.cpp +++ b/WebCore/bindings/v8/ScriptController.cpp @@ -51,7 +51,6 @@ #include "npruntime_priv.h" #include "NPV8Object.h" #include "ScriptSourceCode.h" -#include "ScriptState.h" #include "Widget.h" #include "XSSAuditor.h" @@ -61,6 +60,15 @@ namespace WebCore { +void ScriptController::initializeThreading() +{ + static bool initializedThreading = false; + if (!initializedThreading) { + WTF::initializeThreading(); + initializedThreading = true; + } +} + void ScriptController::setFlags(const char* string, int length) { v8::V8::SetFlagsFromString(string, length); @@ -96,7 +104,6 @@ ScriptController::ScriptController(Frame* frame) , m_sourceURL(0) , m_processingTimerCallback(false) , m_paused(false) - , m_scriptState(new ScriptState(frame)) , m_proxy(new V8Proxy(frame)) #if ENABLE(NETSCAPE_PLUGIN_API) , m_windowScriptNPObject(0) @@ -186,9 +193,14 @@ bool ScriptController::processingUserGesture() const return false; } -void ScriptController::evaluateInNewWorld(const Vector<ScriptSourceCode>& sources, int extensionGroup) +void ScriptController::evaluateInIsolatedWorld(unsigned worldID, const Vector<ScriptSourceCode>& sources) +{ + m_proxy->evaluateInIsolatedWorld(worldID, sources, 0); +} + +void ScriptController::evaluateInIsolatedWorld(unsigned worldID, const Vector<ScriptSourceCode>& sources, int extensionGroup) { - m_proxy->evaluateInNewWorld(sources, extensionGroup); + m_proxy->evaluateInIsolatedWorld(worldID, sources, extensionGroup); } void ScriptController::evaluateInNewContext(const Vector<ScriptSourceCode>& sources, int extensionGroup) @@ -202,12 +214,7 @@ ScriptValue ScriptController::evaluate(const ScriptSourceCode& sourceCode) LOCK_V8; String sourceURL = sourceCode.url(); - if (sourceURL.isNull() && !m_XSSAuditor->canEvaluateJavaScriptURL(sourceCode.source())) { - // This JavaScript URL is not safe to be evaluated. - return ScriptValue(); - } - - if (!sourceURL.isNull() && !m_XSSAuditor->canEvaluate(sourceCode.source())) { + if (!m_XSSAuditor->canEvaluate(sourceCode.source())) { // This script is not safe to be evaluated. return ScriptValue(); } @@ -343,7 +350,7 @@ PassScriptInstance ScriptController::createScriptInstanceForWidget(Widget* widge return V8ScriptInstance::create(wrapper); } -void ScriptController::cleanupScriptObjectsForPlugin(void* nativeHandle) +void ScriptController::cleanupScriptObjectsForPlugin(Widget* nativeHandle) { PluginObjectMap::iterator it = m_pluginObjects.find(nativeHandle); if (it == m_pluginObjects.end()) diff --git a/WebCore/bindings/v8/ScriptController.h b/WebCore/bindings/v8/ScriptController.h index d614619..ec15103 100644 --- a/WebCore/bindings/v8/ScriptController.h +++ b/WebCore/bindings/v8/ScriptController.h @@ -46,7 +46,6 @@ namespace WebCore { class Frame; class HTMLPlugInElement; class ScriptSourceCode; - class ScriptState; class String; class Widget; class XSSAuditor; @@ -60,16 +59,27 @@ namespace WebCore { // or this accessor should be made JSProxy* V8Proxy* proxy() { return m_proxy.get(); } + // This function must be called from the main thread. It is safe to call it repeatedly. + static void initializeThreading(); + // Evaluate a script file in the environment of this proxy. // If succeeded, 'succ' is set to true and result is returned // as a string. ScriptValue evaluate(const ScriptSourceCode&); - // Executes JavaScript in a new world associated with the web frame. The - // script gets its own global scope, its own prototypes for intrinsic - // JavaScript objects (String, Array, and so-on), and its own wrappers for - // all DOM nodes and DOM constructors. - void evaluateInNewWorld(const Vector<ScriptSourceCode>&, int extensionGroup); + void evaluateInIsolatedWorld(unsigned worldID, const Vector<ScriptSourceCode>&); + + // Executes JavaScript in an isolated world. The script gets its own global scope, + // its own prototypes for intrinsic JavaScript objects (String, Array, and so-on), + // and its own wrappers for all DOM nodes and DOM constructors. + // + // If an isolated world with the specified ID already exists, it is reused. + // Otherwise, a new world is created. + // + // If the worldID is 0, a new world is always created. + // + // FIXME: Get rid of extensionGroup here. + void evaluateInIsolatedWorld(unsigned worldID, const Vector<ScriptSourceCode>&, int extensionGroup); // Executes JavaScript in a new context associated with the web frame. The // script gets its own global scope and its own prototypes for intrinsic @@ -86,8 +96,6 @@ namespace WebCore { // with what JSC does as well. ScriptController* windowShell() { return this; } - ScriptState* state() const { return m_scriptState.get(); } - XSSAuditor* xssAuditor() { return m_XSSAuditor.get(); } void collectGarbage(); @@ -146,7 +154,7 @@ namespace WebCore { void updateSecurityOrigin(); void clearScriptObjects(); void updatePlatformScriptObjects(); - void cleanupScriptObjectsForPlugin(void*); + void cleanupScriptObjectsForPlugin(Widget*); #if ENABLE(NETSCAPE_PLUGIN_API) NPObject* createScriptObjectForPluginElement(HTMLPlugInElement*); @@ -160,9 +168,8 @@ namespace WebCore { bool m_processingTimerCallback; bool m_paused; - OwnPtr<ScriptState> m_scriptState; OwnPtr<V8Proxy> m_proxy; - typedef HashMap<void*, NPObject*> PluginObjectMap; + typedef HashMap<Widget*, NPObject*> PluginObjectMap; // A mapping between Widgets and their corresponding script object. // This list is used so that when the plugin dies, we can immediately diff --git a/WebCore/bindings/v8/ScriptEventListener.cpp b/WebCore/bindings/v8/ScriptEventListener.cpp index 7a8aa64..16ca70f 100644 --- a/WebCore/bindings/v8/ScriptEventListener.cpp +++ b/WebCore/bindings/v8/ScriptEventListener.cpp @@ -33,7 +33,11 @@ #include "Attribute.h" #include "Document.h" +#include "EventListener.h" #include "Frame.h" +#include "ScriptScope.h" +#include "V8AbstractEventListener.h" +#include "V8Binding.h" #include "XSSAuditor.h" namespace WebCore { @@ -68,4 +72,18 @@ PassRefPtr<V8LazyEventListener> createAttributeEventListener(Frame* frame, Attri return V8LazyEventListener::create(frame, attr->value(), attr->localName().string(), frame->document()->isSVGDocument()); } +String getEventListenerHandlerBody(ScriptExecutionContext*, ScriptState* scriptState, EventListener* listener) +{ + if (listener->type() != EventListener::JSEventListenerType) + return ""; + + ScriptScope scope(scriptState); + V8AbstractEventListener* v8Listener = static_cast<V8AbstractEventListener*>(listener); + v8::Handle<v8::Object> function = v8Listener->getListenerObject(); + if (function.IsEmpty()) + return ""; + + return toWebCoreStringWithNullCheck(function); +} + } // namespace WebCore diff --git a/WebCore/bindings/v8/ScriptEventListener.h b/WebCore/bindings/v8/ScriptEventListener.h index 0171120..ce12a53 100644 --- a/WebCore/bindings/v8/ScriptEventListener.h +++ b/WebCore/bindings/v8/ScriptEventListener.h @@ -38,11 +38,14 @@ namespace WebCore { class Attribute; + class EventListener; class Frame; class Node; + class ScriptState; PassRefPtr<V8LazyEventListener> createAttributeEventListener(Node*, Attribute*); PassRefPtr<V8LazyEventListener> createAttributeEventListener(Frame*, Attribute*); + String getEventListenerHandlerBody(ScriptExecutionContext*, ScriptState*, EventListener*); } // namespace WebCore diff --git a/WebCore/bindings/v8/ScriptFunctionCall.cpp b/WebCore/bindings/v8/ScriptFunctionCall.cpp index 2fa43d5..9ff6b8c 100644 --- a/WebCore/bindings/v8/ScriptFunctionCall.cpp +++ b/WebCore/bindings/v8/ScriptFunctionCall.cpp @@ -31,8 +31,6 @@ #include "config.h" #include "ScriptFunctionCall.h" -#include "Document.h" -#include "Frame.h" #include "ScriptScope.h" #include "ScriptState.h" #include "ScriptString.h" @@ -40,19 +38,13 @@ #include "V8Binding.h" #include "V8Proxy.h" +#include "V8Utilities.h" #include <v8.h> #include <wtf/OwnArrayPtr.h> namespace WebCore { -static void reportException(ScriptState* scriptState, v8::TryCatch &exceptionCatcher) -{ - v8::Local<v8::Message> message = exceptionCatcher.Message(); - scriptState->frame()->document()->reportException(toWebCoreString(message->Get()), message->GetLineNumber(), toWebCoreString(message->GetScriptResourceName())); - exceptionCatcher.Reset(); -} - ScriptFunctionCall::ScriptFunctionCall(ScriptState* scriptState, const ScriptObject& thisObject, const String& name) : m_scriptState(scriptState) , m_thisObject(thisObject) diff --git a/WebCore/bindings/v8/ScriptObject.cpp b/WebCore/bindings/v8/ScriptObject.cpp index b6943bd..ac8051d 100644 --- a/WebCore/bindings/v8/ScriptObject.cpp +++ b/WebCore/bindings/v8/ScriptObject.cpp @@ -97,6 +97,13 @@ bool ScriptObject::set(const char* name, int value) return scope.success(); } +bool ScriptObject::set(const char* name, unsigned value) +{ + ScriptScope scope(m_scriptState); + v8Object()->Set(v8::String::New(name), v8::Number::New(value)); + return scope.success(); +} + bool ScriptObject::set(const char* name, bool value) { ScriptScope scope(m_scriptState); diff --git a/WebCore/bindings/v8/ScriptObject.h b/WebCore/bindings/v8/ScriptObject.h index dcee3a5..f741f89 100644 --- a/WebCore/bindings/v8/ScriptObject.h +++ b/WebCore/bindings/v8/ScriptObject.h @@ -53,6 +53,7 @@ namespace WebCore { bool set(const char* name, double); bool set(const char* name, long long); bool set(const char* name, int); + bool set(const char* name, unsigned); bool set(const char* name, bool); static ScriptObject createNew(ScriptState*); diff --git a/WebCore/bindings/v8/ScriptObjectQuarantine.cpp b/WebCore/bindings/v8/ScriptObjectQuarantine.cpp index 053cf68..5dd0dc5 100644 --- a/WebCore/bindings/v8/ScriptObjectQuarantine.cpp +++ b/WebCore/bindings/v8/ScriptObjectQuarantine.cpp @@ -35,9 +35,11 @@ #include "Document.h" #include "DOMWindow.h" #include "Frame.h" +#include "InspectorController.h" #include "Page.h" #include "ScriptObject.h" #include "ScriptValue.h" +#include "Storage.h" #include "V8Binding.h" #include "V8Proxy.h" @@ -61,10 +63,11 @@ bool getQuarantinedScriptObject(Database* database, ScriptObject& quarantinedObj return false; } -bool getQuarantinedScriptObject(Frame* frame, Storage* storage, ScriptObject& quarantinedObject) +bool getQuarantinedScriptObject(Storage* storage, ScriptObject& quarantinedObject) { - ASSERT(frame); ASSERT(storage); + Frame* frame = storage->frame(); + ASSERT(frame); #if ENABLE(DOM_STORAGE) v8::HandleScope handleScope; @@ -73,7 +76,8 @@ bool getQuarantinedScriptObject(Frame* frame, Storage* storage, ScriptObject& qu v8::Context::Scope scope(context); v8::Handle<v8::Value> v8Storage = V8DOMWrapper::convertToV8Object(V8ClassIndex::STORAGE, storage); - quarantinedObject = ScriptObject(frame->script()->state(), v8::Local<v8::Object>(v8::Object::Cast(*v8Storage))); + ScriptState* scriptState = frame->page()->inspectorController()->frontendScriptState(); + quarantinedObject = ScriptObject(scriptState, v8::Local<v8::Object>(v8::Object::Cast(*v8Storage))); #else ASSERT_NOT_REACHED(); quarantinedObject = ScriptObject(); @@ -94,7 +98,8 @@ bool getQuarantinedScriptObject(Node* node, ScriptObject& quarantinedObject) v8::Context::Scope scope(context); v8::Handle<v8::Value> v8Node = V8DOMWrapper::convertNodeToV8Object(node); - quarantinedObject = ScriptObject(frame->script()->state(), v8::Local<v8::Object>(v8::Object::Cast(*v8Node))); + ScriptState* scriptState = frame->page()->inspectorController()->frontendScriptState(); + quarantinedObject = ScriptObject(scriptState, v8::Local<v8::Object>(v8::Object::Cast(*v8Node))); return true; } @@ -111,7 +116,8 @@ bool getQuarantinedScriptObject(DOMWindow* domWindow, ScriptObject& quarantinedO v8::Context::Scope scope(context); v8::Handle<v8::Value> v8DomWindow = V8DOMWrapper::convertToV8Object(V8ClassIndex::DOMWINDOW, domWindow); - quarantinedObject = ScriptObject(frame->script()->state(), v8::Local<v8::Object>(v8::Object::Cast(*v8DomWindow))); + ScriptState* scriptState = frame->page()->inspectorController()->frontendScriptState(); + quarantinedObject = ScriptObject(scriptState, v8::Local<v8::Object>(v8::Object::Cast(*v8DomWindow))); return true; } diff --git a/WebCore/bindings/v8/ScriptObjectQuarantine.h b/WebCore/bindings/v8/ScriptObjectQuarantine.h index 3b7ccff..712dd9b 100644 --- a/WebCore/bindings/v8/ScriptObjectQuarantine.h +++ b/WebCore/bindings/v8/ScriptObjectQuarantine.h @@ -42,7 +42,6 @@ namespace WebCore { class Database; class DOMWindow; - class Frame; class Node; class ScriptObject; class ScriptValue; @@ -51,7 +50,7 @@ namespace WebCore { ScriptValue quarantineValue(ScriptState*, const ScriptValue&); bool getQuarantinedScriptObject(Database* database, ScriptObject& quarantinedObject); - bool getQuarantinedScriptObject(Frame* frame, Storage* storage, ScriptObject& quarantinedObject); + bool getQuarantinedScriptObject(Storage* storage, ScriptObject& quarantinedObject); bool getQuarantinedScriptObject(Node* node, ScriptObject& quarantinedObject); bool getQuarantinedScriptObject(DOMWindow* domWindow, ScriptObject& quarantinedObject); diff --git a/WebCore/bindings/v8/ScriptScope.cpp b/WebCore/bindings/v8/ScriptScope.cpp index 52cab10..727ab25 100644 --- a/WebCore/bindings/v8/ScriptScope.cpp +++ b/WebCore/bindings/v8/ScriptScope.cpp @@ -36,14 +36,13 @@ #include "Document.h" #include "Frame.h" #include "V8Binding.h" -#include "V8Proxy.h" #include <v8.h> namespace WebCore { ScriptScope::ScriptScope(ScriptState* scriptState, bool reportExceptions) - : m_context(V8Proxy::context(scriptState->frame())) + : m_context(scriptState->context()) , m_scope(m_context) , m_scriptState(scriptState) , m_reportExceptions(reportExceptions) diff --git a/WebCore/bindings/v8/ScriptSourceCode.h b/WebCore/bindings/v8/ScriptSourceCode.h index fea387a..5c16168 100644 --- a/WebCore/bindings/v8/ScriptSourceCode.h +++ b/WebCore/bindings/v8/ScriptSourceCode.h @@ -50,7 +50,7 @@ public: // Not sure if that matters. ScriptSourceCode(CachedScript* cs) : m_source(cs->script()) - , m_url(cs->url()) + , m_url(ParsedURLString, cs->url()) , m_startLine(1) { } diff --git a/WebCore/bindings/v8/ScriptState.cpp b/WebCore/bindings/v8/ScriptState.cpp index b9e240c..99557b5 100644 --- a/WebCore/bindings/v8/ScriptState.cpp +++ b/WebCore/bindings/v8/ScriptState.cpp @@ -42,9 +42,22 @@ namespace WebCore { ScriptState::ScriptState(Frame* frame) : m_frame(frame) + , m_context(v8::Persistent<v8::Context>::New(V8Proxy::mainWorldContext(frame))) { } +ScriptState::ScriptState(Frame* frame, v8::Handle<v8::Context> context) + : m_frame(frame) + , m_context(v8::Persistent<v8::Context>::New(context)) +{ +} + +ScriptState::~ScriptState() +{ + m_context.Dispose(); + m_context.Clear(); +} + ScriptState* scriptStateFromNode(Node* node) { // This should be never reached with V8 bindings (WebKit only uses it @@ -55,7 +68,9 @@ ScriptState* scriptStateFromNode(Node* node) ScriptState* scriptStateFromPage(Page* page) { - return page->mainFrame()->script()->state(); + // This should be never reached with V8 bindings. + ASSERT_NOT_REACHED(); + return 0; } } diff --git a/WebCore/bindings/v8/ScriptState.h b/WebCore/bindings/v8/ScriptState.h index 8fbcfa3..e44e914 100644 --- a/WebCore/bindings/v8/ScriptState.h +++ b/WebCore/bindings/v8/ScriptState.h @@ -32,16 +32,19 @@ #define ScriptState_h #include <v8.h> +#include <wtf/Noncopyable.h> namespace WebCore { class Node; class Page; class Frame; - class ScriptState { + class ScriptState : public Noncopyable { public: ScriptState() { } ScriptState(Frame* frame); + ScriptState(Frame* frame, v8::Handle<v8::Context> context); + ~ScriptState(); bool hadException() { return !m_exception.IsEmpty(); } void setException(v8::Local<v8::Value> exception) @@ -51,10 +54,15 @@ namespace WebCore { v8::Local<v8::Value> exception() { return m_exception; } Frame* frame() const { return m_frame; } + v8::Local<v8::Context> context() const + { + return v8::Local<v8::Context>::New(m_context); + } private: v8::Local<v8::Value> m_exception; Frame* m_frame; + v8::Persistent<v8::Context> m_context; }; ScriptState* scriptStateFromNode(Node*); diff --git a/WebCore/bindings/v8/ScriptValue.h b/WebCore/bindings/v8/ScriptValue.h index 004851b..a429593 100644 --- a/WebCore/bindings/v8/ScriptValue.h +++ b/WebCore/bindings/v8/ScriptValue.h @@ -110,7 +110,12 @@ public: { return m_value->IsUndefined(); } - + + bool isObject() const + { + return m_value->IsObject(); + } + bool hasNoValue() const { return m_value.IsEmpty(); @@ -128,7 +133,7 @@ public: m_value.Clear(); } - ~ScriptValue() + virtual ~ScriptValue() { clear(); } diff --git a/WebCore/bindings/v8/SerializedScriptValue.h b/WebCore/bindings/v8/SerializedScriptValue.h new file mode 100644 index 0000000..26a4199 --- /dev/null +++ b/WebCore/bindings/v8/SerializedScriptValue.h @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2009 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef SerializedScriptValue_h +#define SerializedScriptValue_h + +#include "ScriptValue.h" +#include "V8Binding.h" +#include <v8.h> +#include <wtf/RefCounted.h> + +namespace WebCore { + +class SerializedScriptValue : public RefCounted<SerializedScriptValue> { +public: + static PassRefPtr<SerializedScriptValue> create(String string) + { + return adoptRef(new SerializedScriptValue(string)); + } + + static PassRefPtr<SerializedScriptValue> create() + { + return adoptRef(new SerializedScriptValue()); + } + + PassRefPtr<SerializedScriptValue> release() + { + RefPtr<SerializedScriptValue> result = adoptRef(new SerializedScriptValue(m_data)); + m_data = String(); + return result.release(); + } + + String toString() + { + return m_data; + } + +private: + SerializedScriptValue(String string) + : m_data(string) + { + } + + SerializedScriptValue() { } + String m_data; +}; + +} // namespace WebCore + +#endif // SerializedScriptValue_h diff --git a/WebCore/bindings/v8/SharedPersistent.h b/WebCore/bindings/v8/SharedPersistent.h new file mode 100644 index 0000000..c1232ae --- /dev/null +++ b/WebCore/bindings/v8/SharedPersistent.h @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2009 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef SharedPersistent_h +#define SharedPersistent_h + +#include <v8.h> + +namespace WebCore { + + // A shareable reference to a v8 persistent handle. Using a shared + // persistent any number of objects can share a reference to a v8 + // object and when it should no longer be accessible the object's + // owner can clear it. + template <typename T> + class SharedPersistent : public WTF::RefCounted<SharedPersistent<T> > { + public: + void set(v8::Persistent<T> value) + { + m_value = value; + } + v8::Persistent<T> get() + { + return m_value; + } + void disposeHandle() + { + if (!m_value.IsEmpty()) { + m_value.Dispose(); + m_value.Clear(); + } + } + static PassRefPtr<SharedPersistent<T> > create(v8::Persistent<T> value) + { + return adoptRef(new SharedPersistent<T>(value)); + } + static PassRefPtr<SharedPersistent<T> > create() + { + return create(v8::Persistent<T>()); + } + private: + explicit SharedPersistent(v8::Persistent<T> value) : m_value(value) { } + v8::Persistent<T> m_value; + }; + +} // namespace WebCore + +#endif // SharedPersistent_h diff --git a/WebCore/bindings/v8/V8AbstractEventListener.cpp b/WebCore/bindings/v8/V8AbstractEventListener.cpp index 0c81846..032b6b9 100644 --- a/WebCore/bindings/v8/V8AbstractEventListener.cpp +++ b/WebCore/bindings/v8/V8AbstractEventListener.cpp @@ -31,17 +31,30 @@ #include "config.h" #include "V8AbstractEventListener.h" +#include "DateExtension.h" #include "Document.h" #include "Event.h" #include "Frame.h" #include "Tokenizer.h" #include "V8Binding.h" +#include "V8EventListenerList.h" +#include "V8Proxy.h" +#include "V8Utilities.h" namespace WebCore { -V8AbstractEventListener::V8AbstractEventListener(Frame* frame, bool isAttribute) - : m_isAttribute(isAttribute) +static void weakEventListenerCallback(v8::Persistent<v8::Value>, void* parameter) +{ + V8AbstractEventListener* listener = static_cast<V8AbstractEventListener*>(parameter); + listener->disposeListenerObject(); +} + +V8AbstractEventListener::V8AbstractEventListener(Frame* frame, PassRefPtr<V8ListenerGuard> guard, bool isAttribute) + : EventListener(JSEventListenerType) + , m_isWeak(true) + , m_isAttribute(isAttribute) , m_frame(frame) + , m_guard(guard) , m_lineNumber(0) , m_columnNumber(0) { @@ -51,8 +64,7 @@ V8AbstractEventListener::V8AbstractEventListener(Frame* frame, bool isAttribute) // We might be called directly from the parser. v8::HandleScope handleScope; - m_context.set(V8Proxy::context(m_frame)); - m_context.makeWeak(); + m_context = V8Proxy::shared_context(m_frame); // Get the position in the source if any. if (m_isAttribute && m_frame->document()->tokenizer()) { @@ -61,52 +73,17 @@ V8AbstractEventListener::V8AbstractEventListener(Frame* frame, bool isAttribute) } } -void V8AbstractEventListener::invokeEventHandler(v8::Handle<v8::Context> v8Context, Event* event, v8::Handle<v8::Value> jsEvent, bool isWindowEvent) +V8AbstractEventListener::~V8AbstractEventListener() { - // We push the event being processed into the global object, so that it can be exposed by DOMWindow's bindings. - v8::Local<v8::String> eventSymbol = v8::String::NewSymbol("event"); - v8::Local<v8::Value> returnValue; - - { - // Catch exceptions thrown in the event handler so they do not propagate to javascript code that caused the event to fire. - v8::TryCatch tryCatch; - tryCatch.SetVerbose(true); - - // Save the old 'event' property so we can restore it later. - v8::Local<v8::Value> savedEvent = v8Context->Global()->GetHiddenValue(eventSymbol); - tryCatch.Reset(); - - // Make the event available in the global object, so DOMWindow can expose it. - v8Context->Global()->SetHiddenValue(eventSymbol, jsEvent); - tryCatch.Reset(); - - // Call the event handler. - returnValue = callListenerFunction(jsEvent, event, isWindowEvent); - tryCatch.Reset(); - - // Restore the old event. This must be done for all exit paths through this method. - if (savedEvent.IsEmpty()) - v8Context->Global()->SetHiddenValue(eventSymbol, v8::Undefined()); - else - v8Context->Global()->SetHiddenValue(eventSymbol, savedEvent); - tryCatch.Reset(); + if (!m_listener.IsEmpty()) { + v8::HandleScope scope; + v8::Local<v8::Object> listener = v8::Local<v8::Object>::New(m_listener); + V8EventListenerList::clearWrapper(listener, m_isAttribute); } - - ASSERT(!V8Proxy::handleOutOfMemory() || returnValue.IsEmpty()); - - if (returnValue.IsEmpty()) - return; - - if (!returnValue->IsNull() && !returnValue->IsUndefined() && event->storesResultAsString()) - event->storeResult(toWebCoreString(returnValue)); - - // Prevent default action if the return value is false; - // FIXME: Add example, and reference to bug entry. - if (m_isAttribute && returnValue->IsBoolean() && !returnValue->BooleanValue()) - event->preventDefault(); + disposeListenerObject(); } -void V8AbstractEventListener::handleEvent(Event* event, bool isWindowEvent) +void V8AbstractEventListener::handleEvent(ScriptExecutionContext* scriptExecutionContext, Event* event) { // EventListener could be disconnected from the frame. if (disconnected()) @@ -119,7 +96,12 @@ void V8AbstractEventListener::handleEvent(Event* event, bool isWindowEvent) LOCK_V8; v8::HandleScope handleScope; - v8::Handle<v8::Context> v8Context = m_context.get(); + if (!m_context) + return; + + // Create a new local handle since the persistent handle stored in + // m_context may be disposed before we're done. + v8::Handle<v8::Context> v8Context = v8::Local<v8::Context>::New(m_context->get()); if (v8Context.IsEmpty()) return; @@ -132,7 +114,7 @@ void V8AbstractEventListener::handleEvent(Event* event, bool isWindowEvent) // Get the V8 wrapper for the event object. v8::Handle<v8::Value> jsEvent = V8DOMWrapper::convertEventToV8Object(event); - invokeEventHandler(v8Context, event, jsEvent, isWindowEvent); + invokeEventHandler(v8Context, event, jsEvent); Document::updateStyleForAllDocuments(); } @@ -148,14 +130,83 @@ void V8AbstractEventListener::disposeListenerObject() } } -v8::Local<v8::Object> V8AbstractEventListener::getReceiverObject(Event* event, bool isWindowEvent) +void V8AbstractEventListener::setListenerObject(v8::Handle<v8::Object> listener) +{ + disposeListenerObject(); + m_listener = v8::Persistent<v8::Object>::New(listener); +#ifndef NDEBUG + V8GCController::registerGlobalHandle(EVENT_LISTENER, this, m_listener); +#endif + if (m_isWeak) + m_listener.MakeWeak(this, &weakEventListenerCallback); +} + +void V8AbstractEventListener::invokeEventHandler(v8::Handle<v8::Context> v8Context, Event* event, v8::Handle<v8::Value> jsEvent) +{ + // We push the event being processed into the global object, so that it can be exposed by DOMWindow's bindings. + v8::Local<v8::String> eventSymbol = v8::String::NewSymbol("event"); + v8::Local<v8::Value> returnValue; + + // In beforeunload/unload handlers, we want to avoid sleeps which do tight loops of calling Date.getTime(). + if (event->type() == "beforeunload" || event->type() == "unload") + DateExtension::get()->setAllowSleep(false); + + { + // Catch exceptions thrown in the event handler so they do not propagate to javascript code that caused the event to fire. + v8::TryCatch tryCatch; + tryCatch.SetVerbose(true); + + // Save the old 'event' property so we can restore it later. + v8::Local<v8::Value> savedEvent = v8Context->Global()->GetHiddenValue(eventSymbol); + tryCatch.Reset(); + + // Make the event available in the global object, so DOMWindow can expose it. + v8Context->Global()->SetHiddenValue(eventSymbol, jsEvent); + tryCatch.Reset(); + + // Call the event handler. + tryCatch.SetVerbose(false); // We do not want to report the exception to the inspector console. + returnValue = callListenerFunction(jsEvent, event); + if (!tryCatch.CanContinue()) + return; + + // If an error occurs while handling the event, it should be reported. + if (tryCatch.HasCaught()) { + reportException(0, tryCatch); + tryCatch.Reset(); + } + + // Restore the old event. This must be done for all exit paths through this method. + tryCatch.SetVerbose(true); + if (savedEvent.IsEmpty()) + v8Context->Global()->SetHiddenValue(eventSymbol, v8::Undefined()); + else + v8Context->Global()->SetHiddenValue(eventSymbol, savedEvent); + tryCatch.Reset(); + } + + if (event->type() == "beforeunload" || event->type() == "unload") + DateExtension::get()->setAllowSleep(true); + + ASSERT(!V8Proxy::handleOutOfMemory() || returnValue.IsEmpty()); + + if (returnValue.IsEmpty()) + return; + + if (!returnValue->IsNull() && !returnValue->IsUndefined() && event->storesResultAsString()) + event->storeResult(toWebCoreString(returnValue)); + + // Prevent default action if the return value is false; + // FIXME: Add example, and reference to bug entry. + if (m_isAttribute && returnValue->IsBoolean() && !returnValue->BooleanValue()) + event->preventDefault(); +} + +v8::Local<v8::Object> V8AbstractEventListener::getReceiverObject(Event* event) { if (!m_listener.IsEmpty() && !m_listener->IsFunction()) return v8::Local<v8::Object>::New(m_listener); - if (isWindowEvent) - return v8::Context::GetCurrent()->Global(); - EventTarget* target = event->currentTarget(); v8::Handle<v8::Value> value = V8DOMWrapper::convertEventTargetToV8Object(target); if (value.IsEmpty()) diff --git a/WebCore/bindings/v8/V8AbstractEventListener.h b/WebCore/bindings/v8/V8AbstractEventListener.h index 1521941..c7736be 100644 --- a/WebCore/bindings/v8/V8AbstractEventListener.h +++ b/WebCore/bindings/v8/V8AbstractEventListener.h @@ -33,12 +33,39 @@ #include "EventListener.h" #include "OwnHandle.h" +#include "SharedPersistent.h" #include <v8.h> +#include <wtf/PassRefPtr.h> +#include <wtf/RefCounted.h> namespace WebCore { class Event; class Frame; + class V8Proxy; + + // Shared by listener objects and V8Proxy so that V8Proxy can + // silence listeners when needed. + class V8ListenerGuard : public RefCounted<V8ListenerGuard> { + public: + static PassRefPtr<V8ListenerGuard> create() + { + return adoptRef(new V8ListenerGuard); + } + + bool isDisconnected() const { return m_disconnected; } + + void disconnectListeners() + { + m_disconnected = true; + } + + private: + V8ListenerGuard() + : m_disconnected(false) { } + + bool m_disconnected; + }; // There are two kinds of event listeners: HTML or non-HMTL. onload, // onfocus, etc (attributes) are always HTML event handler type; Event @@ -50,54 +77,94 @@ namespace WebCore { // but ALLOWs duplicated non-HTML event handlers. class V8AbstractEventListener : public EventListener { public: - virtual ~V8AbstractEventListener() { } + virtual ~V8AbstractEventListener(); + + static const V8AbstractEventListener* cast(const EventListener* listener) + { + return listener->type() == JSEventListenerType + ? static_cast<const V8AbstractEventListener*>(listener) + : 0; + } + + static V8AbstractEventListener* cast(EventListener* listener) + { + return const_cast<V8AbstractEventListener*>(cast(const_cast<const EventListener*>(listener))); + } + + // Implementation of EventListener interface. + + virtual bool operator==(const EventListener& other) { return this == &other; } + + virtual void handleEvent(ScriptExecutionContext*, Event*); // Returns the owner frame of the listener. Frame* frame() { return m_frame; } - virtual void handleEvent(Event*, bool isWindowEvent); - void invokeEventHandler(v8::Handle<v8::Context>, Event*, v8::Handle<v8::Value> jsEvent, bool isWindowEvent); + virtual bool isLazy() const { return false; } // Returns the listener object, either a function or an object. - virtual v8::Local<v8::Object> getListenerObject() + v8::Local<v8::Object> getListenerObject() { + prepareListenerObject(); return v8::Local<v8::Object>::New(m_listener); } + v8::Local<v8::Object> getExistingListenerObject() + { + return v8::Local<v8::Object>::New(m_listener); + } + + bool hasExistingListenerObject() + { + return !m_listener.IsEmpty(); + } + // Dispose listener object and clear the handle. void disposeListenerObject(); - virtual bool disconnected() const { return !m_frame; } + // Detach the listener from its owner frame. + void disconnectFrame() { m_frame = 0; } - virtual bool isObjectListener() const { return false; } + virtual bool disconnected() const { return m_guard && m_guard->isDisconnected(); } protected: - v8::Persistent<v8::Object> m_listener; + V8AbstractEventListener(Frame*, PassRefPtr<V8ListenerGuard>, bool isAttribute); - // Indicates if this is an HTML type listener. - bool m_isAttribute; + virtual void prepareListenerObject() { } - private: - V8AbstractEventListener(Frame*, bool isInline); + void setListenerObject(v8::Handle<v8::Object> listener); - virtual v8::Local<v8::Value> callListenerFunction(v8::Handle<v8::Value> jsevent, Event*, bool isWindowEvent) = 0; + void invokeEventHandler(v8::Handle<v8::Context>, Event*, v8::Handle<v8::Value> jsEvent); // Get the receiver object to use for event listener call. - v8::Local<v8::Object> getReceiverObject(Event*, bool isWindowEvent); + v8::Local<v8::Object> getReceiverObject(Event*); + + int lineNumber() const { return m_lineNumber; } + + private: + // Implementation of EventListener function. + virtual bool virtualisAttribute() const { return m_isAttribute; } + + virtual v8::Local<v8::Value> callListenerFunction(v8::Handle<v8::Value> jsevent, Event*) = 0; + + v8::Persistent<v8::Object> m_listener; + + // Indicates if the above handle is weak. + bool m_isWeak; + + // Indicates if this is an HTML type listener. + bool m_isAttribute; // Frame to which the event listener is attached to. An event listener must be destroyed before its owner frame is // deleted. See fast/dom/replaceChild.html // FIXME: this could hold m_frame live until the event listener is deleted. Frame* m_frame; - OwnHandle<v8::Context> m_context; + RefPtr<SharedPersistent<v8::Context> > m_context; + RefPtr<V8ListenerGuard> m_guard; // Position in the HTML source for HTML event listeners. int m_lineNumber; int m_columnNumber; - - friend class V8EventListener; - friend class V8ObjectEventListener; - friend class V8LazyEventListener; }; } // namespace WebCore diff --git a/WebCore/bindings/v8/V8Binding.cpp b/WebCore/bindings/v8/V8Binding.cpp index 0da910b..9ac7eae 100644 --- a/WebCore/bindings/v8/V8Binding.cpp +++ b/WebCore/bindings/v8/V8Binding.cpp @@ -55,7 +55,8 @@ public: #ifndef NDEBUG m_threadId = WTF::currentThread(); #endif - v8::V8::AdjustAmountOfExternalAllocatedMemory(2 * length()); + ASSERT(!string.isNull()); + v8::V8::AdjustAmountOfExternalAllocatedMemory(2 * string.length()); } explicit WebCoreStringResource(const AtomicString& string) @@ -65,7 +66,8 @@ public: #ifndef NDEBUG m_threadId = WTF::currentThread(); #endif - v8::V8::AdjustAmountOfExternalAllocatedMemory(2 * length()); + ASSERT(!string.isNull()); + v8::V8::AdjustAmountOfExternalAllocatedMemory(2 * string.length()); } virtual ~WebCoreStringResource() @@ -73,18 +75,18 @@ public: #ifndef NDEBUG ASSERT(m_threadId == WTF::currentThread()); #endif - int reducedExternalMemory = -2 * length(); - if (!m_plainString.impl()->inTable()) + int reducedExternalMemory = -2 * m_plainString.length(); + if (m_plainString.impl() != m_atomicString.impl() && !m_atomicString.isNull()) reducedExternalMemory *= 2; v8::V8::AdjustAmountOfExternalAllocatedMemory(reducedExternalMemory); } - const uint16_t* data() const + virtual const uint16_t* data() const { - return reinterpret_cast<const uint16_t*>(m_plainString.characters()); + return reinterpret_cast<const uint16_t*>(m_plainString.impl()->characters()); } - size_t length() const { return m_plainString.length(); } + virtual size_t length() const { return m_plainString.impl()->length(); } String webcoreString() { return m_plainString; } @@ -95,12 +97,17 @@ public: #endif if (m_atomicString.isNull()) { m_atomicString = AtomicString(m_plainString); - if (!m_plainString.impl()->inTable()) - v8::V8::AdjustAmountOfExternalAllocatedMemory(2 * length()); + ASSERT(!m_atomicString.isNull()); + if (m_plainString.impl() != m_atomicString.impl()) + v8::V8::AdjustAmountOfExternalAllocatedMemory(2 * m_atomicString.length()); } return m_atomicString; } + // Returns right string type based on a dummy parameter. + String string(String) { return webcoreString(); } + AtomicString string(AtomicString) { return atomicString(); } + static WebCoreStringResource* toStringResource(v8::Handle<v8::String> v8String) { return static_cast<WebCoreStringResource*>(v8String->GetExternalStringResource()); @@ -121,12 +128,17 @@ private: #endif }; -String v8StringToWebCoreString(v8::Handle<v8::String> v8String, ExternalMode external, - StringType type) +enum ExternalMode { + Externalize, + DoNotExternalize +}; + +template <typename StringType> +static StringType v8StringToWebCoreString(v8::Handle<v8::String> v8String, ExternalMode external) { WebCoreStringResource* stringResource = WebCoreStringResource::toStringResource(v8String); if (stringResource) - return stringResource->webcoreString(); + return stringResource->string(StringType()); int length = v8String->Length(); if (!length) { @@ -134,44 +146,42 @@ String v8StringToWebCoreString(v8::Handle<v8::String> v8String, ExternalMode ext return StringImpl::empty(); } - UChar* buffer; - String result = String::createUninitialized(length, buffer); - v8String->Write(reinterpret_cast<uint16_t*>(buffer), 0, length); - - if (type == AtomicStringType) - result = AtomicString(result); + StringType result; + static const int inlineBufferSize = 16; + if (length <= inlineBufferSize) { + UChar inlineBuffer[inlineBufferSize]; + v8String->Write(reinterpret_cast<uint16_t*>(inlineBuffer), 0, length); + result = StringType(inlineBuffer, length); + } else { + UChar* buffer; + String tmp = String::createUninitialized(length, buffer); + v8String->Write(reinterpret_cast<uint16_t*>(buffer), 0, length); + result = StringType(tmp); + } - if (external == Externalize) { - WebCoreStringResource* resource = new WebCoreStringResource(result); - if (!v8String->MakeExternal(resource)) { + if (external == Externalize && v8String->CanMakeExternal()) { + stringResource = new WebCoreStringResource(result); + if (!v8String->MakeExternal(stringResource)) { // In case of a failure delete the external resource as it was not used. - delete resource; + delete stringResource; } } return result; } -AtomicString v8StringToAtomicWebCoreString(v8::Handle<v8::String> v8String) +String v8StringToWebCoreString(v8::Handle<v8::String> v8String) { - WebCoreStringResource* stringResource = WebCoreStringResource::toStringResource(v8String); - if (!stringResource) { - // If this string hasn't been externalized, we force it now. - String plain = v8StringToWebCoreString(v8String, Externalize, AtomicStringType); - // If the string is empty there's no room to cache an atomic - // string so we bail out. - if (plain.isEmpty()) - return plain; - stringResource = WebCoreStringResource::toStringResource(v8String); - ASSERT(stringResource != NULL); - } - return stringResource->atomicString(); + return v8StringToWebCoreString<String>(v8String, Externalize); } -String v8ValueToWebCoreString(v8::Handle<v8::Value> object) +AtomicString v8StringToAtomicWebCoreString(v8::Handle<v8::String> v8String) { - if (object->IsString()) - return v8StringToWebCoreString(v8::Handle<v8::String>::Cast(object), Externalize, PlainStringType); + return v8StringToWebCoreString<AtomicString>(v8String, Externalize); +} +String v8NonStringValueToWebCoreString(v8::Handle<v8::Value> object) +{ + ASSERT(!object->IsString()); if (object->IsInt32()) { int value = object->Int32Value(); // Most numbers used are <= 100. Even if they aren't used there's very little in using the space. @@ -197,20 +207,13 @@ String v8ValueToWebCoreString(v8::Handle<v8::Value> object) throwError(block.Exception()); return StringImpl::empty(); } - return v8StringToWebCoreString(v8String, DoNotExternalize, PlainStringType); -} - -AtomicString v8ValueToAtomicWebCoreString(v8::Handle<v8::Value> v8Value) -{ - if (v8Value->IsString()) - return v8StringToAtomicWebCoreString(v8::Handle<v8::String>::Cast(v8Value)); - String string = v8ValueToWebCoreString(v8Value); - return AtomicString(string); + return v8StringToWebCoreString<String>(v8String, DoNotExternalize); } -v8::Handle<v8::String> v8String(const String& string) +AtomicString v8NonStringValueToAtomicWebCoreString(v8::Handle<v8::Value> object) { - return v8ExternalString(string); + ASSERT(!object->IsString()); + return AtomicString(v8NonStringValueToWebCoreString(object)); } static bool stringImplCacheEnabled = false; @@ -261,7 +264,7 @@ v8::Local<v8::String> v8ExternalString(const String& string) StringCache& stringCache = getStringCache(); v8::String* cachedV8String = stringCache.get(stringImpl); if (cachedV8String) - return v8::Local<v8::String>(cachedV8String); + return v8::Local<v8::String>::New(v8::Handle<v8::String>(cachedV8String)); v8::Local<v8::String> newString = makeExternalString(string); if (newString.IsEmpty()) diff --git a/WebCore/bindings/v8/V8Binding.h b/WebCore/bindings/v8/V8Binding.h index 4f36f00..a09102d 100644 --- a/WebCore/bindings/v8/V8Binding.h +++ b/WebCore/bindings/v8/V8Binding.h @@ -31,6 +31,7 @@ #ifndef V8Binding_h #define V8Binding_h +#include "AtomicString.h" #include "MathExtras.h" #include "PlatformString.h" @@ -38,32 +39,31 @@ namespace WebCore { - enum ExternalMode { - Externalize, - DoNotExternalize - }; - - enum StringType { - PlainStringType, - AtomicStringType - }; - // Convert v8 types to a WebCore::String. If the V8 string is not already // an external string then it is transformed into an external string at this // point to avoid repeated conversions. - String v8StringToWebCoreString(v8::Handle<v8::String>, ExternalMode mode, StringType type); - String v8ValueToWebCoreString(v8::Handle<v8::Value>); + String v8StringToWebCoreString(v8::Handle<v8::String>); + String v8NonStringValueToWebCoreString(v8::Handle<v8::Value>); + inline String v8ValueToWebCoreString(v8::Handle<v8::Value> value) + { + if (value->IsString()) + return v8StringToWebCoreString(v8::Handle<v8::String>::Cast(value)); + return v8NonStringValueToWebCoreString(value); + } // Convert v8 types to a WebCore::AtomicString. AtomicString v8StringToAtomicWebCoreString(v8::Handle<v8::String>); - AtomicString v8ValueToAtomicWebCoreString(v8::Handle<v8::Value>); - - // Convert a string to a V8 string. - v8::Handle<v8::String> v8String(const String&); + AtomicString v8NonStringValueToAtomicWebCoreString(v8::Handle<v8::Value>); + inline AtomicString v8ValueToAtomicWebCoreString(v8::Handle<v8::Value> value) + { + if (value->IsString()) + return v8StringToAtomicWebCoreString(v8::Handle<v8::String>::Cast(value)); + return v8NonStringValueToAtomicWebCoreString(value); + } - inline String toString(const String& string)
- {
- return string;
+ inline String toString(const String& string) + { + return string; } // Return a V8 external string that shares the underlying buffer with the given @@ -71,6 +71,12 @@ namespace WebCore { // underlying buffer alive while the string is still live in the V8 engine. v8::Local<v8::String> v8ExternalString(const String&); + // Convert a string to a V8 string. + inline v8::Handle<v8::String> v8String(const String& string) + { + return v8ExternalString(string); + } + // Enables caching v8 wrappers created for WebCore::StringImpl. Currently this cache requires // all the calls (both to convert WebCore::String to v8::String and to GC the handle) // to be performed on the main thread. @@ -145,7 +151,7 @@ namespace WebCore { { return value ? v8::True() : v8::False(); } - + inline String toWebCoreStringWithNullCheck(v8::Handle<v8::Value> value) { if (value->IsNull()) @@ -153,6 +159,13 @@ namespace WebCore { return v8ValueToWebCoreString(value); } + inline AtomicString v8ValueToAtomicWebCoreStringWithNullCheck(v8::Handle<v8::Value> value) + { + if (value->IsNull()) + return AtomicString(); + return v8ValueToAtomicWebCoreString(value); + } + inline String toWebCoreStringWithNullOrUndefinedCheck(v8::Handle<v8::Value> value) { if (value->IsNull() || value->IsUndefined()) diff --git a/WebCore/bindings/v8/V8DOMWrapper.cpp b/WebCore/bindings/v8/V8DOMWrapper.cpp index 8a9741a..5a6b901 100644 --- a/WebCore/bindings/v8/V8DOMWrapper.cpp +++ b/WebCore/bindings/v8/V8DOMWrapper.cpp @@ -31,21 +31,32 @@ #include "config.h" #include "V8DOMWrapper.h" +#include "CSSMutableStyleDeclaration.h" #if PLATFORM(CHROMIUM) #include "ChromiumBridge.h" #endif -#include "CSSMutableStyleDeclaration.h" #include "DOMObjectsInclude.h" #include "DocumentLoader.h" #include "FrameLoaderClient.h" +#include "Notification.h" +#if PLATFORM(ANDROID) +// TODO: Upstream SVG guard. +#if ENABLE(SVG) +#include "SVGElementInstance.h" +#endif +#endif #include "ScriptController.h" +#include "V8AbstractEventListener.h" #include "V8Binding.h" #include "V8Collection.h" #include "V8CustomBinding.h" +#include "V8CustomEventListener.h" #include "V8DOMMap.h" #include "V8DOMWindow.h" +#include "V8EventListenerList.h" #include "V8Index.h" #include "V8IsolatedWorld.h" +#include "V8Proxy.h" #include "WorkerContextExecutionProxy.h" #include <algorithm> @@ -337,6 +348,7 @@ v8::Persistent<v8::FunctionTemplate> V8DOMWrapper::getTemplate(V8ClassIndex::V8W descriptor->PrototypeTemplate()->SetNamedPropertyHandler(USE_NAMED_PROPERTY_GETTER(DOMWindow)); descriptor->PrototypeTemplate()->SetIndexedPropertyHandler(USE_INDEXED_PROPERTY_GETTER(DOMWindow)); + descriptor->PrototypeTemplate()->SetInternalFieldCount(V8Custom::kDOMWindowInternalFieldCount); descriptor->SetHiddenPrototype(true); @@ -381,6 +393,24 @@ v8::Persistent<v8::FunctionTemplate> V8DOMWrapper::getTemplate(V8ClassIndex::V8W break; } +#if ENABLE(NOTIFICATIONS) + case V8ClassIndex::NOTIFICATION: { + // Reserve one more internal field for keeping event listeners. + v8::Local<v8::ObjectTemplate> instanceTemplate = descriptor->InstanceTemplate(); + instanceTemplate->SetInternalFieldCount(V8Custom::kNotificationInternalFieldCount); + break; + } +#endif // NOTIFICATIONS + +#if ENABLE(SVG) + case V8ClassIndex::SVGELEMENTINSTANCE: { + // Reserve one more internal field for keeping event listeners. + v8::Local<v8::ObjectTemplate> instanceTemplate = descriptor->InstanceTemplate(); + instanceTemplate->SetInternalFieldCount(V8Custom::kSVGElementInstanceInternalFieldCount); + break; + } +#endif + #if ENABLE(WORKERS) case V8ClassIndex::ABSTRACTWORKER: { // Reserve one more internal field for keeping event listeners. @@ -424,20 +454,42 @@ v8::Persistent<v8::FunctionTemplate> V8DOMWrapper::getTemplate(V8ClassIndex::V8W } #endif +#if ENABLE(3D_CANVAS) // The following objects are created from JavaScript. - case V8ClassIndex::DOMPARSER: - descriptor->SetCallHandler(USE_CALLBACK(DOMParserConstructor)); + case V8ClassIndex::CANVASARRAYBUFFER: + descriptor->SetCallHandler(USE_CALLBACK(CanvasArrayBufferConstructor)); break; -#if ENABLE(VIDEO) - case V8ClassIndex::HTMLAUDIOELEMENT: - descriptor->SetCallHandler(USE_CALLBACK(HTMLAudioElementConstructor)); + case V8ClassIndex::CANVASBYTEARRAY: + descriptor->SetCallHandler(USE_CALLBACK(CanvasByteArrayConstructor)); + descriptor->InstanceTemplate()->SetIndexedPropertyHandler(USE_INDEXED_PROPERTY_GETTER(CanvasByteArray), USE_INDEXED_PROPERTY_SETTER(CanvasByteArray)); break; -#endif - case V8ClassIndex::HTMLIMAGEELEMENT: - descriptor->SetCallHandler(USE_CALLBACK(HTMLImageElementConstructor)); + case V8ClassIndex::CANVASFLOATARRAY: + descriptor->SetCallHandler(USE_CALLBACK(CanvasFloatArrayConstructor)); + descriptor->InstanceTemplate()->SetIndexedPropertyHandler(USE_INDEXED_PROPERTY_GETTER(CanvasFloatArray), USE_INDEXED_PROPERTY_SETTER(CanvasFloatArray)); break; - case V8ClassIndex::HTMLOPTIONELEMENT: - descriptor->SetCallHandler(USE_CALLBACK(HTMLOptionElementConstructor)); + case V8ClassIndex::CANVASINTARRAY: + descriptor->SetCallHandler(USE_CALLBACK(CanvasIntArrayConstructor)); + descriptor->InstanceTemplate()->SetIndexedPropertyHandler(USE_INDEXED_PROPERTY_GETTER(CanvasIntArray), USE_INDEXED_PROPERTY_SETTER(CanvasIntArray)); + break; + case V8ClassIndex::CANVASSHORTARRAY: + descriptor->SetCallHandler(USE_CALLBACK(CanvasShortArrayConstructor)); + descriptor->InstanceTemplate()->SetIndexedPropertyHandler(USE_INDEXED_PROPERTY_GETTER(CanvasShortArray), USE_INDEXED_PROPERTY_SETTER(CanvasShortArray)); + break; + case V8ClassIndex::CANVASUNSIGNEDBYTEARRAY: + descriptor->SetCallHandler(USE_CALLBACK(CanvasUnsignedByteArrayConstructor)); + descriptor->InstanceTemplate()->SetIndexedPropertyHandler(USE_INDEXED_PROPERTY_GETTER(CanvasUnsignedByteArray), USE_INDEXED_PROPERTY_SETTER(CanvasUnsignedByteArray)); + break; + case V8ClassIndex::CANVASUNSIGNEDINTARRAY: + descriptor->SetCallHandler(USE_CALLBACK(CanvasUnsignedIntArrayConstructor)); + descriptor->InstanceTemplate()->SetIndexedPropertyHandler(USE_INDEXED_PROPERTY_GETTER(CanvasUnsignedIntArray), USE_INDEXED_PROPERTY_SETTER(CanvasUnsignedIntArray)); + break; + case V8ClassIndex::CANVASUNSIGNEDSHORTARRAY: + descriptor->SetCallHandler(USE_CALLBACK(CanvasUnsignedShortArrayConstructor)); + descriptor->InstanceTemplate()->SetIndexedPropertyHandler(USE_INDEXED_PROPERTY_GETTER(CanvasUnsignedShortArray), USE_INDEXED_PROPERTY_SETTER(CanvasUnsignedShortArray)); + break; +#endif + case V8ClassIndex::DOMPARSER: + descriptor->SetCallHandler(USE_CALLBACK(DOMParserConstructor)); break; case V8ClassIndex::WEBKITCSSMATRIX: descriptor->SetCallHandler(USE_CALLBACK(WebKitCSSMatrixConstructor)); @@ -445,6 +497,15 @@ v8::Persistent<v8::FunctionTemplate> V8DOMWrapper::getTemplate(V8ClassIndex::V8W case V8ClassIndex::WEBKITPOINT: descriptor->SetCallHandler(USE_CALLBACK(WebKitPointConstructor)); break; +#if ENABLE(WEB_SOCKETS) + case V8ClassIndex::WEBSOCKET: { + // Reserve one more internal field for keeping event listeners. + v8::Local<v8::ObjectTemplate> instanceTemplate = descriptor->InstanceTemplate(); + instanceTemplate->SetInternalFieldCount(V8Custom::kWebSocketInternalFieldCount); + descriptor->SetCallHandler(USE_CALLBACK(WebSocketConstructor)); + break; + } +#endif case V8ClassIndex::XMLSERIALIZER: descriptor->SetCallHandler(USE_CALLBACK(XMLSerializerConstructor)); break; @@ -482,6 +543,9 @@ v8::Persistent<v8::FunctionTemplate> V8DOMWrapper::getTemplate(V8ClassIndex::V8W case V8ClassIndex::CLIENTRECTLIST: descriptor->InstanceTemplate()->SetIndexedPropertyHandler(USE_INDEXED_PROPERTY_GETTER(ClientRectList)); break; + case V8ClassIndex::FILELIST: + descriptor->InstanceTemplate()->SetIndexedPropertyHandler(USE_INDEXED_PROPERTY_GETTER(FileList)); + break; #if ENABLE(DATAGRID) case V8ClassIndex::DATAGRIDCOLUMNLIST: descriptor->InstanceTemplate()->SetIndexedPropertyHandler(USE_INDEXED_PROPERTY_GETTER(DataGridColumnList)); @@ -534,6 +598,16 @@ v8::Local<v8::Function> V8DOMWrapper::getConstructor(V8ClassIndex::V8WrapperType if (!frame) return v8::Local<v8::Function>(); +#if ENABLE(WEB_SOCKETS) + // Make typeof(window.WebSocket) == 'undefined' when + // experimentalWebSocketEnabled is false. + if (type == V8ClassIndex::WEBSOCKET) { + Settings* settings = frame->settings(); + if (!settings || !settings->experimentalWebSocketsEnabled()) + return v8::Local<v8::Function>(); + } +#endif + v8::Handle<v8::Context> context = V8Proxy::context(frame); if (context.IsEmpty()) return v8::Local<v8::Function>(); @@ -562,6 +636,7 @@ v8::Handle<v8::Value> V8DOMWrapper::convertToV8Object(V8ClassIndex::V8WrapperTyp // These objects can be constructed under WorkerContextExecutionProxy. They need special // handling, since if we proceed below V8Proxy::retrieve() will get called and will crash. + // TODO(ukai): websocket? if ((type == V8ClassIndex::DOMCOREEXCEPTION || type == V8ClassIndex::RANGEEXCEPTION || type == V8ClassIndex::EVENTEXCEPTION @@ -982,7 +1057,6 @@ V8ClassIndex::V8WrapperType V8DOMWrapper::htmlElementType(HTMLElement* element) #if ENABLE(SVG_FONTS) #define FOR_EACH_FONTS_TAG(macro) \ - macro(definition-src, DEFINITIONSRC) \ macro(font-face, FONTFACE) \ macro(font-face-format, FONTFACEFORMAT) \ macro(font-face-name, FONTFACENAME) \ @@ -1105,6 +1179,8 @@ v8::Handle<v8::Value> V8DOMWrapper::convertEventToV8Object(Event* event) type = V8ClassIndex::OVERFLOWEVENT; else if (event->isMessageEvent()) type = V8ClassIndex::MESSAGEEVENT; + else if (event->isPageTransitionEvent()) + type = V8ClassIndex::PAGETRANSITIONEVENT; else if (event->isProgressEvent()) { if (event->isXMLHttpRequestProgressEvent()) type = V8ClassIndex::XMLHTTPREQUESTPROGRESSEVENT; @@ -1155,30 +1231,30 @@ static const V8ClassIndex::V8WrapperType mapping[] = { V8ClassIndex::NODE, // XPATH_NAMESPACE_NODE }; -// Caller checks node is not null. -v8::Handle<v8::Value> V8DOMWrapper::convertNodeToV8Object(Node* node) +v8::Handle<v8::Value> V8DOMWrapper::convertDocumentToV8Object(Document* document) { - if (!node) - return v8::Null(); - // Find a proxy for this node. // // Note that if proxy is found, we might initialize the context which can // instantiate a document wrapper. Therefore, we get the proxy before // checking if the node already has a wrapper. - V8Proxy* proxy = 0; - Document* document = node->document(); - if (document) { - Frame* frame = document->frame(); - proxy = V8Proxy::retrieve(frame); - if (proxy) - proxy->initContextIfNeeded(); - } + V8Proxy* proxy = V8Proxy::retrieve(document->frame()); + if (proxy) + proxy->initContextIfNeeded(); DOMWrapperMap<Node>& domNodeMap = getDOMNodeMap(); - v8::Handle<v8::Object> wrapper = domNodeMap.get(node); - if (!wrapper.IsEmpty()) - return wrapper; + v8::Handle<v8::Object> wrapper = domNodeMap.get(document); + if (wrapper.IsEmpty()) + return convertNewNodeToV8Object(document, proxy, domNodeMap); + + return wrapper; +} + +// Caller checks node is not null. +v8::Handle<v8::Value> V8DOMWrapper::convertNewNodeToV8Object(Node* node, V8Proxy* proxy, DOMWrapperMap<Node>& domNodeMap) +{ + if (!proxy && node->document()) + proxy = V8Proxy::retrieve(node->document()->frame()); bool isDocument = false; // document type node has special handling V8ClassIndex::V8WrapperType type; @@ -1272,6 +1348,18 @@ v8::Handle<v8::Value> V8DOMWrapper::convertEventTargetToV8Object(EventTarget* ta return convertToV8Object(V8ClassIndex::WORKER, worker); #endif // WORKERS +#if ENABLE(NOTIFICATIONS) + Notification* notification = target->toNotification(); + if (notification) + return convertToV8Object(V8ClassIndex::NOTIFICATION, notification); +#endif + +#if ENABLE(WEB_SOCKETS) + WebSocket* webSocket = target->toWebSocket(); + if (webSocket) + return convertToV8Object(V8ClassIndex::WEBSOCKET, webSocket); +#endif + Node* node = target->toNode(); if (node) return convertNodeToV8Object(node); @@ -1322,6 +1410,102 @@ v8::Handle<v8::Value> V8DOMWrapper::convertEventListenerToV8Object(EventListener return v8listener->getListenerObject(); } +PassRefPtr<EventListener> V8DOMWrapper::getEventListener(Node* node, v8::Local<v8::Value> value, bool isAttribute, ListenerLookupType lookup) +{ + ScriptExecutionContext* context = node->scriptExecutionContext(); + if (!context) + return 0; + + V8Proxy* proxy = V8Proxy::retrieve(context); + // The document might be created using createDocument, which does + // not have a frame, use the active frame. + if (!proxy) + proxy = V8Proxy::retrieve(V8Proxy::retrieveFrameForEnteredContext()); + + if (proxy) + return (lookup == ListenerFindOnly) ? V8EventListenerList::findWrapper(value, isAttribute) : V8EventListenerList::findOrCreateWrapper<V8EventListener>(proxy->frame(), proxy->listenerGuard(), value, isAttribute); + + return 0; +} + +#if PLATFORM(ANDROID) +#if ENABLE(SVG) +PassRefPtr<EventListener> V8DOMWrapper::getEventListener(SVGElementInstance* element, v8::Local<v8::Value> value, bool isAttribute, ListenerLookupType lookup) +{ + return getEventListener(element->correspondingElement(), value, isAttribute, lookup); +} +#endif +#endif + +PassRefPtr<EventListener> V8DOMWrapper::getEventListener(AbstractWorker* worker, v8::Local<v8::Value> value, bool isAttribute, ListenerLookupType lookup) +{ + if (worker->scriptExecutionContext()->isWorkerContext()) { + WorkerContextExecutionProxy* workerContextProxy = WorkerContextExecutionProxy::retrieve(); + ASSERT(workerContextProxy); + return workerContextProxy->findOrCreateEventListener(value, isAttribute, lookup == ListenerFindOnly); + } + + V8Proxy* proxy = V8Proxy::retrieve(worker->scriptExecutionContext()); + if (proxy) + return (lookup == ListenerFindOnly) ? V8EventListenerList::findWrapper(value, isAttribute) : V8EventListenerList::findOrCreateWrapper<V8EventListener>(proxy->frame(), proxy->listenerGuard(), value, isAttribute); + + return 0; +} + +#if ENABLE(NOTIFICATIONS) +PassRefPtr<EventListener> V8DOMWrapper::getEventListener(Notification* notification, v8::Local<v8::Value> value, bool isAttribute, ListenerLookupType lookup) +{ + if (notification->scriptExecutionContext()->isWorkerContext()) { + WorkerContextExecutionProxy* workerContextProxy = WorkerContextExecutionProxy::retrieve(); + ASSERT(workerContextProxy); + return workerContextProxy->findOrCreateEventListener(value, isAttribute, lookup == ListenerFindOnly); + } + + V8Proxy* proxy = V8Proxy::retrieve(notification->scriptExecutionContext()); + if (proxy) + return (lookup == ListenerFindOnly) ? V8EventListenerList::findWrapper(value, isAttribute) : V8EventListenerList::findOrCreateWrapper<V8EventListener>(proxy->frame(), proxy->listenerGuard(), value, isAttribute); + + return 0; +} +#endif + +PassRefPtr<EventListener> V8DOMWrapper::getEventListener(WorkerContext* workerContext, v8::Local<v8::Value> value, bool isAttribute, ListenerLookupType lookup) +{ + WorkerContextExecutionProxy* workerContextProxy = workerContext->script()->proxy(); + if (workerContextProxy) + return workerContextProxy->findOrCreateEventListener(value, isAttribute, lookup == ListenerFindOnly); + + return 0; +} + +PassRefPtr<EventListener> V8DOMWrapper::getEventListener(XMLHttpRequestUpload* upload, v8::Local<v8::Value> value, bool isAttribute, ListenerLookupType lookup) +{ + return getEventListener(upload->associatedXMLHttpRequest(), value, isAttribute, lookup); +} + +PassRefPtr<EventListener> V8DOMWrapper::getEventListener(EventTarget* eventTarget, v8::Local<v8::Value> value, bool isAttribute, ListenerLookupType lookup) +{ + V8Proxy* proxy = V8Proxy::retrieve(eventTarget->scriptExecutionContext()); + if (proxy) + return (lookup == ListenerFindOnly) ? V8EventListenerList::findWrapper(value, isAttribute) : V8EventListenerList::findOrCreateWrapper<V8EventListener>(proxy->frame(), proxy->listenerGuard(), value, isAttribute); + +#if ENABLE(WORKERS) + WorkerContextExecutionProxy* workerContextProxy = WorkerContextExecutionProxy::retrieve(); + if (workerContextProxy) + return workerContextProxy->findOrCreateEventListener(value, isAttribute, lookup == ListenerFindOnly); +#endif + + return 0; +} + +PassRefPtr<EventListener> V8DOMWrapper::getEventListener(V8Proxy* proxy, v8::Local<v8::Value> value, bool isAttribute, ListenerLookupType lookup) +{ + if (proxy) + return (lookup == ListenerFindOnly) ? V8EventListenerList::findWrapper(value, isAttribute) : V8EventListenerList::findOrCreateWrapper<V8EventListener>(proxy->frame(), proxy->listenerGuard(), value, isAttribute); + + return 0; +} + v8::Handle<v8::Value> V8DOMWrapper::convertDOMImplementationToV8Object(DOMImplementation* impl) { v8::Handle<v8::Object> result = instantiateV8Object(V8ClassIndex::DOMIMPLEMENTATION, V8ClassIndex::DOMIMPLEMENTATION, impl); diff --git a/WebCore/bindings/v8/V8DOMWrapper.h b/WebCore/bindings/v8/V8DOMWrapper.h index 57b3a69..b5587d5 100644 --- a/WebCore/bindings/v8/V8DOMWrapper.h +++ b/WebCore/bindings/v8/V8DOMWrapper.h @@ -31,13 +31,18 @@ #ifndef V8DOMWrapper_h #define V8DOMWrapper_h +#include "Document.h" #include "Event.h" #include "Node.h" #include "NodeFilter.h" #include "PlatformString.h" // for WebCore::String #include "V8CustomBinding.h" +#include "V8CustomXPathNSResolver.h" +#include "V8DOMMap.h" #include "V8Index.h" #include "V8Utilities.h" +#include "V8XPathNSResolver.h" +#include "XPathNSResolver.h" #include <v8.h> namespace WebCore { @@ -86,8 +91,16 @@ namespace WebCore { class StyleSheetList; class V8EventListener; class V8ObjectEventListener; +#if ENABLE(WEB_SOCKETS) + class WebSocket; +#endif class WorkerContext; + enum ListenerLookupType { + ListenerFindOnly, + ListenerFindOrCreate, + }; + class V8DOMWrapper { public: #ifndef NDEBUG @@ -134,7 +147,26 @@ namespace WebCore { return convertNodeToV8Object(node.get()); } - static v8::Handle<v8::Value> convertNodeToV8Object(Node*); + static v8::Handle<v8::Value> convertNodeToV8Object(Node* node) + { + if (!node) + return v8::Null(); + + Document* document = node->document(); + if (node == document) + return convertDocumentToV8Object(document); + + DOMWrapperMap<Node>& domNodeMap = getDOMNodeMap(); + v8::Handle<v8::Object> wrapper = domNodeMap.get(node); + if (wrapper.IsEmpty()) + return convertNewNodeToV8Object(node, 0, domNodeMap); + + return wrapper; + } + + static v8::Handle<v8::Value> convertDocumentToV8Object(Document*); + + static v8::Handle<v8::Value> convertNewNodeToV8Object(Node*, V8Proxy*, DOMWrapperMap<Node>&); template <class C> static C* convertToNativeObject(V8ClassIndex::V8WrapperType type, v8::Handle<v8::Object> object) @@ -184,13 +216,48 @@ namespace WebCore { static v8::Handle<v8::Value> convertEventTargetToV8Object(EventTarget*); // Wrap and unwrap JS event listeners. - static v8::Handle<v8::Value> convertEventListenerToV8Object(PassRefPtr<Event> eventListener) + static v8::Handle<v8::Value> convertEventListenerToV8Object(PassRefPtr<EventListener> eventListener) { return convertEventListenerToV8Object(eventListener.get()); } static v8::Handle<v8::Value> convertEventListenerToV8Object(EventListener*); + static PassRefPtr<EventListener> getEventListener(Node* node, v8::Local<v8::Value> value, bool isAttribute, ListenerLookupType lookup); + + static PassRefPtr<EventListener> getEventListener(SVGElementInstance* element, v8::Local<v8::Value> value, bool isAttribute, ListenerLookupType lookup); + + static PassRefPtr<EventListener> getEventListener(AbstractWorker* worker, v8::Local<v8::Value> value, bool isAttribute, ListenerLookupType lookup); + +#if ENABLE(NOTIFICATIONS) + static PassRefPtr<EventListener> getEventListener(Notification* notification, v8::Local<v8::Value> value, bool isAttribute, ListenerLookupType lookup); +#endif + + static PassRefPtr<EventListener> getEventListener(WorkerContext* workerContext, v8::Local<v8::Value> value, bool isAttribute, ListenerLookupType lookup); + + static PassRefPtr<EventListener> getEventListener(XMLHttpRequestUpload* upload, v8::Local<v8::Value> value, bool isAttribute, ListenerLookupType lookup); + + static PassRefPtr<EventListener> getEventListener(EventTarget* eventTarget, v8::Local<v8::Value> value, bool isAttribute, ListenerLookupType lookup); + + static PassRefPtr<EventListener> getEventListener(V8Proxy* proxy, v8::Local<v8::Value> value, bool isAttribute, ListenerLookupType lookup); + + +#if PLATFORM(ANDROID) +// TODO: upstream XPATH guard. +#if ENABLE(XPATH) + // XPath-related utilities + static RefPtr<XPathNSResolver> getXPathNSResolver(v8::Handle<v8::Value> value) + { + RefPtr<XPathNSResolver> resolver; + if (V8XPathNSResolver::HasInstance(value)) + resolver = convertToNativeObject<XPathNSResolver>(V8ClassIndex::XPATHNSRESOLVER, v8::Handle<v8::Object>::Cast(value)); + else if (value->IsObject()) + resolver = V8CustomXPathNSResolver::create(value->ToObject()); + return resolver; + } +#endif +#endif + // DOMImplementation is a singleton and it is handled in a special // way. A wrapper is generated per document and stored in an // internal field of the document. diff --git a/WebCore/bindings/v8/V8EventListenerList.cpp b/WebCore/bindings/v8/V8EventListenerList.cpp index c9aaa12..e37d630 100644 --- a/WebCore/bindings/v8/V8EventListenerList.cpp +++ b/WebCore/bindings/v8/V8EventListenerList.cpp @@ -31,165 +31,6 @@ #include "config.h" #include "V8EventListenerList.h" -#include "V8CustomEventListener.h" - namespace WebCore { -V8EventListenerListIterator::V8EventListenerListIterator(V8EventListenerList* list) - : m_list(list) - , m_vectorIndex(0) - , m_iter(list->m_table.begin()) -{ -} - -V8EventListenerListIterator::V8EventListenerListIterator(V8EventListenerList* list, bool shouldSeekToEnd) - : m_list(list) - , m_vectorIndex(0) - , m_iter(list->m_table.begin()) -{ - if (shouldSeekToEnd) - seekToEnd(); -} - -V8EventListenerListIterator::~V8EventListenerListIterator() { } - -void V8EventListenerListIterator::operator++() -{ - if (m_iter != m_list->m_table.end()) { - Vector<V8EventListener*>* vector = m_iter->second; - if (m_vectorIndex + 1 < vector->size()) { - m_vectorIndex++; - return; - } - m_vectorIndex = 0; - ++m_iter; - } -} - -bool V8EventListenerListIterator::operator==(const V8EventListenerListIterator& other) -{ - return other.m_iter == m_iter && other.m_vectorIndex == m_vectorIndex && other.m_list == m_list; -} - -bool V8EventListenerListIterator::operator!=(const V8EventListenerListIterator& other) -{ - return !operator==(other); -} - -V8EventListener* V8EventListenerListIterator::operator*() -{ - if (m_iter != m_list->m_table.end()) { - Vector<V8EventListener*>* vector = m_iter->second; - if (m_vectorIndex < vector->size()) - return vector->at(m_vectorIndex); - } - return 0; -} - -void V8EventListenerListIterator::seekToEnd() -{ - m_iter = m_list->m_table.end(); - m_vectorIndex = 0; -} - - -V8EventListenerList::V8EventListenerList() -{ -} - -V8EventListenerList::~V8EventListenerList() -{ -} - -V8EventListenerListIterator V8EventListenerList::begin() -{ - return iterator(this); -} - -V8EventListenerListIterator V8EventListenerList::end() -{ - return iterator(this, true); -} - - -static int getKey(v8::Local<v8::Object> object) -{ - // 0 is a sentinel value for the HashMap key, so we map it to 1. - int hash = object->GetIdentityHash(); - if (!hash) - return 1; - return hash; -} - -void V8EventListenerList::add(V8EventListener* listener) -{ - ASSERT(v8::Context::InContext()); - v8::HandleScope handleScope; - - v8::Local<v8::Object> object = listener->getListenerObject(); - int key = getKey(object); - Vector<V8EventListener*>* vector = m_table.get(key); - if (!vector) { - vector = new Vector<V8EventListener*>(); - m_table.set(key, vector); - } - vector->append(listener); - m_reverseTable.set(listener, key); -} - -void V8EventListenerList::remove(V8EventListener* listener) -{ - if (m_reverseTable.contains(listener)) { - int key = m_reverseTable.get(listener); - Vector<V8EventListener*>* vector = m_table.get(key); - if (!vector) - return; - for (size_t j = 0; j < vector->size(); j++) { - if (vector->at(j) == listener) { - vector->remove(j); - if (!vector->size()) { - m_table.remove(key); - delete vector; - vector = 0; - } - m_reverseTable.remove(listener); - return; - } - } - } -} - -void V8EventListenerList::clear() -{ - m_table.clear(); - m_reverseTable.clear(); -} - -V8EventListener* V8EventListenerList::find(v8::Local<v8::Object> object, bool isAttribute) -{ - ASSERT(v8::Context::InContext()); - int key = getKey(object); - - Vector<V8EventListener*>* vector = m_table.get(key); - if (!vector) - return 0; - - for (size_t i = 0; i < vector->size(); i++) { - V8EventListener* element = vector->at(i); - if (isAttribute == element->isAttribute() && object == element->getListenerObject()) - return element; - } - return 0; -} - -PassRefPtr<V8EventListener> V8EventListenerList::findWrapper(v8::Local<v8::Value> object, bool isAttribute) -{ - ASSERT(v8::Context::InContext()); - if (!object->IsObject()) - return 0; - - // FIXME: Should this be v8::Local<v8::Object>::Cast instead? - return find(object->ToObject(), isAttribute); -} - } // namespace WebCore diff --git a/WebCore/bindings/v8/V8EventListenerList.h b/WebCore/bindings/v8/V8EventListenerList.h index 4255050..506e5dc 100644 --- a/WebCore/bindings/v8/V8EventListenerList.h +++ b/WebCore/bindings/v8/V8EventListenerList.h @@ -32,92 +32,74 @@ #define V8EventListenerList_h #include <v8.h> -#include <wtf/Vector.h> -#include <wtf/HashMap.h> #include "PassRefPtr.h" +#include "V8CustomEventListener.h" +#include "V8HiddenPropertyName.h" namespace WebCore { class Frame; class V8EventListener; - class V8EventListenerListIterator; - // This is a container for V8EventListener objects that uses the identity hash of the v8::Object to - // speed up lookups + // This is a container for V8EventListener objects that uses hidden properties of v8::Object to speed up lookups. class V8EventListenerList { public: - // Because v8::Object identity hashes are not guaranteed to be unique, we unfortunately can't just map - // an int to V8EventListener. Instead we define a HashMap of int to Vector of V8EventListener - // called a ListenerMultiMap. - typedef Vector<V8EventListener*>* Values; - struct ValuesTraits : HashTraits<Values> { - static const bool needsDestruction = true; - }; - typedef HashMap<int, Values, DefaultHash<int>::Hash, HashTraits<int>, ValuesTraits> ListenerMultiMap; - - V8EventListenerList(); - ~V8EventListenerList(); - - friend class V8EventListenerListIterator; - typedef V8EventListenerListIterator iterator; - - iterator begin(); - iterator end(); - - void add(V8EventListener*); - void remove(V8EventListener*); - V8EventListener* find(v8::Local<v8::Object>, bool isAttribute); - void clear(); - size_t size() { return m_table.size(); } - - PassRefPtr<V8EventListener> findWrapper(v8::Local<v8::Value>, bool isAttribute); - template<typename WrapperType> - PassRefPtr<V8EventListener> findOrCreateWrapper(Frame*, v8::Local<v8::Value>, bool isAttribute); + static PassRefPtr<V8EventListener> findWrapper(v8::Local<v8::Value> value, bool isAttribute) + { + ASSERT(v8::Context::InContext()); + if (!value->IsObject()) + return 0; - private: - ListenerMultiMap m_table; + v8::Handle<v8::String> wrapperProperty = getHiddenProperty(isAttribute); + return doFindWrapper(v8::Local<v8::Object>::Cast(value), wrapperProperty); + } - // we also keep a reverse mapping of V8EventListener to v8::Object identity hash, - // in order to speed up removal by V8EventListener - HashMap<V8EventListener*, int> m_reverseTable; - }; + template<typename WrapperType, typename ContextType> + static PassRefPtr<V8EventListener> findOrCreateWrapper(ContextType*, PassRefPtr<V8ListenerGuard>, v8::Local<v8::Value>, bool isAttribute); + + static void clearWrapper(v8::Handle<v8::Object> listenerObject, bool isAttribute) + { + v8::Handle<v8::String> wrapperProperty = getHiddenProperty(isAttribute); + listenerObject->DeleteHiddenValue(wrapperProperty); + } - class V8EventListenerListIterator { - public: - ~V8EventListenerListIterator(); - void operator++(); - bool operator==(const V8EventListenerListIterator&); - bool operator!=(const V8EventListenerListIterator&); - V8EventListener* operator*(); private: - friend class V8EventListenerList; - explicit V8EventListenerListIterator(V8EventListenerList*); - V8EventListenerListIterator(V8EventListenerList*, bool shouldSeekToEnd); - void seekToEnd(); - - V8EventListenerList* m_list; - V8EventListenerList::ListenerMultiMap::iterator m_iter; - size_t m_vectorIndex; + static V8EventListener* doFindWrapper(v8::Local<v8::Object> object, v8::Handle<v8::String> wrapperProperty) + { + ASSERT(v8::Context::InContext()); + v8::HandleScope scope; + v8::Local<v8::Value> listener = object->GetHiddenValue(wrapperProperty); + if (listener.IsEmpty()) + return 0; + return static_cast<V8EventListener*>(v8::External::Unwrap(listener)); + } + + static inline v8::Handle<v8::String> getHiddenProperty(bool isAttribute) + { + return isAttribute ? V8HiddenPropertyName::attributeListener() : V8HiddenPropertyName::listener(); + } }; - template<typename WrapperType> - PassRefPtr<V8EventListener> V8EventListenerList::findOrCreateWrapper(Frame* frame, v8::Local<v8::Value> object, bool isAttribute) + template<typename WrapperType, typename ContextType> + PassRefPtr<V8EventListener> V8EventListenerList::findOrCreateWrapper(ContextType* context, PassRefPtr<V8ListenerGuard> guard, v8::Local<v8::Value> value, bool isAttribute) { ASSERT(v8::Context::InContext()); - if (!object->IsObject()) + if (!value->IsObject()) return 0; - // FIXME: Should this be v8::Local<v8::Object>::Cast instead? - V8EventListener* wrapper = find(object->ToObject(), isAttribute); + v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(value); + v8::Handle<v8::String> wrapperProperty = getHiddenProperty(isAttribute); + + V8EventListener* wrapper = doFindWrapper(object, wrapperProperty); if (wrapper) return wrapper; - // Create a new one, and add to cache. - RefPtr<WrapperType> newListener = WrapperType::create(frame, v8::Local<v8::Object>::Cast(object), isAttribute); - add(newListener.get()); + PassRefPtr<V8EventListener> wrapperPtr = WrapperType::create(context, guard, object, isAttribute); + if (wrapperPtr) + object->SetHiddenValue(wrapperProperty, v8::External::Wrap(wrapperPtr.get())); - return newListener; - }; + return wrapperPtr; + } } // namespace WebCore diff --git a/WebCore/bindings/v8/V8HiddenPropertyName.cpp b/WebCore/bindings/v8/V8HiddenPropertyName.cpp index 16ac232..d83573f 100644 --- a/WebCore/bindings/v8/V8HiddenPropertyName.cpp +++ b/WebCore/bindings/v8/V8HiddenPropertyName.cpp @@ -33,22 +33,21 @@ namespace WebCore { -v8::Handle<v8::String> V8HiddenPropertyName::objectPrototype() -{ - static v8::Persistent<v8::String>* string = createString("WebCore::V8HiddenPropertyName::objectPrototype"); - - return *string; +#define V8_AS_STRING(x) V8_AS_STRING_IMPL(x) +#define V8_AS_STRING_IMPL(x) #x + +#define V8_DEFINE_PROPERTY(name) \ +v8::Handle<v8::String> V8HiddenPropertyName::name() \ +{ \ + static v8::Persistent<v8::String>* string = createString("WebCore::V8HiddenPropertyName::" V8_AS_STRING(name)); \ + return *string; \ } -v8::Handle<v8::String> V8HiddenPropertyName::isolatedWorld() -{ - static v8::Persistent<v8::String>* string = createString("WebCore::V8HiddenPropertyName::isolatedWorld"); - - return *string; -} +V8_HIDDEN_PROPERTIES(V8_DEFINE_PROPERTY); v8::Persistent<v8::String>* V8HiddenPropertyName::createString(const char* key) { + v8::HandleScope scope; return new v8::Persistent<v8::String>(v8::Persistent<v8::String>::New(v8::String::NewSymbol(key))); } diff --git a/WebCore/bindings/v8/V8HiddenPropertyName.h b/WebCore/bindings/v8/V8HiddenPropertyName.h index 874b525..5ef89cb 100644 --- a/WebCore/bindings/v8/V8HiddenPropertyName.h +++ b/WebCore/bindings/v8/V8HiddenPropertyName.h @@ -35,10 +35,19 @@ namespace WebCore { +#define V8_HIDDEN_PROPERTIES(V) \ + V(objectPrototype) \ + V(isolatedWorld) \ + V(listener) \ + V(attributeListener) \ + V(sleepFunction) \ + V(toStringString) + class V8HiddenPropertyName { public: - static v8::Handle<v8::String> objectPrototype(); - static v8::Handle<v8::String> isolatedWorld(); +#define V8_DECLARE_PROPERTY(name) static v8::Handle<v8::String> name(); + V8_HIDDEN_PROPERTIES(V8_DECLARE_PROPERTY); +#undef V8_DECLARE_PROPERTY private: static v8::Persistent<v8::String>* createString(const char* key); diff --git a/WebCore/bindings/v8/V8Index.cpp b/WebCore/bindings/v8/V8Index.cpp index 9efbc3d..2cbd16d 100644 --- a/WebCore/bindings/v8/V8Index.cpp +++ b/WebCore/bindings/v8/V8Index.cpp @@ -33,6 +33,8 @@ #include "V8Attr.h" #include "V8BarInfo.h" +#include "V8BeforeLoadEvent.h" +#include "V8CanvasRenderingContext.h" #include "V8CanvasRenderingContext2D.h" #include "V8CanvasGradient.h" #include "V8CanvasPattern.h" @@ -107,6 +109,7 @@ #include "V8HTMLHtmlElement.h" #include "V8HTMLIFrameElement.h" #include "V8HTMLImageElement.h" +#include "V8HTMLImageElementConstructor.h" #include "V8HTMLInputElement.h" #include "V8HTMLIsIndexElement.h" #include "V8HTMLLabelElement.h" @@ -121,6 +124,7 @@ #include "V8HTMLOListElement.h" #include "V8HTMLOptGroupElement.h" #include "V8HTMLOptionElement.h" +#include "V8HTMLOptionElementConstructor.h" #include "V8HTMLParagraphElement.h" #include "V8HTMLParamElement.h" #include "V8HTMLPreElement.h" @@ -179,6 +183,7 @@ #include "V8Navigator.h" #include "V8MimeType.h" #include "V8MimeTypeArray.h" +#include "V8PageTransitionEvent.h" #include "V8Plugin.h" #include "V8PluginArray.h" #include "V8Range.h" @@ -195,24 +200,11 @@ #include "V8XMLHttpRequestUpload.h" #include "V8XMLSerializer.h" #include "V8RGBColor.h" -#include "V8VoidCallback.h" - -#if !PLATFORM(ANDROID) -#include "V8InspectorBackend.h" -#endif #if ENABLE(OFFLINE_WEB_APPLICATIONS) #include "V8DOMApplicationCache.h" #endif -#if ENABLE(DATABASE) -#include "V8Database.h" -#include "V8SQLError.h" -#include "V8SQLResultSet.h" -#include "V8SQLResultSetRowList.h" -#include "V8SQLTransaction.h" -#endif - #if ENABLE(DOM_STORAGE) #include "V8Storage.h" #include "V8StorageEvent.h" @@ -254,7 +246,6 @@ #endif #if ENABLE(SVG_FONTS) -#include "V8SVGDefinitionSrcElement.h" #include "V8SVGFontFaceElement.h" #include "V8SVGFontFaceFormatElement.h" #include "V8SVGFontFaceNameElement.h" @@ -370,6 +361,7 @@ #if ENABLE(VIDEO) #include "V8HTMLAudioElement.h" +#include "V8HTMLAudioElementConstructor.h" #include "V8HTMLMediaElement.h" #include "V8HTMLSourceElement.h" #include "V8HTMLVideoElement.h" @@ -377,6 +369,10 @@ #include "V8TimeRanges.h" #endif +#if ENABLE(WEB_SOCKETS) +#include "V8WebSocket.h" +#endif + #if ENABLE(WORKERS) #include "V8AbstractWorker.h" #include "V8DedicatedWorkerContext.h" @@ -386,35 +382,76 @@ #include "V8WorkerNavigator.h" #endif +#if ENABLE(NOTIFICATIONS) +#include "V8Notification.h" +#include "V8NotificationCenter.h" +#endif + #if ENABLE(SHARED_WORKERS) #include "V8SharedWorker.h" #endif -#if ENABLE(GEOLOCATION) -#include "V8Coordinates.h" -#include "V8Geolocation.h" -#include "V8Geoposition.h" -#include "V8PositionError.h" +#if ENABLE(3D_CANVAS) +#include "V8CanvasRenderingContext3D.h" +#include "V8CanvasArrayBuffer.h" +#include "V8CanvasArray.h" +#include "V8CanvasByteArray.h" +#include "V8CanvasBuffer.h" +#include "V8CanvasFloatArray.h" +#include "V8CanvasFramebuffer.h" +#include "V8CanvasIntArray.h" +#include "V8CanvasProgram.h" +#include "V8CanvasRenderbuffer.h" +#include "V8CanvasShader.h" +#include "V8CanvasShortArray.h" +#include "V8CanvasTexture.h" +#include "V8CanvasUnsignedByteArray.h" +#include "V8CanvasUnsignedIntArray.h" +#include "V8CanvasUnsignedShortArray.h" #endif -#if ENABLE(TOUCH_EVENTS) -#include "V8Touch.h" -#include "V8TouchList.h" -#include "V8TouchEvent.h" +#if ENABLE(DATABASE) +#include "V8Database.h" +#include "V8SQLError.h" +#include "V8SQLResultSet.h" +#include "V8SQLResultSetRowList.h" +#include "V8SQLTransaction.h" #endif #if ENABLE(XPATH) #include "V8XPathResult.h" #include "V8XPathException.h" #include "V8XPathExpression.h" -#include "V8XPathEvaluator.h" #include "V8XPathNSResolver.h" +#include "V8XPathEvaluator.h" #endif #if ENABLE(XSLT) #include "V8XSLTProcessor.h" #endif +#if ENABLE(INSPECTOR) +#include "V8InspectorBackend.h" +#endif + +#if PLATFORM(ANDROID) +// TODO: Upstream these guards to webkit.org +#if ENABLE(GEOLOCATION) +#include "V8Coordinates.h" +#include "V8Geolocation.h" +#include "V8Geoposition.h" +#include "V8PositionError.h" +#endif + +#if ENABLE(TOUCH_EVENTS) +#include "V8Touch.h" +#include "V8TouchList.h" +#include "V8TouchEvent.h" +#endif + +#include "V8VoidCallback.h" +#endif // PLATFORM(ANDROID) + namespace WebCore { FunctionTemplateFactory V8ClassIndex::GetFactory(V8WrapperType type) diff --git a/WebCore/bindings/v8/V8Index.h b/WebCore/bindings/v8/V8Index.h index d6db4c6..179b963 100644 --- a/WebCore/bindings/v8/V8Index.h +++ b/WebCore/bindings/v8/V8Index.h @@ -54,6 +54,7 @@ typedef v8::Persistent<v8::FunctionTemplate> (*FunctionTemplateFactory)(); #if ENABLE(VIDEO) #define VIDEO_HTMLELEMENT_TYPES(V) \ + V(AUDIO, HTMLAudioElementConstructor) \ V(HTMLAUDIOELEMENT, HTMLAudioElement) \ V(HTMLMEDIAELEMENT, HTMLMediaElement) \ V(HTMLSOURCEELEMENT, HTMLSourceElement) \ @@ -82,16 +83,26 @@ typedef v8::Persistent<v8::FunctionTemplate> (*FunctionTemplateFactory)(); #endif #if ENABLE(OFFLINE_WEB_APPLICATIONS) -#define APPLICATIONCACHE_NONNODE_WRAPPER_TYPES(V) \ +#define APPLICATIONCACHE_NONNODE_WRAPPER_TYPES(V) \ V(DOMAPPLICATIONCACHE, DOMApplicationCache) #else #define APPLICATIONCACHE_NONNODE_WRAPPER_TYPES(V) #endif +#if ENABLE(NOTIFICATIONS) +#define NOTIFICATIONS_NONNODE_WRAPPER_TYPES(V) \ + V(NOTIFICATION, Notification) \ + V(NOTIFICATIONCENTER, NotificationCenter) +#else +#define NOTIFICATIONS_NONNODE_WRAPPER_TYPES(V) +#endif + #if ENABLE(SHARED_WORKERS) #define SHARED_WORKER_ACTIVE_OBJECT_WRAPPER_TYPES(V) \ V(SHAREDWORKER, SharedWorker) -#define SHARED_WORKER_NONNODE_WRAPPER_TYPES(V) + +#define SHARED_WORKER_NONNODE_WRAPPER_TYPES(V) \ + V(SHAREDWORKERCONTEXT, SharedWorkerContext) #else #define SHARED_WORKER_ACTIVE_OBJECT_WRAPPER_TYPES(V) #define SHARED_WORKER_NONNODE_WRAPPER_TYPES(V) @@ -109,7 +120,9 @@ typedef v8::Persistent<v8::FunctionTemplate> (*FunctionTemplateFactory)(); V(ENTITY, Entity) \ V(ENTITYREFERENCE, EntityReference) \ V(HTMLDOCUMENT, HTMLDocument) \ + V(IMAGE, HTMLImageElementConstructor) \ V(NODE, Node) \ + V(OPTION, HTMLOptionElementConstructor) \ V(NOTATION, Notation) \ V(PROCESSINGINSTRUCTION, ProcessingInstruction) \ V(TEXT, Text) \ @@ -216,7 +229,6 @@ typedef v8::Persistent<v8::FunctionTemplate> (*FunctionTemplateFactory)(); #if ENABLE(SVG_FONTS) #define SVG_FONTS_ELEMENT_TYPES(V) \ - V(SVGDEFINITIONSRCELEMENT, SVGDefinitionSrcElement) \ V(SVGFONTFACEELEMENT, SVGFontFaceElement) \ V(SVGFONTFACEFORMATELEMENT, SVGFontFaceFormatElement) \ V(SVGFONTFACENAMEELEMENT, SVGFontFaceNameElement) \ @@ -289,13 +301,21 @@ typedef v8::Persistent<v8::FunctionTemplate> (*FunctionTemplateFactory)(); #endif // SVG +#if ENABLE(WEB_SOCKETS) +#define WEBSOCKET_ACTIVE_OBJECT_WRAPPER_TYPES(V) \ + V(WEBSOCKET, WebSocket) +#else +#define WEBSOCKET_ACTIVE_OBJECT_WRAPPER_TYPES(V) +#endif + // ACTIVE_DOM_OBJECT_TYPES are DOM_OBJECT_TYPES that need special treatement // during GC. #define ACTIVE_DOM_OBJECT_TYPES(V) \ V(MESSAGEPORT, MessagePort) \ V(XMLHTTPREQUEST, XMLHttpRequest) \ WORKER_ACTIVE_OBJECT_WRAPPER_TYPES(V) \ - SHARED_WORKER_ACTIVE_OBJECT_WRAPPER_TYPES(V) + SHARED_WORKER_ACTIVE_OBJECT_WRAPPER_TYPES(V) \ + WEBSOCKET_ACTIVE_OBJECT_WRAPPER_TYPES(V) // NOTE: DOM_OBJECT_TYPES is split into two halves because // Visual Studio's Intellinonsense crashes when macros get @@ -303,8 +323,10 @@ typedef v8::Persistent<v8::FunctionTemplate> (*FunctionTemplateFactory)(); // DOM_OBJECT_TYPES are non-node DOM types. #define DOM_OBJECT_TYPES_1(V) \ V(BARINFO, BarInfo) \ + V(BEFORELOADEVENT, BeforeLoadEvent) \ V(CANVASGRADIENT, CanvasGradient) \ V(CANVASPATTERN, CanvasPattern) \ + V(CANVASRENDERINGCONTEXT, CanvasRenderingContext) \ V(CANVASRENDERINGCONTEXT2D, CanvasRenderingContext2D) \ V(CLIENTRECT, ClientRect) \ V(CLIENTRECTLIST, ClientRectList) \ @@ -359,6 +381,7 @@ typedef v8::Persistent<v8::FunctionTemplate> (*FunctionTemplateFactory)(); V(NODEITERATOR, NodeIterator) \ V(NODELIST, NodeList) \ V(OVERFLOWEVENT, OverflowEvent) \ + V(PAGETRANSITIONEVENT, PageTransitionEvent) \ V(PLUGIN, Plugin) \ V(PLUGINARRAY, PluginArray) \ V(PROGRESSEVENT, ProgressEvent) \ @@ -374,7 +397,6 @@ typedef v8::Persistent<v8::FunctionTemplate> (*FunctionTemplateFactory)(); V(TREEWALKER, TreeWalker) \ V(UIEVENT, UIEvent) \ V(VALIDITYSTATE, ValidityState) \ - V(VOIDCALLBACK, VoidCallback) \ V(WEBKITANIMATIONEVENT, WebKitAnimationEvent) \ V(WEBKITCSSKEYFRAMERULE, WebKitCSSKeyframeRule) \ V(WEBKITCSSKEYFRAMESRULE, WebKitCSSKeyframesRule) \ @@ -391,27 +413,10 @@ typedef v8::Persistent<v8::FunctionTemplate> (*FunctionTemplateFactory)(); APPLICATIONCACHE_NONNODE_WRAPPER_TYPES(V) \ DATAGRID_NONNODE_TYPES(V) \ VIDEO_NONNODE_TYPES(V) \ + NOTIFICATIONS_NONNODE_WRAPPER_TYPES(V) \ SHARED_WORKER_NONNODE_WRAPPER_TYPES(V) \ WORKER_NONNODE_WRAPPER_TYPES(V) -#if ENABLE(XPATH) -#define DOM_OBJECT_XPATH_TYPES(V) \ - V(XPATHEVALUATOR, XPathEvaluator) \ - V(XPATHEXCEPTION, XPathException) \ - V(XPATHEXPRESSION, XPathExpression) \ - V(XPATHNSRESOLVER, XPathNSResolver) \ - V(XPATHRESULT, XPathResult) -#else -#define DOM_OBJECT_XPATH_TYPES(V) -#endif - -#if ENABLE(XSLT) -#define DOM_OBJECT_XSLT_TYPES(V) \ - V(XSLTPROCESSOR, XSLTProcessor) -#else -#define DOM_OBJECT_XSLT_TYPES(V) -#endif - #if ENABLE(DATABASE) #define DOM_OBJECT_DATABASE_TYPES(V) \ V(DATABASE, Database) \ @@ -438,6 +443,53 @@ typedef v8::Persistent<v8::FunctionTemplate> (*FunctionTemplateFactory)(); #define DOM_OBJECT_WORKERS_TYPES(V) #endif +#if ENABLE(3D_CANVAS) +#define DOM_OBJECT_3D_CANVAS_TYPES(V) \ + V(CANVASARRAY, CanvasArray) \ + V(CANVASARRAYBUFFER, CanvasArrayBuffer) \ + V(CANVASBUFFER, CanvasBuffer) \ + V(CANVASBYTEARRAY, CanvasByteArray) \ + V(CANVASFLOATARRAY, CanvasFloatArray) \ + V(CANVASFRAMEBUFFER, CanvasFramebuffer) \ + V(CANVASINTARRAY, CanvasIntArray) \ + V(CANVASPROGRAM, CanvasProgram) \ + V(CANVASRENDERBUFFER, CanvasRenderbuffer) \ + V(CANVASRENDERINGCONTEXT3D, CanvasRenderingContext3D) \ + V(CANVASSHADER, CanvasShader) \ + V(CANVASSHORTARRAY, CanvasShortArray) \ + V(CANVASTEXTURE, CanvasTexture) \ + V(CANVASUNSIGNEDBYTEARRAY, CanvasUnsignedByteArray) \ + V(CANVASUNSIGNEDINTARRAY, CanvasUnsignedIntArray) \ + V(CANVASUNSIGNEDSHORTARRAY, CanvasUnsignedShortArray) +#else +#define DOM_OBJECT_3D_CANVAS_TYPES(V) +#endif + +#if ENABLE(XPATH) +#define DOM_OBJECT_XPATH_TYPES(V) \ + V(XPATHEVALUATOR, XPathEvaluator) \ + V(XPATHEXCEPTION, XPathException) \ + V(XPATHEXPRESSION, XPathExpression) \ + V(XPATHNSRESOLVER, XPathNSResolver) \ + V(XPATHRESULT, XPathResult) +#else +#define DOM_OBJECT_XPATH_TYPES(V) +#endif + +#if ENABLE(XSLT) +#define DOM_OBJECT_XSLT_TYPES(V) \ + V(XSLTPROCESSOR, XSLTProcessor) +#else +#define DOM_OBJECT_XSLT_TYPES(V) +#endif + +#if ENABLE(INSPECTOR) +#define DOM_OBJECT_INSPECTOR_TYPES(V) \ + V(INSPECTORBACKEND, InspectorBackend) +#else +#define DOM_OBJECT_INSPECTOR_TYPES(V) +#endif + #if ENABLE(GEOLOCATION) #define DOM_OBJECT_GEOLOCATION_TYPES(V) \ V(COORDINATES, Coordinates) \ @@ -448,6 +500,8 @@ typedef v8::Persistent<v8::FunctionTemplate> (*FunctionTemplateFactory)(); #define DOM_OBJECT_GEOLOCATION_TYPES(V) #endif +#if PLATFORM(ANDROID) +// TODO: Upstream this guard. #if ENABLE(TOUCH_EVENTS) #define DOM_OBJECT_TOUCH_EVENT_TYPES(V) \ V(TOUCHLIST, TouchList) \ @@ -456,26 +510,30 @@ typedef v8::Persistent<v8::FunctionTemplate> (*FunctionTemplateFactory)(); #else #define DOM_OBJECT_TOUCH_EVENT_TYPES(V) #endif +#endif -#if PLATFORM(CHROMIUM) +#if PLATFORM(ANDROID) +#define DOM_OBJECT_VOIDCALLBACK_TYPES(V) \ + V(VOIDCALLBACK, VoidCallback) +#else +#define DOM_OBJECT_VOIDCALLBACK_TYPES(V) +#endif + +#if PLATFORM(ANDROID) +// This block is modified, but is not Android-specific. #define DOM_OBJECT_TYPES(V) \ DOM_OBJECT_TYPES_1(V) \ DOM_OBJECT_TYPES_2(V) \ DOM_OBJECT_DATABASE_TYPES(V) \ DOM_OBJECT_STORAGE_TYPES(V) \ DOM_OBJECT_WORKERS_TYPES(V) \ + DOM_OBJECT_3D_CANVAS_TYPES(V) \ DOM_OBJECT_XPATH_TYPES(V) \ DOM_OBJECT_XSLT_TYPES(V) \ - V(INSPECTORBACKEND, InspectorBackend) -#elif PLATFORM(ANDROID) -#define DOM_OBJECT_TYPES(V) \ - DOM_OBJECT_TYPES_1(V) \ - DOM_OBJECT_TYPES_2(V) \ - DOM_OBJECT_DATABASE_TYPES(V) \ DOM_OBJECT_GEOLOCATION_TYPES(V) \ - DOM_OBJECT_STORAGE_TYPES(V) \ DOM_OBJECT_TOUCH_EVENT_TYPES(V) \ - DOM_OBJECT_WORKERS_TYPES(V) + DOM_OBJECT_VOIDCALLBACK_TYPES(V) \ + DOM_OBJECT_INSPECTOR_TYPES(V) #endif #if ENABLE(SVG) diff --git a/WebCore/bindings/v8/V8IsolatedWorld.cpp b/WebCore/bindings/v8/V8IsolatedWorld.cpp index 1457545..d4e4f52 100644 --- a/WebCore/bindings/v8/V8IsolatedWorld.cpp +++ b/WebCore/bindings/v8/V8IsolatedWorld.cpp @@ -43,34 +43,29 @@ namespace WebCore { -static int isolatedWorldCount = 0; +int V8IsolatedWorld::isolatedWorldCount = 0; -static void contextWeakReferenceCallback(v8::Persistent<v8::Value> object, void* isolated_world) +void V8IsolatedWorld::contextWeakReferenceCallback(v8::Persistent<v8::Value> object, void* isolated_world) { // Our context is going away. Time to clean up the world. V8IsolatedWorld* world = static_cast<V8IsolatedWorld*>(isolated_world); delete world; } -void V8IsolatedWorld::evaluate(const Vector<ScriptSourceCode>& sources, V8Proxy* proxy, int extensionGroup) +V8IsolatedWorld::V8IsolatedWorld(V8Proxy* proxy, int extensionGroup) { + ++isolatedWorldCount; + v8::HandleScope scope; - v8::Persistent<v8::Context> context = proxy->createNewContext(v8::Handle<v8::Object>(), extensionGroup); + m_context = SharedPersistent<v8::Context>::create(proxy->createNewContext(v8::Handle<v8::Object>(), extensionGroup)); // Run code in the new context. - v8::Context::Scope context_scope(context); - - // The lifetime of this object is controlled by the V8 GC. - // We need to create the world before touching DOM wrappers. - V8IsolatedWorld* world = new V8IsolatedWorld(context); + v8::Context::Scope context_scope(m_context->get()); - V8Proxy::installHiddenObjectPrototype(context); - proxy->installDOMWindow(context, proxy->frame()->domWindow()); - - proxy->frame()->loader()->client()->didCreateIsolatedScriptContext(); + m_context->get()->Global()->SetHiddenValue(V8HiddenPropertyName::isolatedWorld(), v8::External::Wrap(this)); - for (size_t i = 0; i < sources.size(); ++i) - proxy->evaluate(sources[i], 0); + V8Proxy::installHiddenObjectPrototype(m_context->get()); + proxy->installDOMWindow(m_context->get(), proxy->frame()->domWindow()); // Using the default security token means that the canAccess is always // called, which is slow. @@ -78,40 +73,24 @@ void V8IsolatedWorld::evaluate(const Vector<ScriptSourceCode>& sources, V8Proxy* // created contexts so that they can all be updated when the // document domain // changes. - // FIXME: Move this statement above proxy->evaluate? It seems like we - // should set up the token before running the script. - context->UseDefaultSecurityToken(); + m_context->get()->UseDefaultSecurityToken(); - context.Dispose(); - // WARNING! This might well delete |world|. + proxy->frame()->loader()->client()->didCreateIsolatedScriptContext(); } -V8IsolatedWorld::V8IsolatedWorld(v8::Handle<v8::Context> context) - : m_context(v8::Persistent<v8::Context>::New(context)) +void V8IsolatedWorld::destroy() { - ++isolatedWorldCount; - m_context.MakeWeak(this, &contextWeakReferenceCallback); - m_context->Global()->SetHiddenValue(V8HiddenPropertyName::isolatedWorld(), v8::External::Wrap(this)); + m_context->get().MakeWeak(this, &contextWeakReferenceCallback); } V8IsolatedWorld::~V8IsolatedWorld() { --isolatedWorldCount; - m_context.Dispose(); - m_context.Clear(); + m_context->disposeHandle(); } -V8IsolatedWorld* V8IsolatedWorld::getEntered() +V8IsolatedWorld* V8IsolatedWorld::getEnteredImpl() { - if (isolatedWorldCount == 0) { - // This is a temporary performance optimization. Essentially, - // GetHiddenValue is too slow for this code path. We need to get the - // V8 team to add a real property to v8::Context for isolated worlds. - // Until then, we optimize the common case of not having any isolated - // worlds at all. - return 0; - } - if (!v8::Context::InContext()) return 0; v8::HandleScope scope; diff --git a/WebCore/bindings/v8/V8IsolatedWorld.h b/WebCore/bindings/v8/V8IsolatedWorld.h index 2036e65..15d8711 100644 --- a/WebCore/bindings/v8/V8IsolatedWorld.h +++ b/WebCore/bindings/v8/V8IsolatedWorld.h @@ -35,6 +35,7 @@ #include "V8DOMMap.h" #include "V8Index.h" +#include "V8Proxy.h" #include "V8Utilities.h" #include "ScriptSourceCode.h" // for WebCore::ScriptSourceCode @@ -57,11 +58,14 @@ namespace WebCore { // class V8IsolatedWorld { public: - ~V8IsolatedWorld(); + // Creates an isolated world. To destroy it, call destroy(). + // This will delete the isolated world when the context it owns is GC'd. + V8IsolatedWorld(V8Proxy* proxy, int extensionGroup); - // Evaluate JavaScript in a new isolated world. The script has access - // to the DOM of the document associated with |proxy|. - static void evaluate(const Vector<ScriptSourceCode>& sources, V8Proxy* proxy, int extensionGroup); + // Call this to destroy the isolated world. It will be deleted sometime + // after this call, once all script references to the world's context + // have been dropped. + void destroy(); // Returns the isolated world associated with // v8::Context::GetEntered(). Because worlds are isolated, the entire @@ -71,26 +75,42 @@ namespace WebCore { // FIXME: Consider edge cases with DOM mutation events that might // violate this invariant. // - static V8IsolatedWorld* getEntered(); + static V8IsolatedWorld* getEntered() + { + // This is a temporary performance optimization. Essentially, + // GetHiddenValue is too slow for this code path. We need to get the + // V8 team to add a real property to v8::Context for isolated worlds. + // Until then, we optimize the common case of not having any isolated + // worlds at all. + if (!isolatedWorldCount) + return 0; + return getEnteredImpl(); + } - v8::Handle<v8::Context> context() { return m_context; } + v8::Handle<v8::Context> context() { return m_context->get(); } + PassRefPtr<SharedPersistent<v8::Context> > shared_context() { return m_context; } DOMDataStore* getDOMDataStore() const { return m_domDataStore.getStore(); } private: - // The lifetime of an isolated world is managed by the V8 garbage - // collector. In particular, the object created by this constructor is - // freed when |context| is garbage collected. - explicit V8IsolatedWorld(v8::Handle<v8::Context> context); + ~V8IsolatedWorld(); + + static V8IsolatedWorld* getEnteredImpl(); + + // Called by the garbage collector when our JavaScript context is about + // to be destroyed. + static void contextWeakReferenceCallback(v8::Persistent<v8::Value> object, void* isolated_world); // The v8::Context for the isolated world. This object is keep on the // heap as long as |m_context| has not been garbage collected. - v8::Persistent<v8::Context> m_context; + RefPtr<SharedPersistent<v8::Context> > m_context; // The backing store for the isolated world's DOM wrappers. This class // doesn't have visibility into the wrappers. This handle simply helps // manage their lifetime. DOMDataStoreHandle m_domDataStore; + + static int isolatedWorldCount; }; } // namespace WebCore diff --git a/WebCore/bindings/v8/V8LazyEventListener.cpp b/WebCore/bindings/v8/V8LazyEventListener.cpp index 59fa7be..9b58571 100644 --- a/WebCore/bindings/v8/V8LazyEventListener.cpp +++ b/WebCore/bindings/v8/V8LazyEventListener.cpp @@ -33,199 +33,115 @@ #include "Frame.h" #include "V8Binding.h" +#include "V8HiddenPropertyName.h" #include "V8Proxy.h" +#include <wtf/StdLibExtras.h> + namespace WebCore { -V8LazyEventListener::V8LazyEventListener(Frame *frame, const String& code, const String& functionName, bool isSVGEvent) - : V8AbstractEventListener(frame, true) +V8LazyEventListener::V8LazyEventListener(Frame* frame, const String& code, const String& functionName, bool isSVGEvent) + : V8AbstractEventListener(frame, 0, true) , m_code(code) , m_functionName(functionName) , m_isSVGEvent(isSVGEvent) - , m_compiled(false) - , m_wrappedFunctionCompiled(false) -{ -} - -V8LazyEventListener::~V8LazyEventListener() { - disposeListenerObject(); - - // Dispose wrapped function - if (!m_wrappedFunction.IsEmpty()) { -#ifndef NDEBUG - V8GCController::unregisterGlobalHandle(this, m_wrappedFunction); -#endif - m_wrappedFunction.Dispose(); - m_wrappedFunction.Clear(); - } } -v8::Local<v8::Function> V8LazyEventListener::getListenerFunction() +v8::Local<v8::Value> V8LazyEventListener::callListenerFunction(v8::Handle<v8::Value> jsEvent, Event* event) { - if (m_compiled) { - ASSERT(m_listener.IsEmpty() || m_listener->IsFunction()); - return m_listener.IsEmpty() ? v8::Local<v8::Function>() : v8::Local<v8::Function>::New(v8::Persistent<v8::Function>::Cast(m_listener)); - } - - m_compiled = true; - - ASSERT(m_frame); - - { - // Switch to the context of m_frame. - v8::HandleScope handleScope; - - // Use the outer scope to hold context. - v8::Handle<v8::Context> v8Context = V8Proxy::mainWorldContext(m_frame); - // Bail out if we could not get the context. - if (v8Context.IsEmpty()) - return v8::Local<v8::Function>(); - - v8::Context::Scope scope(v8Context); - - // Wrap function around the event code. The parenthesis around the function are needed so that evaluating the code yields - // the function value. Without the parenthesis the function value is thrown away. - - // Make it an anonymous function to avoid name conflict for cases like - // <body onload='onload()'> - // <script> function onload() { alert('hi'); } </script>. - // Set function name to function object instead. - // See issue 944690. - // - // The ECMAScript spec says (very obliquely) that the parameter to an event handler is named "evt". - // - // Don't use new lines so that lines in the modified handler - // have the same numbers as in the original code. - String code = "(function (evt) {"; - code.append(m_code); - code.append("\n})"); - - v8::Handle<v8::String> codeExternalString = v8ExternalString(code); - v8::Handle<v8::Script> script = V8Proxy::compileScript(codeExternalString, m_frame->document()->url(), m_lineNumber - 1); - if (!script.IsEmpty()) { - V8Proxy* proxy = V8Proxy::retrieve(m_frame); - ASSERT(proxy); - v8::Local<v8::Value> value = proxy->runScript(script, false); - if (!value.IsEmpty()) { - ASSERT(value->IsFunction()); - v8::Local<v8::Function> listenerFunction = v8::Local<v8::Function>::Cast(value); - listenerFunction->SetName(v8::String::New(fromWebCoreString(m_functionName), m_functionName.length())); - - m_listener = v8::Persistent<v8::Function>::New(listenerFunction); -#ifndef NDEBUG - V8GCController::registerGlobalHandle(EVENT_LISTENER, this, m_listener); -#endif - } - } - } - - ASSERT(m_listener.IsEmpty() || m_listener->IsFunction()); - return m_listener.IsEmpty() ? v8::Local<v8::Function>() : v8::Local<v8::Function>::New(v8::Persistent<v8::Function>::Cast(m_listener)); -} - -v8::Local<v8::Value> V8LazyEventListener::callListenerFunction(v8::Handle<v8::Value> jsEvent, Event* event, bool isWindowEvent) -{ - v8::Local<v8::Function> handlerFunction = getWrappedListenerFunction(); - v8::Local<v8::Object> receiver = getReceiverObject(event, isWindowEvent); + v8::Local<v8::Function> handlerFunction = v8::Local<v8::Function>::Cast(getListenerObject()); + v8::Local<v8::Object> receiver = getReceiverObject(event); if (handlerFunction.IsEmpty() || receiver.IsEmpty()) return v8::Local<v8::Value>(); v8::Handle<v8::Value> parameters[1] = { jsEvent }; - V8Proxy* proxy = V8Proxy::retrieve(m_frame); + V8Proxy* proxy = V8Proxy::retrieve(frame()); return proxy->callFunction(handlerFunction, receiver, 1, parameters); } - static v8::Handle<v8::Value> V8LazyEventListenerToString(const v8::Arguments& args) { - return args.Callee()->GetHiddenValue(v8::String::New("toStringString")); + return args.Holder()->GetHiddenValue(V8HiddenPropertyName::toStringString()); } - -v8::Local<v8::Function> V8LazyEventListener::getWrappedListenerFunction() +void V8LazyEventListener::prepareListenerObject() { - if (m_wrappedFunctionCompiled) { - ASSERT(m_wrappedFunction.IsEmpty() || m_wrappedFunction->IsFunction()); - return m_wrappedFunction.IsEmpty() ? v8::Local<v8::Function>() : v8::Local<v8::Function>::New(m_wrappedFunction); - } - - m_wrappedFunctionCompiled = true; - - { - // Switch to the context of m_frame. - v8::HandleScope handleScope; - - // Use the outer scope to hold context. - v8::Handle<v8::Context> v8Context = V8Proxy::mainWorldContext(m_frame); - // Bail out if we cannot get the context. - if (v8Context.IsEmpty()) - return v8::Local<v8::Function>(); - - v8::Context::Scope scope(v8Context); - - // FIXME: cache the wrapper function. - - // Nodes other than the document object, when executing inline event handlers push document, form, and the target node on the scope chain. - // We do this by using 'with' statement. - // See chrome/fast/forms/form-action.html - // chrome/fast/forms/selected-index-value.html - // base/fast/overflow/onscroll-layer-self-destruct.html - // - // Don't use new lines so that lines in the modified handler - // have the same numbers as in the original code. - String code = "(function (evt) {" \ - "with (this.ownerDocument ? this.ownerDocument : {}) {" \ - "with (this.form ? this.form : {}) {" \ - "with (this) {" \ - "return (function(evt){"; - code.append(m_code); - // Insert '\n' otherwise //-style comments could break the handler. - code.append( "\n}).call(this, evt);}}}})"); - v8::Handle<v8::String> codeExternalString = v8ExternalString(code); - v8::Handle<v8::Script> script = V8Proxy::compileScript(codeExternalString, m_frame->document()->url(), m_lineNumber); - if (!script.IsEmpty()) { - V8Proxy* proxy = V8Proxy::retrieve(m_frame); - ASSERT(proxy); - v8::Local<v8::Value> value = proxy->runScript(script, false); - if (!value.IsEmpty()) { - ASSERT(value->IsFunction()); - - m_wrappedFunction = v8::Persistent<v8::Function>::New(v8::Local<v8::Function>::Cast(value)); - - // Change the toString function on the wrapper function to avoid it returning the source for the actual wrapper function. Instead - // it returns source for a clean wrapper function with the event argument wrapping the event source code. The reason for this - // is that some web sites uses toString on event functions and the evals the source returned (some times a RegExp is applied as - // well) for some other use. That fails miserably if the actual wrapper source is returned. - v8::Local<v8::FunctionTemplate> toStringTemplate = v8::FunctionTemplate::New(V8LazyEventListenerToString); - v8::Local<v8::Function> toStringFunction; - if (!toStringTemplate.IsEmpty()) - toStringFunction = toStringTemplate->GetFunction(); - if (!toStringFunction.IsEmpty()) { - String toStringResult = "function "; - toStringResult.append(m_functionName); - toStringResult.append("("); - if (m_isSVGEvent) - toStringResult.append("evt"); - else - toStringResult.append("event"); - toStringResult.append(") {\n "); - toStringResult.append(m_code); - toStringResult.append("\n}"); - toStringFunction->SetHiddenValue(v8::String::New("toStringString"), v8ExternalString(toStringResult)); - m_wrappedFunction->Set(v8::String::New("toString"), toStringFunction); - } - -#ifndef NDEBUG - V8GCController::registerGlobalHandle(EVENT_LISTENER, this, m_wrappedFunction); -#endif - m_wrappedFunction->SetName(v8::String::New(fromWebCoreString(m_functionName), m_functionName.length())); + if (hasExistingListenerObject()) + return; + + // Switch to the context of m_frame. + v8::HandleScope handleScope; + + // Use the outer scope to hold context. + v8::Handle<v8::Context> v8Context = V8Proxy::mainWorldContext(frame()); + // Bail out if we cannot get the context. + if (v8Context.IsEmpty()) + return; + + v8::Context::Scope scope(v8Context); + + // FIXME: cache the wrapper function. + + // Nodes other than the document object, when executing inline event handlers push document, form, and the target node on the scope chain. + // We do this by using 'with' statement. + // See chrome/fast/forms/form-action.html + // chrome/fast/forms/selected-index-value.html + // base/fast/overflow/onscroll-layer-self-destruct.html + // + // Don't use new lines so that lines in the modified handler + // have the same numbers as in the original code. + String code = "(function (evt) {" \ + "with (this.ownerDocument ? this.ownerDocument : {}) {" \ + "with (this.form ? this.form : {}) {" \ + "with (this) {" \ + "return (function(evt){"; + code.append(m_code); + // Insert '\n' otherwise //-style comments could break the handler. + code.append( "\n}).call(this, evt);}}}})"); + v8::Handle<v8::String> codeExternalString = v8ExternalString(code); + v8::Handle<v8::Script> script = V8Proxy::compileScript(codeExternalString, frame()->document()->url(), lineNumber()); + if (!script.IsEmpty()) { + V8Proxy* proxy = V8Proxy::retrieve(frame()); + ASSERT(proxy); + v8::Local<v8::Value> value = proxy->runScript(script, false); + if (!value.IsEmpty()) { + ASSERT(value->IsFunction()); + + v8::Local<v8::Function> wrappedFunction = v8::Local<v8::Function>::Cast(value); + + // Change the toString function on the wrapper function to avoid it + // returning the source for the actual wrapper function. Instead it + // returns source for a clean wrapper function with the event + // argument wrapping the event source code. The reason for this is + // that some web sites use toString on event functions and eval the + // source returned (sometimes a RegExp is applied as well) for some + // other use. That fails miserably if the actual wrapper source is + // returned. + DEFINE_STATIC_LOCAL(v8::Persistent<v8::FunctionTemplate>, toStringTemplate, ()); + if (toStringTemplate.IsEmpty()) + toStringTemplate = v8::Persistent<v8::FunctionTemplate>::New(v8::FunctionTemplate::New(V8LazyEventListenerToString)); + v8::Local<v8::Function> toStringFunction; + if (!toStringTemplate.IsEmpty()) + toStringFunction = toStringTemplate->GetFunction(); + if (!toStringFunction.IsEmpty()) { + String toStringResult = "function "; + toStringResult.append(m_functionName); + toStringResult.append("("); + toStringResult.append(m_isSVGEvent ? "evt" : "event"); + toStringResult.append(") {\n "); + toStringResult.append(m_code); + toStringResult.append("\n}"); + wrappedFunction->SetHiddenValue(V8HiddenPropertyName::toStringString(), v8ExternalString(toStringResult)); + wrappedFunction->Set(v8::String::New("toString"), toStringFunction); } + + wrappedFunction->SetName(v8::String::New(fromWebCoreString(m_functionName), m_functionName.length())); + + setListenerObject(wrappedFunction); } } - - return v8::Local<v8::Function>::New(m_wrappedFunction); } } // namespace WebCore diff --git a/WebCore/bindings/v8/V8LazyEventListener.h b/WebCore/bindings/v8/V8LazyEventListener.h index 62d9342..ba460e6 100644 --- a/WebCore/bindings/v8/V8LazyEventListener.h +++ b/WebCore/bindings/v8/V8LazyEventListener.h @@ -50,30 +50,19 @@ namespace WebCore { return adoptRef(new V8LazyEventListener(frame, code, functionName, isSVGEvent)); } - // For lazy event listener, the listener object is the same as its listener - // function without additional scope chains. - virtual v8::Local<v8::Object> getListenerObject() { return getWrappedListenerFunction(); } + virtual bool isLazy() const { return true; } + + protected: + virtual void prepareListenerObject(); private: V8LazyEventListener(Frame*, const String& code, const String& functionName, bool isSVGEvent); - virtual ~V8LazyEventListener(); - virtual bool virtualisAttribute() const { return true; } + virtual v8::Local<v8::Value> callListenerFunction(v8::Handle<v8::Value> jsEvent, Event*); String m_code; String m_functionName; bool m_isSVGEvent; - bool m_compiled; - - // If the event listener is on a non-document dom node, we compile the function with some implicit scope chains before it. - bool m_wrappedFunctionCompiled; - v8::Persistent<v8::Function> m_wrappedFunction; - - v8::Local<v8::Function> getWrappedListenerFunction(); - - virtual v8::Local<v8::Value> callListenerFunction(v8::Handle<v8::Value> jsEvent, Event*, bool isWindowEvent); - - v8::Local<v8::Function> getListenerFunction(); }; } // namespace WebCore diff --git a/WebCore/bindings/v8/V8ObjectEventListener.cpp b/WebCore/bindings/v8/V8ObjectEventListener.cpp deleted file mode 100644 index f10766c..0000000 --- a/WebCore/bindings/v8/V8ObjectEventListener.cpp +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright (C) 2006, 2007, 2008, 2009 Google Inc. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of Google Inc. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "config.h" -#include "V8ObjectEventListener.h" - -#include "Frame.h" -#include "V8Proxy.h" - -namespace WebCore { - -static void weakObjectEventListenerCallback(v8::Persistent<v8::Value>, void* parameter) -{ - V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(parameter); - - // Remove the wrapper - Frame* frame = listener->frame(); - if (frame) { - V8Proxy* proxy = V8Proxy::retrieve(frame); - if (proxy) - proxy->objectListeners()->remove(listener); - - // Because the listener is no longer in the list, it must be disconnected from the frame to avoid dangling frame pointer - // in the destructor. - listener->disconnectFrame(); - } - listener->disposeListenerObject(); -} - -// An object event listener wrapper only holds a weak reference to the -// JS function. A strong reference can create a cycle. -// -// The lifetime of these objects is bounded by the life time of the JS -// wrapper of XHR or Node. So we can create a hidden reference from -// the JS wrapper to to its JS function. -// -// (map) -// XHR or Node <---------- JS_wrapper -// | (hidden) : ^ -// V V : (may be reachable by closure) -// V8_listener --------> JS_function -// (weak) <-- may create a cycle if it is strong -// -// The persistent reference is made weak in the constructor of -// V8ObjectEventListener. - -V8ObjectEventListener::V8ObjectEventListener(Frame* frame, v8::Local<v8::Object> listener, bool isInline) - : V8EventListener(frame, listener, isInline) -{ - m_listener.MakeWeak(this, weakObjectEventListenerCallback); -} - -V8ObjectEventListener::~V8ObjectEventListener() -{ - if (m_frame) { - ASSERT(!m_listener.IsEmpty()); - V8Proxy* proxy = V8Proxy::retrieve(m_frame); - if (proxy) - proxy->objectListeners()->remove(this); - } - - disposeListenerObject(); -} - -} // namespace WebCore diff --git a/WebCore/bindings/v8/V8Proxy.cpp b/WebCore/bindings/v8/V8Proxy.cpp index 1cfafb8..dd3c218 100644 --- a/WebCore/bindings/v8/V8Proxy.cpp +++ b/WebCore/bindings/v8/V8Proxy.cpp @@ -32,11 +32,14 @@ #include "V8Proxy.h" #include "CSSMutableStyleDeclaration.h" -#include "CString.h" +#include "DateExtension.h" #include "DOMObjectsInclude.h" #include "DocumentLoader.h" #include "FrameLoaderClient.h" +#include "Page.h" +#include "PageGroup.h" #include "ScriptController.h" +#include "StorageNamespace.h" #include "V8Binding.h" #include "V8Collection.h" #include "V8ConsoleMessage.h" @@ -47,6 +50,8 @@ #include "V8Index.h" #include "V8IsolatedWorld.h" +#include <algorithm> +#include <utility> #include <v8.h> #include <v8-debug.h> #include <wtf/Assertions.h> @@ -62,12 +67,16 @@ #include "TimeCounter.h" #endif +#if PLATFORM(ANDROID) +#include "CString.h" +#endif + namespace WebCore { v8::Persistent<v8::Context> V8Proxy::m_utilityContext; // Static list of registered extensions -V8ExtensionList V8Proxy::m_extensions; +V8Extensions V8Proxy::m_extensions; const char* V8Proxy::kContextDebugDataType = "type"; const char* V8Proxy::kContextDebugDataValue = "value"; @@ -213,6 +222,14 @@ static void reportFatalErrorInV8(const char* location, const char* message) handleFatalErrorInV8(); } +V8Proxy::V8Proxy(Frame* frame) + : m_frame(frame), + m_context(SharedPersistent<v8::Context>::create()), + m_listenerGuard(V8ListenerGuard::create()), + m_inlineCode(false), + m_timerCallback(false), + m_recursion(0) { } + V8Proxy::~V8Proxy() { clearForClose(); @@ -230,22 +247,6 @@ void V8Proxy::destroyGlobal() } } -static void disconnectEventListenersInList(V8EventListenerList& list) -{ - V8EventListenerList::iterator it = list.begin(); - while (it != list.end()) { - (*it)->disconnectFrame(); - ++it; - } - list.clear(); -} - -void V8Proxy::disconnectEventListeners() -{ - disconnectEventListenersInList(m_eventListeners); - disconnectEventListenersInList(m_objectListeners); -} - v8::Handle<v8::Script> V8Proxy::compileScript(v8::Handle<v8::String> code, const String& fileName, int baseLine) #ifdef ANDROID_INSTRUMENT { @@ -296,10 +297,32 @@ bool V8Proxy::handleOutOfMemory() return true; } -void V8Proxy::evaluateInNewWorld(const Vector<ScriptSourceCode>& sources, int extensionGroup) +void V8Proxy::evaluateInIsolatedWorld(int worldID, const Vector<ScriptSourceCode>& sources, int extensionGroup) { initContextIfNeeded(); - V8IsolatedWorld::evaluate(sources, this, extensionGroup); + + v8::HandleScope handleScope; + V8IsolatedWorld* world = 0; + + if (worldID > 0) { + IsolatedWorldMap::iterator iter = m_isolatedWorlds.find(worldID); + if (iter != m_isolatedWorlds.end()) { + world = iter->second; + } else { + world = new V8IsolatedWorld(this, extensionGroup); + m_isolatedWorlds.set(worldID, world); + } + } else { + world = new V8IsolatedWorld(this, extensionGroup); + } + + v8::Local<v8::Context> context = v8::Local<v8::Context>::New(world->context()); + v8::Context::Scope context_scope(context); + for (size_t i = 0; i < sources.size(); ++i) + evaluate(sources[i], 0); + + if (worldID == 0) + world->destroy(); } void V8Proxy::evaluateInNewContext(const Vector<ScriptSourceCode>& sources, int extensionGroup) @@ -309,7 +332,7 @@ void V8Proxy::evaluateInNewContext(const Vector<ScriptSourceCode>& sources, int v8::HandleScope handleScope; // Set up the DOM window as the prototype of the new global object. - v8::Handle<v8::Context> windowContext = m_context; + v8::Handle<v8::Context> windowContext = context(); v8::Handle<v8::Object> windowGlobal = windowContext->Global(); v8::Handle<v8::Object> windowWrapper = V8DOMWrapper::lookupDOMWrapper(V8ClassIndex::DOMWINDOW, windowGlobal); @@ -357,29 +380,31 @@ v8::Local<v8::Value> V8Proxy::evaluate(const ScriptSourceCode& source, Node* nod ASSERT(v8::Context::InContext()); LOCK_V8; - // Compile the script. - v8::Local<v8::String> code = v8ExternalString(source.source()); -#if PLATFORM(CHROMIUM) - // TODO(andreip): ChromeBridge->BrowserBridge? - ChromiumBridge::traceEventBegin("v8.compile", node, ""); -#endif - - // NOTE: For compatibility with WebCore, ScriptSourceCode's line starts at - // 1, whereas v8 starts at 0. - v8::Handle<v8::Script> script = compileScript(code, source.url(), source.startLine() - 1); -#if PLATFORM(CHROMIUM) - // TODO(andreip): ChromeBridge->BrowserBridge? - ChromiumBridge::traceEventEnd("v8.compile", node, ""); - ChromiumBridge::traceEventBegin("v8.run", node, ""); -#endif v8::Local<v8::Value> result; { - // Isolate exceptions that occur when executing the code. These - // exceptions should not interfere with javascript code we might - // evaluate from C++ when returning from here. + // Isolate exceptions that occur when compiling and executing + // the code. These exceptions should not interfere with + // javascript code we might evaluate from C++ when returning + // from here. v8::TryCatch tryCatch; tryCatch.SetVerbose(true); + // Compile the script. + v8::Local<v8::String> code = v8ExternalString(source.source()); +#if PLATFORM(CHROMIUM) + // TODO(andreip): ChromeBridge->BrowserBridge? + ChromiumBridge::traceEventBegin("v8.compile", node, ""); +#endif + + // NOTE: For compatibility with WebCore, ScriptSourceCode's line starts at + // 1, whereas v8 starts at 0. + v8::Handle<v8::Script> script = compileScript(code, source.url(), source.startLine() - 1); +#if PLATFORM(CHROMIUM) + // TODO(andreip): ChromeBridge->BrowserBridge? + ChromiumBridge::traceEventEnd("v8.compile", node, ""); + + ChromiumBridge::traceEventBegin("v8.run", node, ""); +#endif // Set inlineCode to true for <a href="javascript:doSomething()"> // and false for <script>doSomething</script>. We make a rough guess at // this based on whether the script source has a URL. @@ -431,15 +456,18 @@ v8::Local<v8::Value> V8Proxy::runScriptInternal(v8::Handle<v8::Script> script, b v8::Local<v8::Value> result; { V8ConsoleMessage::Scope scope; - m_recursion++; // See comment in V8Proxy::callFunction. m_frame->keepAlive(); + m_recursion++; result = script->Run(); m_recursion--; } + // Release the storage mutex if applicable. + releaseStorageMutex(); + if (handleOutOfMemory()) ASSERT(result.IsEmpty()); @@ -461,14 +489,21 @@ v8::Local<v8::Value> V8Proxy::callFunction(v8::Handle<v8::Function> function, v8 #ifdef ANDROID_INSTRUMENT android::TimeCounter::start(android::TimeCounter::JavaScriptExecuteTimeCounter); #endif - - // For now, we don't put any artificial limitations on the depth - // of recursion that stems from calling functions. This is in - // contrast to the script evaluations. v8::Local<v8::Value> result; { V8ConsoleMessage::Scope scope; + if (m_recursion >= kMaxRecursionDepth) { + v8::Local<v8::String> code = v8::String::New("throw new RangeError('Maximum call stack size exceeded.')"); + if (code.IsEmpty()) + return result; + v8::Local<v8::Script> script = v8::Script::Compile(code); + if (script.IsEmpty()) + return result; + script->Run(); + return result; + } + // Evaluating the JavaScript could cause the frame to be deallocated, // so we start the keep alive timer here. // Frame::keepAlive method adds the ref count of the frame and sets a @@ -476,9 +511,14 @@ v8::Local<v8::Value> V8Proxy::callFunction(v8::Handle<v8::Function> function, v8 // execution finishs before firing the timer. m_frame->keepAlive(); + m_recursion++; result = function->Call(receiver, argc, args); + m_recursion--; } + // Release the storage mutex if applicable. + releaseStorageMutex(); + if (v8::V8::IsDead()) handleFatalErrorInV8(); @@ -508,17 +548,13 @@ v8::Local<v8::Value> V8Proxy::newInstance(v8::Handle<v8::Function> constructor, return result; } -v8::Local<v8::Object> V8Proxy::createWrapperFromCache(V8ClassIndex::V8WrapperType type) +v8::Local<v8::Object> V8Proxy::createWrapperFromCacheSlowCase(V8ClassIndex::V8WrapperType type) { - int classIndex = V8ClassIndex::ToInt(type); - v8::Local<v8::Object> clone(m_wrapperBoilerplates->CloneElementAt(classIndex)); - if (!clone.IsEmpty()) - return clone; - // Not in cache. + int classIndex = V8ClassIndex::ToInt(type); initContextIfNeeded(); - v8::Context::Scope scope(m_context); - v8::Local<v8::Function> function = V8DOMWrapper::getConstructor(type, getHiddenObjectPrototype(m_context)); + v8::Context::Scope scope(context()); + v8::Local<v8::Function> function = V8DOMWrapper::getConstructor(type, getHiddenObjectPrototype(context())); v8::Local<v8::Object> instance = SafeAllocation::newInstance(function); if (!instance.IsEmpty()) { m_wrapperBoilerplates->Set(v8::Integer::New(classIndex), instance); @@ -531,9 +567,9 @@ bool V8Proxy::isContextInitialized() { // m_context, m_global, and m_wrapperBoilerplates should // all be non-empty if if m_context is non-empty. - ASSERT(m_context.IsEmpty() || !m_global.IsEmpty()); - ASSERT(m_context.IsEmpty() || !m_wrapperBoilerplates.IsEmpty()); - return !m_context.IsEmpty(); + ASSERT(context().IsEmpty() || !m_global.IsEmpty()); + ASSERT(context().IsEmpty() || !m_wrapperBoilerplates.IsEmpty()); + return !context().IsEmpty(); } DOMWindow* V8Proxy::retrieveWindow(v8::Handle<v8::Context> context) @@ -547,7 +583,14 @@ DOMWindow* V8Proxy::retrieveWindow(v8::Handle<v8::Context> context) Frame* V8Proxy::retrieveFrame(v8::Handle<v8::Context> context) { - return retrieveWindow(context)->frame(); + DOMWindow* window = retrieveWindow(context); + Frame* frame = window->frame(); + if (frame && frame->domWindow() == window) + return frame; + // We return 0 here because |context| is detached from the Frame. If we + // did return |frame| we could get in trouble because the frame could be + // navigated to another security origin. + return 0; } Frame* V8Proxy::retrieveFrameForEnteredContext() @@ -701,21 +744,20 @@ void V8Proxy::updateDocumentWrapperCache() clearDocumentWrapperCache(); return; } - m_context->Global()->ForceSet(v8::String::New("document"), documentWrapper, static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete)); + context()->Global()->ForceSet(v8::String::New("document"), documentWrapper, static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete)); } void V8Proxy::clearDocumentWrapperCache() { - ASSERT(!m_context.IsEmpty()); - m_context->Global()->ForceDelete(v8::String::New("document")); + ASSERT(!context().IsEmpty()); + context()->Global()->ForceDelete(v8::String::New("document")); } void V8Proxy::disposeContextHandles() { - if (!m_context.IsEmpty()) { + if (!context().IsEmpty()) { m_frame->loader()->client()->didDestroyScriptContextForFrame(); - m_context.Dispose(); - m_context.Clear(); + shared_context()->disposeHandle(); } if (!m_wrapperBoilerplates.IsEmpty()) { @@ -727,9 +769,40 @@ void V8Proxy::disposeContextHandles() } } +void V8Proxy::releaseStorageMutex() +{ + // If we've just left a top level script context and local storage has been + // instantiated, we must ensure that any storage locks have been freed. + // Per http://dev.w3.org/html5/spec/Overview.html#storage-mutex + if (m_recursion != 0) + return; + Page* page = m_frame->page(); + if (!page) + return; + if (page->group().hasLocalStorage()) + page->group().localStorage()->unlock(); +} + +void V8Proxy::disconnectEventListeners() +{ + m_listenerGuard->disconnectListeners(); + m_listenerGuard = V8ListenerGuard::create(); +} + +void V8Proxy::resetIsolatedWorlds() +{ + for (IsolatedWorldMap::iterator iter = m_isolatedWorlds.begin(); + iter != m_isolatedWorlds.end(); ++iter) { + iter->second->destroy(); + } + m_isolatedWorlds.clear(); +} + void V8Proxy::clearForClose() { - if (!m_context.IsEmpty()) { + resetIsolatedWorlds(); + + if (!context().IsEmpty()) { LOCK_V8; v8::HandleScope handleScope; @@ -741,13 +814,14 @@ void V8Proxy::clearForClose() void V8Proxy::clearForNavigation() { disconnectEventListeners(); + resetIsolatedWorlds(); - if (!m_context.IsEmpty()) { + if (!context().IsEmpty()) { LOCK_V8; v8::HandleScope handle; clearDocumentWrapper(); - v8::Context::Scope contextScope(m_context); + v8::Context::Scope contextScope(context()); // Clear the document wrapper cache before turning on access checks on // the old DOMWindow wrapper. This way, access to the document wrapper @@ -760,7 +834,7 @@ void V8Proxy::clearForNavigation() wrapper->TurnOnAccessCheck(); // Separate the context from its global object. - m_context->DetachGlobal(); + context()->DetachGlobal(); disposeContextHandles(); } @@ -771,7 +845,7 @@ void V8Proxy::setSecurityToken() Document* document = m_frame->document(); // Setup security origin and security token. if (!document) { - m_context->UseDefaultSecurityToken(); + context()->UseDefaultSecurityToken(); return; } @@ -791,14 +865,14 @@ void V8Proxy::setSecurityToken() // case, we use the global object as the security token to avoid // calling canAccess when a script accesses its own objects. if (token.isEmpty() || token == "null") { - m_context->UseDefaultSecurityToken(); + context()->UseDefaultSecurityToken(); return; } CString utf8Token = token.utf8(); // NOTE: V8 does identity comparison in fast path, must use a symbol // as the security token. - m_context->SetSecurityToken(v8::String::NewSymbol(utf8Token.data(), utf8Token.length())); + context()->SetSecurityToken(v8::String::NewSymbol(utf8Token.data(), utf8Token.length())); } void V8Proxy::updateDocument() @@ -816,6 +890,10 @@ void V8Proxy::updateDocument() // global object wrapper succeed. initContextIfNeeded(); + // Bail out if context initialization failed. + if (context().IsEmpty()) + return; + // We have a new document and we need to update the cache. updateDocumentWrapperCache(); @@ -930,21 +1008,26 @@ v8::Persistent<v8::Context> V8Proxy::createNewContext(v8::Handle<v8::Object> glo // Install a security handler with V8. globalTemplate->SetAccessCheckCallbacks(V8Custom::v8DOMWindowNamedSecurityCheck, V8Custom::v8DOMWindowIndexedSecurityCheck, v8::Integer::New(V8ClassIndex::DOMWINDOW)); + globalTemplate->SetInternalFieldCount(V8Custom::kDOMWindowInternalFieldCount); + + // Used to avoid sleep calls in unload handlers. + if (!registeredExtensionWithV8(DateExtension::get())) + registerExtension(DateExtension::get(), String()); // Dynamically tell v8 about our extensions now. OwnArrayPtr<const char*> extensionNames(new const char*[m_extensions.size()]); int index = 0; - for (V8ExtensionList::iterator it = m_extensions.begin(); it != m_extensions.end(); ++it) { - if (it->group && it->group != extensionGroup) + for (size_t i = 0; i < m_extensions.size(); ++i) { + if (m_extensions[i].group && m_extensions[i].group != extensionGroup) continue; // Note: we check the loader URL here instead of the document URL // because we might be currently loading an URL into a blank page. // See http://code.google.com/p/chromium/issues/detail?id=10924 - if (it->scheme.length() > 0 && (it->scheme != m_frame->loader()->activeDocumentLoader()->url().protocol() || it->scheme != m_frame->page()->mainFrame()->loader()->activeDocumentLoader()->url().protocol())) + if (m_extensions[i].scheme.length() > 0 && (m_extensions[i].scheme != m_frame->loader()->activeDocumentLoader()->url().protocol() || m_extensions[i].scheme != m_frame->page()->mainFrame()->loader()->activeDocumentLoader()->url().protocol())) continue; - extensionNames[index++] = it->extension->name(); + extensionNames[index++] = m_extensions[i].extension->name(); } v8::ExtensionConfiguration extensions(index, extensionNames.get()); result = v8::Context::New(&extensions, globalTemplate, global); @@ -967,12 +1050,14 @@ bool V8Proxy::installDOMWindow(v8::Handle<v8::Context> context, DOMWindow* windo // Wrap the window. V8DOMWrapper::setDOMWrapper(jsWindow, V8ClassIndex::ToInt(V8ClassIndex::DOMWINDOW), window); + V8DOMWrapper::setDOMWrapper(v8::Handle<v8::Object>::Cast(jsWindow->GetPrototype()), V8ClassIndex::ToInt(V8ClassIndex::DOMWINDOW), window); window->ref(); V8DOMWrapper::setJSWrapperForDOMObject(window, v8::Persistent<v8::Object>::New(jsWindow)); // Insert the window instance as the prototype of the shadow object. v8::Handle<v8::Object> v8Global = context->Global(); + V8DOMWrapper::setDOMWrapper(v8::Handle<v8::Object>::Cast(v8Global->GetPrototype()), V8ClassIndex::ToInt(V8ClassIndex::DOMWINDOW), window); v8Global->Set(implicitProtoString, jsWindow); return true; } @@ -1015,7 +1100,7 @@ bool V8Proxy::installDOMWindow(v8::Handle<v8::Context> context, DOMWindow* windo void V8Proxy::initContextIfNeeded() { // Bail out if the context has already been initialized. - if (!m_context.IsEmpty()) + if (!context().IsEmpty()) return; #ifdef ANDROID_INSTRUMENT @@ -1044,17 +1129,18 @@ void V8Proxy::initContextIfNeeded() isV8Initialized = true; } - m_context = createNewContext(m_global, 0); - if (m_context.IsEmpty()) + + v8::Persistent<v8::Context> context = createNewContext(m_global, 0); + if (context.IsEmpty()) return; + m_context->set(context); + - // Starting from now, use local context only. - v8::Local<v8::Context> v8Context = context(); - v8::Context::Scope contextScope(v8Context); + v8::Context::Scope contextScope(context); // Store the first global object created so we can reuse it. if (m_global.IsEmpty()) { - m_global = v8::Persistent<v8::Object>::New(v8Context->Global()); + m_global = v8::Persistent<v8::Object>::New(context->Global()); // Bail out if allocation of the first global objects fails. if (m_global.IsEmpty()) { disposeContextHandles(); @@ -1065,7 +1151,7 @@ void V8Proxy::initContextIfNeeded() #endif } - installHiddenObjectPrototype(m_context); + installHiddenObjectPrototype(context); m_wrapperBoilerplates = v8::Persistent<v8::Array>::New(v8::Array::New(V8ClassIndex::WRAPPER_TYPE_COUNT)); // Bail out if allocation failed. if (m_wrapperBoilerplates.IsEmpty()) { @@ -1076,7 +1162,7 @@ void V8Proxy::initContextIfNeeded() V8GCController::registerGlobalHandle(PROXY, this, m_wrapperBoilerplates); #endif - if (!installDOMWindow(v8Context, m_frame->domWindow())) + if (!installDOMWindow(context, m_frame->domWindow())) disposeContextHandles(); updateDocument(); @@ -1162,6 +1248,23 @@ v8::Local<v8::Context> V8Proxy::context(Frame* frame) return context; } +PassRefPtr<SharedPersistent<v8::Context> > V8Proxy::shared_context(Frame* frame) +{ + V8Proxy *proxy = V8Proxy::retrieve(frame); + if (!proxy) + return 0; + + proxy->initContextIfNeeded(); + RefPtr<SharedPersistent<v8::Context> > context = proxy->shared_context(); + if (V8IsolatedWorld* world = V8IsolatedWorld::getEntered()) { + context = world->shared_context(); + if (frame != V8Proxy::retrieveFrame(context->get())) + return 0; + } + + return context; +} + v8::Local<v8::Context> V8Proxy::mainWorldContext(Frame* frame) { V8Proxy* proxy = retrieve(frame); @@ -1169,7 +1272,7 @@ v8::Local<v8::Context> V8Proxy::mainWorldContext(Frame* frame) return v8::Local<v8::Context>(); proxy->initContextIfNeeded(); - return proxy->context(); + return v8::Local<v8::Context>::New(proxy->context()); } v8::Local<v8::Context> V8Proxy::currentContext() @@ -1279,14 +1382,21 @@ String V8Proxy::sourceName() #endif } -void V8Proxy::registerExtensionWithV8(v8::Extension* extension) { +void V8Proxy::registerExtensionWithV8(v8::Extension* extension) +{ // If the extension exists in our list, it was already registered with V8. - for (V8ExtensionList::iterator it = m_extensions.begin(); it != m_extensions.end(); ++it) { - if (it->extension == extension) - return; + if (!registeredExtensionWithV8(extension)) + v8::RegisterExtension(extension); +} + +bool V8Proxy::registeredExtensionWithV8(v8::Extension* extension) +{ + for (size_t i = 0; i < m_extensions.size(); ++i) { + if (m_extensions[i].extension == extension) + return true; } - v8::RegisterExtension(extension); + return false; } void V8Proxy::registerExtension(v8::Extension* extension, const String& schemeRestriction) @@ -1306,17 +1416,17 @@ void V8Proxy::registerExtension(v8::Extension* extension, int extensionGroup) bool V8Proxy::setContextDebugId(int debugId) { ASSERT(debugId > 0); - if (m_context.IsEmpty()) + if (context().IsEmpty()) return false; v8::HandleScope scope; - if (!m_context->GetData()->IsUndefined()) + if (!context()->GetData()->IsUndefined()) return false; - v8::Context::Scope contextScope(m_context); + v8::Context::Scope contextScope(context()); v8::Handle<v8::Object> contextData = v8::Object::New(); contextData->Set(v8::String::New(kContextDebugDataType), v8::String::New("page")); contextData->Set(v8::String::New(kContextDebugDataValue), v8::Integer::New(debugId)); - m_context->SetData(contextData); + context()->SetData(contextData); return true; } diff --git a/WebCore/bindings/v8/V8Proxy.h b/WebCore/bindings/v8/V8Proxy.h index d8f546c..30f682d 100644 --- a/WebCore/bindings/v8/V8Proxy.h +++ b/WebCore/bindings/v8/V8Proxy.h @@ -31,20 +31,14 @@ #ifndef V8Proxy_h #define V8Proxy_h -#include "Node.h" -#include "NodeFilter.h" -#include "PlatformString.h" // for WebCore::String #include "ScriptSourceCode.h" // for WebCore::ScriptSourceCode #include "SecurityOrigin.h" // for WebCore::SecurityOrigin -#include "V8CustomBinding.h" -#include "V8DOMMap.h" +#include "SharedPersistent.h" +#include "V8AbstractEventListener.h" #include "V8DOMWrapper.h" -#include "V8EventListenerList.h" #include "V8GCController.h" #include "V8Index.h" -#include "V8Utilities.h" #include <v8.h> -#include <wtf/Assertions.h> #include <wtf/PassRefPtr.h> // so generated bindings don't have to #include <wtf/Vector.h> @@ -57,49 +51,14 @@ namespace WebCore { - class CSSRule; - class CSSRuleList; - class CSSStyleDeclaration; - class CSSValue; - class CSSValueList; - class ClientRectList; - class DOMImplementation; class DOMWindow; - class Document; - class Element; - class Event; - class EventListener; - class EventTarget; class Frame; - class HTMLCollection; - class HTMLDocument; - class HTMLElement; - class HTMLOptionsCollection; - class MediaList; - class MimeType; - class MimeTypeArray; - class NamedNodeMap; - class Navigator; class Node; - class NodeFilter; - class NodeList; - class Plugin; - class PluginArray; class SVGElement; -#if ENABLE(SVG) - class SVGElementInstance; -#endif - class Screen; class ScriptExecutionContext; -#if ENABLE(DOM_STORAGE) - class Storage; - class StorageEvent; -#endif class String; - class StyleSheet; - class StyleSheetList; class V8EventListener; - class V8ObjectEventListener; + class V8IsolatedWorld; // FIXME: use standard logging facilities in WebCore. void logInfo(Frame*, const String& message, const String& url); @@ -145,7 +104,7 @@ namespace WebCore { int group; v8::Extension* extension; }; - typedef WTF::Vector<V8ExtensionInfo> V8ExtensionList; + typedef WTF::Vector<V8ExtensionInfo> V8Extensions; class V8Proxy { public: @@ -158,7 +117,7 @@ namespace WebCore { GeneralError }; - explicit V8Proxy(Frame* frame) : m_frame(frame), m_inlineCode(false), m_timerCallback(false), m_recursion(0) { } + explicit V8Proxy(Frame*); ~V8Proxy(); @@ -196,9 +155,6 @@ namespace WebCore { bool isEnabled(); - V8EventListenerList* eventListeners() { return &m_eventListeners; } - V8EventListenerList* objectListeners() { return &m_objectListeners; } - #if ENABLE(SVG) static void setSVGContext(void*, SVGElement*); static SVGElement* svgContext(void*); @@ -211,7 +167,7 @@ namespace WebCore { // global scope, its own prototypes for intrinsic JavaScript objects (String, // Array, and so-on), and its own wrappers for all DOM nodes and DOM // constructors. - void evaluateInNewWorld(const Vector<ScriptSourceCode>& sources, int extensionGroup); + void evaluateInIsolatedWorld(int worldId, const Vector<ScriptSourceCode>& sources, int extensionGroup); // Evaluate JavaScript in a new context. The script gets its own global scope // and its own prototypes for intrinsic JavaScript objects (String, Array, @@ -239,7 +195,12 @@ namespace WebCore { // To create JS Wrapper objects, we create a cache of a 'boiler plate' // object, and then simply Clone that object each time we need a new one. // This is faster than going through the full object creation process. - v8::Local<v8::Object> createWrapperFromCache(V8ClassIndex::V8WrapperType); + v8::Local<v8::Object> createWrapperFromCache(V8ClassIndex::V8WrapperType type) + { + int classIndex = V8ClassIndex::ToInt(type); + v8::Local<v8::Object> clone(m_wrapperBoilerplates->CloneElementAt(classIndex)); + return clone.IsEmpty() ? createWrapperFromCacheSlowCase(type) : clone; + } // Returns the window object associated with a context. static DOMWindow* retrieveWindow(v8::Handle<v8::Context>); @@ -290,6 +251,7 @@ namespace WebCore { // Returns V8 Context of a frame. If none exists, creates // a new context. It is potentially slow and consumes memory. static v8::Local<v8::Context> context(Frame*); + static PassRefPtr<SharedPersistent<v8::Context> > shared_context(Frame*); static v8::Local<v8::Context> mainWorldContext(Frame*); static v8::Local<v8::Context> currentContext(); @@ -334,10 +296,19 @@ namespace WebCore { static int sourceLineNumber(); static String sourceName(); - // Returns a local handle of the context. - v8::Local<v8::Context> context() + v8::Handle<v8::Context> context() + { + return m_context->get(); + } + + PassRefPtr<SharedPersistent<v8::Context> > shared_context() + { + return m_context; + } + + PassRefPtr<V8ListenerGuard> listenerGuard() { - return v8::Local<v8::Context>::New(m_context); + return m_listenerGuard; } bool setContextDebugId(int id); @@ -368,7 +339,6 @@ namespace WebCore { static const char* kContextDebugDataType; static const char* kContextDebugDataValue; - void disconnectEventListeners(); void setSecurityToken(); void clearDocumentWrapper(); @@ -382,6 +352,14 @@ namespace WebCore { // Dispose global handles of m_contexts and friends. void disposeContextHandles(); + // If m_recursionCount is 0, let LocalStorage know so we can release + // the storage mutex. + void releaseStorageMutex(); + + void disconnectEventListeners(); + + void resetIsolatedWorlds(); + static bool canAccessPrivate(DOMWindow*); static const char* rangeExceptionName(int exceptionCode); @@ -407,11 +385,17 @@ namespace WebCore { return v8::Local<v8::Context>::New(m_utilityContext); } + v8::Local<v8::Object> createWrapperFromCacheSlowCase(V8ClassIndex::V8WrapperType); + static void registerExtensionWithV8(v8::Extension*); + static bool registeredExtensionWithV8(v8::Extension*); Frame* m_frame; - v8::Persistent<v8::Context> m_context; + RefPtr<SharedPersistent<v8::Context> > m_context; + + RefPtr<V8ListenerGuard> m_listenerGuard; + // For each possible type of wrapper, we keep a boilerplate object. // The boilerplate is used to create additional wrappers of the same // type. We keep a single persistent handle to an array of the @@ -426,14 +410,6 @@ namespace WebCore { int m_handlerLineNumber; - // A list of event listeners created for this frame, - // the list gets cleared when removing all timeouts. - V8EventListenerList m_eventListeners; - - // A list of event listeners create for XMLHttpRequest object for this frame, - // the list gets cleared when removing all timeouts. - V8EventListenerList m_objectListeners; - // True for <a href="javascript:foo()"> and false for <script>foo()</script>. // Only valid during execution. bool m_inlineCode; @@ -447,8 +423,18 @@ namespace WebCore { // excessive recursion in the binding layer. int m_recursion; - // List of extensions registered with the context. - static V8ExtensionList m_extensions; + // All of the extensions registered with the context. + static V8Extensions m_extensions; + + // The isolated worlds we are tracking for this frame. We hold them alive + // here so that they can be used again by future calls to + // evaluateInIsolatedWorld(). + // + // Note: although the pointer is raw, the instance is kept alive by a strong + // reference to the v8 context it contains, which is not made weak until we + // call world->destroy(). + typedef HashMap<int, V8IsolatedWorld*> IsolatedWorldMap; + IsolatedWorldMap m_isolatedWorlds; }; template <int tag, typename T> diff --git a/WebCore/bindings/v8/V8Utilities.cpp b/WebCore/bindings/v8/V8Utilities.cpp index c1ac6d4..a66f435 100644 --- a/WebCore/bindings/v8/V8Utilities.cpp +++ b/WebCore/bindings/v8/V8Utilities.cpp @@ -33,8 +33,15 @@ #include <v8.h> +#include "Document.h" +#include "Frame.h" +#include "ScriptExecutionContext.h" +#include "ScriptState.h" #include "V8CustomBinding.h" +#include "V8Binding.h" #include "V8Proxy.h" +#include "WorkerContext.h" +#include "WorkerContextExecutionProxy.h" #include <wtf/Assertions.h> #include "Frame.h" @@ -43,7 +50,7 @@ namespace WebCore { // Use an array to hold dependents. It works like a ref-counted scheme. // A value can be added more than once to the DOM object. -void createHiddenDependency(v8::Local<v8::Object> object, v8::Local<v8::Value> value, int cacheIndex) +void createHiddenDependency(v8::Handle<v8::Object> object, v8::Local<v8::Value> value, int cacheIndex) { v8::Local<v8::Value> cache = object->GetInternalField(cacheIndex); if (cache->IsNull() || cache->IsUndefined()) { @@ -55,7 +62,7 @@ void createHiddenDependency(v8::Local<v8::Object> object, v8::Local<v8::Value> v cacheArray->Set(v8::Integer::New(cacheArray->Length()), value); } -void removeHiddenDependency(v8::Local<v8::Object> object, v8::Local<v8::Value> value, int cacheIndex) +void removeHiddenDependency(v8::Handle<v8::Object> object, v8::Local<v8::Value> value, int cacheIndex) { v8::Local<v8::Value> cache = object->GetInternalField(cacheIndex); if (!cache->IsArray()) @@ -98,7 +105,46 @@ void navigateIfAllowed(Frame* frame, const KURL& url, bool lockHistory, bool loc return; if (!protocolIsJavaScript(url) || ScriptController::isSafeScript(frame)) - frame->loader()->scheduleLocationChange(url.string(), callingFrame->loader()->outgoingReferrer(), lockHistory, lockBackForwardList, processingUserGesture()); + frame->redirectScheduler()->scheduleLocationChange(url.string(), callingFrame->loader()->outgoingReferrer(), lockHistory, lockBackForwardList, processingUserGesture()); +} + +ScriptExecutionContext* getScriptExecutionContext(ScriptState* scriptState) +{ +#if ENABLE(WORKERS) + WorkerContextExecutionProxy* proxy = WorkerContextExecutionProxy::retrieve(); + if (proxy) + return proxy->workerContext()->scriptExecutionContext(); +#endif + + if (scriptState) + return scriptState->frame()->document()->scriptExecutionContext(); + else { + Frame* frame = V8Proxy::retrieveFrameForCurrentContext(); + if (frame) + return frame->document()->scriptExecutionContext(); + } + + return 0; +} + +void reportException(ScriptState* scriptState, v8::TryCatch& exceptionCatcher) +{ + String errorMessage; + int lineNumber = 0; + String sourceURL; + + // There can be a situation that an exception is thrown without setting a message. + v8::Local<v8::Message> message = exceptionCatcher.Message(); + if (message.IsEmpty()) + errorMessage = toWebCoreString(exceptionCatcher.Exception()->ToString()); + else { + errorMessage = toWebCoreString(message->Get()); + lineNumber = message->GetLineNumber(); + sourceURL = toWebCoreString(message->GetScriptResourceName()); + } + + getScriptExecutionContext(scriptState)->reportException(errorMessage, lineNumber, sourceURL); + exceptionCatcher.Reset(); } } // namespace WebCore diff --git a/WebCore/bindings/v8/V8Utilities.h b/WebCore/bindings/v8/V8Utilities.h index 3e59d34..1a713c9 100644 --- a/WebCore/bindings/v8/V8Utilities.h +++ b/WebCore/bindings/v8/V8Utilities.h @@ -44,17 +44,26 @@ namespace WebCore { class Frame; class KURL; + class ScriptExecutionContext; + class ScriptState; class String; // Use an array to hold dependents. It works like a ref-counted scheme. A value can be added more than once to the DOM object. - void createHiddenDependency(v8::Local<v8::Object>, v8::Local<v8::Value>, int cacheIndex); - void removeHiddenDependency(v8::Local<v8::Object>, v8::Local<v8::Value>, int cacheIndex); + void createHiddenDependency(v8::Handle<v8::Object>, v8::Local<v8::Value>, int cacheIndex); + void removeHiddenDependency(v8::Handle<v8::Object>, v8::Local<v8::Value>, int cacheIndex); bool processingUserGesture(); bool shouldAllowNavigation(Frame*); KURL completeURL(const String& relativeURL); void navigateIfAllowed(Frame*, const KURL&, bool lockHistory, bool lockBackForwardList); + ScriptExecutionContext* getScriptExecutionContext(ScriptState*); + inline ScriptExecutionContext* getScriptExecutionContext() { + return getScriptExecutionContext(0); + } + + void reportException(ScriptState*, v8::TryCatch&); + class AllowAllocation { public: inline AllowAllocation() diff --git a/WebCore/bindings/v8/V8WorkerContextEventListener.cpp b/WebCore/bindings/v8/V8WorkerContextEventListener.cpp index 862cd2d..24e493c 100644 --- a/WebCore/bindings/v8/V8WorkerContextEventListener.cpp +++ b/WebCore/bindings/v8/V8WorkerContextEventListener.cpp @@ -41,20 +41,13 @@ namespace WebCore { -V8WorkerContextEventListener::V8WorkerContextEventListener(WorkerContextExecutionProxy* proxy, v8::Local<v8::Object> listener, bool isInline) - : V8EventListener(0, listener, isInline) +V8WorkerContextEventListener::V8WorkerContextEventListener(WorkerContextExecutionProxy* proxy, PassRefPtr<V8ListenerGuard> guard, v8::Local<v8::Object> listener, bool isInline) + : V8EventListener(0, guard, listener, isInline) , m_proxy(proxy) { } -V8WorkerContextEventListener::~V8WorkerContextEventListener() -{ - if (m_proxy) - m_proxy->removeEventListener(this); - disposeListenerObject(); -} - -void V8WorkerContextEventListener::handleEvent(Event* event, bool isWindowEvent) +void V8WorkerContextEventListener::handleEvent(ScriptExecutionContext*, Event* event) { // Is the EventListener disconnected? if (disconnected()) @@ -77,7 +70,7 @@ void V8WorkerContextEventListener::handleEvent(Event* event, bool isWindowEvent) // Get the V8 wrapper for the event object. v8::Handle<v8::Value> jsEvent = WorkerContextExecutionProxy::convertEventToV8Object(event); - invokeEventHandler(context, event, jsEvent, isWindowEvent); + invokeEventHandler(context, event, jsEvent); } bool V8WorkerContextEventListener::reportError(const String& message, const String& url, int lineNumber) @@ -98,6 +91,7 @@ bool V8WorkerContextEventListener::reportError(const String& message, const Stri // Enter the V8 context in which to perform the event handling. v8::Context::Scope scope(context); + v8::Local<v8::Object> listener = getListenerObject(); v8::Local<v8::Value> returnValue; { // Catch exceptions thrown in calling the function so they do not propagate to javascript code that caused the event to fire. @@ -105,8 +99,8 @@ bool V8WorkerContextEventListener::reportError(const String& message, const Stri tryCatch.SetVerbose(true); // Call the function. - if (!m_listener.IsEmpty() && m_listener->IsFunction()) { - v8::Local<v8::Function> callFunction = v8::Local<v8::Function>::New(v8::Persistent<v8::Function>::Cast(m_listener)); + if (!listener.IsEmpty() && listener->IsFunction()) { + v8::Local<v8::Function> callFunction = v8::Local<v8::Function>::Cast(listener); v8::Local<v8::Object> thisValue = v8::Context::GetCurrent()->Global(); v8::Handle<v8::Value> parameters[3] = { v8String(message), v8String(url), v8::Integer::New(lineNumber) }; @@ -126,10 +120,10 @@ bool V8WorkerContextEventListener::reportError(const String& message, const Stri return errorHandled; } -v8::Local<v8::Value> V8WorkerContextEventListener::callListenerFunction(v8::Handle<v8::Value> jsEvent, Event* event, bool isWindowEvent) +v8::Local<v8::Value> V8WorkerContextEventListener::callListenerFunction(v8::Handle<v8::Value> jsEvent, Event* event) { v8::Local<v8::Function> handlerFunction = getListenerFunction(); - v8::Local<v8::Object> receiver = getReceiverObject(event, isWindowEvent); + v8::Local<v8::Object> receiver = getReceiverObject(event); if (handlerFunction.IsEmpty() || receiver.IsEmpty()) return v8::Local<v8::Value>(); @@ -141,13 +135,12 @@ v8::Local<v8::Value> V8WorkerContextEventListener::callListenerFunction(v8::Hand return result; } -v8::Local<v8::Object> V8WorkerContextEventListener::getReceiverObject(Event* event, bool isWindowEvent) +v8::Local<v8::Object> V8WorkerContextEventListener::getReceiverObject(Event* event) { - if (!m_listener.IsEmpty() && !m_listener->IsFunction()) - return v8::Local<v8::Object>::New(m_listener); + v8::Local<v8::Object> listener = getListenerObject(); - if (isWindowEvent) - return v8::Context::GetCurrent()->Global(); + if (!listener.IsEmpty() && !listener->IsFunction()) + return listener; EventTarget* target = event->currentTarget(); v8::Handle<v8::Value> value = WorkerContextExecutionProxy::convertEventTargetToV8Object(target); diff --git a/WebCore/bindings/v8/V8WorkerContextEventListener.h b/WebCore/bindings/v8/V8WorkerContextEventListener.h index c901c51..3752533 100644 --- a/WebCore/bindings/v8/V8WorkerContextEventListener.h +++ b/WebCore/bindings/v8/V8WorkerContextEventListener.h @@ -44,23 +44,21 @@ namespace WebCore { class V8WorkerContextEventListener : public V8EventListener { public: - static PassRefPtr<V8WorkerContextEventListener> create(WorkerContextExecutionProxy* proxy, v8::Local<v8::Object> listener, bool isInline) + static PassRefPtr<V8WorkerContextEventListener> create(WorkerContextExecutionProxy* proxy, PassRefPtr<V8ListenerGuard> guard, v8::Local<v8::Object> listener, bool isInline) { - return adoptRef(new V8WorkerContextEventListener(proxy, listener, isInline)); + return adoptRef(new V8WorkerContextEventListener(proxy, guard, listener, isInline)); } - V8WorkerContextEventListener(WorkerContextExecutionProxy*, v8::Local<v8::Object> listener, bool isInline); - virtual ~V8WorkerContextEventListener(); - virtual void handleEvent(Event*, bool isWindowEvent); + virtual void handleEvent(ScriptExecutionContext*, Event*); virtual bool reportError(const String& message, const String& url, int lineNumber); - virtual bool disconnected() const { return !m_proxy; } WorkerContextExecutionProxy* proxy() const { return m_proxy; } - void disconnect() { m_proxy = 0; } private: - virtual v8::Local<v8::Value> callListenerFunction(v8::Handle<v8::Value> jsEvent, Event*, bool isWindowEvent); - v8::Local<v8::Object> getReceiverObject(Event*, bool isWindowEvent); + V8WorkerContextEventListener(WorkerContextExecutionProxy*, PassRefPtr<V8ListenerGuard>, v8::Local<v8::Object> listener, bool isInline); + + virtual v8::Local<v8::Value> callListenerFunction(v8::Handle<v8::Value> jsEvent, Event*); + v8::Local<v8::Object> getReceiverObject(Event*); WorkerContextExecutionProxy* m_proxy; }; diff --git a/WebCore/bindings/v8/WorkerContextExecutionProxy.cpp b/WebCore/bindings/v8/WorkerContextExecutionProxy.cpp index ba858cf..90c1f6f 100644 --- a/WebCore/bindings/v8/WorkerContextExecutionProxy.cpp +++ b/WebCore/bindings/v8/WorkerContextExecutionProxy.cpp @@ -38,6 +38,8 @@ #include "DOMCoreException.h" #include "DedicatedWorkerContext.h" #include "Event.h" +#include "Notification.h" +#include "NotificationCenter.h" #include "EventException.h" #include "MessagePort.h" #include "RangeException.h" @@ -46,7 +48,9 @@ #include "V8Index.h" #include "V8Proxy.h" #include "V8WorkerContextEventListener.h" -#include "V8WorkerContextObjectEventListener.h" +#if ENABLE(WEB_SOCKETS) +#include "WebSocket.h" +#endif #include "Worker.h" #include "WorkerContext.h" #include "WorkerLocation.h" @@ -66,6 +70,7 @@ static void reportFatalErrorInV8(const char* location, const char* message) WorkerContextExecutionProxy::WorkerContextExecutionProxy(WorkerContext* workerContext) : m_workerContext(workerContext) , m_recursion(0) + , m_listenerGuard(V8ListenerGuard::create()) { initV8IfNeeded(); } @@ -77,13 +82,7 @@ WorkerContextExecutionProxy::~WorkerContextExecutionProxy() void WorkerContextExecutionProxy::dispose() { - // Disconnect all event listeners. - if (m_listeners.get()) { - for (V8EventListenerList::iterator iterator(m_listeners->begin()); iterator != m_listeners->end(); ++iterator) - static_cast<V8WorkerContextEventListener*>(*iterator)->disconnect(); - - m_listeners->clear(); - } + m_listenerGuard->disconnectListeners(); // Detach all events from their JS wrappers. for (size_t eventIndex = 0; eventIndex < m_events.size(); ++eventIndex) { @@ -169,8 +168,6 @@ void WorkerContextExecutionProxy::initContextIfNeeded() // Insert the object instance as the prototype of the shadow object. v8::Handle<v8::Object> globalObject = m_context->Global(); globalObject->Set(implicitProtoString, jsWorkerContext); - - m_listeners.set(new V8EventListenerList()); } v8::Handle<v8::Value> WorkerContextExecutionProxy::convertToV8Object(V8ClassIndex::V8WrapperType type, void* impl) @@ -224,6 +221,14 @@ v8::Handle<v8::Value> WorkerContextExecutionProxy::convertToV8Object(V8ClassInde case V8ClassIndex::WORKERNAVIGATOR: static_cast<WorkerNavigator*>(impl)->ref(); break; +#if ENABLE(NOTIFICATIONS) + case V8ClassIndex::NOTIFICATIONCENTER: + static_cast<NotificationCenter*>(impl)->ref(); + break; + case V8ClassIndex::NOTIFICATION: + static_cast<Notification*>(impl)->ref(); + break; +#endif case V8ClassIndex::DOMCOREEXCEPTION: static_cast<DOMCoreException*>(impl)->ref(); break; @@ -398,40 +403,9 @@ v8::Local<v8::Value> WorkerContextExecutionProxy::runScript(v8::Handle<v8::Scrip return result; } -PassRefPtr<V8EventListener> WorkerContextExecutionProxy::findOrCreateEventListenerHelper(v8::Local<v8::Value> object, bool isInline, bool findOnly, bool createObjectEventListener) -{ - if (!object->IsObject()) - return 0; - - V8EventListener* listener = m_listeners->find(object->ToObject(), isInline); - if (findOnly) - return listener; - - // Create a new one, and add to cache. - RefPtr<V8EventListener> newListener; - if (createObjectEventListener) - newListener = V8WorkerContextObjectEventListener::create(this, v8::Local<v8::Object>::Cast(object), isInline); - else - newListener = V8WorkerContextEventListener::create(this, v8::Local<v8::Object>::Cast(object), isInline); - - m_listeners->add(newListener.get()); - - return newListener.release(); -} - PassRefPtr<V8EventListener> WorkerContextExecutionProxy::findOrCreateEventListener(v8::Local<v8::Value> object, bool isInline, bool findOnly) { - return findOrCreateEventListenerHelper(object, isInline, findOnly, false); -} - -PassRefPtr<V8EventListener> WorkerContextExecutionProxy::findOrCreateObjectEventListener(v8::Local<v8::Value> object, bool isInline, bool findOnly) -{ - return findOrCreateEventListenerHelper(object, isInline, findOnly, true); -} - -void WorkerContextExecutionProxy::removeEventListener(V8EventListener* listener) -{ - m_listeners->remove(listener); + return findOnly ? V8EventListenerList::findWrapper(object, isInline) : V8EventListenerList::findOrCreateWrapper<V8WorkerContextEventListener>(this, m_listenerGuard, object, isInline); } void WorkerContextExecutionProxy::trackEvent(Event* event) diff --git a/WebCore/bindings/v8/WorkerContextExecutionProxy.h b/WebCore/bindings/v8/WorkerContextExecutionProxy.h index 75024df..a08395c 100644 --- a/WebCore/bindings/v8/WorkerContextExecutionProxy.h +++ b/WebCore/bindings/v8/WorkerContextExecutionProxy.h @@ -64,12 +64,8 @@ namespace WebCore { WorkerContextExecutionProxy(WorkerContext*); ~WorkerContextExecutionProxy(); - void removeEventListener(V8EventListener*); - // Finds/creates event listener wrappers. PassRefPtr<V8EventListener> findOrCreateEventListener(v8::Local<v8::Value> listener, bool isInline, bool findOnly); - PassRefPtr<V8EventListener> findOrCreateObjectEventListener(v8::Local<v8::Value> object, bool isInline, bool findOnly); - PassRefPtr<V8EventListener> findOrCreateEventListenerHelper(v8::Local<v8::Value> object, bool isInline, bool findOnly, bool createObjectEventListener); // Track the event so that we can detach it from the JS wrapper when a worker // terminates. This is needed because we need to be able to dispose these @@ -114,8 +110,8 @@ namespace WebCore { WorkerContext* m_workerContext; v8::Persistent<v8::Context> m_context; int m_recursion; + RefPtr<V8ListenerGuard> m_listenerGuard; - OwnPtr<V8EventListenerList> m_listeners; Vector<Event*> m_events; }; diff --git a/WebCore/bindings/v8/custom/V8AbstractWorkerCustom.cpp b/WebCore/bindings/v8/custom/V8AbstractWorkerCustom.cpp index ce759eb..0240895 100644 --- a/WebCore/bindings/v8/custom/V8AbstractWorkerCustom.cpp +++ b/WebCore/bindings/v8/custom/V8AbstractWorkerCustom.cpp @@ -38,75 +38,18 @@ #include "ScriptExecutionContext.h" #include "V8Binding.h" #include "V8CustomBinding.h" -#include "V8ObjectEventListener.h" #include "V8Proxy.h" #include "V8Utilities.h" #include "WorkerContextExecutionProxy.h" namespace WebCore { -PassRefPtr<EventListener> getEventListener(AbstractWorker* worker, v8::Local<v8::Value> value, bool isAttribute, bool findOnly) -{ - if (worker->scriptExecutionContext()->isWorkerContext()) { - WorkerContextExecutionProxy* workerContextProxy = WorkerContextExecutionProxy::retrieve(); - ASSERT(workerContextProxy); - return workerContextProxy->findOrCreateObjectEventListener(value, isAttribute, findOnly); - } - - V8Proxy* proxy = V8Proxy::retrieve(worker->scriptExecutionContext()); - if (proxy) { - V8EventListenerList* list = proxy->objectListeners(); - return findOnly ? list->findWrapper(value, isAttribute) : list->findOrCreateWrapper<V8ObjectEventListener>(proxy->frame(), value, isAttribute); - } - - return 0; -} - -ACCESSOR_GETTER(AbstractWorkerOnerror) -{ - INC_STATS(L"DOM.AbstractWorker.onerror._get"); - AbstractWorker* worker = V8DOMWrapper::convertToNativeObject<AbstractWorker>(V8ClassIndex::ABSTRACTWORKER, info.Holder()); - if (worker->onerror()) { - V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(worker->onerror()); - v8::Local<v8::Object> v8Listener = listener->getListenerObject(); - return v8Listener; - } - return v8::Undefined(); -} - -ACCESSOR_SETTER(AbstractWorkerOnerror) -{ - INC_STATS(L"DOM.AbstractWorker.onerror._set"); - AbstractWorker* worker = V8DOMWrapper::convertToNativeObject<AbstractWorker>(V8ClassIndex::ABSTRACTWORKER, info.Holder()); - V8ObjectEventListener* oldListener = static_cast<V8ObjectEventListener*>(worker->onerror()); - if (value->IsNull()) { - if (oldListener) { - v8::Local<v8::Object> oldV8Listener = oldListener->getListenerObject(); - removeHiddenDependency(info.Holder(), oldV8Listener, V8Custom::kAbstractWorkerRequestCacheIndex); - } - - // Clear the listener. - worker->setOnerror(0); - } else { - RefPtr<EventListener> listener = getEventListener(worker, value, true, false); - if (listener) { - if (oldListener) { - v8::Local<v8::Object> oldV8Listener = oldListener->getListenerObject(); - removeHiddenDependency(info.Holder(), oldV8Listener, V8Custom::kAbstractWorkerRequestCacheIndex); - } - - worker->setOnerror(listener); - createHiddenDependency(info.Holder(), value, V8Custom::kAbstractWorkerRequestCacheIndex); - } - } -} - CALLBACK_FUNC_DECL(AbstractWorkerAddEventListener) { INC_STATS(L"DOM.AbstractWorker.addEventListener()"); AbstractWorker* worker = V8DOMWrapper::convertToNativeObject<AbstractWorker>(V8ClassIndex::ABSTRACTWORKER, args.Holder()); - RefPtr<EventListener> listener = getEventListener(worker, args[1], false, false); + RefPtr<EventListener> listener = V8DOMWrapper::getEventListener(worker, args[1], false, ListenerFindOrCreate); if (listener) { String type = toWebCoreString(args[0]); bool useCapture = args[2]->BooleanValue(); @@ -122,7 +65,7 @@ CALLBACK_FUNC_DECL(AbstractWorkerRemoveEventListener) INC_STATS(L"DOM.AbstractWorker.removeEventListener()"); AbstractWorker* worker = V8DOMWrapper::convertToNativeObject<AbstractWorker>(V8ClassIndex::ABSTRACTWORKER, args.Holder()); - RefPtr<EventListener> listener = getEventListener(worker, args[1], false, true); + RefPtr<EventListener> listener = V8DOMWrapper::getEventListener(worker, args[1], false, ListenerFindOnly); if (listener) { String type = toWebCoreString(args[0]); bool useCapture = args[2]->BooleanValue(); diff --git a/WebCore/bindings/v8/custom/V8CanvasArrayBufferCustom.cpp b/WebCore/bindings/v8/custom/V8CanvasArrayBufferCustom.cpp new file mode 100644 index 0000000..4a85e82 --- /dev/null +++ b/WebCore/bindings/v8/custom/V8CanvasArrayBufferCustom.cpp @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2009 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" + +#if ENABLE(3D_CANVAS) + +#include "CanvasArrayBuffer.h" + +#include "V8Binding.h" +#include "V8CanvasArrayBuffer.h" +#include "V8CustomBinding.h" +#include "V8Proxy.h" + +namespace WebCore { + +CALLBACK_FUNC_DECL(CanvasArrayBufferConstructor) +{ + INC_STATS("DOM.CanvasArrayBuffer.Contructor"); + + if (!args.IsConstructCall()) + return throwError("DOM object constructor cannot be called as a function."); + + int argLen = args.Length(); + // Supported constructors: + // CanvasArrayBuffer(n) where n is an integer: + // -- create an empty buffer of n bytes + + if (argLen != 1) + return throwError("Wrong number of arguments specified to constructor (requires 1)"); + + int len = 0; + if (!args[0]->IsInt32()) + return throwError("Argument to CanvasArrayBuffer constructor was not an integer"); + len = toInt32(args[0]); + RefPtr<CanvasArrayBuffer> buffer = CanvasArrayBuffer::create(len); + // Transform the holder into a wrapper object for the array. + V8DOMWrapper::setDOMWrapper(args.Holder(), V8ClassIndex::ToInt(V8ClassIndex::CANVASARRAYBUFFER), buffer.get()); + return toV8(buffer.release(), args.Holder()); +} + +} // namespace WebCore + +#endif // ENABLE(3D_CANVAS) diff --git a/WebCore/bindings/v8/custom/V8CanvasArrayCustom.h b/WebCore/bindings/v8/custom/V8CanvasArrayCustom.h new file mode 100644 index 0000000..311b838 --- /dev/null +++ b/WebCore/bindings/v8/custom/V8CanvasArrayCustom.h @@ -0,0 +1,136 @@ +/* + * Copyright (C) 2009 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" + +#if ENABLE(3D_CANVAS) + +#include "CanvasArrayBuffer.h" + +#include "V8Binding.h" +#include "V8CanvasArrayBuffer.h" +#include "V8CustomBinding.h" +#include "V8Proxy.h" + +namespace WebCore { + + // Template function used by the CanvasArray*Constructor callbacks. + template<class ArrayClass> + v8::Handle<v8::Value> constructCanvasArray(const v8::Arguments& args, + int classIndex) + { + if (!args.IsConstructCall()) + return throwError("DOM object constructor cannot be called as a function."); + + int argLen = args.Length(); + // Supported constructors: + // Canvas<T>Array(n) where n is an integer: + // -- create an empty array of n elements + // Canvas<T>Array(arr) where arr is an array: + // -- create a Canvas<T>Array containing the contents of "arr" + // Canvas<T>Array(buf, offset, length) + // -- create a Canvas<T>Array pointing to the CanvasArrayBuffer + // "buf", starting at the specified offset, for the given + // length + + if (argLen == 0) + return throwError("No arguments specified to constructor"); + + // See whether the first argument is a CanvasArrayBuffer. + if (V8CanvasArrayBuffer::HasInstance(args[0])) { + if (argLen > 3) + return throwError("Wrong number of arguments to new Canvas<T>Array(CanvasArrayBuffer, int, int)"); + + CanvasArrayBuffer* buf = + V8DOMWrapper::convertToNativeObject<CanvasArrayBuffer>(V8ClassIndex::CANVASARRAYBUFFER, + args[0]->ToObject()); + if (buf == NULL) + return throwError("Could not convert argument 0 to a CanvasArrayBuffer"); + bool ok; + int offset = 0; + if (argLen > 1) { + offset = toInt32(args[1], ok); + if (!ok) + return throwError("Could not convert argument 1 to an integer"); + } + int length = buf->byteLength() - offset; + if (argLen > 2) { + length = toInt32(args[2], ok); + if (!ok) + return throwError("Could not convert argument 2 to an integer"); + } + if (length < 0) + return throwError("Length / offset out of range"); + + RefPtr<ArrayClass> array = ArrayClass::create(buf, offset, length); + if (array == NULL) + return throwError("Invalid arguments to new Canvas<T>Array(CanvasArrayBuffer, int, int)"); + // Transform the holder into a wrapper object for the array. + V8DOMWrapper::setDOMWrapper(args.Holder(), classIndex, array.get()); + return toV8(array.release(), args.Holder()); + } + + int len = 0; + v8::Handle<v8::Array> srcArray; + if (argLen != 1) + return throwError("Wrong number of arguments to new Canvas<T>Array(int / array)"); + + if (args[0]->IsInt32()) { + len = toInt32(args[0]); + } else if (args[0]->IsArray()) { + srcArray = v8::Local<v8::Array>::Cast(args[0]); + if (srcArray.IsEmpty()) + return throwError("Could not convert argument 0 to an array"); + len = srcArray->Length(); + } else + return throwError("Could not convert argument 0 to either an int32 or an array"); + + RefPtr<ArrayClass> array = ArrayClass::create(len); + if (!srcArray.IsEmpty()) { + // Need to copy the incoming array into the newly created CanvasArray. + for (int i = 0; i < len; i++) { + v8::Local<v8::Value> val = srcArray->Get(v8::Integer::New(i)); + if (!val->IsNumber()) { + char buf[256]; + sprintf(buf, "Could not convert array element %d to a number", i); + return throwError(buf); + } + array->set(i, val->NumberValue()); + } + } + + // Transform the holder into a wrapper object for the array. + V8DOMWrapper::setDOMWrapper(args.Holder(), classIndex, array.get()); + return toV8(array.release(), args.Holder()); + } + +} + +#endif // ENABLE(3D_CANVAS) diff --git a/WebCore/bindings/v8/custom/V8CanvasByteArrayCustom.cpp b/WebCore/bindings/v8/custom/V8CanvasByteArrayCustom.cpp new file mode 100644 index 0000000..503e5e8 --- /dev/null +++ b/WebCore/bindings/v8/custom/V8CanvasByteArrayCustom.cpp @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2009 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" + +#if ENABLE(3D_CANVAS) + +#include "CanvasArrayBuffer.h" +#include "CanvasByteArray.h" + +#include "V8Binding.h" +#include "V8CanvasArrayBuffer.h" +#include "V8CanvasArrayCustom.h" +#include "V8CustomBinding.h" +#include "V8Proxy.h" + +namespace WebCore { + +CALLBACK_FUNC_DECL(CanvasByteArrayConstructor) +{ + INC_STATS("DOM.CanvasByteArray.Contructor"); + + return constructCanvasArray<CanvasByteArray>(args, V8ClassIndex::ToInt(V8ClassIndex::CANVASBYTEARRAY)); +} + +// Get the specified value from the byte buffer and return it wrapped as a JavaScript Number object to V8. Accesses outside the valid byte buffer range return "undefined". +INDEXED_PROPERTY_GETTER(CanvasByteArray) +{ + INC_STATS("DOM.CanvasByteArray.IndexedPropertyGetter"); + CanvasByteArray* byteBuffer = V8DOMWrapper::convertToNativeObject<CanvasByteArray>(V8ClassIndex::CANVASBYTEARRAY, info.Holder()); + + if ((index < 0) || (index >= byteBuffer->length())) + return v8::Undefined(); + signed char result; + if (!byteBuffer->get(index, result)) + return v8::Undefined(); + return v8::Number::New(result); +} + +// Set the specified value in the byte buffer. Accesses outside the valid byte buffer range are silently ignored. +INDEXED_PROPERTY_SETTER(CanvasByteArray) +{ + INC_STATS("DOM.CanvasByteArray.IndexedPropertySetter"); + CanvasByteArray* array = V8DOMWrapper::convertToNativeObject<CanvasByteArray>(V8ClassIndex::CANVASBYTEARRAY, info.Holder()); + + if ((index >= 0) && (index < array->length())) { + if (!value->IsNumber()) + return throwError("Could not convert value argument to a number"); + array->set(index, value->NumberValue()); + } + return value; +} + +} // namespace WebCore + +#endif // ENABLE(3D_CANVAS) diff --git a/WebCore/bindings/v8/custom/V8CanvasFloatArrayCustom.cpp b/WebCore/bindings/v8/custom/V8CanvasFloatArrayCustom.cpp new file mode 100644 index 0000000..b3c1d62 --- /dev/null +++ b/WebCore/bindings/v8/custom/V8CanvasFloatArrayCustom.cpp @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2009 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" + +#if ENABLE(3D_CANVAS) + +#include "CanvasArrayBuffer.h" +#include "CanvasFloatArray.h" + +#include "V8Binding.h" +#include "V8CanvasArrayBuffer.h" +#include "V8CanvasArrayCustom.h" +#include "V8CustomBinding.h" +#include "V8Proxy.h" + +namespace WebCore { + +CALLBACK_FUNC_DECL(CanvasFloatArrayConstructor) +{ + INC_STATS("DOM.CanvasFloatArray.Contructor"); + + return constructCanvasArray<CanvasFloatArray>(args, V8ClassIndex::ToInt(V8ClassIndex::CANVASFLOATARRAY)); +} + +// Get the specified value from the array and return it wrapped as a JavaScript Number object to V8. Accesses outside the valid array range return "undefined". +INDEXED_PROPERTY_GETTER(CanvasFloatArray) +{ + INC_STATS("DOM.CanvasFloatArray.IndexedPropertyGetter"); + CanvasFloatArray* array = V8DOMWrapper::convertToNativeObject<CanvasFloatArray>(V8ClassIndex::CANVASFLOATARRAY, info.Holder()); + + if ((index < 0) || (index >= array->length())) + return v8::Undefined(); + float result; + if (!array->get(index, result)) + return v8::Undefined(); + return v8::Number::New(result); +} + +// Set the specified value in the array. Accesses outside the valid array range are silently ignored. +INDEXED_PROPERTY_SETTER(CanvasFloatArray) +{ + INC_STATS("DOM.CanvasFloatArray.IndexedPropertySetter"); + CanvasFloatArray* array = V8DOMWrapper::convertToNativeObject<CanvasFloatArray>(V8ClassIndex::CANVASFLOATARRAY, info.Holder()); + + if ((index >= 0) && (index < array->length())) + array->set(index, value->NumberValue()); + return value; +} + +} // namespace WebCore + +#endif // ENABLE(3D_CANVAS) diff --git a/WebCore/bindings/v8/custom/V8CanvasIntArrayCustom.cpp b/WebCore/bindings/v8/custom/V8CanvasIntArrayCustom.cpp new file mode 100644 index 0000000..6f35db8 --- /dev/null +++ b/WebCore/bindings/v8/custom/V8CanvasIntArrayCustom.cpp @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2009 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" + +#if ENABLE(3D_CANVAS) + +#include "CanvasArrayBuffer.h" +#include "CanvasIntArray.h" + +#include "V8Binding.h" +#include "V8CanvasArrayBuffer.h" +#include "V8CanvasArrayCustom.h" +#include "V8CustomBinding.h" +#include "V8Proxy.h" + +namespace WebCore { + +CALLBACK_FUNC_DECL(CanvasIntArrayConstructor) +{ + INC_STATS("DOM.CanvasIntArray.Contructor"); + + return constructCanvasArray<CanvasIntArray>(args, V8ClassIndex::ToInt(V8ClassIndex::CANVASINTARRAY)); +} + +// Get the specified value from the integer array and return it wrapped as a JavaScript Number object to V8. Accesses outside the valid pixel buffer range return "undefined". +INDEXED_PROPERTY_GETTER(CanvasIntArray) +{ + INC_STATS("DOM.CanvasIntArray.IndexedPropertyGetter"); + CanvasIntArray* array = V8DOMWrapper::convertToNativeObject<CanvasIntArray>(V8ClassIndex::CANVASINTARRAY, info.Holder()); + + if ((index < 0) || (index >= array->length())) + return v8::Undefined(); + int result; + if (!array->get(index, result)) + return v8::Undefined(); + return v8::Number::New(result); +} + +// Set the specified value in the integer array. Accesses outside the valid integer array range are silently ignored. +INDEXED_PROPERTY_SETTER(CanvasIntArray) +{ + INC_STATS("DOM.CanvasIntArray.IndexedPropertySetter"); + CanvasIntArray* array = V8DOMWrapper::convertToNativeObject<CanvasIntArray>(V8ClassIndex::CANVASINTARRAY, info.Holder()); + + if ((index >= 0) && (index < array->length())) { + if (!value->IsNumber()) + return throwError("Could not convert value argument to a number"); + array->set(index, value->NumberValue()); + } + return value; +} + +} // namespace WebCore + +#endif // ENABLE(3D_CANVAS) diff --git a/WebCore/bindings/v8/custom/V8CanvasRenderingContext3DCustom.cpp b/WebCore/bindings/v8/custom/V8CanvasRenderingContext3DCustom.cpp new file mode 100644 index 0000000..c109bb8 --- /dev/null +++ b/WebCore/bindings/v8/custom/V8CanvasRenderingContext3DCustom.cpp @@ -0,0 +1,597 @@ +/* + * Copyright (C) 2009 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" + +#if ENABLE(3D_CANVAS) + +#include "CanvasRenderingContext3D.h" + +#include "ExceptionCode.h" + +#include "NotImplemented.h" + +#include <wtf/FastMalloc.h> + +#include "V8Binding.h" +#include "V8CanvasArray.h" +#include "V8CanvasByteArray.h" +#include "V8CanvasFloatArray.h" +#include "V8CanvasIntArray.h" +#include "V8CanvasShortArray.h" +#include "V8CanvasUnsignedByteArray.h" +#include "V8CanvasUnsignedIntArray.h" +#include "V8CanvasUnsignedShortArray.h" +#include "V8HTMLImageElement.h" +#include "V8Proxy.h" + +namespace WebCore { + +// Allocates new storage via tryFastMalloc. +// Returns NULL if array failed to convert for any reason. +static float* jsArrayToFloatArray(v8::Handle<v8::Array> array, uint32_t len) +{ + // Convert the data element-by-element. + float* data; + if (!tryFastMalloc(len * sizeof(float)).getValue(data)) + return 0; + for (uint32_t i = 0; i < len; i++) { + v8::Local<v8::Value> val = array->Get(v8::Integer::New(i)); + if (!val->IsNumber()) { + fastFree(data); + return 0; + } + data[i] = toFloat(val); + } + return data; +} + +// Allocates new storage via tryFastMalloc. +// Returns NULL if array failed to convert for any reason. +static int* jsArrayToIntArray(v8::Handle<v8::Array> array, uint32_t len) +{ + // Convert the data element-by-element. + int* data; + if (!tryFastMalloc(len * sizeof(int)).getValue(data)) + return 0; + for (uint32_t i = 0; i < len; i++) { + v8::Local<v8::Value> val = array->Get(v8::Integer::New(i)); + bool ok; + int ival = toInt32(val, ok); + if (!ok) { + fastFree(data); + return 0; + } + data[i] = ival; + } + return data; +} + +CALLBACK_FUNC_DECL(CanvasRenderingContext3DBufferData) +{ + INC_STATS("DOM.CanvasRenderingContext3D.bufferData()"); + + // Forms: + // * bufferData(GLenum target, CanvasArray data, GLenum usage); + // - Sets the buffer's data from the given CanvasArray + // * bufferData(GLenum target, GLsizeiptr size, GLenum usage); + // - Sets the size of the buffer to the given size in bytes + if (args.Length() != 3) { + V8Proxy::setDOMException(SYNTAX_ERR); + return notHandledByInterceptor(); + } + + CanvasRenderingContext3D* context = + V8DOMWrapper::convertDOMWrapperToNative<CanvasRenderingContext3D>(args.Holder()); + bool ok; + int target = toInt32(args[0], ok); + if (!ok) { + V8Proxy::setDOMException(SYNTAX_ERR); + return notHandledByInterceptor(); + } + int usage = toInt32(args[2], ok); + if (!ok) { + V8Proxy::setDOMException(SYNTAX_ERR); + return notHandledByInterceptor(); + } + if (args[1]->IsInt32()) { + int size = toInt32(args[1]); + context->bufferData(target, size, usage); + } else if (V8CanvasArray::HasInstance(args[1])) { + CanvasArray* array = V8DOMWrapper::convertToNativeObject<CanvasArray>(V8ClassIndex::CANVASARRAY, args[1]->ToObject()); + context->bufferData(target, array, usage); + } else { + V8Proxy::setDOMException(SYNTAX_ERR); + return notHandledByInterceptor(); + } + return v8::Undefined(); +} + +CALLBACK_FUNC_DECL(CanvasRenderingContext3DBufferSubData) +{ + INC_STATS("DOM.CanvasRenderingContext3D.bufferSubData()"); + + // Forms: + // * bufferSubData(GLenum target, GLintptr offset, CanvasArray data); + if (args.Length() != 3) { + V8Proxy::setDOMException(SYNTAX_ERR); + return notHandledByInterceptor(); + } + + CanvasRenderingContext3D* context = + V8DOMWrapper::convertDOMWrapperToNative<CanvasRenderingContext3D>(args.Holder()); + bool ok; + int target = toInt32(args[0], ok); + if (!ok) { + V8Proxy::setDOMException(SYNTAX_ERR); + return notHandledByInterceptor(); + } + int offset = toInt32(args[1], ok); + if (!ok) { + V8Proxy::setDOMException(SYNTAX_ERR); + return notHandledByInterceptor(); + } + if (!V8CanvasArray::HasInstance(args[2])) { + V8Proxy::setDOMException(SYNTAX_ERR); + return notHandledByInterceptor(); + } + CanvasArray* array = V8DOMWrapper::convertToNativeObject<CanvasArray>(V8ClassIndex::CANVASARRAY, args[2]->ToObject()); + context->bufferSubData(target, offset, array); + return v8::Undefined(); +} + +CALLBACK_FUNC_DECL(CanvasRenderingContext3DTexImage2D) +{ + INC_STATS("DOM.CanvasRenderingContext3D.texImage2D()"); + + // Currently supported forms: + // * void texImage2D(in GLenum target, in GLint level, + // in GLint internalformat, + // in GLsizei width, in GLsizei height, in GLint border, + // in GLenum format, in GLenum type, in CanvasArray pixels); + // * void texImage2D(in GLenum target, in GLint level, in HTMLImageElement image, + // [Optional] in GLboolean flipY, [Optional] in GLboolean premultiplyAlpha); + if (args.Length() != 3 && + args.Length() != 4 && + args.Length() != 5 && + args.Length() != 9) { + V8Proxy::setDOMException(SYNTAX_ERR); + return notHandledByInterceptor(); + } + + CanvasRenderingContext3D* context = + V8DOMWrapper::convertDOMWrapperToNative<CanvasRenderingContext3D>(args.Holder()); + bool ok; + int target = toInt32(args[0], ok); + if (!ok) { + V8Proxy::setDOMException(SYNTAX_ERR); + return notHandledByInterceptor(); + } + int level = toInt32(args[1], ok); + if (!ok) { + V8Proxy::setDOMException(SYNTAX_ERR); + return notHandledByInterceptor(); + } + + ExceptionCode ec = 0; + if (args.Length() == 3 || + args.Length() == 4 || + args.Length() == 5) { + v8::Handle<v8::Value> arg = args[2]; + if (V8HTMLImageElement::HasInstance(arg)) { + HTMLImageElement* image_element = V8DOMWrapper::convertDOMWrapperToNode<HTMLImageElement>(v8::Handle<v8::Object>::Cast(arg)); + bool flipY = false; + bool premultiplyAlpha = false; + if (args.Length() >= 4) + flipY = args[3]->BooleanValue(); + if (args.Length() >= 5) + premultiplyAlpha = args[4]->BooleanValue(); + context->texImage2D(target, level, image_element, flipY, premultiplyAlpha, ec); + } else { + // FIXME: consider different / better exception type. + V8Proxy::setDOMException(SYNTAX_ERR); + return notHandledByInterceptor(); + } + // Fall through + } else if (args.Length() == 9) { + int internalformat = toInt32(args[2], ok); + if (!ok) { + V8Proxy::setDOMException(SYNTAX_ERR); + return notHandledByInterceptor(); + } + int width = toInt32(args[3], ok); + if (!ok) { + V8Proxy::setDOMException(SYNTAX_ERR); + return notHandledByInterceptor(); + } + int height = toInt32(args[4], ok); + if (!ok) { + V8Proxy::setDOMException(SYNTAX_ERR); + return notHandledByInterceptor(); + } + int border = toInt32(args[5], ok); + if (!ok) { + V8Proxy::setDOMException(SYNTAX_ERR); + return notHandledByInterceptor(); + } + int format = toInt32(args[6], ok); + if (!ok) { + V8Proxy::setDOMException(SYNTAX_ERR); + return notHandledByInterceptor(); + } + int type = toInt32(args[7], ok); + if (!ok) { + V8Proxy::setDOMException(SYNTAX_ERR); + return notHandledByInterceptor(); + } + v8::Handle<v8::Value> arg = args[8]; + if (V8CanvasArray::HasInstance(arg)) { + CanvasArray* array = V8DOMWrapper::convertToNativeObject<CanvasArray>(V8ClassIndex::CANVASARRAY, arg->ToObject()); + // FIXME: must do validation similar to JOGL's to ensure that + // the incoming array is of the appropriate length and type + context->texImage2D(target, + level, + internalformat, + width, + height, + border, + format, + type, + array, + ec); + // Fall through + } else { + V8Proxy::setDOMException(SYNTAX_ERR); + return notHandledByInterceptor(); + } + } else { + ASSERT_NOT_REACHED(); + V8Proxy::setDOMException(SYNTAX_ERR); + return notHandledByInterceptor(); + } + if (ec) { + V8Proxy::setDOMException(ec); + return v8::Handle<v8::Value>(); + } + return v8::Undefined(); +} + +CALLBACK_FUNC_DECL(CanvasRenderingContext3DTexSubImage2D) +{ + INC_STATS("DOM.CanvasRenderingContext3D.texSubImage2D()"); + + // FIXME: implement + notImplemented(); + + return v8::Undefined(); +} + +enum FunctionToCall { + kUniform1v, kUniform2v, kUniform3v, kUniform4v, + kVertexAttrib1v, kVertexAttrib2v, kVertexAttrib3v, kVertexAttrib4v +}; + +static v8::Handle<v8::Value> vertexAttribAndUniformHelperf(const v8::Arguments& args, + FunctionToCall functionToCall) { + // Forms: + // * glUniform1fv(GLint location, Array data); + // * glUniform1fv(GLint location, CanvasFloatArray data); + // * glUniform2fv(GLint location, Array data); + // * glUniform2fv(GLint location, CanvasFloatArray data); + // * glUniform3fv(GLint location, Array data); + // * glUniform3fv(GLint location, CanvasFloatArray data); + // * glUniform4fv(GLint location, Array data); + // * glUniform4fv(GLint location, CanvasFloatArray data); + // * glVertexAttrib1fv(GLint location, Array data); + // * glVertexAttrib1fv(GLint location, CanvasFloatArray data); + // * glVertexAttrib2fv(GLint location, Array data); + // * glVertexAttrib2fv(GLint location, CanvasFloatArray data); + // * glVertexAttrib3fv(GLint location, Array data); + // * glVertexAttrib3fv(GLint location, CanvasFloatArray data); + // * glVertexAttrib4fv(GLint location, Array data); + // * glVertexAttrib4fv(GLint location, CanvasFloatArray data); + + if (args.Length() != 3) { + V8Proxy::setDOMException(SYNTAX_ERR); + return notHandledByInterceptor(); + } + + CanvasRenderingContext3D* context = + V8DOMWrapper::convertDOMWrapperToNative<CanvasRenderingContext3D>(args.Holder()); + bool ok; + int location = toInt32(args[0], ok); + if (!ok) { + V8Proxy::setDOMException(SYNTAX_ERR); + return notHandledByInterceptor(); + } + if (V8CanvasFloatArray::HasInstance(args[1])) { + CanvasFloatArray* array = + V8DOMWrapper::convertToNativeObject<CanvasFloatArray>(V8ClassIndex::CANVASFLOATARRAY, args[1]->ToObject()); + ASSERT(array != NULL); + switch (functionToCall) { + case kUniform1v: context->uniform1fv(location, array); break; + case kUniform2v: context->uniform2fv(location, array); break; + case kUniform3v: context->uniform3fv(location, array); break; + case kUniform4v: context->uniform4fv(location, array); break; + case kVertexAttrib1v: context->vertexAttrib1fv(location, array); break; + case kVertexAttrib2v: context->vertexAttrib2fv(location, array); break; + case kVertexAttrib3v: context->vertexAttrib3fv(location, array); break; + case kVertexAttrib4v: context->vertexAttrib4fv(location, array); break; + default: ASSERT_NOT_REACHED(); break; + } + return v8::Undefined(); + } + + v8::Handle<v8::Array> array = + v8::Local<v8::Array>::Cast(args[1]); + if (array.IsEmpty()) { + V8Proxy::setDOMException(SYNTAX_ERR); + return notHandledByInterceptor(); + } + uint32_t len = array->Length(); + float* data = jsArrayToFloatArray(array, len); + if (!data) { + // FIXME: consider different / better exception type. + V8Proxy::setDOMException(SYNTAX_ERR); + return notHandledByInterceptor(); + } + switch (functionToCall) { + case kUniform1v: context->uniform1fv(location, data, len); break; + case kUniform2v: context->uniform2fv(location, data, len); break; + case kUniform3v: context->uniform3fv(location, data, len); break; + case kUniform4v: context->uniform4fv(location, data, len); break; + case kVertexAttrib1v: context->vertexAttrib1fv(location, data, len); break; + case kVertexAttrib2v: context->vertexAttrib2fv(location, data, len); break; + case kVertexAttrib3v: context->vertexAttrib3fv(location, data, len); break; + case kVertexAttrib4v: context->vertexAttrib4fv(location, data, len); break; + default: ASSERT_NOT_REACHED(); break; + } + fastFree(data); + return v8::Undefined(); +} + +static v8::Handle<v8::Value> uniformHelperi(const v8::Arguments& args, + FunctionToCall functionToCall) { + // Forms: + // * glUniform1iv(GLint location, Array data); + // * glUniform1iv(GLint location, CanvasIntArray data); + // * glUniform2iv(GLint location, Array data); + // * glUniform2iv(GLint location, CanvasIntArray data); + // * glUniform3iv(GLint location, Array data); + // * glUniform3iv(GLint location, CanvasIntArray data); + // * glUniform4iv(GLint location, Array data); + // * glUniform4iv(GLint location, CanvasIntArray data); + + if (args.Length() != 3) { + V8Proxy::setDOMException(SYNTAX_ERR); + return notHandledByInterceptor(); + } + + CanvasRenderingContext3D* context = + V8DOMWrapper::convertDOMWrapperToNative<CanvasRenderingContext3D>(args.Holder()); + bool ok; + int location = toInt32(args[0], ok); + if (!ok) { + V8Proxy::setDOMException(SYNTAX_ERR); + return notHandledByInterceptor(); + } + if (V8CanvasIntArray::HasInstance(args[1])) { + CanvasIntArray* array = + V8DOMWrapper::convertToNativeObject<CanvasIntArray>(V8ClassIndex::CANVASINTARRAY, args[1]->ToObject()); + ASSERT(array != NULL); + switch (functionToCall) { + case kUniform1v: context->uniform1iv(location, array); break; + case kUniform2v: context->uniform2iv(location, array); break; + case kUniform3v: context->uniform3iv(location, array); break; + case kUniform4v: context->uniform4iv(location, array); break; + default: ASSERT_NOT_REACHED(); break; + } + return v8::Undefined(); + } + + v8::Handle<v8::Array> array = + v8::Local<v8::Array>::Cast(args[1]); + if (array.IsEmpty()) { + V8Proxy::setDOMException(SYNTAX_ERR); + return notHandledByInterceptor(); + } + uint32_t len = array->Length(); + int* data = jsArrayToIntArray(array, len); + if (!data) { + // FIXME: consider different / better exception type. + V8Proxy::setDOMException(SYNTAX_ERR); + return notHandledByInterceptor(); + } + switch (functionToCall) { + case kUniform1v: context->uniform1iv(location, data, len); break; + case kUniform2v: context->uniform2iv(location, data, len); break; + case kUniform3v: context->uniform3iv(location, data, len); break; + case kUniform4v: context->uniform4iv(location, data, len); break; + default: ASSERT_NOT_REACHED(); break; + } + fastFree(data); + return v8::Undefined(); +} + +CALLBACK_FUNC_DECL(CanvasRenderingContext3DUniform1fv) +{ + INC_STATS("DOM.CanvasRenderingContext3D.uniform1fv()"); + return vertexAttribAndUniformHelperf(args, kUniform1v); +} + +CALLBACK_FUNC_DECL(CanvasRenderingContext3DUniform1iv) +{ + INC_STATS("DOM.CanvasRenderingContext3D.uniform1iv()"); + return uniformHelperi(args, kUniform1v); +} + +CALLBACK_FUNC_DECL(CanvasRenderingContext3DUniform2fv) +{ + INC_STATS("DOM.CanvasRenderingContext3D.uniform2fv()"); + return vertexAttribAndUniformHelperf(args, kUniform2v); +} + +CALLBACK_FUNC_DECL(CanvasRenderingContext3DUniform2iv) +{ + INC_STATS("DOM.CanvasRenderingContext3D.uniform2iv()"); + return uniformHelperi(args, kUniform2v); +} + +CALLBACK_FUNC_DECL(CanvasRenderingContext3DUniform3fv) +{ + INC_STATS("DOM.CanvasRenderingContext3D.uniform3fv()"); + return vertexAttribAndUniformHelperf(args, kUniform3v); +} + +CALLBACK_FUNC_DECL(CanvasRenderingContext3DUniform3iv) +{ + INC_STATS("DOM.CanvasRenderingContext3D.uniform3iv()"); + return uniformHelperi(args, kUniform3v); +} + +CALLBACK_FUNC_DECL(CanvasRenderingContext3DUniform4fv) +{ + INC_STATS("DOM.CanvasRenderingContext3D.uniform4fv()"); + return vertexAttribAndUniformHelperf(args, kUniform4v); +} + +CALLBACK_FUNC_DECL(CanvasRenderingContext3DUniform4iv) +{ + INC_STATS("DOM.CanvasRenderingContext3D.uniform4iv()"); + return uniformHelperi(args, kUniform4v); +} + +static v8::Handle<v8::Value> uniformMatrixHelper(const v8::Arguments& args, + int matrixSize) +{ + // Forms: + // * glUniformMatrix2fv(GLint location, GLboolean transpose, Array data); + // * glUniformMatrix2fv(GLint location, GLboolean transpose, CanvasFloatArray data); + // * glUniformMatrix3fv(GLint location, GLboolean transpose, Array data); + // * glUniformMatrix3fv(GLint location, GLboolean transpose, CanvasFloatArray data); + // * glUniformMatrix4fv(GLint location, GLboolean transpose, Array data); + // * glUniformMatrix4fv(GLint location, GLboolean transpose, CanvasFloatArray data); + // + // FIXME: need to change to accept CanvasFloatArray as well. + if (args.Length() != 3) { + V8Proxy::setDOMException(SYNTAX_ERR); + return notHandledByInterceptor(); + } + + CanvasRenderingContext3D* context = + V8DOMWrapper::convertDOMWrapperToNative<CanvasRenderingContext3D>(args.Holder()); + bool ok; + int location = toInt32(args[0], ok); + if (!ok) { + V8Proxy::setDOMException(SYNTAX_ERR); + return notHandledByInterceptor(); + } + bool transpose = args[1]->BooleanValue(); + if (V8CanvasFloatArray::HasInstance(args[2])) { + CanvasFloatArray* array = + V8DOMWrapper::convertToNativeObject<CanvasFloatArray>(V8ClassIndex::CANVASFLOATARRAY, args[2]->ToObject()); + ASSERT(array != NULL); + switch (matrixSize) { + case 2: context->uniformMatrix2fv(location, transpose, array); break; + case 3: context->uniformMatrix3fv(location, transpose, array); break; + case 4: context->uniformMatrix4fv(location, transpose, array); break; + default: ASSERT_NOT_REACHED(); break; + } + return v8::Undefined(); + } + + v8::Handle<v8::Array> array = + v8::Local<v8::Array>::Cast(args[2]); + if (array.IsEmpty()) { + V8Proxy::setDOMException(SYNTAX_ERR); + return notHandledByInterceptor(); + } + uint32_t len = array->Length(); + float* data = jsArrayToFloatArray(array, len); + if (!data) { + // FIXME: consider different / better exception type. + V8Proxy::setDOMException(SYNTAX_ERR); + return notHandledByInterceptor(); + } + switch (matrixSize) { + case 2: context->uniformMatrix2fv(location, transpose, data, len); break; + case 3: context->uniformMatrix3fv(location, transpose, data, len); break; + case 4: context->uniformMatrix4fv(location, transpose, data, len); break; + default: ASSERT_NOT_REACHED(); break; + } + fastFree(data); + return v8::Undefined(); +} + +CALLBACK_FUNC_DECL(CanvasRenderingContext3DUniformMatrix2fv) +{ + INC_STATS("DOM.CanvasRenderingContext3D.uniformMatrix2fv()"); + return uniformMatrixHelper(args, 2); +} + +CALLBACK_FUNC_DECL(CanvasRenderingContext3DUniformMatrix3fv) +{ + INC_STATS("DOM.CanvasRenderingContext3D.uniformMatrix3fv()"); + return uniformMatrixHelper(args, 3); +} + +CALLBACK_FUNC_DECL(CanvasRenderingContext3DUniformMatrix4fv) +{ + INC_STATS("DOM.CanvasRenderingContext3D.uniformMatrix4fv()"); + return uniformMatrixHelper(args, 4); +} + +CALLBACK_FUNC_DECL(CanvasRenderingContext3DVertexAttrib1fv) +{ + INC_STATS("DOM.CanvasRenderingContext3D.vertexAttrib1fv()"); + return vertexAttribAndUniformHelperf(args, kVertexAttrib1v); +} + +CALLBACK_FUNC_DECL(CanvasRenderingContext3DVertexAttrib2fv) +{ + INC_STATS("DOM.CanvasRenderingContext3D.vertexAttrib2fv()"); + return vertexAttribAndUniformHelperf(args, kVertexAttrib2v); +} + +CALLBACK_FUNC_DECL(CanvasRenderingContext3DVertexAttrib3fv) +{ + INC_STATS("DOM.CanvasRenderingContext3D.vertexAttrib3fv()"); + return vertexAttribAndUniformHelperf(args, kVertexAttrib3v); +} + +CALLBACK_FUNC_DECL(CanvasRenderingContext3DVertexAttrib4fv) +{ + INC_STATS("DOM.CanvasRenderingContext3D.vertexAttrib4fv()"); + return vertexAttribAndUniformHelperf(args, kVertexAttrib4v); +} + +} // namespace WebCore + +#endif // ENABLE(3D_CANVAS) diff --git a/WebCore/bindings/v8/custom/V8CanvasShortArrayCustom.cpp b/WebCore/bindings/v8/custom/V8CanvasShortArrayCustom.cpp new file mode 100644 index 0000000..8b83022 --- /dev/null +++ b/WebCore/bindings/v8/custom/V8CanvasShortArrayCustom.cpp @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2009 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" + +#if ENABLE(3D_CANVAS) + +#include "CanvasArrayBuffer.h" +#include "CanvasShortArray.h" + +#include "V8Binding.h" +#include "V8CanvasArrayBuffer.h" +#include "V8CanvasArrayCustom.h" +#include "V8CustomBinding.h" +#include "V8Proxy.h" + +namespace WebCore { + +CALLBACK_FUNC_DECL(CanvasShortArrayConstructor) +{ + INC_STATS("DOM.CanvasShortArray.Contructor"); + + return constructCanvasArray<CanvasShortArray>(args, V8ClassIndex::ToInt(V8ClassIndex::CANVASSHORTARRAY)); +} + +// Get the specified value from the array and return it wrapped as a JavaScript Number object to V8. Accesses outside the valid array range return "undefined". +INDEXED_PROPERTY_GETTER(CanvasShortArray) +{ + INC_STATS("DOM.CanvasShortArray.IndexedPropertyGetter"); + CanvasShortArray* array = V8DOMWrapper::convertToNativeObject<CanvasShortArray>(V8ClassIndex::CANVASSHORTARRAY, info.Holder()); + + if ((index < 0) || (index >= array->length())) + return v8::Undefined(); + short result; + if (!array->get(index, result)) + return v8::Undefined(); + return v8::Number::New(result); +} + +// Set the specified value in the array. Accesses outside the valid array range are silently ignored. +INDEXED_PROPERTY_SETTER(CanvasShortArray) +{ + INC_STATS("DOM.CanvasShortArray.IndexedPropertySetter"); + CanvasShortArray* array = V8DOMWrapper::convertToNativeObject<CanvasShortArray>(V8ClassIndex::CANVASSHORTARRAY, info.Holder()); + + if ((index >= 0) && (index < array->length())) { + if (!value->IsNumber()) + return throwError("Could not convert value argument to a number"); + array->set(index, value->NumberValue()); + } + return value; +} + +} // namespace WebCore + +#endif // ENABLE(3D_CANVAS) diff --git a/WebCore/bindings/v8/custom/V8CanvasUnsignedByteArrayCustom.cpp b/WebCore/bindings/v8/custom/V8CanvasUnsignedByteArrayCustom.cpp new file mode 100644 index 0000000..62ab880 --- /dev/null +++ b/WebCore/bindings/v8/custom/V8CanvasUnsignedByteArrayCustom.cpp @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2009 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" + +#if ENABLE(3D_CANVAS) + +#include "CanvasArrayBuffer.h" +#include "CanvasUnsignedByteArray.h" + +#include "V8Binding.h" +#include "V8CanvasArrayBuffer.h" +#include "V8CanvasArrayCustom.h" +#include "V8CustomBinding.h" +#include "V8Proxy.h" + +namespace WebCore { + +CALLBACK_FUNC_DECL(CanvasUnsignedByteArrayConstructor) +{ + INC_STATS("DOM.CanvasUnsignedByteArray.Contructor"); + + return constructCanvasArray<CanvasUnsignedByteArray>(args, V8ClassIndex::ToInt(V8ClassIndex::CANVASUNSIGNEDBYTEARRAY)); +} + +// Get the specified value from the array and return it wrapped as a JavaScript Number object to V8. Accesses outside the valid array range return "undefined". +INDEXED_PROPERTY_GETTER(CanvasUnsignedByteArray) +{ + INC_STATS("DOM.CanvasUnsignedByteArray.IndexedPropertyGetter"); + CanvasUnsignedByteArray* array = V8DOMWrapper::convertToNativeObject<CanvasUnsignedByteArray>(V8ClassIndex::CANVASUNSIGNEDBYTEARRAY, info.Holder()); + + if ((index < 0) || (index >= array->length())) + return v8::Undefined(); + unsigned char result; + if (!array->get(index, result)) + return v8::Undefined(); + return v8::Number::New(result); +} + +// Set the specified value in the array. Accesses outside the valid array range are silently ignored. +INDEXED_PROPERTY_SETTER(CanvasUnsignedByteArray) +{ + INC_STATS("DOM.CanvasUnsignedByteArray.IndexedPropertySetter"); + CanvasUnsignedByteArray* array = V8DOMWrapper::convertToNativeObject<CanvasUnsignedByteArray>(V8ClassIndex::CANVASUNSIGNEDBYTEARRAY, info.Holder()); + + if ((index >= 0) && (index < array->length())) { + if (!value->IsNumber()) + return throwError("Could not convert value argument to a number"); + array->set(index, value->NumberValue()); + } + return value; +} + +} // namespace WebCore + +#endif // ENABLE(3D_CANVAS) diff --git a/WebCore/bindings/v8/custom/V8CanvasUnsignedIntArrayCustom.cpp b/WebCore/bindings/v8/custom/V8CanvasUnsignedIntArrayCustom.cpp new file mode 100644 index 0000000..94ec7d0 --- /dev/null +++ b/WebCore/bindings/v8/custom/V8CanvasUnsignedIntArrayCustom.cpp @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2009 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" + +#if ENABLE(3D_CANVAS) + +#include "CanvasArrayBuffer.h" +#include "CanvasUnsignedIntArray.h" + +#include "V8Binding.h" +#include "V8CanvasArrayBuffer.h" +#include "V8CanvasArrayCustom.h" +#include "V8CustomBinding.h" +#include "V8Proxy.h" + +namespace WebCore { + +CALLBACK_FUNC_DECL(CanvasUnsignedIntArrayConstructor) +{ + INC_STATS("DOM.CanvasUnsignedIntArray.Contructor"); + + return constructCanvasArray<CanvasUnsignedIntArray>(args, V8ClassIndex::ToInt(V8ClassIndex::CANVASUNSIGNEDINTARRAY)); +} + +// Get the specified value from the integer array and return it wrapped as a JavaScript Number object to V8. Accesses outside the valid pixel buffer range return "undefined". +INDEXED_PROPERTY_GETTER(CanvasUnsignedIntArray) +{ + INC_STATS("DOM.CanvasUnsignedIntArray.IndexedPropertyGetter"); + CanvasUnsignedIntArray* array = V8DOMWrapper::convertToNativeObject<CanvasUnsignedIntArray>(V8ClassIndex::CANVASUNSIGNEDINTARRAY, info.Holder()); + + if ((index < 0) || (index >= array->length())) + return v8::Undefined(); + unsigned int result; + if (!array->get(index, result)) + return v8::Undefined(); + return v8::Number::New(result); +} + +// Set the specified value in the integer array. Accesses outside the valid integer array range are silently ignored. +INDEXED_PROPERTY_SETTER(CanvasUnsignedIntArray) +{ + INC_STATS("DOM.CanvasUnsignedIntArray.IndexedPropertySetter"); + CanvasUnsignedIntArray* array = V8DOMWrapper::convertToNativeObject<CanvasUnsignedIntArray>(V8ClassIndex::CANVASUNSIGNEDINTARRAY, info.Holder()); + + if ((index >= 0) && (index < array->length())) { + if (!value->IsNumber()) + return throwError("Could not convert value argument to a number"); + array->set(index, value->NumberValue()); + } + return value; +} + +} // namespace WebCore + +#endif // ENABLE(3D_CANVAS) diff --git a/WebCore/bindings/v8/custom/V8CanvasUnsignedShortArrayCustom.cpp b/WebCore/bindings/v8/custom/V8CanvasUnsignedShortArrayCustom.cpp new file mode 100644 index 0000000..eeef82c --- /dev/null +++ b/WebCore/bindings/v8/custom/V8CanvasUnsignedShortArrayCustom.cpp @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2009 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" + +#if ENABLE(3D_CANVAS) + +#include "CanvasArrayBuffer.h" +#include "CanvasUnsignedShortArray.h" + +#include "V8Binding.h" +#include "V8CanvasArrayBuffer.h" +#include "V8CanvasArrayCustom.h" +#include "V8CustomBinding.h" +#include "V8Proxy.h" + +namespace WebCore { + +CALLBACK_FUNC_DECL(CanvasUnsignedShortArrayConstructor) +{ + INC_STATS("DOM.CanvasUnsignedShortArray.Contructor"); + + return constructCanvasArray<CanvasUnsignedShortArray>(args, V8ClassIndex::ToInt(V8ClassIndex::CANVASUNSIGNEDSHORTARRAY)); +} + +// Get the specified value from the array and return it wrapped as a JavaScript Number object to V8. Accesses outside the valid array range return "undefined". +INDEXED_PROPERTY_GETTER(CanvasUnsignedShortArray) +{ + INC_STATS("DOM.CanvasUnsignedShortArray.IndexedPropertyGetter"); + CanvasUnsignedShortArray* array = V8DOMWrapper::convertToNativeObject<CanvasUnsignedShortArray>(V8ClassIndex::CANVASUNSIGNEDSHORTARRAY, info.Holder()); + + if ((index < 0) || (index >= array->length())) + return v8::Undefined(); + unsigned short result; + if (!array->get(index, result)) + return v8::Undefined(); + return v8::Number::New(result); +} + +// Set the specified value in the array. Accesses outside the valid array range are silently ignored. +INDEXED_PROPERTY_SETTER(CanvasUnsignedShortArray) +{ + INC_STATS("DOM.CanvasUnsignedShortArray.IndexedPropertySetter"); + CanvasUnsignedShortArray* array = V8DOMWrapper::convertToNativeObject<CanvasUnsignedShortArray>(V8ClassIndex::CANVASUNSIGNEDSHORTARRAY, info.Holder()); + + if ((index >= 0) && (index < array->length())) { + if (!value->IsNumber()) + return throwError("Could not convert value argument to a number"); + array->set(index, value->NumberValue()); + } + return value; +} + +} // namespace WebCore + +#endif // ENABLE(3D_CANVAS) diff --git a/WebCore/bindings/v8/custom/V8CustomBinding.h b/WebCore/bindings/v8/custom/V8CustomBinding.h index 22b4a94..82ce1c1 100644 --- a/WebCore/bindings/v8/custom/V8CustomBinding.h +++ b/WebCore/bindings/v8/custom/V8CustomBinding.h @@ -135,6 +135,16 @@ namespace WebCore { static const int kAbstractWorkerInternalFieldCount = kDefaultWrapperInternalFieldCount + 1; #endif +#if ENABLE(NOTIFICATIONS) + static const int kNotificationRequestCacheIndex = kDefaultWrapperInternalFieldCount + 0; + static const int kNotificationInternalFieldCount = kDefaultWrapperInternalFieldCount + 1; +#endif + +#if ENABLE(SVG) + static const int kSVGElementInstanceEventListenerCacheIndex = kDefaultWrapperInternalFieldCount + 0; + static const int kSVGElementInstanceInternalFieldCount = kDefaultWrapperInternalFieldCount + 1; +#endif + static const int kDOMWindowConsoleIndex = kDefaultWrapperInternalFieldCount + 0; static const int kDOMWindowHistoryIndex = kDefaultWrapperInternalFieldCount + 1; static const int kDOMWindowLocationbarIndex = kDefaultWrapperInternalFieldCount + 2; @@ -148,7 +158,8 @@ namespace WebCore { static const int kDOMWindowToolbarIndex = kDefaultWrapperInternalFieldCount + 10; static const int kDOMWindowLocationIndex = kDefaultWrapperInternalFieldCount + 11; static const int kDOMWindowDOMSelectionIndex = kDefaultWrapperInternalFieldCount + 12; - static const int kDOMWindowInternalFieldCount = kDefaultWrapperInternalFieldCount + 13; + static const int kDOMWindowEventListenerCacheIndex = kDefaultWrapperInternalFieldCount + 13; + static const int kDOMWindowInternalFieldCount = kDefaultWrapperInternalFieldCount + 14; static const int kStyleSheetOwnerNodeIndex = kDefaultWrapperInternalFieldCount + 0; static const int kStyleSheetInternalFieldCount = kDefaultWrapperInternalFieldCount + 1; @@ -158,6 +169,11 @@ namespace WebCore { static const int kDOMApplicationCacheFieldCount = kDefaultWrapperInternalFieldCount + 1; #endif +#if ENABLE(WEB_SOCKETS) + static const int kWebSocketCacheIndex = kDefaultWrapperInternalFieldCount + 0; + static const int kWebSocketInternalFieldCount = kDefaultWrapperInternalFieldCount + 1; +#endif + #define DECLARE_PROPERTY_ACCESSOR_GETTER(NAME) \ static v8::Handle<v8::Value> v8##NAME##AccessorGetter( \ v8::Local<v8::String> name, const v8::AccessorInfo& info) @@ -218,11 +234,18 @@ namespace WebCore { DECLARE_PROPERTY_ACCESSOR(CanvasRenderingContext2DStrokeStyle); DECLARE_PROPERTY_ACCESSOR(CanvasRenderingContext2DFillStyle); - DECLARE_PROPERTY_ACCESSOR_GETTER(DOMWindowEvent); + DECLARE_PROPERTY_ACCESSOR(DOMWindowEvent); DECLARE_PROPERTY_ACCESSOR_GETTER(DOMWindowCrypto); DECLARE_PROPERTY_ACCESSOR_SETTER(DOMWindowLocation); DECLARE_PROPERTY_ACCESSOR_SETTER(DOMWindowOpener); +#if ENABLE(VIDEO) + DECLARE_PROPERTY_ACCESSOR_GETTER(DOMWindowAudio); +#endif + + DECLARE_PROPERTY_ACCESSOR_GETTER(DOMWindowImage); + DECLARE_PROPERTY_ACCESSOR_GETTER(DOMWindowOption); + DECLARE_PROPERTY_ACCESSOR(DocumentLocation); DECLARE_PROPERTY_ACCESSOR(DocumentImplementation); DECLARE_PROPERTY_ACCESSOR_GETTER(EventSrcElement); @@ -231,7 +254,6 @@ namespace WebCore { DECLARE_PROPERTY_ACCESSOR_GETTER(EventClipboardData); DECLARE_PROPERTY_ACCESSOR(DOMWindowEventHandler); - DECLARE_PROPERTY_ACCESSOR(NodeEventHandler); DECLARE_CALLBACK(HTMLCanvasElementGetContext); @@ -319,6 +341,38 @@ namespace WebCore { DECLARE_CALLBACK(CanvasRenderingContext2DStrokeText); DECLARE_CALLBACK(CanvasRenderingContext2DPutImageData); +#if ENABLE(3D_CANVAS) + DECLARE_CALLBACK(CanvasRenderingContext3DBufferData); + DECLARE_CALLBACK(CanvasRenderingContext3DBufferSubData); + DECLARE_CALLBACK(CanvasRenderingContext3DSizeof); + DECLARE_CALLBACK(CanvasRenderingContext3DTexImage2D); + DECLARE_CALLBACK(CanvasRenderingContext3DTexSubImage2D); + DECLARE_CALLBACK(CanvasRenderingContext3DUniform1fv); + DECLARE_CALLBACK(CanvasRenderingContext3DUniform1iv); + DECLARE_CALLBACK(CanvasRenderingContext3DUniform2fv); + DECLARE_CALLBACK(CanvasRenderingContext3DUniform2iv); + DECLARE_CALLBACK(CanvasRenderingContext3DUniform3fv); + DECLARE_CALLBACK(CanvasRenderingContext3DUniform3iv); + DECLARE_CALLBACK(CanvasRenderingContext3DUniform4fv); + DECLARE_CALLBACK(CanvasRenderingContext3DUniform4iv); + DECLARE_CALLBACK(CanvasRenderingContext3DUniformMatrix2fv); + DECLARE_CALLBACK(CanvasRenderingContext3DUniformMatrix3fv); + DECLARE_CALLBACK(CanvasRenderingContext3DUniformMatrix4fv); + DECLARE_CALLBACK(CanvasRenderingContext3DVertexAttrib1fv); + DECLARE_CALLBACK(CanvasRenderingContext3DVertexAttrib2fv); + DECLARE_CALLBACK(CanvasRenderingContext3DVertexAttrib3fv); + DECLARE_CALLBACK(CanvasRenderingContext3DVertexAttrib4fv); + + DECLARE_CALLBACK(CanvasArrayBufferConstructor); + DECLARE_CALLBACK(CanvasByteArrayConstructor); + DECLARE_CALLBACK(CanvasFloatArrayConstructor); + DECLARE_CALLBACK(CanvasIntArrayConstructor); + DECLARE_CALLBACK(CanvasShortArrayConstructor); + DECLARE_CALLBACK(CanvasUnsignedByteArrayConstructor); + DECLARE_CALLBACK(CanvasUnsignedIntArrayConstructor); + DECLARE_CALLBACK(CanvasUnsignedShortArrayConstructor); +#endif + DECLARE_PROPERTY_ACCESSOR_GETTER(ClipboardTypes); DECLARE_CALLBACK(ClipboardClearData); DECLARE_CALLBACK(ClipboardGetData); @@ -400,14 +454,22 @@ namespace WebCore { DECLARE_CALLBACK(InspectorBackendAddSourceToFrame); DECLARE_CALLBACK(InspectorBackendSearch); DECLARE_CALLBACK(InspectorBackendSetting); + DECLARE_CALLBACK(InspectorBackendDatabaseForId); DECLARE_CALLBACK(InspectorBackendInspectedWindow); DECLARE_CALLBACK(InspectorBackendSetSetting); DECLARE_CALLBACK(InspectorBackendCurrentCallFrame); DECLARE_CALLBACK(InspectorBackendDebuggerEnabled); DECLARE_CALLBACK(InspectorBackendPauseOnExceptions); DECLARE_CALLBACK(InspectorBackendProfilerEnabled); + DECLARE_CALLBACK(InspectorBackendNodeForId); + DECLARE_CALLBACK(InspectorBackendWrapObject); + DECLARE_CALLBACK(InspectorBackendUnwrapObject); + DECLARE_CALLBACK(InspectorBackendPushNodePathToFrontend); #if ENABLE(DATABASE) - DECLARE_CALLBACK(InspectorBackendDatabaseTableNames); + DECLARE_CALLBACK(InspectorBackendSelectDatabase); +#endif +#if ENABLE(DOM_STORAGE) + DECLARE_CALLBACK(InspectorBackendSelectDOMStorage); #endif DECLARE_CALLBACK(InspectorBackendWrapCallback); @@ -445,21 +507,52 @@ namespace WebCore { DECLARE_INDEXED_PROPERTY_SETTER(HTMLSelectElementCollection); DECLARE_NAMED_PROPERTY_GETTER(HTMLCollection); +#if ENABLE(3D_CANVAS) + DECLARE_INDEXED_PROPERTY_GETTER(CanvasByteArray); + DECLARE_INDEXED_PROPERTY_SETTER(CanvasByteArray); + + DECLARE_INDEXED_PROPERTY_GETTER(CanvasFloatArray); + DECLARE_INDEXED_PROPERTY_SETTER(CanvasFloatArray); + + DECLARE_INDEXED_PROPERTY_GETTER(CanvasIntArray); + DECLARE_INDEXED_PROPERTY_SETTER(CanvasIntArray); +#endif + DECLARE_INDEXED_PROPERTY_GETTER(CanvasPixelArray); DECLARE_INDEXED_PROPERTY_SETTER(CanvasPixelArray); +#if ENABLE(3D_CANVAS) + DECLARE_INDEXED_PROPERTY_GETTER(CanvasShortArray); + DECLARE_INDEXED_PROPERTY_SETTER(CanvasShortArray); + + DECLARE_INDEXED_PROPERTY_GETTER(CanvasUnsignedByteArray); + DECLARE_INDEXED_PROPERTY_SETTER(CanvasUnsignedByteArray); + + DECLARE_INDEXED_PROPERTY_GETTER(CanvasUnsignedIntArray); + DECLARE_INDEXED_PROPERTY_SETTER(CanvasUnsignedIntArray); + + DECLARE_INDEXED_PROPERTY_GETTER(CanvasUnsignedShortArray); + DECLARE_INDEXED_PROPERTY_SETTER(CanvasUnsignedShortArray); +#endif + + DECLARE_PROPERTY_ACCESSOR_GETTER(MessageEventPorts); + DECLARE_CALLBACK(MessageEventInitMessageEvent); + DECLARE_PROPERTY_ACCESSOR(MessagePortOnmessage); DECLARE_PROPERTY_ACCESSOR(MessagePortOnclose); - DECLARE_CALLBACK(MessagePortStartConversation); DECLARE_CALLBACK(MessagePortAddEventListener); + DECLARE_CALLBACK(MessagePortPostMessage); DECLARE_CALLBACK(MessagePortRemoveEventListener); + DECLARE_CALLBACK(MessagePortStartConversation); DECLARE_CALLBACK(DatabaseChangeVersion); DECLARE_CALLBACK(DatabaseTransaction); + DECLARE_CALLBACK(DatabaseReadTransaction); DECLARE_CALLBACK(SQLTransactionExecuteSql); DECLARE_CALLBACK(SQLResultSetRowListItem); DECLARE_INDEXED_PROPERTY_GETTER(ClientRectList); + DECLARE_INDEXED_PROPERTY_GETTER(FileList); #if ENABLE(DATAGRID) DECLARE_PROPERTY_ACCESSOR(HTMLDataGridElementDataSource); @@ -496,8 +589,10 @@ namespace WebCore { DECLARE_CALLBACK(AbstractWorkerRemoveEventListener); DECLARE_PROPERTY_ACCESSOR(DedicatedWorkerContextOnmessage); + DECLARE_CALLBACK(DedicatedWorkerContextPostMessage); DECLARE_PROPERTY_ACCESSOR(WorkerOnmessage); + DECLARE_CALLBACK(WorkerPostMessage); DECLARE_CALLBACK(WorkerConstructor); DECLARE_PROPERTY_ACCESSOR_GETTER(WorkerContextSelf); @@ -509,7 +604,17 @@ namespace WebCore { DECLARE_CALLBACK(WorkerContextClearInterval); DECLARE_CALLBACK(WorkerContextAddEventListener); DECLARE_CALLBACK(WorkerContextRemoveEventListener); -#endif +#endif // ENABLE(WORKERS) + +#if ENABLE(NOTIFICATIONS) + DECLARE_CALLBACK(NotificationCenterRequestPermission); + DECLARE_CALLBACK(NotificationCenterCreateNotification); + DECLARE_CALLBACK(NotificationCenterCreateHTMLNotification); + + DECLARE_CALLBACK(NotificationAddEventListener); + DECLARE_CALLBACK(NotificationRemoveEventListener); + DECLARE_PROPERTY_ACCESSOR(NotificationEventHandler); +#endif // ENABLE(NOTIFICATIONS) #if ENABLE(OFFLINE_WEB_APPLICATIONS) DECLARE_PROPERTY_ACCESSOR(DOMApplicationCacheEventHandler); @@ -528,6 +633,15 @@ namespace WebCore { DECLARE_PROPERTY_ACCESSOR_GETTER(CoordinatesHeading); DECLARE_PROPERTY_ACCESSOR_GETTER(CoordinatesSpeed); +#if ENABLE(WEB_SOCKETS) + DECLARE_PROPERTY_ACCESSOR(WebSocketOnopen); + DECLARE_PROPERTY_ACCESSOR(WebSocketOnmessage); + DECLARE_PROPERTY_ACCESSOR(WebSocketOnclose); + DECLARE_CALLBACK(WebSocketConstructor); + DECLARE_CALLBACK(WebSocketSend); + DECLARE_CALLBACK(WebSocketClose); +#endif + #undef DECLARE_INDEXED_ACCESS_CHECK #undef DECLARE_NAMED_ACCESS_CHECK diff --git a/WebCore/bindings/v8/custom/V8CustomEventListener.cpp b/WebCore/bindings/v8/custom/V8CustomEventListener.cpp index 305da8d..91abecd 100644 --- a/WebCore/bindings/v8/custom/V8CustomEventListener.cpp +++ b/WebCore/bindings/v8/custom/V8CustomEventListener.cpp @@ -35,37 +35,25 @@ namespace WebCore { -V8EventListener::V8EventListener(Frame* frame, v8::Local<v8::Object> listener, bool isAttribute) - : V8AbstractEventListener(frame, isAttribute) +V8EventListener::V8EventListener(Frame* frame, PassRefPtr<V8ListenerGuard> guard, v8::Local<v8::Object> listener, bool isAttribute) + : V8AbstractEventListener(frame, guard, isAttribute) { - m_listener = v8::Persistent<v8::Object>::New(listener); -#ifndef NDEBUG - V8GCController::registerGlobalHandle(EVENT_LISTENER, this, m_listener); -#endif -} - -V8EventListener::~V8EventListener() -{ - if (m_frame) { - V8Proxy* proxy = V8Proxy::retrieve(m_frame); - if (proxy) - proxy->eventListeners()->remove(this); - } - - disposeListenerObject(); + setListenerObject(listener); } v8::Local<v8::Function> V8EventListener::getListenerFunction() { + v8::Local<v8::Object> listener = getListenerObject(); + // Has the listener been disposed? - if (m_listener.IsEmpty()) + if (listener.IsEmpty()) return v8::Local<v8::Function>(); - if (m_listener->IsFunction()) - return v8::Local<v8::Function>::New(v8::Persistent<v8::Function>::Cast(m_listener)); + if (listener->IsFunction()) + return v8::Local<v8::Function>::Cast(listener); - if (m_listener->IsObject()) { - v8::Local<v8::Value> property = m_listener->Get(v8::String::NewSymbol("handleEvent")); + if (listener->IsObject()) { + v8::Local<v8::Value> property = listener->Get(v8::String::NewSymbol("handleEvent")); if (property->IsFunction()) return v8::Local<v8::Function>::Cast(property); } @@ -73,16 +61,16 @@ v8::Local<v8::Function> V8EventListener::getListenerFunction() return v8::Local<v8::Function>(); } -v8::Local<v8::Value> V8EventListener::callListenerFunction(v8::Handle<v8::Value> jsEvent, Event* event, bool isWindowEvent) +v8::Local<v8::Value> V8EventListener::callListenerFunction(v8::Handle<v8::Value> jsEvent, Event* event) { v8::Local<v8::Function> handlerFunction = getListenerFunction(); - v8::Local<v8::Object> receiver = getReceiverObject(event, isWindowEvent); + v8::Local<v8::Object> receiver = getReceiverObject(event); if (handlerFunction.IsEmpty() || receiver.IsEmpty()) return v8::Local<v8::Value>(); v8::Handle<v8::Value> parameters[1] = { jsEvent }; - V8Proxy* proxy = V8Proxy::retrieve(m_frame); + V8Proxy* proxy = V8Proxy::retrieve(frame()); if (!proxy) return v8::Local<v8::Value>(); return proxy->callFunction(handlerFunction, receiver, 1, parameters); diff --git a/WebCore/bindings/v8/custom/V8CustomEventListener.h b/WebCore/bindings/v8/custom/V8CustomEventListener.h index 20adf99..e34f24f 100644 --- a/WebCore/bindings/v8/custom/V8CustomEventListener.h +++ b/WebCore/bindings/v8/custom/V8CustomEventListener.h @@ -44,23 +44,19 @@ namespace WebCore { // that can handle the event. class V8EventListener : public V8AbstractEventListener { public: - static PassRefPtr<V8EventListener> create(Frame* frame, v8::Local<v8::Object> listener, bool isAttribute) + static PassRefPtr<V8EventListener> create(Frame* frame, PassRefPtr<V8ListenerGuard> guard, v8::Local<v8::Object> listener, bool isAttribute) { - return adoptRef(new V8EventListener(frame, listener, isAttribute)); + return adoptRef(new V8EventListener(frame, guard, listener, isAttribute)); } - // Detach the listener from its owner frame. - void disconnectFrame() { m_frame = 0; } - protected: - V8EventListener(Frame*, v8::Local<v8::Object> listener, bool isAttribute); - virtual ~V8EventListener(); + V8EventListener(Frame*, PassRefPtr<V8ListenerGuard>, v8::Local<v8::Object> listener, bool isAttribute); + v8::Local<v8::Function> getListenerFunction(); private: - virtual v8::Local<v8::Value> callListenerFunction(v8::Handle<v8::Value> jsEvent, Event*, bool isWindowEvent); - virtual bool virtualisAttribute() const { return m_isAttribute; } - }; + virtual v8::Local<v8::Value> callListenerFunction(v8::Handle<v8::Value> jsEvent, Event*); + }; } // namespace WebCore diff --git a/WebCore/bindings/v8/custom/V8DOMApplicationCacheCustom.cpp b/WebCore/bindings/v8/custom/V8DOMApplicationCacheCustom.cpp index 7a3bfe6..134de95 100644 --- a/WebCore/bindings/v8/custom/V8DOMApplicationCacheCustom.cpp +++ b/WebCore/bindings/v8/custom/V8DOMApplicationCacheCustom.cpp @@ -37,77 +37,19 @@ #include "V8Binding.h" #include "V8CustomBinding.h" #include "V8Document.h" -#include "V8ObjectEventListener.h" #include "V8Proxy.h" #include "V8Utilities.h" #include "WorkerContextExecutionProxy.h" namespace WebCore { -static const bool kFindOnly = true; -static const bool kFindOrCreate = false; - -static PassRefPtr<EventListener> argumentToEventListener(DOMApplicationCache* appcache, v8::Local<v8::Value> value, bool findOnly) -{ - V8Proxy* proxy = V8Proxy::retrieve(appcache->scriptExecutionContext()); - if (proxy) - return findOnly ? proxy->objectListeners()->findWrapper(value, false) - : proxy->objectListeners()->findOrCreateWrapper<V8ObjectEventListener>(proxy->frame(), value, false); - return 0; -} - -static v8::Local<v8::Object> eventListenerToV8Object(EventListener* listener) -{ - return (static_cast<V8ObjectEventListener*>(listener))->getListenerObject(); -} - -static inline ApplicationCacheHost::EventID toEventID(v8::Local<v8::String> value) -{ - String key = toWebCoreString(value); - ASSERT(key.startsWith("on")); - return DOMApplicationCache::toEventID(key.substring(2)); -} - -// Handles appcache.onfooevent attribute getting -ACCESSOR_GETTER(DOMApplicationCacheEventHandler) -{ - INC_STATS("DOMApplicationCache.onevent_getter"); - DOMApplicationCache* appcache = V8DOMWrapper::convertToNativeObject<DOMApplicationCache>(V8ClassIndex::DOMAPPLICATIONCACHE, info.Holder()); - EventListener* listener = appcache->getAttributeEventListener(toEventID(name)); - if (!listener) - return v8::Null(); - return eventListenerToV8Object(listener); -} - -// Handles appcache.onfooevent attribute setting -ACCESSOR_SETTER(DOMApplicationCacheEventHandler) -{ - INC_STATS("DOMApplicationCache.onevent_setter"); - DOMApplicationCache* appcache = V8DOMWrapper::convertToNativeObject<DOMApplicationCache>(V8ClassIndex::DOMAPPLICATIONCACHE, info.Holder()); - ApplicationCacheHost::EventID eventType = toEventID(name); - - if (EventListener* oldListener = appcache->getAttributeEventListener(eventType)) { - v8::Local<v8::Object> object = eventListenerToV8Object(oldListener); - removeHiddenDependency(info.Holder(), object, V8Custom::kDOMApplicationCacheCacheIndex); - appcache->clearAttributeEventListener(eventType); - } - - if (value->IsFunction()) { - RefPtr<EventListener> newListener = argumentToEventListener(appcache, value, kFindOrCreate); - if (newListener) { - createHiddenDependency(info.Holder(), value, V8Custom::kDOMApplicationCacheCacheIndex); - appcache->setAttributeEventListener(eventType, newListener); - } - } -} - // Handles appcache.addEventListner(name, func, capture) method calls CALLBACK_FUNC_DECL(DOMApplicationCacheAddEventListener) { INC_STATS("DOMApplicationCache.addEventListener()"); DOMApplicationCache* appcache = V8DOMWrapper::convertToNativeObject<DOMApplicationCache>(V8ClassIndex::DOMAPPLICATIONCACHE, args.Holder()); - RefPtr<EventListener> listener = argumentToEventListener(appcache, args[1], kFindOrCreate); + RefPtr<EventListener> listener = V8DOMWrapper::getEventListener(appcache, args[1], false, ListenerFindOrCreate); if (listener) { createHiddenDependency(args.Holder(), args[1], V8Custom::kDOMApplicationCacheCacheIndex); String eventType = toWebCoreString(args[0]); @@ -123,7 +65,7 @@ CALLBACK_FUNC_DECL(DOMApplicationCacheRemoveEventListener) INC_STATS("DOMApplicationCache.removeEventListener()"); DOMApplicationCache* appcache = V8DOMWrapper::convertToNativeObject<DOMApplicationCache>(V8ClassIndex::DOMAPPLICATIONCACHE, args.Holder()); - RefPtr<EventListener> listener = argumentToEventListener(appcache, args[1], kFindOnly); + RefPtr<EventListener> listener = V8DOMWrapper::getEventListener(appcache, args[1], false, ListenerFindOnly); if (listener) { removeHiddenDependency(args.Holder(), args[1], V8Custom::kDOMApplicationCacheCacheIndex); String eventType = toWebCoreString(args[0]); diff --git a/WebCore/bindings/v8/custom/V8DOMWindowCustom.cpp b/WebCore/bindings/v8/custom/V8DOMWindowCustom.cpp index 7d0b9e6..0dc5a96 100644 --- a/WebCore/bindings/v8/custom/V8DOMWindowCustom.cpp +++ b/WebCore/bindings/v8/custom/V8DOMWindowCustom.cpp @@ -34,6 +34,7 @@ #include "V8Binding.h" #include "V8CustomBinding.h" #include "V8CustomEventListener.h" +#include "V8MessagePortCustom.h" #include "V8Proxy.h" #include "V8Utilities.h" @@ -48,6 +49,7 @@ #include "PlatformScreen.h" #include "ScheduledAction.h" #include "ScriptSourceCode.h" +#include "SerializedScriptValue.h" #include "Settings.h" #include "WindowFeatures.h" @@ -64,32 +66,44 @@ v8::Handle<v8::Value> V8Custom::WindowSetTimeoutImpl(const v8::Arguments& args, if (argumentCount < 1) return v8::Undefined(); - DOMWindow* imp = V8DOMWrapper::convertToNativeObject<DOMWindow>(V8ClassIndex::DOMWINDOW, args.Holder()); - - if (!imp->frame()) - return v8::Undefined(); - - if (!V8Proxy::canAccessFrame(imp->frame(), true)) - return v8::Undefined(); + v8::Handle<v8::Value> function = args[0]; - ScriptExecutionContext* scriptContext = static_cast<ScriptExecutionContext*>(imp->frame()->document()); + WebCore::String functionString; + if (!function->IsFunction()) { + if (function->IsString()) + functionString = toWebCoreString(function); + else { + v8::Handle<v8::Value> v8String = function->ToString(); - v8::Handle<v8::Value> function = args[0]; + // Bail out if string conversion failed. + if (v8String.IsEmpty()) + return v8::Undefined(); - int32_t timeout = 0; - if (argumentCount >= 2) - timeout = args[1]->Int32Value(); + functionString = toWebCoreString(v8String); + } - int id; - if (function->IsString()) { // Don't allow setting timeouts to run empty functions! // (Bug 1009597) - WebCore::String functionString = toWebCoreString(function); if (functionString.length() == 0) return v8::Undefined(); + } + + int32_t timeout = 0; + if (argumentCount >= 2) + timeout = args[1]->Int32Value(); + + DOMWindow* imp = V8DOMWrapper::convertToNativeObject<DOMWindow>(V8ClassIndex::DOMWINDOW, args.Holder()); + + if (!V8Proxy::canAccessFrame(imp->frame(), true)) + return v8::Undefined(); - id = DOMTimer::install(scriptContext, new ScheduledAction(functionString), timeout, singleShot); - } else if (function->IsFunction()) { + ScriptExecutionContext* scriptContext = static_cast<ScriptExecutionContext*>(imp->document()); + + if (!scriptContext) + return v8::Undefined(); + + int id; + if (function->IsFunction()) { int paramCount = argumentCount >= 2 ? argumentCount - 2 : 0; v8::Local<v8::Value>* params = 0; if (paramCount > 0) { @@ -100,14 +114,14 @@ v8::Handle<v8::Value> V8Custom::WindowSetTimeoutImpl(const v8::Arguments& args, } // params is passed to action, and released in action's destructor - ScheduledAction* action = new ScheduledAction(v8::Handle<v8::Function>::Cast(function), paramCount, params); + ScheduledAction* action = new ScheduledAction(V8Proxy::context(imp->frame()), v8::Handle<v8::Function>::Cast(function), paramCount, params); delete[] params; id = DOMTimer::install(scriptContext, action, timeout, singleShot); - } else - // FIXME(fqian): what's the right return value if failed. - return v8::Undefined(); + } else { + id = DOMTimer::install(scriptContext, new ScheduledAction(V8Proxy::context(imp->frame()), functionString), timeout, singleShot); + } return v8::Integer::New(id); } @@ -145,14 +159,37 @@ static v8::Handle<v8::Value> convertBase64(const String& str, bool encode) ACCESSOR_GETTER(DOMWindowEvent) { + v8::Handle<v8::Object> holder = V8DOMWrapper::lookupDOMWrapper(V8ClassIndex::DOMWINDOW, info.This()); + if (holder.IsEmpty()) + return v8::Undefined(); + + Frame* frame = V8DOMWrapper::convertToNativeObject<DOMWindow>(V8ClassIndex::DOMWINDOW, holder)->frame(); + if (!V8Proxy::canAccessFrame(frame, true)) + return v8::Undefined(); + + v8::Local<v8::Context> context = V8Proxy::context(frame); v8::Local<v8::String> eventSymbol = v8::String::NewSymbol("event"); - v8::Local<v8::Context> context = v8::Context::GetCurrent(); v8::Handle<v8::Value> jsEvent = context->Global()->GetHiddenValue(eventSymbol); if (jsEvent.IsEmpty()) return v8::Undefined(); return jsEvent; } +ACCESSOR_SETTER(DOMWindowEvent) +{ + v8::Handle<v8::Object> holder = V8DOMWrapper::lookupDOMWrapper(V8ClassIndex::DOMWINDOW, info.This()); + if (holder.IsEmpty()) + return; + + Frame* frame = V8DOMWrapper::convertToNativeObject<DOMWindow>(V8ClassIndex::DOMWINDOW, holder)->frame(); + if (!V8Proxy::canAccessFrame(frame, true)) + return; + + v8::Local<v8::Context> context = V8Proxy::context(frame); + v8::Local<v8::String> eventSymbol = v8::String::NewSymbol("event"); + context->Global()->SetHiddenValue(eventSymbol, value); +} + ACCESSOR_GETTER(DOMWindowCrypto) { // FIXME: Implement me. @@ -161,11 +198,7 @@ ACCESSOR_GETTER(DOMWindowCrypto) ACCESSOR_SETTER(DOMWindowLocation) { - v8::Handle<v8::Object> holder = V8DOMWrapper::lookupDOMWrapper(V8ClassIndex::DOMWINDOW, info.This()); - if (holder.IsEmpty()) - return; - - DOMWindow* imp = V8DOMWrapper::convertToNativeObject<DOMWindow>(V8ClassIndex::DOMWINDOW, holder); + DOMWindow* imp = V8DOMWrapper::convertToNativeObject<DOMWindow>(V8ClassIndex::DOMWINDOW, info.Holder()); WindowSetLocation(imp, toWebCoreString(value)); } @@ -194,18 +227,42 @@ ACCESSOR_SETTER(DOMWindowOpener) info.This()->Set(name, value); } +#if ENABLE(VIDEO) + +ACCESSOR_GETTER(DOMWindowAudio) +{ + DOMWindow* window = V8DOMWrapper::convertToNativeObject<DOMWindow>(V8ClassIndex::DOMWINDOW, info.Holder()); + return V8DOMWrapper::getConstructor(V8ClassIndex::AUDIO, window); +} + +#endif + +ACCESSOR_GETTER(DOMWindowImage) +{ + DOMWindow* window = V8DOMWrapper::convertToNativeObject<DOMWindow>(V8ClassIndex::DOMWINDOW, info.Holder()); + return V8DOMWrapper::getConstructor(V8ClassIndex::IMAGE, window); +} + +ACCESSOR_GETTER(DOMWindowOption) +{ + DOMWindow* window = V8DOMWrapper::convertToNativeObject<DOMWindow>(V8ClassIndex::DOMWINDOW, info.Holder()); + return V8DOMWrapper::getConstructor(V8ClassIndex::OPTION, window); +} + CALLBACK_FUNC_DECL(DOMWindowAddEventListener) { INC_STATS("DOM.DOMWindow.addEventListener()"); + + String eventType = toWebCoreString(args[0]); + bool useCapture = args[2]->BooleanValue(); + DOMWindow* imp = V8DOMWrapper::convertToNativeObject<DOMWindow>(V8ClassIndex::DOMWINDOW, args.Holder()); if (!V8Proxy::canAccessFrame(imp->frame(), true)) return v8::Undefined(); - if (!imp->frame()) - return v8::Undefined(); // DOMWindow could be disconnected from the frame - - Document* doc = imp->frame()->document(); + Document* doc = imp->document(); + if (!doc) return v8::Undefined(); @@ -214,12 +271,11 @@ CALLBACK_FUNC_DECL(DOMWindowAddEventListener) if (!proxy) return v8::Undefined(); - RefPtr<EventListener> listener = proxy->eventListeners()->findOrCreateWrapper<V8EventListener>(proxy->frame(), args[1], false); + RefPtr<EventListener> listener = V8DOMWrapper::getEventListener(proxy, args[1], false, ListenerFindOrCreate); if (listener) { - String eventType = toWebCoreString(args[0]); - bool useCapture = args[2]->BooleanValue(); imp->addEventListener(eventType, listener, useCapture); + createHiddenDependency(args.Holder(), args[1], V8Custom::kDOMWindowEventListenerCacheIndex); } return v8::Undefined(); @@ -229,15 +285,17 @@ CALLBACK_FUNC_DECL(DOMWindowAddEventListener) CALLBACK_FUNC_DECL(DOMWindowRemoveEventListener) { INC_STATS("DOM.DOMWindow.removeEventListener()"); + + String eventType = toWebCoreString(args[0]); + bool useCapture = args[2]->BooleanValue(); + DOMWindow* imp = V8DOMWrapper::convertToNativeObject<DOMWindow>(V8ClassIndex::DOMWINDOW, args.Holder()); if (!V8Proxy::canAccessFrame(imp->frame(), true)) return v8::Undefined(); - if (!imp->frame()) - return v8::Undefined(); + Document* doc = imp->document(); - Document* doc = imp->frame()->document(); if (!doc) return v8::Undefined(); @@ -245,12 +303,11 @@ CALLBACK_FUNC_DECL(DOMWindowRemoveEventListener) if (!proxy) return v8::Undefined(); - RefPtr<EventListener> listener = proxy->eventListeners()->findWrapper(args[1], false); + RefPtr<EventListener> listener = V8DOMWrapper::getEventListener(proxy, args[1], false, ListenerFindOnly); if (listener) { - String eventType = toWebCoreString(args[0]); - bool useCapture = args[2]->BooleanValue(); imp->removeEventListener(eventType, listener.get(), useCapture); + removeHiddenDependency(args.Holder(), args[1], V8Custom::kDOMWindowEventListenerCacheIndex); } return v8::Undefined(); @@ -265,8 +322,8 @@ CALLBACK_FUNC_DECL(DOMWindowPostMessage) ASSERT(source->frame()); v8::TryCatch tryCatch; - String message = toWebCoreString(args[0]); - MessagePort* port = 0; + RefPtr<SerializedScriptValue> message = SerializedScriptValue::create(toWebCoreString(args[0])); + MessagePortArray portArray; String targetOrigin; // This function has variable arguments and can either be: @@ -274,8 +331,8 @@ CALLBACK_FUNC_DECL(DOMWindowPostMessage) // or // postMessage(message, targetOrigin); if (args.Length() > 2) { - if (V8DOMWrapper::isWrapperOfType(args[1], V8ClassIndex::MESSAGEPORT)) - port = V8DOMWrapper::convertToNativeObject<MessagePort>(V8ClassIndex::MESSAGEPORT, v8::Handle<v8::Object>::Cast(args[1])); + if (!getMessagePortArray(args[1], portArray)) + return v8::Undefined(); targetOrigin = toWebCoreStringWithNullOrUndefinedCheck(args[2]); } else { targetOrigin = toWebCoreStringWithNullOrUndefinedCheck(args[1]); @@ -285,16 +342,18 @@ CALLBACK_FUNC_DECL(DOMWindowPostMessage) return v8::Undefined(); ExceptionCode ec = 0; - window->postMessage(message, port, targetOrigin, source, ec); - if (ec) - V8Proxy::setDOMException(ec); - - return v8::Undefined(); + window->postMessage(message.release(), &portArray, targetOrigin, source, ec); + return throwError(ec); } CALLBACK_FUNC_DECL(DOMWindowAtob) { INC_STATS("DOM.DOMWindow.atob()"); + + if (args[0]->IsNull()) + return v8String(""); + String str = toWebCoreString(args[0]); + DOMWindow* imp = V8DOMWrapper::convertToNativeObject<DOMWindow>(V8ClassIndex::DOMWINDOW, args.Holder()); if (!V8Proxy::canAccessFrame(imp->frame(), true)) @@ -303,16 +362,17 @@ CALLBACK_FUNC_DECL(DOMWindowAtob) if (args.Length() < 1) return throwError("Not enough arguments", V8Proxy::SyntaxError); - if (args[0]->IsNull()) - return v8String(""); - - String str = toWebCoreString(args[0]); return convertBase64(str, false); } CALLBACK_FUNC_DECL(DOMWindowBtoa) { INC_STATS("DOM.DOMWindow.btoa()"); + + if (args[0]->IsNull()) + return v8String(""); + String str = toWebCoreString(args[0]); + DOMWindow* imp = V8DOMWrapper::convertToNativeObject<DOMWindow>(V8ClassIndex::DOMWINDOW, args.Holder()); if (!V8Proxy::canAccessFrame(imp->frame(), true)) @@ -321,10 +381,6 @@ CALLBACK_FUNC_DECL(DOMWindowBtoa) if (args.Length() < 1) return throwError("Not enough arguments", V8Proxy::SyntaxError); - if (args[0]->IsNull()) - return v8String(""); - - String str = toWebCoreString(args[0]); return convertBase64(str, true); } @@ -335,7 +391,10 @@ CALLBACK_FUNC_DECL(DOMWindowBtoa) CALLBACK_FUNC_DECL(DOMWindowToString) { INC_STATS("DOM.DOMWindow.toString()"); - return args.This()->ObjectProtoToString(); + v8::Handle<v8::Object> domWrapper = V8DOMWrapper::lookupDOMWrapper(V8ClassIndex::DOMWINDOW, args.This()); + if (domWrapper.IsEmpty()) + return args.This()->ObjectProtoToString(); + return domWrapper->ObjectProtoToString(); } CALLBACK_FUNC_DECL(DOMWindowNOP) @@ -344,84 +403,6 @@ CALLBACK_FUNC_DECL(DOMWindowNOP) return v8::Undefined(); } -static String eventNameFromAttributeName(const String& name) -{ - ASSERT(name.startsWith("on")); - String eventType = name.substring(2); - - if (eventType.startsWith("w")) { - switch(eventType[eventType.length() - 1]) { - case 't': - eventType = "webkitAnimationStart"; - break; - case 'n': - eventType = "webkitAnimationIteration"; - break; - case 'd': - ASSERT(eventType.length() > 7); - if (eventType[7] == 'a') - eventType = "webkitAnimationEnd"; - else - eventType = "webkitTransitionEnd"; - break; - } - } - - return eventType; -} - -ACCESSOR_SETTER(DOMWindowEventHandler) -{ - v8::Handle<v8::Object> holder = V8DOMWrapper::lookupDOMWrapper(V8ClassIndex::DOMWINDOW, info.This()); - if (holder.IsEmpty()) - return; - - DOMWindow* imp = V8DOMWrapper::convertToNativeObject<DOMWindow>(V8ClassIndex::DOMWINDOW, holder); - if (!imp->frame()) - return; - - Document* doc = imp->frame()->document(); - if (!doc) - return; - - String key = toWebCoreString(name); - String eventType = eventNameFromAttributeName(key); - - if (value->IsNull()) { - // Clear the event listener - imp->clearAttributeEventListener(eventType); - } else { - V8Proxy* proxy = V8Proxy::retrieve(imp->frame()); - if (!proxy) - return; - - RefPtr<EventListener> listener = proxy->eventListeners()->findOrCreateWrapper<V8EventListener>(proxy->frame(), value, true); - if (listener) - imp->setAttributeEventListener(eventType, listener); - } -} - -ACCESSOR_GETTER(DOMWindowEventHandler) -{ - v8::Handle<v8::Object> holder = V8DOMWrapper::lookupDOMWrapper(V8ClassIndex::DOMWINDOW, info.This()); - if (holder.IsEmpty()) - return v8::Undefined(); - - DOMWindow* imp = V8DOMWrapper::convertToNativeObject<DOMWindow>(V8ClassIndex::DOMWINDOW, holder); - if (!imp->frame()) - return v8::Undefined(); - - Document* doc = imp->frame()->document(); - if (!doc) - return v8::Undefined(); - - String key = toWebCoreString(name); - String eventType = eventNameFromAttributeName(key); - - EventListener* listener = imp->getAttributeEventListener(eventType); - return V8DOMWrapper::convertEventListenerToV8Object(listener); -} - static bool canShowModalDialogNow(const Frame* frame) { // A frame can out live its page. See bug 1219613. @@ -512,7 +493,7 @@ static Frame* createWindow(Frame* callingFrame, return 0; newFrame->loader()->setOpener(openerFrame); - newFrame->loader()->setOpenedByDOM(); + newFrame->page()->setOpenedByDOM(); // Set dialog arguments on the global object of the new frame. if (!dialogArgs.IsEmpty()) { @@ -525,13 +506,13 @@ static Frame* createWindow(Frame* callingFrame, if (protocolIsJavaScript(url) || ScriptController::isSafeScript(newFrame)) { KURL completedUrl = - url.isEmpty() ? KURL("") : completeURL(url); + url.isEmpty() ? KURL(ParsedURLString, "") : completeURL(url); bool userGesture = processingUserGesture(); if (created) newFrame->loader()->changeLocation(completedUrl, referrer, false, false, userGesture); else if (!url.isEmpty()) - newFrame->loader()->scheduleLocationChange(completedUrl.string(), referrer, false, userGesture); + newFrame->redirectScheduler()->scheduleLocationChange(completedUrl.string(), referrer, false, userGesture); } return newFrame; @@ -542,11 +523,16 @@ static Frame* createWindow(Frame* callingFrame, CALLBACK_FUNC_DECL(DOMWindowShowModalDialog) { INC_STATS("DOM.DOMWindow.showModalDialog()"); + + String url = toWebCoreStringWithNullOrUndefinedCheck(args[0]); + v8::Local<v8::Value> dialogArgs = args[1]; + String featureArgs = toWebCoreStringWithNullOrUndefinedCheck(args[2]); + DOMWindow* window = V8DOMWrapper::convertToNativeObject<DOMWindow>( V8ClassIndex::DOMWINDOW, args.Holder()); Frame* frame = window->frame(); - if (!frame || !V8Proxy::canAccessFrame(frame, true)) + if (!V8Proxy::canAccessFrame(frame, true)) return v8::Undefined(); Frame* callingFrame = V8Proxy::retrieveFrameForCallingContext(); @@ -560,10 +546,6 @@ CALLBACK_FUNC_DECL(DOMWindowShowModalDialog) if (!canShowModalDialogNow(frame) || !allowPopUp()) return v8::Undefined(); - String url = toWebCoreStringWithNullOrUndefinedCheck(args[0]); - v8::Local<v8::Value> dialogArgs = args[1]; - String featureArgs = toWebCoreStringWithNullOrUndefinedCheck(args[2]); - const HashMap<String, String> features = parseModalDialogFeatures(featureArgs); const bool trusted = false; @@ -631,10 +613,14 @@ CALLBACK_FUNC_DECL(DOMWindowShowModalDialog) CALLBACK_FUNC_DECL(DOMWindowOpen) { INC_STATS("DOM.DOMWindow.open()"); + + String urlString = toWebCoreStringWithNullOrUndefinedCheck(args[0]); + AtomicString frameName = (args[1]->IsUndefined() || args[1]->IsNull()) ? "_blank" : AtomicString(toWebCoreString(args[1])); + DOMWindow* parent = V8DOMWrapper::convertToNativeObject<DOMWindow>(V8ClassIndex::DOMWINDOW, args.Holder()); Frame* frame = parent->frame(); - if (!frame || !V8Proxy::canAccessFrame(frame, true)) + if (!V8Proxy::canAccessFrame(frame, true)) return v8::Undefined(); Frame* callingFrame = V8Proxy::retrieveFrameForCallingContext(); @@ -649,9 +635,6 @@ CALLBACK_FUNC_DECL(DOMWindowOpen) if (!page) return v8::Undefined(); - String urlString = toWebCoreStringWithNullOrUndefinedCheck(args[0]); - AtomicString frameName = (args[1]->IsUndefined() || args[1]->IsNull()) ? "_blank" : AtomicString(toWebCoreString(args[1])); - // Because FrameTree::find() returns true for empty strings, we must check // for empty framenames. Otherwise, illegitimate window.open() calls with // no name will pass right through the popup blocker. @@ -687,7 +670,7 @@ CALLBACK_FUNC_DECL(DOMWindowOpen) // the outgoingReferrer. We replicate that behavior here. String referrer = enteredFrame->loader()->outgoingReferrer(); - frame->loader()->scheduleLocationChange(completedUrl, referrer, false, userGesture); + frame->redirectScheduler()->scheduleLocationChange(completedUrl, referrer, false, userGesture); } return V8DOMWrapper::convertToV8Object(V8ClassIndex::DOMWINDOW, frame->domWindow()); } @@ -756,11 +739,8 @@ CALLBACK_FUNC_DECL(DOMWindowOpen) INDEXED_PROPERTY_GETTER(DOMWindow) { INC_STATS("DOM.DOMWindow.IndexedPropertyGetter"); - v8::Handle<v8::Object> holder = V8DOMWrapper::lookupDOMWrapper(V8ClassIndex::DOMWINDOW, info.This()); - if (holder.IsEmpty()) - return notHandledByInterceptor(); - DOMWindow* window = V8DOMWrapper::convertToNativeObject<DOMWindow>(V8ClassIndex::DOMWINDOW, holder); + DOMWindow* window = V8DOMWrapper::convertToNativeObject<DOMWindow>(V8ClassIndex::DOMWINDOW, info.Holder()); if (!window) return notHandledByInterceptor(); @@ -780,11 +760,8 @@ NAMED_PROPERTY_GETTER(DOMWindow) { INC_STATS("DOM.DOMWindow.NamedPropertyGetter"); - v8::Handle<v8::Object> holder = V8DOMWrapper::lookupDOMWrapper(V8ClassIndex::DOMWINDOW, info.This()); - if (holder.IsEmpty()) - return notHandledByInterceptor(); - - DOMWindow* window = V8DOMWrapper::convertToNativeObject<DOMWindow>(V8ClassIndex::DOMWINDOW, holder); + // TODO(antonm): investigate what convertToNativeObject does for the case of DOMWINDOW. + DOMWindow* window = V8DOMWrapper::convertToNativeObject<DOMWindow>(V8ClassIndex::DOMWINDOW, info.Holder()); if (!window) return notHandledByInterceptor(); @@ -800,12 +777,13 @@ NAMED_PROPERTY_GETTER(DOMWindow) return V8DOMWrapper::convertToV8Object(V8ClassIndex::DOMWINDOW, child->domWindow()); // Search IDL functions defined in the prototype - v8::Handle<v8::Value> result = holder->GetRealNamedPropertyInPrototypeChain(name); + v8::Handle<v8::Value> result = info.Holder()->GetRealNamedProperty(name); if (!result.IsEmpty()) return result; // Search named items in the document. Document* doc = frame->document(); + if (doc) { RefPtr<HTMLCollection> items = doc->windowNamedItems(propName); if (items->length() >= 1) { @@ -826,13 +804,13 @@ void V8Custom::WindowSetLocation(DOMWindow* window, const String& relativeURL) if (!frame) return; - if (!shouldAllowNavigation(frame)) - return; - KURL url = completeURL(relativeURL); if (url.isNull()) return; + if (!shouldAllowNavigation(frame)) + return; + navigateIfAllowed(frame, url, false, false); } @@ -853,12 +831,15 @@ CALLBACK_FUNC_DECL(DOMWindowSetInterval) void V8Custom::ClearTimeoutImpl(const v8::Arguments& args) { + int handle = toInt32(args[0]); + v8::Handle<v8::Object> holder = args.Holder(); DOMWindow* imp = V8DOMWrapper::convertToNativeObject<DOMWindow>(V8ClassIndex::DOMWINDOW, holder); if (!V8Proxy::canAccessFrame(imp->frame(), true)) return; - ScriptExecutionContext* context = static_cast<ScriptExecutionContext*>(imp->frame()->document()); - int handle = toInt32(args[0]); + ScriptExecutionContext* context = static_cast<ScriptExecutionContext*>(imp->document()); + if (!context) + return; DOMTimer::removeById(context, handle); } diff --git a/WebCore/bindings/v8/custom/V8DatabaseCustom.cpp b/WebCore/bindings/v8/custom/V8DatabaseCustom.cpp index 300e829..9ddd620 100644 --- a/WebCore/bindings/v8/custom/V8DatabaseCustom.cpp +++ b/WebCore/bindings/v8/custom/V8DatabaseCustom.cpp @@ -45,13 +45,50 @@ namespace WebCore { CALLBACK_FUNC_DECL(DatabaseChangeVersion) { INC_STATS("DOM.Database.changeVersion()"); + + if (args.Length() < 2) + return throwError("The old and new version strings are required.", V8Proxy::SyntaxError); + + if (!(args[0]->IsString() && args[1]->IsString())) + return throwError("The old and new versions must be strings."); + + Database* database = V8DOMWrapper::convertToNativeObject<Database>(V8ClassIndex::DATABASE, args.Holder()); + + Frame* frame = V8Proxy::retrieveFrameForCurrentContext(); + if (!frame) + return v8::Undefined(); + + RefPtr<V8CustomSQLTransactionCallback> callback; + if (args.Length() > 2) { + if (!args[2]->IsObject()) + return throwError("changeVersion transaction callback must be of valid type."); + + callback = V8CustomSQLTransactionCallback::create(args[2], frame); + } + + RefPtr<V8CustomSQLTransactionErrorCallback> errorCallback; + if (args.Length() > 3) { + if (!args[3]->IsObject()) + return throwError("changeVersion error callback must be of valid type."); + + errorCallback = V8CustomSQLTransactionErrorCallback::create(args[3], frame); + } + + RefPtr<V8CustomVoidCallback> successCallback; + if (args.Length() > 4) { + if (!args[4]->IsObject()) + return throwError("changeVersion success callback must be of valid type."); + + successCallback = V8CustomVoidCallback::create(args[4], frame); + } + + database->changeVersion(toWebCoreString(args[0]), toWebCoreString(args[1]), callback.release(), errorCallback.release(), successCallback.release()); + return v8::Undefined(); } -CALLBACK_FUNC_DECL(DatabaseTransaction) +static v8::Handle<v8::Value> createTransaction(const v8::Arguments& args, bool readOnly) { - INC_STATS("DOM.Database.transaction()"); - if (!args.Length()) return throwError("Transaction callback is required.", V8Proxy::SyntaxError); @@ -76,18 +113,28 @@ CALLBACK_FUNC_DECL(DatabaseTransaction) RefPtr<V8CustomVoidCallback> successCallback; if (args.Length() > 2) { - if (!args[1]->IsObject()) + if (!args[2]->IsObject()) return throwError("Transaction success callback must be of valid type."); successCallback = V8CustomVoidCallback::create(args[2], frame); } - database->transaction(callback.release(), errorCallback.release(), successCallback.release()); - + database->transaction(callback.release(), errorCallback.release(), successCallback.release(), readOnly); return v8::Undefined(); } +CALLBACK_FUNC_DECL(DatabaseTransaction) +{ + INC_STATS("DOM.Database.transaction()"); + return createTransaction(args, false); +} + +CALLBACK_FUNC_DECL(DatabaseReadTransaction) +{ + INC_STATS("DOM.Database.readTransaction()"); + return createTransaction(args, true); +} + } // namespace WebCore #endif - diff --git a/WebCore/bindings/v8/custom/V8DedicatedWorkerContextCustom.cpp b/WebCore/bindings/v8/custom/V8DedicatedWorkerContextCustom.cpp index f13e45e..263c005 100644 --- a/WebCore/bindings/v8/custom/V8DedicatedWorkerContextCustom.cpp +++ b/WebCore/bindings/v8/custom/V8DedicatedWorkerContextCustom.cpp @@ -35,48 +35,26 @@ #include "WorkerContextExecutionProxy.h" #include "DedicatedWorkerContext.h" +#include "V8Binding.h" +#include "V8MessagePortCustom.h" #include "V8Proxy.h" #include "V8WorkerContextEventListener.h" namespace WebCore { -ACCESSOR_GETTER(DedicatedWorkerContextOnmessage) +CALLBACK_FUNC_DECL(DedicatedWorkerContextPostMessage) { - INC_STATS(L"DOM.DedicatedWorkerContext.onmessage._get"); - DedicatedWorkerContext* workerContext = V8DOMWrapper::convertToNativeObject<DedicatedWorkerContext>(V8ClassIndex::DEDICATEDWORKERCONTEXT, info.Holder()); - if (workerContext->onmessage()) { - V8WorkerContextEventListener* listener = static_cast<V8WorkerContextEventListener*>(workerContext->onmessage()); - v8::Local<v8::Object> v8Listener = listener->getListenerObject(); - return v8Listener; - } - return v8::Undefined(); -} - -ACCESSOR_SETTER(DedicatedWorkerContextOnmessage) -{ - INC_STATS(L"DOM.DedicatedWorkerContext.onmessage._set"); - DedicatedWorkerContext* workerContext = V8DOMWrapper::convertToNativeObject<DedicatedWorkerContext>(V8ClassIndex::DEDICATEDWORKERCONTEXT, info.Holder()); - V8WorkerContextEventListener* oldListener = static_cast<V8WorkerContextEventListener*>(workerContext->onmessage()); - if (value->IsNull()) { - if (workerContext->onmessage()) { - v8::Local<v8::Object> oldV8Listener = oldListener->getListenerObject(); - removeHiddenDependency(info.Holder(), oldV8Listener, V8Custom::kDedicatedWorkerContextRequestCacheIndex); - } - - // Clear the listener. - workerContext->setOnmessage(0); - } else { - RefPtr<V8EventListener> listener = workerContext->script()->proxy()->findOrCreateEventListener(v8::Local<v8::Object>::Cast(value), false, false); - if (listener) { - if (oldListener) { - v8::Local<v8::Object> oldV8Listener = oldListener->getListenerObject(); - removeHiddenDependency(info.Holder(), oldV8Listener, V8Custom::kDedicatedWorkerContextRequestCacheIndex); - } - - workerContext->setOnmessage(listener); - createHiddenDependency(info.Holder(), value, V8Custom::kDedicatedWorkerContextRequestCacheIndex); - } + INC_STATS(L"DOM.DedicatedWorkerContext.postMessage"); + DedicatedWorkerContext* workerContext = V8DOMWrapper::convertToNativeObject<DedicatedWorkerContext>(V8ClassIndex::DEDICATEDWORKERCONTEXT, args.Holder()); + RefPtr<SerializedScriptValue> message = SerializedScriptValue::create(v8ValueToWebCoreString(args[0])); + MessagePortArray portArray; + if (args.Length() > 1) { + if (!getMessagePortArray(args[1], portArray)) + return v8::Undefined(); } + ExceptionCode ec = 0; + workerContext->postMessage(message.release(), &portArray, ec); + return throwError(ec); } } // namespace WebCore diff --git a/WebCore/bindings/v8/custom/V8DocumentCustom.cpp b/WebCore/bindings/v8/custom/V8DocumentCustom.cpp index 80d204b..8b77a3a 100644 --- a/WebCore/bindings/v8/custom/V8DocumentCustom.cpp +++ b/WebCore/bindings/v8/custom/V8DocumentCustom.cpp @@ -31,10 +31,12 @@ #include "config.h" #include "Document.h" +#include "CanvasRenderingContext.h" #include "ExceptionCode.h" #include "Node.h" #include "XPathNSResolver.h" #include "XPathResult.h" +#include "CanvasRenderingContext.h" #include "V8Binding.h" #include "V8CustomBinding.h" @@ -62,12 +64,8 @@ CALLBACK_FUNC_DECL(DocumentEvaluate) if (V8Node::HasInstance(args[1])) contextNode = V8DOMWrapper::convertDOMWrapperToNode<Node>(v8::Handle<v8::Object>::Cast(args[1])); - RefPtr<XPathNSResolver> resolver; - if (V8XPathNSResolver::HasInstance(args[2])) - resolver = V8DOMWrapper::convertToNativeObject<XPathNSResolver>(V8ClassIndex::XPATHNSRESOLVER, v8::Handle<v8::Object>::Cast(args[2])); - else if (args[2]->IsObject()) - resolver = V8CustomXPathNSResolver::create(args[2]->ToObject()); - else if (!args[2]->IsNull() && !args[2]->IsUndefined()) + RefPtr<XPathNSResolver> resolver = V8DOMWrapper::getXPathNSResolver(args[2]); + if (!resolver && !args[2]->IsNull() && !args[2]->IsUndefined()) return throwError(TYPE_MISMATCH_ERR); int type = toInt32(args[3]); @@ -99,8 +97,17 @@ CALLBACK_FUNC_DECL(DocumentGetCSSCanvasContext) String name = toWebCoreString(args[1]); int width = toInt32(args[2]); int height = toInt32(args[3]); - CanvasRenderingContext2D* result = imp->getCSSCanvasContext(contextId, name, width, height); - return V8DOMWrapper::convertToV8Object(V8ClassIndex::CANVASRENDERINGCONTEXT2D, result); + CanvasRenderingContext* result = imp->getCSSCanvasContext(contextId, name, width, height); + if (!result) + return v8::Undefined(); + if (result->is2d()) + return V8DOMWrapper::convertToV8Object(V8ClassIndex::CANVASRENDERINGCONTEXT2D, result); +#if ENABLE(3D_CANVAS) + else if (result->is3d()) + return V8DOMWrapper::convertToV8Object(V8ClassIndex::CANVASRENDERINGCONTEXT3D, result); +#endif // ENABLE(3D_CANVAS) + ASSERT_NOT_REACHED(); + return v8::Undefined(); } } // namespace WebCore diff --git a/WebCore/bindings/v8/V8WorkerContextObjectEventListener.cpp b/WebCore/bindings/v8/custom/V8FileListCustom.cpp index ce56563..bc533cf 100644 --- a/WebCore/bindings/v8/V8WorkerContextObjectEventListener.cpp +++ b/WebCore/bindings/v8/custom/V8FileListCustom.cpp @@ -29,31 +29,26 @@ */ #include "config.h" +#include "FileList.h" -#if ENABLE(WORKERS) +#include "File.h" +#include "V8Binding.h" +#include "V8CustomBinding.h" +#include "V8Proxy.h" -#include "V8WorkerContextObjectEventListener.h" - -#include "WorkerContextExecutionProxy.h" +#include <wtf/RefPtr.h> namespace WebCore { -static void weakObjectEventListenerCallback(v8::Persistent<v8::Value>, void* parameter) +INDEXED_PROPERTY_GETTER(FileList) { - V8WorkerContextObjectEventListener* listener = static_cast<V8WorkerContextObjectEventListener*>(parameter); - - // Remove the wrapper - listener->proxy()->removeEventListener(listener); - - listener->disposeListenerObject(); -} + INC_STATS("DOM.FileList.IndexedPropertyGetter"); + FileList* fileList = V8DOMWrapper::convertToNativeObject<FileList>(V8ClassIndex::FILELIST, info.Holder()); + RefPtr<File> file = fileList->item(index); + if (!file) + return notHandledByInterceptor(); -V8WorkerContextObjectEventListener::V8WorkerContextObjectEventListener(WorkerContextExecutionProxy* proxy, v8::Local<v8::Object> listener, bool isInline) - : V8WorkerContextEventListener(proxy, listener, isInline) -{ - m_listener.MakeWeak(this, weakObjectEventListenerCallback); + return V8DOMWrapper::convertToV8Object(V8ClassIndex::FILE, file.release()); } } // namespace WebCore - -#endif // WORKERS diff --git a/WebCore/bindings/v8/custom/V8GeolocationCustom.cpp b/WebCore/bindings/v8/custom/V8GeolocationCustom.cpp index 7bc687c..b4fd9f5 100644 --- a/WebCore/bindings/v8/custom/V8GeolocationCustom.cpp +++ b/WebCore/bindings/v8/custom/V8GeolocationCustom.cpp @@ -130,13 +130,7 @@ static PassRefPtr<PositionOptions> createPositionOptions(v8::Local<v8::Value> va return 0; } double timeoutDouble = timeoutNumber->Value(); - // V8 does not export a public symbol for infinity, so we must use a - // platform type. On Android, it seems that V8 uses 0xf70f000000000000, - // which is the standard way to represent infinity in a double. However, - // numeric_limits<double>::infinity uses the system HUGE_VAL, which is - // different. Therefore we test using isinf() and check that the value - // is positive, which seems to handle things correctly. - // If the value is infinity, there's nothing to do. + // If the value is positive infinity, there's nothing to do. if (!(isinf(timeoutDouble) && timeoutDouble > 0)) { v8::Local<v8::Int32> timeoutInt32 = timeoutValue->ToInt32(); if (timeoutInt32.IsEmpty()) { @@ -161,7 +155,7 @@ static PassRefPtr<PositionOptions> createPositionOptions(v8::Local<v8::Value> va } double maximumAgeDouble = maximumAgeNumber->Value(); if (isinf(maximumAgeDouble) && maximumAgeDouble > 0) { - // If the value is infinity, clear maximumAge. + // If the value is positive infinity, clear maximumAge. options->clearMaximumAge(); } else { v8::Local<v8::Int32> maximumAgeInt32 = maximumAgeValue->ToInt32(); diff --git a/WebCore/bindings/v8/custom/V8HTMLAudioElementConstructor.cpp b/WebCore/bindings/v8/custom/V8HTMLAudioElementConstructor.cpp index 6f9b761..c735c49 100644 --- a/WebCore/bindings/v8/custom/V8HTMLAudioElementConstructor.cpp +++ b/WebCore/bindings/v8/custom/V8HTMLAudioElementConstructor.cpp @@ -29,8 +29,9 @@ */ #include "config.h" -#include "HTMLAudioElement.h" +#include "V8HTMLAudioElementConstructor.h" +#include "HTMLAudioElement.h" #include "Document.h" #include "Frame.h" #include "HTMLNames.h" @@ -42,6 +43,24 @@ namespace WebCore { +v8::Persistent<v8::FunctionTemplate> V8HTMLAudioElementConstructor::GetTemplate() +{ + static v8::Persistent<v8::FunctionTemplate> cachedTemplate; + if (!cachedTemplate.IsEmpty()) + return cachedTemplate; + + v8::HandleScope scope; + v8::Local<v8::FunctionTemplate> result = v8::FunctionTemplate::New(USE_CALLBACK(HTMLAudioElementConstructor)); + + v8::Local<v8::ObjectTemplate> instance = result->InstanceTemplate(); + instance->SetInternalFieldCount(V8Custom::kNodeMinimumInternalFieldCount); + result->SetClassName(v8::String::New("HTMLAudioElement")); + result->Inherit(V8DOMWrapper::getTemplate(V8ClassIndex::HTMLAUDIOELEMENT)); + + cachedTemplate = v8::Persistent<v8::FunctionTemplate>::New(result); + return cachedTemplate; +} + CALLBACK_FUNC_DECL(HTMLAudioElementConstructor) { INC_STATS("DOM.HTMLAudioElement.Contructor"); diff --git a/WebCore/bindings/v8/custom/V8HTMLAudioElementConstructor.h b/WebCore/bindings/v8/custom/V8HTMLAudioElementConstructor.h new file mode 100755 index 0000000..ac4b46d --- /dev/null +++ b/WebCore/bindings/v8/custom/V8HTMLAudioElementConstructor.h @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2009 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef V8HTMLAudioElementConstructor_h +#define V8HTMLAudioElementConstructor_h + +#include <v8.h> + +namespace WebCore { + +class V8HTMLAudioElementConstructor { +private: + static v8::Persistent<v8::FunctionTemplate> GetTemplate(); + + friend class V8ClassIndex; +}; + +} + +#endif // V8HTMLAudioElementConstructor_h diff --git a/WebCore/bindings/v8/custom/V8HTMLCanvasElementCustom.cpp b/WebCore/bindings/v8/custom/V8HTMLCanvasElementCustom.cpp index 6ba9367..4116673 100644 --- a/WebCore/bindings/v8/custom/V8HTMLCanvasElementCustom.cpp +++ b/WebCore/bindings/v8/custom/V8HTMLCanvasElementCustom.cpp @@ -30,7 +30,9 @@ #include "config.h" #include "HTMLCanvasElement.h" +#include "CanvasRenderingContext.h" +#include "CanvasRenderingContext.h" #include "V8Binding.h" #include "V8CustomBinding.h" #include "V8Node.h" @@ -44,8 +46,17 @@ CALLBACK_FUNC_DECL(HTMLCanvasElementGetContext) v8::Handle<v8::Object> holder = args.Holder(); HTMLCanvasElement* imp = V8DOMWrapper::convertDOMWrapperToNode<HTMLCanvasElement>(holder); String contextId = toWebCoreString(args[0]); - CanvasRenderingContext2D* result = imp->getContext(contextId); - return V8DOMWrapper::convertToV8Object(V8ClassIndex::CANVASRENDERINGCONTEXT2D, result); + CanvasRenderingContext* result = imp->getContext(contextId); + if (!result) + return v8::Undefined(); + if (result->is2d()) + return V8DOMWrapper::convertToV8Object(V8ClassIndex::CANVASRENDERINGCONTEXT2D, result); +#if ENABLE(3D_CANVAS) + else if (result->is3d()) + return V8DOMWrapper::convertToV8Object(V8ClassIndex::CANVASRENDERINGCONTEXT3D, result); +#endif + ASSERT_NOT_REACHED(); + return v8::Undefined(); } } // namespace WebCore diff --git a/WebCore/bindings/v8/custom/V8HTMLFrameElementCustom.cpp b/WebCore/bindings/v8/custom/V8HTMLFrameElementCustom.cpp index 4f865dd..2f55758 100644 --- a/WebCore/bindings/v8/custom/V8HTMLFrameElementCustom.cpp +++ b/WebCore/bindings/v8/custom/V8HTMLFrameElementCustom.cpp @@ -30,6 +30,7 @@ #include "config.h" #include "HTMLFrameElement.h" +#include "HTMLNames.h" #include "V8Binding.h" #include "V8CustomBinding.h" @@ -37,6 +38,8 @@ namespace WebCore { +using namespace HTMLNames; + ACCESSOR_SETTER(HTMLFrameElementSrc) { HTMLFrameElement* frame = V8DOMWrapper::convertDOMWrapperToNode<HTMLFrameElement>(info.Holder()); @@ -45,7 +48,7 @@ ACCESSOR_SETTER(HTMLFrameElementSrc) if (!allowSettingFrameSrcToJavascriptUrl(frame, srcValue)) return; - frame->setSrc(srcValue); + frame->setAttribute(srcAttr, srcValue); } ACCESSOR_SETTER(HTMLFrameElementLocation) diff --git a/WebCore/bindings/v8/custom/V8HTMLIFrameElementCustom.cpp b/WebCore/bindings/v8/custom/V8HTMLIFrameElementCustom.cpp index ce2c29a..a4863e8 100644 --- a/WebCore/bindings/v8/custom/V8HTMLIFrameElementCustom.cpp +++ b/WebCore/bindings/v8/custom/V8HTMLIFrameElementCustom.cpp @@ -30,6 +30,7 @@ #include "config.h" #include "HTMLIFrameElement.h" +#include "HTMLNames.h" #include "V8Binding.h" #include "V8CustomBinding.h" @@ -37,6 +38,8 @@ namespace WebCore { +using namespace HTMLNames; + ACCESSOR_SETTER(HTMLIFrameElementSrc) { HTMLIFrameElement* iframe = V8DOMWrapper::convertDOMWrapperToNode<HTMLIFrameElement>(info.Holder()); @@ -45,7 +48,7 @@ ACCESSOR_SETTER(HTMLIFrameElementSrc) if (!allowSettingFrameSrcToJavascriptUrl(iframe, v)) return; - iframe->setSrc(v); + iframe->setAttribute(srcAttr, v); } } // namespace WebCore diff --git a/WebCore/bindings/v8/custom/V8HTMLImageElementConstructor.cpp b/WebCore/bindings/v8/custom/V8HTMLImageElementConstructor.cpp index 91ebd5f..71c99cf 100644 --- a/WebCore/bindings/v8/custom/V8HTMLImageElementConstructor.cpp +++ b/WebCore/bindings/v8/custom/V8HTMLImageElementConstructor.cpp @@ -29,8 +29,9 @@ */ #include "config.h" -#include "HTMLImageElement.h" +#include "V8HTMLImageElementConstructor.h" +#include "HTMLImageElement.h" #include "Document.h" #include "Frame.h" #include "HTMLNames.h" @@ -42,6 +43,24 @@ namespace WebCore { +v8::Persistent<v8::FunctionTemplate> V8HTMLImageElementConstructor::GetTemplate() +{ + static v8::Persistent<v8::FunctionTemplate> cachedTemplate; + if (!cachedTemplate.IsEmpty()) + return cachedTemplate; + + v8::HandleScope scope; + v8::Local<v8::FunctionTemplate> result = v8::FunctionTemplate::New(USE_CALLBACK(HTMLImageElementConstructor)); + + v8::Local<v8::ObjectTemplate> instance = result->InstanceTemplate(); + instance->SetInternalFieldCount(V8Custom::kNodeMinimumInternalFieldCount); + result->SetClassName(v8::String::New("HTMLImageElement")); + result->Inherit(V8DOMWrapper::getTemplate(V8ClassIndex::HTMLIMAGEELEMENT)); + + cachedTemplate = v8::Persistent<v8::FunctionTemplate>::New(result); + return cachedTemplate; +} + CALLBACK_FUNC_DECL(HTMLImageElementConstructor) { INC_STATS("DOM.HTMLImageElement.Contructor"); diff --git a/WebCore/bindings/v8/V8WorkerContextObjectEventListener.h b/WebCore/bindings/v8/custom/V8HTMLImageElementConstructor.h index 6471637..cdce0e0 100644..100755 --- a/WebCore/bindings/v8/V8WorkerContextObjectEventListener.h +++ b/WebCore/bindings/v8/custom/V8HTMLImageElementConstructor.h @@ -28,32 +28,20 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef V8WorkerContextObjectEventListener_h -#define V8WorkerContextObjectEventListener_h +#ifndef V8HTMLImageElementConstructor_h +#define V8HTMLImageElementConstructor_h -#if ENABLE(WORKERS) - -#include "V8WorkerContextEventListener.h" #include <v8.h> -#include <wtf/PassRefPtr.h> namespace WebCore { - class WorkerContextExecutionProxy; - - class V8WorkerContextObjectEventListener : public V8WorkerContextEventListener { - public: - static PassRefPtr<V8WorkerContextObjectEventListener> create(WorkerContextExecutionProxy* proxy, v8::Local<v8::Object> listener, bool isInline) - { - return adoptRef(new V8WorkerContextObjectEventListener(proxy, listener, isInline)); - } - + class V8HTMLImageElementConstructor { private: - V8WorkerContextObjectEventListener(WorkerContextExecutionProxy*, v8::Local<v8::Object> listener, bool isInline); - }; + static v8::Persistent<v8::FunctionTemplate> GetTemplate(); -} // namespace WebCore + friend class V8ClassIndex; + }; -#endif // WORKERS +} -#endif // V8WorkerContextObjectEventListener_h +#endif // V8HTMLImageElementConstructor_h diff --git a/WebCore/bindings/v8/custom/V8HTMLOptionElementConstructor.cpp b/WebCore/bindings/v8/custom/V8HTMLOptionElementConstructor.cpp index c22d127..f9c9cb4 100644 --- a/WebCore/bindings/v8/custom/V8HTMLOptionElementConstructor.cpp +++ b/WebCore/bindings/v8/custom/V8HTMLOptionElementConstructor.cpp @@ -29,8 +29,9 @@ */ #include "config.h" -#include "HTMLOptionElement.h" +#include "V8HTMLOptionElementConstructor.h" +#include "HTMLOptionElement.h" #include "Document.h" #include "Frame.h" #include "HTMLNames.h" @@ -43,6 +44,24 @@ namespace WebCore { +v8::Persistent<v8::FunctionTemplate> V8HTMLOptionElementConstructor::GetTemplate() +{ + static v8::Persistent<v8::FunctionTemplate> cachedTemplate; + if (!cachedTemplate.IsEmpty()) + return cachedTemplate; + + v8::HandleScope scope; + v8::Local<v8::FunctionTemplate> result = v8::FunctionTemplate::New(USE_CALLBACK(HTMLOptionElementConstructor)); + + v8::Local<v8::ObjectTemplate> instance = result->InstanceTemplate(); + instance->SetInternalFieldCount(V8Custom::kNodeMinimumInternalFieldCount); + result->SetClassName(v8::String::New("HTMLOptionElement")); + result->Inherit(V8DOMWrapper::getTemplate(V8ClassIndex::HTMLOPTIONELEMENT)); + + cachedTemplate = v8::Persistent<v8::FunctionTemplate>::New(result); + return cachedTemplate; +} + CALLBACK_FUNC_DECL(HTMLOptionElementConstructor) { INC_STATS("DOM.HTMLOptionElement.Contructor"); diff --git a/WebCore/bindings/v8/custom/V8HTMLOptionElementConstructor.h b/WebCore/bindings/v8/custom/V8HTMLOptionElementConstructor.h new file mode 100755 index 0000000..70076a5 --- /dev/null +++ b/WebCore/bindings/v8/custom/V8HTMLOptionElementConstructor.h @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2009 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef V8HTMLOptionElementConstructor_h +#define V8HTMLOptionElementConstructor_h + +#include <v8.h> + +namespace WebCore { + + class V8HTMLOptionElementConstructor { + private: + static v8::Persistent<v8::FunctionTemplate> GetTemplate(); + + friend class V8ClassIndex; + }; + +} + +#endif // V8HTMLOptionElementConstructor_h diff --git a/WebCore/bindings/v8/custom/V8InspectorBackendCustom.cpp b/WebCore/bindings/v8/custom/V8InspectorBackendCustom.cpp index 2571df4..ec9b034 100644 --- a/WebCore/bindings/v8/custom/V8InspectorBackendCustom.cpp +++ b/WebCore/bindings/v8/custom/V8InspectorBackendCustom.cpp @@ -31,6 +31,7 @@ #include "config.h" #include "InspectorBackend.h" +#include "Database.h" #include "DOMWindow.h" #include "Frame.h" #include "FrameLoader.h" @@ -57,13 +58,8 @@ CALLBACK_FUNC_DECL(InspectorBackendHighlightDOMNode) if (args.Length() < 1) return v8::Undefined(); - Node* node = V8DOMWrapper::convertDOMWrapperToNode<Node>(v8::Handle<v8::Object>::Cast(args[0])); - if (!node) - return v8::Undefined(); - InspectorBackend* inspectorBackend = V8DOMWrapper::convertToNativeObject<InspectorBackend>(V8ClassIndex::INSPECTORBACKEND, args.Holder()); - inspectorBackend->highlight(node); - + inspectorBackend->highlight(args[0]->ToInt32()->Value()); return v8::Undefined(); } @@ -107,11 +103,17 @@ CALLBACK_FUNC_DECL(InspectorBackendSearch) } #if ENABLE(DATABASE) -CALLBACK_FUNC_DECL(InspectorBackendDatabaseTableNames) +CALLBACK_FUNC_DECL(InspectorBackendDatabaseForId) { - INC_STATS("InspectorBackend.databaseTableNames()"); - v8::Local<v8::Array> result = v8::Array::New(0); - return result; + INC_STATS("InspectorBackend.databaseForId()"); + if (args.Length() < 1) + return v8::Undefined(); + + InspectorBackend* inspectorBackend = V8DOMWrapper::convertToNativeObject<InspectorBackend>(V8ClassIndex::INSPECTORBACKEND, args.Holder()); + Database* database = inspectorBackend->databaseForId(args[0]->ToInt32()->Value()); + if (!database) + return v8::Undefined(); + return V8DOMWrapper::convertToV8Object<Database>(V8ClassIndex::DATABASE, database); } #endif @@ -215,4 +217,90 @@ CALLBACK_FUNC_DECL(InspectorBackendWrapCallback) return args[0]; } +CALLBACK_FUNC_DECL(InspectorBackendNodeForId) +{ + INC_STATS("InspectorBackend.nodeForId()"); + if (args.Length() < 1) + return v8::Undefined(); + + InspectorBackend* inspectorBackend = V8DOMWrapper::convertToNativeObject<InspectorBackend>(V8ClassIndex::INSPECTORBACKEND, args.Holder()); + + Node* node = inspectorBackend->nodeForId(args[0]->ToInt32()->Value()); + if (!node) + return v8::Undefined(); + + InspectorController* ic = inspectorBackend->inspectorController(); + if (!ic) + return v8::Undefined(); + + return V8DOMWrapper::convertToV8Object(V8ClassIndex::NODE, node); +} + +CALLBACK_FUNC_DECL(InspectorBackendWrapObject) +{ + INC_STATS("InspectorBackend.wrapObject()"); + if (args.Length() < 2) + return v8::Undefined(); + + InspectorBackend* inspectorBackend = V8DOMWrapper::convertToNativeObject<InspectorBackend>(V8ClassIndex::INSPECTORBACKEND, args.Holder()); + return inspectorBackend->wrapObject(ScriptValue(args[0]), toWebCoreStringWithNullCheck(args[1])).v8Value(); +} + +CALLBACK_FUNC_DECL(InspectorBackendUnwrapObject) +{ + INC_STATS("InspectorBackend.unwrapObject()"); + if (args.Length() < 1) + return v8::Undefined(); + + InspectorBackend* inspectorBackend = V8DOMWrapper::convertToNativeObject<InspectorBackend>(V8ClassIndex::INSPECTORBACKEND, args.Holder()); + return inspectorBackend->unwrapObject(toWebCoreStringWithNullCheck(args[0])).v8Value(); +} + +CALLBACK_FUNC_DECL(InspectorBackendPushNodePathToFrontend) +{ + INC_STATS("InspectorBackend.pushNodePathToFrontend()"); + if (args.Length() < 2) + return v8::Undefined(); + + InspectorBackend* inspectorBackend = V8DOMWrapper::convertToNativeObject<InspectorBackend>(V8ClassIndex::INSPECTORBACKEND, args.Holder()); + Node* node = V8DOMWrapper::convertDOMWrapperToNode<Node>(v8::Handle<v8::Object>::Cast(args[0])); + bool selectInUI = args[1]->ToBoolean()->Value(); + if (node) + return v8::Number::New(inspectorBackend->pushNodePathToFrontend(node, selectInUI)); + + return v8::Undefined(); +} + +#if ENABLE(DATABASE) +CALLBACK_FUNC_DECL(InspectorBackendSelectDatabase) +{ + INC_STATS("InspectorBackend.selectDatabase()"); + if (args.Length() < 1) + return v8::Undefined(); + + InspectorBackend* inspectorBackend = V8DOMWrapper::convertToNativeObject<InspectorBackend>(V8ClassIndex::INSPECTORBACKEND, args.Holder()); + Database* database = V8DOMWrapper::convertToNativeObject<Database>(V8ClassIndex::DATABASE, v8::Handle<v8::Object>::Cast(args[0])); + if (database) + inspectorBackend->selectDatabase(database); + + return v8::Undefined(); +} +#endif + +#if ENABLE(DOM_STORAGE) +CALLBACK_FUNC_DECL(InspectorBackendSelectDOMStorage) +{ + INC_STATS("InspectorBackend.selectDOMStorage()"); + if (args.Length() < 1) + return v8::Undefined(); + + InspectorBackend* inspectorBackend = V8DOMWrapper::convertToNativeObject<InspectorBackend>(V8ClassIndex::INSPECTORBACKEND, args.Holder()); + Storage* storage = V8DOMWrapper::convertToNativeObject<Storage>(V8ClassIndex::STORAGE, v8::Handle<v8::Object>::Cast(args[0])); + if (storage) + inspectorBackend->selectDOMStorage(storage); + + return v8::Undefined(); +} +#endif + } // namespace WebCore diff --git a/WebCore/bindings/v8/custom/V8LocationCustom.cpp b/WebCore/bindings/v8/custom/V8LocationCustom.cpp index 3f3ff6b..1ed4c51 100644 --- a/WebCore/bindings/v8/custom/V8LocationCustom.cpp +++ b/WebCore/bindings/v8/custom/V8LocationCustom.cpp @@ -128,13 +128,13 @@ ACCESSOR_SETTER(LocationHref) if (!frame) return; - if (!shouldAllowNavigation(frame)) - return; - KURL url = completeURL(toWebCoreString(value)); if (url.isNull()) return; + if (!shouldAllowNavigation(frame)) + return; + navigateIfAllowed(frame, url, false, false); } @@ -274,7 +274,7 @@ CALLBACK_FUNC_DECL(LocationReload) return v8::Undefined(); if (!protocolIsJavaScript(frame->loader()->url())) - frame->loader()->scheduleRefresh(processingUserGesture()); + frame->redirectScheduler()->scheduleRefresh(processingUserGesture()); return v8::Undefined(); } @@ -288,13 +288,13 @@ CALLBACK_FUNC_DECL(LocationReplace) if (!frame) return v8::Undefined(); - if (!shouldAllowNavigation(frame)) - return v8::Undefined(); - KURL url = completeURL(toWebCoreString(args[0])); if (url.isNull()) return v8::Undefined(); + if (!shouldAllowNavigation(frame)) + return v8::Undefined(); + navigateIfAllowed(frame, url, true, true); return v8::Undefined(); } @@ -309,13 +309,13 @@ CALLBACK_FUNC_DECL(LocationAssign) if (!frame) return v8::Undefined(); - if (!shouldAllowNavigation(frame)) - return v8::Undefined(); - KURL url = completeURL(toWebCoreString(args[0])); if (url.isNull()) return v8::Undefined(); + if (!shouldAllowNavigation(frame)) + return v8::Undefined(); + navigateIfAllowed(frame, url, false, false); return v8::Undefined(); } diff --git a/WebCore/bindings/v8/custom/V8MessageChannelConstructor.cpp b/WebCore/bindings/v8/custom/V8MessageChannelConstructor.cpp index f45aecf..ca02b16 100644 --- a/WebCore/bindings/v8/custom/V8MessageChannelConstructor.cpp +++ b/WebCore/bindings/v8/custom/V8MessageChannelConstructor.cpp @@ -36,6 +36,7 @@ #include "Document.h" #include "Frame.h" +#include "V8Utilities.h" #include "WorkerContext.h" #include "WorkerContextExecutionProxy.h" @@ -52,16 +53,9 @@ CALLBACK_FUNC_DECL(MessageChannelConstructor) return throwError("DOM object constructor cannot be called as a function."); // Get the ScriptExecutionContext (WorkerContext or Document) - ScriptExecutionContext* context = 0; - WorkerContextExecutionProxy* proxy = WorkerContextExecutionProxy::retrieve(); - if (proxy) - context = proxy->workerContext(); - else { - Frame* frame = V8Proxy::retrieveFrameForCurrentContext(); - if (!frame) - return v8::Undefined(); - context = frame->document(); - } + ScriptExecutionContext* context = getScriptExecutionContext(); + if (!context) + return v8::Undefined(); // Note: it's OK to let this RefPtr go out of scope because we also call // SetDOMWrapper(), which effectively holds a reference to obj. diff --git a/WebCore/bindings/v8/custom/V8MessageEventCustom.cpp b/WebCore/bindings/v8/custom/V8MessageEventCustom.cpp new file mode 100644 index 0000000..c5af635 --- /dev/null +++ b/WebCore/bindings/v8/custom/V8MessageEventCustom.cpp @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2009 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" +#include "MessageEvent.h" +#include "SerializedScriptValue.h" + +#include "V8Binding.h" +#include "V8CustomBinding.h" +#include "V8DOMWindow.h" +#include "V8MessagePortCustom.h" +#include "V8Proxy.h" + +namespace WebCore { + +ACCESSOR_GETTER(MessageEventPorts) +{ + INC_STATS("DOM.MessageEvent.ports"); + MessageEvent* event = V8DOMWrapper::convertToNativeObject<MessageEvent>(V8ClassIndex::MESSAGEEVENT, info.Holder()); + + MessagePortArray* ports = event->ports(); + if (!ports || ports->isEmpty()) + return v8::Null(); + + v8::Local<v8::Array> portArray = v8::Array::New(ports->size()); + for (size_t i = 0; i < ports->size(); ++i) + portArray->Set(v8::Integer::New(i), V8DOMWrapper::convertToV8Object(V8ClassIndex::MESSAGEPORT, (*ports)[i].get())); + + return portArray; +} + +CALLBACK_FUNC_DECL(MessageEventInitMessageEvent) +{ + INC_STATS("DOM.MessageEvent.initMessageEvent"); + MessageEvent* event = V8DOMWrapper::convertToNativeObject<MessageEvent>(V8ClassIndex::MESSAGEEVENT, args.Holder()); + String typeArg = v8ValueToWebCoreString(args[0]); + bool canBubbleArg = args[1]->BooleanValue(); + bool cancelableArg = args[2]->BooleanValue(); + RefPtr<SerializedScriptValue> dataArg = SerializedScriptValue::create(v8ValueToWebCoreString(args[3])); + String originArg = v8ValueToWebCoreString(args[4]); + String lastEventIdArg = v8ValueToWebCoreString(args[5]); + DOMWindow* sourceArg = V8DOMWindow::HasInstance(args[6]) ? V8DOMWrapper::convertToNativeObject<DOMWindow>(V8ClassIndex::DOMWINDOW, v8::Handle<v8::Object>::Cast(args[6])) : 0; + OwnPtr<MessagePortArray> portArray; + + if (!isUndefinedOrNull(args[7])) { + portArray = new MessagePortArray(); + if (!getMessagePortArray(args[7], *portArray)) + return v8::Undefined(); + } + event->initMessageEvent(typeArg, canBubbleArg, cancelableArg, dataArg.release(), originArg, lastEventIdArg, sourceArg, portArray.release()); + return v8::Undefined(); + } + +} // namespace WebCore diff --git a/WebCore/bindings/v8/custom/V8MessagePortCustom.cpp b/WebCore/bindings/v8/custom/V8MessagePortCustom.cpp index 95d248c..0d8631f 100644 --- a/WebCore/bindings/v8/custom/V8MessagePortCustom.cpp +++ b/WebCore/bindings/v8/custom/V8MessagePortCustom.cpp @@ -32,66 +32,22 @@ #include "ExceptionCode.h" #include "MessagePort.h" +#include "SerializedScriptValue.h" #include "V8Binding.h" #include "V8CustomBinding.h" -#include "V8ObjectEventListener.h" +#include "V8MessagePortCustom.h" +#include "V8MessagePort.h" #include "V8Proxy.h" #include "V8Utilities.h" #include "WorkerContextExecutionProxy.h" namespace WebCore { -PassRefPtr<EventListener> getEventListener(MessagePort* messagePort, v8::Local<v8::Value> value, bool findOnly, bool createObjectEventListener) -{ - V8Proxy* proxy = V8Proxy::retrieve(messagePort->scriptExecutionContext()); - if (proxy) { - V8EventListenerList* list = proxy->objectListeners(); - return findOnly ? list->findWrapper(value, false) : list->findOrCreateWrapper<V8ObjectEventListener>(proxy->frame(), value, false); - } - -#if ENABLE(WORKERS) - WorkerContextExecutionProxy* workerContextProxy = WorkerContextExecutionProxy::retrieve(); - if (workerContextProxy) - return workerContextProxy->findOrCreateEventListenerHelper(value, false, findOnly, createObjectEventListener); -#endif - - return PassRefPtr<EventListener>(); -} - -ACCESSOR_GETTER(MessagePortOnmessage) -{ - INC_STATS("DOM.MessagePort.onmessage._get"); - MessagePort* messagePort = V8DOMWrapper::convertToNativeObject<MessagePort>(V8ClassIndex::MESSAGEPORT, info.Holder()); - return V8DOMWrapper::convertEventListenerToV8Object(messagePort->onmessage()); -} - -ACCESSOR_SETTER(MessagePortOnmessage) -{ - INC_STATS("DOM.MessagePort.onmessage._set"); - MessagePort* messagePort = V8DOMWrapper::convertToNativeObject<MessagePort>(V8ClassIndex::MESSAGEPORT, info.Holder()); - if (value->IsNull()) { - if (messagePort->onmessage()) { - V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(messagePort->onmessage()); - removeHiddenDependency(info.Holder(), listener->getListenerObject(), V8Custom::kMessagePortRequestCacheIndex); - } - - // Clear the listener. - messagePort->setOnmessage(0); - - } else { - RefPtr<EventListener> listener = getEventListener(messagePort, value, false, false); - if (listener) { - messagePort->setOnmessage(listener); - createHiddenDependency(info.Holder(), value, V8Custom::kMessagePortRequestCacheIndex); - } - } -} - CALLBACK_FUNC_DECL(MessagePortAddEventListener) { INC_STATS("DOM.MessagePort.addEventListener()"); MessagePort* messagePort = V8DOMWrapper::convertToNativeObject<MessagePort>(V8ClassIndex::MESSAGEPORT, args.Holder()); - RefPtr<EventListener> listener = getEventListener(messagePort, args[1], false, true); + RefPtr<EventListener> listener = V8DOMWrapper::getEventListener(messagePort, args[1], false, ListenerFindOrCreate); if (listener) { String type = toWebCoreString(args[0]); bool useCapture = args[2]->BooleanValue(); @@ -106,7 +62,7 @@ CALLBACK_FUNC_DECL(MessagePortRemoveEventListener) { INC_STATS("DOM.MessagePort.removeEventListener()"); MessagePort* messagePort = V8DOMWrapper::convertToNativeObject<MessagePort>(V8ClassIndex::MESSAGEPORT, args.Holder()); - RefPtr<EventListener> listener = getEventListener(messagePort, args[1], true, true); + RefPtr<EventListener> listener = V8DOMWrapper::getEventListener(messagePort, args[1], false, ListenerFindOnly); if (listener) { String type = toWebCoreString(args[0]); bool useCapture = args[2]->BooleanValue(); @@ -118,4 +74,64 @@ CALLBACK_FUNC_DECL(MessagePortRemoveEventListener) return v8::Undefined(); } +CALLBACK_FUNC_DECL(MessagePortPostMessage) +{ + INC_STATS("DOM.MessagePort.postMessage"); + MessagePort* messagePort = V8DOMWrapper::convertToNativeObject<MessagePort>(V8ClassIndex::MESSAGEPORT, args.Holder()); + RefPtr<SerializedScriptValue> message = SerializedScriptValue::create(toWebCoreString(args[0])); + MessagePortArray portArray; + if (args.Length() > 1) { + if (!getMessagePortArray(args[1], portArray)) + return v8::Undefined(); + } + ExceptionCode ec = 0; + messagePort->postMessage(message.release(), &portArray, ec); + return throwError(ec); +} + +bool getMessagePortArray(v8::Local<v8::Value> value, MessagePortArray& portArray) +{ + if (isUndefinedOrNull(value)) { + portArray.resize(0); + return true; + } + + if (!value->IsObject()) { + throwError("MessagePortArray argument must be an object"); + return false; + } + uint32_t length = 0; + v8::Local<v8::Object> ports = v8::Local<v8::Object>::Cast(value); + + if (value->IsArray()) { + v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(value); + length = array->Length(); + } else { + // Sequence-type object - get the length attribute + v8::Local<v8::Value> sequenceLength = ports->Get(v8::String::New("length")); + if (!sequenceLength->IsNumber()) { + throwError("MessagePortArray argument has no length attribute"); + return false; + } + length = sequenceLength->Uint32Value(); + } + portArray.resize(length); + + for (unsigned int i = 0; i < length; ++i) { + v8::Local<v8::Value> port = ports->Get(v8::Integer::New(i)); + // Validation of non-null objects, per HTML5 spec 8.3.3. + if (isUndefinedOrNull(port)) { + throwError(INVALID_STATE_ERR); + return false; + } + // Validation of Objects implementing an interface, per WebIDL spec 4.1.15. + if (!V8MessagePort::HasInstance(port)) { + throwError("MessagePortArray argument must contain only MessagePorts"); + return false; + } + portArray[i] = V8DOMWrapper::convertToNativeObject<MessagePort>(V8ClassIndex::MESSAGEPORT, v8::Handle<v8::Object>::Cast(port)); + } + return true; +} + } // namespace WebCore diff --git a/WebCore/bindings/v8/V8ObjectEventListener.h b/WebCore/bindings/v8/custom/V8MessagePortCustom.h index 3c5ae10..7ab502b 100644 --- a/WebCore/bindings/v8/V8ObjectEventListener.h +++ b/WebCore/bindings/v8/custom/V8MessagePortCustom.h @@ -1,5 +1,6 @@ /* - * Copyright (C) 2006, 2007, 2008, 2009 Google Inc. All rights reserved. + * Copyright (c) 2008, 2009 Google Inc. + * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are @@ -28,32 +29,20 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef V8ObjectEventListener_h -#define V8ObjectEventListener_h +#ifndef V8MessagePortCustom_h +#define V8MessagePortCustom_h -#include "V8CustomEventListener.h" #include <v8.h> -#include <wtf/PassRefPtr.h> -namespace WebCore { - - class Frame; +#include "MessagePort.h" - // V8ObjectEventListener is a special listener wrapper for objects not in the DOM. It keeps the JS listener as a weak pointer. - class V8ObjectEventListener : public V8EventListener { - public: - static PassRefPtr<V8ObjectEventListener> create(Frame* frame, v8::Local<v8::Object> listener, bool isInline) - { - return adoptRef(new V8ObjectEventListener(frame, listener, isInline)); - } - - virtual bool isObjectListener() const { return true; } +namespace WebCore { - private: - V8ObjectEventListener(Frame*, v8::Local<v8::Object> listener, bool isInline); - virtual ~V8ObjectEventListener(); - }; + // Helper function which pulls the values out of a JS sequence and into a MessagePortArray. + // Also validates the elements per sections 4.1.13 and 4.1.15 of the WebIDL spec and section 8.3.3 of the HTML5 spec and generates exceptions as appropriate. + // Returns true if the array was filled, or false if the passed value was not of an appropriate type. + bool getMessagePortArray(v8::Local<v8::Value>, MessagePortArray&); } // namespace WebCore -#endif // V8ObjectEventListener_h +#endif // V8MessagePortCustom_h diff --git a/WebCore/bindings/v8/custom/V8NodeCustom.cpp b/WebCore/bindings/v8/custom/V8NodeCustom.cpp index 6b0d740..9b4b9aa 100644 --- a/WebCore/bindings/v8/custom/V8NodeCustom.cpp +++ b/WebCore/bindings/v8/custom/V8NodeCustom.cpp @@ -39,76 +39,18 @@ #include "V8CustomBinding.h" #include "V8CustomEventListener.h" #include "V8Node.h" -#include "V8ObjectEventListener.h" #include "V8Proxy.h" #include <wtf/RefPtr.h> namespace WebCore { -static inline String toEventType(v8::Local<v8::String> value) -{ - String key = toWebCoreString(value); - ASSERT(key.startsWith("on")); - return key.substring(2); -} - -static PassRefPtr<EventListener> getEventListener(Node* node, v8::Local<v8::Value> value, bool isAttribute, bool findOnly) -{ - V8Proxy* proxy = V8Proxy::retrieve(node->scriptExecutionContext()); - // The document might be created using createDocument, which does - // not have a frame, use the active frame. - if (!proxy) - proxy = V8Proxy::retrieve(V8Proxy::retrieveFrameForEnteredContext()); - - if (proxy) { - V8EventListenerList* list = proxy->objectListeners(); - return findOnly ? list->findWrapper(value, isAttribute) : list->findOrCreateWrapper<V8ObjectEventListener>(proxy->frame(), value, isAttribute); - } - - return 0; -} - -ACCESSOR_SETTER(NodeEventHandler) -{ - Node* node = V8DOMWrapper::convertDOMWrapperToNode<Node>(info.Holder()); - String eventType = toEventType(name); - - // Remove hidden dependency on the old event handler. - if (EventListener* listener = node->getAttributeEventListener(eventType)) { - if (static_cast<V8AbstractEventListener*>(listener)->isObjectListener()) { - v8::Local<v8::Object> v8Listener = static_cast<V8ObjectEventListener*>(listener)->getListenerObject(); - removeHiddenDependency(info.Holder(), v8Listener, V8Custom::kNodeEventListenerCacheIndex); - } - } - - // Set handler if the value is a function. - if (value->IsFunction()) { - RefPtr<EventListener> listener = getEventListener(node, value, true, false); - if (listener) { - node->setAttributeEventListener(eventType, listener); - createHiddenDependency(info.Holder(), value, V8Custom::kNodeEventListenerCacheIndex); - } - } else { - // Otherwise, clear the handler. - node->clearAttributeEventListener(eventType); - } -} - -ACCESSOR_GETTER(NodeEventHandler) -{ - Node* node = V8DOMWrapper::convertDOMWrapperToNode<Node>(info.Holder()); - - EventListener* listener = node->getAttributeEventListener(toEventType(name)); - return V8DOMWrapper::convertEventListenerToV8Object(listener); -} - CALLBACK_FUNC_DECL(NodeAddEventListener) { INC_STATS("DOM.Node.addEventListener()"); Node* node = V8DOMWrapper::convertDOMWrapperToNode<Node>(args.Holder()); - RefPtr<EventListener> listener = getEventListener(node, args[1], false, false); + RefPtr<EventListener> listener = V8DOMWrapper::getEventListener(node, args[1], false, ListenerFindOrCreate); if (listener) { String type = toWebCoreString(args[0]); bool useCapture = args[2]->BooleanValue(); @@ -126,9 +68,9 @@ CALLBACK_FUNC_DECL(NodeRemoveEventListener) // It is possbile that the owner document of the node is detached // from the frame. // See issue http://b/878909 - RefPtr<EventListener> listener = getEventListener(node, args[1], false, true); + RefPtr<EventListener> listener = V8DOMWrapper::getEventListener(node, args[1], false, ListenerFindOnly); if (listener) { - String type = toWebCoreString(args[0]); + AtomicString type = v8ValueToAtomicWebCoreString(args[0]); bool useCapture = args[2]->BooleanValue(); node->removeEventListener(type, listener.get(), useCapture); removeHiddenDependency(args.Holder(), args[1], V8Custom::kNodeEventListenerCacheIndex); diff --git a/WebCore/bindings/v8/custom/V8NotificationCenterCustom.cpp b/WebCore/bindings/v8/custom/V8NotificationCenterCustom.cpp new file mode 100644 index 0000000..bd5fb4a --- /dev/null +++ b/WebCore/bindings/v8/custom/V8NotificationCenterCustom.cpp @@ -0,0 +1,143 @@ +/* + * Copyright (C) 2009 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" + +#if ENABLE(NOTIFICATIONS) + +#include "NotImplemented.h" +#include "Notification.h" +#include "NotificationCenter.h" +#include "V8Binding.h" +#include "V8CustomBinding.h" +#include "V8CustomEventListener.h" +#include "V8CustomVoidCallback.h" +#include "V8Proxy.h" +#include "V8Utilities.h" +#include "WorkerContext.h" +#include "WorkerContextExecutionProxy.h" + +namespace WebCore { + +CALLBACK_FUNC_DECL(NotificationAddEventListener) +{ + INC_STATS("DOM.Notification.addEventListener()"); + Notification* notification = V8DOMWrapper::convertToNativeObject<Notification>(V8ClassIndex::NOTIFICATION, args.Holder()); + + RefPtr<EventListener> listener = V8DOMWrapper::getEventListener(notification, args[1], false, ListenerFindOrCreate); + if (listener) { + String type = toWebCoreString(args[0]); + bool useCapture = args[2]->BooleanValue(); + notification->addEventListener(type, listener, useCapture); + createHiddenDependency(args.Holder(), args[1], V8Custom::kNotificationRequestCacheIndex); + } + + return v8::Undefined(); +} + +CALLBACK_FUNC_DECL(NotificationRemoveEventListener) +{ + INC_STATS("DOM.Notification.removeEventListener()"); + Notification* notification = V8DOMWrapper::convertToNativeObject<Notification>(V8ClassIndex::NOTIFICATION, args.Holder()); + + RefPtr<EventListener> listener = V8DOMWrapper::getEventListener(notification, args[1], false, ListenerFindOnly); + if (listener) { + String type = toWebCoreString(args[0]); + bool useCapture = args[2]->BooleanValue(); + notification->removeEventListener(type, listener.get(), useCapture); + removeHiddenDependency(args.Holder(), args[1], V8Custom::kNotificationRequestCacheIndex); + } + + return v8::Undefined(); +} + +CALLBACK_FUNC_DECL(NotificationCenterCreateHTMLNotification) +{ + INC_STATS(L"DOM.NotificationCenter.CreateHTMLNotification()"); + NotificationCenter* notificationCenter = V8DOMWrapper::convertToNativeObject<NotificationCenter>(V8ClassIndex::NOTIFICATIONCENTER, args.Holder()); + ScriptExecutionContext* context = notificationCenter->context(); + ExceptionCode ec = 0; + String url = toWebCoreString(args[0]); + RefPtr<Notification> notification = Notification::create(url, context, ec, notificationCenter->presenter()); + + if (ec) + return throwError(ec); + + if (context->isWorkerContext()) + return WorkerContextExecutionProxy::convertToV8Object(V8ClassIndex::NOTIFICATION, notification.get()); + + return V8DOMWrapper::convertToV8Object(V8ClassIndex::NOTIFICATION, notification.get()); +} + +CALLBACK_FUNC_DECL(NotificationCenterCreateNotification) +{ + INC_STATS(L"DOM.NotificationCenter.CreateNotification()"); + NotificationCenter* notificationCenter = V8DOMWrapper::convertToNativeObject<NotificationCenter>(V8ClassIndex::NOTIFICATIONCENTER, args.Holder()); + NotificationContents contents(toWebCoreString(args[0]), toWebCoreString(args[1]), toWebCoreString(args[2])); + + ScriptExecutionContext* context = notificationCenter->context(); + ExceptionCode ec = 0; + RefPtr<Notification> notification = Notification::create(contents, context, ec, notificationCenter->presenter()); + + if (ec) + return throwError(ec); + + if (context->isWorkerContext()) + return WorkerContextExecutionProxy::convertToV8Object(V8ClassIndex::NOTIFICATION, notification.get()); + + return V8DOMWrapper::convertToV8Object(V8ClassIndex::NOTIFICATION, notification.get()); +} + +CALLBACK_FUNC_DECL(NotificationCenterRequestPermission) +{ + INC_STATS(L"DOM.NotificationCenter.RequestPermission()"); + NotificationCenter* notificationCenter = V8DOMWrapper::convertToNativeObject<NotificationCenter>(V8ClassIndex::NOTIFICATIONCENTER, args.Holder()); + ScriptExecutionContext* context = notificationCenter->context(); + + // Requesting permission is only valid from a page context. + if (context->isWorkerContext()) + return throwError(NOT_SUPPORTED_ERR); + + RefPtr<V8CustomVoidCallback> callback; + if (args.Length() > 0) { + if (!args[0]->IsObject()) + return throwError("Callback must be of valid type.", V8Proxy::TypeError); + + callback = V8CustomVoidCallback::create(args[0], V8Proxy::retrieveFrameForCurrentContext()); + } + + notificationCenter->requestPermission(callback.release()); + return v8::Undefined(); +} + + +} // namespace WebCore + +#endif // ENABLE(NOTIFICATIONS) diff --git a/WebCore/bindings/v8/custom/V8SVGElementInstanceCustom.cpp b/WebCore/bindings/v8/custom/V8SVGElementInstanceCustom.cpp index ce9c345..dff4ff4 100644 --- a/WebCore/bindings/v8/custom/V8SVGElementInstanceCustom.cpp +++ b/WebCore/bindings/v8/custom/V8SVGElementInstanceCustom.cpp @@ -49,15 +49,12 @@ CALLBACK_FUNC_DECL(SVGElementInstanceAddEventListener) INC_STATS("DOM.SVGElementInstance.AddEventListener()"); SVGElementInstance* instance = V8DOMWrapper::convertDOMWrapperToNative<SVGElementInstance>(args.Holder()); - V8Proxy* proxy = V8Proxy::retrieve(instance->scriptExecutionContext()); - if (!proxy) - return v8::Undefined(); - - RefPtr<EventListener> listener = proxy->eventListeners()->findOrCreateWrapper<V8EventListener>(proxy->frame(), args[1], false); + RefPtr<EventListener> listener = V8DOMWrapper::getEventListener(instance, args[1], false, ListenerFindOrCreate); if (listener) { String type = toWebCoreString(args[0]); bool useCapture = args[2]->BooleanValue(); instance->addEventListener(type, listener, useCapture); + createHiddenDependency(args.Holder(), args[1], V8Custom::kNodeEventListenerCacheIndex); } return v8::Undefined(); @@ -68,15 +65,12 @@ CALLBACK_FUNC_DECL(SVGElementInstanceRemoveEventListener) INC_STATS("DOM.SVGElementInstance.RemoveEventListener()"); SVGElementInstance* instance = V8DOMWrapper::convertDOMWrapperToNative<SVGElementInstance>(args.Holder()); - V8Proxy* proxy = V8Proxy::retrieve(instance->scriptExecutionContext()); - if (!proxy) - return v8::Undefined(); - - RefPtr<EventListener> listener = proxy->eventListeners()->findWrapper(args[1], false); + RefPtr<EventListener> listener = V8DOMWrapper::getEventListener(instance, args[1], false, ListenerFindOnly); if (listener) { String type = toWebCoreString(args[0]); bool useCapture = args[2]->BooleanValue(); instance->removeEventListener(type, listener.get(), useCapture); + removeHiddenDependency(args.Holder(), args[1], V8Custom::kNodeEventListenerCacheIndex); } return v8::Undefined(); diff --git a/WebCore/bindings/v8/custom/V8SharedWorkerCustom.cpp b/WebCore/bindings/v8/custom/V8SharedWorkerCustom.cpp index 3ab2f8e..e470bc8 100644 --- a/WebCore/bindings/v8/custom/V8SharedWorkerCustom.cpp +++ b/WebCore/bindings/v8/custom/V8SharedWorkerCustom.cpp @@ -53,12 +53,15 @@ CALLBACK_FUNC_DECL(SharedWorkerConstructor) if (!args.IsConstructCall()) return throwError("DOM object constructor cannot be called as a function."); - if (args.Length() < 2) + if (args.Length() < 1) return throwError("Not enough arguments", V8Proxy::SyntaxError); v8::TryCatch tryCatch; v8::Handle<v8::String> scriptUrl = args[0]->ToString(); - v8::Handle<v8::String> name = args[1]->ToString(); + String name; + if (args.Length() > 1) + name = toWebCoreString(args[1]->ToString()); + if (tryCatch.HasCaught()) return throwError(tryCatch.Exception()); @@ -66,21 +69,14 @@ CALLBACK_FUNC_DECL(SharedWorkerConstructor) return v8::Undefined(); // Get the script execution context. - ScriptExecutionContext* context = 0; - WorkerContextExecutionProxy* proxy = WorkerContextExecutionProxy::retrieve(); - if (proxy) - context = proxy->workerContext(); - else { - Frame* frame = V8Proxy::retrieveFrame(); - if (!frame) - return v8::Undefined(); - context = frame->document(); - } + ScriptExecutionContext* context = getScriptExecutionContext(); + if (!context) + return v8::Undefined(); // Create the worker object. // Note: it's OK to let this RefPtr go out of scope because we also call SetDOMWrapper(), which effectively holds a reference to obj. ExceptionCode ec = 0; - RefPtr<SharedWorker> obj = SharedWorker::create(toWebCoreString(scriptUrl), toWebCoreString(name), context, ec); + RefPtr<SharedWorker> obj = SharedWorker::create(toWebCoreString(scriptUrl), name, context, ec); // Setup the standard wrapper object internal fields. v8::Handle<v8::Object> wrapperObject = args.Holder(); diff --git a/WebCore/bindings/v8/custom/V8WebKitPointConstructor.cpp b/WebCore/bindings/v8/custom/V8WebKitPointConstructor.cpp index dd19a88..b2a807d 100755 --- a/WebCore/bindings/v8/custom/V8WebKitPointConstructor.cpp +++ b/WebCore/bindings/v8/custom/V8WebKitPointConstructor.cpp @@ -30,17 +30,38 @@ #include "config.h" +#include "V8Binding.h" #include "V8CustomBinding.h" +#include "V8DOMWrapper.h" #include "V8Index.h" #include "V8Proxy.h" #include "WebKitPoint.h" +#include <wtf/MathExtras.h> + namespace WebCore { CALLBACK_FUNC_DECL(WebKitPointConstructor) { INC_STATS("DOM.WebKitPoint.Constructor"); - return V8Proxy::constructDOMObject<V8ClassIndex::WEBKITPOINT, WebKitPoint>(args); + float x = 0; + float y = 0; + if (args.Length() > 1) { + if (!args[0]->IsUndefined()) { + x = toFloat(args[0]); + if (isnan(x)) + x = 0; + } + if (!args[1]->IsUndefined()) { + y = toFloat(args[1]); + if (isnan(y)) + y = 0; + } + } + PassRefPtr<WebKitPoint> point = WebKitPoint::create(x, y); + point->ref(); + V8DOMWrapper::setDOMWrapper(args.Holder(), V8ClassIndex::WEBKITPOINT, point.get()); + return args.Holder(); } } // namespace WebCore diff --git a/WebCore/bindings/v8/custom/V8WebSocketCustom.cpp b/WebCore/bindings/v8/custom/V8WebSocketCustom.cpp new file mode 100644 index 0000000..f498e4f --- /dev/null +++ b/WebCore/bindings/v8/custom/V8WebSocketCustom.cpp @@ -0,0 +1,131 @@ +/* + * Copyright (C) 2009 Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "config.h" + +#if ENABLE(WEB_SOCKETS) + +#include "WebSocket.h" + +#include "Frame.h" +#include "Settings.h" +#include "V8Binding.h" +#include "V8Proxy.h" +#include "V8Utilities.h" +#include "WorkerContext.h" +#include "WorkerContextExecutionProxy.h" + +namespace WebCore { + +// ??? AddEventListener, RemoveEventListener + +CALLBACK_FUNC_DECL(WebSocketConstructor) +{ + INC_STATS("DOM.WebSocket.Constructor"); + + if (!args.IsConstructCall()) + return throwError("DOM object custructor cannot be called as a function."); + if (args.Length() == 0) + return throwError("Not enough arguments", V8Proxy::SyntaxError); + + v8::TryCatch tryCatch; + v8::Handle<v8::String> urlstring = args[0]->ToString(); + if (tryCatch.HasCaught()) + return throwError(tryCatch.Exception()); + if (urlstring.IsEmpty()) + return throwError("Empty URL", V8Proxy::SyntaxError); + + // Get the script execution context. + ScriptExecutionContext* context = 0; + // TODO: Workers + if (!context) { + Frame* frame = V8Proxy::retrieveFrameForCurrentContext(); + if (!frame) + return throwError("WebSocket constructor's associated frame is not available", V8Proxy::ReferenceError); + context = frame->document(); + } + + const KURL& url = context->completeURL(toWebCoreString(urlstring)); + + RefPtr<WebSocket> webSocket = WebSocket::create(context); + ExceptionCode ec = 0; + + if (args.Length() < 2) + webSocket->connect(url, ec); + else { + v8::TryCatch tryCatchProtocol; + v8::Handle<v8::String> protocol = args[1]->ToString(); + if (tryCatchProtocol.HasCaught()) + return throwError(tryCatchProtocol.Exception()); + webSocket->connect(url, toWebCoreString(protocol), ec); + } + if (ec) + return throwError(ec); + + // Setup the standard wrapper object internal fields. + V8DOMWrapper::setDOMWrapper(args.Holder(), V8ClassIndex::ToInt(V8ClassIndex::WEBSOCKET), webSocket.get()); + + // Add object to the wrapper map. + webSocket->ref(); + V8DOMWrapper::setJSWrapperForActiveDOMObject(webSocket.get(), v8::Persistent<v8::Object>::New(args.Holder())); + + return args.Holder(); +} + +CALLBACK_FUNC_DECL(WebSocketSend) +{ + INC_STATS("DOM.WebSocket.send()"); + WebSocket* webSocket = V8DOMWrapper::convertToNativeObject<WebSocket>(V8ClassIndex::WEBSOCKET, args.Holder()); + + ExceptionCode ec = 0; + bool ret = false; + if (args.Length() < 1) + return throwError("Not enough arguments", V8Proxy::SyntaxError); + else { + String msg = toWebCoreString(args[0]); + ret = webSocket->send(msg, ec); + } + if (ec) + return throwError(ec); + return v8Boolean(ret); +} + +CALLBACK_FUNC_DECL(WebSocketClose) +{ + INC_STATS("DOM.WebSocket.close()"); + WebSocket* webSocket = V8DOMWrapper::convertToNativeObject<WebSocket>(V8ClassIndex::WEBSOCKET, args.Holder()); + + webSocket->close(); + return v8::Undefined(); +} + +} // namespace WebCore + +#endif // ENABLE(WEB_SOCKETS) diff --git a/WebCore/bindings/v8/custom/V8WorkerContextCustom.cpp b/WebCore/bindings/v8/custom/V8WorkerContextCustom.cpp index b526040..21b3c30 100755 --- a/WebCore/bindings/v8/custom/V8WorkerContextCustom.cpp +++ b/WebCore/bindings/v8/custom/V8WorkerContextCustom.cpp @@ -52,45 +52,6 @@ ACCESSOR_GETTER(WorkerContextSelf) return WorkerContextExecutionProxy::convertWorkerContextToV8Object(workerContext); } -ACCESSOR_GETTER(WorkerContextOnerror) -{ - INC_STATS(L"DOM.WorkerContext.onerror._get"); - WorkerContext* workerContext = V8DOMWrapper::convertToNativeObject<WorkerContext>(V8ClassIndex::WORKERCONTEXT, info.Holder()); - if (workerContext->onerror()) { - V8WorkerContextEventListener* listener = static_cast<V8WorkerContextEventListener*>(workerContext->onerror()); - v8::Local<v8::Object> v8Listener = listener->getListenerObject(); - return v8Listener; - } - return v8::Undefined(); -} - -ACCESSOR_SETTER(WorkerContextOnerror) -{ - INC_STATS(L"DOM.WorkerContext.onerror._set"); - WorkerContext* workerContext = V8DOMWrapper::convertToNativeObject<WorkerContext>(V8ClassIndex::WORKERCONTEXT, info.Holder()); - V8WorkerContextEventListener* oldListener = static_cast<V8WorkerContextEventListener*>(workerContext->onerror()); - if (value->IsNull()) { - if (workerContext->onerror()) { - v8::Local<v8::Object> oldV8Listener = oldListener->getListenerObject(); - removeHiddenDependency(info.Holder(), oldV8Listener, V8Custom::kWorkerContextRequestCacheIndex); - } - - // Clear the listener. - workerContext->setOnerror(0); - } else { - RefPtr<V8EventListener> listener = workerContext->script()->proxy()->findOrCreateEventListener(v8::Local<v8::Object>::Cast(value), true, false); - if (listener) { - if (oldListener) { - v8::Local<v8::Object> oldV8Listener = oldListener->getListenerObject(); - removeHiddenDependency(info.Holder(), oldV8Listener, V8Custom::kWorkerContextRequestCacheIndex); - } - - workerContext->setOnerror(listener); - createHiddenDependency(info.Holder(), value, V8Custom::kWorkerContextRequestCacheIndex); - } - } -} - v8::Handle<v8::Value> SetTimeoutOrInterval(const v8::Arguments& args, bool singleShot) { WorkerContext* workerContext = V8DOMWrapper::convertDOMWrapperToNative<WorkerContext>(args.Holder()); @@ -103,9 +64,10 @@ v8::Handle<v8::Value> SetTimeoutOrInterval(const v8::Arguments& args, bool singl int32_t timeout = argumentCount >= 2 ? args[1]->Int32Value() : 0; int timerId; + v8::Handle<v8::Context> v8Context = workerContext->script()->proxy()->context(); if (function->IsString()) { WebCore::String stringFunction = toWebCoreString(function); - timerId = DOMTimer::install(workerContext, new ScheduledAction(stringFunction, workerContext->url()), timeout, singleShot); + timerId = DOMTimer::install(workerContext, new ScheduledAction(v8Context, stringFunction, workerContext->url()), timeout, singleShot); } else if (function->IsFunction()) { size_t paramCount = argumentCount >= 2 ? argumentCount - 2 : 0; v8::Local<v8::Value>* params = 0; @@ -115,7 +77,7 @@ v8::Handle<v8::Value> SetTimeoutOrInterval(const v8::Arguments& args, bool singl params[i] = args[i+2]; } // ScheduledAction takes ownership of actual params and releases them in its destructor. - ScheduledAction* action = new ScheduledAction(v8::Handle<v8::Function>::Cast(function), paramCount, params); + ScheduledAction* action = new ScheduledAction(v8Context, v8::Handle<v8::Function>::Cast(function), paramCount, params); delete [] params; timerId = DOMTimer::install(workerContext, action, timeout, singleShot); } else @@ -170,8 +132,7 @@ CALLBACK_FUNC_DECL(WorkerContextAddEventListener) INC_STATS(L"DOM.WorkerContext.addEventListener()"); WorkerContext* workerContext = V8DOMWrapper::convertDOMWrapperToNative<WorkerContext>(args.Holder()); - RefPtr<V8EventListener> listener = workerContext->script()->proxy()->findOrCreateEventListener(v8::Local<v8::Object>::Cast(args[1]), false, false); - + RefPtr<EventListener> listener = V8DOMWrapper::getEventListener(workerContext, args[1], false, ListenerFindOrCreate); if (listener) { String type = toWebCoreString(args[0]); bool useCapture = args[2]->BooleanValue(); @@ -186,10 +147,8 @@ CALLBACK_FUNC_DECL(WorkerContextRemoveEventListener) { INC_STATS(L"DOM.WorkerContext.removeEventListener()"); WorkerContext* workerContext = V8DOMWrapper::convertDOMWrapperToNative<WorkerContext>(args.Holder()); - WorkerContextExecutionProxy* proxy = workerContext->script()->proxy(); - - RefPtr<V8EventListener> listener = proxy->findOrCreateEventListener(v8::Local<v8::Object>::Cast(args[1]), false, true); + RefPtr<EventListener> listener = V8DOMWrapper::getEventListener(workerContext, args[1], false, ListenerFindOnly); if (listener) { String type = toWebCoreString(args[0]); bool useCapture = args[2]->BooleanValue(); @@ -197,7 +156,6 @@ CALLBACK_FUNC_DECL(WorkerContextRemoveEventListener) removeHiddenDependency(args.Holder(), args[1], V8Custom::kWorkerContextRequestCacheIndex); } - return v8::Undefined(); } diff --git a/WebCore/bindings/v8/custom/V8WorkerCustom.cpp b/WebCore/bindings/v8/custom/V8WorkerCustom.cpp index 32450b8..454e41f 100755 --- a/WebCore/bindings/v8/custom/V8WorkerCustom.cpp +++ b/WebCore/bindings/v8/custom/V8WorkerCustom.cpp @@ -36,9 +36,10 @@ #include "ExceptionCode.h" #include "Frame.h" +#include "SerializedScriptValue.h" #include "V8Binding.h" #include "V8CustomBinding.h" -#include "V8ObjectEventListener.h" +#include "V8MessagePortCustom.h" #include "V8Proxy.h" #include "V8Utilities.h" #include "WorkerContext.h" @@ -65,16 +66,9 @@ CALLBACK_FUNC_DECL(WorkerConstructor) return v8::Undefined(); // Get the script execution context. - ScriptExecutionContext* context = 0; - WorkerContextExecutionProxy* proxy = WorkerContextExecutionProxy::retrieve(); - if (proxy) - context = proxy->workerContext(); - else { - Frame* frame = V8Proxy::retrieveFrameForCurrentContext(); - if (!frame) - return v8::Undefined(); - context = frame->document(); - } + ScriptExecutionContext* context = getScriptExecutionContext(); + if (!context) + return v8::Undefined(); // Create the worker object. // Note: it's OK to let this RefPtr go out of scope because we also call setDOMWrapper(), which effectively holds a reference to obj. @@ -93,60 +87,19 @@ CALLBACK_FUNC_DECL(WorkerConstructor) return wrapperObject; } -PassRefPtr<EventListener> getEventListener(Worker* worker, v8::Local<v8::Value> value, bool isAttribute, bool findOnly) -{ - if (worker->scriptExecutionContext()->isWorkerContext()) { - WorkerContextExecutionProxy* workerContextProxy = WorkerContextExecutionProxy::retrieve(); - ASSERT(workerContextProxy); - return workerContextProxy->findOrCreateObjectEventListener(value, isAttribute, findOnly); - } - - V8Proxy* proxy = V8Proxy::retrieve(worker->scriptExecutionContext()); - if (proxy) { - V8EventListenerList* list = proxy->objectListeners(); - return findOnly ? list->findWrapper(value, isAttribute) : list->findOrCreateWrapper<V8ObjectEventListener>(proxy->frame(), value, isAttribute); - } - - return 0; -} - -ACCESSOR_GETTER(WorkerOnmessage) -{ - INC_STATS(L"DOM.Worker.onmessage._get"); - Worker* worker = V8DOMWrapper::convertToNativeObject<Worker>(V8ClassIndex::WORKER, info.Holder()); - if (worker->onmessage()) { - V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(worker->onmessage()); - v8::Local<v8::Object> v8Listener = listener->getListenerObject(); - return v8Listener; - } - return v8::Undefined(); -} - -ACCESSOR_SETTER(WorkerOnmessage) +CALLBACK_FUNC_DECL(WorkerPostMessage) { - INC_STATS(L"DOM.Worker.onmessage._set"); - Worker* worker = V8DOMWrapper::convertToNativeObject<Worker>(V8ClassIndex::WORKER, info.Holder()); - V8ObjectEventListener* oldListener = static_cast<V8ObjectEventListener*>(worker->onmessage()); - if (value->IsNull()) { - if (oldListener) { - v8::Local<v8::Object> oldV8Listener = oldListener->getListenerObject(); - removeHiddenDependency(info.Holder(), oldV8Listener, V8Custom::kWorkerRequestCacheIndex); - } - - // Clear the listener. - worker->setOnmessage(0); - } else { - RefPtr<EventListener> listener = getEventListener(worker, value, true, false); - if (listener) { - if (oldListener) { - v8::Local<v8::Object> oldV8Listener = oldListener->getListenerObject(); - removeHiddenDependency(info.Holder(), oldV8Listener, V8Custom::kWorkerRequestCacheIndex); - } - - worker->setOnmessage(listener); - createHiddenDependency(info.Holder(), value, V8Custom::kWorkerRequestCacheIndex); - } + INC_STATS("DOM.Worker.postMessage"); + Worker* worker = V8DOMWrapper::convertToNativeObject<Worker>(V8ClassIndex::WORKER, args.Holder()); + RefPtr<SerializedScriptValue> message = SerializedScriptValue::create(toWebCoreString(args[0])); + MessagePortArray portArray; + if (args.Length() > 1) { + if (!getMessagePortArray(args[1], portArray)) + return v8::Undefined(); } + ExceptionCode ec = 0; + worker->postMessage(message.release(), &portArray, ec); + return throwError(ec); } } // namespace WebCore diff --git a/WebCore/bindings/v8/custom/V8XMLHttpRequestConstructor.cpp b/WebCore/bindings/v8/custom/V8XMLHttpRequestConstructor.cpp index 02ce8e2..af647cd 100644 --- a/WebCore/bindings/v8/custom/V8XMLHttpRequestConstructor.cpp +++ b/WebCore/bindings/v8/custom/V8XMLHttpRequestConstructor.cpp @@ -33,8 +33,8 @@ #include "Frame.h" #include "V8Binding.h" #include "V8CustomBinding.h" -#include "V8ObjectEventListener.h" #include "V8Proxy.h" +#include "V8Utilities.h" #include "XMLHttpRequest.h" #include "WorkerContext.h" #include "WorkerContextExecutionProxy.h" @@ -50,20 +50,9 @@ CALLBACK_FUNC_DECL(XMLHttpRequestConstructor) // Expect no parameters. // Allocate a XMLHttpRequest object as its internal field. - ScriptExecutionContext* context = 0; -#if ENABLE(WORKERS) - WorkerContextExecutionProxy* proxy = WorkerContextExecutionProxy::retrieve(); - if (proxy) - context = proxy->workerContext(); - else { -#endif - Frame* frame = V8Proxy::retrieveFrameForCurrentContext(); - if (!frame) - return throwError("XMLHttpRequest constructor's associated frame is not available", V8Proxy::ReferenceError); - context = frame->document(); -#if ENABLE(WORKERS) - } -#endif + ScriptExecutionContext* context = getScriptExecutionContext(); + if (!context) + return throwError("XMLHttpRequest constructor's associated context is not available", V8Proxy::ReferenceError); RefPtr<XMLHttpRequest> xmlHttpRequest = XMLHttpRequest::create(context); V8DOMWrapper::setDOMWrapper(args.Holder(), V8ClassIndex::ToInt(V8ClassIndex::XMLHTTPREQUEST), xmlHttpRequest.get()); diff --git a/WebCore/bindings/v8/custom/V8XMLHttpRequestCustom.cpp b/WebCore/bindings/v8/custom/V8XMLHttpRequestCustom.cpp index 7204a61..39105de 100644 --- a/WebCore/bindings/v8/custom/V8XMLHttpRequestCustom.cpp +++ b/WebCore/bindings/v8/custom/V8XMLHttpRequestCustom.cpp @@ -33,10 +33,10 @@ #include "Frame.h" #include "V8Binding.h" -#include "V8Document.h" #include "V8CustomBinding.h" +#include "V8Document.h" +#include "V8File.h" #include "V8HTMLDocument.h" -#include "V8ObjectEventListener.h" #include "V8Proxy.h" #include "V8Utilities.h" #include "WorkerContext.h" @@ -44,227 +44,6 @@ namespace WebCore { -static PassRefPtr<EventListener> getEventListener(XMLHttpRequest* xmlHttpRequest, v8::Local<v8::Value> value, bool findOnly) -{ -#if ENABLE(WORKERS) - WorkerContextExecutionProxy* workerContextProxy = WorkerContextExecutionProxy::retrieve(); - if (workerContextProxy) - return workerContextProxy->findOrCreateObjectEventListener(value, false, findOnly); -#endif - - V8Proxy* proxy = V8Proxy::retrieve(xmlHttpRequest->scriptExecutionContext()); - if (proxy) { - V8EventListenerList* list = proxy->objectListeners(); - return findOnly ? list->findWrapper(value, false) : list->findOrCreateWrapper<V8ObjectEventListener>(proxy->frame(), value, false); - } - - return PassRefPtr<EventListener>(); -} - -ACCESSOR_GETTER(XMLHttpRequestOnabort) -{ - INC_STATS("DOM.XMLHttpRequest.onabort._get"); - XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder()); - if (xmlHttpRequest->onabort()) { - V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequest->onabort()); - v8::Local<v8::Object> v8Listener = listener->getListenerObject(); - return v8Listener; - } - return v8::Null(); -} - -ACCESSOR_SETTER(XMLHttpRequestOnabort) -{ - INC_STATS("DOM.XMLHttpRequest.onabort._set"); - XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder()); - if (value->IsNull()) { - if (xmlHttpRequest->onabort()) { - V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequest->onabort()); - v8::Local<v8::Object> v8Listener = listener->getListenerObject(); - removeHiddenDependency(info.Holder(), v8Listener, V8Custom::kXMLHttpRequestCacheIndex); - } - - // Clear the listener. - xmlHttpRequest->setOnabort(0); - } else { - RefPtr<EventListener> listener = getEventListener(xmlHttpRequest, value, false); - if (listener) { - xmlHttpRequest->setOnabort(listener); - createHiddenDependency(info.Holder(), value, V8Custom::kXMLHttpRequestCacheIndex); - } - } -} - -ACCESSOR_GETTER(XMLHttpRequestOnerror) -{ - INC_STATS("DOM.XMLHttpRequest.onerror._get"); - XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder()); - if (xmlHttpRequest->onerror()) { - RefPtr<V8ObjectEventListener> listener = static_cast<V8ObjectEventListener*>(xmlHttpRequest->onerror()); - v8::Local<v8::Object> v8Listener = listener->getListenerObject(); - return v8Listener; - } - return v8::Null(); -} - -ACCESSOR_SETTER(XMLHttpRequestOnerror) -{ - INC_STATS("DOM.XMLHttpRequest.onerror._set"); - XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder()); - if (value->IsNull()) { - if (xmlHttpRequest->onerror()) { - V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequest->onerror()); - v8::Local<v8::Object> v8Listener = listener->getListenerObject(); - removeHiddenDependency(info.Holder(), v8Listener, V8Custom::kXMLHttpRequestCacheIndex); - } - - // Clear the listener. - xmlHttpRequest->setOnerror(0); - } else { - RefPtr<EventListener> listener = getEventListener(xmlHttpRequest, value, false); - if (listener) { - xmlHttpRequest->setOnerror(listener); - createHiddenDependency(info.Holder(), value, V8Custom::kXMLHttpRequestCacheIndex); - } - } -} - -ACCESSOR_GETTER(XMLHttpRequestOnload) -{ - INC_STATS("DOM.XMLHttpRequest.onload._get"); - XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder()); - if (xmlHttpRequest->onload()) { - V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequest->onload()); - v8::Local<v8::Object> v8Listener = listener->getListenerObject(); - return v8Listener; - } - return v8::Null(); -} - -ACCESSOR_SETTER(XMLHttpRequestOnload) -{ - INC_STATS("DOM.XMLHttpRequest.onload._set"); - XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder()); - if (value->IsNull()) { - if (xmlHttpRequest->onload()) { - V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequest->onload()); - v8::Local<v8::Object> v8Listener = listener->getListenerObject(); - removeHiddenDependency(info.Holder(), v8Listener, V8Custom::kXMLHttpRequestCacheIndex); - } - - xmlHttpRequest->setOnload(0); - - } else { - RefPtr<EventListener> listener = getEventListener(xmlHttpRequest, value, false); - if (listener) { - xmlHttpRequest->setOnload(listener.get()); - createHiddenDependency(info.Holder(), value, V8Custom::kXMLHttpRequestCacheIndex); - } - } -} - -ACCESSOR_GETTER(XMLHttpRequestOnloadstart) -{ - INC_STATS("DOM.XMLHttpRequest.onloadstart._get"); - XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder()); - if (xmlHttpRequest->onloadstart()) { - V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequest->onloadstart()); - v8::Local<v8::Object> v8Listener = listener->getListenerObject(); - return v8Listener; - } - return v8::Null(); -} - -ACCESSOR_SETTER(XMLHttpRequestOnloadstart) -{ - INC_STATS("DOM.XMLHttpRequest.onloadstart._set"); - XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder()); - if (value->IsNull()) { - if (xmlHttpRequest->onloadstart()) { - V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequest->onloadstart()); - v8::Local<v8::Object> v8Listener = listener->getListenerObject(); - removeHiddenDependency(info.Holder(), v8Listener, V8Custom::kXMLHttpRequestCacheIndex); - } - - // Clear the listener. - xmlHttpRequest->setOnloadstart(0); - } else { - RefPtr<EventListener> listener = getEventListener(xmlHttpRequest, value, false); - if (listener) { - xmlHttpRequest->setOnloadstart(listener); - createHiddenDependency(info.Holder(), value, V8Custom::kXMLHttpRequestCacheIndex); - } - } -} - -ACCESSOR_GETTER(XMLHttpRequestOnprogress) -{ - INC_STATS("DOM.XMLHttpRequest.onprogress._get"); - XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder()); - if (xmlHttpRequest->onprogress()) { - V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequest->onprogress()); - v8::Local<v8::Object> v8Listener = listener->getListenerObject(); - return v8Listener; - } - return v8::Null(); -} - -ACCESSOR_SETTER(XMLHttpRequestOnprogress) -{ - INC_STATS("DOM.XMLHttpRequest.onprogress._set"); - XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder()); - if (value->IsNull()) { - if (xmlHttpRequest->onprogress()) { - V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequest->onprogress()); - v8::Local<v8::Object> v8Listener = listener->getListenerObject(); - removeHiddenDependency(info.Holder(), v8Listener, V8Custom::kXMLHttpRequestCacheIndex); - } - - // Clear the listener. - xmlHttpRequest->setOnprogress(0); - } else { - RefPtr<EventListener> listener = getEventListener(xmlHttpRequest, value, false); - if (listener) { - xmlHttpRequest->setOnprogress(listener); - createHiddenDependency(info.Holder(), value, V8Custom::kXMLHttpRequestCacheIndex); - } - } -} - -ACCESSOR_GETTER(XMLHttpRequestOnreadystatechange) -{ - INC_STATS("DOM.XMLHttpRequest.onreadystatechange._get"); - XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder()); - if (xmlHttpRequest->onreadystatechange()) { - V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequest->onreadystatechange()); - v8::Local<v8::Object> v8Listener = listener->getListenerObject(); - return v8Listener; - } - return v8::Null(); -} - -ACCESSOR_SETTER(XMLHttpRequestOnreadystatechange) -{ - INC_STATS("DOM.XMLHttpRequest.onreadystatechange._set"); - XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder()); - if (value->IsNull()) { - if (xmlHttpRequest->onreadystatechange()) { - V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequest->onreadystatechange()); - v8::Local<v8::Object> v8Listener = listener->getListenerObject(); - removeHiddenDependency(info.Holder(), v8Listener, V8Custom::kXMLHttpRequestCacheIndex); - } - - // Clear the listener. - xmlHttpRequest->setOnreadystatechange(0); - } else { - RefPtr<EventListener> listener = getEventListener(xmlHttpRequest, value, false); - if (listener) { - xmlHttpRequest->setOnreadystatechange(listener.get()); - createHiddenDependency(info.Holder(), value, V8Custom::kXMLHttpRequestCacheIndex); - } - } -} - ACCESSOR_GETTER(XMLHttpRequestResponseText) { // FIXME: This is only needed because webkit set this getter as custom. @@ -279,7 +58,7 @@ CALLBACK_FUNC_DECL(XMLHttpRequestAddEventListener) INC_STATS("DOM.XMLHttpRequest.addEventListener()"); XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, args.Holder()); - RefPtr<EventListener> listener = getEventListener(xmlHttpRequest, args[1], false); + RefPtr<EventListener> listener = V8DOMWrapper::getEventListener(xmlHttpRequest, args[1], false, ListenerFindOrCreate); if (listener) { String type = toWebCoreString(args[0]); bool useCapture = args[2]->BooleanValue(); @@ -295,7 +74,7 @@ CALLBACK_FUNC_DECL(XMLHttpRequestRemoveEventListener) INC_STATS("DOM.XMLHttpRequest.removeEventListener()"); XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, args.Holder()); - RefPtr<EventListener> listener = getEventListener(xmlHttpRequest, args[1], true); + RefPtr<EventListener> listener = V8DOMWrapper::getEventListener(xmlHttpRequest, args[1], false, ListenerFindOnly); if (listener) { String type = toWebCoreString(args[0]); bool useCapture = args[2]->BooleanValue(); @@ -323,22 +102,9 @@ CALLBACK_FUNC_DECL(XMLHttpRequestOpen) String method = toWebCoreString(args[0]); String urlstring = toWebCoreString(args[1]); - ScriptExecutionContext* context = 0; -#if ENABLE(WORKERS) - WorkerContextExecutionProxy* workerContextProxy = WorkerContextExecutionProxy::retrieve(); - if (workerContextProxy) { - context = workerContextProxy->workerContext(); - ASSERT(context); - } -#endif - - if (!context) { - V8Proxy* proxy = V8Proxy::retrieve(); - if (!proxy) - return v8::Undefined(); - context = proxy->frame()->document(); - ASSERT(context); - } + ScriptExecutionContext* context = getScriptExecutionContext(); + if (!context) + return v8::Undefined(); KURL url = context->completeURL(urlstring); @@ -379,12 +145,16 @@ CALLBACK_FUNC_DECL(XMLHttpRequestSend) xmlHttpRequest->send(ec); else { v8::Handle<v8::Value> arg = args[0]; - // FIXME: upstream handles "File" objects too. if (IsDocumentType(arg)) { v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg); Document* document = V8DOMWrapper::convertDOMWrapperToNode<Document>(object); ASSERT(document); xmlHttpRequest->send(document, ec); + } else if (V8File::HasInstance(arg)) { + v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg); + File* file = V8DOMWrapper::convertDOMWrapperToNative<File>(object); + ASSERT(file); + xmlHttpRequest->send(file, ec); } else xmlHttpRequest->send(toWebCoreStringWithNullCheck(arg), ec); } diff --git a/WebCore/bindings/v8/custom/V8XMLHttpRequestUploadCustom.cpp b/WebCore/bindings/v8/custom/V8XMLHttpRequestUploadCustom.cpp index bbc69dd..9323f71 100644 --- a/WebCore/bindings/v8/custom/V8XMLHttpRequestUploadCustom.cpp +++ b/WebCore/bindings/v8/custom/V8XMLHttpRequestUploadCustom.cpp @@ -34,7 +34,6 @@ #include "ExceptionCode.h" #include "V8Binding.h" #include "V8CustomBinding.h" -#include "V8ObjectEventListener.h" #include "V8Proxy.h" #include "V8Utilities.h" #include "XMLHttpRequest.h" @@ -43,212 +42,14 @@ namespace WebCore { -ACCESSOR_GETTER(XMLHttpRequestUploadOnabort) -{ - INC_STATS("DOM.XMLHttpRequestUpload.onabort._get"); - XMLHttpRequestUpload* xmlHttpRequestUpload = V8DOMWrapper::convertToNativeObject<XMLHttpRequestUpload>(V8ClassIndex::XMLHTTPREQUESTUPLOAD, info.Holder()); - if (xmlHttpRequestUpload->onabort()) { - V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequestUpload->onabort()); - v8::Local<v8::Object> v8Listener = listener->getListenerObject(); - return v8Listener; - } - return v8::Null(); -} - -ACCESSOR_SETTER(XMLHttpRequestUploadOnabort) -{ - INC_STATS("DOM.XMLHttpRequestUpload.onabort._set"); - XMLHttpRequestUpload* xmlHttpRequestUpload = V8DOMWrapper::convertToNativeObject<XMLHttpRequestUpload>(V8ClassIndex::XMLHTTPREQUESTUPLOAD, info.Holder()); - if (value->IsNull()) { - if (xmlHttpRequestUpload->onabort()) { - V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequestUpload->onabort()); - v8::Local<v8::Object> v8Listener = listener->getListenerObject(); - removeHiddenDependency(info.Holder(), v8Listener, V8Custom::kXMLHttpRequestCacheIndex); - } - - // Clear the listener. - xmlHttpRequestUpload->setOnabort(0); - } else { - XMLHttpRequest* xmlHttpRequest = xmlHttpRequestUpload->associatedXMLHttpRequest(); - V8Proxy* proxy = V8Proxy::retrieve(xmlHttpRequest->scriptExecutionContext()); - if (!proxy) - return; - - RefPtr<EventListener> listener = proxy->objectListeners()->findOrCreateWrapper<V8ObjectEventListener>(proxy->frame(), value, false); - if (listener) { - xmlHttpRequestUpload->setOnabort(listener); - createHiddenDependency(info.Holder(), value, V8Custom::kXMLHttpRequestCacheIndex); - } - } -} - -ACCESSOR_GETTER(XMLHttpRequestUploadOnerror) -{ - INC_STATS("DOM.XMLHttpRequestUpload.onerror._get"); - XMLHttpRequestUpload* xmlHttpRequestUpload = V8DOMWrapper::convertToNativeObject<XMLHttpRequestUpload>(V8ClassIndex::XMLHTTPREQUESTUPLOAD, info.Holder()); - if (xmlHttpRequestUpload->onerror()) { - V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequestUpload->onerror()); - v8::Local<v8::Object> v8Listener = listener->getListenerObject(); - return v8Listener; - } - return v8::Null(); -} - -ACCESSOR_SETTER(XMLHttpRequestUploadOnerror) -{ - INC_STATS("DOM.XMLHttpRequestUpload.onerror._set"); - XMLHttpRequestUpload* xmlHttpRequestUpload = V8DOMWrapper::convertToNativeObject<XMLHttpRequestUpload>(V8ClassIndex::XMLHTTPREQUESTUPLOAD, info.Holder()); - if (value->IsNull()) { - if (xmlHttpRequestUpload->onerror()) { - V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequestUpload->onerror()); - v8::Local<v8::Object> v8Listener = listener->getListenerObject(); - removeHiddenDependency(info.Holder(), v8Listener, V8Custom::kXMLHttpRequestCacheIndex); - } - - // Clear the listener. - xmlHttpRequestUpload->setOnerror(0); - } else { - XMLHttpRequest* xmlHttpRequest = xmlHttpRequestUpload->associatedXMLHttpRequest(); - V8Proxy* proxy = V8Proxy::retrieve(xmlHttpRequest->scriptExecutionContext()); - if (!proxy) - return; - - RefPtr<EventListener> listener = proxy->objectListeners()->findOrCreateWrapper<V8ObjectEventListener>(proxy->frame(), value, false); - if (listener) { - xmlHttpRequestUpload->setOnerror(listener); - createHiddenDependency(info.Holder(), value, V8Custom::kXMLHttpRequestCacheIndex); - } - } -} - -ACCESSOR_GETTER(XMLHttpRequestUploadOnload) -{ - INC_STATS("DOM.XMLHttpRequestUpload.onload._get"); - XMLHttpRequestUpload* xmlHttpRequestUpload = V8DOMWrapper::convertToNativeObject<XMLHttpRequestUpload>(V8ClassIndex::XMLHTTPREQUESTUPLOAD, info.Holder()); - if (xmlHttpRequestUpload->onload()) { - V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequestUpload->onload()); - v8::Local<v8::Object> v8Listener = listener->getListenerObject(); - return v8Listener; - } - return v8::Null(); -} - -ACCESSOR_SETTER(XMLHttpRequestUploadOnload) -{ - INC_STATS("DOM.XMLHttpRequestUpload.onload._set"); - XMLHttpRequestUpload* xmlHttpRequestUpload = V8DOMWrapper::convertToNativeObject<XMLHttpRequestUpload>(V8ClassIndex::XMLHTTPREQUESTUPLOAD, info.Holder()); - if (value->IsNull()) { - if (xmlHttpRequestUpload->onload()) { - V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequestUpload->onload()); - v8::Local<v8::Object> v8Listener = listener->getListenerObject(); - removeHiddenDependency(info.Holder(), v8Listener, V8Custom::kXMLHttpRequestCacheIndex); - } - - // Clear the listener. - xmlHttpRequestUpload->setOnload(0); - } else { - XMLHttpRequest* xmlHttpRequest = xmlHttpRequestUpload->associatedXMLHttpRequest(); - V8Proxy* proxy = V8Proxy::retrieve(xmlHttpRequest->scriptExecutionContext()); - if (!proxy) - return; - - RefPtr<EventListener> listener = proxy->objectListeners()->findOrCreateWrapper<V8ObjectEventListener>(proxy->frame(), value, false); - if (listener) { - xmlHttpRequestUpload->setOnload(listener); - createHiddenDependency(info.Holder(), value, V8Custom::kXMLHttpRequestCacheIndex); - } - } -} - -ACCESSOR_GETTER(XMLHttpRequestUploadOnloadstart) -{ - INC_STATS("DOM.XMLHttpRequestUpload.onloadstart._get"); - XMLHttpRequestUpload* xmlHttpRequestUpload = V8DOMWrapper::convertToNativeObject<XMLHttpRequestUpload>(V8ClassIndex::XMLHTTPREQUESTUPLOAD, info.Holder()); - if (xmlHttpRequestUpload->onloadstart()) { - V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequestUpload->onloadstart()); - v8::Local<v8::Object> v8Listener = listener->getListenerObject(); - return v8Listener; - } - return v8::Null(); -} - -ACCESSOR_SETTER(XMLHttpRequestUploadOnloadstart) -{ - INC_STATS("DOM.XMLHttpRequestUpload.onloadstart._set"); - XMLHttpRequestUpload* xmlHttpRequestUpload = V8DOMWrapper::convertToNativeObject<XMLHttpRequestUpload>(V8ClassIndex::XMLHTTPREQUESTUPLOAD, info.Holder()); - if (value->IsNull()) { - if (xmlHttpRequestUpload->onloadstart()) { - V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequestUpload->onloadstart()); - v8::Local<v8::Object> v8Listener = listener->getListenerObject(); - removeHiddenDependency(info.Holder(), v8Listener, V8Custom::kXMLHttpRequestCacheIndex); - } - - // Clear the listener. - xmlHttpRequestUpload->setOnloadstart(0); - } else { - XMLHttpRequest* xmlHttpRequest = xmlHttpRequestUpload->associatedXMLHttpRequest(); - V8Proxy* proxy = V8Proxy::retrieve(xmlHttpRequest->scriptExecutionContext()); - if (!proxy) - return; - - RefPtr<EventListener> listener = proxy->objectListeners()->findOrCreateWrapper<V8ObjectEventListener>(proxy->frame(), value, false); - if (listener) { - xmlHttpRequestUpload->setOnloadstart(listener); - createHiddenDependency(info.Holder(), value, V8Custom::kXMLHttpRequestCacheIndex); - } - } -} - -ACCESSOR_GETTER(XMLHttpRequestUploadOnprogress) -{ - INC_STATS("DOM.XMLHttpRequestUpload.onprogress._get"); - XMLHttpRequestUpload* xmlHttpRequestUpload = V8DOMWrapper::convertToNativeObject<XMLHttpRequestUpload>(V8ClassIndex::XMLHTTPREQUESTUPLOAD, info.Holder()); - if (xmlHttpRequestUpload->onprogress()) { - V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequestUpload->onprogress()); - v8::Local<v8::Object> v8Listener = listener->getListenerObject(); - return v8Listener; - } - return v8::Null(); -} - -ACCESSOR_SETTER(XMLHttpRequestUploadOnprogress) -{ - INC_STATS("DOM.XMLHttpRequestUpload.onprogress._set"); - XMLHttpRequestUpload* xmlHttpRequestUpload = V8DOMWrapper::convertToNativeObject<XMLHttpRequestUpload>(V8ClassIndex::XMLHTTPREQUESTUPLOAD, info.Holder()); - if (value->IsNull()) { - if (xmlHttpRequestUpload->onprogress()) { - V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequestUpload->onprogress()); - v8::Local<v8::Object> v8Listener = listener->getListenerObject(); - removeHiddenDependency(info.Holder(), v8Listener, V8Custom::kXMLHttpRequestCacheIndex); - } - - // Clear the listener. - xmlHttpRequestUpload->setOnprogress(0); - } else { - XMLHttpRequest* xmlHttpRequest = xmlHttpRequestUpload->associatedXMLHttpRequest(); - V8Proxy* proxy = V8Proxy::retrieve(xmlHttpRequest->scriptExecutionContext()); - if (!proxy) - return; - - RefPtr<EventListener> listener = proxy->objectListeners()->findOrCreateWrapper<V8ObjectEventListener>(proxy->frame(), value, false); - if (listener) { - xmlHttpRequestUpload->setOnprogress(listener); - createHiddenDependency(info.Holder(), value, V8Custom::kXMLHttpRequestCacheIndex); - } - } -} - CALLBACK_FUNC_DECL(XMLHttpRequestUploadAddEventListener) { INC_STATS("DOM.XMLHttpRequestUpload.addEventListener()"); XMLHttpRequestUpload* xmlHttpRequestUpload = V8DOMWrapper::convertToNativeObject<XMLHttpRequestUpload>(V8ClassIndex::XMLHTTPREQUESTUPLOAD, args.Holder()); XMLHttpRequest* xmlHttpRequest = xmlHttpRequestUpload->associatedXMLHttpRequest(); - V8Proxy* proxy = V8Proxy::retrieve(xmlHttpRequest->scriptExecutionContext()); - if (!proxy) - return v8::Undefined(); - RefPtr<EventListener> listener = proxy->objectListeners()->findOrCreateWrapper<V8ObjectEventListener>(proxy->frame(), args[1], false); + RefPtr<EventListener> listener = V8DOMWrapper::getEventListener(xmlHttpRequest, args[1], false, ListenerFindOrCreate); if (listener) { String type = toWebCoreString(args[0]); bool useCapture = args[2]->BooleanValue(); @@ -265,12 +66,8 @@ CALLBACK_FUNC_DECL(XMLHttpRequestUploadRemoveEventListener) XMLHttpRequestUpload* xmlHttpRequestUpload = V8DOMWrapper::convertToNativeObject<XMLHttpRequestUpload>(V8ClassIndex::XMLHTTPREQUESTUPLOAD, args.Holder()); XMLHttpRequest* xmlHttpRequest = xmlHttpRequestUpload->associatedXMLHttpRequest(); - V8Proxy* proxy = V8Proxy::retrieve(xmlHttpRequest->scriptExecutionContext()); - if (!proxy) - return v8::Undefined(); // Probably leaked. - - RefPtr<EventListener> listener = proxy->objectListeners()->findWrapper(args[1], false); + RefPtr<EventListener> listener = V8DOMWrapper::getEventListener(xmlHttpRequest, args[1], false, ListenerFindOnly); if (listener) { String type = toWebCoreString(args[0]); bool useCapture = args[2]->BooleanValue(); diff --git a/WebCore/bindings/v8/npruntime.cpp b/WebCore/bindings/v8/npruntime.cpp index 64a1927..35015b0 100644 --- a/WebCore/bindings/v8/npruntime.cpp +++ b/WebCore/bindings/v8/npruntime.cpp @@ -39,7 +39,7 @@ // The static initializer here should work okay, but we want to avoid // static initialization in general. -namespace { +namespace npruntime { // We use StringKey here as the key-type to avoid a string copy to // construct the map key and for faster comparisons than strcmp. @@ -112,7 +112,10 @@ struct StringKeyHash { static const bool safeToCompareToEmptyOrDeleted = true; }; -} // namespace +} // namespace npruntime + +using npruntime::StringKey; +using npruntime::StringKeyHash; // Implement HashTraits<StringKey> struct StringKeyHashTraits : WTF::GenericHashTraits<StringKey> { @@ -309,6 +312,15 @@ void _NPN_DeallocateObject(NPObject* npObject) } } +#if PLATFORM(ANDROID) +// Android uses NPN_ReleaseObject (the 'public' version of _NPN_ReleaseObject) +// in WebCoreFrameBridge.cpp. See http://trac.webkit.org/changeset/47021. +// TODO: Upstream this to webkit.org. +void NPN_ReleaseObject(NPObject *obj) +{ + _NPN_ReleaseObject(obj); +} +#endif void _NPN_ReleaseObject(NPObject* npObject) { ASSERT(npObject); |