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_version.h" | 5 #include "content/browser/service_worker/service_worker_version.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 170 ServiceWorkerStatusCode status) { | 170 ServiceWorkerStatusCode status) { |
| 171 callback.Run(status, false /* accept_connection */); | 171 callback.Run(status, false /* accept_connection */); |
| 172 } | 172 } |
| 173 | 173 |
| 174 void RunErrorSendStashedPortsCallback( | 174 void RunErrorSendStashedPortsCallback( |
| 175 const ServiceWorkerVersion::SendStashedPortsCallback& callback, | 175 const ServiceWorkerVersion::SendStashedPortsCallback& callback, |
| 176 ServiceWorkerStatusCode status) { | 176 ServiceWorkerStatusCode status) { |
| 177 callback.Run(status, std::vector<int>()); | 177 callback.Run(status, std::vector<int>()); |
| 178 } | 178 } |
| 179 | 179 |
| 180 using WindowOpenedCallback = base::Callback<void(int, int)>; | 180 using OpenURLCallback = base::Callback<void(int, int)>; |
| 181 | 181 |
| 182 // The WindowOpenedObserver class is a WebContentsObserver that will wait for a | 182 // The OpenURLObserver class is a WebContentsObserver that will wait for a |
| 183 // new Window's WebContents to be initialized, run the |callback| passed to its | 183 // WebContents to be initialized, run the |callback| passed to its constructor |
| 184 // constructor then self destroy. | 184 // then self destroy. |
| 185 // The callback will receive the process and frame ids. If something went wrong | 185 // The callback will receive the process and frame ids. If something went wrong |
| 186 // those will be (kInvalidUniqueID, MSG_ROUTING_NONE). | 186 // those will be (kInvalidUniqueID, MSG_ROUTING_NONE). |
| 187 // The callback will be called in the IO thread. | 187 // The callback will be called in the IO thread. |
| 188 class WindowOpenedObserver : public WebContentsObserver { | 188 class OpenURLObserver : public WebContentsObserver { |
| 189 public: | 189 public: |
| 190 WindowOpenedObserver(WebContents* web_contents, | 190 OpenURLObserver(WebContents* web_contents, const OpenURLCallback& callback) |
| 191 const WindowOpenedCallback& callback) | 191 : WebContentsObserver(web_contents), callback_(callback) {} |
| 192 : WebContentsObserver(web_contents), | |
| 193 callback_(callback) | |
| 194 {} | |
| 195 | 192 |
| 196 void DidCommitProvisionalLoadForFrame( | 193 void DidCommitProvisionalLoadForFrame( |
| 197 RenderFrameHost* render_frame_host, | 194 RenderFrameHost* render_frame_host, |
| 198 const GURL& validated_url, | 195 const GURL& validated_url, |
| 199 ui::PageTransition transition_type) override { | 196 ui::PageTransition transition_type) override { |
| 200 DCHECK(web_contents()); | 197 DCHECK(web_contents()); |
| 201 | 198 |
| 202 if (render_frame_host != web_contents()->GetMainFrame()) | 199 if (render_frame_host != web_contents()->GetMainFrame()) |
| 203 return; | 200 return; |
| 204 | 201 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 222 DCHECK(web_contents()); | 219 DCHECK(web_contents()); |
| 223 | 220 |
| 224 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 221 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 225 base::Bind(callback_, | 222 base::Bind(callback_, |
| 226 render_process_id, | 223 render_process_id, |
| 227 render_frame_id)); | 224 render_frame_id)); |
| 228 Observe(nullptr); | 225 Observe(nullptr); |
| 229 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | 226 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); |
| 230 } | 227 } |
| 231 | 228 |
| 232 const WindowOpenedCallback callback_; | 229 const OpenURLCallback callback_; |
| 233 | 230 |
| 234 DISALLOW_COPY_AND_ASSIGN(WindowOpenedObserver); | 231 DISALLOW_COPY_AND_ASSIGN(OpenURLObserver); |
| 235 }; | 232 }; |
| 236 | 233 |
| 237 void DidOpenURL(const WindowOpenedCallback& callback, | 234 void DidOpenURL(const OpenURLCallback& callback, WebContents* web_contents) { |
| 238 WebContents* web_contents) { | |
| 239 DCHECK(web_contents); | 235 DCHECK(web_contents); |
| 240 | 236 |
| 241 new WindowOpenedObserver(web_contents, callback); | 237 new OpenURLObserver(web_contents, callback); |
| 238 } | |
| 239 | |
| 240 void NavigateClientOnUI(const GURL& url, | |
| 241 const GURL& script_url, | |
| 242 int process_id, | |
| 243 int frame_id, | |
| 244 const OpenURLCallback& callback) { | |
| 245 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 246 | |
| 247 RenderFrameHost* render_frame_host = | |
| 248 RenderFrameHost::FromID(process_id, frame_id); | |
| 249 WebContents* web_contents = | |
| 250 WebContents::FromRenderFrameHost(render_frame_host); | |
| 251 | |
| 252 if (!render_frame_host || !web_contents) | |
|
nhiroki
2015/07/15 05:36:30
Could you run |callback| with invalid parameters s
| |
| 253 return; | |
| 254 | |
| 255 OpenURLParams params( | |
| 256 url, Referrer::SanitizeForRequest( | |
| 257 url, Referrer(script_url, blink::WebReferrerPolicyDefault)), | |
| 258 CURRENT_TAB, ui::PAGE_TRANSITION_AUTO_TOPLEVEL, | |
| 259 true /* is_renderer_initiated */); | |
| 260 web_contents->OpenURL(params); | |
| 261 DidOpenURL(callback, web_contents); | |
| 242 } | 262 } |
| 243 | 263 |
| 244 void OpenWindowOnUI( | 264 void OpenWindowOnUI( |
| 245 const GURL& url, | 265 const GURL& url, |
| 246 const GURL& script_url, | 266 const GURL& script_url, |
| 247 int process_id, | 267 int process_id, |
| 248 const scoped_refptr<ServiceWorkerContextWrapper>& context_wrapper, | 268 const scoped_refptr<ServiceWorkerContextWrapper>& context_wrapper, |
| 249 const WindowOpenedCallback& callback) { | 269 const OpenURLCallback& callback) { |
| 250 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 270 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 251 | 271 |
| 252 BrowserContext* browser_context = context_wrapper->storage_partition() | 272 BrowserContext* browser_context = context_wrapper->storage_partition() |
| 253 ? context_wrapper->storage_partition()->browser_context() | 273 ? context_wrapper->storage_partition()->browser_context() |
| 254 : nullptr; | 274 : nullptr; |
| 255 // We are shutting down. | 275 // We are shutting down. |
| 256 if (!browser_context) | 276 if (!browser_context) |
| 257 return; | 277 return; |
| 258 | 278 |
| 259 RenderProcessHost* render_process_host = | 279 RenderProcessHost* render_process_host = |
| (...skipping 935 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1195 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_OpenWindow, | 1215 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_OpenWindow, |
| 1196 OnOpenWindow) | 1216 OnOpenWindow) |
| 1197 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SetCachedMetadata, | 1217 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SetCachedMetadata, |
| 1198 OnSetCachedMetadata) | 1218 OnSetCachedMetadata) |
| 1199 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ClearCachedMetadata, | 1219 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ClearCachedMetadata, |
| 1200 OnClearCachedMetadata) | 1220 OnClearCachedMetadata) |
| 1201 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToClient, | 1221 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToClient, |
| 1202 OnPostMessageToClient) | 1222 OnPostMessageToClient) |
| 1203 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FocusClient, | 1223 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FocusClient, |
| 1204 OnFocusClient) | 1224 OnFocusClient) |
| 1225 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_NavigateClient, OnNavigateClient) | |
| 1205 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SkipWaiting, | 1226 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SkipWaiting, |
| 1206 OnSkipWaiting) | 1227 OnSkipWaiting) |
| 1207 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ClaimClients, | 1228 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ClaimClients, |
| 1208 OnClaimClients) | 1229 OnClaimClients) |
| 1209 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_Pong, OnPongFromWorker) | 1230 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_Pong, OnPongFromWorker) |
| 1210 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_StashMessagePort, | 1231 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_StashMessagePort, |
| 1211 OnStashMessagePort) | 1232 OnStashMessagePort) |
| 1212 IPC_MESSAGE_UNHANDLED(handled = false) | 1233 IPC_MESSAGE_UNHANDLED(handled = false) |
| 1213 IPC_END_MESSAGE_MAP() | 1234 IPC_END_MESSAGE_MAP() |
| 1214 return handled; | 1235 return handled; |
| (...skipping 424 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1639 if (running_status() != RUNNING) | 1660 if (running_status() != RUNNING) |
| 1640 return; | 1661 return; |
| 1641 | 1662 |
| 1642 ServiceWorkerClientInfo client_info(client); | 1663 ServiceWorkerClientInfo client_info(client); |
| 1643 client_info.client_uuid = client_uuid; | 1664 client_info.client_uuid = client_uuid; |
| 1644 | 1665 |
| 1645 embedded_worker_->SendMessage(ServiceWorkerMsg_FocusClientResponse( | 1666 embedded_worker_->SendMessage(ServiceWorkerMsg_FocusClientResponse( |
| 1646 request_id, client_info)); | 1667 request_id, client_info)); |
| 1647 } | 1668 } |
| 1648 | 1669 |
| 1670 void ServiceWorkerVersion::OnNavigateClient(int request_id, | |
| 1671 const std::string& client_uuid, | |
| 1672 const GURL& url) { | |
| 1673 if (!context_) | |
| 1674 return; | |
| 1675 | |
| 1676 TRACE_EVENT2("ServiceWorker", "ServiceWorkerVersion::OnNavigateClient", | |
| 1677 "Request id", request_id, "Client id", client_uuid); | |
| 1678 | |
| 1679 if (!url.is_valid()) { | |
| 1680 DVLOG(1) << "Received unexpected invalid URL from renderer process."; | |
| 1681 BrowserThread::PostTask( | |
| 1682 BrowserThread::UI, FROM_HERE, | |
| 1683 base::Bind(&KillEmbeddedWorkerProcess, embedded_worker_->process_id(), | |
| 1684 RESULT_CODE_KILLED_BAD_MESSAGE)); | |
| 1685 return; | |
| 1686 } | |
| 1687 | |
| 1688 // Reject requests for URLs that the process is not allowed to access. It's | |
| 1689 // possible to receive such requests since the renderer-side checks are | |
| 1690 // slightly different. For example, the view-source scheme will not be | |
| 1691 // filtered out by Blink. | |
| 1692 if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanRequestURL( | |
| 1693 embedded_worker_->process_id(), url)) { | |
| 1694 embedded_worker_->SendMessage(ServiceWorkerMsg_NavigateClientError( | |
| 1695 request_id, url.spec() + " cannot navigate.")); | |
| 1696 return; | |
| 1697 } | |
| 1698 | |
| 1699 ServiceWorkerProviderHost* provider_host = | |
| 1700 context_->GetProviderHostByClientID(client_uuid); | |
| 1701 if (!provider_host || provider_host->active_version() != this) { | |
| 1702 embedded_worker_->SendMessage(ServiceWorkerMsg_NavigateClientError( | |
| 1703 request_id, url.spec() + " cannot navigate.")); | |
| 1704 return; | |
| 1705 } | |
| 1706 | |
| 1707 BrowserThread::PostTask( | |
| 1708 BrowserThread::UI, FROM_HERE, | |
| 1709 base::Bind(&NavigateClientOnUI, url, script_url_, | |
| 1710 provider_host->process_id(), provider_host->frame_id(), | |
| 1711 base::Bind(&ServiceWorkerVersion::DidNavigateClient, | |
| 1712 weak_factory_.GetWeakPtr(), request_id))); | |
| 1713 } | |
| 1714 | |
| 1715 void ServiceWorkerVersion::DidNavigateClient(int request_id, | |
| 1716 int render_process_id, | |
| 1717 int render_frame_id) { | |
| 1718 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 1719 | |
| 1720 if (running_status() != RUNNING) | |
| 1721 return; | |
| 1722 | |
| 1723 if (render_process_id == ChildProcessHost::kInvalidUniqueID && | |
| 1724 render_frame_id == MSG_ROUTING_NONE) { | |
| 1725 embedded_worker_->SendMessage(ServiceWorkerMsg_NavigateClientError( | |
| 1726 request_id, "Something went wrong while trying to navigate client.")); | |
| 1727 return; | |
| 1728 } | |
| 1729 | |
| 1730 for (auto it = | |
| 1731 context_->GetClientProviderHostIterator(script_url_.GetOrigin()); | |
| 1732 !it->IsAtEnd(); it->Advance()) { | |
| 1733 ServiceWorkerProviderHost* provider_host = it->GetProviderHost(); | |
| 1734 if (provider_host->process_id() != render_process_id || | |
| 1735 provider_host->frame_id() != render_frame_id) { | |
| 1736 continue; | |
| 1737 } | |
| 1738 provider_host->GetWindowClientInfo(base::Bind( | |
| 1739 &ServiceWorkerVersion::OnNavigateClientFinished, | |
| 1740 weak_factory_.GetWeakPtr(), request_id, provider_host->client_uuid())); | |
| 1741 return; | |
| 1742 } | |
| 1743 | |
| 1744 OnNavigateClientFinished(request_id, std::string(), | |
| 1745 ServiceWorkerClientInfo()); | |
| 1746 } | |
| 1747 | |
| 1748 void ServiceWorkerVersion::OnNavigateClientFinished( | |
| 1749 int request_id, | |
| 1750 const std::string& client_uuid, | |
| 1751 const ServiceWorkerClientInfo& client_info) { | |
| 1752 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 1753 | |
| 1754 if (running_status() != RUNNING) | |
| 1755 return; | |
| 1756 | |
| 1757 ServiceWorkerClientInfo client(client_info); | |
| 1758 | |
| 1759 // If the |client_info| is empty, it means that the navigated client wasn't | |
| 1760 // controlled but the action still succeeded. The renderer process is | |
| 1761 // expecting an empty client in such case. | |
| 1762 if (!client.IsEmpty()) | |
| 1763 client.client_uuid = client_uuid; | |
| 1764 | |
| 1765 embedded_worker_->SendMessage( | |
| 1766 ServiceWorkerMsg_NavigateClientResponse(request_id, client)); | |
| 1767 } | |
| 1768 | |
| 1649 void ServiceWorkerVersion::OnSkipWaiting(int request_id) { | 1769 void ServiceWorkerVersion::OnSkipWaiting(int request_id) { |
| 1650 skip_waiting_ = true; | 1770 skip_waiting_ = true; |
| 1651 if (status_ != INSTALLED) | 1771 if (status_ != INSTALLED) |
| 1652 return DidSkipWaiting(request_id); | 1772 return DidSkipWaiting(request_id); |
| 1653 | 1773 |
| 1654 if (!context_) | 1774 if (!context_) |
| 1655 return; | 1775 return; |
| 1656 ServiceWorkerRegistration* registration = | 1776 ServiceWorkerRegistration* registration = |
| 1657 context_->GetLiveRegistration(registration_id_); | 1777 context_->GetLiveRegistration(registration_id_); |
| 1658 if (!registration) | 1778 if (!registration) |
| (...skipping 509 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2168 | 2288 |
| 2169 streaming_url_request_jobs_.clear(); | 2289 streaming_url_request_jobs_.clear(); |
| 2170 | 2290 |
| 2171 FOR_EACH_OBSERVER(Listener, listeners_, OnRunningStateChanged(this)); | 2291 FOR_EACH_OBSERVER(Listener, listeners_, OnRunningStateChanged(this)); |
| 2172 | 2292 |
| 2173 if (should_restart) | 2293 if (should_restart) |
| 2174 StartWorkerInternal(); | 2294 StartWorkerInternal(); |
| 2175 } | 2295 } |
| 2176 | 2296 |
| 2177 } // namespace content | 2297 } // namespace content |
| OLD | NEW |