Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(343)

Side by Side Diff: chrome/browser/renderer_host/download_throttling_resource_handler.cc

Issue 6459005: Cancel prerender when we discover a download starting from a page we are prer... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Changed source of notification and now notify on all entry points for download initiation. Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/renderer_host/download_throttling_resource_handler.h" 5 #include "chrome/browser/renderer_host/download_throttling_resource_handler.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "chrome/browser/renderer_host/download_resource_handler.h" 8 #include "chrome/browser/renderer_host/download_resource_handler.h"
9 #include "chrome/browser/renderer_host/render_view_host.h"
9 #include "chrome/browser/renderer_host/resource_dispatcher_host.h" 10 #include "chrome/browser/renderer_host/resource_dispatcher_host.h"
11 #include "chrome/common/notification_service.h"
10 #include "chrome/common/resource_response.h" 12 #include "chrome/common/resource_response.h"
11 #include "net/base/io_buffer.h" 13 #include "net/base/io_buffer.h"
12 #include "net/base/mime_sniffer.h" 14 #include "net/base/mime_sniffer.h"
13 15
16 namespace {
17 void NotifyOnUI(NotificationType::Type notification_type,
18 int process_id, int view_id) {
19 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
20 RenderViewHost* rvh = RenderViewHost::FromID(process_id, view_id);
21 if (!rvh)
22 return;
23
24 NotificationService::current()->Notify(notification_type,
25 Source<RenderViewHost>(rvh),
26 NotificationService::NoDetails());
27 }
28 } // namespace
29
14 DownloadThrottlingResourceHandler::DownloadThrottlingResourceHandler( 30 DownloadThrottlingResourceHandler::DownloadThrottlingResourceHandler(
15 ResourceDispatcherHost* host, 31 ResourceDispatcherHost* host,
16 net::URLRequest* request, 32 net::URLRequest* request,
17 const GURL& url, 33 const GURL& url,
18 int render_process_host_id, 34 int render_process_host_id,
19 int render_view_id, 35 int render_view_id,
20 int request_id, 36 int request_id,
21 bool in_complete) 37 bool in_complete)
22 : host_(host), 38 : host_(host),
23 request_(request), 39 request_(request),
24 url_(url), 40 url_(url),
25 render_process_host_id_(render_process_host_id), 41 render_process_host_id_(render_process_host_id),
26 render_view_id_(render_view_id), 42 render_view_id_(render_view_id),
27 request_id_(request_id), 43 request_id_(request_id),
28 tmp_buffer_length_(0), 44 tmp_buffer_length_(0),
29 ignore_on_read_complete_(in_complete) { 45 ignore_on_read_complete_(in_complete) {
30 // Pause the request. 46 // Pause the request.
31 host_->PauseRequest(render_process_host_id_, request_id_, true); 47 host_->PauseRequest(render_process_host_id_, request_id_, true);
32 host_->download_request_limiter()->CanDownloadOnIOThread( 48 host_->download_request_limiter()->CanDownloadOnIOThread(
33 render_process_host_id_, render_view_id, this); 49 render_process_host_id_, render_view_id, this);
34 } 50 }
35 51
36 DownloadThrottlingResourceHandler::~DownloadThrottlingResourceHandler() { 52 DownloadThrottlingResourceHandler::~DownloadThrottlingResourceHandler() {
37 } 53 }
38 54
39 bool DownloadThrottlingResourceHandler::OnUploadProgress(int request_id, 55 bool DownloadThrottlingResourceHandler::OnUploadProgress(int request_id,
40 uint64 position, 56 uint64 position,
41 uint64 size) { 57 uint64 size) {
42 if (download_handler_.get()) 58 if (download_handler_.get())
43 return download_handler_->OnUploadProgress(request_id, position, size); 59 return download_handler_->OnUploadProgress(request_id, position, size);
44 return true; 60 return true;
45 } 61 }
46 62
47 bool DownloadThrottlingResourceHandler::OnRequestRedirected( 63 bool DownloadThrottlingResourceHandler::OnRequestRedirected(
48 int request_id, 64 int request_id,
49 const GURL& url, 65 const GURL& url,
50 ResourceResponse* response, 66 ResourceResponse* response,
51 bool* defer) { 67 bool* defer) {
68 BrowserThread::PostTask(
69 BrowserThread::UI, FROM_HERE,
70 NewRunnableFunction(&NotifyOnUI, NotificationType::DOWNLOAD_INITIATED,
71 render_process_host_id_, render_view_id_));
52 if (download_handler_.get()) { 72 if (download_handler_.get()) {
53 return download_handler_->OnRequestRedirected( 73 return download_handler_->OnRequestRedirected(
54 request_id, url, response, defer); 74 request_id, url, response, defer);
55 } 75 }
56 url_ = url; 76 url_ = url;
57 return true; 77 return true;
58 } 78 }
59 79
60 bool DownloadThrottlingResourceHandler::OnResponseStarted( 80 bool DownloadThrottlingResourceHandler::OnResponseStarted(
61 int request_id, 81 int request_id,
62 ResourceResponse* response) { 82 ResourceResponse* response) {
83 BrowserThread::PostTask(
84 BrowserThread::UI, FROM_HERE,
85 NewRunnableFunction(&NotifyOnUI, NotificationType::DOWNLOAD_INITIATED,
86 render_process_host_id_, render_view_id_));
Randy Smith (Not in Mondays) 2011/02/11 18:44:15 Why do separate notifications for request redirect
dominich 2011/02/11 22:51:54 Done.
87
63 if (download_handler_.get()) 88 if (download_handler_.get())
64 return download_handler_->OnResponseStarted(request_id, response); 89 return download_handler_->OnResponseStarted(request_id, response);
65 response_ = response; 90 response_ = response;
66 return true; 91 return true;
67 } 92 }
68 93
69 bool DownloadThrottlingResourceHandler::OnWillStart(int request_id, 94 bool DownloadThrottlingResourceHandler::OnWillStart(int request_id,
70 const GURL& url, 95 const GURL& url,
71 bool* defer) { 96 bool* defer) {
72 if (download_handler_.get()) 97 if (download_handler_.get())
73 return download_handler_->OnWillStart(request_id, url, defer); 98 return download_handler_->OnWillStart(request_id, url, defer);
74 return true; 99 return true;
75 } 100 }
76 101
77 bool DownloadThrottlingResourceHandler::OnWillRead(int request_id, 102 bool DownloadThrottlingResourceHandler::OnWillRead(int request_id,
78 net::IOBuffer** buf, 103 net::IOBuffer** buf,
79 int* buf_size, 104 int* buf_size,
80 int min_size) { 105 int min_size) {
106 BrowserThread::PostTask(
107 BrowserThread::UI, FROM_HERE,
108 NewRunnableFunction(&NotifyOnUI, NotificationType::DOWNLOAD_INITIATED,
dominich 2011/02/10 20:02:17 Moving this to OnWillStart for the same reasons as
109 render_process_host_id_, render_view_id_));
81 if (download_handler_.get()) 110 if (download_handler_.get())
82 return download_handler_->OnWillRead(request_id, buf, buf_size, min_size); 111 return download_handler_->OnWillRead(request_id, buf, buf_size, min_size);
83 112
84 // We should only have this invoked once, as such we only deal with one 113 // We should only have this invoked once, as such we only deal with one
85 // tmp buffer. 114 // tmp buffer.
86 DCHECK(!tmp_buffer_.get()); 115 DCHECK(!tmp_buffer_.get());
87 // If the caller passed a negative |min_size| then chose an appropriate 116 // If the caller passed a negative |min_size| then chose an appropriate
88 // default. The BufferedResourceHandler requires this to be at least 2 times 117 // default. The BufferedResourceHandler requires this to be at least 2 times
89 // the size required for mime detection. 118 // the size required for mime detection.
90 if (min_size < 0) 119 if (min_size < 0)
(...skipping 14 matching lines...) Expand all
105 if (!*bytes_read) 134 if (!*bytes_read)
106 return true; 135 return true;
107 136
108 if (tmp_buffer_.get()) { 137 if (tmp_buffer_.get()) {
109 DCHECK(!tmp_buffer_length_); 138 DCHECK(!tmp_buffer_length_);
110 tmp_buffer_length_ = *bytes_read; 139 tmp_buffer_length_ = *bytes_read;
111 if (download_handler_.get()) 140 if (download_handler_.get())
112 CopyTmpBufferToDownloadHandler(); 141 CopyTmpBufferToDownloadHandler();
113 return true; 142 return true;
114 } 143 }
144 BrowserThread::PostTask(
145 BrowserThread::UI, FROM_HERE,
cbentzel 2011/02/10 19:32:18 I really don't think this is what you want - it wi
dominich 2011/02/10 20:02:17 You're right - I was going from rdsmith's diagram
dominich 2011/02/10 20:02:17 Done.
Randy Smith (Not in Mondays) 2011/02/11 18:44:15 Sorry; that diagram shows control flow within the
146 NewRunnableFunction(&NotifyOnUI, NotificationType::DOWNLOAD_INITIATED,
147 render_process_host_id_, render_view_id_));
115 if (download_handler_.get()) 148 if (download_handler_.get())
116 return download_handler_->OnReadCompleted(request_id, bytes_read); 149 return download_handler_->OnReadCompleted(request_id, bytes_read);
117 return true; 150 return true;
118 } 151 }
119 152
120 bool DownloadThrottlingResourceHandler::OnResponseCompleted( 153 bool DownloadThrottlingResourceHandler::OnResponseCompleted(
121 int request_id, 154 int request_id,
122 const net::URLRequestStatus& status, 155 const net::URLRequestStatus& status,
123 const std::string& security_info) { 156 const std::string& security_info) {
157 BrowserThread::PostTask(
158 BrowserThread::UI, FROM_HERE,
159 NewRunnableFunction(&NotifyOnUI, NotificationType::DOWNLOAD_INITIATED,
dominich 2011/02/10 20:02:17 This suffers from the same issue as above. Even th
160 render_process_host_id_, render_view_id_));
124 if (download_handler_.get()) 161 if (download_handler_.get())
125 return download_handler_->OnResponseCompleted(request_id, status, 162 return download_handler_->OnResponseCompleted(request_id, status,
126 security_info); 163 security_info);
127 164
128 // For a download, if ResourceDispatcher::Read() fails, 165 // For a download, if ResourceDispatcher::Read() fails,
129 // ResourceDispatcher::OnresponseStarted() will call 166 // ResourceDispatcher::OnresponseStarted() will call
130 // OnResponseCompleted(), and we will end up here with an error 167 // OnResponseCompleted(), and we will end up here with an error
131 // status. 168 // status.
132 if (!status.is_success()) 169 if (!status.is_success())
133 return false; 170 return false;
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 int buf_size; 213 int buf_size;
177 if (download_handler_->OnWillRead(request_id_, &buffer, &buf_size, 214 if (download_handler_->OnWillRead(request_id_, &buffer, &buf_size,
178 tmp_buffer_length_)) { 215 tmp_buffer_length_)) {
179 CHECK(buf_size >= tmp_buffer_length_); 216 CHECK(buf_size >= tmp_buffer_length_);
180 memcpy(buffer->data(), tmp_buffer_->data(), tmp_buffer_length_); 217 memcpy(buffer->data(), tmp_buffer_->data(), tmp_buffer_length_);
181 download_handler_->OnReadCompleted(request_id_, &tmp_buffer_length_); 218 download_handler_->OnReadCompleted(request_id_, &tmp_buffer_length_);
182 } 219 }
183 tmp_buffer_length_ = 0; 220 tmp_buffer_length_ = 0;
184 tmp_buffer_ = NULL; 221 tmp_buffer_ = NULL;
185 } 222 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698