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" |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
13 #include "base/debug/trace_event.h" | 13 #include "base/debug/trace_event.h" |
14 #include "base/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
16 #include "base/memory/ref_counted_memory.h" | 16 #include "base/memory/ref_counted_memory.h" |
17 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
18 #include "base/message_loop/message_loop.h" | 18 #include "base/message_loop/message_loop.h" |
19 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
20 #include "base/strings/stringprintf.h" | 20 #include "base/strings/stringprintf.h" |
21 #include "content/browser/fileapi/chrome_blob_storage_context.h" | 21 #include "content/browser/fileapi/chrome_blob_storage_context.h" |
22 #include "content/browser/histogram_internals_request_job.h" | 22 #include "content/browser/histogram_internals_request_job.h" |
23 #include "content/browser/net/view_blob_internals_job_factory.h" | 23 #include "content/browser/net/view_blob_internals_job_factory.h" |
24 #include "content/browser/net/view_http_cache_job_factory.h" | 24 #include "content/browser/net/view_http_cache_job_factory.h" |
25 #include "content/browser/resource_context_impl.h" | 25 #include "content/browser/resource_context_impl.h" |
26 #include "content/browser/tcmalloc_internals_request_job.h" | 26 #include "content/browser/tcmalloc_internals_request_job.h" |
27 #include "content/browser/webui/shared_resources_data_source.h" | 27 #include "content/browser/webui/shared_resources_data_source.h" |
28 #include "content/browser/webui/url_data_source_impl.h" | 28 #include "content/browser/webui/url_data_source_impl.h" |
| 29 #include "content/public/browser/browser_context.h" |
29 #include "content/public/browser/browser_thread.h" | 30 #include "content/public/browser/browser_thread.h" |
30 #include "content/public/browser/content_browser_client.h" | 31 #include "content/public/browser/content_browser_client.h" |
31 #include "content/public/browser/render_process_host.h" | 32 #include "content/public/browser/render_process_host.h" |
32 #include "content/public/browser/resource_request_info.h" | 33 #include "content/public/browser/resource_request_info.h" |
33 #include "content/public/common/url_constants.h" | 34 #include "content/public/common/url_constants.h" |
34 #include "net/base/io_buffer.h" | 35 #include "net/base/io_buffer.h" |
35 #include "net/base/net_errors.h" | 36 #include "net/base/net_errors.h" |
36 #include "net/http/http_response_headers.h" | 37 #include "net/http/http_response_headers.h" |
37 #include "net/http/http_status_code.h" | 38 #include "net/http/http_status_code.h" |
38 #include "net/url_request/url_request.h" | 39 #include "net/url_request/url_request.h" |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 // Returns true when job was generated from an incognito profile. | 156 // Returns true when job was generated from an incognito profile. |
156 bool is_incognito() const { | 157 bool is_incognito() const { |
157 return is_incognito_; | 158 return is_incognito_; |
158 } | 159 } |
159 | 160 |
160 private: | 161 private: |
161 virtual ~URLRequestChromeJob(); | 162 virtual ~URLRequestChromeJob(); |
162 | 163 |
163 // Helper for Start(), to let us start asynchronously. | 164 // Helper for Start(), to let us start asynchronously. |
164 // (This pattern is shared by most net::URLRequestJob implementations.) | 165 // (This pattern is shared by most net::URLRequestJob implementations.) |
165 void StartAsync(); | 166 void StartAsync(bool allowed); |
| 167 |
| 168 // Called on the UI thread to check if this request is allowed. |
| 169 static void CheckStoragePartitionMatches( |
| 170 int render_process_id, |
| 171 const GURL& url, |
| 172 const base::WeakPtr<URLRequestChromeJob>& job); |
166 | 173 |
167 // Do the actual copy from data_ (the data we're serving) into |buf|. | 174 // Do the actual copy from data_ (the data we're serving) into |buf|. |
168 // Separate from ReadRawData so we can handle async I/O. | 175 // Separate from ReadRawData so we can handle async I/O. |
169 void CompleteRead(net::IOBuffer* buf, int buf_size, int* bytes_read); | 176 void CompleteRead(net::IOBuffer* buf, int buf_size, int* bytes_read); |
170 | 177 |
171 // The actual data we're serving. NULL until it's been fetched. | 178 // The actual data we're serving. NULL until it's been fetched. |
172 scoped_refptr<base::RefCountedMemory> data_; | 179 scoped_refptr<base::RefCountedMemory> data_; |
173 // The current offset into the data that we're handing off to our | 180 // The current offset into the data that we're handing off to our |
174 // callers via the Read interfaces. | 181 // callers via the Read interfaces. |
175 int data_offset_; | 182 int data_offset_; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 backend_(backend), | 231 backend_(backend), |
225 weak_factory_(this) { | 232 weak_factory_(this) { |
226 DCHECK(backend); | 233 DCHECK(backend); |
227 } | 234 } |
228 | 235 |
229 URLRequestChromeJob::~URLRequestChromeJob() { | 236 URLRequestChromeJob::~URLRequestChromeJob() { |
230 CHECK(!backend_->HasPendingJob(this)); | 237 CHECK(!backend_->HasPendingJob(this)); |
231 } | 238 } |
232 | 239 |
233 void URLRequestChromeJob::Start() { | 240 void URLRequestChromeJob::Start() { |
234 // Start reading asynchronously so that all error reporting and data | 241 int render_process_id, unused; |
235 // callbacks happen as they would for network requests. | 242 ResourceRequestInfo::GetRenderFrameForRequest( |
236 base::MessageLoop::current()->PostTask( | 243 request_, &render_process_id, &unused); |
| 244 BrowserThread::PostTask( |
| 245 BrowserThread::UI, |
237 FROM_HERE, | 246 FROM_HERE, |
238 base::Bind(&URLRequestChromeJob::StartAsync, weak_factory_.GetWeakPtr())); | 247 base::Bind(&URLRequestChromeJob::CheckStoragePartitionMatches, |
239 | 248 render_process_id, request_->url(), AsWeakPtr())); |
240 TRACE_EVENT_ASYNC_BEGIN1("browser", "DataManager:Request", this, "URL", | 249 TRACE_EVENT_ASYNC_BEGIN1("browser", "DataManager:Request", this, "URL", |
241 request_->url().possibly_invalid_spec()); | 250 request_->url().possibly_invalid_spec()); |
242 } | 251 } |
243 | 252 |
244 void URLRequestChromeJob::Kill() { | 253 void URLRequestChromeJob::Kill() { |
245 backend_->RemoveRequest(this); | 254 backend_->RemoveRequest(this); |
246 } | 255 } |
247 | 256 |
248 bool URLRequestChromeJob::GetMimeType(std::string* mime_type) const { | 257 bool URLRequestChromeJob::GetMimeType(std::string* mime_type) const { |
249 *mime_type = mime_type_; | 258 *mime_type = mime_type_; |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
333 int remaining = static_cast<int>(data_->size()) - data_offset_; | 342 int remaining = static_cast<int>(data_->size()) - data_offset_; |
334 if (buf_size > remaining) | 343 if (buf_size > remaining) |
335 buf_size = remaining; | 344 buf_size = remaining; |
336 if (buf_size > 0) { | 345 if (buf_size > 0) { |
337 memcpy(buf->data(), data_->front() + data_offset_, buf_size); | 346 memcpy(buf->data(), data_->front() + data_offset_, buf_size); |
338 data_offset_ += buf_size; | 347 data_offset_ += buf_size; |
339 } | 348 } |
340 *bytes_read = buf_size; | 349 *bytes_read = buf_size; |
341 } | 350 } |
342 | 351 |
343 void URLRequestChromeJob::StartAsync() { | 352 void URLRequestChromeJob::CheckStoragePartitionMatches( |
| 353 int render_process_id, |
| 354 const GURL& url, |
| 355 const base::WeakPtr<URLRequestChromeJob>& job) { |
| 356 // The embedder could put some webui pages in separate storage partition. |
| 357 // RenderProcessHostImpl::IsSuitableHost would guard against top level pages |
| 358 // being in the same process. We do an extra check to guard against an |
| 359 // exploited renderer pretending to add them as a subframe. |
| 360 bool allowed = false; |
| 361 RenderProcessHost* process = RenderProcessHost::FromID(render_process_id); |
| 362 if (process) { |
| 363 StoragePartition* partition = BrowserContext::GetStoragePartitionForSite( |
| 364 process->GetBrowserContext(), url); |
| 365 allowed = partition == process->GetStoragePartition(); |
| 366 } |
| 367 |
| 368 BrowserThread::PostTask( |
| 369 BrowserThread::IO, |
| 370 FROM_HERE, |
| 371 base::Bind(&URLRequestChromeJob::StartAsync, job, allowed)); |
| 372 } |
| 373 |
| 374 void URLRequestChromeJob::StartAsync(bool allowed) { |
344 if (!request_) | 375 if (!request_) |
345 return; | 376 return; |
346 | 377 |
347 if (!backend_->StartRequest(request_, this)) { | 378 if (!allowed || !backend_->StartRequest(request_, this)) { |
348 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, | 379 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
349 net::ERR_INVALID_URL)); | 380 net::ERR_INVALID_URL)); |
350 } | 381 } |
351 } | 382 } |
352 | 383 |
353 namespace { | 384 namespace { |
354 | 385 |
355 // Gets mime type for data that is available from |source| by |path|. | 386 // Gets mime type for data that is available from |source| by |path|. |
356 // After that, notifies |job| that mime type is available. This method | 387 // After that, notifies |job| that mime type is available. This method |
357 // should be called on the UI thread, but notification is performed on | 388 // should be called on the UI thread, but notification is performed on |
(...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
664 | 695 |
665 } // namespace | 696 } // namespace |
666 | 697 |
667 net::URLRequestJobFactory::ProtocolHandler* | 698 net::URLRequestJobFactory::ProtocolHandler* |
668 CreateDevToolsProtocolHandler(content::ResourceContext* resource_context, | 699 CreateDevToolsProtocolHandler(content::ResourceContext* resource_context, |
669 bool is_incognito) { | 700 bool is_incognito) { |
670 return new DevToolsJobFactory(resource_context, is_incognito); | 701 return new DevToolsJobFactory(resource_context, is_incognito); |
671 } | 702 } |
672 | 703 |
673 } // namespace content | 704 } // namespace content |
OLD | NEW |