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);

View file

@ -5,6 +5,7 @@
#include "atom/browser/net/asar/url_request_asar_job.h"
#include <string>
#include <vector>
#include "base/bind.h"
#include "base/files/file_util.h"

View file

@ -4,6 +4,8 @@
#include "atom/browser/net/js_asker.h"
#include <vector>
#include "atom/common/native_mate_converters/callback.h"
#include "atom/common/native_mate_converters/v8_value_converter.h"
#include "native_mate/function_template.h"

View file

@ -5,11 +5,11 @@
#ifndef ATOM_BROWSER_NET_URL_REQUEST_STRING_JOB_H_
#define ATOM_BROWSER_NET_URL_REQUEST_STRING_JOB_H_
#include <string>
#include "atom/browser/net/js_asker.h"
#include "net/url_request/url_request_simple_job.h"
#include <string>
namespace atom {
class URLRequestStringJob : public JsAsker<net::URLRequestSimpleJob> {

2
vendor/native_mate vendored

@ -1 +1 @@
Subproject commit 67d9eaa215e8727d86dc7b1f7a10be8699848f1f
Subproject commit 24d31e204698bafc65c50295167764fbb0f28bc0