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 set_mime_type(const std::string& mime_type) { |
181 mime_type_ = mime_type; | 181 mime_type_ = mime_type; |
182 } | 182 } |
183 | 183 |
| 184 void set_allow_caching(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_; |
206 | 210 |
| 211 // If true, set a header in the response to prevent it from being cached. |
| 212 bool allow_caching_; |
| 213 |
207 // The backend is owned by ChromeURLRequestContext and always outlives us. | 214 // The backend is owned by ChromeURLRequestContext and always outlives us. |
208 ChromeURLDataManagerBackend* backend_; | 215 ChromeURLDataManagerBackend* backend_; |
209 | 216 |
210 base::WeakPtrFactory<URLRequestChromeJob> weak_factory_; | 217 base::WeakPtrFactory<URLRequestChromeJob> weak_factory_; |
211 | 218 |
212 DISALLOW_COPY_AND_ASSIGN(URLRequestChromeJob); | 219 DISALLOW_COPY_AND_ASSIGN(URLRequestChromeJob); |
213 }; | 220 }; |
214 | 221 |
215 URLRequestChromeJob::URLRequestChromeJob(net::URLRequest* request, | 222 URLRequestChromeJob::URLRequestChromeJob(net::URLRequest* request, |
216 ChromeURLDataManagerBackend* backend) | 223 ChromeURLDataManagerBackend* backend) |
217 : net::URLRequestJob(request), | 224 : net::URLRequestJob(request), |
218 data_offset_(0), | 225 data_offset_(0), |
219 pending_buf_size_(0), | 226 pending_buf_size_(0), |
| 227 allow_caching_(true), |
220 backend_(backend), | 228 backend_(backend), |
221 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 229 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
222 DCHECK(backend); | 230 DCHECK(backend); |
223 } | 231 } |
224 | 232 |
225 URLRequestChromeJob::~URLRequestChromeJob() { | 233 URLRequestChromeJob::~URLRequestChromeJob() { |
226 CHECK(!backend_->HasPendingJob(this)); | 234 CHECK(!backend_->HasPendingJob(this)); |
227 } | 235 } |
228 | 236 |
229 void URLRequestChromeJob::Start() { | 237 void URLRequestChromeJob::Start() { |
(...skipping 17 matching lines...) Expand all Loading... |
247 return !mime_type_.empty(); | 255 return !mime_type_.empty(); |
248 } | 256 } |
249 | 257 |
250 void URLRequestChromeJob::GetResponseInfo(net::HttpResponseInfo* info) { | 258 void URLRequestChromeJob::GetResponseInfo(net::HttpResponseInfo* info) { |
251 DCHECK(!info->headers); | 259 DCHECK(!info->headers); |
252 // Set the headers so that requests serviced by ChromeURLDataManager return a | 260 // 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 | 261 // 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. | 262 // indistiguishable from other error types. Instant relies on getting a 200. |
255 info->headers = new net::HttpResponseHeaders("HTTP/1.1 200 OK"); | 263 info->headers = new net::HttpResponseHeaders("HTTP/1.1 200 OK"); |
256 AddContentSecurityPolicyHeader(request_->url(), info->headers); | 264 AddContentSecurityPolicyHeader(request_->url(), info->headers); |
| 265 if (!allow_caching_) |
| 266 info->headers->AddHeader("Cache-Control: no-cache"); |
257 } | 267 } |
258 | 268 |
259 void URLRequestChromeJob::DataAvailable(base::RefCountedMemory* bytes) { | 269 void URLRequestChromeJob::DataAvailable(base::RefCountedMemory* bytes) { |
260 TRACE_EVENT_ASYNC_END0("browser", "DataManager:Request", this); | 270 TRACE_EVENT_ASYNC_END0("browser", "DataManager:Request", this); |
261 if (bytes) { | 271 if (bytes) { |
262 // The request completed, and we have all the data. | 272 // The request completed, and we have all the data. |
263 // Clear any IO pending status. | 273 // Clear any IO pending status. |
264 SetStatus(net::URLRequestStatus()); | 274 SetStatus(net::URLRequestStatus()); |
265 | 275 |
266 data_ = bytes; | 276 data_ = bytes; |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 | 420 |
411 ChromeURLDataManager::DataSource* source = i->second; | 421 ChromeURLDataManager::DataSource* source = i->second; |
412 | 422 |
413 // Save this request so we know where to send the data. | 423 // Save this request so we know where to send the data. |
414 RequestID request_id = next_request_id_++; | 424 RequestID request_id = next_request_id_++; |
415 pending_requests_.insert(std::make_pair(request_id, job)); | 425 pending_requests_.insert(std::make_pair(request_id, job)); |
416 | 426 |
417 // TODO(eroman): would be nicer if the mimetype were set at the same time | 427 // 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 | 428 // as the data blob. For now do it here, since NotifyHeadersComplete() is |
419 // going to get called once we return. | 429 // going to get called once we return. |
420 job->SetMimeType(source->GetMimeType(path)); | 430 job->set_mime_type(source->GetMimeType(path)); |
| 431 job->set_allow_caching(source->AllowCaching()); |
421 | 432 |
422 const ChromeURLRequestContext* context = | 433 const ChromeURLRequestContext* context = |
423 static_cast<const ChromeURLRequestContext*>(job->request()->context()); | 434 static_cast<const ChromeURLRequestContext*>(job->request()->context()); |
424 | 435 |
425 // Forward along the request to the data source. | 436 // Forward along the request to the data source. |
426 MessageLoop* target_message_loop = source->MessageLoopForRequestPath(path); | 437 MessageLoop* target_message_loop = source->MessageLoopForRequestPath(path); |
427 if (!target_message_loop) { | 438 if (!target_message_loop) { |
428 // The DataSource is agnostic to which thread StartDataRequest is called | 439 // The DataSource is agnostic to which thread StartDataRequest is called |
429 // on for this path. Call directly into it from this thread, the IO | 440 // on for this path. Call directly into it from this thread, the IO |
430 // thread. | 441 // thread. |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
555 | 566 |
556 return new URLRequestChromeJob(request, backend_); | 567 return new URLRequestChromeJob(request, backend_); |
557 } | 568 } |
558 | 569 |
559 } // namespace | 570 } // namespace |
560 | 571 |
561 net::URLRequestJobFactory::ProtocolHandler* | 572 net::URLRequestJobFactory::ProtocolHandler* |
562 CreateDevToolsProtocolHandler(ChromeURLDataManagerBackend* backend) { | 573 CreateDevToolsProtocolHandler(ChromeURLDataManagerBackend* backend) { |
563 return new DevToolsJobFactory(backend); | 574 return new DevToolsJobFactory(backend); |
564 } | 575 } |
OLD | NEW |