Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/loader/navigation_resource_handler.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "content/browser/devtools/devtools_netlog_observer.h" | |
| 9 #include "content/browser/loader/navigation_url_loader_impl_core.h" | |
| 10 #include "content/browser/loader/resource_request_info_impl.h" | |
| 11 #include "content/browser/resource_context_impl.h" | |
| 12 #include "content/browser/streams/stream.h" | |
| 13 #include "content/browser/streams/stream_context.h" | |
| 14 #include "content/public/browser/resource_controller.h" | |
| 15 #include "content/public/browser/stream_handle.h" | |
| 16 #include "content/public/common/resource_response.h" | |
| 17 #include "net/base/net_errors.h" | |
| 18 #include "net/url_request/url_request.h" | |
| 19 | |
| 20 namespace content { | |
| 21 | |
| 22 NavigationResourceHandler::NavigationResourceHandler( | |
| 23 net::URLRequest* request, | |
| 24 NavigationURLLoaderImplCore* core) | |
| 25 : ResourceHandler(request), | |
| 26 core_(core) { | |
| 27 core_->set_resource_handler(this); | |
| 28 } | |
| 29 | |
| 30 NavigationResourceHandler::~NavigationResourceHandler() { | |
| 31 if (core_) { | |
| 32 core_->NotifyRequestFailed(net::ERR_ABORTED); | |
| 33 DetachFromCore(); | |
| 34 } | |
| 35 } | |
| 36 | |
| 37 void NavigationResourceHandler::Cancel() { | |
| 38 controller()->Cancel(); | |
| 39 core_ = nullptr; | |
| 40 } | |
| 41 | |
| 42 void NavigationResourceHandler::FollowRedirect() { | |
| 43 controller()->Resume(); | |
| 44 } | |
| 45 | |
| 46 void NavigationResourceHandler::SetController(ResourceController* controller) { | |
| 47 writer_.set_controller(controller); | |
| 48 ResourceHandler::SetController(controller); | |
| 49 } | |
| 50 | |
| 51 bool NavigationResourceHandler::OnUploadProgress(uint64 position, | |
| 52 uint64 size) { | |
|
nasko
2014/10/23 22:36:16
nit: this should fit on the previous line
davidben
2014/10/27 20:46:04
Done.
| |
| 53 return true; | |
| 54 } | |
| 55 | |
| 56 bool NavigationResourceHandler::OnRequestRedirected( | |
| 57 const net::RedirectInfo& redirect_info, | |
| 58 ResourceResponse* response, | |
| 59 bool* defer) { | |
| 60 DCHECK(core_); | |
| 61 | |
| 62 // TODO(davidben): Perform a CSP check here, and anything else that would have | |
| 63 // been done renderer-side. | |
| 64 DevToolsNetLogObserver::PopulateResponseInfo(request(), response); | |
| 65 core_->NotifyRequestRedirected(redirect_info, response); | |
| 66 *defer = true; | |
| 67 return true; | |
| 68 } | |
| 69 | |
| 70 bool NavigationResourceHandler::OnResponseStarted(ResourceResponse* response, | |
| 71 bool* defer) { | |
| 72 DCHECK(core_); | |
| 73 | |
| 74 ResourceRequestInfoImpl* info = GetRequestInfo(); | |
| 75 | |
| 76 // If the BufferedResourceHandler intercepted this request and converted it | |
| 77 // into a download, it will still call OnResponseStarted and immediately | |
| 78 // cancel. Ignore the call; OnReadCompleted will happen shortly. | |
| 79 // | |
| 80 // TODO(davidben): Move the dispatch out of BufferedResourceHandler. Perhaps | |
| 81 // all the way to the UI thread. Downloads, user certificates, etc., should be | |
| 82 // dispatched at the navigation layer. | |
| 83 if (info->IsDownload() || info->is_stream()) | |
| 84 return true; | |
| 85 | |
| 86 StreamContext* stream_context = | |
| 87 GetStreamContextForResourceContext(info->GetContext()); | |
| 88 writer_.InitializeStream(stream_context->registry(), | |
| 89 request()->url().GetOrigin()); | |
| 90 | |
| 91 // Detach from the loader; at this point, the request is now owned by the | |
| 92 // StreamHandle. | |
| 93 DevToolsNetLogObserver::PopulateResponseInfo(request(), response); | |
| 94 core_->NotifyResponseStarted(response, writer_.stream()->CreateHandle()); | |
| 95 DetachFromCore(); | |
| 96 return true; | |
| 97 } | |
| 98 | |
| 99 bool NavigationResourceHandler::OnWillStart(const GURL& url, bool* defer) { | |
| 100 return true; | |
| 101 } | |
| 102 | |
| 103 bool NavigationResourceHandler::OnBeforeNetworkStart(const GURL& url, | |
| 104 bool* defer) { | |
| 105 return true; | |
| 106 } | |
| 107 | |
| 108 bool NavigationResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, | |
| 109 int* buf_size, | |
| 110 int min_size) { | |
| 111 writer_.OnWillRead(buf, buf_size, min_size); | |
| 112 return true; | |
| 113 } | |
| 114 | |
| 115 bool NavigationResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { | |
| 116 writer_.OnReadCompleted(bytes_read, defer); | |
| 117 return true; | |
| 118 } | |
| 119 | |
| 120 void NavigationResourceHandler::OnResponseCompleted( | |
| 121 const net::URLRequestStatus& status, | |
| 122 const std::string& security_info, | |
| 123 bool* defer) { | |
| 124 // If the request has already committed, close the stream and leave it as-is. | |
| 125 // | |
| 126 // TODO(davidben): The net error code should be passed through StreamWriter | |
| 127 // down to the stream's consumer. See https://crbug.com/426162. | |
| 128 if (writer_.stream()) { | |
| 129 writer_.Finalize(); | |
| 130 return; | |
| 131 } | |
| 132 | |
| 133 if (core_) { | |
| 134 DCHECK_NE(net::OK, status.error()); | |
| 135 core_->NotifyRequestFailed(status.error()); | |
| 136 DetachFromCore(); | |
| 137 } | |
| 138 } | |
| 139 | |
| 140 void NavigationResourceHandler::OnDataDownloaded(int bytes_downloaded) { | |
| 141 NOTREACHED(); | |
| 142 } | |
| 143 | |
| 144 void NavigationResourceHandler::DetachFromCore() { | |
| 145 DCHECK(core_); | |
| 146 core_->set_resource_handler(nullptr); | |
| 147 core_ = nullptr; | |
| 148 } | |
| 149 | |
| 150 } // namespace content | |
| OLD | NEW |