Chromium Code Reviews| 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 c71f73dd74430bd4a9f6b700dadfd961078d83fb..4e2b8f2599d54d0235d3df2e5218cef785ed6f4a 100644 |
| --- a/third_party/WebKit/Source/core/loader/FrameFetchContext.cpp |
| +++ b/third_party/WebKit/Source/core/loader/FrameFetchContext.cpp |
| @@ -606,6 +606,22 @@ void FrameFetchContext::ModifyRequestForCSP(ResourceRequest& resource_request) { |
| GetFrame()->Loader().ModifyRequestForCSP(resource_request, GetDocument()); |
| } |
| +float FrameFetchContext::ClientHintsDeviceRAM(int 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 = -1; // To compensate of the extra loop iteration. |
| + // Extract the MSB location. |
| + while (physical_memory_mb != 0) { |
| + physical_memory_mb >>= 1; |
| + power++; |
| + } |
|
Yoav Weiss
2017/05/26 05:44:22
```
int power = 0;
// Extract the MSB location.
wh
fmeawad
2017/05/26 20:13:16
Done. and thank you for your patience.
|
| + // Restore to the power of 2, and convert to GB. |
| + return (float)(1 << power) / (float)1024.0; |
|
Yoav Weiss
2017/05/26 05:44:22
static_cast<float>() would be better for the first
fmeawad
2017/05/26 20:13:16
Done.
|
| +} |
| + |
| void FrameFetchContext::AddClientHintsIfNecessary( |
| const ClientHintsPreferences& hints_preferences, |
| const FetchParameters::ResourceWidth& resource_width, |
| @@ -613,6 +629,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(); |
| @@ -623,6 +642,13 @@ void FrameFetchContext::AddClientHintsIfNecessary( |
| GetDocument()->GetClientHintsPreferences().ShouldSendViewportWidth() || |
| hints_preferences.ShouldSendViewportWidth(); |
| + if (should_send_device_ram) { |
| + int physical_memory = Platform::Current()->AmountOfPhysicalMemoryMB(); |
| + request.AddHTTPHeaderField( |
| + "device-ram", |
| + AtomicString(String::Number(ClientHintsDeviceRAM(physical_memory)))); |
| + } |
| + |
| if (should_send_dpr) { |
| request.AddHTTPHeaderField( |
| "DPR", AtomicString(String::Number(GetDocument()->DevicePixelRatio()))); |