Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(65)

Side by Side Diff: content/browser/loader/mime_type_resource_handler.cc

Issue 1839473002: Centralize the setting of Accept headers (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address mmenke's comments Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/loader/mime_type_resource_handler.h" 5 #include "content/browser/loader/mime_type_resource_handler.h"
6 6
7 #include <utility> 7 #include <utility>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 17 matching lines...) Expand all
28 #include "content/public/browser/resource_context.h" 28 #include "content/public/browser/resource_context.h"
29 #include "content/public/browser/resource_dispatcher_host_delegate.h" 29 #include "content/public/browser/resource_dispatcher_host_delegate.h"
30 #include "content/public/common/resource_response.h" 30 #include "content/public/common/resource_response.h"
31 #include "content/public/common/webplugininfo.h" 31 #include "content/public/common/webplugininfo.h"
32 #include "net/base/io_buffer.h" 32 #include "net/base/io_buffer.h"
33 #include "net/base/mime_sniffer.h" 33 #include "net/base/mime_sniffer.h"
34 #include "net/base/mime_util.h" 34 #include "net/base/mime_util.h"
35 #include "net/base/net_errors.h" 35 #include "net/base/net_errors.h"
36 #include "net/http/http_content_disposition.h" 36 #include "net/http/http_content_disposition.h"
37 #include "net/http/http_response_headers.h" 37 #include "net/http/http_response_headers.h"
38 #include "net/url_request/url_request.h"
38 39
39 namespace content { 40 namespace content {
40 41
41 namespace { 42 namespace {
42 43
44 const char kAcceptHeader[] = "Accept";
45 const char kFrameAcceptHeader[] =
46 "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,"
47 "*/*;q=0.8";
48 const char kStylesheetAcceptHeader[] = "text/css,*/*;q=0.1";
49 const char kImageAcceptHeader[] = "image/webp,image/*,*/*;q=0.8";
50 const char kDefaultAcceptHeader[] = "*/*";
51
43 // Used to write into an existing IOBuffer at a given offset. 52 // Used to write into an existing IOBuffer at a given offset.
44 class DependentIOBuffer : public net::WrappedIOBuffer { 53 class DependentIOBuffer : public net::WrappedIOBuffer {
45 public: 54 public:
46 DependentIOBuffer(net::IOBuffer* buf, int offset) 55 DependentIOBuffer(net::IOBuffer* buf, int offset)
47 : net::WrappedIOBuffer(buf->data() + offset), 56 : net::WrappedIOBuffer(buf->data() + offset),
48 buf_(buf) { 57 buf_(buf) {
49 } 58 }
50 59
51 private: 60 private:
52 ~DependentIOBuffer() override {} 61 ~DependentIOBuffer() override {}
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 if (response_->head.mime_type == "application/rss+xml" || 118 if (response_->head.mime_type == "application/rss+xml" ||
110 response_->head.mime_type == "application/atom+xml") { 119 response_->head.mime_type == "application/atom+xml") {
111 response_->head.mime_type.assign("text/plain"); 120 response_->head.mime_type.assign("text/plain");
112 } 121 }
113 } 122 }
114 123
115 state_ = STATE_PROCESSING; 124 state_ = STATE_PROCESSING;
116 return ProcessResponse(defer); 125 return ProcessResponse(defer);
117 } 126 }
118 127
128 bool MimeTypeResourceHandler::OnWillStart(const GURL& url, bool* defer) {
129 const char* accept_value = nullptr;
130 switch (GetRequestInfo()->GetResourceType()) {
131 case RESOURCE_TYPE_MAIN_FRAME:
132 case RESOURCE_TYPE_SUB_FRAME:
133 accept_value = kFrameAcceptHeader;
134 break;
135 case RESOURCE_TYPE_STYLESHEET:
136 accept_value = kStylesheetAcceptHeader;
137 break;
138 case RESOURCE_TYPE_IMAGE:
139 accept_value = kImageAcceptHeader;
140 break;
141 case RESOURCE_TYPE_SCRIPT:
142 case RESOURCE_TYPE_FONT_RESOURCE:
143 case RESOURCE_TYPE_SUB_RESOURCE:
144 case RESOURCE_TYPE_OBJECT:
145 case RESOURCE_TYPE_MEDIA:
146 case RESOURCE_TYPE_WORKER:
147 case RESOURCE_TYPE_SHARED_WORKER:
148 case RESOURCE_TYPE_PREFETCH:
149 case RESOURCE_TYPE_FAVICON:
150 case RESOURCE_TYPE_XHR:
151 case RESOURCE_TYPE_PING:
152 case RESOURCE_TYPE_SERVICE_WORKER:
153 case RESOURCE_TYPE_CSP_REPORT:
154 case RESOURCE_TYPE_PLUGIN_RESOURCE:
155 accept_value = kDefaultAcceptHeader;
156 break;
157 case RESOURCE_TYPE_LAST_TYPE:
158 NOTREACHED();
159 break;
160 }
Yoav Weiss 2016/04/01 17:54:10 Could we add a test here that makes sure that the
Nate Chapin 2016/04/01 19:01:24 Done.
161
162 // The false parameter prevents overwriting an existing accept header value,
163 // which we need because JS can manually set an accept header on an XHR.
mmenke 2016/04/01 15:29:08 nit: Avoid "we" in heaers
mmenke 2016/04/01 17:57:00 Err..."heaers" -> comments (Don't ask me how that
Nate Chapin 2016/04/01 19:01:24 I figured it out :) Done.
164 request()->SetExtraRequestHeaderByName(kAcceptHeader, accept_value, false);
165 return next_handler_->OnWillStart(url, defer);
166 }
167
119 bool MimeTypeResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, 168 bool MimeTypeResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf,
120 int* buf_size, 169 int* buf_size,
121 int min_size) { 170 int min_size) {
122 if (state_ == STATE_STREAMING) 171 if (state_ == STATE_STREAMING)
123 return next_handler_->OnWillRead(buf, buf_size, min_size); 172 return next_handler_->OnWillRead(buf, buf_size, min_size);
124 173
125 DCHECK_EQ(-1, min_size); 174 DCHECK_EQ(-1, min_size);
126 175
127 if (read_buffer_.get()) { 176 if (read_buffer_.get()) {
128 CHECK_LT(bytes_read_, read_buffer_size_); 177 CHECK_LT(bytes_read_, read_buffer_size_);
(...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after
484 request()->LogUnblocked(); 533 request()->LogUnblocked();
485 bool defer = false; 534 bool defer = false;
486 if (!ProcessResponse(&defer)) { 535 if (!ProcessResponse(&defer)) {
487 controller()->Cancel(); 536 controller()->Cancel();
488 } else if (!defer) { 537 } else if (!defer) {
489 controller()->Resume(); 538 controller()->Resume();
490 } 539 }
491 } 540 }
492 541
493 } // namespace content 542 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698