Make the completion callback optional

This commit is contained in:
Cheng Zhao 2015-08-12 21:32:52 +08:00
parent f493eb34ae
commit 225321b580
6 changed files with 35 additions and 7 deletions

View file

@ -49,13 +49,13 @@ mate::ObjectTemplateBuilder Protocol::GetObjectTemplateBuilder(
return mate::ObjectTemplateBuilder(isolate)
.SetMethod("registerStandardSchemes", &Protocol::RegisterStandardSchemes)
.SetMethod("registerStringProtocol",
&Protocol::RegisterProtocol<URLRequestStringJob>)
&Protocol::JavaScriptRegisterProtocol<URLRequestStringJob>)
.SetMethod("registerBufferProtocol",
&Protocol::RegisterProtocol<URLRequestBufferJob>)
&Protocol::JavaScriptRegisterProtocol<URLRequestBufferJob>)
.SetMethod("registerFileProtocol",
&Protocol::RegisterProtocol<UrlRequestAsyncAsarJob>)
&Protocol::JavaScriptRegisterProtocol<UrlRequestAsyncAsarJob>)
.SetMethod("registerHttpProtocol",
&Protocol::RegisterProtocol<URLRequestFetchJob>);
&Protocol::JavaScriptRegisterProtocol<URLRequestFetchJob>);
}
void Protocol::RegisterStandardSchemes(
@ -65,6 +65,10 @@ void Protocol::RegisterStandardSchemes(
void Protocol::OnIOCompleted(
const CompletionCallback& callback, ProtocolError error) {
// The completion callback is optional.
if (callback.is_null())
return;
v8::Locker locker(isolate());
v8::HandleScope handle_scope(isolate());

View file

@ -12,6 +12,8 @@
#include "atom/browser/net/atom_url_request_job_factory.h"
#include "base/callback.h"
#include "content/public/browser/browser_thread.h"
#include "native_mate/arguments.h"
#include "native_mate/dictionary.h"
#include "native_mate/handle.h"
#include "native_mate/wrappable.h"
@ -106,6 +108,25 @@ class Protocol : public mate::Wrappable {
return PROTOCOL_FAIL;
}
// Parse optional parameters for registerProtocol.
template<typename RequestJob>
void JavaScriptRegisterProtocol(v8::Isolate* isolate,
const std::string& scheme,
mate::Arguments* args) {
// protocol.registerProtocol(scheme[, options], handler[, callback]);
mate::Dictionary options = mate::Dictionary::CreateEmpty(isolate);
Handler handler;
CompletionCallback callback;
args->GetNext(&options);
if (!args->GetNext(&handler)) {
args->ThrowError();
return;
}
args->GetNext(&callback);
RegisterProtocol<RequestJob>(isolate, scheme, handler, callback);
}
// Convert error code to JS exception and call the callback.
void OnIOCompleted(const CompletionCallback& callback, ProtocolError error);