OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // Implements the Chrome Extensions WebNavigation API. | 5 // Implements the Chrome Extensions WebNavigation API. |
6 | 6 |
7 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api.h" | 7 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api.h" |
8 | 8 |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api_consta
nts.h" | 10 #include "chrome/browser/extensions/api/web_navigation/web_navigation_api_consta
nts.h" |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 details->target_url); | 209 details->target_url); |
210 } | 210 } |
211 } | 211 } |
212 | 212 |
213 void WebNavigationEventRouter::TabAdded(content::WebContents* tab) { | 213 void WebNavigationEventRouter::TabAdded(content::WebContents* tab) { |
214 std::map<content::WebContents*, PendingWebContents>::iterator iter = | 214 std::map<content::WebContents*, PendingWebContents>::iterator iter = |
215 pending_web_contents_.find(tab); | 215 pending_web_contents_.find(tab); |
216 if (iter == pending_web_contents_.end()) | 216 if (iter == pending_web_contents_.end()) |
217 return; | 217 return; |
218 | 218 |
219 helpers::DispatchOnCreatedNavigationTarget( | 219 WebNavigationTabObserver* tab_observer = |
220 iter->second.source_web_contents, | 220 WebNavigationTabObserver::Get(iter->second.source_web_contents); |
221 iter->second.target_web_contents->GetBrowserContext(), | 221 if (!tab_observer) { |
| 222 NOTREACHED(); |
| 223 return; |
| 224 } |
| 225 const FrameNavigationState& frame_navigation_state = |
| 226 tab_observer->frame_navigation_state(); |
| 227 |
| 228 FrameNavigationState::FrameID frame_id( |
222 iter->second.source_frame_id, | 229 iter->second.source_frame_id, |
223 iter->second.source_frame_is_main_frame, | 230 iter->second.source_web_contents->GetRenderViewHost()); |
224 iter->second.target_web_contents, | 231 if (frame_navigation_state.CanSendEvents(frame_id)) { |
225 iter->second.target_url); | 232 helpers::DispatchOnCreatedNavigationTarget( |
| 233 iter->second.source_web_contents, |
| 234 iter->second.target_web_contents->GetBrowserContext(), |
| 235 iter->second.source_frame_id, |
| 236 iter->second.source_frame_is_main_frame, |
| 237 iter->second.target_web_contents, |
| 238 iter->second.target_url); |
| 239 } |
226 pending_web_contents_.erase(iter); | 240 pending_web_contents_.erase(iter); |
227 } | 241 } |
228 | 242 |
229 void WebNavigationEventRouter::TabDestroyed(content::WebContents* tab) { | 243 void WebNavigationEventRouter::TabDestroyed(content::WebContents* tab) { |
230 pending_web_contents_.erase(tab); | 244 pending_web_contents_.erase(tab); |
231 for (std::map<content::WebContents*, PendingWebContents>::iterator i = | 245 for (std::map<content::WebContents*, PendingWebContents>::iterator i = |
232 pending_web_contents_.begin(); i != pending_web_contents_.end(); ) { | 246 pending_web_contents_.begin(); i != pending_web_contents_.end(); ) { |
233 if (i->second.source_web_contents == tab) | 247 if (i->second.source_web_contents == tab) |
234 pending_web_contents_.erase(i++); | 248 pending_web_contents_.erase(i++); |
235 else | 249 else |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 render_view_host = render_view_host_; | 308 render_view_host = render_view_host_; |
295 } else if (pending_render_view_host_ && | 309 } else if (pending_render_view_host_ && |
296 resource_redirect_details->origin_child_id == | 310 resource_redirect_details->origin_child_id == |
297 pending_render_view_host_->GetProcess()->GetID()) { | 311 pending_render_view_host_->GetProcess()->GetID()) { |
298 render_view_host = pending_render_view_host_; | 312 render_view_host = pending_render_view_host_; |
299 } | 313 } |
300 if (!render_view_host) | 314 if (!render_view_host) |
301 return; | 315 return; |
302 FrameNavigationState::FrameID frame_id( | 316 FrameNavigationState::FrameID frame_id( |
303 resource_redirect_details->frame_id, render_view_host); | 317 resource_redirect_details->frame_id, render_view_host); |
304 if (!navigation_state_.CanSendEvents(frame_id)) | |
305 return; | |
306 navigation_state_.SetIsServerRedirected(frame_id); | 318 navigation_state_.SetIsServerRedirected(frame_id); |
307 } | 319 } |
308 break; | 320 break; |
309 } | 321 } |
310 | 322 |
311 case content::NOTIFICATION_RENDER_VIEW_HOST_DELETED: { | 323 case content::NOTIFICATION_RENDER_VIEW_HOST_DELETED: { |
312 content::RenderViewHost* render_view_host = | 324 content::RenderViewHost* render_view_host = |
313 content::Source<content::RenderViewHost>(source).ptr(); | 325 content::Source<content::RenderViewHost>(source).ptr(); |
314 if (render_view_host == render_view_host_) | 326 if (render_view_host == render_view_host_) |
315 render_view_host_ = NULL; | 327 render_view_host_ = NULL; |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
399 SendErrorEvents( | 411 SendErrorEvents( |
400 web_contents(), render_view_host_, FrameNavigationState::FrameID()); | 412 web_contents(), render_view_host_, FrameNavigationState::FrameID()); |
401 render_view_host_ = pending_render_view_host_; | 413 render_view_host_ = pending_render_view_host_; |
402 pending_render_view_host_ = NULL; | 414 pending_render_view_host_ = NULL; |
403 } | 415 } |
404 | 416 |
405 // Update the URL as it might have changed. | 417 // Update the URL as it might have changed. |
406 navigation_state_.UpdateFrame(frame_id, url); | 418 navigation_state_.UpdateFrame(frame_id, url); |
407 navigation_state_.SetNavigationCommitted(frame_id); | 419 navigation_state_.SetNavigationCommitted(frame_id); |
408 | 420 |
| 421 if (is_reference_fragment_navigation || is_history_state_modification) |
| 422 navigation_state_.SetNavigationCompleted(frame_id); |
| 423 |
409 if (!navigation_state_.CanSendEvents(frame_id)) | 424 if (!navigation_state_.CanSendEvents(frame_id)) |
410 return; | 425 return; |
411 | 426 |
412 if (is_reference_fragment_navigation) { | 427 if (is_reference_fragment_navigation) { |
413 helpers::DispatchOnCommitted( | 428 helpers::DispatchOnCommitted( |
414 keys::kOnReferenceFragmentUpdated, | 429 keys::kOnReferenceFragmentUpdated, |
415 web_contents(), | 430 web_contents(), |
416 frame_num, | 431 frame_num, |
417 is_main_frame, | 432 is_main_frame, |
418 url, | 433 url, |
419 transition_type); | 434 transition_type); |
420 navigation_state_.SetNavigationCompleted(frame_id); | |
421 } else if (is_history_state_modification) { | 435 } else if (is_history_state_modification) { |
422 helpers::DispatchOnCommitted( | 436 helpers::DispatchOnCommitted( |
423 keys::kOnHistoryStateUpdated, | 437 keys::kOnHistoryStateUpdated, |
424 web_contents(), | 438 web_contents(), |
425 frame_num, | 439 frame_num, |
426 is_main_frame, | 440 is_main_frame, |
427 url, | 441 url, |
428 transition_type); | 442 transition_type); |
429 navigation_state_.SetNavigationCompleted(frame_id); | |
430 } else { | 443 } else { |
431 if (navigation_state_.GetIsServerRedirected(frame_id)) { | 444 if (navigation_state_.GetIsServerRedirected(frame_id)) { |
432 transition_type = static_cast<content::PageTransition>( | 445 transition_type = static_cast<content::PageTransition>( |
433 transition_type | content::PAGE_TRANSITION_SERVER_REDIRECT); | 446 transition_type | content::PAGE_TRANSITION_SERVER_REDIRECT); |
434 } | 447 } |
435 helpers::DispatchOnCommitted( | 448 helpers::DispatchOnCommitted( |
436 keys::kOnCommitted, | 449 keys::kOnCommitted, |
437 web_contents(), | 450 web_contents(), |
438 frame_num, | 451 frame_num, |
439 is_main_frame, | 452 is_main_frame, |
(...skipping 13 matching lines...) Expand all Loading... |
453 render_view_host != pending_render_view_host_) | 466 render_view_host != pending_render_view_host_) |
454 return; | 467 return; |
455 bool stop_tracking_frames = false; | 468 bool stop_tracking_frames = false; |
456 if (render_view_host == pending_render_view_host_) { | 469 if (render_view_host == pending_render_view_host_) { |
457 pending_render_view_host_ = NULL; | 470 pending_render_view_host_ = NULL; |
458 stop_tracking_frames = true; | 471 stop_tracking_frames = true; |
459 } | 472 } |
460 | 473 |
461 FrameNavigationState::FrameID frame_id(frame_num, render_view_host); | 474 FrameNavigationState::FrameID frame_id(frame_num, render_view_host); |
462 if (navigation_state_.CanSendEvents(frame_id)) { | 475 if (navigation_state_.CanSendEvents(frame_id)) { |
463 navigation_state_.SetErrorOccurredInFrame(frame_id); | |
464 helpers::DispatchOnErrorOccurred( | 476 helpers::DispatchOnErrorOccurred( |
465 web_contents(), render_view_host->GetProcess()->GetID(), validated_url, | 477 web_contents(), render_view_host->GetProcess()->GetID(), validated_url, |
466 frame_num, is_main_frame, error_code); | 478 frame_num, is_main_frame, error_code); |
467 } | 479 } |
| 480 navigation_state_.SetErrorOccurredInFrame(frame_id); |
468 if (stop_tracking_frames) { | 481 if (stop_tracking_frames) { |
469 navigation_state_.StopTrackingFramesInRVH(render_view_host, | 482 navigation_state_.StopTrackingFramesInRVH(render_view_host, |
470 FrameNavigationState::FrameID()); | 483 FrameNavigationState::FrameID()); |
471 } | 484 } |
472 } | 485 } |
473 | 486 |
474 void WebNavigationTabObserver::DocumentLoadedInFrame( | 487 void WebNavigationTabObserver::DocumentLoadedInFrame( |
475 int64 frame_num, | 488 int64 frame_num, |
476 content::RenderViewHost* render_view_host) { | 489 content::RenderViewHost* render_view_host) { |
477 if (render_view_host != render_view_host_) | 490 if (render_view_host != render_view_host_) |
478 return; | 491 return; |
479 FrameNavigationState::FrameID frame_id(frame_num, render_view_host); | 492 FrameNavigationState::FrameID frame_id(frame_num, render_view_host); |
480 if (!navigation_state_.CanSendEvents(frame_id)) | 493 if (!navigation_state_.CanSendEvents(frame_id)) |
481 return; | 494 return; |
482 helpers::DispatchOnDOMContentLoaded(web_contents(), | 495 helpers::DispatchOnDOMContentLoaded(web_contents(), |
483 navigation_state_.GetUrl(frame_id), | 496 navigation_state_.GetUrl(frame_id), |
484 navigation_state_.IsMainFrame(frame_id), | 497 navigation_state_.IsMainFrame(frame_id), |
485 frame_num); | 498 frame_num); |
486 } | 499 } |
487 | 500 |
488 void WebNavigationTabObserver::DidFinishLoad( | 501 void WebNavigationTabObserver::DidFinishLoad( |
489 int64 frame_num, | 502 int64 frame_num, |
490 const GURL& validated_url, | 503 const GURL& validated_url, |
491 bool is_main_frame, | 504 bool is_main_frame, |
492 content::RenderViewHost* render_view_host) { | 505 content::RenderViewHost* render_view_host) { |
493 if (render_view_host != render_view_host_) | 506 if (render_view_host != render_view_host_) |
494 return; | 507 return; |
495 FrameNavigationState::FrameID frame_id(frame_num, render_view_host); | 508 FrameNavigationState::FrameID frame_id(frame_num, render_view_host); |
| 509 navigation_state_.SetNavigationCompleted(frame_id); |
496 if (!navigation_state_.CanSendEvents(frame_id)) | 510 if (!navigation_state_.CanSendEvents(frame_id)) |
497 return; | 511 return; |
498 navigation_state_.SetNavigationCompleted(frame_id); | |
499 DCHECK_EQ(navigation_state_.GetUrl(frame_id), validated_url); | 512 DCHECK_EQ(navigation_state_.GetUrl(frame_id), validated_url); |
500 DCHECK_EQ(navigation_state_.IsMainFrame(frame_id), is_main_frame); | 513 DCHECK_EQ(navigation_state_.IsMainFrame(frame_id), is_main_frame); |
501 helpers::DispatchOnCompleted(web_contents(), | 514 helpers::DispatchOnCompleted(web_contents(), |
502 validated_url, | 515 validated_url, |
503 is_main_frame, | 516 is_main_frame, |
504 frame_num); | 517 frame_num); |
505 } | 518 } |
506 | 519 |
507 void WebNavigationTabObserver::DidFailLoad( | 520 void WebNavigationTabObserver::DidFailLoad( |
508 int64 frame_num, | 521 int64 frame_num, |
509 const GURL& validated_url, | 522 const GURL& validated_url, |
510 bool is_main_frame, | 523 bool is_main_frame, |
511 int error_code, | 524 int error_code, |
512 const string16& error_description, | 525 const string16& error_description, |
513 content::RenderViewHost* render_view_host) { | 526 content::RenderViewHost* render_view_host) { |
514 if (render_view_host != render_view_host_) | 527 if (render_view_host != render_view_host_) |
515 return; | 528 return; |
516 FrameNavigationState::FrameID frame_id(frame_num, render_view_host); | 529 FrameNavigationState::FrameID frame_id(frame_num, render_view_host); |
517 if (!navigation_state_.CanSendEvents(frame_id)) | 530 // A navigation might fail before we even started a provisional load. |
| 531 if (!navigation_state_.IsValidFrame(frame_id)) |
518 return; | 532 return; |
| 533 if (navigation_state_.CanSendEvents(frame_id)) { |
| 534 helpers::DispatchOnErrorOccurred( |
| 535 web_contents(), render_view_host->GetProcess()->GetID(), validated_url, |
| 536 frame_num, is_main_frame, error_code); |
| 537 } |
519 navigation_state_.SetErrorOccurredInFrame(frame_id); | 538 navigation_state_.SetErrorOccurredInFrame(frame_id); |
520 helpers::DispatchOnErrorOccurred( | |
521 web_contents(), render_view_host->GetProcess()->GetID(), validated_url, | |
522 frame_num, is_main_frame, error_code); | |
523 } | 539 } |
524 | 540 |
525 void WebNavigationTabObserver::DidOpenRequestedURL( | 541 void WebNavigationTabObserver::DidOpenRequestedURL( |
526 content::WebContents* new_contents, | 542 content::WebContents* new_contents, |
527 const GURL& url, | 543 const GURL& url, |
528 const content::Referrer& referrer, | 544 const content::Referrer& referrer, |
529 WindowOpenDisposition disposition, | 545 WindowOpenDisposition disposition, |
530 content::PageTransition transition, | 546 content::PageTransition transition, |
531 int64 source_frame_num) { | 547 int64 source_frame_num) { |
532 FrameNavigationState::FrameID frame_id(source_frame_num, render_view_host_); | 548 FrameNavigationState::FrameID frame_id(source_frame_num, render_view_host_); |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
688 navigation_state.IsMainFrame(frame_id), frame_id.frame_num); | 704 navigation_state.IsMainFrame(frame_id), frame_id.frame_num); |
689 frame->process_id = frame_id.render_view_host->GetProcess()->GetID(); | 705 frame->process_id = frame_id.render_view_host->GetProcess()->GetID(); |
690 frame->error_occurred = navigation_state.GetErrorOccurredInFrame(frame_id); | 706 frame->error_occurred = navigation_state.GetErrorOccurredInFrame(frame_id); |
691 result_list.push_back(frame); | 707 result_list.push_back(frame); |
692 } | 708 } |
693 results_ = GetAllFrames::Results::Create(result_list); | 709 results_ = GetAllFrames::Results::Create(result_list); |
694 return true; | 710 return true; |
695 } | 711 } |
696 | 712 |
697 } // namespace extensions | 713 } // namespace extensions |
OLD | NEW |