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 588 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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) { |
| 610 // TODO(fmeawad): The calculations in this method are still evolving as the |
| 611 // spec gets updated: https://github.com/WICG/device-ram The reported |
| 612 // device-ram is rounded down to next power of 2 in GB. Ex. 3072MB will return |
| 613 // 2, and 768MB will return 0.5. |
| 614 DCHECK_GT(physical_memory_mb, 0); |
| 615 int power = -1; // To compensate of the extra loop iteration. |
| 616 // Extract the MSB location. |
| 617 while (physical_memory_mb != 0) { |
| 618 physical_memory_mb >>= 1; |
| 619 power++; |
| 620 } |
| 621 // Restore to the power of 2, and convert to GB. |
| 622 return (float)(1 << power) / (float)1024.0; |
| 623 } |
| 624 |
609 void FrameFetchContext::AddClientHintsIfNecessary( | 625 void FrameFetchContext::AddClientHintsIfNecessary( |
610 const ClientHintsPreferences& hints_preferences, | 626 const ClientHintsPreferences& hints_preferences, |
611 const FetchParameters::ResourceWidth& resource_width, | 627 const FetchParameters::ResourceWidth& resource_width, |
612 ResourceRequest& request) { | 628 ResourceRequest& request) { |
613 if (!RuntimeEnabledFeatures::clientHintsEnabled() || !GetDocument()) | 629 if (!RuntimeEnabledFeatures::clientHintsEnabled() || !GetDocument()) |
614 return; | 630 return; |
615 | 631 |
| 632 bool should_send_device_ram = |
| 633 GetDocument()->GetClientHintsPreferences().ShouldSendDeviceRAM() || |
| 634 hints_preferences.ShouldSendDeviceRAM(); |
616 bool should_send_dpr = | 635 bool should_send_dpr = |
617 GetDocument()->GetClientHintsPreferences().ShouldSendDPR() || | 636 GetDocument()->GetClientHintsPreferences().ShouldSendDPR() || |
618 hints_preferences.ShouldSendDPR(); | 637 hints_preferences.ShouldSendDPR(); |
619 bool should_send_resource_width = | 638 bool should_send_resource_width = |
620 GetDocument()->GetClientHintsPreferences().ShouldSendResourceWidth() || | 639 GetDocument()->GetClientHintsPreferences().ShouldSendResourceWidth() || |
621 hints_preferences.ShouldSendResourceWidth(); | 640 hints_preferences.ShouldSendResourceWidth(); |
622 bool should_send_viewport_width = | 641 bool should_send_viewport_width = |
623 GetDocument()->GetClientHintsPreferences().ShouldSendViewportWidth() || | 642 GetDocument()->GetClientHintsPreferences().ShouldSendViewportWidth() || |
624 hints_preferences.ShouldSendViewportWidth(); | 643 hints_preferences.ShouldSendViewportWidth(); |
625 | 644 |
| 645 if (should_send_device_ram) { |
| 646 int physical_memory = Platform::Current()->AmountOfPhysicalMemoryMB(); |
| 647 request.AddHTTPHeaderField( |
| 648 "device-ram", |
| 649 AtomicString(String::Number(ClientHintsDeviceRAM(physical_memory)))); |
| 650 } |
| 651 |
626 if (should_send_dpr) { | 652 if (should_send_dpr) { |
627 request.AddHTTPHeaderField( | 653 request.AddHTTPHeaderField( |
628 "DPR", AtomicString(String::Number(GetDocument()->DevicePixelRatio()))); | 654 "DPR", AtomicString(String::Number(GetDocument()->DevicePixelRatio()))); |
629 } | 655 } |
630 | 656 |
631 if (should_send_resource_width) { | 657 if (should_send_resource_width) { |
632 if (resource_width.is_set) { | 658 if (resource_width.is_set) { |
633 float physical_width = | 659 float physical_width = |
634 resource_width.width * GetDocument()->DevicePixelRatio(); | 660 resource_width.width * GetDocument()->DevicePixelRatio(); |
635 request.AddHTTPHeaderField( | 661 request.AddHTTPHeaderField( |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
775 std::unique_ptr<WebURLLoader> FrameFetchContext::CreateURLLoader() { | 801 std::unique_ptr<WebURLLoader> FrameFetchContext::CreateURLLoader() { |
776 return GetFrame()->CreateURLLoader(); | 802 return GetFrame()->CreateURLLoader(); |
777 } | 803 } |
778 | 804 |
779 DEFINE_TRACE(FrameFetchContext) { | 805 DEFINE_TRACE(FrameFetchContext) { |
780 visitor->Trace(document_loader_); | 806 visitor->Trace(document_loader_); |
781 BaseFetchContext::Trace(visitor); | 807 BaseFetchContext::Trace(visitor); |
782 } | 808 } |
783 | 809 |
784 } // namespace blink | 810 } // namespace blink |
OLD | NEW |