Chromium Code Reviews| Index: content/browser/loader/navigation_resource_handler.cc |
| diff --git a/content/browser/loader/navigation_resource_handler.cc b/content/browser/loader/navigation_resource_handler.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f8917579126185961019c095da4fa729123a7faf |
| --- /dev/null |
| +++ b/content/browser/loader/navigation_resource_handler.cc |
| @@ -0,0 +1,175 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/browser/loader/navigation_resource_handler.h" |
| + |
| +#include "base/guid.h" |
| +#include "base/logging.h" |
| +#include "content/browser/loader/navigation_url_loader_core.h" |
| +#include "content/browser/loader/resource_request_info_impl.h" |
| +#include "content/browser/resource_context_impl.h" |
| +#include "content/browser/streams/stream.h" |
| +#include "content/browser/streams/stream_context.h" |
| +#include "content/browser/streams/stream_registry.h" |
| +#include "content/public/browser/resource_controller.h" |
| +#include "content/public/browser/stream_handle.h" |
| +#include "content/public/common/resource_response.h" |
| +#include "net/base/io_buffer.h" |
| +#include "net/base/net_errors.h" |
| +#include "net/url_request/url_request.h" |
| +#include "url/url_constants.h" |
| + |
| +namespace content { |
| + |
| +NavigationResourceHandler::NavigationResourceHandler( |
| + net::URLRequest* request, |
| + NavigationURLLoaderCore* core) |
| + : ResourceHandler(request), |
| + core_(core) { |
| + core_->set_resource_handler(this); |
| +} |
| + |
| +NavigationResourceHandler::~NavigationResourceHandler() { |
| + if (stream_.get()) { |
| + stream_->Finalize(); |
| + stream_->RemoveWriteObserver(this); |
| + } |
| + if (core_.get()) { |
| + core_->RequestFailedOnIOThread(net::ERR_ABORTED); |
| + core_ = NULL; |
| + } |
| +} |
| + |
| +void NavigationResourceHandler::Cancel() { |
| + controller()->Cancel(); |
|
clamy
2014/09/12 20:51:25
So if we cancel the request on the UI thread, this
davidben
2014/09/19 18:30:49
Done. It'll actually call onResponseCompleted firs
|
| +} |
| + |
| +void NavigationResourceHandler::Resume() { |
| + controller()->Resume(); |
| +} |
| + |
| +bool NavigationResourceHandler::OnUploadProgress(uint64 position, |
| + uint64 size) { |
| + return true; |
| +} |
| + |
| +bool NavigationResourceHandler::OnRequestRedirected( |
| + const net::RedirectInfo& redirect_info, |
| + ResourceResponse* response, |
| + bool* defer) { |
| + // TODO(davidben): Perform a CSP check here, and anything else that would have |
| + // been done renderer-side. |
| + core_->RequestRedirectedOnIOThread(redirect_info, response); |
| + *defer = true; |
| + return true; |
| +} |
| + |
| +bool NavigationResourceHandler::OnResponseStarted(ResourceResponse* response, |
| + bool* defer) { |
| + DCHECK(!stream_.get()); |
| + DCHECK(core_.get()); |
| + |
| + ResourceRequestInfoImpl* info = GetRequestInfo(); |
| + |
| + // If the BufferedResourceHandler intercepted this request and converted it |
| + // into a download, it will still call OnResponseStarted and immediately |
| + // cancel. Ignore the call; OnReadCompleted will happen shortly. |
| + // |
| + // TODO(davidben): Move the dispatch out of BufferedResourceHandler. Perhaps |
| + // all the way to the UI thread. Downloads, user certificates, etc., should be |
| + // dispatched at the navigation layer. |
| + if (info->IsDownload() || info->is_stream()) |
| + return true; |
| + |
| + StreamContext* stream_context = |
| + GetStreamContextForResourceContext(info->GetContext()); |
| + |
| + GURL stream_url(std::string(url::kBlobScheme) + ":" + |
| + request()->url().GetOrigin().spec() + |
| + base::GenerateGUID()); |
| + stream_ = new Stream(stream_context->registry(), this, stream_url); |
| + |
| + core_->ResponseStartedOnIOThread(response, stream_->CreateHandle()); |
| + core_ = NULL; |
| + return true; |
| +} |
| + |
| +bool NavigationResourceHandler::OnWillStart(const GURL& url, bool* defer) { |
| + return true; |
| +} |
| + |
| +bool NavigationResourceHandler::OnBeforeNetworkStart(const GURL& url, |
| + bool* defer) { |
| + return true; |
| +} |
| + |
| +bool NavigationResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
| + int* buf_size, |
| + int min_size) { |
| + static const int kReadBufSize = 32768; |
|
clamy
2014/09/12 20:51:25
This seem to be a copy paste of StreamResourceHand
davidben
2014/09/19 18:30:49
Using StreamResourceHandler directly is a little a
|
| + |
| + DCHECK(buf && buf_size); |
| + if (!read_buffer_.get()) |
| + read_buffer_ = new net::IOBuffer(kReadBufSize); |
| + *buf = read_buffer_.get(); |
| + *buf_size = kReadBufSize; |
| + |
| + return true; |
| +} |
| + |
| +bool NavigationResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { |
| + DCHECK(stream_.get()); |
| + |
| + if (!bytes_read) |
| + return true; |
| + |
| + // We have more data to read. |
| + DCHECK(read_buffer_.get()); |
| + |
| + // Release the ownership of the buffer, and store a reference |
| + // to it. A new one will be allocated in OnWillRead(). |
| + scoped_refptr<net::IOBuffer> buffer; |
| + read_buffer_.swap(buffer); |
| + stream_->AddData(buffer, bytes_read); |
| + |
| + if (!stream_->can_add_data()) |
| + *defer = true; |
| + |
| + return true; |
| +} |
| + |
| +void NavigationResourceHandler::OnResponseCompleted( |
| + const net::URLRequestStatus& status, |
| + const std::string& security_info, |
| + bool* defer) { |
| + // If the request has already committed, close the stream and leave it as-is. |
| + // |
| + // TODO(davidben): Is the final status of a navigation request meaningful? |
| + // Perhaps we should eventually extend the streams interface to push the |
| + // status down. (If there was an error partway through loading the top-level.) |
| + if (stream_.get()) { |
| + stream_->Finalize(); |
| + return; |
| + } |
| + |
| + DCHECK_NE(net::OK, status.error()); |
| + core_->RequestFailedOnIOThread(status.error()); |
| + core_ = NULL; |
| +} |
| + |
| +void NavigationResourceHandler::OnDataDownloaded(int bytes_downloaded) { |
| + NOTREACHED(); |
| +} |
| + |
| +void NavigationResourceHandler::OnSpaceAvailable(Stream* stream) { |
| + controller()->Resume(); |
| +} |
| + |
| +void NavigationResourceHandler::OnClose(Stream* stream) { |
| + // TODO(davidben): This doesn't cancel the request synchronously. Does this |
| + // matter? |
| + controller()->Cancel(); |
| +} |
| + |
| +} // namespace content |