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 "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
167 ServiceWorkerStatusCode status) { | 167 ServiceWorkerStatusCode status) { |
168 callback.Run(status, false /* accept_connection */); | 168 callback.Run(status, false /* accept_connection */); |
169 } | 169 } |
170 | 170 |
171 void RunErrorSendStashedPortsCallback( | 171 void RunErrorSendStashedPortsCallback( |
172 const ServiceWorkerVersion::SendStashedPortsCallback& callback, | 172 const ServiceWorkerVersion::SendStashedPortsCallback& callback, |
173 ServiceWorkerStatusCode status) { | 173 ServiceWorkerStatusCode status) { |
174 callback.Run(status, std::vector<int>()); | 174 callback.Run(status, std::vector<int>()); |
175 } | 175 } |
176 | 176 |
177 using WindowOpenedCallback = base::Callback<void(int, int)>; | 177 using OpenURLCallback = base::Callback<void(int, int)>; |
178 | 178 |
179 // The WindowOpenedObserver class is a WebContentsObserver that will wait for a | 179 // The OpenURLObserver class is a WebContentsObserver that will wait for a |
180 // new Window's WebContents to be initialized, run the |callback| passed to its | 180 // WebContents to be initialized, run the |callback| passed to its constructor |
181 // constructor then self destroy. | 181 // then self destroy. |
182 // The callback will receive the process and frame ids. If something went wrong | 182 // The callback will receive the process and frame ids. If something went wrong |
183 // those will be (kInvalidUniqueID, MSG_ROUTING_NONE). | 183 // those will be (kInvalidUniqueID, MSG_ROUTING_NONE). |
184 // The callback will be called in the IO thread. | 184 // The callback will be called in the IO thread. |
185 class WindowOpenedObserver : public WebContentsObserver { | 185 class OpenURLObserver : public WebContentsObserver { |
186 public: | 186 public: |
187 WindowOpenedObserver(WebContents* web_contents, | 187 OpenURLObserver(WebContents* web_contents, const OpenURLCallback& callback) |
188 const WindowOpenedCallback& callback) | 188 : WebContentsObserver(web_contents), callback_(callback) {} |
189 : WebContentsObserver(web_contents), | |
190 callback_(callback) | |
191 {} | |
192 | 189 |
193 void DidCommitProvisionalLoadForFrame( | 190 void DidCommitProvisionalLoadForFrame( |
194 RenderFrameHost* render_frame_host, | 191 RenderFrameHost* render_frame_host, |
195 const GURL& validated_url, | 192 const GURL& validated_url, |
196 ui::PageTransition transition_type) override { | 193 ui::PageTransition transition_type) override { |
197 DCHECK(web_contents()); | 194 DCHECK(web_contents()); |
198 | 195 |
199 if (render_frame_host != web_contents()->GetMainFrame()) | 196 if (render_frame_host != web_contents()->GetMainFrame()) |
200 return; | 197 return; |
201 | 198 |
(...skipping 17 matching lines...) Expand all Loading... | |
219 DCHECK(web_contents()); | 216 DCHECK(web_contents()); |
220 | 217 |
221 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 218 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
222 base::Bind(callback_, | 219 base::Bind(callback_, |
223 render_process_id, | 220 render_process_id, |
224 render_frame_id)); | 221 render_frame_id)); |
225 Observe(nullptr); | 222 Observe(nullptr); |
226 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | 223 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); |
227 } | 224 } |
228 | 225 |
229 const WindowOpenedCallback callback_; | 226 const OpenURLCallback callback_; |
230 | 227 |
231 DISALLOW_COPY_AND_ASSIGN(WindowOpenedObserver); | 228 DISALLOW_COPY_AND_ASSIGN(OpenURLObserver); |
232 }; | 229 }; |
233 | 230 |
234 void DidOpenURL(const WindowOpenedCallback& callback, | 231 void DidOpenURL(const OpenURLCallback& callback, WebContents* web_contents) { |
235 WebContents* web_contents) { | |
236 DCHECK(web_contents); | 232 DCHECK(web_contents); |
237 | 233 |
238 new WindowOpenedObserver(web_contents, callback); | 234 new OpenURLObserver(web_contents, callback); |
235 } | |
236 | |
237 void NavigateClientOnUI(const GURL& url, | |
238 const GURL& script_url, | |
239 int process_id, | |
240 int frame_id, | |
241 const OpenURLCallback& callback) { | |
242 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
243 | |
244 RenderFrameHost* render_frame_host = | |
245 RenderFrameHost::FromID(process_id, frame_id); | |
246 WebContents* web_contents = | |
247 WebContents::FromRenderFrameHost(render_frame_host); | |
248 | |
249 if (!render_frame_host || !web_contents) | |
250 return; | |
251 | |
252 OpenURLParams params( | |
253 url, Referrer::SanitizeForRequest( | |
254 url, Referrer(script_url, blink::WebReferrerPolicyDefault)), | |
255 CURRENT_TAB, ui::PAGE_TRANSITION_TYPED, false); | |
nhiroki
2015/07/10 06:11:19
Why is this false? This API call is issued by SW o
nhiroki
2015/07/10 06:11:19
Please add a comment after |false| like this:
C
nhiroki
2015/07/10 06:15:35
This transition type could be PAGE_TRANSITION_AUTO
zino
2015/07/13 09:00:40
Done.
zino
2015/07/13 09:00:40
Done.
zino
2015/07/13 09:00:40
Done.
| |
256 web_contents->OpenURL(params); | |
257 DidOpenURL(callback, web_contents); | |
239 } | 258 } |
240 | 259 |
241 void OpenWindowOnUI( | 260 void OpenWindowOnUI( |
242 const GURL& url, | 261 const GURL& url, |
243 const GURL& script_url, | 262 const GURL& script_url, |
244 int process_id, | 263 int process_id, |
245 const scoped_refptr<ServiceWorkerContextWrapper>& context_wrapper, | 264 const scoped_refptr<ServiceWorkerContextWrapper>& context_wrapper, |
246 const WindowOpenedCallback& callback) { | 265 const OpenURLCallback& callback) { |
247 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 266 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
248 | 267 |
249 BrowserContext* browser_context = context_wrapper->storage_partition() | 268 BrowserContext* browser_context = context_wrapper->storage_partition() |
250 ? context_wrapper->storage_partition()->browser_context() | 269 ? context_wrapper->storage_partition()->browser_context() |
251 : nullptr; | 270 : nullptr; |
252 // We are shutting down. | 271 // We are shutting down. |
253 if (!browser_context) | 272 if (!browser_context) |
254 return; | 273 return; |
255 | 274 |
256 RenderProcessHost* render_process_host = | 275 RenderProcessHost* render_process_host = |
(...skipping 934 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1191 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_OpenWindow, | 1210 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_OpenWindow, |
1192 OnOpenWindow) | 1211 OnOpenWindow) |
1193 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SetCachedMetadata, | 1212 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SetCachedMetadata, |
1194 OnSetCachedMetadata) | 1213 OnSetCachedMetadata) |
1195 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ClearCachedMetadata, | 1214 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ClearCachedMetadata, |
1196 OnClearCachedMetadata) | 1215 OnClearCachedMetadata) |
1197 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToClient, | 1216 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToClient, |
1198 OnPostMessageToClient) | 1217 OnPostMessageToClient) |
1199 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FocusClient, | 1218 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FocusClient, |
1200 OnFocusClient) | 1219 OnFocusClient) |
1220 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_NavigateClient, OnNavigateClient) | |
1201 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SkipWaiting, | 1221 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SkipWaiting, |
1202 OnSkipWaiting) | 1222 OnSkipWaiting) |
1203 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ClaimClients, | 1223 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ClaimClients, |
1204 OnClaimClients) | 1224 OnClaimClients) |
1205 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_Pong, OnPongFromWorker) | 1225 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_Pong, OnPongFromWorker) |
1206 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_StashMessagePort, | 1226 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_StashMessagePort, |
1207 OnStashMessagePort) | 1227 OnStashMessagePort) |
1208 IPC_MESSAGE_UNHANDLED(handled = false) | 1228 IPC_MESSAGE_UNHANDLED(handled = false) |
1209 IPC_END_MESSAGE_MAP() | 1229 IPC_END_MESSAGE_MAP() |
1210 return handled; | 1230 return handled; |
(...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1634 if (running_status() != RUNNING) | 1654 if (running_status() != RUNNING) |
1635 return; | 1655 return; |
1636 | 1656 |
1637 ServiceWorkerClientInfo client_info(client); | 1657 ServiceWorkerClientInfo client_info(client); |
1638 client_info.client_uuid = client_uuid; | 1658 client_info.client_uuid = client_uuid; |
1639 | 1659 |
1640 embedded_worker_->SendMessage(ServiceWorkerMsg_FocusClientResponse( | 1660 embedded_worker_->SendMessage(ServiceWorkerMsg_FocusClientResponse( |
1641 request_id, client_info)); | 1661 request_id, client_info)); |
1642 } | 1662 } |
1643 | 1663 |
1664 void ServiceWorkerVersion::OnNavigateClient(int request_id, | |
1665 const std::string& client_uuid, | |
1666 const GURL& url) { | |
1667 if (!context_) | |
1668 return; | |
1669 | |
1670 TRACE_EVENT2("ServiceWorker", "ServiceWorkerVersion::OnNavigateClient", | |
1671 "Request id", request_id, "Client id", client_uuid); | |
1672 | |
1673 if (!url.is_valid()) { | |
1674 DVLOG(1) << "Received unexpected invalid URL from renderer process."; | |
1675 BrowserThread::PostTask( | |
1676 BrowserThread::UI, FROM_HERE, | |
1677 base::Bind(&KillEmbeddedWorkerProcess, embedded_worker_->process_id(), | |
1678 RESULT_CODE_KILLED_BAD_MESSAGE)); | |
1679 return; | |
1680 } | |
1681 | |
1682 // Reject requests for URLs that the process is not allowed to access. It's | |
1683 // possible to receive such requests since the renderer-side checks are | |
1684 // slightly different. For example, the view-source scheme will not be | |
1685 // filtered out by Blink. | |
1686 if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanRequestURL( | |
1687 embedded_worker_->process_id(), url)) { | |
1688 embedded_worker_->SendMessage(ServiceWorkerMsg_NavigateClientError( | |
1689 request_id, url.spec() + " cannot navigate.")); | |
1690 return; | |
1691 } | |
1692 | |
1693 ServiceWorkerProviderHost* provider_host = | |
1694 context_->GetProviderHostByClientID(client_uuid); | |
1695 if (!provider_host || provider_host->active_version() != this) { | |
1696 embedded_worker_->SendMessage(ServiceWorkerMsg_NavigateClientError( | |
1697 request_id, url.spec() + " cannot navigate.")); | |
1698 return; | |
1699 } | |
1700 | |
1701 BrowserThread::PostTask( | |
1702 BrowserThread::UI, FROM_HERE, | |
1703 base::Bind(&NavigateClientOnUI, url, script_url_, | |
1704 provider_host->process_id(), provider_host->frame_id(), | |
1705 base::Bind(&ServiceWorkerVersion::DidNavigateClient, | |
1706 weak_factory_.GetWeakPtr(), request_id))); | |
1707 } | |
1708 | |
1709 void ServiceWorkerVersion::DidNavigateClient(int request_id, | |
1710 int render_process_id, | |
1711 int render_frame_id) { | |
1712 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
1713 | |
1714 if (running_status() != RUNNING) | |
1715 return; | |
1716 | |
1717 if (render_process_id == ChildProcessHost::kInvalidUniqueID && | |
1718 render_frame_id == MSG_ROUTING_NONE) { | |
1719 embedded_worker_->SendMessage(ServiceWorkerMsg_NavigateClientError( | |
1720 request_id, "Something went wrong while trying to navigate client.")); | |
1721 return; | |
1722 } | |
1723 | |
1724 for (auto it = | |
1725 context_->GetClientProviderHostIterator(script_url_.GetOrigin()); | |
1726 !it->IsAtEnd(); it->Advance()) { | |
1727 ServiceWorkerProviderHost* provider_host = it->GetProviderHost(); | |
1728 if (provider_host->process_id() != render_process_id || | |
1729 provider_host->frame_id() != render_frame_id) { | |
1730 continue; | |
1731 } | |
1732 provider_host->GetWindowClientInfo(base::Bind( | |
1733 &ServiceWorkerVersion::OnNavigateClientFinished, | |
1734 weak_factory_.GetWeakPtr(), request_id, provider_host->client_uuid())); | |
1735 return; | |
1736 } | |
1737 | |
1738 OnNavigateClientFinished(request_id, std::string(), | |
1739 ServiceWorkerClientInfo()); | |
1740 } | |
1741 | |
1742 void ServiceWorkerVersion::OnNavigateClientFinished( | |
1743 int request_id, | |
1744 const std::string& client_uuid, | |
1745 const ServiceWorkerClientInfo& client_info) { | |
1746 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
1747 | |
1748 if (running_status() != RUNNING) | |
1749 return; | |
1750 | |
1751 ServiceWorkerClientInfo client(client_info); | |
1752 if (!client.IsEmpty()) | |
nhiroki
2015/07/10 06:11:19
Why didn't you copy the comment from OnOpenWindowF
zino
2015/07/13 09:00:40
Done.
| |
1753 client.client_uuid = client_uuid; | |
1754 | |
1755 embedded_worker_->SendMessage( | |
1756 ServiceWorkerMsg_NavigateClientResponse(request_id, client)); | |
1757 } | |
1758 | |
1644 void ServiceWorkerVersion::OnSkipWaiting(int request_id) { | 1759 void ServiceWorkerVersion::OnSkipWaiting(int request_id) { |
1645 skip_waiting_ = true; | 1760 skip_waiting_ = true; |
1646 if (status_ != INSTALLED) | 1761 if (status_ != INSTALLED) |
1647 return DidSkipWaiting(request_id); | 1762 return DidSkipWaiting(request_id); |
1648 | 1763 |
1649 if (!context_) | 1764 if (!context_) |
1650 return; | 1765 return; |
1651 ServiceWorkerRegistration* registration = | 1766 ServiceWorkerRegistration* registration = |
1652 context_->GetLiveRegistration(registration_id_); | 1767 context_->GetLiveRegistration(registration_id_); |
1653 if (!registration) | 1768 if (!registration) |
(...skipping 504 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2158 | 2273 |
2159 streaming_url_request_jobs_.clear(); | 2274 streaming_url_request_jobs_.clear(); |
2160 | 2275 |
2161 FOR_EACH_OBSERVER(Listener, listeners_, OnRunningStateChanged(this)); | 2276 FOR_EACH_OBSERVER(Listener, listeners_, OnRunningStateChanged(this)); |
2162 | 2277 |
2163 if (should_restart) | 2278 if (should_restart) |
2164 StartWorkerInternal(false /* pause_after_download */); | 2279 StartWorkerInternal(false /* pause_after_download */); |
2165 } | 2280 } |
2166 | 2281 |
2167 } // namespace content | 2282 } // namespace content |
OLD | NEW |