113 lines
3.7 KiB
C++
113 lines
3.7 KiB
C++
// Copyright (c) 2013 GitHub, Inc.
|
|
// Use of this source code is governed by the MIT license that can be
|
|
// found in the LICENSE file.
|
|
|
|
#include "atom/browser/api/atom_api_protocol.h"
|
|
|
|
#include "atom/browser/atom_browser_client.h"
|
|
#include "atom/browser/atom_browser_context.h"
|
|
#include "atom/browser/atom_browser_main_parts.h"
|
|
#include "atom/browser/net/url_request_async_asar_job.h"
|
|
#include "atom/browser/net/url_request_buffer_job.h"
|
|
#include "atom/browser/net/url_request_fetch_job.h"
|
|
#include "atom/browser/net/url_request_string_job.h"
|
|
#include "atom/common/native_mate_converters/callback.h"
|
|
#include "native_mate/dictionary.h"
|
|
|
|
#include "atom/common/node_includes.h"
|
|
|
|
using content::BrowserThread;
|
|
|
|
namespace mate {
|
|
|
|
template<>
|
|
struct Converter<const net::URLRequest*> {
|
|
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
|
const net::URLRequest* val) {
|
|
return mate::ObjectTemplateBuilder(isolate)
|
|
.SetValue("method", val->method())
|
|
.SetValue("url", val->url().spec())
|
|
.SetValue("referrer", val->referrer())
|
|
.Build()->NewInstance();
|
|
}
|
|
};
|
|
|
|
} // namespace mate
|
|
|
|
namespace atom {
|
|
|
|
namespace api {
|
|
|
|
Protocol::Protocol(AtomBrowserContext* browser_context)
|
|
: request_context_getter_(browser_context->GetRequestContext()),
|
|
job_factory_(browser_context->job_factory()) {
|
|
CHECK(job_factory_);
|
|
}
|
|
|
|
mate::ObjectTemplateBuilder Protocol::GetObjectTemplateBuilder(
|
|
v8::Isolate* isolate) {
|
|
return mate::ObjectTemplateBuilder(isolate)
|
|
.SetMethod("registerStandardSchemes", &Protocol::RegisterStandardSchemes)
|
|
.SetMethod("registerStringProtocol",
|
|
&Protocol::RegisterProtocol<URLRequestStringJob>)
|
|
.SetMethod("registerBufferProtocol",
|
|
&Protocol::RegisterProtocol<URLRequestBufferJob>)
|
|
.SetMethod("registerFileProtocol",
|
|
&Protocol::RegisterProtocol<UrlRequestAsyncAsarJob>)
|
|
.SetMethod("registerHttpProtocol",
|
|
&Protocol::RegisterProtocol<URLRequestFetchJob>);
|
|
}
|
|
|
|
void Protocol::RegisterStandardSchemes(
|
|
const std::vector<std::string>& schemes) {
|
|
atom::AtomBrowserClient::SetCustomSchemes(schemes);
|
|
}
|
|
|
|
void Protocol::OnIOCompleted(
|
|
const CompletionCallback& callback, ProtocolError error) {
|
|
v8::Locker locker(isolate());
|
|
v8::HandleScope handle_scope(isolate());
|
|
|
|
if (error == PROTOCOL_OK) {
|
|
callback.Run(v8::Null(isolate()));
|
|
} else {
|
|
std::string str = ErrorCodeToString(error);
|
|
callback.Run(v8::Exception::Error(mate::StringToV8(isolate(), str)));
|
|
}
|
|
}
|
|
|
|
std::string Protocol::ErrorCodeToString(ProtocolError error) {
|
|
switch (error) {
|
|
case PROTOCOL_FAIL: return "Failed to manipulate protocol factory";
|
|
case PROTOCOL_REGISTERED: return "The scheme has been registred";
|
|
case PROTOCOL_NOT_REGISTERED: return "The scheme has not been registred";
|
|
case PROTOCOL_INTERCEPTED: return "The scheme has been intercepted";
|
|
case PROTOCOL_NOT_INTERCEPTED: return "The scheme has not been intercepted";
|
|
default: return "Unexpected error";
|
|
}
|
|
}
|
|
|
|
// static
|
|
mate::Handle<Protocol> Protocol::Create(
|
|
v8::Isolate* isolate, AtomBrowserContext* browser_context) {
|
|
return mate::CreateHandle(isolate, new Protocol(browser_context));
|
|
}
|
|
|
|
} // namespace api
|
|
|
|
} // namespace atom
|
|
|
|
namespace {
|
|
|
|
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
|
|
v8::Local<v8::Context> context, void* priv) {
|
|
v8::Isolate* isolate = context->GetIsolate();
|
|
mate::Dictionary dict(isolate, exports);
|
|
auto browser_context = static_cast<atom::AtomBrowserContext*>(
|
|
atom::AtomBrowserMainParts::Get()->browser_context());
|
|
dict.Set("protocol", atom::api::Protocol::Create(isolate, browser_context));
|
|
}
|
|
|
|
} // namespace
|
|
|
|
NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_protocol, Initialize)
|