 Chromium Code Reviews
 Chromium Code Reviews Issue 1335303004:
  ServiceWorker: Fix the bug in navigate() if windowclient is nested frame.  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master
    
  
    Issue 1335303004:
  ServiceWorker: Fix the bug in navigate() if windowclient is nested frame.  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master| 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 <algorithm> | 7 #include <algorithm> | 
| 8 #include <map> | 8 #include <map> | 
| 9 #include <string> | 9 #include <string> | 
| 10 | 10 | 
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" | 
| 12 #include "base/guid.h" | 12 #include "base/guid.h" | 
| 13 #include "base/location.h" | 13 #include "base/location.h" | 
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" | 
| 15 #include "base/metrics/histogram_macros.h" | 15 #include "base/metrics/histogram_macros.h" | 
| 16 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" | 
| 17 #include "base/stl_util.h" | 17 #include "base/stl_util.h" | 
| 18 #include "base/strings/string16.h" | 18 #include "base/strings/string16.h" | 
| 19 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" | 
| 20 #include "base/thread_task_runner_handle.h" | 20 #include "base/thread_task_runner_handle.h" | 
| 21 #include "base/time/time.h" | 21 #include "base/time/time.h" | 
| 22 #include "content/browser/bad_message.h" | 22 #include "content/browser/bad_message.h" | 
| 23 #include "content/browser/child_process_security_policy_impl.h" | 23 #include "content/browser/child_process_security_policy_impl.h" | 
| 24 #include "content/browser/frame_host/frame_tree_node.h" | |
| 25 #include "content/browser/frame_host/render_frame_host_impl.h" | |
| 24 #include "content/browser/message_port_message_filter.h" | 26 #include "content/browser/message_port_message_filter.h" | 
| 25 #include "content/browser/message_port_service.h" | 27 #include "content/browser/message_port_service.h" | 
| 26 #include "content/browser/service_worker/embedded_worker_instance.h" | 28 #include "content/browser/service_worker/embedded_worker_instance.h" | 
| 27 #include "content/browser/service_worker/embedded_worker_registry.h" | 29 #include "content/browser/service_worker/embedded_worker_registry.h" | 
| 28 #include "content/browser/service_worker/service_worker_context_core.h" | 30 #include "content/browser/service_worker/service_worker_context_core.h" | 
| 29 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 31 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 
| 30 #include "content/browser/service_worker/service_worker_metrics.h" | 32 #include "content/browser/service_worker/service_worker_metrics.h" | 
| 31 #include "content/browser/service_worker/service_worker_registration.h" | 33 #include "content/browser/service_worker/service_worker_registration.h" | 
| 32 #include "content/browser/storage_partition_impl.h" | 34 #include "content/browser/storage_partition_impl.h" | 
| 33 #include "content/common/service_worker/service_worker_messages.h" | 35 #include "content/common/service_worker/service_worker_messages.h" | 
| 34 #include "content/common/service_worker/service_worker_type_converters.h" | 36 #include "content/common/service_worker/service_worker_type_converters.h" | 
| 35 #include "content/common/service_worker/service_worker_utils.h" | 37 #include "content/common/service_worker/service_worker_utils.h" | 
| 36 #include "content/public/browser/browser_thread.h" | 38 #include "content/public/browser/browser_thread.h" | 
| 37 #include "content/public/browser/content_browser_client.h" | 39 #include "content/public/browser/content_browser_client.h" | 
| 38 #include "content/public/browser/page_navigator.h" | 40 #include "content/public/browser/page_navigator.h" | 
| 39 #include "content/public/browser/render_frame_host.h" | |
| 40 #include "content/public/browser/render_process_host.h" | 41 #include "content/public/browser/render_process_host.h" | 
| 41 #include "content/public/browser/web_contents.h" | 42 #include "content/public/browser/web_contents.h" | 
| 42 #include "content/public/browser/web_contents_observer.h" | 43 #include "content/public/browser/web_contents_observer.h" | 
| 43 #include "content/public/common/background_sync.mojom.h" | 44 #include "content/public/common/background_sync.mojom.h" | 
| 44 #include "content/public/common/child_process_host.h" | 45 #include "content/public/common/child_process_host.h" | 
| 45 #include "content/public/common/content_client.h" | 46 #include "content/public/common/content_client.h" | 
| 46 #include "content/public/common/content_switches.h" | 47 #include "content/public/common/content_switches.h" | 
| 47 #include "content/public/common/result_codes.h" | 48 #include "content/public/common/result_codes.h" | 
| 48 #include "content/public/common/service_registry.h" | 49 #include "content/public/common/service_registry.h" | 
| 49 #include "mojo/common/common_type_converters.h" | 50 #include "mojo/common/common_type_converters.h" | 
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 180 using OpenURLCallback = base::Callback<void(int, int)>; | 181 using OpenURLCallback = base::Callback<void(int, int)>; | 
| 181 | 182 | 
| 182 // The OpenURLObserver class is a WebContentsObserver that will wait for a | 183 // The OpenURLObserver class is a WebContentsObserver that will wait for a | 
| 183 // WebContents to be initialized, run the |callback| passed to its constructor | 184 // WebContents to be initialized, run the |callback| passed to its constructor | 
| 184 // then self destroy. | 185 // then self destroy. | 
| 185 // The callback will receive the process and frame ids. If something went wrong | 186 // The callback will receive the process and frame ids. If something went wrong | 
| 186 // those will be (kInvalidUniqueID, MSG_ROUTING_NONE). | 187 // those will be (kInvalidUniqueID, MSG_ROUTING_NONE). | 
| 187 // The callback will be called in the IO thread. | 188 // The callback will be called in the IO thread. | 
| 188 class OpenURLObserver : public WebContentsObserver { | 189 class OpenURLObserver : public WebContentsObserver { | 
| 189 public: | 190 public: | 
| 190 OpenURLObserver(WebContents* web_contents, const OpenURLCallback& callback) | 191 OpenURLObserver(WebContents* web_contents, | 
| 191 : WebContentsObserver(web_contents), callback_(callback) {} | 192 int frame_tree_node_id, | 
| 193 const OpenURLCallback& callback) | |
| 194 : WebContentsObserver(web_contents), | |
| 195 frame_tree_node_id_(frame_tree_node_id), | |
| 196 callback_(callback) {} | |
| 192 | 197 | 
| 193 void DidCommitProvisionalLoadForFrame( | 198 void DidCommitProvisionalLoadForFrame( | 
| 194 RenderFrameHost* render_frame_host, | 199 RenderFrameHost* render_frame_host, | 
| 195 const GURL& validated_url, | 200 const GURL& validated_url, | 
| 196 ui::PageTransition transition_type) override { | 201 ui::PageTransition transition_type) override { | 
| 197 DCHECK(web_contents()); | 202 DCHECK(web_contents()); | 
| 198 | 203 | 
| 199 if (render_frame_host != web_contents()->GetMainFrame()) | 204 RenderFrameHostImpl* rfhi = | 
| 205 static_cast<RenderFrameHostImpl*>(render_frame_host); | |
| 206 if (rfhi->frame_tree_node()->frame_tree_node_id() != frame_tree_node_id_) | |
| 200 return; | 207 return; | 
| 201 | 208 | 
| 202 RunCallback(render_frame_host->GetProcess()->GetID(), | 209 RunCallback(render_frame_host->GetProcess()->GetID(), | 
| 203 render_frame_host->GetRoutingID()); | 210 render_frame_host->GetRoutingID()); | 
| 204 } | 211 } | 
| 205 | 212 | 
| 206 void RenderProcessGone(base::TerminationStatus status) override { | 213 void RenderProcessGone(base::TerminationStatus status) override { | 
| 207 RunCallback(ChildProcessHost::kInvalidUniqueID, MSG_ROUTING_NONE); | 214 RunCallback(ChildProcessHost::kInvalidUniqueID, MSG_ROUTING_NONE); | 
| 208 } | 215 } | 
| 209 | 216 | 
| 210 void WebContentsDestroyed() override { | 217 void WebContentsDestroyed() override { | 
| 211 RunCallback(ChildProcessHost::kInvalidUniqueID, MSG_ROUTING_NONE); | 218 RunCallback(ChildProcessHost::kInvalidUniqueID, MSG_ROUTING_NONE); | 
| 212 } | 219 } | 
| 213 | 220 | 
| 214 private: | 221 private: | 
| 215 void RunCallback(int render_process_id, int render_frame_id) { | 222 void RunCallback(int render_process_id, int render_frame_id) { | 
| 216 // After running the callback, |this| will stop observing, thus | 223 // After running the callback, |this| will stop observing, thus | 
| 217 // web_contents() should return nullptr and |RunCallback| should no longer | 224 // web_contents() should return nullptr and |RunCallback| should no longer | 
| 218 // be called. Then, |this| will self destroy. | 225 // be called. Then, |this| will self destroy. | 
| 219 DCHECK(web_contents()); | 226 DCHECK(web_contents()); | 
| 220 | 227 | 
| 221 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 228 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 
| 222 base::Bind(callback_, | 229 base::Bind(callback_, | 
| 223 render_process_id, | 230 render_process_id, | 
| 224 render_frame_id)); | 231 render_frame_id)); | 
| 225 Observe(nullptr); | 232 Observe(nullptr); | 
| 226 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | 233 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | 
| 227 } | 234 } | 
| 228 | 235 | 
| 236 int frame_tree_node_id_; | |
| 229 const OpenURLCallback callback_; | 237 const OpenURLCallback callback_; | 
| 230 | 238 | 
| 231 DISALLOW_COPY_AND_ASSIGN(OpenURLObserver); | 239 DISALLOW_COPY_AND_ASSIGN(OpenURLObserver); | 
| 232 }; | 240 }; | 
| 233 | 241 | 
| 234 void DidOpenURL(const OpenURLCallback& callback, WebContents* web_contents) { | 242 void DidOpenURL(const OpenURLCallback& callback, WebContents* web_contents) { | 
| 
Charlie Reis
2015/09/24 18:36:58
Let's add a comment saying this should only be cal
 
zino
2015/09/25 01:08:30
Done.
 | |
| 235 DCHECK(web_contents); | 243 DCHECK(web_contents); | 
| 236 | 244 | 
| 237 new OpenURLObserver(web_contents, callback); | 245 RenderFrameHostImpl* rfhi = | 
| 246 static_cast<RenderFrameHostImpl*>(web_contents->GetMainFrame()); | |
| 247 new OpenURLObserver(web_contents, | |
| 248 rfhi->frame_tree_node()->frame_tree_node_id(), callback); | |
| 238 } | 249 } | 
| 239 | 250 | 
| 240 void NavigateClientOnUI(const GURL& url, | 251 void NavigateClientOnUI(const GURL& url, | 
| 241 const GURL& script_url, | 252 const GURL& script_url, | 
| 242 int process_id, | 253 int process_id, | 
| 243 int frame_id, | 254 int frame_id, | 
| 244 const OpenURLCallback& callback) { | 255 const OpenURLCallback& callback) { | 
| 245 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 256 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 
| 246 | 257 | 
| 247 RenderFrameHost* render_frame_host = | 258 RenderFrameHostImpl* rfhi = RenderFrameHostImpl::FromID(process_id, frame_id); | 
| 248 RenderFrameHost::FromID(process_id, frame_id); | 259 WebContents* web_contents = WebContents::FromRenderFrameHost(rfhi); | 
| 249 WebContents* web_contents = | |
| 250 WebContents::FromRenderFrameHost(render_frame_host); | |
| 251 | 260 | 
| 252 if (!render_frame_host || !web_contents) { | 261 if (!rfhi || !web_contents) { | 
| 253 BrowserThread::PostTask( | 262 BrowserThread::PostTask( | 
| 254 BrowserThread::IO, FROM_HERE, | 263 BrowserThread::IO, FROM_HERE, | 
| 255 base::Bind(callback, ChildProcessHost::kInvalidUniqueID, | 264 base::Bind(callback, ChildProcessHost::kInvalidUniqueID, | 
| 256 MSG_ROUTING_NONE)); | 265 MSG_ROUTING_NONE)); | 
| 257 return; | 266 return; | 
| 258 } | 267 } | 
| 259 | 268 | 
| 269 ui::PageTransition transition = rfhi->GetParent() | |
| 270 ? ui::PAGE_TRANSITION_AUTO_SUBFRAME | |
| 271 : ui::PAGE_TRANSITION_AUTO_TOPLEVEL; | |
| 272 int frame_tree_node_id = rfhi->frame_tree_node()->frame_tree_node_id(); | |
| 273 | |
| 260 OpenURLParams params( | 274 OpenURLParams params( | 
| 261 url, Referrer::SanitizeForRequest( | 275 url, Referrer::SanitizeForRequest( | 
| 262 url, Referrer(script_url, blink::WebReferrerPolicyDefault)), | 276 url, Referrer(script_url, blink::WebReferrerPolicyDefault)), | 
| 263 CURRENT_TAB, ui::PAGE_TRANSITION_AUTO_TOPLEVEL, | 277 frame_tree_node_id, CURRENT_TAB, transition, | 
| 264 true /* is_renderer_initiated */); | 278 true /* is_renderer_initiated */); | 
| 265 web_contents->OpenURL(params); | 279 web_contents->OpenURL(params); | 
| 266 DidOpenURL(callback, web_contents); | 280 new OpenURLObserver(web_contents, frame_tree_node_id, callback); | 
| 267 } | 281 } | 
| 268 | 282 | 
| 269 void OpenWindowOnUI( | 283 void OpenWindowOnUI( | 
| 270 const GURL& url, | 284 const GURL& url, | 
| 271 const GURL& script_url, | 285 const GURL& script_url, | 
| 272 int process_id, | 286 int process_id, | 
| 273 const scoped_refptr<ServiceWorkerContextWrapper>& context_wrapper, | 287 const scoped_refptr<ServiceWorkerContextWrapper>& context_wrapper, | 
| 274 const OpenURLCallback& callback) { | 288 const OpenURLCallback& callback) { | 
| 275 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 289 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 
| 276 | 290 | 
| (...skipping 2077 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2354 void ServiceWorkerVersion::OnBeginEvent() { | 2368 void ServiceWorkerVersion::OnBeginEvent() { | 
| 2355 if (should_exclude_from_uma_ || running_status() != RUNNING || | 2369 if (should_exclude_from_uma_ || running_status() != RUNNING || | 
| 2356 idle_time_.is_null()) { | 2370 idle_time_.is_null()) { | 
| 2357 return; | 2371 return; | 
| 2358 } | 2372 } | 
| 2359 ServiceWorkerMetrics::RecordTimeBetweenEvents(base::TimeTicks::Now() - | 2373 ServiceWorkerMetrics::RecordTimeBetweenEvents(base::TimeTicks::Now() - | 
| 2360 idle_time_); | 2374 idle_time_); | 
| 2361 } | 2375 } | 
| 2362 | 2376 | 
| 2363 } // namespace content | 2377 } // namespace content | 
| OLD | NEW |