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/loader/navigation_url_loader_core.h" |
| 9 #include "content/browser/loader/resource_request_info_impl.h" |
| 10 #include "content/browser/resource_context_impl.h" |
| 11 #include "content/browser/streams/stream.h" |
| 12 #include "content/browser/streams/stream_context.h" |
| 13 #include "content/public/browser/resource_controller.h" |
| 14 #include "content/public/browser/stream_handle.h" |
| 15 #include "content/public/common/resource_response.h" |
| 16 #include "net/base/net_errors.h" |
| 17 #include "net/url_request/url_request.h" |
| 18 |
| 19 namespace content { |
| 20 |
| 21 NavigationResourceHandler::NavigationResourceHandler( |
| 22 net::URLRequest* request, |
| 23 NavigationURLLoaderCore* core) |
| 24 : ResourceHandler(request), |
| 25 core_(core) { |
| 26 core_->set_resource_handler(this); |
| 27 } |
| 28 |
| 29 NavigationResourceHandler::~NavigationResourceHandler() { |
| 30 if (core_.get()) { |
| 31 core_->RequestFailedOnIOThread(net::ERR_ABORTED); |
| 32 core_->set_resource_handler(NULL); |
| 33 core_ = NULL; |
| 34 } |
| 35 } |
| 36 |
| 37 void NavigationResourceHandler::Cancel() { |
| 38 controller()->Cancel(); |
| 39 |
| 40 if (core_.get()) { |
| 41 // At this point, the core is detached from the UI-thread loader, so there |
| 42 // is no use in signaling back in OnResponseCompleted. |
| 43 core_->set_resource_handler(NULL); |
| 44 core_ = NULL; |
| 45 } |
| 46 } |
| 47 |
| 48 void NavigationResourceHandler::FollowRedirect() { |
| 49 controller()->Resume(); |
| 50 } |
| 51 |
| 52 void NavigationResourceHandler::SetController(ResourceController* controller) { |
| 53 writer_.set_controller(controller); |
| 54 ResourceHandler::SetController(controller); |
| 55 } |
| 56 |
| 57 bool NavigationResourceHandler::OnUploadProgress(uint64 position, |
| 58 uint64 size) { |
| 59 return true; |
| 60 } |
| 61 |
| 62 bool NavigationResourceHandler::OnRequestRedirected( |
| 63 const net::RedirectInfo& redirect_info, |
| 64 ResourceResponse* response, |
| 65 bool* defer) { |
| 66 // TODO(davidben): Perform a CSP check here, and anything else that would have |
| 67 // been done renderer-side. |
| 68 core_->RequestRedirectedOnIOThread(redirect_info, response); |
| 69 *defer = true; |
| 70 return true; |
| 71 } |
| 72 |
| 73 bool NavigationResourceHandler::OnResponseStarted(ResourceResponse* response, |
| 74 bool* defer) { |
| 75 DCHECK(core_.get()); |
| 76 |
| 77 ResourceRequestInfoImpl* info = GetRequestInfo(); |
| 78 |
| 79 // If the BufferedResourceHandler intercepted this request and converted it |
| 80 // into a download, it will still call OnResponseStarted and immediately |
| 81 // cancel. Ignore the call; OnReadCompleted will happen shortly. |
| 82 // |
| 83 // TODO(davidben): Move the dispatch out of BufferedResourceHandler. Perhaps |
| 84 // all the way to the UI thread. Downloads, user certificates, etc., should be |
| 85 // dispatched at the navigation layer. |
| 86 if (info->IsDownload() || info->is_stream()) |
| 87 return true; |
| 88 |
| 89 StreamContext* stream_context = |
| 90 GetStreamContextForResourceContext(info->GetContext()); |
| 91 writer_.InitializeStream(stream_context->registry(), |
| 92 request()->url().GetOrigin()); |
| 93 |
| 94 // Detach from the loader; at this point, the request is now owned by the |
| 95 // StreamHandle. |
| 96 core_->ResponseStartedOnIOThread(response, writer_.stream()->CreateHandle()); |
| 97 core_->set_resource_handler(NULL); |
| 98 core_ = NULL; |
| 99 return true; |
| 100 } |
| 101 |
| 102 bool NavigationResourceHandler::OnWillStart(const GURL& url, bool* defer) { |
| 103 return true; |
| 104 } |
| 105 |
| 106 bool NavigationResourceHandler::OnBeforeNetworkStart(const GURL& url, |
| 107 bool* defer) { |
| 108 return true; |
| 109 } |
| 110 |
| 111 bool NavigationResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
| 112 int* buf_size, |
| 113 int min_size) { |
| 114 writer_.OnWillRead(buf, buf_size, min_size); |
| 115 return true; |
| 116 } |
| 117 |
| 118 bool NavigationResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { |
| 119 writer_.OnReadCompleted(bytes_read, defer); |
| 120 return true; |
| 121 } |
| 122 |
| 123 void NavigationResourceHandler::OnResponseCompleted( |
| 124 const net::URLRequestStatus& status, |
| 125 const std::string& security_info, |
| 126 bool* defer) { |
| 127 // If the request has already committed, close the stream and leave it as-is. |
| 128 // |
| 129 // TODO(davidben): Is the final status of a navigation request meaningful? |
| 130 // Perhaps we should eventually extend the streams interface to push the |
| 131 // status down. (If there was an error partway through loading the top-level.) |
| 132 if (writer_.stream()) { |
| 133 writer_.Finalize(); |
| 134 return; |
| 135 } |
| 136 |
| 137 if (core_.get()) { |
| 138 DCHECK_NE(net::OK, status.error()); |
| 139 core_->RequestFailedOnIOThread(status.error()); |
| 140 core_->set_resource_handler(NULL); |
| 141 core_ = NULL; |
| 142 } |
| 143 } |
| 144 |
| 145 void NavigationResourceHandler::OnDataDownloaded(int bytes_downloaded) { |
| 146 NOTREACHED(); |
| 147 } |
| 148 |
| 149 } // namespace content |
OLD | NEW |