Index: Source/bindings/core/dart/DartNativeUtilities.cpp |
diff --git a/Source/bindings/core/dart/DartNativeUtilities.cpp b/Source/bindings/core/dart/DartNativeUtilities.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..6c49b12c41a987a7efccca89ca45791662bce6c0 |
--- /dev/null |
+++ b/Source/bindings/core/dart/DartNativeUtilities.cpp |
@@ -0,0 +1,357 @@ |
+// Copyright 2012, 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 "bindings/core/dart/DartNativeUtilities.h" |
+ |
+#include "bindings/core/dart/DartController.h" |
+#include "bindings/core/dart/DartDOMData.h" |
+#include "bindings/core/dart/DartDOMWrapper.h" |
+#include "bindings/core/dart/DartJsInterop.h" |
+#include "bindings/core/dart/DartUtilities.h" |
+#include "bindings/core/dart/V8Converter.h" |
+#include "bindings/core/v8/ScriptController.h" |
+#include "bindings/core/v8/V8Document.h" |
+#include "bindings/core/v8/npruntime_impl.h" |
+#include "core/dom/Document.h" |
+#include "core/dom/ExecutionContext.h" |
+#include "core/dom/ExecutionContextTask.h" |
+#include "core/dom/custom/CustomElementProcessingStack.h" |
+#include "core/dom/custom/CustomElementRegistrationContext.h" |
+#include "core/frame/LocalFrame.h" |
+#include "core/html/HTMLElement.h" |
+#include "platform/RuntimeEnabledFeatures.h" |
+ |
+#include "wtf/HashMap.h" |
+#include "wtf/StdLibExtras.h" |
+#include "wtf/text/CString.h" |
+#include "wtf/text/StringHash.h" |
+#include <bindings/npruntime.h> |
+#include <dart_api.h> |
+ |
+namespace blink { |
+ |
+namespace DartNativeUtilitiesInternal { |
+ |
+/** |
+ * Gadget for testing. |
+ * |
+ * With DART_FORWARDING_PRINT environment variable set, it invokes dartPrint |
+ * JavaScript function on global object to communicate to Dart test framework. |
+ */ |
+static void forwardingPrint(Dart_NativeArguments args) |
+{ |
+ v8::Isolate* v8Isolate = v8::Isolate::GetCurrent(); |
+ v8::HandleScope v8Scope(v8Isolate); |
+ ExecutionContext* scriptExecutionContext = DartUtilities::scriptExecutionContext(); |
+ ASSERT(scriptExecutionContext); |
+ |
+ DartController* dartController = DartController::retrieve(scriptExecutionContext); |
+ if (!dartController) |
+ return; |
+ LocalFrame* frame = dartController->frame(); |
+ v8::Handle<v8::Context> v8Context = toV8Context(frame, DOMWrapperWorld::mainWorld()); |
+ v8::Context::Scope scope(v8Context); |
+ v8::TryCatch tryCatch; |
+ |
+ v8::Handle<v8::Value> function = v8Context->Global()->Get(v8::String::NewFromUtf8(v8Isolate, "dartPrint")); |
+ if (function.IsEmpty() || !function->IsFunction()) |
+ return; |
+ |
+ v8::Handle<v8::Value> message = V8Converter::stringToV8(Dart_GetNativeArgument(args, 0)); |
+ function.As<v8::Function>()->Call(v8Context->Global(), 1, &message); |
+} |
+ |
+static void getNewIsolateId(Dart_NativeArguments args) |
+{ |
+ static int isolateId = 0; |
+ Dart_SetReturnValue(args, DartUtilities::intToDart(isolateId++)); |
+} |
+ |
+// Use the first argument of a function as the receiver (this) of the native |
+// C++ implementation of a few special DOM calls (createElement, registerElement, |
+// etc.). |
+Document * getBlinkJsoAsDocument(Dart_NativeArguments args, DartDOMData* domData) |
+{ |
+ Dart_Handle exception = 0; |
+ { |
+ v8::Local<v8::Value> value = JsInterop::fromDart(domData, Dart_GetNativeArgument(args, 0), exception); |
+ if (exception) |
+ goto fail; |
+ |
+ ASSERT(value->IsObject()); |
+ |
+ v8::Handle<v8::Object> object = value.As<v8::Object>(); |
+ v8::Isolate* isolate = object->CreationContext()->GetIsolate(); |
+ if (V8Document::hasInstance(object, isolate)) { |
+ // FIXME(jacobr): there has to be a faster way to perform this check. |
+ if (!object->CreationContext()->Global()->StrictEquals(DartUtilities::currentV8Context()->Global())) { |
+ // The object is from a different context so do not convert. |
+ return 0; |
+ } |
+ |
+ // TODO(terry): Should probably check that this really is a Document. |
+ Document* doc = V8Document::toImpl(object); |
+ return doc; |
+ } |
+ } |
+ |
+fail: |
+ Dart_ThrowException(exception); |
+ return 0; |
+} |
+ |
+// dart:html (Dartium only) hookups up the Custom element using JsInterop when |
+// JS createCallback is invoked this function is called to allocate the custom |
+// Dart class (user's class) setup the blink_jsObject to point to the JS custom |
+// element just created and call the custom Dart class created constructor. |
+void customConstructorCreate(Dart_NativeArguments args) |
+{ |
+ Dart_Handle exception = 0; |
+ { |
+ DartDOMData* domData = DartDOMData::current(); |
+ |
+ Dart_Handle dartElementType = Dart_GetNativeArgument(args, 0); |
+ ASSERT(Dart_IsType(dartElementType)); |
+ |
+ Dart_Handle blinkJsObject = Dart_GetNativeArgument(args, 1); |
+ |
+ Dart_Handle newObject = Dart_Allocate(dartElementType); |
+ ASSERT(!Dart_IsError(newObject)); |
+ |
+ Dart_Handle jsObject = Dart_SetField(newObject, Dart_NewStringFromCString("blink_jsObject"), blinkJsObject); |
+ if (Dart_IsError(jsObject)) { |
+ DartUtilities::reportProblem(domData->scriptExecutionContext(), jsObject); |
+ goto fail; |
+ } |
+ |
+ newObject = Dart_InvokeConstructor(newObject, Dart_NewStringFromCString("created"), 0, 0); |
+ if (Dart_IsError(newObject)) { |
+ DartUtilities::reportProblem(domData->scriptExecutionContext(), newObject); |
+ goto fail; |
+ } |
+ |
+ Dart_SetReturnValue(args, newObject); |
+ return; |
+ } |
+ |
+fail: |
+ Dart_ThrowException(exception); |
+ ASSERT_NOT_REACHED(); |
+} |
+ |
+void initializeCustomElement(Dart_NativeArguments args) |
+{ |
+ ASSERT_NOT_REACHED(); |
+} |
+ |
+void spawnDomUri(Dart_NativeArguments args) |
+{ |
+ Dart_Handle exception = 0; |
+ { |
+ ExecutionContext* scriptExecutionContext = DartUtilities::scriptExecutionContext(); |
+ ASSERT(scriptExecutionContext); |
+ DartController* dartController = DartController::retrieve(scriptExecutionContext); |
+ if (!dartController) |
+ return; |
+ DartStringAdapter uri = DartUtilities::dartToString(args, 0, exception); |
+ if (exception) |
+ goto fail; |
+ // TODO(vsm): Return isolate future. |
+ dartController->spawnDomUri(uri); |
+ return; |
+ } |
+ |
+fail: |
+ Dart_ThrowException(exception); |
+ ASSERT_NOT_REACHED(); |
+} |
+ |
+void changeElementWrapper(Dart_NativeArguments args) |
+{ |
+ ASSERT_NOT_REACHED(); |
+} |
+ |
+class SpawnDomFunctionTask : public ExecutionContextTask { |
+public: |
+ SpawnDomFunctionTask(const char* libraryUrl, const char* functionName, Dart_Port replyTo, DartDOMData* parentDOMData) |
+ : m_libraryUrl(strdup(libraryUrl)) |
+ , m_functionName(strdup(functionName)) |
+ , m_replyTo(replyTo) |
+ , m_parentDOMData(parentDOMData) { } |
+ |
+ ~SpawnDomFunctionTask() |
+ { |
+ free(m_functionName); |
+ free(m_libraryUrl); |
+ } |
+ |
+ virtual void performTask(ExecutionContext* context) |
+ { |
+ ASSERT(isMainThread()); |
+ // controller can be 0 if context has been partially destroyed. |
+ if (DartController* controller = DartController::retrieve(context)) { |
+ controller->spawnHelperDomIsolate(String(m_libraryUrl), |
+ String(m_functionName), m_parentDOMData, m_replyTo); |
+ } |
+ } |
+ |
+private: |
+ char* const m_libraryUrl; |
+ char* const m_functionName; |
+ const Dart_Port m_replyTo; |
+ DartDOMData * const m_parentDOMData; |
+}; |
+ |
+static Dart_Handle spawnDomHelperErrorMessage() |
+{ |
+ return Dart_NewStringFromCString("spawnDomHelper expects a top-level function."); |
+} |
+ |
+static void spawnDomHelper(Dart_NativeArguments args) |
+{ |
+ // NB: can be invoked on _any_ thread. DartUtilities assume main thread. |
+ Dart_Handle exception = 0; |
+ { |
+ Dart_Handle closure = Dart_GetNativeArgument(args, 0); |
+ ASSERT(!Dart_IsError(closure)); |
+ if (!Dart_IsClosure(closure)) { |
+ exception = spawnDomHelperErrorMessage(); |
+ goto fail; |
+ } |
+ |
+ Dart_Handle function = Dart_ClosureFunction(closure); |
+ ASSERT(!Dart_IsError(function)); |
+ bool isStatic = false; |
+ Dart_Handle ALLOW_UNUSED result = Dart_FunctionIsStatic(function, &isStatic); |
+ ASSERT(!Dart_IsError(result)); |
+ if (!isStatic) { |
+ exception = spawnDomHelperErrorMessage(); |
+ goto fail; |
+ } |
+ |
+ Dart_Handle container = Dart_FunctionOwner(function); |
+ ASSERT(!Dart_IsError(container)); |
+ // FIXME: Check if the container is a function once dartbug.com/4337 is resolved. |
+ if (!Dart_IsLibrary(container)) { |
+ // FIXME: Support static class methods. |
+ exception = spawnDomHelperErrorMessage(); |
+ goto fail; |
+ } |
+ Dart_Handle libraryUrlHandle = Dart_LibraryUrl(container); |
+ ASSERT(!Dart_IsError(libraryUrlHandle)); |
+ const char* libraryUrl = 0; |
+ Dart_StringToCString(libraryUrlHandle, &libraryUrl); |
+ |
+ Dart_Handle functionNameHandle = Dart_FunctionName(function); |
+ ASSERT(!Dart_IsError(functionNameHandle)); |
+ const char* functionName; |
+ Dart_StringToCString(functionNameHandle, &functionName); |
+ |
+ Dart_Port portId = ILLEGAL_PORT; |
+ Dart_IntegerToInt64(Dart_GetNativeArgument(args, 1), &portId); |
+ |
+ DartDOMData* parentDOMData = DartDOMData::current(); |
+ |
+ ExecutionContext* context = DartDOMData::current()->scriptExecutionContext(); |
+ if (!context) { |
+ exception = DartUtilities::internalErrorException("[spawnDomHelper] failed to retrieve ExecutionContext"); |
+ goto fail; |
+ } |
+ |
+ context->postTask(adoptPtr(new SpawnDomFunctionTask(libraryUrl, functionName, portId, parentDOMData))); |
+ return; |
+ } |
+ |
+fail: |
+ Dart_ThrowException(exception); |
+ ASSERT_NOT_REACHED(); |
+} |
+ |
+} // namespace DartNativeUtilitiesInternal |
+ |
+extern Dart_NativeFunction blinkSnapshotResolver(Dart_Handle name, int argumentCount, bool* autoSetupScope); |
+extern Dart_NativeFunction customDartDOMStringMapResolver(Dart_Handle name, int argumentCount, bool* autoSetupScope); |
+ |
+static DartNativeEntry nativeEntries[] = { |
+ { DartNativeUtilitiesInternal::forwardingPrint, 1, "Utils_forwardingPrint" }, |
+ { DartNativeUtilitiesInternal::getNewIsolateId, 0, "Utils_getNewIsolateId" }, |
+ { DartNativeUtilitiesInternal::changeElementWrapper, 2, "Utils_changeElementWrapper" }, |
+ { DartNativeUtilitiesInternal::customConstructorCreate, 2, "Utils_constructor_create" }, |
+ { DartNativeUtilitiesInternal::spawnDomUri, 1, "Utils_spawnDomUri" }, |
+ { 0, 0, 0 }, |
+}; |
+ |
+Dart_NativeFunction commonHtmlResolver(Dart_Handle name, int argumentCount, bool* autoSetupScope) |
+{ |
+ String str = DartUtilities::toString(name); |
+ if (argumentCount == 2 && str == "Utils_spawnDomHelper") |
+ return DartNativeUtilitiesInternal::spawnDomHelper; |
+ return 0; |
+} |
+ |
+Dart_NativeFunction domIsolateHtmlResolver(Dart_Handle name, int argumentCount, bool* autoSetupScope) |
+{ |
+ // Some utility functions. |
+ if (Dart_NativeFunction func = commonHtmlResolver(name, argumentCount, autoSetupScope)) |
+ return func; |
+ if (Dart_NativeFunction func = JsInterop::resolver(name, argumentCount, autoSetupScope)) |
+ return func; |
+ |
+ String str = DartUtilities::toString(name); |
+ ASSERT(autoSetupScope); |
+ *autoSetupScope = true; |
+ for (intptr_t i = 0; nativeEntries[i].nativeFunction != 0; i++) { |
+ if (argumentCount == nativeEntries[i].argumentCount && str == nativeEntries[i].name) { |
+ return nativeEntries[i].nativeFunction; |
+ } |
+ } |
+ return 0; |
+} |
+ |
+ |
+const uint8_t* domIsolateHtmlSymbolizer(Dart_NativeFunction nf) |
+{ |
+ const uint8_t* r = 0; |
+ r = JsInterop::symbolizer(nf); |
+ if (r) { |
+ return r; |
+ } |
+ |
+ for (intptr_t i = 0; nativeEntries[i].nativeFunction != 0; i++) { |
+ if (nf == nativeEntries[i].nativeFunction) { |
+ return reinterpret_cast<const uint8_t*>(nativeEntries[i].name); |
+ } |
+ } |
+ |
+ return 0; |
+} |
+ |
+ |
+} |