Chromium Code Reviews| 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 if (!registration->active_version()) { | |
|
horo
2016/11/22 14:28:23
Please add comment about this behavior and a link
falken
2016/11/24 07:10:00
Done.
| |
| 714 Send(new ServiceWorkerMsg_EnableNavigationPreloadError( | |
| 715 thread_id, request_id, WebServiceWorkerError::ErrorTypeState, | |
| 716 std::string(kEnableNavigationPreloadErrorPrefix) + | |
| 717 std::string(kNoActiveWorkerErrorMessage))); | |
| 718 return; | |
| 719 } | |
| 710 | 720 |
| 711 std::vector<GURL> urls = {provider_host->document_url(), | 721 std::vector<GURL> urls = {provider_host->document_url(), |
| 712 registration->pattern()}; | 722 registration->pattern()}; |
| 713 if (!ServiceWorkerUtils::AllOriginsMatchAndCanAccessServiceWorkers(urls)) { | 723 if (!ServiceWorkerUtils::AllOriginsMatchAndCanAccessServiceWorkers(urls)) { |
| 714 bad_message::ReceivedBadMessage( | 724 bad_message::ReceivedBadMessage( |
| 715 this, bad_message::SWDH_ENABLE_NAVIGATION_PRELOAD_INVALID_ORIGIN); | 725 this, bad_message::SWDH_ENABLE_NAVIGATION_PRELOAD_INVALID_ORIGIN); |
| 716 return; | 726 return; |
| 717 } | 727 } |
| 718 | 728 |
| 719 if (!GetContentClient()->browser()->AllowServiceWorker( | 729 if (!GetContentClient()->browser()->AllowServiceWorker( |
| 720 registration->pattern(), provider_host->topmost_frame_url(), | 730 registration->pattern(), provider_host->topmost_frame_url(), |
| 721 resource_context_, render_process_id_, provider_host->frame_id())) { | 731 resource_context_, render_process_id_, provider_host->frame_id())) { |
| 722 Send(new ServiceWorkerMsg_EnableNavigationPreloadError( | 732 Send(new ServiceWorkerMsg_EnableNavigationPreloadError( |
| 723 thread_id, request_id, WebServiceWorkerError::ErrorTypeDisabled, | 733 thread_id, request_id, WebServiceWorkerError::ErrorTypeDisabled, |
| 724 std::string(kEnableNavigationPreloadErrorPrefix) + | 734 std::string(kEnableNavigationPreloadErrorPrefix) + |
| 725 std::string(kUserDeniedPermissionMessage))); | 735 std::string(kUserDeniedPermissionMessage))); |
| 726 return; | 736 return; |
| 727 } | 737 } |
| 728 | 738 |
| 729 // TODO(falken): Write to disk before resolving the promise. | 739 GetContext()->storage()->UpdateNavigationPreloadEnabled( |
| 730 registration->EnableNavigationPreload(enable); | 740 registration->id(), registration->pattern().GetOrigin(), enable, |
| 731 Send(new ServiceWorkerMsg_DidEnableNavigationPreload(thread_id, request_id)); | 741 base::Bind( |
| 742 &ServiceWorkerDispatcherHost::DidUpdateNavigationPreloadEnabled, this, | |
| 743 thread_id, request_id, registration->id(), enable)); | |
| 732 } | 744 } |
| 733 | 745 |
| 734 void ServiceWorkerDispatcherHost::OnGetNavigationPreloadState( | 746 void ServiceWorkerDispatcherHost::OnGetNavigationPreloadState( |
| 735 int thread_id, | 747 int thread_id, |
| 736 int request_id, | 748 int request_id, |
| 737 int provider_id, | 749 int provider_id, |
| 738 int64_t registration_id) { | 750 int64_t registration_id) { |
| 739 ProviderStatus provider_status; | 751 ProviderStatus provider_status; |
| 740 ServiceWorkerProviderHost* provider_host = | 752 ServiceWorkerProviderHost* provider_host = |
| 741 GetProviderHostForRequest(&provider_status, provider_id); | 753 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(), | 796 registration->pattern(), provider_host->topmost_frame_url(), |
| 785 resource_context_, render_process_id_, provider_host->frame_id())) { | 797 resource_context_, render_process_id_, provider_host->frame_id())) { |
| 786 Send(new ServiceWorkerMsg_GetNavigationPreloadStateError( | 798 Send(new ServiceWorkerMsg_GetNavigationPreloadStateError( |
| 787 thread_id, request_id, WebServiceWorkerError::ErrorTypeDisabled, | 799 thread_id, request_id, WebServiceWorkerError::ErrorTypeDisabled, |
| 788 std::string(kGetNavigationPreloadStateErrorPrefix) + | 800 std::string(kGetNavigationPreloadStateErrorPrefix) + |
| 789 std::string(kUserDeniedPermissionMessage))); | 801 std::string(kUserDeniedPermissionMessage))); |
| 790 return; | 802 return; |
| 791 } | 803 } |
| 792 | 804 |
| 793 Send(new ServiceWorkerMsg_DidGetNavigationPreloadState( | 805 Send(new ServiceWorkerMsg_DidGetNavigationPreloadState( |
| 794 thread_id, request_id, | 806 thread_id, request_id, registration->navigation_preload_state())); |
| 795 NavigationPreloadState(registration->is_navigation_preload_enabled(), | |
| 796 registration->navigation_preload_header()))); | |
| 797 } | 807 } |
| 798 | 808 |
| 799 void ServiceWorkerDispatcherHost::OnSetNavigationPreloadHeader( | 809 void ServiceWorkerDispatcherHost::OnSetNavigationPreloadHeader( |
| 800 int thread_id, | 810 int thread_id, |
| 801 int request_id, | 811 int request_id, |
| 802 int provider_id, | 812 int provider_id, |
| 803 int64_t registration_id, | 813 int64_t registration_id, |
| 804 const std::string& value) { | 814 const std::string& value) { |
| 805 ProviderStatus provider_status; | 815 ProviderStatus provider_status; |
| 806 ServiceWorkerProviderHost* provider_host = | 816 ServiceWorkerProviderHost* provider_host = |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 830 ServiceWorkerRegistration* registration = | 840 ServiceWorkerRegistration* registration = |
| 831 GetContext()->GetLiveRegistration(registration_id); | 841 GetContext()->GetLiveRegistration(registration_id); |
| 832 if (!registration) { | 842 if (!registration) { |
| 833 // |registration| must be alive because a renderer retains a registration | 843 // |registration| must be alive because a renderer retains a registration |
| 834 // reference at this point. | 844 // reference at this point. |
| 835 bad_message::ReceivedBadMessage( | 845 bad_message::ReceivedBadMessage( |
| 836 this, | 846 this, |
| 837 bad_message::SWDH_SET_NAVIGATION_PRELOAD_HEADER_BAD_REGISTRATION_ID); | 847 bad_message::SWDH_SET_NAVIGATION_PRELOAD_HEADER_BAD_REGISTRATION_ID); |
| 838 return; | 848 return; |
| 839 } | 849 } |
| 850 if (!registration->active_version()) { | |
|
horo
2016/11/22 14:28:23
ditto
falken
2016/11/24 07:10:00
Done.
| |
| 851 Send(new ServiceWorkerMsg_SetNavigationPreloadHeaderError( | |
| 852 thread_id, request_id, WebServiceWorkerError::ErrorTypeState, | |
| 853 std::string(kSetNavigationPreloadHeaderErrorPrefix) + | |
| 854 std::string(kNoActiveWorkerErrorMessage))); | |
| 855 return; | |
| 856 } | |
| 840 | 857 |
| 841 std::vector<GURL> urls = {provider_host->document_url(), | 858 std::vector<GURL> urls = {provider_host->document_url(), |
| 842 registration->pattern()}; | 859 registration->pattern()}; |
| 843 if (!ServiceWorkerUtils::AllOriginsMatchAndCanAccessServiceWorkers(urls)) { | 860 if (!ServiceWorkerUtils::AllOriginsMatchAndCanAccessServiceWorkers(urls)) { |
| 844 bad_message::ReceivedBadMessage( | 861 bad_message::ReceivedBadMessage( |
| 845 this, bad_message::SWDH_SET_NAVIGATION_PRELOAD_HEADER_INVALID_ORIGIN); | 862 this, bad_message::SWDH_SET_NAVIGATION_PRELOAD_HEADER_INVALID_ORIGIN); |
| 846 return; | 863 return; |
| 847 } | 864 } |
| 848 | 865 |
| 849 // TODO(falken): Ideally this would match Blink's isValidHTTPHeaderValue. | 866 // TODO(falken): Ideally this would match Blink's isValidHTTPHeaderValue. |
| 850 // Chrome's check is less restrictive: it allows non-latin1 characters. | 867 // Chrome's check is less restrictive: it allows non-latin1 characters. |
| 851 if (!net::HttpUtil::IsValidHeaderValue(value)) { | 868 if (!net::HttpUtil::IsValidHeaderValue(value)) { |
| 852 bad_message::ReceivedBadMessage( | 869 bad_message::ReceivedBadMessage( |
| 853 this, bad_message::SWDH_SET_NAVIGATION_PRELOAD_HEADER_BAD_VALUE); | 870 this, bad_message::SWDH_SET_NAVIGATION_PRELOAD_HEADER_BAD_VALUE); |
| 854 return; | 871 return; |
| 855 } | 872 } |
| 856 | 873 |
| 857 if (!GetContentClient()->browser()->AllowServiceWorker( | 874 if (!GetContentClient()->browser()->AllowServiceWorker( |
| 858 registration->pattern(), provider_host->topmost_frame_url(), | 875 registration->pattern(), provider_host->topmost_frame_url(), |
| 859 resource_context_, render_process_id_, provider_host->frame_id())) { | 876 resource_context_, render_process_id_, provider_host->frame_id())) { |
| 860 Send(new ServiceWorkerMsg_SetNavigationPreloadHeaderError( | 877 Send(new ServiceWorkerMsg_SetNavigationPreloadHeaderError( |
| 861 thread_id, request_id, WebServiceWorkerError::ErrorTypeDisabled, | 878 thread_id, request_id, WebServiceWorkerError::ErrorTypeDisabled, |
| 862 std::string(kSetNavigationPreloadHeaderErrorPrefix) + | 879 std::string(kSetNavigationPreloadHeaderErrorPrefix) + |
| 863 std::string(kUserDeniedPermissionMessage))); | 880 std::string(kUserDeniedPermissionMessage))); |
| 864 return; | 881 return; |
| 865 } | 882 } |
| 866 | 883 |
| 867 // TODO(falken): Write to disk before resolving the promise. | 884 GetContext()->storage()->UpdateNavigationPreloadHeader( |
| 868 registration->SetNavigationPreloadHeader(value); | 885 registration->id(), registration->pattern().GetOrigin(), value, |
| 869 Send(new ServiceWorkerMsg_DidSetNavigationPreloadHeader(thread_id, | 886 base::Bind(&ServiceWorkerDispatcherHost::DidUpdateNavigationPreloadHeader, |
| 870 request_id)); | 887 this, thread_id, request_id, registration->id(), value)); |
| 871 } | 888 } |
| 872 | 889 |
| 873 void ServiceWorkerDispatcherHost::OnPostMessageToWorker( | 890 void ServiceWorkerDispatcherHost::OnPostMessageToWorker( |
| 874 int handle_id, | 891 int handle_id, |
| 875 int provider_id, | 892 int provider_id, |
| 876 const base::string16& message, | 893 const base::string16& message, |
| 877 const url::Origin& source_origin, | 894 const url::Origin& source_origin, |
| 878 const std::vector<int>& sent_message_ports) { | 895 const std::vector<int>& sent_message_ports) { |
| 879 TRACE_EVENT0("ServiceWorker", | 896 TRACE_EVENT0("ServiceWorker", |
| 880 "ServiceWorkerDispatcherHost::OnPostMessageToWorker"); | 897 "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. | 1661 // TODO(falken): This check can be removed once crbug.com/439697 is fixed. |
| 1645 if (provider_host->document_url().is_empty()) { | 1662 if (provider_host->document_url().is_empty()) { |
| 1646 *status = ProviderStatus::NO_URL; | 1663 *status = ProviderStatus::NO_URL; |
| 1647 return nullptr; | 1664 return nullptr; |
| 1648 } | 1665 } |
| 1649 | 1666 |
| 1650 *status = ProviderStatus::OK; | 1667 *status = ProviderStatus::OK; |
| 1651 return provider_host; | 1668 return provider_host; |
| 1652 } | 1669 } |
| 1653 | 1670 |
| 1671 void ServiceWorkerDispatcherHost::DidUpdateNavigationPreloadEnabled( | |
| 1672 int thread_id, | |
| 1673 int request_id, | |
| 1674 int registration_id, | |
| 1675 bool enable, | |
| 1676 ServiceWorkerStatusCode status) { | |
| 1677 if (status != SERVICE_WORKER_OK) { | |
| 1678 Send(new ServiceWorkerMsg_EnableNavigationPreloadError( | |
| 1679 thread_id, request_id, WebServiceWorkerError::ErrorTypeUnknown, | |
| 1680 std::string(kEnableNavigationPreloadErrorPrefix) + | |
| 1681 std::string(kDatabaseErrorMessage))); | |
| 1682 return; | |
| 1683 } | |
| 1684 ServiceWorkerRegistration* registration = | |
| 1685 GetContext()->GetLiveRegistration(registration_id); | |
| 1686 if (registration) | |
| 1687 registration->EnableNavigationPreload(enable); | |
| 1688 Send(new ServiceWorkerMsg_DidEnableNavigationPreload(thread_id, request_id)); | |
| 1689 } | |
| 1690 | |
| 1691 void ServiceWorkerDispatcherHost::DidUpdateNavigationPreloadHeader( | |
| 1692 int thread_id, | |
| 1693 int request_id, | |
| 1694 int registration_id, | |
| 1695 const std::string& value, | |
| 1696 ServiceWorkerStatusCode status) { | |
| 1697 if (status != SERVICE_WORKER_OK) { | |
| 1698 Send(new ServiceWorkerMsg_SetNavigationPreloadHeaderError( | |
| 1699 thread_id, request_id, WebServiceWorkerError::ErrorTypeUnknown, | |
| 1700 std::string(kSetNavigationPreloadHeaderErrorPrefix) + | |
| 1701 std::string(kDatabaseErrorMessage))); | |
| 1702 return; | |
| 1703 } | |
| 1704 ServiceWorkerRegistration* registration = | |
| 1705 GetContext()->GetLiveRegistration(registration_id); | |
| 1706 if (registration) | |
| 1707 registration->SetNavigationPreloadHeader(value); | |
| 1708 Send(new ServiceWorkerMsg_DidSetNavigationPreloadHeader(thread_id, | |
| 1709 request_id)); | |
| 1710 } | |
| 1711 | |
| 1654 void ServiceWorkerDispatcherHost::OnTerminateWorker(int handle_id) { | 1712 void ServiceWorkerDispatcherHost::OnTerminateWorker(int handle_id) { |
| 1655 ServiceWorkerHandle* handle = handles_.Lookup(handle_id); | 1713 ServiceWorkerHandle* handle = handles_.Lookup(handle_id); |
| 1656 if (!handle) { | 1714 if (!handle) { |
| 1657 bad_message::ReceivedBadMessage(this, | 1715 bad_message::ReceivedBadMessage(this, |
| 1658 bad_message::SWDH_TERMINATE_BAD_HANDLE); | 1716 bad_message::SWDH_TERMINATE_BAD_HANDLE); |
| 1659 return; | 1717 return; |
| 1660 } | 1718 } |
| 1661 handle->version()->StopWorker( | 1719 handle->version()->StopWorker( |
| 1662 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 1720 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 1663 } | 1721 } |
| 1664 | 1722 |
| 1665 } // namespace content | 1723 } // namespace content |
| OLD | NEW |