OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/browser/webui/url_data_manager_backend.h" | 5 #include "content/browser/webui/url_data_manager_backend.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
112 public: | 112 public: |
113 // |is_incognito| set when job is generated from an incognito profile. | 113 // |is_incognito| set when job is generated from an incognito profile. |
114 URLRequestChromeJob(net::URLRequest* request, | 114 URLRequestChromeJob(net::URLRequest* request, |
115 net::NetworkDelegate* network_delegate, | 115 net::NetworkDelegate* network_delegate, |
116 URLDataManagerBackend* backend, | 116 URLDataManagerBackend* backend, |
117 bool is_incognito); | 117 bool is_incognito); |
118 | 118 |
119 // net::URLRequestJob implementation. | 119 // net::URLRequestJob implementation. |
120 void Start() override; | 120 void Start() override; |
121 void Kill() override; | 121 void Kill() override; |
122 bool ReadRawData(net::IOBuffer* buf, int buf_size, int* bytes_read) override; | 122 int ReadRawData(net::IOBuffer* buf, int buf_size) override; |
123 bool GetMimeType(std::string* mime_type) const override; | 123 bool GetMimeType(std::string* mime_type) const override; |
124 int GetResponseCode() const override; | 124 int GetResponseCode() const override; |
125 void GetResponseInfo(net::HttpResponseInfo* info) override; | 125 void GetResponseInfo(net::HttpResponseInfo* info) override; |
126 | 126 |
127 // Used to notify that the requested data's |mime_type| is ready. | 127 // Used to notify that the requested data's |mime_type| is ready. |
128 void MimeTypeAvailable(const std::string& mime_type); | 128 void MimeTypeAvailable(const std::string& mime_type); |
129 | 129 |
130 // Called by ChromeURLDataManager to notify us that the data blob is ready | 130 // Called by ChromeURLDataManager to notify us that the data blob is ready |
131 // for us. | 131 // for us. |
132 void DataAvailable(base::RefCountedMemory* bytes); | 132 void DataAvailable(base::RefCountedMemory* bytes); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
184 static void CheckStoragePartitionMatches( | 184 static void CheckStoragePartitionMatches( |
185 int render_process_id, | 185 int render_process_id, |
186 const GURL& url, | 186 const GURL& url, |
187 const base::WeakPtr<URLRequestChromeJob>& job); | 187 const base::WeakPtr<URLRequestChromeJob>& job); |
188 | 188 |
189 // Specific resources require unsafe-eval in the Content Security Policy. | 189 // Specific resources require unsafe-eval in the Content Security Policy. |
190 bool RequiresUnsafeEval() const; | 190 bool RequiresUnsafeEval() const; |
191 | 191 |
192 // Do the actual copy from data_ (the data we're serving) into |buf|. | 192 // Do the actual copy from data_ (the data we're serving) into |buf|. |
193 // Separate from ReadRawData so we can handle async I/O. | 193 // Separate from ReadRawData so we can handle async I/O. |
194 void CompleteRead(net::IOBuffer* buf, int buf_size, int* bytes_read); | 194 // return value is either >= 0 to indicate a successful read and count of |
195 // bytes read, or < 0 to indicate an error. | |
davidben
2015/11/03 22:50:05
It doesn't look like this function can actually re
xunjieli
2015/11/04 01:26:51
Done.
| |
196 int CompleteRead(net::IOBuffer* buf, int buf_size); | |
195 | 197 |
196 // The actual data we're serving. NULL until it's been fetched. | 198 // The actual data we're serving. NULL until it's been fetched. |
197 scoped_refptr<base::RefCountedMemory> data_; | 199 scoped_refptr<base::RefCountedMemory> data_; |
198 // The current offset into the data that we're handing off to our | 200 // The current offset into the data that we're handing off to our |
199 // callers via the Read interfaces. | 201 // callers via the Read interfaces. |
200 int data_offset_; | 202 int data_offset_; |
201 | 203 |
202 // For async reads, we keep around a pointer to the buffer that | 204 // For async reads, we keep around a pointer to the buffer that |
203 // we're reading into. | 205 // we're reading into. |
204 scoped_refptr<net::IOBuffer> pending_buf_; | 206 scoped_refptr<net::IOBuffer> pending_buf_; |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
329 } | 331 } |
330 | 332 |
331 void URLRequestChromeJob::MimeTypeAvailable(const std::string& mime_type) { | 333 void URLRequestChromeJob::MimeTypeAvailable(const std::string& mime_type) { |
332 set_mime_type(mime_type); | 334 set_mime_type(mime_type); |
333 NotifyHeadersComplete(); | 335 NotifyHeadersComplete(); |
334 } | 336 } |
335 | 337 |
336 void URLRequestChromeJob::DataAvailable(base::RefCountedMemory* bytes) { | 338 void URLRequestChromeJob::DataAvailable(base::RefCountedMemory* bytes) { |
337 TRACE_EVENT_ASYNC_END0("browser", "DataManager:Request", this); | 339 TRACE_EVENT_ASYNC_END0("browser", "DataManager:Request", this); |
338 if (bytes) { | 340 if (bytes) { |
339 // The request completed, and we have all the data. | |
340 // Clear any IO pending status. | |
341 SetStatus(net::URLRequestStatus()); | |
342 | |
343 data_ = bytes; | 341 data_ = bytes; |
344 int bytes_read; | |
345 if (pending_buf_.get()) { | 342 if (pending_buf_.get()) { |
346 CHECK(pending_buf_->data()); | 343 CHECK(pending_buf_->data()); |
347 CompleteRead(pending_buf_.get(), pending_buf_size_, &bytes_read); | 344 int result = CompleteRead(pending_buf_.get(), pending_buf_size_); |
348 pending_buf_ = NULL; | 345 pending_buf_ = NULL; |
349 NotifyReadComplete(bytes_read); | 346 ReadRawDataComplete(result); |
350 } | 347 } |
351 } else { | 348 } else { |
352 // The request failed. | 349 // The request failed. |
353 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED, | 350 ReadRawDataComplete(net::ERR_FAILED); |
354 net::ERR_FAILED)); | |
355 } | 351 } |
356 } | 352 } |
357 | 353 |
358 base::WeakPtr<URLRequestChromeJob> URLRequestChromeJob::AsWeakPtr() { | 354 base::WeakPtr<URLRequestChromeJob> URLRequestChromeJob::AsWeakPtr() { |
359 return weak_factory_.GetWeakPtr(); | 355 return weak_factory_.GetWeakPtr(); |
360 } | 356 } |
361 | 357 |
362 bool URLRequestChromeJob::ReadRawData(net::IOBuffer* buf, int buf_size, | 358 int URLRequestChromeJob::ReadRawData(net::IOBuffer* buf, int buf_size) { |
363 int* bytes_read) { | |
364 if (!data_.get()) { | 359 if (!data_.get()) { |
365 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); | |
366 DCHECK(!pending_buf_.get()); | 360 DCHECK(!pending_buf_.get()); |
367 CHECK(buf->data()); | 361 CHECK(buf->data()); |
368 pending_buf_ = buf; | 362 pending_buf_ = buf; |
369 pending_buf_size_ = buf_size; | 363 pending_buf_size_ = buf_size; |
370 return false; // Tell the caller we're still waiting for data. | 364 return net::ERR_IO_PENDING; |
371 } | 365 } |
372 | 366 |
373 // Otherwise, the data is available. | 367 // Otherwise, the data is available. |
374 CompleteRead(buf, buf_size, bytes_read); | 368 return CompleteRead(buf, buf_size); |
375 return true; | |
376 } | 369 } |
377 | 370 |
378 void URLRequestChromeJob::CompleteRead(net::IOBuffer* buf, int buf_size, | 371 int URLRequestChromeJob::CompleteRead(net::IOBuffer* buf, int buf_size) { |
379 int* bytes_read) { | 372 int remaining = data_->size() - data_offset_; |
380 int remaining = static_cast<int>(data_->size()) - data_offset_; | |
381 if (buf_size > remaining) | 373 if (buf_size > remaining) |
382 buf_size = remaining; | 374 buf_size = remaining; |
383 if (buf_size > 0) { | 375 if (buf_size > 0) { |
384 // TODO(pkasting): Remove ScopedTracker below once crbug.com/455423 is | 376 // TODO(pkasting): Remove ScopedTracker below once crbug.com/455423 is |
385 // fixed. | 377 // fixed. |
386 tracked_objects::ScopedTracker tracking_profile( | 378 tracked_objects::ScopedTracker tracking_profile( |
387 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 379 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
388 "455423 URLRequestChromeJob::CompleteRead memcpy")); | 380 "455423 URLRequestChromeJob::CompleteRead memcpy")); |
389 memcpy(buf->data(), data_->front() + data_offset_, buf_size); | 381 memcpy(buf->data(), data_->front() + data_offset_, buf_size); |
390 data_offset_ += buf_size; | 382 data_offset_ += buf_size; |
391 } | 383 } |
392 *bytes_read = buf_size; | 384 return buf_size; |
393 } | 385 } |
394 | 386 |
395 void URLRequestChromeJob::CheckStoragePartitionMatches( | 387 void URLRequestChromeJob::CheckStoragePartitionMatches( |
396 int render_process_id, | 388 int render_process_id, |
397 const GURL& url, | 389 const GURL& url, |
398 const base::WeakPtr<URLRequestChromeJob>& job) { | 390 const base::WeakPtr<URLRequestChromeJob>& job) { |
399 // The embedder could put some webui pages in separate storage partition. | 391 // The embedder could put some webui pages in separate storage partition. |
400 // RenderProcessHostImpl::IsSuitableHost would guard against top level pages | 392 // RenderProcessHostImpl::IsSuitableHost would guard against top level pages |
401 // being in the same process. We do an extra check to guard against an | 393 // being in the same process. We do an extra check to guard against an |
402 // exploited renderer pretending to add them as a subframe. We skip this check | 394 // exploited renderer pretending to add them as a subframe. We skip this check |
(...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
773 | 765 |
774 } // namespace | 766 } // namespace |
775 | 767 |
776 net::URLRequestJobFactory::ProtocolHandler* | 768 net::URLRequestJobFactory::ProtocolHandler* |
777 CreateDevToolsProtocolHandler(content::ResourceContext* resource_context, | 769 CreateDevToolsProtocolHandler(content::ResourceContext* resource_context, |
778 bool is_incognito) { | 770 bool is_incognito) { |
779 return new DevToolsJobFactory(resource_context, is_incognito); | 771 return new DevToolsJobFactory(resource_context, is_incognito); |
780 } | 772 } |
781 | 773 |
782 } // namespace content | 774 } // namespace content |
OLD | NEW |