OLD | NEW |
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 596 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
607 return document_ ? document_->GetSecurityOrigin() : nullptr; | 607 return document_ ? document_->GetSecurityOrigin() : nullptr; |
608 } | 608 } |
609 | 609 |
610 void FrameFetchContext::ModifyRequestForCSP(ResourceRequest& resource_request) { | 610 void FrameFetchContext::ModifyRequestForCSP(ResourceRequest& resource_request) { |
611 // Record the latest requiredCSP value that will be used when sending this | 611 // Record the latest requiredCSP value that will be used when sending this |
612 // request. | 612 // request. |
613 GetFrame()->Loader().RecordLatestRequiredCSP(); | 613 GetFrame()->Loader().RecordLatestRequiredCSP(); |
614 GetFrame()->Loader().ModifyRequestForCSP(resource_request, GetDocument()); | 614 GetFrame()->Loader().ModifyRequestForCSP(resource_request, GetDocument()); |
615 } | 615 } |
616 | 616 |
| 617 float FrameFetchContext::ClientHintsDeviceRAM(int64_t physical_memory_mb) { |
| 618 // TODO(fmeawad): The calculations in this method are still evolving as the |
| 619 // spec gets updated: https://github.com/WICG/device-ram. The reported |
| 620 // device-ram is rounded down to next power of 2 in GB. Ex. 3072MB will return |
| 621 // 2, and 768MB will return 0.5. |
| 622 DCHECK_GT(physical_memory_mb, 0); |
| 623 int power = 0; |
| 624 // Extract the MSB location. |
| 625 while (physical_memory_mb > 1) { |
| 626 physical_memory_mb >>= 1; |
| 627 power++; |
| 628 } |
| 629 // Restore to the power of 2, and convert to GB. |
| 630 return static_cast<float>(1 << power) / 1024.0; |
| 631 } |
| 632 |
617 void FrameFetchContext::AddClientHintsIfNecessary( | 633 void FrameFetchContext::AddClientHintsIfNecessary( |
618 const ClientHintsPreferences& hints_preferences, | 634 const ClientHintsPreferences& hints_preferences, |
619 const FetchParameters::ResourceWidth& resource_width, | 635 const FetchParameters::ResourceWidth& resource_width, |
620 ResourceRequest& request) { | 636 ResourceRequest& request) { |
621 if (!RuntimeEnabledFeatures::clientHintsEnabled() || !GetDocument()) | 637 if (!RuntimeEnabledFeatures::clientHintsEnabled() || !GetDocument()) |
622 return; | 638 return; |
623 | 639 |
| 640 bool should_send_device_ram = |
| 641 GetDocument()->GetClientHintsPreferences().ShouldSendDeviceRAM() || |
| 642 hints_preferences.ShouldSendDeviceRAM(); |
624 bool should_send_dpr = | 643 bool should_send_dpr = |
625 GetDocument()->GetClientHintsPreferences().ShouldSendDPR() || | 644 GetDocument()->GetClientHintsPreferences().ShouldSendDPR() || |
626 hints_preferences.ShouldSendDPR(); | 645 hints_preferences.ShouldSendDPR(); |
627 bool should_send_resource_width = | 646 bool should_send_resource_width = |
628 GetDocument()->GetClientHintsPreferences().ShouldSendResourceWidth() || | 647 GetDocument()->GetClientHintsPreferences().ShouldSendResourceWidth() || |
629 hints_preferences.ShouldSendResourceWidth(); | 648 hints_preferences.ShouldSendResourceWidth(); |
630 bool should_send_viewport_width = | 649 bool should_send_viewport_width = |
631 GetDocument()->GetClientHintsPreferences().ShouldSendViewportWidth() || | 650 GetDocument()->GetClientHintsPreferences().ShouldSendViewportWidth() || |
632 hints_preferences.ShouldSendViewportWidth(); | 651 hints_preferences.ShouldSendViewportWidth(); |
633 | 652 |
| 653 if (should_send_device_ram) { |
| 654 int64_t physical_memory = MemoryCoordinator::GetPhysicalMemoryMB(); |
| 655 request.AddHTTPHeaderField( |
| 656 "device-ram", |
| 657 AtomicString(String::Number(ClientHintsDeviceRAM(physical_memory)))); |
| 658 } |
| 659 |
634 if (should_send_dpr) { | 660 if (should_send_dpr) { |
635 request.AddHTTPHeaderField( | 661 request.AddHTTPHeaderField( |
636 "DPR", AtomicString(String::Number(GetDocument()->DevicePixelRatio()))); | 662 "DPR", AtomicString(String::Number(GetDocument()->DevicePixelRatio()))); |
637 } | 663 } |
638 | 664 |
639 if (should_send_resource_width) { | 665 if (should_send_resource_width) { |
640 if (resource_width.is_set) { | 666 if (resource_width.is_set) { |
641 float physical_width = | 667 float physical_width = |
642 resource_width.width * GetDocument()->DevicePixelRatio(); | 668 resource_width.width * GetDocument()->DevicePixelRatio(); |
643 request.AddHTTPHeaderField( | 669 request.AddHTTPHeaderField( |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
809 return loader; | 835 return loader; |
810 } | 836 } |
811 | 837 |
812 DEFINE_TRACE(FrameFetchContext) { | 838 DEFINE_TRACE(FrameFetchContext) { |
813 visitor->Trace(document_loader_); | 839 visitor->Trace(document_loader_); |
814 visitor->Trace(document_); | 840 visitor->Trace(document_); |
815 BaseFetchContext::Trace(visitor); | 841 BaseFetchContext::Trace(visitor); |
816 } | 842 } |
817 | 843 |
818 } // namespace blink | 844 } // namespace blink |
OLD | NEW |