Merge pull request #9597 from electron/revert-9358

Revert #9358
This commit is contained in:
Kevin Sawicki 2017-05-26 10:09:37 -07:00 committed by GitHub
commit aceea50f41
5 changed files with 77 additions and 105 deletions

View file

@ -9,18 +9,14 @@
#include "atom/browser/api/atom_api_session.h" #include "atom/browser/api/atom_api_session.h"
#include "atom/browser/atom_browser_context.h" #include "atom/browser/atom_browser_context.h"
#include "base/guid.h"
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "brightray/browser/url_request_context_getter.h"
#include "content/browser/streams/stream_context.h"
#include "native_mate/dictionary.h" #include "native_mate/dictionary.h"
#include "net/base/io_buffer.h" #include "net/base/io_buffer.h"
#include "net/base/net_errors.h" #include "net/base/net_errors.h"
#include "net/http/http_response_headers.h" #include "net/http/http_response_headers.h"
#include "net/url_request/url_fetcher.h" #include "net/url_request/url_fetcher.h"
#include "net/url_request/url_fetcher_response_writer.h" #include "net/url_request/url_fetcher_response_writer.h"
#include "url/url_constants.h"
using content::BrowserThread; using content::BrowserThread;
@ -66,8 +62,7 @@ class ResponsePiper : public net::URLFetcherResponseWriter {
job_->HeadersCompleted(); job_->HeadersCompleted();
first_write_ = false; first_write_ = false;
} }
job_->stream()->AddData(buffer->data(), num_bytes); return job_->DataAvailable(buffer, num_bytes, callback);
return num_bytes;
} }
int Finish(int net_error, const net::CompletionCallback& callback) override { int Finish(int net_error, const net::CompletionCallback& callback) override {
return net::OK; return net::OK;
@ -82,11 +77,12 @@ class ResponsePiper : public net::URLFetcherResponseWriter {
} // namespace } // namespace
URLRequestFetchJob::URLRequestFetchJob(net::URLRequest* request, URLRequestFetchJob::URLRequestFetchJob(
net::NetworkDelegate* network_delegate) net::URLRequest* request, net::NetworkDelegate* network_delegate)
: JsAsker<net::URLRequestJob>(request, network_delegate), : JsAsker<net::URLRequestJob>(request, network_delegate),
pending_buffer_size_(0), pending_buffer_size_(0),
total_bytes_read_(0) {} write_num_bytes_(0) {
}
void URLRequestFetchJob::BeforeStartInUI( void URLRequestFetchJob::BeforeStartInUI(
v8::Isolate* isolate, v8::Local<v8::Value> value) { v8::Isolate* isolate, v8::Local<v8::Value> value) {
@ -173,22 +169,7 @@ void URLRequestFetchJob::StartAsync(std::unique_ptr<base::Value> options) {
fetcher_->SetExtraRequestHeaders( fetcher_->SetExtraRequestHeaders(
request()->extra_request_headers().ToString()); request()->extra_request_headers().ToString());
// Create readable stream for URLFetcher response.
content::StreamContext* stream_context =
static_cast<brightray::URLRequestContextGetter*>(request_context_getter())
->stream_context();
if (stream_context) {
GURL stream_url(std::string(url::kBlobScheme) + ":" +
formated_url.GetOrigin().spec() + base::GenerateGUID());
stream_ =
new content::Stream(stream_context->registry(), nullptr, stream_url);
stream_->SetReadObserver(this);
fetcher_->Start(); fetcher_->Start();
} else {
NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::CANCELED,
net::ERR_ABORTED));
}
} }
void URLRequestFetchJob::HeadersCompleted() { void URLRequestFetchJob::HeadersCompleted() {
@ -197,30 +178,30 @@ void URLRequestFetchJob::HeadersCompleted() {
NotifyHeadersComplete(); NotifyHeadersComplete();
} }
void URLRequestFetchJob::Kill() { int URLRequestFetchJob::DataAvailable(net::IOBuffer* buffer,
JsAsker<URLRequestJob>::Kill(); int num_bytes,
ClearStream(); const net::CompletionCallback& callback) {
fetcher_.reset(); // When pending_buffer_ is empty, there's no ReadRawData() operation waiting
// for IO completion, we have to save the parameters until the request is
// ready to read data.
if (!pending_buffer_.get()) {
write_buffer_ = buffer;
write_num_bytes_ = num_bytes;
write_callback_ = callback;
return net::ERR_IO_PENDING;
} }
void URLRequestFetchJob::OnDataAvailable(content::Stream* stream) { // Write data to the pending buffer and clear them after the writing.
if (!pending_buffer_.get()) int bytes_read = BufferCopy(buffer, num_bytes,
return; pending_buffer_.get(), pending_buffer_size_);
ClearPendingBuffer();
ReadRawDataComplete(bytes_read);
return bytes_read;
}
int result = 0; void URLRequestFetchJob::Kill() {
auto state = stream_->ReadRawData(pending_buffer_.get(), pending_buffer_size_, JsAsker<URLRequestJob>::Kill();
&result); fetcher_.reset();
if (state == content::Stream::STREAM_ABORTED)
result = net::ERR_CONNECTION_RESET;
// Clear the buffers before notifying the read is complete, so that it is
// safe for the observer to read.
pending_buffer_ = nullptr;
pending_buffer_size_ = 0;
if (result > 0)
total_bytes_read_ += result;
ReadRawDataComplete(result);
} }
int URLRequestFetchJob::ReadRawData(net::IOBuffer* dest, int dest_size) { int URLRequestFetchJob::ReadRawData(net::IOBuffer* dest, int dest_size) {
@ -229,23 +210,21 @@ int URLRequestFetchJob::ReadRawData(net::IOBuffer* dest, int dest_size) {
return net::OK; return net::OK;
} }
int bytes_read = 0; // When write_buffer_ is empty, there is no data valable yet, we have to save
switch (stream_->ReadRawData(dest, dest_size, &bytes_read)) { // the dest buffer util DataAvailable.
case content::Stream::STREAM_HAS_DATA: if (!write_buffer_.get()) {
total_bytes_read_ += bytes_read;
return bytes_read;
case content::Stream::STREAM_COMPLETE:
return stream_->GetStatus();
case content::Stream::STREAM_EMPTY:
pending_buffer_ = dest; pending_buffer_ = dest;
pending_buffer_size_ = dest_size; pending_buffer_size_ = dest_size;
return net::ERR_IO_PENDING; return net::ERR_IO_PENDING;
case content::Stream::STREAM_ABORTED:
// Handle this as connection reset.
return net::ERR_CONNECTION_RESET;
} }
NOTREACHED();
return net::ERR_FAILED; // Read from the write buffer and clear them after reading.
int bytes_read = BufferCopy(write_buffer_.get(), write_num_bytes_,
dest, dest_size);
net::CompletionCallback write_callback = write_callback_;
ClearWriteBuffer();
write_callback.Run(bytes_read);
return bytes_read;
} }
bool URLRequestFetchJob::GetMimeType(std::string* mime_type) const { bool URLRequestFetchJob::GetMimeType(std::string* mime_type) const {
@ -267,18 +246,11 @@ int URLRequestFetchJob::GetResponseCode() const {
return response_info_->headers->response_code(); return response_info_->headers->response_code();
} }
int64_t URLRequestFetchJob::GetTotalReceivedBytes() const {
int64_t total_received_bytes = 0;
if (response_info_)
total_received_bytes = response_info_->headers->raw_headers().size();
if (stream_.get())
total_received_bytes += total_bytes_read_;
return total_received_bytes;
}
void URLRequestFetchJob::OnURLFetchComplete(const net::URLFetcher* source) { void URLRequestFetchJob::OnURLFetchComplete(const net::URLFetcher* source) {
auto status = fetcher_->GetStatus(); ClearPendingBuffer();
if (status.is_success()) { ClearWriteBuffer();
if (fetcher_->GetStatus().is_success()) {
if (!response_info_) { if (!response_info_) {
// Since we notify header completion only after first write there will be // Since we notify header completion only after first write there will be
// no response object constructed for http respones with no content 204. // no response object constructed for http respones with no content 204.
@ -286,16 +258,28 @@ void URLRequestFetchJob::OnURLFetchComplete(const net::URLFetcher* source) {
HeadersCompleted(); HeadersCompleted();
return; return;
} }
stream_->Finalize(0); ReadRawDataComplete(0);
} else { } else {
stream_->Finalize(status.error()); NotifyStartError(fetcher_->GetStatus());
NotifyStartError(status);
} }
} }
void URLRequestFetchJob::ClearStream() { int URLRequestFetchJob::BufferCopy(net::IOBuffer* source, int num_bytes,
stream_->RemoveReadObserver(this); net::IOBuffer* target, int target_size) {
stream_ = nullptr; int bytes_written = std::min(num_bytes, target_size);
memcpy(target->data(), source->data(), bytes_written);
return bytes_written;
}
void URLRequestFetchJob::ClearPendingBuffer() {
pending_buffer_ = nullptr;
pending_buffer_size_ = 0;
}
void URLRequestFetchJob::ClearWriteBuffer() {
write_buffer_ = nullptr;
write_num_bytes_ = 0;
write_callback_.Reset();
} }
} // namespace atom } // namespace atom

View file

@ -17,15 +17,15 @@ namespace atom {
class URLRequestFetchJob : public JsAsker<net::URLRequestJob>, class URLRequestFetchJob : public JsAsker<net::URLRequestJob>,
public net::URLFetcherDelegate, public net::URLFetcherDelegate,
public brightray::URLRequestContextGetter::Delegate, public brightray::URLRequestContextGetter::Delegate {
public content::StreamReadObserver {
public: public:
URLRequestFetchJob(net::URLRequest*, net::NetworkDelegate*); URLRequestFetchJob(net::URLRequest*, net::NetworkDelegate*);
// Called by response writer. // Called by response writer.
void HeadersCompleted(); void HeadersCompleted();
int DataAvailable(net::IOBuffer* buffer,
content::Stream* stream() const { return stream_.get(); } int num_bytes,
const net::CompletionCallback& callback);
protected: protected:
// JsAsker: // JsAsker:
@ -38,26 +38,28 @@ class URLRequestFetchJob : public JsAsker<net::URLRequestJob>,
bool GetMimeType(std::string* mime_type) const override; bool GetMimeType(std::string* mime_type) const override;
void GetResponseInfo(net::HttpResponseInfo* info) override; void GetResponseInfo(net::HttpResponseInfo* info) override;
int GetResponseCode() const override; int GetResponseCode() const override;
int64_t GetTotalReceivedBytes() const override;
// net::URLFetcherDelegate: // net::URLFetcherDelegate:
void OnURLFetchComplete(const net::URLFetcher* source) override; void OnURLFetchComplete(const net::URLFetcher* source) override;
// content::StreamReadObserver:
void OnDataAvailable(content::Stream* stream) override;
private: private:
void ClearStream(); int BufferCopy(net::IOBuffer* source, int num_bytes,
net::IOBuffer* target, int target_size);
void ClearPendingBuffer();
void ClearWriteBuffer();
scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
std::unique_ptr<net::URLFetcher> fetcher_; std::unique_ptr<net::URLFetcher> fetcher_;
std::unique_ptr<net::HttpResponseInfo> response_info_; std::unique_ptr<net::HttpResponseInfo> response_info_;
scoped_refptr<content::Stream> stream_;
// Saved arguments passed to ReadRawData. // Saved arguments passed to ReadRawData.
scoped_refptr<net::IOBuffer> pending_buffer_; scoped_refptr<net::IOBuffer> pending_buffer_;
int pending_buffer_size_; int pending_buffer_size_;
int total_bytes_read_;
// Saved arguments passed to DataAvailable.
scoped_refptr<net::IOBuffer> write_buffer_;
int write_num_bytes_;
net::CompletionCallback write_callback_;
DISALLOW_COPY_AND_ASSIGN(URLRequestFetchJob); DISALLOW_COPY_AND_ASSIGN(URLRequestFetchJob);
}; };

View file

@ -19,7 +19,6 @@
#include "components/prefs/pref_registry_simple.h" #include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h" #include "components/prefs/pref_service.h"
#include "components/prefs/pref_service_factory.h" #include "components/prefs/pref_service_factory.h"
#include "content/browser/streams/stream_context.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "content/public/browser/resource_context.h" #include "content/public/browser/resource_context.h"
#include "content/public/browser/storage_partition.h" #include "content/public/browser/storage_partition.h"
@ -162,10 +161,6 @@ MediaDeviceIDSalt* BrowserContext::GetMediaDeviceIDSalt() {
return media_device_id_salt_.get(); return media_device_id_salt_.get();
} }
content::StreamContext* BrowserContext::GetStreamContext() {
return content::StreamContext::GetFor(this);
}
base::FilePath BrowserContext::GetPath() const { base::FilePath BrowserContext::GetPath() const {
return path_; return path_;
} }

View file

@ -89,7 +89,6 @@ class BrowserContext : public base::RefCounted<BrowserContext>,
// URLRequestContextGetter::Delegate: // URLRequestContextGetter::Delegate:
net::NetworkDelegate* CreateNetworkDelegate() override; net::NetworkDelegate* CreateNetworkDelegate() override;
MediaDeviceIDSalt* GetMediaDeviceIDSalt() override; MediaDeviceIDSalt* GetMediaDeviceIDSalt() override;
content::StreamContext* GetStreamContext() override;
base::FilePath GetPath() const override; base::FilePath GetPath() const override;

View file

@ -21,10 +21,6 @@ namespace base {
class MessageLoop; class MessageLoop;
} }
namespace content {
class StreamContext;
}
namespace net { namespace net {
class HostMappingRules; class HostMappingRules;
class HostResolver; class HostResolver;
@ -65,7 +61,6 @@ class URLRequestContextGetter : public net::URLRequestContextGetter {
return nullptr; return nullptr;
} }
virtual MediaDeviceIDSalt* GetMediaDeviceIDSalt() { return nullptr; } virtual MediaDeviceIDSalt* GetMediaDeviceIDSalt() { return nullptr; }
virtual content::StreamContext* GetStreamContext() { return nullptr; }
}; };
URLRequestContextGetter( URLRequestContextGetter(
@ -90,9 +85,6 @@ class URLRequestContextGetter : public net::URLRequestContextGetter {
MediaDeviceIDSalt* GetMediaDeviceIDSalt() const { MediaDeviceIDSalt* GetMediaDeviceIDSalt() const {
return delegate_->GetMediaDeviceIDSalt(); return delegate_->GetMediaDeviceIDSalt();
} }
content::StreamContext* stream_context() const {
return delegate_->GetStreamContext();
}
private: private:
Delegate* delegate_; Delegate* delegate_;