Chromium Code Reviews| 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 "chrome/browser/ui/webui/chrome_url_data_manager_backend.h" | 5 #include "chrome/browser/ui/webui/chrome_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 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 170 virtual bool ReadRawData(net::IOBuffer* buf, | 170 virtual bool ReadRawData(net::IOBuffer* buf, |
| 171 int buf_size, | 171 int buf_size, |
| 172 int* bytes_read) OVERRIDE; | 172 int* bytes_read) OVERRIDE; |
| 173 virtual bool GetMimeType(std::string* mime_type) const OVERRIDE; | 173 virtual bool GetMimeType(std::string* mime_type) const OVERRIDE; |
| 174 virtual void GetResponseInfo(net::HttpResponseInfo* info) OVERRIDE; | 174 virtual void GetResponseInfo(net::HttpResponseInfo* info) OVERRIDE; |
| 175 | 175 |
| 176 // Called by ChromeURLDataManager to notify us that the data blob is ready | 176 // Called by ChromeURLDataManager to notify us that the data blob is ready |
| 177 // for us. | 177 // for us. |
| 178 void DataAvailable(base::RefCountedMemory* bytes); | 178 void DataAvailable(base::RefCountedMemory* bytes); |
| 179 | 179 |
| 180 void SetMimeType(const std::string& mime_type) { | 180 void SetMimeType(const std::string& mime_type) { |
|
James Hawkins
2012/05/21 15:58:50
nit: Per style-guide, these should be set_mime_typ
Patrick Dubroy
2012/05/21 16:26:28
Done.
| |
| 181 mime_type_ = mime_type; | 181 mime_type_ = mime_type; |
| 182 } | 182 } |
| 183 | 183 |
| 184 void SetAllowCaching(bool allow_caching) { | |
| 185 allow_caching_ = allow_caching; | |
| 186 } | |
| 187 | |
| 184 private: | 188 private: |
| 185 virtual ~URLRequestChromeJob(); | 189 virtual ~URLRequestChromeJob(); |
| 186 | 190 |
| 187 // Helper for Start(), to let us start asynchronously. | 191 // Helper for Start(), to let us start asynchronously. |
| 188 // (This pattern is shared by most net::URLRequestJob implementations.) | 192 // (This pattern is shared by most net::URLRequestJob implementations.) |
| 189 void StartAsync(); | 193 void StartAsync(); |
| 190 | 194 |
| 191 // Do the actual copy from data_ (the data we're serving) into |buf|. | 195 // Do the actual copy from data_ (the data we're serving) into |buf|. |
| 192 // Separate from ReadRawData so we can handle async I/O. | 196 // Separate from ReadRawData so we can handle async I/O. |
| 193 void CompleteRead(net::IOBuffer* buf, int buf_size, int* bytes_read); | 197 void CompleteRead(net::IOBuffer* buf, int buf_size, int* bytes_read); |
| 194 | 198 |
| 195 // The actual data we're serving. NULL until it's been fetched. | 199 // The actual data we're serving. NULL until it's been fetched. |
| 196 scoped_refptr<base::RefCountedMemory> data_; | 200 scoped_refptr<base::RefCountedMemory> data_; |
| 197 // The current offset into the data that we're handing off to our | 201 // The current offset into the data that we're handing off to our |
| 198 // callers via the Read interfaces. | 202 // callers via the Read interfaces. |
| 199 int data_offset_; | 203 int data_offset_; |
| 200 | 204 |
| 201 // For async reads, we keep around a pointer to the buffer that | 205 // For async reads, we keep around a pointer to the buffer that |
| 202 // we're reading into. | 206 // we're reading into. |
| 203 scoped_refptr<net::IOBuffer> pending_buf_; | 207 scoped_refptr<net::IOBuffer> pending_buf_; |
| 204 int pending_buf_size_; | 208 int pending_buf_size_; |
| 205 std::string mime_type_; | 209 std::string mime_type_; |
| 210 bool allow_caching_; | |
|
James Hawkins
2012/05/21 15:58:50
nit: Document member variable.
Patrick Dubroy
2012/05/21 16:26:28
Done.
| |
| 206 | 211 |
| 207 // The backend is owned by ChromeURLRequestContext and always outlives us. | 212 // The backend is owned by ChromeURLRequestContext and always outlives us. |
| 208 ChromeURLDataManagerBackend* backend_; | 213 ChromeURLDataManagerBackend* backend_; |
| 209 | 214 |
| 210 base::WeakPtrFactory<URLRequestChromeJob> weak_factory_; | 215 base::WeakPtrFactory<URLRequestChromeJob> weak_factory_; |
| 211 | 216 |
| 212 DISALLOW_COPY_AND_ASSIGN(URLRequestChromeJob); | 217 DISALLOW_COPY_AND_ASSIGN(URLRequestChromeJob); |
| 213 }; | 218 }; |
| 214 | 219 |
| 215 URLRequestChromeJob::URLRequestChromeJob(net::URLRequest* request, | 220 URLRequestChromeJob::URLRequestChromeJob(net::URLRequest* request, |
| 216 ChromeURLDataManagerBackend* backend) | 221 ChromeURLDataManagerBackend* backend) |
| 217 : net::URLRequestJob(request), | 222 : net::URLRequestJob(request), |
| 218 data_offset_(0), | 223 data_offset_(0), |
| 219 pending_buf_size_(0), | 224 pending_buf_size_(0), |
| 225 allow_caching_(true), | |
| 220 backend_(backend), | 226 backend_(backend), |
| 221 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 227 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 222 DCHECK(backend); | 228 DCHECK(backend); |
| 223 } | 229 } |
| 224 | 230 |
| 225 URLRequestChromeJob::~URLRequestChromeJob() { | 231 URLRequestChromeJob::~URLRequestChromeJob() { |
| 226 CHECK(!backend_->HasPendingJob(this)); | 232 CHECK(!backend_->HasPendingJob(this)); |
| 227 } | 233 } |
| 228 | 234 |
| 229 void URLRequestChromeJob::Start() { | 235 void URLRequestChromeJob::Start() { |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 247 return !mime_type_.empty(); | 253 return !mime_type_.empty(); |
| 248 } | 254 } |
| 249 | 255 |
| 250 void URLRequestChromeJob::GetResponseInfo(net::HttpResponseInfo* info) { | 256 void URLRequestChromeJob::GetResponseInfo(net::HttpResponseInfo* info) { |
| 251 DCHECK(!info->headers); | 257 DCHECK(!info->headers); |
| 252 // Set the headers so that requests serviced by ChromeURLDataManager return a | 258 // Set the headers so that requests serviced by ChromeURLDataManager return a |
| 253 // status code of 200. Without this they return a 0, which makes the status | 259 // status code of 200. Without this they return a 0, which makes the status |
| 254 // indistiguishable from other error types. Instant relies on getting a 200. | 260 // indistiguishable from other error types. Instant relies on getting a 200. |
| 255 info->headers = new net::HttpResponseHeaders("HTTP/1.1 200 OK"); | 261 info->headers = new net::HttpResponseHeaders("HTTP/1.1 200 OK"); |
| 256 AddContentSecurityPolicyHeader(request_->url(), info->headers); | 262 AddContentSecurityPolicyHeader(request_->url(), info->headers); |
| 263 if (!allow_caching_) | |
| 264 info->headers->AddHeader("Cache-Control: no-cache"); | |
| 257 } | 265 } |
| 258 | 266 |
| 259 void URLRequestChromeJob::DataAvailable(base::RefCountedMemory* bytes) { | 267 void URLRequestChromeJob::DataAvailable(base::RefCountedMemory* bytes) { |
| 260 TRACE_EVENT_ASYNC_END0("browser", "DataManager:Request", this); | 268 TRACE_EVENT_ASYNC_END0("browser", "DataManager:Request", this); |
| 261 if (bytes) { | 269 if (bytes) { |
| 262 // The request completed, and we have all the data. | 270 // The request completed, and we have all the data. |
| 263 // Clear any IO pending status. | 271 // Clear any IO pending status. |
| 264 SetStatus(net::URLRequestStatus()); | 272 SetStatus(net::URLRequestStatus()); |
| 265 | 273 |
| 266 data_ = bytes; | 274 data_ = bytes; |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 411 ChromeURLDataManager::DataSource* source = i->second; | 419 ChromeURLDataManager::DataSource* source = i->second; |
| 412 | 420 |
| 413 // Save this request so we know where to send the data. | 421 // Save this request so we know where to send the data. |
| 414 RequestID request_id = next_request_id_++; | 422 RequestID request_id = next_request_id_++; |
| 415 pending_requests_.insert(std::make_pair(request_id, job)); | 423 pending_requests_.insert(std::make_pair(request_id, job)); |
| 416 | 424 |
| 417 // TODO(eroman): would be nicer if the mimetype were set at the same time | 425 // TODO(eroman): would be nicer if the mimetype were set at the same time |
| 418 // as the data blob. For now do it here, since NotifyHeadersComplete() is | 426 // as the data blob. For now do it here, since NotifyHeadersComplete() is |
| 419 // going to get called once we return. | 427 // going to get called once we return. |
| 420 job->SetMimeType(source->GetMimeType(path)); | 428 job->SetMimeType(source->GetMimeType(path)); |
| 429 job->SetAllowCaching(source->AllowCaching()); | |
| 421 | 430 |
| 422 const ChromeURLRequestContext* context = | 431 const ChromeURLRequestContext* context = |
| 423 static_cast<const ChromeURLRequestContext*>(job->request()->context()); | 432 static_cast<const ChromeURLRequestContext*>(job->request()->context()); |
| 424 | 433 |
| 425 // Forward along the request to the data source. | 434 // Forward along the request to the data source. |
| 426 MessageLoop* target_message_loop = source->MessageLoopForRequestPath(path); | 435 MessageLoop* target_message_loop = source->MessageLoopForRequestPath(path); |
| 427 if (!target_message_loop) { | 436 if (!target_message_loop) { |
| 428 // The DataSource is agnostic to which thread StartDataRequest is called | 437 // The DataSource is agnostic to which thread StartDataRequest is called |
| 429 // on for this path. Call directly into it from this thread, the IO | 438 // on for this path. Call directly into it from this thread, the IO |
| 430 // thread. | 439 // thread. |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 555 | 564 |
| 556 return new URLRequestChromeJob(request, backend_); | 565 return new URLRequestChromeJob(request, backend_); |
| 557 } | 566 } |
| 558 | 567 |
| 559 } // namespace | 568 } // namespace |
| 560 | 569 |
| 561 net::URLRequestJobFactory::ProtocolHandler* | 570 net::URLRequestJobFactory::ProtocolHandler* |
| 562 CreateDevToolsProtocolHandler(ChromeURLDataManagerBackend* backend) { | 571 CreateDevToolsProtocolHandler(ChromeURLDataManagerBackend* backend) { |
| 563 return new DevToolsJobFactory(backend); | 572 return new DevToolsJobFactory(backend); |
| 564 } | 573 } |
| OLD | NEW |