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) { | |
| 253 BrowserThread::PostTask( | |
|
zino
2015/07/15 06:10:18
Is this correct? If it's incorrect, please let me
nhiroki
2015/07/15 06:43:14
Looks good. Thanks!
| |
| 254 BrowserThread::IO, FROM_HERE, | |
| 255 base::Bind(callback, ChildProcessHost::kInvalidUniqueID, | |
| 256 MSG_ROUTING_NONE)); | |
| 257 return; | |
| 258 } | |
| 259 | |
| 260 OpenURLParams params( | |
| 261 url, Referrer::SanitizeForRequest( | |
| 262 url, Referrer(script_url, blink::WebReferrerPolicyDefault)), | |
| 263 CURRENT_TAB, ui::PAGE_TRANSITION_AUTO_TOPLEVEL, | |
| 264 true /* is_renderer_initiated */); | |
| 265 web_contents->OpenURL(params); | |
| 266 DidOpenURL(callback, web_contents); | |
| 242 } | 267 } |
| 243 | 268 |
| 244 void OpenWindowOnUI( | 269 void OpenWindowOnUI( |
| 245 const GURL& url, | 270 const GURL& url, |
| 246 const GURL& script_url, | 271 const GURL& script_url, |
| 247 int process_id, | 272 int process_id, |
| 248 const scoped_refptr<ServiceWorkerContextWrapper>& context_wrapper, | 273 const scoped_refptr<ServiceWorkerContextWrapper>& context_wrapper, |
| 249 const WindowOpenedCallback& callback) { | 274 const OpenURLCallback& callback) { |
| 250 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 275 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 251 | 276 |
| 252 BrowserContext* browser_context = context_wrapper->storage_partition() | 277 BrowserContext* browser_context = context_wrapper->storage_partition() |
| 253 ? context_wrapper->storage_partition()->browser_context() | 278 ? context_wrapper->storage_partition()->browser_context() |
| 254 : nullptr; | 279 : nullptr; |
| 255 // We are shutting down. | 280 // We are shutting down. |
| 256 if (!browser_context) | 281 if (!browser_context) |
| 257 return; | 282 return; |
| 258 | 283 |
| 259 RenderProcessHost* render_process_host = | 284 RenderProcessHost* render_process_host = |
| (...skipping 935 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1195 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_OpenWindow, | 1220 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_OpenWindow, |
| 1196 OnOpenWindow) | 1221 OnOpenWindow) |
| 1197 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SetCachedMetadata, | 1222 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SetCachedMetadata, |
| 1198 OnSetCachedMetadata) | 1223 OnSetCachedMetadata) |
| 1199 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ClearCachedMetadata, | 1224 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ClearCachedMetadata, |
| 1200 OnClearCachedMetadata) | 1225 OnClearCachedMetadata) |
| 1201 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToClient, | 1226 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToClient, |
| 1202 OnPostMessageToClient) | 1227 OnPostMessageToClient) |
| 1203 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FocusClient, | 1228 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FocusClient, |
| 1204 OnFocusClient) | 1229 OnFocusClient) |
| 1230 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_NavigateClient, OnNavigateClient) | |
| 1205 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SkipWaiting, | 1231 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SkipWaiting, |
| 1206 OnSkipWaiting) | 1232 OnSkipWaiting) |
| 1207 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ClaimClients, | 1233 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ClaimClients, |
| 1208 OnClaimClients) | 1234 OnClaimClients) |
| 1209 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_Pong, OnPongFromWorker) | 1235 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_Pong, OnPongFromWorker) |
| 1210 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_StashMessagePort, | 1236 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_StashMessagePort, |
| 1211 OnStashMessagePort) | 1237 OnStashMessagePort) |
| 1212 IPC_MESSAGE_UNHANDLED(handled = false) | 1238 IPC_MESSAGE_UNHANDLED(handled = false) |
| 1213 IPC_END_MESSAGE_MAP() | 1239 IPC_END_MESSAGE_MAP() |
| 1214 return handled; | 1240 return handled; |
| (...skipping 424 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1639 if (running_status() != RUNNING) | 1665 if (running_status() != RUNNING) |
| 1640 return; | 1666 return; |
| 1641 | 1667 |
| 1642 ServiceWorkerClientInfo client_info(client); | 1668 ServiceWorkerClientInfo client_info(client); |
| 1643 client_info.client_uuid = client_uuid; | 1669 client_info.client_uuid = client_uuid; |
| 1644 | 1670 |
| 1645 embedded_worker_->SendMessage(ServiceWorkerMsg_FocusClientResponse( | 1671 embedded_worker_->SendMessage(ServiceWorkerMsg_FocusClientResponse( |
| 1646 request_id, client_info)); | 1672 request_id, client_info)); |
| 1647 } | 1673 } |
| 1648 | 1674 |
| 1675 void ServiceWorkerVersion::OnNavigateClient(int request_id, | |
| 1676 const std::string& client_uuid, | |
| 1677 const GURL& url) { | |
| 1678 if (!context_) | |
| 1679 return; | |
| 1680 | |
| 1681 TRACE_EVENT2("ServiceWorker", "ServiceWorkerVersion::OnNavigateClient", | |
| 1682 "Request id", request_id, "Client id", client_uuid); | |
| 1683 | |
| 1684 if (!url.is_valid()) { | |
| 1685 DVLOG(1) << "Received unexpected invalid URL from renderer process."; | |
| 1686 BrowserThread::PostTask( | |
| 1687 BrowserThread::UI, FROM_HERE, | |
| 1688 base::Bind(&KillEmbeddedWorkerProcess, embedded_worker_->process_id(), | |
| 1689 RESULT_CODE_KILLED_BAD_MESSAGE)); | |
| 1690 return; | |
| 1691 } | |
| 1692 | |
| 1693 // Reject requests for URLs that the process is not allowed to access. It's | |
| 1694 // possible to receive such requests since the renderer-side checks are | |
| 1695 // slightly different. For example, the view-source scheme will not be | |
| 1696 // filtered out by Blink. | |
| 1697 if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanRequestURL( | |
| 1698 embedded_worker_->process_id(), url)) { | |
| 1699 embedded_worker_->SendMessage(ServiceWorkerMsg_NavigateClientError( | |
| 1700 request_id, url.spec() + " cannot navigate.")); | |
| 1701 return; | |
| 1702 } | |
| 1703 | |
| 1704 ServiceWorkerProviderHost* provider_host = | |
| 1705 context_->GetProviderHostByClientID(client_uuid); | |
| 1706 if (!provider_host || provider_host->active_version() != this) { | |
| 1707 embedded_worker_->SendMessage(ServiceWorkerMsg_NavigateClientError( | |
| 1708 request_id, url.spec() + " cannot navigate.")); | |
| 1709 return; | |
| 1710 } | |
| 1711 | |
| 1712 BrowserThread::PostTask( | |
| 1713 BrowserThread::UI, FROM_HERE, | |
| 1714 base::Bind(&NavigateClientOnUI, url, script_url_, | |
| 1715 provider_host->process_id(), provider_host->frame_id(), | |
| 1716 base::Bind(&ServiceWorkerVersion::DidNavigateClient, | |
| 1717 weak_factory_.GetWeakPtr(), request_id))); | |
| 1718 } | |
| 1719 | |
| 1720 void ServiceWorkerVersion::DidNavigateClient(int request_id, | |
| 1721 int render_process_id, | |
| 1722 int render_frame_id) { | |
| 1723 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 1724 | |
| 1725 if (running_status() != RUNNING) | |
| 1726 return; | |
| 1727 | |
| 1728 if (render_process_id == ChildProcessHost::kInvalidUniqueID && | |
| 1729 render_frame_id == MSG_ROUTING_NONE) { | |
| 1730 embedded_worker_->SendMessage(ServiceWorkerMsg_NavigateClientError( | |
| 1731 request_id, "Something went wrong while trying to navigate client.")); | |
| 1732 return; | |
| 1733 } | |
| 1734 | |
| 1735 for (auto it = | |
| 1736 context_->GetClientProviderHostIterator(script_url_.GetOrigin()); | |
| 1737 !it->IsAtEnd(); it->Advance()) { | |
| 1738 ServiceWorkerProviderHost* provider_host = it->GetProviderHost(); | |
| 1739 if (provider_host->process_id() != render_process_id || | |
| 1740 provider_host->frame_id() != render_frame_id) { | |
| 1741 continue; | |
| 1742 } | |
| 1743 provider_host->GetWindowClientInfo(base::Bind( | |
| 1744 &ServiceWorkerVersion::OnNavigateClientFinished, | |
| 1745 weak_factory_.GetWeakPtr(), request_id, provider_host->client_uuid())); | |
| 1746 return; | |
| 1747 } | |
| 1748 | |
| 1749 OnNavigateClientFinished(request_id, std::string(), | |
| 1750 ServiceWorkerClientInfo()); | |
| 1751 } | |
| 1752 | |
| 1753 void ServiceWorkerVersion::OnNavigateClientFinished( | |
| 1754 int request_id, | |
| 1755 const std::string& client_uuid, | |
| 1756 const ServiceWorkerClientInfo& client_info) { | |
| 1757 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 1758 | |
| 1759 if (running_status() != RUNNING) | |
| 1760 return; | |
| 1761 | |
| 1762 ServiceWorkerClientInfo client(client_info); | |
| 1763 | |
| 1764 // If the |client_info| is empty, it means that the navigated client wasn't | |
| 1765 // controlled but the action still succeeded. The renderer process is | |
| 1766 // expecting an empty client in such case. | |
| 1767 if (!client.IsEmpty()) | |
| 1768 client.client_uuid = client_uuid; | |
| 1769 | |
| 1770 embedded_worker_->SendMessage( | |
| 1771 ServiceWorkerMsg_NavigateClientResponse(request_id, client)); | |
| 1772 } | |
| 1773 | |
| 1649 void ServiceWorkerVersion::OnSkipWaiting(int request_id) { | 1774 void ServiceWorkerVersion::OnSkipWaiting(int request_id) { |
| 1650 skip_waiting_ = true; | 1775 skip_waiting_ = true; |
| 1651 if (status_ != INSTALLED) | 1776 if (status_ != INSTALLED) |
| 1652 return DidSkipWaiting(request_id); | 1777 return DidSkipWaiting(request_id); |
| 1653 | 1778 |
| 1654 if (!context_) | 1779 if (!context_) |
| 1655 return; | 1780 return; |
| 1656 ServiceWorkerRegistration* registration = | 1781 ServiceWorkerRegistration* registration = |
| 1657 context_->GetLiveRegistration(registration_id_); | 1782 context_->GetLiveRegistration(registration_id_); |
| 1658 if (!registration) | 1783 if (!registration) |
| (...skipping 509 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2168 | 2293 |
| 2169 streaming_url_request_jobs_.clear(); | 2294 streaming_url_request_jobs_.clear(); |
| 2170 | 2295 |
| 2171 FOR_EACH_OBSERVER(Listener, listeners_, OnRunningStateChanged(this)); | 2296 FOR_EACH_OBSERVER(Listener, listeners_, OnRunningStateChanged(this)); |
| 2172 | 2297 |
| 2173 if (should_restart) | 2298 if (should_restart) |
| 2174 StartWorkerInternal(); | 2299 StartWorkerInternal(); |
| 2175 } | 2300 } |
| 2176 | 2301 |
| 2177 } // namespace content | 2302 } // namespace content |
| OLD | NEW |