Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(478)

Side by Side Diff: content/browser/service_worker/service_worker_dispatcher_host.cc

Issue 2451373003: service worker: Implement NavigationPreloadManager.setHeaderValue (Closed)
Patch Set: revise Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 "No URL is associated with the caller's document."; 48 "No URL is associated with the caller's document.";
49 const char kShutdownErrorMessage[] = 49 const char kShutdownErrorMessage[] =
50 "The Service Worker system has shutdown."; 50 "The Service Worker system has shutdown.";
51 const char kUserDeniedPermissionMessage[] = 51 const char kUserDeniedPermissionMessage[] =
52 "The user denied permission to use Service Worker."; 52 "The user denied permission to use Service Worker.";
53 const char kInvalidStateErrorMessage[] = "The object is in an invalid state."; 53 const char kInvalidStateErrorMessage[] = "The object is in an invalid state.";
54 const char kEnableNavigationPreloadErrorPrefix[] = 54 const char kEnableNavigationPreloadErrorPrefix[] =
55 "Failed to enable or disable navigation preload: "; 55 "Failed to enable or disable navigation preload: ";
56 const char kGetNavigationPreloadStateErrorPrefix[] = 56 const char kGetNavigationPreloadStateErrorPrefix[] =
57 "Failed to get navigation preload state: "; 57 "Failed to get navigation preload state: ";
58 const char kSetNavigationPreloadHeaderErrorPrefix[] =
59 "Failed to set navigation preload header: ";
58 60
59 const uint32_t kFilteredMessageClasses[] = { 61 const uint32_t kFilteredMessageClasses[] = {
60 ServiceWorkerMsgStart, EmbeddedWorkerMsgStart, 62 ServiceWorkerMsgStart, EmbeddedWorkerMsgStart,
61 }; 63 };
62 64
63 void RunSoon(const base::Closure& callback) { 65 void RunSoon(const base::Closure& callback) {
64 if (!callback.is_null()) 66 if (!callback.is_null())
65 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); 67 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback);
66 } 68 }
67 69
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 OnDecrementServiceWorkerRefCount) 187 OnDecrementServiceWorkerRefCount)
186 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_IncrementRegistrationRefCount, 188 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_IncrementRegistrationRefCount,
187 OnIncrementRegistrationRefCount) 189 OnIncrementRegistrationRefCount)
188 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_DecrementRegistrationRefCount, 190 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_DecrementRegistrationRefCount,
189 OnDecrementRegistrationRefCount) 191 OnDecrementRegistrationRefCount)
190 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_TerminateWorker, OnTerminateWorker) 192 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_TerminateWorker, OnTerminateWorker)
191 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_EnableNavigationPreload, 193 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_EnableNavigationPreload,
192 OnEnableNavigationPreload) 194 OnEnableNavigationPreload)
193 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetNavigationPreloadState, 195 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetNavigationPreloadState,
194 OnGetNavigationPreloadState) 196 OnGetNavigationPreloadState)
197 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SetNavigationPreloadHeader,
198 OnSetNavigationPreloadHeader)
195 IPC_MESSAGE_UNHANDLED(handled = false) 199 IPC_MESSAGE_UNHANDLED(handled = false)
196 IPC_END_MESSAGE_MAP() 200 IPC_END_MESSAGE_MAP()
197 201
198 if (!handled && GetContext()) { 202 if (!handled && GetContext()) {
199 handled = GetContext()->embedded_worker_registry()->OnMessageReceived( 203 handled = GetContext()->embedded_worker_registry()->OnMessageReceived(
200 message, render_process_id_); 204 message, render_process_id_);
201 if (!handled) 205 if (!handled)
202 bad_message::ReceivedBadMessage(this, bad_message::SWDH_NOT_HANDLED); 206 bad_message::ReceivedBadMessage(this, bad_message::SWDH_NOT_HANDLED);
203 } 207 }
204 208
(...skipping 576 matching lines...) Expand 10 before | Expand all | Expand 10 after
781 registration->pattern(), provider_host->topmost_frame_url(), 785 registration->pattern(), provider_host->topmost_frame_url(),
782 resource_context_, render_process_id_, provider_host->frame_id())) { 786 resource_context_, render_process_id_, provider_host->frame_id())) {
783 Send(new ServiceWorkerMsg_GetNavigationPreloadStateError( 787 Send(new ServiceWorkerMsg_GetNavigationPreloadStateError(
784 thread_id, request_id, WebServiceWorkerError::ErrorTypeDisabled, 788 thread_id, request_id, WebServiceWorkerError::ErrorTypeDisabled,
785 std::string(kGetNavigationPreloadStateErrorPrefix) + 789 std::string(kGetNavigationPreloadStateErrorPrefix) +
786 std::string(kUserDeniedPermissionMessage))); 790 std::string(kUserDeniedPermissionMessage)));
787 return; 791 return;
788 } 792 }
789 793
790 Send(new ServiceWorkerMsg_DidGetNavigationPreloadState( 794 Send(new ServiceWorkerMsg_DidGetNavigationPreloadState(
791 thread_id, request_id, registration->is_navigation_preload_enabled())); 795 thread_id, request_id,
796 NavigationPreloadState(registration->is_navigation_preload_enabled(),
797 registration->navigation_preload_header())));
798 }
799
800 void ServiceWorkerDispatcherHost::OnSetNavigationPreloadHeader(
801 int thread_id,
802 int request_id,
803 int provider_id,
804 int64_t registration_id,
805 const std::string& value) {
806 ProviderStatus provider_status;
807 ServiceWorkerProviderHost* provider_host =
808 GetProviderHostForRequest(&provider_status, provider_id);
809 switch (provider_status) {
810 case ProviderStatus::NO_CONTEXT: // fallthrough
811 case ProviderStatus::DEAD_HOST:
812 Send(new ServiceWorkerMsg_SetNavigationPreloadHeaderError(
813 thread_id, request_id, WebServiceWorkerError::ErrorTypeAbort,
814 std::string(kSetNavigationPreloadHeaderErrorPrefix) +
815 std::string(kShutdownErrorMessage)));
816 return;
817 case ProviderStatus::NO_HOST:
818 bad_message::ReceivedBadMessage(
819 this, bad_message::SWDH_SET_NAVIGATION_PRELOAD_HEADER_NO_HOST);
820 return;
821 case ProviderStatus::NO_URL:
822 Send(new ServiceWorkerMsg_SetNavigationPreloadHeaderError(
823 thread_id, request_id, WebServiceWorkerError::ErrorTypeSecurity,
824 std::string(kSetNavigationPreloadHeaderErrorPrefix) +
825 std::string(kNoDocumentURLErrorMessage)));
826 return;
827 case ProviderStatus::OK:
828 break;
829 }
830
831 ServiceWorkerRegistration* registration =
832 GetContext()->GetLiveRegistration(registration_id);
833 if (!registration) {
834 // |registration| must be alive because a renderer retains a registration
835 // reference at this point.
836 bad_message::ReceivedBadMessage(
837 this,
838 bad_message::SWDH_SET_NAVIGATION_PRELOAD_HEADER_BAD_REGISTRATION_ID);
839 return;
840 }
841
842 std::vector<GURL> urls = {provider_host->document_url(),
843 registration->pattern()};
844 if (!ServiceWorkerUtils::AllOriginsMatchAndCanAccessServiceWorkers(urls)) {
845 bad_message::ReceivedBadMessage(
846 this, bad_message::SWDH_SET_NAVIGATION_PRELOAD_HEADER_INVALID_ORIGIN);
847 return;
848 }
849
850 if (!GetContentClient()->browser()->AllowServiceWorker(
851 registration->pattern(), provider_host->topmost_frame_url(),
852 resource_context_, render_process_id_, provider_host->frame_id())) {
853 Send(new ServiceWorkerMsg_SetNavigationPreloadHeaderError(
854 thread_id, request_id, WebServiceWorkerError::ErrorTypeDisabled,
855 std::string(kSetNavigationPreloadHeaderErrorPrefix) +
856 std::string(kUserDeniedPermissionMessage)));
857 return;
858 }
859
860 // TODO(falken): Write to disk before resolving the promise.
861 registration->SetNavigationPreloadHeader(value);
862 Send(new ServiceWorkerMsg_DidSetNavigationPreloadHeader(thread_id,
863 request_id));
792 } 864 }
793 865
794 void ServiceWorkerDispatcherHost::OnPostMessageToWorker( 866 void ServiceWorkerDispatcherHost::OnPostMessageToWorker(
795 int handle_id, 867 int handle_id,
796 int provider_id, 868 int provider_id,
797 const base::string16& message, 869 const base::string16& message,
798 const url::Origin& source_origin, 870 const url::Origin& source_origin,
799 const std::vector<int>& sent_message_ports) { 871 const std::vector<int>& sent_message_ports) {
800 TRACE_EVENT0("ServiceWorker", 872 TRACE_EVENT0("ServiceWorker",
801 "ServiceWorkerDispatcherHost::OnPostMessageToWorker"); 873 "ServiceWorkerDispatcherHost::OnPostMessageToWorker");
(...skipping 775 matching lines...) Expand 10 before | Expand all | Expand 10 after
1577 if (!handle) { 1649 if (!handle) {
1578 bad_message::ReceivedBadMessage(this, 1650 bad_message::ReceivedBadMessage(this,
1579 bad_message::SWDH_TERMINATE_BAD_HANDLE); 1651 bad_message::SWDH_TERMINATE_BAD_HANDLE);
1580 return; 1652 return;
1581 } 1653 }
1582 handle->version()->StopWorker( 1654 handle->version()->StopWorker(
1583 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); 1655 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
1584 } 1656 }
1585 1657
1586 } // namespace content 1658 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698