2015-08-12 05:30:19 +00:00
|
|
|
// Copyright (c) 2015 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "atom/browser/net/js_asker.h"
|
|
|
|
|
|
|
|
#include "atom/common/native_mate_converters/callback.h"
|
|
|
|
#include "atom/common/native_mate_converters/v8_value_converter.h"
|
|
|
|
#include "native_mate/function_template.h"
|
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct CallbackHolder {
|
|
|
|
ResponseCallback callback;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Cached JavaScript version of |HandlerCallback|.
|
|
|
|
v8::Persistent<v8::FunctionTemplate> g_handler_callback_;
|
|
|
|
|
|
|
|
// The callback which is passed to |handler|.
|
2015-08-12 05:43:27 +00:00
|
|
|
void HandlerCallback(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::External> external,
|
|
|
|
v8::Local<v8::Object> state,
|
|
|
|
mate::Arguments* args) {
|
|
|
|
// Check if the callback has already been called.
|
|
|
|
v8::Local<v8::String> called_symbol = mate::StringToSymbol(isolate, "called");
|
|
|
|
if (state->Has(called_symbol))
|
|
|
|
return; // no nothing
|
|
|
|
else
|
|
|
|
state->Set(called_symbol, v8::Boolean::New(isolate, true));
|
|
|
|
|
|
|
|
// If there is no argument passed then we failed.
|
2015-08-12 05:30:19 +00:00
|
|
|
scoped_ptr<CallbackHolder> holder(
|
2015-08-12 05:43:27 +00:00
|
|
|
static_cast<CallbackHolder*>(external->Value()));
|
2015-08-12 05:30:19 +00:00
|
|
|
CHECK(holder);
|
|
|
|
v8::Local<v8::Value> value;
|
|
|
|
if (!args->GetNext(&value)) {
|
|
|
|
holder->callback.Run(false, nullptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-12 05:43:27 +00:00
|
|
|
// Pass whatever user passed to the actaul request job.
|
2015-08-12 05:30:19 +00:00
|
|
|
V8ValueConverter converter;
|
|
|
|
v8::Local<v8::Context> context = args->isolate()->GetCurrentContext();
|
|
|
|
scoped_ptr<base::Value> options(converter.FromV8Value(value, context));
|
2015-08-12 13:09:44 +00:00
|
|
|
content::BrowserThread::PostTask(
|
|
|
|
content::BrowserThread::IO, FROM_HERE,
|
|
|
|
base::Bind(holder->callback, true, base::Passed(&options)));
|
2015-08-12 05:30:19 +00:00
|
|
|
}
|
|
|
|
|
2015-08-12 05:43:27 +00:00
|
|
|
// func.bind(func, ...).
|
2015-08-12 05:30:19 +00:00
|
|
|
template<typename... ArgTypes>
|
|
|
|
v8::Local<v8::Value> BindFunctionWith(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Context> context,
|
|
|
|
v8::Local<v8::Function> func,
|
|
|
|
ArgTypes... args) {
|
|
|
|
v8::MaybeLocal<v8::Value> bind = func->Get(mate::StringToV8(isolate, "bind"));
|
|
|
|
CHECK(!bind.IsEmpty());
|
|
|
|
v8::Local<v8::Function> bind_func =
|
|
|
|
v8::Local<v8::Function>::Cast(bind.ToLocalChecked());
|
|
|
|
std::vector<v8::Local<v8::Value>> converted = {
|
|
|
|
func, mate::ConvertToV8(isolate, args)...,
|
|
|
|
};
|
|
|
|
return bind_func->Call(
|
|
|
|
context, func, converted.size(), &converted.front()).ToLocalChecked();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate the callback that will be passed to |handler|.
|
|
|
|
v8::MaybeLocal<v8::Value> GenerateCallback(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Context> context,
|
|
|
|
const ResponseCallback& callback) {
|
|
|
|
// The FunctionTemplate is cached.
|
|
|
|
if (g_handler_callback_.IsEmpty())
|
|
|
|
g_handler_callback_.Reset(
|
|
|
|
isolate,
|
|
|
|
mate::CreateFunctionTemplate(isolate, base::Bind(&HandlerCallback)));
|
|
|
|
|
|
|
|
v8::Local<v8::FunctionTemplate> handler_callback =
|
|
|
|
v8::Local<v8::FunctionTemplate>::New(isolate, g_handler_callback_);
|
|
|
|
CallbackHolder* holder = new CallbackHolder;
|
|
|
|
holder->callback = callback;
|
|
|
|
return BindFunctionWith(isolate, context,
|
|
|
|
handler_callback->GetFunction(),
|
2015-08-12 05:43:27 +00:00
|
|
|
v8::External::New(isolate, holder),
|
|
|
|
v8::Object::New(isolate));
|
2015-08-12 05:30:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void AskForOptions(v8::Isolate* isolate,
|
|
|
|
const JavaScriptHandler& handler,
|
|
|
|
net::URLRequest* request,
|
|
|
|
const ResponseCallback& callback) {
|
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
|
|
|
|
v8::Locker locker(isolate);
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
v8::Local<v8::Context> context = isolate->GetCurrentContext();
|
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
// We don't convert the callback to C++ directly because creating
|
|
|
|
// FunctionTemplate will cause memory leak since V8 never releases it. So we
|
|
|
|
// have to create the function object in JavaScript to work around it.
|
|
|
|
v8::MaybeLocal<v8::Value> wrapped_callback = GenerateCallback(
|
|
|
|
isolate, context, callback);
|
|
|
|
if (wrapped_callback.IsEmpty()) {
|
|
|
|
callback.Run(false, nullptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
handler.Run(request, wrapped_callback.ToLocalChecked());
|
|
|
|
}
|
|
|
|
|
2015-08-12 05:50:31 +00:00
|
|
|
bool IsErrorOptions(base::Value* value, int* error) {
|
|
|
|
if (value->IsType(base::Value::TYPE_DICTIONARY)) {
|
|
|
|
base::DictionaryValue* dict = static_cast<base::DictionaryValue*>(value);
|
|
|
|
if (dict->GetInteger("error", error))
|
|
|
|
return true;
|
|
|
|
} else if (value->IsType(base::Value::TYPE_INTEGER)) {
|
|
|
|
if (value->GetAsInteger(error))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-12 05:30:19 +00:00
|
|
|
} // namespace internal
|
|
|
|
|
|
|
|
} // namespace atom
|