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" | |
30 #include "content/public/browser/browser_thread.h" | 29 #include "content/public/browser/browser_thread.h" |
31 #include "content/public/browser/content_browser_client.h" | 30 #include "content/public/browser/content_browser_client.h" |
32 #include "content/public/browser/render_process_host.h" | 31 #include "content/public/browser/render_process_host.h" |
33 #include "content/public/browser/resource_request_info.h" | 32 #include "content/public/browser/resource_request_info.h" |
34 #include "content/public/common/url_constants.h" | 33 #include "content/public/common/url_constants.h" |
35 #include "net/base/io_buffer.h" | 34 #include "net/base/io_buffer.h" |
36 #include "net/base/net_errors.h" | 35 #include "net/base/net_errors.h" |
37 #include "net/http/http_response_headers.h" | 36 #include "net/http/http_response_headers.h" |
38 #include "net/http/http_status_code.h" | 37 #include "net/http/http_status_code.h" |
39 #include "net/url_request/url_request.h" | 38 #include "net/url_request/url_request.h" |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 // Returns true when job was generated from an incognito profile. | 154 // Returns true when job was generated from an incognito profile. |
156 bool is_incognito() const { | 155 bool is_incognito() const { |
157 return is_incognito_; | 156 return is_incognito_; |
158 } | 157 } |
159 | 158 |
160 private: | 159 private: |
161 virtual ~URLRequestChromeJob(); | 160 virtual ~URLRequestChromeJob(); |
162 | 161 |
163 // Helper for Start(), to let us start asynchronously. | 162 // Helper for Start(), to let us start asynchronously. |
164 // (This pattern is shared by most net::URLRequestJob implementations.) | 163 // (This pattern is shared by most net::URLRequestJob implementations.) |
165 void StartAsync(bool allowed); | 164 void StartAsync(); |
166 | |
167 // Called on the UI thread to check if this request is allowed. | |
168 static void CheckStoragePartitionMatches( | |
169 int render_process_id, | |
170 const GURL& url, | |
171 const base::WeakPtr<URLRequestChromeJob>& job); | |
172 | 165 |
173 // Do the actual copy from data_ (the data we're serving) into |buf|. | 166 // Do the actual copy from data_ (the data we're serving) into |buf|. |
174 // Separate from ReadRawData so we can handle async I/O. | 167 // Separate from ReadRawData so we can handle async I/O. |
175 void CompleteRead(net::IOBuffer* buf, int buf_size, int* bytes_read); | 168 void CompleteRead(net::IOBuffer* buf, int buf_size, int* bytes_read); |
176 | 169 |
177 // The actual data we're serving. NULL until it's been fetched. | 170 // The actual data we're serving. NULL until it's been fetched. |
178 scoped_refptr<base::RefCountedMemory> data_; | 171 scoped_refptr<base::RefCountedMemory> data_; |
179 // The current offset into the data that we're handing off to our | 172 // The current offset into the data that we're handing off to our |
180 // callers via the Read interfaces. | 173 // callers via the Read interfaces. |
181 int data_offset_; | 174 int data_offset_; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 backend_(backend), | 223 backend_(backend), |
231 weak_factory_(this) { | 224 weak_factory_(this) { |
232 DCHECK(backend); | 225 DCHECK(backend); |
233 } | 226 } |
234 | 227 |
235 URLRequestChromeJob::~URLRequestChromeJob() { | 228 URLRequestChromeJob::~URLRequestChromeJob() { |
236 CHECK(!backend_->HasPendingJob(this)); | 229 CHECK(!backend_->HasPendingJob(this)); |
237 } | 230 } |
238 | 231 |
239 void URLRequestChromeJob::Start() { | 232 void URLRequestChromeJob::Start() { |
240 int render_process_id, unused; | 233 // Start reading asynchronously so that all error reporting and data |
241 ResourceRequestInfo::GetRenderFrameForRequest( | 234 // callbacks happen as they would for network requests. |
242 request_, &render_process_id, &unused); | 235 base::MessageLoop::current()->PostTask( |
243 BrowserThread::PostTask( | |
244 BrowserThread::UI, | |
245 FROM_HERE, | 236 FROM_HERE, |
246 base::Bind(&URLRequestChromeJob::CheckStoragePartitionMatches, | 237 base::Bind(&URLRequestChromeJob::StartAsync, weak_factory_.GetWeakPtr())); |
247 render_process_id, request_->url(), AsWeakPtr())); | 238 |
248 TRACE_EVENT_ASYNC_BEGIN1("browser", "DataManager:Request", this, "URL", | 239 TRACE_EVENT_ASYNC_BEGIN1("browser", "DataManager:Request", this, "URL", |
249 request_->url().possibly_invalid_spec()); | 240 request_->url().possibly_invalid_spec()); |
250 } | 241 } |
251 | 242 |
252 void URLRequestChromeJob::Kill() { | 243 void URLRequestChromeJob::Kill() { |
253 backend_->RemoveRequest(this); | 244 backend_->RemoveRequest(this); |
254 } | 245 } |
255 | 246 |
256 bool URLRequestChromeJob::GetMimeType(std::string* mime_type) const { | 247 bool URLRequestChromeJob::GetMimeType(std::string* mime_type) const { |
257 *mime_type = mime_type_; | 248 *mime_type = mime_type_; |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
341 int remaining = static_cast<int>(data_->size()) - data_offset_; | 332 int remaining = static_cast<int>(data_->size()) - data_offset_; |
342 if (buf_size > remaining) | 333 if (buf_size > remaining) |
343 buf_size = remaining; | 334 buf_size = remaining; |
344 if (buf_size > 0) { | 335 if (buf_size > 0) { |
345 memcpy(buf->data(), data_->front() + data_offset_, buf_size); | 336 memcpy(buf->data(), data_->front() + data_offset_, buf_size); |
346 data_offset_ += buf_size; | 337 data_offset_ += buf_size; |
347 } | 338 } |
348 *bytes_read = buf_size; | 339 *bytes_read = buf_size; |
349 } | 340 } |
350 | 341 |
351 void URLRequestChromeJob::CheckStoragePartitionMatches( | 342 void URLRequestChromeJob::StartAsync() { |
352 int render_process_id, | |
353 const GURL& url, | |
354 const base::WeakPtr<URLRequestChromeJob>& job) { | |
355 // The embedder could put some webui pages in separate storage partition. | |
356 // RenderProcessHostImpl::IsSuitableHost would guard against top level pages | |
357 // being in the same process. We do an extra check to guard against an | |
358 // exploited renderer pretending to add them as a subframe. We skip this check | |
359 // for resources. | |
360 bool allowed = false; | |
361 if (url.SchemeIs(kChromeUIScheme) && url.host() == kChromeUIResourcesHost) { | |
362 allowed = true; | |
363 } else { | |
364 RenderProcessHost* process = RenderProcessHost::FromID(render_process_id); | |
365 if (process) { | |
366 StoragePartition* partition = BrowserContext::GetStoragePartitionForSite( | |
367 process->GetBrowserContext(), url); | |
368 allowed = partition == process->GetStoragePartition(); | |
369 } | |
370 } | |
371 | |
372 BrowserThread::PostTask( | |
373 BrowserThread::IO, | |
374 FROM_HERE, | |
375 base::Bind(&URLRequestChromeJob::StartAsync, job, allowed)); | |
376 } | |
377 | |
378 void URLRequestChromeJob::StartAsync(bool allowed) { | |
379 if (!request_) | 343 if (!request_) |
380 return; | 344 return; |
381 | 345 |
382 if (!allowed || !backend_->StartRequest(request_, this)) { | 346 if (!backend_->StartRequest(request_, this)) { |
383 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, | 347 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
384 net::ERR_INVALID_URL)); | 348 net::ERR_INVALID_URL)); |
385 } | 349 } |
386 } | 350 } |
387 | 351 |
388 namespace { | 352 namespace { |
389 | 353 |
390 // Gets mime type for data that is available from |source| by |path|. | 354 // Gets mime type for data that is available from |source| by |path|. |
391 // After that, notifies |job| that mime type is available. This method | 355 // After that, notifies |job| that mime type is available. This method |
392 // should be called on the UI thread, but notification is performed on | 356 // should be called on the UI thread, but notification is performed on |
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
715 | 679 |
716 } // namespace | 680 } // namespace |
717 | 681 |
718 net::URLRequestJobFactory::ProtocolHandler* | 682 net::URLRequestJobFactory::ProtocolHandler* |
719 CreateDevToolsProtocolHandler(content::ResourceContext* resource_context, | 683 CreateDevToolsProtocolHandler(content::ResourceContext* resource_context, |
720 bool is_incognito) { | 684 bool is_incognito) { |
721 return new DevToolsJobFactory(resource_context, is_incognito); | 685 return new DevToolsJobFactory(resource_context, is_incognito); |
722 } | 686 } |
723 | 687 |
724 } // namespace content | 688 } // namespace content |
OLD | NEW |