refactor: Clean up the implementation of the registerStreamProtocol (#11357)

* Use weak pointer to avoid race condition

* Use DeleteSoon to delete pointer across threads

* Simplify EventSubscriber

* No need to manually mange V8 convertions

* Fix cpplint warning

We should update cpplint for this, but let's do it in other PR.

* Move UI thread operations to EventSubscriber

* Less and more assertions

Some methods are now private so no more need to assert threads.

* Fix cpplint warnings

* No longer needs the EventEmitted

* EventSubscriber => StreamSubscriber

* Reduce the copies when passing data

* Fix cpplint warnings
This commit is contained in:
Cheng Zhao 2018-10-04 23:13:09 +09:00 committed by John Kleinschmidt
parent 3805c5f538
commit d3ae541397
9 changed files with 314 additions and 373 deletions

View file

@ -5,11 +5,11 @@
#ifndef ATOM_BROWSER_NET_URL_REQUEST_STREAM_JOB_H_
#define ATOM_BROWSER_NET_URL_REQUEST_STREAM_JOB_H_
#include <deque>
#include <memory>
#include <string>
#include <vector>
#include "atom/browser/api/event_subscriber.h"
#include "atom/browser/api/stream_subscriber.h"
#include "atom/browser/net/js_asker.h"
#include "base/memory/ref_counted_memory.h"
#include "native_mate/persistent_dictionary.h"
@ -26,9 +26,9 @@ class URLRequestStreamJob : public JsAsker<net::URLRequestJob> {
net::NetworkDelegate* network_delegate);
~URLRequestStreamJob() override;
void OnData(mate::Arguments* args);
void OnEnd(mate::Arguments* args);
void OnError(mate::Arguments* args);
void OnData(std::vector<char>&& buffer); // NOLINT
void OnEnd();
void OnError();
// URLRequestJob
void GetResponseInfo(net::HttpResponseInfo* info) override;
@ -48,17 +48,21 @@ class URLRequestStreamJob : public JsAsker<net::URLRequestJob> {
void StartAsync(std::unique_ptr<base::Value> options) override;
void OnResponse(bool success, std::unique_ptr<base::Value> value);
// Callback after data is asynchronously read from the file into |buf|.
void CopyMoreData(scoped_refptr<net::IOBuffer> io_buf, int io_buf_size);
void CopyMoreDataDone(scoped_refptr<net::IOBuffer> io_buf, int read_count);
int BufferCopy(std::vector<char>* source,
net::IOBuffer* target, int target_size);
std::deque<char> buffer_;
bool ended_ = false;
bool errored_ = false;
scoped_refptr<net::IOBuffer> pending_io_buf_;
int pending_io_buf_size_ = 0;
// Saved arguments passed to ReadRawData.
scoped_refptr<net::IOBuffer> pending_buf_;
int pending_buf_size_;
// Saved arguments passed to OnData.
std::vector<char> write_buffer_;
bool ended_;
bool has_error_;
scoped_refptr<net::HttpResponseHeaders> response_headers_;
mate::EventSubscriber<URLRequestStreamJob>::SafePtr subscriber_;
std::unique_ptr<mate::StreamSubscriber> subscriber_;
base::WeakPtrFactory<URLRequestStreamJob> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(URLRequestStreamJob);