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"
|
|
|
|
|
2018-09-13 00:25:56 +00:00
|
|
|
#include <utility>
|
2015-08-12 13:32:52 +00:00
|
|
|
|
2015-08-12 05:30:19 +00:00
|
|
|
#include "atom/common/native_mate_converters/callback.h"
|
2018-11-30 09:48:27 +00:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
2015-08-12 05:30:19 +00:00
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
2018-10-05 20:29:57 +00:00
|
|
|
JsAsker::JsAsker() = default;
|
2015-08-12 05:30:19 +00:00
|
|
|
|
2018-10-05 20:29:57 +00:00
|
|
|
JsAsker::~JsAsker() = default;
|
2015-08-12 05:30:19 +00:00
|
|
|
|
2018-10-05 20:29:57 +00:00
|
|
|
void JsAsker::SetHandlerInfo(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
net::URLRequestContextGetter* request_context_getter,
|
|
|
|
const JavaScriptHandler& handler) {
|
|
|
|
isolate_ = isolate;
|
|
|
|
request_context_getter_ = request_context_getter;
|
|
|
|
handler_ = handler;
|
2015-08-12 05:30:19 +00:00
|
|
|
}
|
|
|
|
|
2018-10-05 20:29:57 +00:00
|
|
|
// static
|
|
|
|
void JsAsker::AskForOptions(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
const JavaScriptHandler& handler,
|
|
|
|
std::unique_ptr<base::DictionaryValue> request_details,
|
|
|
|
const BeforeStartCallback& before_start) {
|
2015-08-12 05:30:19 +00:00
|
|
|
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()),
|
2018-10-05 20:29:57 +00:00
|
|
|
mate::ConvertToV8(isolate, before_start));
|
2015-08-12 05:30:19 +00:00
|
|
|
}
|
|
|
|
|
2018-10-05 20:29:57 +00:00
|
|
|
// static
|
|
|
|
bool JsAsker::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()) {
|
2018-06-27 21:52:48 +00:00
|
|
|
*error = value->GetInt();
|
|
|
|
return true;
|
2015-08-12 05:50:31 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-12 05:30:19 +00:00
|
|
|
} // namespace atom
|