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

Unified Diff: third_party/WebKit/Source/core/loader/FrameFetchContext.cpp

Issue 2860093003: Implement device-ram client hints header (Closed)
Patch Set: Minor fixes 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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/loader/FrameFetchContext.cpp
diff --git a/third_party/WebKit/Source/core/loader/FrameFetchContext.cpp b/third_party/WebKit/Source/core/loader/FrameFetchContext.cpp
index 0065c6e443491787b7fd6288591f036075c23407..b1bc1d009e1fb72b30fdf1fd3b3e3b1a23e066d0 100644
--- a/third_party/WebKit/Source/core/loader/FrameFetchContext.cpp
+++ b/third_party/WebKit/Source/core/loader/FrameFetchContext.cpp
@@ -610,6 +610,22 @@ void FrameFetchContext::ModifyRequestForCSP(ResourceRequest& resource_request) {
GetFrame()->Loader().ModifyRequestForCSP(resource_request, GetDocument());
}
+float FrameFetchContext::ClientHintsDeviceRAM(int64_t physical_memory_mb) {
+ // TODO(fmeawad): The calculations in this method are still evolving as the
+ // spec gets updated: https://github.com/WICG/device-ram. The reported
+ // device-ram is rounded down to next power of 2 in GB. Ex. 3072MB will return
+ // 2, and 768MB will return 0.5.
+ DCHECK_GT(physical_memory_mb, 0);
+ int power = 0; // To compensate of the extra loop iteration.
Yoav Weiss 2017/05/26 20:48:15 The "compensate" comment seems out of place now
fmeawad 2017/05/30 17:20:59 Done.
+ // Extract the MSB location.
+ while (physical_memory_mb > 1) {
+ physical_memory_mb >>= 1;
+ power++;
+ }
+ // Restore to the power of 2, and convert to GB.
+ return static_cast<float>(1 << power) / 1024.0;
+}
+
void FrameFetchContext::AddClientHintsIfNecessary(
const ClientHintsPreferences& hints_preferences,
const FetchParameters::ResourceWidth& resource_width,
@@ -617,6 +633,9 @@ void FrameFetchContext::AddClientHintsIfNecessary(
if (!RuntimeEnabledFeatures::clientHintsEnabled() || !GetDocument())
return;
+ bool should_send_device_ram =
+ GetDocument()->GetClientHintsPreferences().ShouldSendDeviceRAM() ||
+ hints_preferences.ShouldSendDeviceRAM();
bool should_send_dpr =
GetDocument()->GetClientHintsPreferences().ShouldSendDPR() ||
hints_preferences.ShouldSendDPR();
@@ -627,6 +646,13 @@ void FrameFetchContext::AddClientHintsIfNecessary(
GetDocument()->GetClientHintsPreferences().ShouldSendViewportWidth() ||
hints_preferences.ShouldSendViewportWidth();
+ if (should_send_device_ram) {
+ int64_t physical_memory = MemoryCoordinator::GetPhysicalMemoryMB();
+ request.AddHTTPHeaderField(
+ "device-ram",
+ AtomicString(String::Number(ClientHintsDeviceRAM(physical_memory))));
+ }
+
if (should_send_dpr) {
request.AddHTTPHeaderField(
"DPR", AtomicString(String::Number(GetDocument()->DevicePixelRatio())));

Powered by Google App Engine
This is Rietveld 408576698