| 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_version.h" | 5 #include "content/browser/service_worker/service_worker_version.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 636 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 647 ServiceWorkerStatusCode status = embedded_worker_->SendMessage( | 647 ServiceWorkerStatusCode status = embedded_worker_->SendMessage( |
| 648 ServiceWorkerMsg_FetchEvent(request_id, request)); | 648 ServiceWorkerMsg_FetchEvent(request_id, request)); |
| 649 if (status != SERVICE_WORKER_OK) { | 649 if (status != SERVICE_WORKER_OK) { |
| 650 fetch_requests_.Remove(request_id); | 650 fetch_requests_.Remove(request_id); |
| 651 RunSoon(base::Bind(&RunErrorFetchCallback, | 651 RunSoon(base::Bind(&RunErrorFetchCallback, |
| 652 fetch_callback, | 652 fetch_callback, |
| 653 SERVICE_WORKER_ERROR_FAILED)); | 653 SERVICE_WORKER_ERROR_FAILED)); |
| 654 } | 654 } |
| 655 } | 655 } |
| 656 | 656 |
| 657 void ServiceWorkerVersion::DispatchNotificationClickEvent( | |
| 658 const StatusCallback& callback, | |
| 659 int64_t persistent_notification_id, | |
| 660 const PlatformNotificationData& notification_data, | |
| 661 int action_index) { | |
| 662 OnBeginEvent(); | |
| 663 DCHECK_EQ(ACTIVATED, status()) << status(); | |
| 664 if (running_status() != RUNNING) { | |
| 665 // Schedule calling this method after starting the worker. | |
| 666 StartWorker(base::Bind( | |
| 667 &RunTaskAfterStartWorker, weak_factory_.GetWeakPtr(), callback, | |
| 668 base::Bind(&self::DispatchNotificationClickEvent, | |
| 669 weak_factory_.GetWeakPtr(), callback, | |
| 670 persistent_notification_id, notification_data, | |
| 671 action_index))); | |
| 672 return; | |
| 673 } | |
| 674 | |
| 675 int request_id = AddRequest( | |
| 676 callback, ¬ification_click_requests_, REQUEST_NOTIFICATION_CLICK, | |
| 677 ServiceWorkerMetrics::EventType::NOTIFICATION_CLICK); | |
| 678 ServiceWorkerStatusCode status = | |
| 679 embedded_worker_->SendMessage(ServiceWorkerMsg_NotificationClickEvent( | |
| 680 request_id, persistent_notification_id, notification_data, | |
| 681 action_index)); | |
| 682 if (status != SERVICE_WORKER_OK) { | |
| 683 notification_click_requests_.Remove(request_id); | |
| 684 RunSoon(base::Bind(callback, status)); | |
| 685 } | |
| 686 } | |
| 687 | |
| 688 void ServiceWorkerVersion::DispatchCrossOriginMessageEvent( | 657 void ServiceWorkerVersion::DispatchCrossOriginMessageEvent( |
| 689 const NavigatorConnectClient& client, | 658 const NavigatorConnectClient& client, |
| 690 const base::string16& message, | 659 const base::string16& message, |
| 691 const std::vector<TransferredMessagePort>& sent_message_ports, | 660 const std::vector<TransferredMessagePort>& sent_message_ports, |
| 692 const StatusCallback& callback) { | 661 const StatusCallback& callback) { |
| 693 OnBeginEvent(); | 662 OnBeginEvent(); |
| 694 // Unlike in the case of DispatchMessageEvent, here the caller is assumed to | 663 // Unlike in the case of DispatchMessageEvent, here the caller is assumed to |
| 695 // have already put all the sent message ports on hold. So no need to do that | 664 // have already put all the sent message ports on hold. So no need to do that |
| 696 // here again. | 665 // here again. |
| 697 | 666 |
| (...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 987 bool handled = true; | 956 bool handled = true; |
| 988 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerVersion, message) | 957 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerVersion, message) |
| 989 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetClients, | 958 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetClients, |
| 990 OnGetClients) | 959 OnGetClients) |
| 991 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ActivateEventFinished, | 960 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ActivateEventFinished, |
| 992 OnActivateEventFinished) | 961 OnActivateEventFinished) |
| 993 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_InstallEventFinished, | 962 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_InstallEventFinished, |
| 994 OnInstallEventFinished) | 963 OnInstallEventFinished) |
| 995 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FetchEventFinished, | 964 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FetchEventFinished, |
| 996 OnFetchEventFinished) | 965 OnFetchEventFinished) |
| 997 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_NotificationClickEventFinished, | |
| 998 OnNotificationClickEventFinished) | |
| 999 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_OpenWindow, | 966 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_OpenWindow, |
| 1000 OnOpenWindow) | 967 OnOpenWindow) |
| 1001 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SetCachedMetadata, | 968 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SetCachedMetadata, |
| 1002 OnSetCachedMetadata) | 969 OnSetCachedMetadata) |
| 1003 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ClearCachedMetadata, | 970 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ClearCachedMetadata, |
| 1004 OnClearCachedMetadata) | 971 OnClearCachedMetadata) |
| 1005 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToClient, | 972 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToClient, |
| 1006 OnPostMessageToClient) | 973 OnPostMessageToClient) |
| 1007 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FocusClient, | 974 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FocusClient, |
| 1008 OnFocusClient) | 975 OnFocusClient) |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1158 handled); | 1125 handled); |
| 1159 | 1126 |
| 1160 ServiceWorkerMetrics::RecordFetchEventTime( | 1127 ServiceWorkerMetrics::RecordFetchEventTime( |
| 1161 result, base::TimeTicks::Now() - request->start_time); | 1128 result, base::TimeTicks::Now() - request->start_time); |
| 1162 | 1129 |
| 1163 scoped_refptr<ServiceWorkerVersion> protect(this); | 1130 scoped_refptr<ServiceWorkerVersion> protect(this); |
| 1164 request->callback.Run(SERVICE_WORKER_OK, result, response); | 1131 request->callback.Run(SERVICE_WORKER_OK, result, response); |
| 1165 RemoveCallbackAndStopIfRedundant(&fetch_requests_, request_id); | 1132 RemoveCallbackAndStopIfRedundant(&fetch_requests_, request_id); |
| 1166 } | 1133 } |
| 1167 | 1134 |
| 1168 void ServiceWorkerVersion::OnNotificationClickEventFinished( | |
| 1169 int request_id) { | |
| 1170 TRACE_EVENT1("ServiceWorker", | |
| 1171 "ServiceWorkerVersion::OnNotificationClickEventFinished", | |
| 1172 "Request id", request_id); | |
| 1173 PendingRequest<StatusCallback>* request = | |
| 1174 notification_click_requests_.Lookup(request_id); | |
| 1175 if (!request) { | |
| 1176 NOTREACHED() << "Got unexpected message: " << request_id; | |
| 1177 return; | |
| 1178 } | |
| 1179 | |
| 1180 ServiceWorkerMetrics::RecordEventDuration( | |
| 1181 request->event_type, base::TimeTicks::Now() - request->start_time); | |
| 1182 | |
| 1183 scoped_refptr<ServiceWorkerVersion> protect(this); | |
| 1184 request->callback.Run(SERVICE_WORKER_OK); | |
| 1185 RemoveCallbackAndStopIfRedundant(¬ification_click_requests_, request_id); | |
| 1186 } | |
| 1187 | |
| 1188 void ServiceWorkerVersion::OnSimpleEventResponse( | 1135 void ServiceWorkerVersion::OnSimpleEventResponse( |
| 1189 int request_id, | 1136 int request_id, |
| 1190 blink::WebServiceWorkerEventResult result) { | 1137 blink::WebServiceWorkerEventResult result) { |
| 1191 // Copy error callback before calling FinishRequest. | 1138 // Copy error callback before calling FinishRequest. |
| 1192 PendingRequest<StatusCallback>* request = custom_requests_.Lookup(request_id); | 1139 PendingRequest<StatusCallback>* request = custom_requests_.Lookup(request_id); |
| 1193 DCHECK(request) << "Invalid request id"; | 1140 DCHECK(request) << "Invalid request id"; |
| 1194 StatusCallback callback = request->callback; | 1141 StatusCallback callback = request->callback; |
| 1195 | 1142 |
| 1196 FinishRequest(request_id); | 1143 FinishRequest(request_id); |
| 1197 | 1144 |
| (...skipping 531 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1729 !stop_callbacks_.empty()) { | 1676 !stop_callbacks_.empty()) { |
| 1730 return; | 1677 return; |
| 1731 } | 1678 } |
| 1732 | 1679 |
| 1733 embedded_worker_->StopIfIdle(); | 1680 embedded_worker_->StopIfIdle(); |
| 1734 } | 1681 } |
| 1735 | 1682 |
| 1736 bool ServiceWorkerVersion::HasInflightRequests() const { | 1683 bool ServiceWorkerVersion::HasInflightRequests() const { |
| 1737 return !activate_requests_.IsEmpty() || !install_requests_.IsEmpty() || | 1684 return !activate_requests_.IsEmpty() || !install_requests_.IsEmpty() || |
| 1738 !fetch_requests_.IsEmpty() || | 1685 !fetch_requests_.IsEmpty() || |
| 1739 !notification_click_requests_.IsEmpty() || | |
| 1740 !custom_requests_.IsEmpty() || !streaming_url_request_jobs_.empty(); | 1686 !custom_requests_.IsEmpty() || !streaming_url_request_jobs_.empty(); |
| 1741 } | 1687 } |
| 1742 | 1688 |
| 1743 void ServiceWorkerVersion::RecordStartWorkerResult( | 1689 void ServiceWorkerVersion::RecordStartWorkerResult( |
| 1744 ServiceWorkerStatusCode status) { | 1690 ServiceWorkerStatusCode status) { |
| 1745 base::TimeTicks start_time = start_time_; | 1691 base::TimeTicks start_time = start_time_; |
| 1746 ClearTick(&start_time_); | 1692 ClearTick(&start_time_); |
| 1747 | 1693 |
| 1748 if (context_) | 1694 if (context_) |
| 1749 context_->UpdateVersionFailureCount(version_id_, status); | 1695 context_->UpdateVersionFailureCount(version_id_, status); |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1836 return RunIDMapCallback(&activate_requests_, info.id, | 1782 return RunIDMapCallback(&activate_requests_, info.id, |
| 1837 SERVICE_WORKER_ERROR_TIMEOUT); | 1783 SERVICE_WORKER_ERROR_TIMEOUT); |
| 1838 case REQUEST_INSTALL: | 1784 case REQUEST_INSTALL: |
| 1839 return RunIDMapCallback(&install_requests_, info.id, | 1785 return RunIDMapCallback(&install_requests_, info.id, |
| 1840 SERVICE_WORKER_ERROR_TIMEOUT); | 1786 SERVICE_WORKER_ERROR_TIMEOUT); |
| 1841 case REQUEST_FETCH: | 1787 case REQUEST_FETCH: |
| 1842 return RunIDMapCallback( | 1788 return RunIDMapCallback( |
| 1843 &fetch_requests_, info.id, SERVICE_WORKER_ERROR_TIMEOUT, | 1789 &fetch_requests_, info.id, SERVICE_WORKER_ERROR_TIMEOUT, |
| 1844 /* The other args are ignored for non-OK status. */ | 1790 /* The other args are ignored for non-OK status. */ |
| 1845 SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, ServiceWorkerResponse()); | 1791 SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, ServiceWorkerResponse()); |
| 1846 case REQUEST_NOTIFICATION_CLICK: | |
| 1847 return RunIDMapCallback(¬ification_click_requests_, info.id, | |
| 1848 SERVICE_WORKER_ERROR_TIMEOUT); | |
| 1849 case REQUEST_CUSTOM: | 1792 case REQUEST_CUSTOM: |
| 1850 return RunIDMapCallback(&custom_requests_, info.id, | 1793 return RunIDMapCallback(&custom_requests_, info.id, |
| 1851 SERVICE_WORKER_ERROR_TIMEOUT); | 1794 SERVICE_WORKER_ERROR_TIMEOUT); |
| 1852 case NUM_REQUEST_TYPES: | 1795 case NUM_REQUEST_TYPES: |
| 1853 break; | 1796 break; |
| 1854 } | 1797 } |
| 1855 NOTREACHED() << "Got unexpected request type: " << info.type; | 1798 NOTREACHED() << "Got unexpected request type: " << info.type; |
| 1856 return false; | 1799 return false; |
| 1857 } | 1800 } |
| 1858 | 1801 |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1957 // Let all message callbacks fail (this will also fire and clear all | 1900 // Let all message callbacks fail (this will also fire and clear all |
| 1958 // callbacks for events). | 1901 // callbacks for events). |
| 1959 // TODO(kinuko): Consider if we want to add queue+resend mechanism here. | 1902 // TODO(kinuko): Consider if we want to add queue+resend mechanism here. |
| 1960 RunIDMapCallbacks(&activate_requests_, | 1903 RunIDMapCallbacks(&activate_requests_, |
| 1961 SERVICE_WORKER_ERROR_ACTIVATE_WORKER_FAILED); | 1904 SERVICE_WORKER_ERROR_ACTIVATE_WORKER_FAILED); |
| 1962 RunIDMapCallbacks(&install_requests_, | 1905 RunIDMapCallbacks(&install_requests_, |
| 1963 SERVICE_WORKER_ERROR_INSTALL_WORKER_FAILED); | 1906 SERVICE_WORKER_ERROR_INSTALL_WORKER_FAILED); |
| 1964 RunIDMapCallbacks(&fetch_requests_, SERVICE_WORKER_ERROR_FAILED, | 1907 RunIDMapCallbacks(&fetch_requests_, SERVICE_WORKER_ERROR_FAILED, |
| 1965 SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, | 1908 SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK, |
| 1966 ServiceWorkerResponse()); | 1909 ServiceWorkerResponse()); |
| 1967 RunIDMapCallbacks(¬ification_click_requests_, SERVICE_WORKER_ERROR_FAILED); | |
| 1968 RunIDMapCallbacks(&custom_requests_, SERVICE_WORKER_ERROR_FAILED); | 1910 RunIDMapCallbacks(&custom_requests_, SERVICE_WORKER_ERROR_FAILED); |
| 1969 | 1911 |
| 1970 // Close all mojo services. This will also fire and clear all callbacks | 1912 // Close all mojo services. This will also fire and clear all callbacks |
| 1971 // for messages that are still outstanding for those services. | 1913 // for messages that are still outstanding for those services. |
| 1972 mojo_services_.clear(); | 1914 mojo_services_.clear(); |
| 1973 | 1915 |
| 1974 // TODO(falken): Call SWURLRequestJob::ClearStream here? | 1916 // TODO(falken): Call SWURLRequestJob::ClearStream here? |
| 1975 streaming_url_request_jobs_.clear(); | 1917 streaming_url_request_jobs_.clear(); |
| 1976 | 1918 |
| 1977 FOR_EACH_OBSERVER(Listener, listeners_, OnRunningStateChanged(this)); | 1919 FOR_EACH_OBSERVER(Listener, listeners_, OnRunningStateChanged(this)); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 1989 void ServiceWorkerVersion::OnBeginEvent() { | 1931 void ServiceWorkerVersion::OnBeginEvent() { |
| 1990 if (should_exclude_from_uma_ || running_status() != RUNNING || | 1932 if (should_exclude_from_uma_ || running_status() != RUNNING || |
| 1991 idle_time_.is_null()) { | 1933 idle_time_.is_null()) { |
| 1992 return; | 1934 return; |
| 1993 } | 1935 } |
| 1994 ServiceWorkerMetrics::RecordTimeBetweenEvents(base::TimeTicks::Now() - | 1936 ServiceWorkerMetrics::RecordTimeBetweenEvents(base::TimeTicks::Now() - |
| 1995 idle_time_); | 1937 idle_time_); |
| 1996 } | 1938 } |
| 1997 | 1939 |
| 1998 } // namespace content | 1940 } // namespace content |
| OLD | NEW |