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

Side by Side Diff: third_party/WebKit/Source/core/loader/FrameFetchContext.cpp

Issue 2860093003: Implement device-ram client hints header (Closed)
Patch Set: Fix FrameFetchContextTest Created 3 years, 7 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 /* 1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved. 2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 588 matching lines...) Expand 10 before | Expand all | Expand 10 after
599 GetFrame()->Console().AddMessage(console_message); 599 GetFrame()->Console().AddMessage(console_message);
600 } 600 }
601 601
602 void FrameFetchContext::ModifyRequestForCSP(ResourceRequest& resource_request) { 602 void FrameFetchContext::ModifyRequestForCSP(ResourceRequest& resource_request) {
603 // Record the latest requiredCSP value that will be used when sending this 603 // Record the latest requiredCSP value that will be used when sending this
604 // request. 604 // request.
605 GetFrame()->Loader().RecordLatestRequiredCSP(); 605 GetFrame()->Loader().RecordLatestRequiredCSP();
606 GetFrame()->Loader().ModifyRequestForCSP(resource_request, GetDocument()); 606 GetFrame()->Loader().ModifyRequestForCSP(resource_request, GetDocument());
607 } 607 }
608 608
609 float FrameFetchContext::ClientHintsDeviceRam(int physical_memory_mb) {
panicker 2017/05/24 17:27:20 let's add a comment that this math with change and
fmeawad 2017/05/24 18:24:57 Done.
610 // The reported device-ram is rounded down to next power of 2 in GB. Ex.
611 // 3072MB will return 2, and 768MB will return 0.5.
612 DCHECK_GT(physical_memory_mb, 0);
613 int power = -1;
Yoav Weiss 2017/05/24 07:48:02 I find the MSB counting here a bit confusing. Mayb
fmeawad 2017/05/24 18:24:57 The loop does an extra iteration, to compensate, w
614 // Extract the MSB location.
615 while (physical_memory_mb != 0) {
616 physical_memory_mb = physical_memory_mb >> 1;
Yoav Weiss 2017/05/24 07:48:02 Nit: "physical_memory_mb >>= 1;" might be a bit cl
fmeawad 2017/05/24 18:24:56 Done.
617 power++;
618 }
619 // Restore to the power of 2, and convert to GB.
620 return ((1 << power) / 1024.0);
Yoav Weiss 2017/05/24 07:48:02 Implicit conversion from int to float... Can you m
fmeawad 2017/05/24 18:24:56 Done.
621 }
622
609 void FrameFetchContext::AddClientHintsIfNecessary( 623 void FrameFetchContext::AddClientHintsIfNecessary(
610 const ClientHintsPreferences& hints_preferences, 624 const ClientHintsPreferences& hints_preferences,
611 const FetchParameters::ResourceWidth& resource_width, 625 const FetchParameters::ResourceWidth& resource_width,
612 ResourceRequest& request) { 626 ResourceRequest& request) {
613 if (!RuntimeEnabledFeatures::clientHintsEnabled() || !GetDocument()) 627 if (!RuntimeEnabledFeatures::clientHintsEnabled() || !GetDocument())
614 return; 628 return;
615 629
630 bool should_send_device_ram =
631 GetDocument()->GetClientHintsPreferences().ShouldSendDeviceRam() ||
632 hints_preferences.ShouldSendDeviceRam();
616 bool should_send_dpr = 633 bool should_send_dpr =
617 GetDocument()->GetClientHintsPreferences().ShouldSendDPR() || 634 GetDocument()->GetClientHintsPreferences().ShouldSendDPR() ||
618 hints_preferences.ShouldSendDPR(); 635 hints_preferences.ShouldSendDPR();
619 bool should_send_resource_width = 636 bool should_send_resource_width =
620 GetDocument()->GetClientHintsPreferences().ShouldSendResourceWidth() || 637 GetDocument()->GetClientHintsPreferences().ShouldSendResourceWidth() ||
621 hints_preferences.ShouldSendResourceWidth(); 638 hints_preferences.ShouldSendResourceWidth();
622 bool should_send_viewport_width = 639 bool should_send_viewport_width =
623 GetDocument()->GetClientHintsPreferences().ShouldSendViewportWidth() || 640 GetDocument()->GetClientHintsPreferences().ShouldSendViewportWidth() ||
624 hints_preferences.ShouldSendViewportWidth(); 641 hints_preferences.ShouldSendViewportWidth();
625 642
643 if (should_send_device_ram) {
644 int physical_memory = Platform::Current()->AmountOfPhysicalMemoryMB();
645 request.AddHTTPHeaderField(
646 "device-ram",
647 AtomicString(String::Number(ClientHintsDeviceRam(physical_memory))));
648 }
649
626 if (should_send_dpr) { 650 if (should_send_dpr) {
627 request.AddHTTPHeaderField( 651 request.AddHTTPHeaderField(
628 "DPR", AtomicString(String::Number(GetDocument()->DevicePixelRatio()))); 652 "DPR", AtomicString(String::Number(GetDocument()->DevicePixelRatio())));
629 } 653 }
630 654
631 if (should_send_resource_width) { 655 if (should_send_resource_width) {
632 if (resource_width.is_set) { 656 if (resource_width.is_set) {
633 float physical_width = 657 float physical_width =
634 resource_width.width * GetDocument()->DevicePixelRatio(); 658 resource_width.width * GetDocument()->DevicePixelRatio();
635 request.AddHTTPHeaderField( 659 request.AddHTTPHeaderField(
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
779 std::unique_ptr<WebURLLoader> FrameFetchContext::CreateURLLoader() { 803 std::unique_ptr<WebURLLoader> FrameFetchContext::CreateURLLoader() {
780 return GetFrame()->CreateURLLoader(); 804 return GetFrame()->CreateURLLoader();
781 } 805 }
782 806
783 DEFINE_TRACE(FrameFetchContext) { 807 DEFINE_TRACE(FrameFetchContext) {
784 visitor->Trace(document_loader_); 808 visitor->Trace(document_loader_);
785 BaseFetchContext::Trace(visitor); 809 BaseFetchContext::Trace(visitor);
786 } 810 }
787 811
788 } // namespace blink 812 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698