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"
|
|
|
|
|
2015-08-12 13:32:52 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2015-08-12 05:30:19 +00:00
|
|
|
#include "atom/common/native_mate_converters/callback.h"
|
|
|
|
#include "atom/common/native_mate_converters/v8_value_converter.h"
|
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// The callback which is passed to |handler|.
|
2016-03-10 05:39:40 +00:00
|
|
|
void HandlerCallback(const BeforeStartCallback& before_start,
|
|
|
|
const ResponseCallback& callback,
|
|
|
|
mate::Arguments* args) {
|
2015-08-12 05:43:27 +00:00
|
|
|
// If there is no argument passed then we failed.
|
2015-08-12 05:30:19 +00:00
|
|
|
v8::Local<v8::Value> value;
|
|
|
|
if (!args->GetNext(&value)) {
|
2018-04-18 01:55:30 +00:00
|
|
|
content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE,
|
2018-04-20 10:55:05 +00:00
|
|
|
base::BindOnce(callback, false, nullptr));
|
2015-08-12 05:30:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-10 05:39:40 +00:00
|
|
|
// Give the job a chance to parse V8 value.
|
|
|
|
before_start.Run(args->isolate(), value);
|
|
|
|
|
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();
|
2016-05-23 01:59:39 +00:00
|
|
|
std::unique_ptr<base::Value> options(converter.FromV8Value(value, context));
|
2015-08-12 13:09:44 +00:00
|
|
|
content::BrowserThread::PostTask(
|
|
|
|
content::BrowserThread::IO, FROM_HERE,
|
2018-04-20 10:55:05 +00:00
|
|
|
base::BindOnce(callback, true, std::move(options)));
|
2015-08-12 05:30:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void AskForOptions(v8::Isolate* isolate,
|
|
|
|
const JavaScriptHandler& handler,
|
2016-06-08 13:52:21 +00:00
|
|
|
std::unique_ptr<base::DictionaryValue> request_details,
|
2016-03-10 05:39:40 +00:00
|
|
|
const BeforeStartCallback& before_start,
|
2015-08-12 05:30:19 +00:00
|
|
|
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);
|
2018-04-18 01:55:30 +00:00
|
|
|
handler.Run(*(request_details.get()),
|
|
|
|
mate::ConvertToV8(isolate, base::Bind(&HandlerCallback,
|
|
|
|
before_start, callback)));
|
2015-08-12 05:30:19 +00:00
|
|
|
}
|
|
|
|
|
2015-08-12 05:50:31 +00:00
|
|
|
bool IsErrorOptions(base::Value* value, int* error) {
|
2018-04-10 14:05:36 +00:00
|
|
|
if (value->is_dict()) {
|
2015-08-12 05:50:31 +00:00
|
|
|
base::DictionaryValue* dict = static_cast<base::DictionaryValue*>(value);
|
|
|
|
if (dict->GetInteger("error", error))
|
|
|
|
return true;
|
2018-04-10 14:05:36 +00:00
|
|
|
} else if (value->is_int()) {
|
2015-08-12 05:50:31 +00:00
|
|
|
if (value->GetAsInteger(error))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-12 05:30:19 +00:00
|
|
|
} // namespace internal
|
|
|
|
|
|
|
|
} // namespace atom
|