electron/atom/browser/net/url_request_stream_job.h
Cheng Zhao d3ae541397 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
2018-10-04 10:13:09 -04:00

72 lines
2.2 KiB
C++

// Copyright (c) 2017 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ATOM_BROWSER_NET_URL_REQUEST_STREAM_JOB_H_
#define ATOM_BROWSER_NET_URL_REQUEST_STREAM_JOB_H_
#include <memory>
#include <string>
#include <vector>
#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"
#include "net/base/io_buffer.h"
#include "net/http/http_status_code.h"
#include "net/url_request/url_request_context_getter.h"
#include "v8/include/v8.h"
namespace atom {
class URLRequestStreamJob : public JsAsker<net::URLRequestJob> {
public:
URLRequestStreamJob(net::URLRequest* request,
net::NetworkDelegate* network_delegate);
~URLRequestStreamJob() override;
void OnData(std::vector<char>&& buffer); // NOLINT
void OnEnd();
void OnError();
// URLRequestJob
void GetResponseInfo(net::HttpResponseInfo* info) override;
protected:
// URLRequestJob
int ReadRawData(net::IOBuffer* buf, int buf_size) override;
void DoneReading() override;
void DoneReadingRedirectResponse() override;
std::unique_ptr<net::SourceStream> SetUpSourceStream() override;
bool GetMimeType(std::string* mime_type) const override;
int GetResponseCode() const override;
private:
// JSAsker
void BeforeStartInUI(v8::Isolate*, v8::Local<v8::Value>) override;
void StartAsync(std::unique_ptr<base::Value> options) override;
void OnResponse(bool success, std::unique_ptr<base::Value> value);
int BufferCopy(std::vector<char>* source,
net::IOBuffer* target, int target_size);
// 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_;
std::unique_ptr<mate::StreamSubscriber> subscriber_;
base::WeakPtrFactory<URLRequestStreamJob> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(URLRequestStreamJob);
};
} // namespace atom
#endif // ATOM_BROWSER_NET_URL_REQUEST_STREAM_JOB_H_