OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * |
| 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are |
| 6 * met: |
| 7 * |
| 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above |
| 11 * copyright notice, this list of conditions and the following disclaimer |
| 12 * in the documentation and/or other materials provided with the |
| 13 * distribution. |
| 14 * * Neither the name of Google Inc. nor the names of its |
| 15 * contributors may be used to endorse or promote products derived from |
| 16 * this software without specific prior written permission. |
| 17 * |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ |
| 30 |
| 31 #include "config.h" |
| 32 #include "bindings/v8/V8PromiseUtilities.h" |
| 33 |
| 34 #include "PromiseCustomScript.h" |
| 35 #include "PromiseResolverCustomScript.h" |
| 36 #include "V8PromiseResolver.h" |
| 37 #include "bindings/v8/ScriptFunctionCall.h" |
| 38 #include "bindings/v8/ScriptValue.h" |
| 39 #include "bindings/v8/V8Binding.h" |
| 40 #include "bindings/v8/V8HiddenPropertyName.h" |
| 41 #include "bindings/v8/V8ScriptRunner.h" |
| 42 #include "bindings/v8/V8ThrowException.h" |
| 43 #include "core/dom/Document.h" |
| 44 #include "core/dom/ExceptionCode.h" |
| 45 #include "core/page/DOMWindow.h" |
| 46 #include "core/platform/Task.h" |
| 47 #include "wtf/Functional.h" |
| 48 #include "wtf/PassOwnPtr.h" |
| 49 |
| 50 #include <v8.h> |
| 51 |
| 52 namespace WebCore { |
| 53 namespace { |
| 54 class PromiseCallbackTask : public ScriptExecutionContext::Task { |
| 55 public: |
| 56 PromiseCallbackTask(v8::Handle<v8::Function> callback): m_callback(callback)
{ } |
| 57 virtual ~PromiseCallbackTask() { } |
| 58 |
| 59 virtual void performTask(ScriptExecutionContext*) OVERRIDE; |
| 60 |
| 61 private: |
| 62 ScriptValue m_callback; |
| 63 }; |
| 64 |
| 65 void PromiseCallbackTask::performTask(ScriptExecutionContext* context) |
| 66 { |
| 67 ASSERT(context->isDocument()); |
| 68 ScriptState* state = mainWorldScriptState(static_cast<Document*>(context)->f
rame()); |
| 69 v8::HandleScope handleScope; |
| 70 if (!state) { |
| 71 // We can't execute the task. Ignore it. |
| 72 return; |
| 73 } |
| 74 v8::Handle<v8::Context> v8context = state->context(); |
| 75 if (v8context.IsEmpty()) { |
| 76 // We can't execute the task. Ignore it. |
| 77 return; |
| 78 } |
| 79 v8::Context::Scope scope(v8context); |
| 80 ScriptCallback(state, m_callback).call(); |
| 81 }; |
| 82 |
| 83 v8::Handle<v8::Value> postTask(const v8::Arguments& args) |
| 84 { |
| 85 DOMWindow* window = activeDOMWindow(); |
| 86 if (args.Length() != 1 || args[0].IsEmpty() || !args[0]->IsFunction()) |
| 87 return V8ThrowException::throwError(v8TypeError, "postTask takes exact o
ne function argument.", args.GetIsolate()); |
| 88 v8::Handle<v8::Function> callback = args[0].As<v8::Function>(); |
| 89 if (!window) |
| 90 return V8ThrowException::setDOMException(INVALID_STATE_ERR, args.GetIsol
ate()); |
| 91 Document* document = window->document(); |
| 92 if (!document) |
| 93 return V8ThrowException::setDOMException(INVALID_STATE_ERR, args.GetIsol
ate()); |
| 94 document->postTask(adoptPtr(new PromiseCallbackTask(callback))); |
| 95 return v8Undefined(); |
| 96 } |
| 97 |
| 98 } // unnamed namespace |
| 99 |
| 100 v8::Handle<v8::Function> V8PromiseUtilities::promiseConstructor(v8::Isolate* iso
late) |
| 101 { |
| 102 const char name[] = "Promise"; |
| 103 v8::Local<v8::Object> global = isolate->GetCurrentContext()->Global(); |
| 104 v8::Local<v8::Value> placeholderValue = global->Get(v8::String::NewSymbol(na
me)); |
| 105 if (placeholderValue.IsEmpty() || !placeholderValue->IsObject()) |
| 106 return V8ThrowException::throwError(v8GeneralError, "The placeholder for
Promise is not found.", isolate).As<v8::Function>(); |
| 107 v8::Local<v8::Object> placeholder = placeholderValue.As<v8::Object>(); |
| 108 v8::Handle<v8::Value> value = placeholder->GetHiddenValue(V8HiddenPropertyNa
me::jsConstructor()); |
| 109 if (!value.IsEmpty() && value->IsFunction()) { |
| 110 // Return the stored constructor. |
| 111 return value.As<v8::Function>(); |
| 112 } |
| 113 |
| 114 value = V8ScriptRunner::compileAndRunInternalScript(v8String(String(Promise_
js, sizeof(Promise_js)), isolate), isolate); |
| 115 if (value.IsEmpty()) |
| 116 return v8::Handle<v8::Function>(); |
| 117 if (!value->IsFunction()) |
| 118 return V8ThrowException::throwError(v8TypeError, "Promise implementation
must be a function.", isolate).As<v8::Function>(); |
| 119 |
| 120 v8::Handle<v8::Function> impl = value.As<v8::Function>(); |
| 121 v8::Handle<v8::Value> args[] = { |
| 122 v8::FunctionTemplate::New(postTask)->GetFunction(), |
| 123 }; |
| 124 value = impl->Call(v8::Object::New(), sizeof(args) / sizeof(args[0]), &args[
0]); |
| 125 if (value.IsEmpty()) |
| 126 return v8::Handle<v8::Function>(); |
| 127 if (!value->IsFunction()) |
| 128 return V8ThrowException::throwError(v8TypeError, "Promise constructor mu
st be a function.", isolate).As<v8::Function>(); |
| 129 placeholder->SetHiddenValue(V8HiddenPropertyName::jsConstructor(), value); |
| 130 return value.As<v8::Function>(); |
| 131 } |
| 132 |
| 133 v8::Handle<v8::Value> V8PromiseUtilities::promisePrototype(v8::Isolate* isolate) |
| 134 { |
| 135 v8::Handle<v8::Object> constructor = promiseConstructor(isolate); |
| 136 if (constructor.IsEmpty()) |
| 137 return constructor; |
| 138 return constructor->Get(v8::String::NewSymbol("prototype")); |
| 139 } |
| 140 |
| 141 v8::Handle<v8::Function> V8PromiseUtilities::promiseResolverConstructor(v8::Isol
ate* isolate) |
| 142 { |
| 143 const char name[] = "PromiseResolver"; |
| 144 v8::Local<v8::Object> global = isolate->GetCurrentContext()->Global(); |
| 145 v8::Local<v8::Value> placeholderValue = global->Get(v8::String::NewSymbol(na
me)); |
| 146 if (placeholderValue.IsEmpty() || !placeholderValue->IsObject()) |
| 147 return V8ThrowException::throwError(v8GeneralError, "The placeholder for
Promise is not found.", isolate).As<v8::Function>(); |
| 148 v8::Local<v8::Object> placeholder = placeholderValue.As<v8::Object>(); |
| 149 v8::Handle<v8::Value> value = placeholder->GetHiddenValue(V8HiddenPropertyNa
me::jsConstructor()); |
| 150 if (!value.IsEmpty() && value->IsFunction()) { |
| 151 // Return the stored constructor. |
| 152 return value.As<v8::Function>(); |
| 153 } |
| 154 value = V8ScriptRunner::compileAndRunInternalScript(v8String(String(PromiseR
esolver_js, sizeof(PromiseResolver_js)), isolate), isolate); |
| 155 if (value.IsEmpty()) |
| 156 return v8::Handle<v8::Function>(); |
| 157 if (!value->IsFunction()) |
| 158 return V8ThrowException::throwError(v8TypeError, "PromiseResolver implem
entation must be a function.", isolate).As<v8::Function>(); |
| 159 |
| 160 v8::Handle<v8::Function> impl = value.As<v8::Function>(); |
| 161 v8::Handle<v8::Value> postTaskWrapper = v8::FunctionTemplate::New(postTask)-
>GetFunction(); |
| 162 value = impl->Call(v8::Object::New(), 1, &postTaskWrapper); |
| 163 if (value.IsEmpty()) |
| 164 return v8::Handle<v8::Function>(); |
| 165 if (!value->IsFunction()) |
| 166 return V8ThrowException::throwError(v8TypeError, "Promise constructor mu
st be a function.", isolate).As<v8::Function>(); |
| 167 placeholder->SetHiddenValue(V8HiddenPropertyName::jsConstructor(), value); |
| 168 return value.As<v8::Function>(); |
| 169 } |
| 170 |
| 171 v8::Handle<v8::Value> V8PromiseUtilities::promiseResolverPrototype(v8::Isolate*
isolate) |
| 172 { |
| 173 v8::Handle<v8::Object> constructor = promiseResolverConstructor(isolate); |
| 174 if (constructor.IsEmpty()) |
| 175 return constructor; |
| 176 return constructor->Get(v8::String::NewSymbol("prototype")); |
| 177 } |
| 178 |
| 179 v8::Handle<v8::Value> V8PromiseUtilities::callUnwrappedMethod(const char* name,
const v8::FunctionCallbackInfo<v8::Value>& args, v8::Handle<v8::Value> prototype
) |
| 180 { |
| 181 Vector<v8::Handle<v8::Value> > arguments; |
| 182 for (int i = 0; i < args.Length(); ++i) |
| 183 arguments.append(args[i]); |
| 184 return callUnwrappedMethod(name, args.This(), arguments.size(), arguments.da
ta(), args.GetIsolate(), prototype); |
| 185 } |
| 186 |
| 187 v8::Handle<v8::Value> V8PromiseUtilities::callUnwrappedMethod(const char* name,
v8::Handle<v8::Object> thisObject, |
| 188 int argc, v8::Handle<v8::Value> argv[], v8::Isolate* isolate, v8::Handle<v8:
:Value> prototype) |
| 189 { |
| 190 v8::Handle<v8::Value> that = thisObject->GetInternalField(v8DOMWrapperObject
Index); |
| 191 if (that.IsEmpty() || !that->IsObject()) |
| 192 return V8ThrowException::throwError(v8TypeError, "This object is not an
object.", isolate); |
| 193 |
| 194 if (prototype.IsEmpty() || !prototype->IsObject()) |
| 195 return V8ThrowException::throwError(v8TypeError, "The prototypex is not
an object.", isolate); |
| 196 |
| 197 v8::Local<v8::Value> property = prototype.As<v8::Object>()->Get(v8::String::
NewSymbol(name)); |
| 198 if (property.IsEmpty() || !property->IsFunction()) |
| 199 return V8ThrowException::throwError(v8TypeError, (std::string("The prope
rty ") + name + " is not a function.").c_str(), isolate); |
| 200 return property.As<v8::Function>()->Call(that.As<v8::Object>(), argc, argv); |
| 201 } |
| 202 |
| 203 v8::Handle<v8::Value> V8PromiseUtilities::callStatic(const char* name, const v8:
:FunctionCallbackInfo<v8::Value>& args, v8::Handle<v8::Value> constructor) |
| 204 { |
| 205 if (constructor.IsEmpty() || !constructor->IsObject()) |
| 206 return V8ThrowException::throwError(v8TypeError, "The constructor object
is not found.", args.GetIsolate()); |
| 207 v8::Local<v8::Value> property = constructor.As<v8::Object>()->Get(v8::String
::NewSymbol(name)); |
| 208 if (property.IsEmpty() || !property->IsFunction()) |
| 209 return V8ThrowException::throwError(v8TypeError, (std::string("The prope
rty ") + name + " is not a function.").c_str(), args.GetIsolate()); |
| 210 Vector<v8::Handle<v8::Value> > arguments; |
| 211 for (int i = 0; i < args.Length(); ++i) |
| 212 arguments.append(args[i]); |
| 213 return property.As<v8::Function>()->Call(args.This(), arguments.size(), argu
ments.data()); |
| 214 } |
| 215 |
| 216 } // namespace WebCore |
OLD | NEW |