| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/service_worker/service_worker_dispatcher_host.h" | 5 #include "content/browser/service_worker/service_worker_dispatcher_host.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/debug/crash_logging.h" | 9 #include "base/debug/crash_logging.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 "The Service Worker system has shutdown."; | 51 "The Service Worker system has shutdown."; |
| 52 const char kUserDeniedPermissionMessage[] = | 52 const char kUserDeniedPermissionMessage[] = |
| 53 "The user denied permission to use Service Worker."; | 53 "The user denied permission to use Service Worker."; |
| 54 const char kInvalidStateErrorMessage[] = "The object is in an invalid state."; | 54 const char kInvalidStateErrorMessage[] = "The object is in an invalid state."; |
| 55 const char kEnableNavigationPreloadErrorPrefix[] = | 55 const char kEnableNavigationPreloadErrorPrefix[] = |
| 56 "Failed to enable or disable navigation preload: "; | 56 "Failed to enable or disable navigation preload: "; |
| 57 const char kGetNavigationPreloadStateErrorPrefix[] = | 57 const char kGetNavigationPreloadStateErrorPrefix[] = |
| 58 "Failed to get navigation preload state: "; | 58 "Failed to get navigation preload state: "; |
| 59 const char kSetNavigationPreloadHeaderErrorPrefix[] = | 59 const char kSetNavigationPreloadHeaderErrorPrefix[] = |
| 60 "Failed to set navigation preload header: "; | 60 "Failed to set navigation preload header: "; |
| 61 const char kNoActiveWorkerErrorMessage[] = |
| 62 "The registration does not have an active worker."; |
| 63 const char kDatabaseErrorMessage[] = "Failed to access storage."; |
| 61 | 64 |
| 62 const uint32_t kFilteredMessageClasses[] = { | 65 const uint32_t kFilteredMessageClasses[] = { |
| 63 ServiceWorkerMsgStart, EmbeddedWorkerMsgStart, | 66 ServiceWorkerMsgStart, EmbeddedWorkerMsgStart, |
| 64 }; | 67 }; |
| 65 | 68 |
| 66 void RunSoon(const base::Closure& callback) { | 69 void RunSoon(const base::Closure& callback) { |
| 67 if (!callback.is_null()) | 70 if (!callback.is_null()) |
| 68 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); | 71 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); |
| 69 } | 72 } |
| 70 | 73 |
| (...skipping 629 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 700 | 703 |
| 701 ServiceWorkerRegistration* registration = | 704 ServiceWorkerRegistration* registration = |
| 702 GetContext()->GetLiveRegistration(registration_id); | 705 GetContext()->GetLiveRegistration(registration_id); |
| 703 if (!registration) { | 706 if (!registration) { |
| 704 // |registration| must be alive because a renderer retains a registration | 707 // |registration| must be alive because a renderer retains a registration |
| 705 // reference at this point. | 708 // reference at this point. |
| 706 bad_message::ReceivedBadMessage( | 709 bad_message::ReceivedBadMessage( |
| 707 this, bad_message::SWDH_ENABLE_NAVIGATION_PRELOAD_BAD_REGISTRATION_ID); | 710 this, bad_message::SWDH_ENABLE_NAVIGATION_PRELOAD_BAD_REGISTRATION_ID); |
| 708 return; | 711 return; |
| 709 } | 712 } |
| 713 // The spec discussion consensus is to reject if there is no active worker: |
| 714 // https://github.com/w3c/ServiceWorker/issues/920#issuecomment-262212670 |
| 715 // TODO(falken): Remove this comment when the spec is updated. |
| 716 if (!registration->active_version()) { |
| 717 Send(new ServiceWorkerMsg_EnableNavigationPreloadError( |
| 718 thread_id, request_id, WebServiceWorkerError::ErrorTypeState, |
| 719 std::string(kEnableNavigationPreloadErrorPrefix) + |
| 720 std::string(kNoActiveWorkerErrorMessage))); |
| 721 return; |
| 722 } |
| 710 | 723 |
| 711 std::vector<GURL> urls = {provider_host->document_url(), | 724 std::vector<GURL> urls = {provider_host->document_url(), |
| 712 registration->pattern()}; | 725 registration->pattern()}; |
| 713 if (!ServiceWorkerUtils::AllOriginsMatchAndCanAccessServiceWorkers(urls)) { | 726 if (!ServiceWorkerUtils::AllOriginsMatchAndCanAccessServiceWorkers(urls)) { |
| 714 bad_message::ReceivedBadMessage( | 727 bad_message::ReceivedBadMessage( |
| 715 this, bad_message::SWDH_ENABLE_NAVIGATION_PRELOAD_INVALID_ORIGIN); | 728 this, bad_message::SWDH_ENABLE_NAVIGATION_PRELOAD_INVALID_ORIGIN); |
| 716 return; | 729 return; |
| 717 } | 730 } |
| 718 | 731 |
| 719 if (!GetContentClient()->browser()->AllowServiceWorker( | 732 if (!GetContentClient()->browser()->AllowServiceWorker( |
| 720 registration->pattern(), provider_host->topmost_frame_url(), | 733 registration->pattern(), provider_host->topmost_frame_url(), |
| 721 resource_context_, render_process_id_, provider_host->frame_id())) { | 734 resource_context_, render_process_id_, provider_host->frame_id())) { |
| 722 Send(new ServiceWorkerMsg_EnableNavigationPreloadError( | 735 Send(new ServiceWorkerMsg_EnableNavigationPreloadError( |
| 723 thread_id, request_id, WebServiceWorkerError::ErrorTypeDisabled, | 736 thread_id, request_id, WebServiceWorkerError::ErrorTypeDisabled, |
| 724 std::string(kEnableNavigationPreloadErrorPrefix) + | 737 std::string(kEnableNavigationPreloadErrorPrefix) + |
| 725 std::string(kUserDeniedPermissionMessage))); | 738 std::string(kUserDeniedPermissionMessage))); |
| 726 return; | 739 return; |
| 727 } | 740 } |
| 728 | 741 |
| 729 // TODO(falken): Write to disk before resolving the promise. | 742 GetContext()->storage()->UpdateNavigationPreloadEnabled( |
| 730 registration->EnableNavigationPreload(enable); | 743 registration->id(), registration->pattern().GetOrigin(), enable, |
| 731 Send(new ServiceWorkerMsg_DidEnableNavigationPreload(thread_id, request_id)); | 744 base::Bind( |
| 745 &ServiceWorkerDispatcherHost::DidUpdateNavigationPreloadEnabled, this, |
| 746 thread_id, request_id, registration->id(), enable)); |
| 732 } | 747 } |
| 733 | 748 |
| 734 void ServiceWorkerDispatcherHost::OnGetNavigationPreloadState( | 749 void ServiceWorkerDispatcherHost::OnGetNavigationPreloadState( |
| 735 int thread_id, | 750 int thread_id, |
| 736 int request_id, | 751 int request_id, |
| 737 int provider_id, | 752 int provider_id, |
| 738 int64_t registration_id) { | 753 int64_t registration_id) { |
| 739 ProviderStatus provider_status; | 754 ProviderStatus provider_status; |
| 740 ServiceWorkerProviderHost* provider_host = | 755 ServiceWorkerProviderHost* provider_host = |
| 741 GetProviderHostForRequest(&provider_status, provider_id); | 756 GetProviderHostForRequest(&provider_status, provider_id); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 784 registration->pattern(), provider_host->topmost_frame_url(), | 799 registration->pattern(), provider_host->topmost_frame_url(), |
| 785 resource_context_, render_process_id_, provider_host->frame_id())) { | 800 resource_context_, render_process_id_, provider_host->frame_id())) { |
| 786 Send(new ServiceWorkerMsg_GetNavigationPreloadStateError( | 801 Send(new ServiceWorkerMsg_GetNavigationPreloadStateError( |
| 787 thread_id, request_id, WebServiceWorkerError::ErrorTypeDisabled, | 802 thread_id, request_id, WebServiceWorkerError::ErrorTypeDisabled, |
| 788 std::string(kGetNavigationPreloadStateErrorPrefix) + | 803 std::string(kGetNavigationPreloadStateErrorPrefix) + |
| 789 std::string(kUserDeniedPermissionMessage))); | 804 std::string(kUserDeniedPermissionMessage))); |
| 790 return; | 805 return; |
| 791 } | 806 } |
| 792 | 807 |
| 793 Send(new ServiceWorkerMsg_DidGetNavigationPreloadState( | 808 Send(new ServiceWorkerMsg_DidGetNavigationPreloadState( |
| 794 thread_id, request_id, | 809 thread_id, request_id, registration->navigation_preload_state())); |
| 795 NavigationPreloadState(registration->is_navigation_preload_enabled(), | |
| 796 registration->navigation_preload_header()))); | |
| 797 } | 810 } |
| 798 | 811 |
| 799 void ServiceWorkerDispatcherHost::OnSetNavigationPreloadHeader( | 812 void ServiceWorkerDispatcherHost::OnSetNavigationPreloadHeader( |
| 800 int thread_id, | 813 int thread_id, |
| 801 int request_id, | 814 int request_id, |
| 802 int provider_id, | 815 int provider_id, |
| 803 int64_t registration_id, | 816 int64_t registration_id, |
| 804 const std::string& value) { | 817 const std::string& value) { |
| 805 ProviderStatus provider_status; | 818 ProviderStatus provider_status; |
| 806 ServiceWorkerProviderHost* provider_host = | 819 ServiceWorkerProviderHost* provider_host = |
| (...skipping 23 matching lines...) Expand all Loading... |
| 830 ServiceWorkerRegistration* registration = | 843 ServiceWorkerRegistration* registration = |
| 831 GetContext()->GetLiveRegistration(registration_id); | 844 GetContext()->GetLiveRegistration(registration_id); |
| 832 if (!registration) { | 845 if (!registration) { |
| 833 // |registration| must be alive because a renderer retains a registration | 846 // |registration| must be alive because a renderer retains a registration |
| 834 // reference at this point. | 847 // reference at this point. |
| 835 bad_message::ReceivedBadMessage( | 848 bad_message::ReceivedBadMessage( |
| 836 this, | 849 this, |
| 837 bad_message::SWDH_SET_NAVIGATION_PRELOAD_HEADER_BAD_REGISTRATION_ID); | 850 bad_message::SWDH_SET_NAVIGATION_PRELOAD_HEADER_BAD_REGISTRATION_ID); |
| 838 return; | 851 return; |
| 839 } | 852 } |
| 853 // The spec discussion consensus is to reject if there is no active worker: |
| 854 // https://github.com/w3c/ServiceWorker/issues/920#issuecomment-262212670 |
| 855 // TODO(falken): Remove this comment when the spec is updated. |
| 856 if (!registration->active_version()) { |
| 857 Send(new ServiceWorkerMsg_SetNavigationPreloadHeaderError( |
| 858 thread_id, request_id, WebServiceWorkerError::ErrorTypeState, |
| 859 std::string(kSetNavigationPreloadHeaderErrorPrefix) + |
| 860 std::string(kNoActiveWorkerErrorMessage))); |
| 861 return; |
| 862 } |
| 840 | 863 |
| 841 std::vector<GURL> urls = {provider_host->document_url(), | 864 std::vector<GURL> urls = {provider_host->document_url(), |
| 842 registration->pattern()}; | 865 registration->pattern()}; |
| 843 if (!ServiceWorkerUtils::AllOriginsMatchAndCanAccessServiceWorkers(urls)) { | 866 if (!ServiceWorkerUtils::AllOriginsMatchAndCanAccessServiceWorkers(urls)) { |
| 844 bad_message::ReceivedBadMessage( | 867 bad_message::ReceivedBadMessage( |
| 845 this, bad_message::SWDH_SET_NAVIGATION_PRELOAD_HEADER_INVALID_ORIGIN); | 868 this, bad_message::SWDH_SET_NAVIGATION_PRELOAD_HEADER_INVALID_ORIGIN); |
| 846 return; | 869 return; |
| 847 } | 870 } |
| 848 | 871 |
| 849 // TODO(falken): Ideally this would match Blink's isValidHTTPHeaderValue. | 872 // TODO(falken): Ideally this would match Blink's isValidHTTPHeaderValue. |
| 850 // Chrome's check is less restrictive: it allows non-latin1 characters. | 873 // Chrome's check is less restrictive: it allows non-latin1 characters. |
| 851 if (!net::HttpUtil::IsValidHeaderValue(value)) { | 874 if (!net::HttpUtil::IsValidHeaderValue(value)) { |
| 852 bad_message::ReceivedBadMessage( | 875 bad_message::ReceivedBadMessage( |
| 853 this, bad_message::SWDH_SET_NAVIGATION_PRELOAD_HEADER_BAD_VALUE); | 876 this, bad_message::SWDH_SET_NAVIGATION_PRELOAD_HEADER_BAD_VALUE); |
| 854 return; | 877 return; |
| 855 } | 878 } |
| 856 | 879 |
| 857 if (!GetContentClient()->browser()->AllowServiceWorker( | 880 if (!GetContentClient()->browser()->AllowServiceWorker( |
| 858 registration->pattern(), provider_host->topmost_frame_url(), | 881 registration->pattern(), provider_host->topmost_frame_url(), |
| 859 resource_context_, render_process_id_, provider_host->frame_id())) { | 882 resource_context_, render_process_id_, provider_host->frame_id())) { |
| 860 Send(new ServiceWorkerMsg_SetNavigationPreloadHeaderError( | 883 Send(new ServiceWorkerMsg_SetNavigationPreloadHeaderError( |
| 861 thread_id, request_id, WebServiceWorkerError::ErrorTypeDisabled, | 884 thread_id, request_id, WebServiceWorkerError::ErrorTypeDisabled, |
| 862 std::string(kSetNavigationPreloadHeaderErrorPrefix) + | 885 std::string(kSetNavigationPreloadHeaderErrorPrefix) + |
| 863 std::string(kUserDeniedPermissionMessage))); | 886 std::string(kUserDeniedPermissionMessage))); |
| 864 return; | 887 return; |
| 865 } | 888 } |
| 866 | 889 |
| 867 // TODO(falken): Write to disk before resolving the promise. | 890 GetContext()->storage()->UpdateNavigationPreloadHeader( |
| 868 registration->SetNavigationPreloadHeader(value); | 891 registration->id(), registration->pattern().GetOrigin(), value, |
| 869 Send(new ServiceWorkerMsg_DidSetNavigationPreloadHeader(thread_id, | 892 base::Bind(&ServiceWorkerDispatcherHost::DidUpdateNavigationPreloadHeader, |
| 870 request_id)); | 893 this, thread_id, request_id, registration->id(), value)); |
| 871 } | 894 } |
| 872 | 895 |
| 873 void ServiceWorkerDispatcherHost::OnPostMessageToWorker( | 896 void ServiceWorkerDispatcherHost::OnPostMessageToWorker( |
| 874 int handle_id, | 897 int handle_id, |
| 875 int provider_id, | 898 int provider_id, |
| 876 const base::string16& message, | 899 const base::string16& message, |
| 877 const url::Origin& source_origin, | 900 const url::Origin& source_origin, |
| 878 const std::vector<int>& sent_message_ports) { | 901 const std::vector<int>& sent_message_ports) { |
| 879 TRACE_EVENT0("ServiceWorker", | 902 TRACE_EVENT0("ServiceWorker", |
| 880 "ServiceWorkerDispatcherHost::OnPostMessageToWorker"); | 903 "ServiceWorkerDispatcherHost::OnPostMessageToWorker"); |
| (...skipping 763 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1644 // TODO(falken): This check can be removed once crbug.com/439697 is fixed. | 1667 // TODO(falken): This check can be removed once crbug.com/439697 is fixed. |
| 1645 if (provider_host->document_url().is_empty()) { | 1668 if (provider_host->document_url().is_empty()) { |
| 1646 *status = ProviderStatus::NO_URL; | 1669 *status = ProviderStatus::NO_URL; |
| 1647 return nullptr; | 1670 return nullptr; |
| 1648 } | 1671 } |
| 1649 | 1672 |
| 1650 *status = ProviderStatus::OK; | 1673 *status = ProviderStatus::OK; |
| 1651 return provider_host; | 1674 return provider_host; |
| 1652 } | 1675 } |
| 1653 | 1676 |
| 1677 void ServiceWorkerDispatcherHost::DidUpdateNavigationPreloadEnabled( |
| 1678 int thread_id, |
| 1679 int request_id, |
| 1680 int registration_id, |
| 1681 bool enable, |
| 1682 ServiceWorkerStatusCode status) { |
| 1683 if (status != SERVICE_WORKER_OK) { |
| 1684 Send(new ServiceWorkerMsg_EnableNavigationPreloadError( |
| 1685 thread_id, request_id, WebServiceWorkerError::ErrorTypeUnknown, |
| 1686 std::string(kEnableNavigationPreloadErrorPrefix) + |
| 1687 std::string(kDatabaseErrorMessage))); |
| 1688 return; |
| 1689 } |
| 1690 ServiceWorkerRegistration* registration = |
| 1691 GetContext()->GetLiveRegistration(registration_id); |
| 1692 if (registration) |
| 1693 registration->EnableNavigationPreload(enable); |
| 1694 Send(new ServiceWorkerMsg_DidEnableNavigationPreload(thread_id, request_id)); |
| 1695 } |
| 1696 |
| 1697 void ServiceWorkerDispatcherHost::DidUpdateNavigationPreloadHeader( |
| 1698 int thread_id, |
| 1699 int request_id, |
| 1700 int registration_id, |
| 1701 const std::string& value, |
| 1702 ServiceWorkerStatusCode status) { |
| 1703 if (status != SERVICE_WORKER_OK) { |
| 1704 Send(new ServiceWorkerMsg_SetNavigationPreloadHeaderError( |
| 1705 thread_id, request_id, WebServiceWorkerError::ErrorTypeUnknown, |
| 1706 std::string(kSetNavigationPreloadHeaderErrorPrefix) + |
| 1707 std::string(kDatabaseErrorMessage))); |
| 1708 return; |
| 1709 } |
| 1710 ServiceWorkerRegistration* registration = |
| 1711 GetContext()->GetLiveRegistration(registration_id); |
| 1712 if (registration) |
| 1713 registration->SetNavigationPreloadHeader(value); |
| 1714 Send(new ServiceWorkerMsg_DidSetNavigationPreloadHeader(thread_id, |
| 1715 request_id)); |
| 1716 } |
| 1717 |
| 1654 void ServiceWorkerDispatcherHost::OnTerminateWorker(int handle_id) { | 1718 void ServiceWorkerDispatcherHost::OnTerminateWorker(int handle_id) { |
| 1655 ServiceWorkerHandle* handle = handles_.Lookup(handle_id); | 1719 ServiceWorkerHandle* handle = handles_.Lookup(handle_id); |
| 1656 if (!handle) { | 1720 if (!handle) { |
| 1657 bad_message::ReceivedBadMessage(this, | 1721 bad_message::ReceivedBadMessage(this, |
| 1658 bad_message::SWDH_TERMINATE_BAD_HANDLE); | 1722 bad_message::SWDH_TERMINATE_BAD_HANDLE); |
| 1659 return; | 1723 return; |
| 1660 } | 1724 } |
| 1661 handle->version()->StopWorker( | 1725 handle->version()->StopWorker( |
| 1662 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 1726 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 1663 } | 1727 } |
| 1664 | 1728 |
| 1665 } // namespace content | 1729 } // namespace content |
| OLD | NEW |