electron/atom/browser/net/node_stream_loader.cc
Cheng Zhao 326215e1f1
feat: migrate protocol module to NetworkService (Part 6) (#18223)
* fix: start node strem asyncly

* fix: headers value may be a list

* fix: simply destruct on finish/error

* fix: class may destruct immediately after subscribing "data"

* fix: send meaningful error

* fix: must always provide a response body

* fix: handle the case when one write can not write all data

* fix: handle connection error
2019-05-11 15:15:01 +09:00

142 lines
4.6 KiB
C++

// Copyright (c) 2019 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/node_stream_loader.h"
#include <utility>
#include "atom/common/api/event_emitter_caller.h"
#include "atom/common/native_mate_converters/callback.h"
#include "atom/common/node_includes.h"
namespace atom {
NodeStreamLoader::NodeStreamLoader(network::ResourceResponseHead head,
network::mojom::URLLoaderRequest loader,
network::mojom::URLLoaderClientPtr client,
v8::Isolate* isolate,
v8::Local<v8::Object> emitter)
: binding_(this, std::move(loader)),
client_(std::move(client)),
isolate_(isolate),
emitter_(isolate, emitter),
weak_factory_(this) {
binding_.set_connection_error_handler(
base::BindOnce(&NodeStreamLoader::NotifyComplete,
weak_factory_.GetWeakPtr(), net::ERR_FAILED));
// PostTask since it might destruct.
base::SequencedTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(&NodeStreamLoader::Start,
weak_factory_.GetWeakPtr(), std::move(head)));
}
NodeStreamLoader::~NodeStreamLoader() {
v8::Locker locker(isolate_);
v8::Isolate::Scope isolate_scope(isolate_);
v8::HandleScope handle_scope(isolate_);
// Unsubscribe all handlers.
for (const auto& it : handlers_) {
v8::Local<v8::Value> args[] = {mate::StringToV8(isolate_, it.first),
it.second.Get(isolate_)};
node::MakeCallback(isolate_, emitter_.Get(isolate_), "removeListener",
node::arraysize(args), args, {0, 0});
}
// Release references.
emitter_.Reset();
buffer_.Reset();
}
void NodeStreamLoader::Start(network::ResourceResponseHead head) {
mojo::ScopedDataPipeProducerHandle producer;
mojo::ScopedDataPipeConsumerHandle consumer;
MojoResult rv = mojo::CreateDataPipe(nullptr, &producer, &consumer);
if (rv != MOJO_RESULT_OK) {
NotifyComplete(net::ERR_INSUFFICIENT_RESOURCES);
return;
}
producer_ =
std::make_unique<mojo::StringDataPipeProducer>(std::move(producer));
client_->OnReceiveResponse(head);
client_->OnStartLoadingResponseBody(std::move(consumer));
auto weak = weak_factory_.GetWeakPtr();
On("end",
base::BindRepeating(&NodeStreamLoader::NotifyComplete, weak, net::OK));
On("error", base::BindRepeating(&NodeStreamLoader::NotifyComplete, weak,
net::ERR_FAILED));
On("readable", base::BindRepeating(&NodeStreamLoader::ReadMore, weak));
}
void NodeStreamLoader::NotifyComplete(int result) {
// Wait until write finishes or fails.
if (is_writing_) {
ended_ = true;
result_ = result;
return;
}
client_->OnComplete(network::URLLoaderCompletionStatus(result));
delete this;
}
void NodeStreamLoader::ReadMore() {
// buffer = emitter.read()
v8::MaybeLocal<v8::Value> ret = node::MakeCallback(
isolate_, emitter_.Get(isolate_), "read", 0, nullptr, {0, 0});
// If there is no buffer read, wait until |readable| is emitted again.
v8::Local<v8::Value> buffer;
if (!ret.ToLocal(&buffer) || !node::Buffer::HasInstance(buffer))
return;
// Hold the buffer until the write is done.
buffer_.Reset(isolate_, buffer);
// Write buffer to mojo pipe asyncronously.
is_writing_ = true;
producer_->Write(
base::StringPiece(node::Buffer::Data(buffer),
node::Buffer::Length(buffer)),
mojo::StringDataPipeProducer::AsyncWritingMode::
STRING_STAYS_VALID_UNTIL_COMPLETION,
base::BindOnce(&NodeStreamLoader::DidWrite, weak_factory_.GetWeakPtr()));
}
void NodeStreamLoader::DidWrite(MojoResult result) {
is_writing_ = false;
// We were told to end streaming.
if (ended_) {
NotifyComplete(result_);
return;
}
if (result == MOJO_RESULT_OK)
ReadMore();
else
NotifyComplete(net::ERR_FAILED);
}
void NodeStreamLoader::On(const char* event, EventCallback callback) {
v8::Locker locker(isolate_);
v8::Isolate::Scope isolate_scope(isolate_);
v8::HandleScope handle_scope(isolate_);
// emitter.on(event, callback)
v8::Local<v8::Value> args[] = {
mate::StringToV8(isolate_, event),
mate::CallbackToV8(isolate_, std::move(callback)),
};
handlers_[event].Reset(isolate_, args[1]);
node::MakeCallback(isolate_, emitter_.Get(isolate_), "on",
node::arraysize(args), args, {0, 0});
// No more code bellow, as this class may destruct when subscribing.
}
} // namespace atom