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

Side by Side Diff: content/browser/webui/url_data_manager_backend.cc

Issue 2544683002: [MD settings] i18n source stream filtering (Closed)
Patch Set: review changes; merge with master Created 4 years 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/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 #include <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 13 matching lines...) Expand all
24 #include "base/strings/string_util.h" 24 #include "base/strings/string_util.h"
25 #include "base/strings/stringprintf.h" 25 #include "base/strings/stringprintf.h"
26 #include "base/threading/thread_task_runner_handle.h" 26 #include "base/threading/thread_task_runner_handle.h"
27 #include "base/threading/worker_pool.h" 27 #include "base/threading/worker_pool.h"
28 #include "base/trace_event/trace_event.h" 28 #include "base/trace_event/trace_event.h"
29 #include "content/browser/blob_storage/chrome_blob_storage_context.h" 29 #include "content/browser/blob_storage/chrome_blob_storage_context.h"
30 #include "content/browser/histogram_internals_request_job.h" 30 #include "content/browser/histogram_internals_request_job.h"
31 #include "content/browser/net/view_blob_internals_job_factory.h" 31 #include "content/browser/net/view_blob_internals_job_factory.h"
32 #include "content/browser/net/view_http_cache_job_factory.h" 32 #include "content/browser/net/view_http_cache_job_factory.h"
33 #include "content/browser/resource_context_impl.h" 33 #include "content/browser/resource_context_impl.h"
34 #include "content/browser/webui/i18n_source_stream.h"
34 #include "content/browser/webui/shared_resources_data_source.h" 35 #include "content/browser/webui/shared_resources_data_source.h"
35 #include "content/browser/webui/url_data_source_impl.h" 36 #include "content/browser/webui/url_data_source_impl.h"
36 #include "content/browser/webui/web_ui_data_source_impl.h" 37 #include "content/browser/webui/web_ui_data_source_impl.h"
37 #include "content/public/browser/browser_context.h" 38 #include "content/public/browser/browser_context.h"
38 #include "content/public/browser/browser_thread.h" 39 #include "content/public/browser/browser_thread.h"
39 #include "content/public/browser/content_browser_client.h" 40 #include "content/public/browser/content_browser_client.h"
40 #include "content/public/browser/render_process_host.h" 41 #include "content/public/browser/render_process_host.h"
41 #include "content/public/browser/resource_request_info.h" 42 #include "content/public/browser/resource_request_info.h"
42 #include "content/public/common/url_constants.h" 43 #include "content/public/common/url_constants.h"
43 #include "net/base/io_buffer.h" 44 #include "net/base/io_buffer.h"
44 #include "net/base/net_errors.h" 45 #include "net/base/net_errors.h"
45 #include "net/filter/gzip_source_stream.h" 46 #include "net/filter/gzip_source_stream.h"
46 #include "net/filter/source_stream.h" 47 #include "net/filter/source_stream.h"
47 #include "net/http/http_response_headers.h" 48 #include "net/http/http_response_headers.h"
48 #include "net/http/http_status_code.h" 49 #include "net/http/http_status_code.h"
49 #include "net/log/net_log_util.h" 50 #include "net/log/net_log_util.h"
50 #include "net/url_request/url_request.h" 51 #include "net/url_request/url_request.h"
51 #include "net/url_request/url_request_context.h" 52 #include "net/url_request/url_request_context.h"
52 #include "net/url_request/url_request_error_job.h" 53 #include "net/url_request/url_request_error_job.h"
53 #include "net/url_request/url_request_job.h" 54 #include "net/url_request/url_request_job.h"
54 #include "net/url_request/url_request_job_factory.h" 55 #include "net/url_request/url_request_job_factory.h"
56 #include "ui/base/template_expressions.h"
55 #include "url/url_util.h" 57 #include "url/url_util.h"
56 58
57 namespace content { 59 namespace content {
58 60
59 namespace { 61 namespace {
60 62
61 const char kChromeURLContentSecurityPolicyHeaderBase[] = 63 const char kChromeURLContentSecurityPolicyHeaderBase[] =
62 "Content-Security-Policy: "; 64 "Content-Security-Policy: ";
63 65
64 const char kChromeURLXFrameOptionsHeader[] = "X-Frame-Options: DENY"; 66 const char kChromeURLXFrameOptionsHeader[] = "X-Frame-Options: DENY";
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 } 205 }
204 206
205 void set_access_control_allow_origin(const std::string& value) { 207 void set_access_control_allow_origin(const std::string& value) {
206 access_control_allow_origin_ = value; 208 access_control_allow_origin_ = value;
207 } 209 }
208 210
209 void set_is_gzipped(bool is_gzipped) { 211 void set_is_gzipped(bool is_gzipped) {
210 is_gzipped_ = is_gzipped; 212 is_gzipped_ = is_gzipped;
211 } 213 }
212 214
215 void SetReplacements(const ui::TemplateReplacements* replacements) {
216 replacements_ = replacements;
217 }
218
213 // Returns true when job was generated from an incognito profile. 219 // Returns true when job was generated from an incognito profile.
214 bool is_incognito() const { 220 bool is_incognito() const {
215 return is_incognito_; 221 return is_incognito_;
216 } 222 }
217 223
218 private: 224 private:
219 ~URLRequestChromeJob() override; 225 ~URLRequestChromeJob() override;
220 226
221 // Helper for Start(), to let us start asynchronously. 227 // Helper for Start(), to let us start asynchronously.
222 // (This pattern is shared by most net::URLRequestJob implementations.) 228 // (This pattern is shared by most net::URLRequestJob implementations.)
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 // string. 279 // string.
274 std::string access_control_allow_origin_; 280 std::string access_control_allow_origin_;
275 281
276 // True when job is generated from an incognito profile. 282 // True when job is generated from an incognito profile.
277 const bool is_incognito_; 283 const bool is_incognito_;
278 284
279 // True when gzip encoding should be used. NOTE: this requires the original 285 // True when gzip encoding should be used. NOTE: this requires the original
280 // resources in resources.pak use compress="gzip". 286 // resources in resources.pak use compress="gzip".
281 bool is_gzipped_; 287 bool is_gzipped_;
282 288
289 // Replacement dictionary for i18n.
290 const ui::TemplateReplacements* replacements_;
291
283 // The backend is owned by net::URLRequestContext and always outlives us. 292 // The backend is owned by net::URLRequestContext and always outlives us.
284 URLDataManagerBackend* const backend_; 293 URLDataManagerBackend* const backend_;
285 294
286 base::WeakPtrFactory<URLRequestChromeJob> weak_factory_; 295 base::WeakPtrFactory<URLRequestChromeJob> weak_factory_;
287 296
288 DISALLOW_COPY_AND_ASSIGN(URLRequestChromeJob); 297 DISALLOW_COPY_AND_ASSIGN(URLRequestChromeJob);
289 }; 298 };
290 299
291 URLRequestChromeJob::URLRequestChromeJob(net::URLRequest* request, 300 URLRequestChromeJob::URLRequestChromeJob(net::URLRequest* request,
292 net::NetworkDelegate* network_delegate, 301 net::NetworkDelegate* network_delegate,
293 URLDataManagerBackend* backend, 302 URLDataManagerBackend* backend,
294 bool is_incognito) 303 bool is_incognito)
295 : net::URLRequestJob(request, network_delegate), 304 : net::URLRequestJob(request, network_delegate),
296 data_offset_(0), 305 data_offset_(0),
297 data_available_status_(net::OK), 306 data_available_status_(net::OK),
298 pending_buf_size_(0), 307 pending_buf_size_(0),
299 allow_caching_(true), 308 allow_caching_(true),
300 add_content_security_policy_(true), 309 add_content_security_policy_(true),
301 deny_xframe_options_(true), 310 deny_xframe_options_(true),
302 send_content_type_header_(false), 311 send_content_type_header_(false),
303 is_incognito_(is_incognito), 312 is_incognito_(is_incognito),
304 is_gzipped_(false), 313 is_gzipped_(false),
314 replacements_(nullptr),
305 backend_(backend), 315 backend_(backend),
306 weak_factory_(this) { 316 weak_factory_(this) {
307 DCHECK(backend); 317 DCHECK(backend);
308 } 318 }
309 319
310 URLRequestChromeJob::~URLRequestChromeJob() { 320 URLRequestChromeJob::~URLRequestChromeJob() {
311 CHECK(!backend_->HasPendingJob(this)); 321 CHECK(!backend_->HasPendingJob(this));
312 } 322 }
313 323
314 void URLRequestChromeJob::Start() { 324 void URLRequestChromeJob::Start() {
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
389 info->headers->AddHeader("Access-Control-Allow-Origin: " + 399 info->headers->AddHeader("Access-Control-Allow-Origin: " +
390 access_control_allow_origin_); 400 access_control_allow_origin_);
391 info->headers->AddHeader("Vary: Origin"); 401 info->headers->AddHeader("Vary: Origin");
392 } 402 }
393 403
394 if (is_gzipped_) 404 if (is_gzipped_)
395 info->headers->AddHeader("Content-Encoding: gzip"); 405 info->headers->AddHeader("Content-Encoding: gzip");
396 } 406 }
397 407
398 std::unique_ptr<net::SourceStream> URLRequestChromeJob::SetUpSourceStream() { 408 std::unique_ptr<net::SourceStream> URLRequestChromeJob::SetUpSourceStream() {
399 std::unique_ptr<net::SourceStream> source = 409 std::unique_ptr<net::SourceStream> source_stream =
400 net::URLRequestJob::SetUpSourceStream(); 410 net::URLRequestJob::SetUpSourceStream();
401 return is_gzipped_ ? net::GzipSourceStream::Create( 411
402 std::move(source), net::SourceStream::TYPE_GZIP) 412 if (is_gzipped_) {
403 : std::move(source); 413 source_stream = net::GzipSourceStream::Create(std::move(source_stream),
414 net::SourceStream::TYPE_GZIP);
415 }
416
417 if (replacements_) {
418 source_stream = content::I18nSourceStream::Create(
419 std::move(source_stream), net::SourceStream::TYPE_NONE, replacements_);
420 }
421
422 return source_stream;
404 } 423 }
405 424
406 void URLRequestChromeJob::MimeTypeAvailable(const std::string& mime_type) { 425 void URLRequestChromeJob::MimeTypeAvailable(const std::string& mime_type) {
407 set_mime_type(mime_type); 426 set_mime_type(mime_type);
408 NotifyHeadersComplete(); 427 NotifyHeadersComplete();
409 } 428 }
410 429
411 void URLRequestChromeJob::DataAvailable(base::RefCountedMemory* bytes) { 430 void URLRequestChromeJob::DataAvailable(base::RefCountedMemory* bytes) {
412 TRACE_EVENT_ASYNC_END0("browser", "DataManager:Request", this); 431 TRACE_EVENT_ASYNC_END0("browser", "DataManager:Request", this);
413 DCHECK(!data_); 432 DCHECK(!data_);
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after
705 job->set_content_security_policy_style_source( 724 job->set_content_security_policy_style_source(
706 source->source()->GetContentSecurityPolicyStyleSrc()); 725 source->source()->GetContentSecurityPolicyStyleSrc());
707 job->set_content_security_policy_image_source( 726 job->set_content_security_policy_image_source(
708 source->source()->GetContentSecurityPolicyImgSrc()); 727 source->source()->GetContentSecurityPolicyImgSrc());
709 job->set_deny_xframe_options( 728 job->set_deny_xframe_options(
710 source->source()->ShouldDenyXFrameOptions()); 729 source->source()->ShouldDenyXFrameOptions());
711 job->set_send_content_type_header( 730 job->set_send_content_type_header(
712 source->source()->ShouldServeMimeTypeAsContentTypeHeader()); 731 source->source()->ShouldServeMimeTypeAsContentTypeHeader());
713 job->set_is_gzipped(source->source()->IsGzipped(path)); 732 job->set_is_gzipped(source->source()->IsGzipped(path));
714 733
734 // TODO(dschuyler): improve filtering of which resource to run template
735 // replacements upon.
736 if (source->source()->GetMimeType(path) == "text/html")
737 job->SetReplacements(source->GetReplacements());
738
715 std::string origin = GetOriginHeaderValue(request); 739 std::string origin = GetOriginHeaderValue(request);
716 if (!origin.empty()) { 740 if (!origin.empty()) {
717 std::string header = 741 std::string header =
718 source->source()->GetAccessControlAllowOriginForOrigin(origin); 742 source->source()->GetAccessControlAllowOriginForOrigin(origin);
719 DCHECK(header.empty() || header == origin || header == "*" || 743 DCHECK(header.empty() || header == origin || header == "*" ||
720 header == "null"); 744 header == "null");
721 job->set_access_control_allow_origin(header); 745 job->set_access_control_allow_origin(header);
722 } 746 }
723 747
724 // Look up additional request info to pass down. 748 // Look up additional request info to pass down.
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
869 893
870 } // namespace 894 } // namespace
871 895
872 net::URLRequestJobFactory::ProtocolHandler* CreateDevToolsProtocolHandler( 896 net::URLRequestJobFactory::ProtocolHandler* CreateDevToolsProtocolHandler(
873 ResourceContext* resource_context, 897 ResourceContext* resource_context,
874 bool is_incognito) { 898 bool is_incognito) {
875 return new DevToolsJobFactory(resource_context, is_incognito); 899 return new DevToolsJobFactory(resource_context, is_incognito);
876 } 900 }
877 901
878 } // namespace content 902 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698