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.
|
|
|
|
|
|
|
|
#ifndef ATOM_BROWSER_NET_JS_ASKER_H_
|
|
|
|
#define ATOM_BROWSER_NET_JS_ASKER_H_
|
|
|
|
|
|
|
|
#include "base/callback.h"
|
2015-08-12 14:57:25 +00:00
|
|
|
#include "base/memory/ref_counted.h"
|
2015-08-12 05:30:19 +00:00
|
|
|
#include "base/memory/weak_ptr.h"
|
|
|
|
#include "base/values.h"
|
|
|
|
#include "content/public/browser/browser_thread.h"
|
|
|
|
#include "net/base/net_errors.h"
|
2015-08-13 14:26:27 +00:00
|
|
|
#include "net/http/http_response_headers.h"
|
2015-08-12 14:57:25 +00:00
|
|
|
#include "net/url_request/url_request_context_getter.h"
|
2015-08-12 05:30:19 +00:00
|
|
|
#include "net/url_request/url_request_job.h"
|
|
|
|
#include "v8/include/v8.h"
|
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
using JavaScriptHandler =
|
|
|
|
base::Callback<void(const net::URLRequest*, v8::Local<v8::Value>)>;
|
|
|
|
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
using ResponseCallback =
|
|
|
|
base::Callback<void(bool, scoped_ptr<base::Value> options)>;
|
|
|
|
|
|
|
|
// Ask handler for options in UI thread.
|
|
|
|
void AskForOptions(v8::Isolate* isolate,
|
|
|
|
const JavaScriptHandler& handler,
|
|
|
|
net::URLRequest* request,
|
|
|
|
const ResponseCallback& callback);
|
|
|
|
|
2015-08-12 05:50:31 +00:00
|
|
|
// Test whether the |options| means an error.
|
|
|
|
bool IsErrorOptions(base::Value* value, int* error);
|
|
|
|
|
2015-08-12 05:30:19 +00:00
|
|
|
} // namespace internal
|
|
|
|
|
|
|
|
template<typename RequestJob>
|
|
|
|
class JsAsker : public RequestJob {
|
|
|
|
public:
|
2015-08-12 14:57:25 +00:00
|
|
|
JsAsker(net::URLRequest* request, net::NetworkDelegate* network_delegate)
|
|
|
|
: RequestJob(request, network_delegate), weak_factory_(this) {}
|
|
|
|
|
|
|
|
// Called by |CustomProtocolHandler| to store handler related information.
|
|
|
|
void SetHandlerInfo(
|
|
|
|
v8::Isolate* isolate,
|
2015-09-09 11:27:28 +00:00
|
|
|
net::URLRequestContextGetter* request_context_getter,
|
2015-08-12 14:57:25 +00:00
|
|
|
const JavaScriptHandler& handler) {
|
|
|
|
isolate_ = isolate;
|
|
|
|
request_context_getter_ = request_context_getter;
|
|
|
|
handler_ = handler;
|
|
|
|
}
|
2015-08-12 05:30:19 +00:00
|
|
|
|
|
|
|
// Subclass should do initailze work here.
|
2015-08-12 13:09:44 +00:00
|
|
|
virtual void StartAsync(scoped_ptr<base::Value> options) = 0;
|
2015-08-12 05:30:19 +00:00
|
|
|
|
2015-08-12 14:57:25 +00:00
|
|
|
net::URLRequestContextGetter* request_context_getter() const {
|
2015-09-09 11:27:28 +00:00
|
|
|
return request_context_getter_;
|
2015-08-12 14:57:25 +00:00
|
|
|
}
|
|
|
|
|
2015-08-12 05:30:19 +00:00
|
|
|
private:
|
|
|
|
// RequestJob:
|
|
|
|
void Start() override {
|
|
|
|
content::BrowserThread::PostTask(
|
|
|
|
content::BrowserThread::UI, FROM_HERE,
|
|
|
|
base::Bind(&internal::AskForOptions,
|
|
|
|
isolate_,
|
|
|
|
handler_,
|
|
|
|
RequestJob::request(),
|
|
|
|
base::Bind(&JsAsker::OnResponse,
|
|
|
|
weak_factory_.GetWeakPtr())));
|
|
|
|
}
|
2015-08-13 14:26:27 +00:00
|
|
|
void GetResponseInfo(net::HttpResponseInfo* info) override {
|
|
|
|
info->headers = new net::HttpResponseHeaders("");
|
|
|
|
}
|
2015-08-12 05:30:19 +00:00
|
|
|
|
|
|
|
// Called when the JS handler has sent the response, we need to decide whether
|
|
|
|
// to start, or fail the job.
|
2015-08-12 05:50:31 +00:00
|
|
|
void OnResponse(bool success, scoped_ptr<base::Value> value) {
|
|
|
|
int error = net::ERR_NOT_IMPLEMENTED;
|
|
|
|
if (success && value && !internal::IsErrorOptions(value.get(), &error)) {
|
|
|
|
StartAsync(value.Pass());
|
2015-08-12 05:30:19 +00:00
|
|
|
} else {
|
|
|
|
RequestJob::NotifyStartError(
|
2015-08-12 05:50:31 +00:00
|
|
|
net::URLRequestStatus(net::URLRequestStatus::FAILED, error));
|
2015-08-12 05:30:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::Isolate* isolate_;
|
2015-09-09 11:27:28 +00:00
|
|
|
net::URLRequestContextGetter* request_context_getter_;
|
2015-08-12 05:30:19 +00:00
|
|
|
JavaScriptHandler handler_;
|
2015-08-12 14:57:25 +00:00
|
|
|
|
2015-08-12 05:30:19 +00:00
|
|
|
base::WeakPtrFactory<JsAsker> weak_factory_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(JsAsker);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace atom
|
|
|
|
|
|
|
|
#endif // ATOM_BROWSER_NET_JS_ASKER_H_
|