Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(280)

Side by Side Diff: chrome/browser/printing/print_preview_dialog_controller.cc

Issue 17500003: Close web contents modal dialogs on content load start (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Don't observe nav committed for initiator tab Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 #include "chrome/browser/printing/print_preview_dialog_controller.h" 5 #include "chrome/browser/printing/print_preview_dialog_controller.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after
296 return (url.SchemeIs(chrome::kChromeUIScheme) && 296 return (url.SchemeIs(chrome::kChromeUIScheme) &&
297 url.host() == chrome::kChromeUIPrintHost); 297 url.host() == chrome::kChromeUIPrintHost);
298 } 298 }
299 299
300 void PrintPreviewDialogController::EraseInitiatorTabInfo( 300 void PrintPreviewDialogController::EraseInitiatorTabInfo(
301 WebContents* preview_dialog) { 301 WebContents* preview_dialog) {
302 PrintPreviewDialogMap::iterator it = preview_dialog_map_.find(preview_dialog); 302 PrintPreviewDialogMap::iterator it = preview_dialog_map_.find(preview_dialog);
303 if (it == preview_dialog_map_.end()) 303 if (it == preview_dialog_map_.end())
304 return; 304 return;
305 305
306 RemoveObservers(it->second); 306 RemoveObservers(it->second, INITIATOR_TAB);
307 preview_dialog_map_[preview_dialog] = NULL; 307 preview_dialog_map_[preview_dialog] = NULL;
308 } 308 }
309 309
310 PrintPreviewDialogController::~PrintPreviewDialogController() {} 310 PrintPreviewDialogController::~PrintPreviewDialogController() {}
311 311
312 void PrintPreviewDialogController::OnRendererProcessClosed( 312 void PrintPreviewDialogController::OnRendererProcessClosed(
313 content::RenderProcessHost* rph) { 313 content::RenderProcessHost* rph) {
314 // Store contents in a vector and deal with them after iterating through 314 // Store contents in a vector and deal with them after iterating through
315 // |preview_dialog_map_| because RemoveFoo() can change |preview_dialog_map_|. 315 // |preview_dialog_map_| because RemoveFoo() can change |preview_dialog_map_|.
316 std::vector<WebContents*> closed_initiator_tabs; 316 std::vector<WebContents*> closed_initiator_tabs;
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
354 } 354 }
355 355
356 void PrintPreviewDialogController::OnNavEntryCommitted( 356 void PrintPreviewDialogController::OnNavEntryCommitted(
357 WebContents* contents, content::LoadCommittedDetails* details) { 357 WebContents* contents, content::LoadCommittedDetails* details) {
358 WebContents* preview_dialog = GetPrintPreviewForContents(contents); 358 WebContents* preview_dialog = GetPrintPreviewForContents(contents);
359 if (!preview_dialog) { 359 if (!preview_dialog) {
360 NOTREACHED(); 360 NOTREACHED();
361 return; 361 return;
362 } 362 }
363 363
364 if (contents == preview_dialog) { 364 DCHECK(contents == preview_dialog);
Lei Zhang 2013/06/27 21:36:18 nit: DCHECK_EQ(preview_dialog, content);
Mike Wittman 2013/06/27 23:39:38 Done.
365 // Preview dialog navigated.
366 if (details) {
367 content::PageTransition transition_type =
368 details->entry->GetTransitionType();
369 content::NavigationType nav_type = details->type;
370 365
371 // New |preview_dialog| is created. Don't update/erase map entry. 366 // Preview dialog navigated.
372 if (waiting_for_new_preview_page_ && 367 if (details) {
373 transition_type == content::PAGE_TRANSITION_AUTO_TOPLEVEL && 368 content::PageTransition transition_type =
374 nav_type == content::NAVIGATION_TYPE_NEW_PAGE) { 369 details->entry->GetTransitionType();
375 waiting_for_new_preview_page_ = false; 370 content::NavigationType nav_type = details->type;
376 SaveInitiatorTabTitle(preview_dialog);
377 return;
378 }
379 371
380 // Cloud print sign-in causes a reload. 372 // New |preview_dialog| is created. Don't update/erase map entry.
381 if (!waiting_for_new_preview_page_ && 373 if (waiting_for_new_preview_page_ &&
382 transition_type == content::PAGE_TRANSITION_RELOAD && 374 transition_type == content::PAGE_TRANSITION_AUTO_TOPLEVEL &&
383 nav_type == content::NAVIGATION_TYPE_EXISTING_PAGE && 375 nav_type == content::NAVIGATION_TYPE_NEW_PAGE) {
384 IsPrintPreviewURL(details->previous_url)) { 376 waiting_for_new_preview_page_ = false;
385 return; 377 SaveInitiatorTabTitle(preview_dialog);
386 } 378 return;
387 } 379 }
388 NOTREACHED(); 380
389 return; 381 // Cloud print sign-in causes a reload.
382 if (!waiting_for_new_preview_page_ &&
383 transition_type == content::PAGE_TRANSITION_RELOAD &&
384 nav_type == content::NAVIGATION_TYPE_EXISTING_PAGE &&
385 IsPrintPreviewURL(details->previous_url)) {
386 return;
387 }
390 } 388 }
391 389
392 RemoveInitiatorTab(contents); 390 NOTREACHED();
393 } 391 }
394 392
395 WebContents* PrintPreviewDialogController::CreatePrintPreviewDialog( 393 WebContents* PrintPreviewDialogController::CreatePrintPreviewDialog(
396 WebContents* initiator_tab) { 394 WebContents* initiator_tab) {
397 base::AutoReset<bool> auto_reset(&is_creating_print_preview_dialog_, true); 395 base::AutoReset<bool> auto_reset(&is_creating_print_preview_dialog_, true);
398 Profile* profile = 396 Profile* profile =
399 Profile::FromBrowserContext(initiator_tab->GetBrowserContext()); 397 Profile::FromBrowserContext(initiator_tab->GetBrowserContext());
400 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kChromeFrame)) { 398 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kChromeFrame)) {
401 // Chrome Frame only ever runs on the native desktop, so it is safe to 399 // Chrome Frame only ever runs on the native desktop, so it is safe to
402 // create the popup on the native desktop. 400 // create the popup on the native desktop.
(...skipping 19 matching lines...) Expand all
422 pp_wcd, 420 pp_wcd,
423 initiator_tab); 421 initiator_tab);
424 WebContents* preview_dialog = constrained_delegate->GetWebContents(); 422 WebContents* preview_dialog = constrained_delegate->GetWebContents();
425 EnableInternalPDFPluginForContents(preview_dialog); 423 EnableInternalPDFPluginForContents(preview_dialog);
426 PrintViewManager::CreateForWebContents(preview_dialog); 424 PrintViewManager::CreateForWebContents(preview_dialog);
427 425
428 // Add an entry to the map. 426 // Add an entry to the map.
429 preview_dialog_map_[preview_dialog] = initiator_tab; 427 preview_dialog_map_[preview_dialog] = initiator_tab;
430 waiting_for_new_preview_page_ = true; 428 waiting_for_new_preview_page_ = true;
431 429
432 AddObservers(initiator_tab); 430 AddObservers(initiator_tab, INITIATOR_TAB);
433 AddObservers(preview_dialog); 431 AddObservers(preview_dialog, PREVIEW_TAB);
434 432
435 return preview_dialog; 433 return preview_dialog;
436 } 434 }
437 435
438 void PrintPreviewDialogController::SaveInitiatorTabTitle( 436 void PrintPreviewDialogController::SaveInitiatorTabTitle(
439 WebContents* preview_dialog) { 437 WebContents* preview_dialog) {
440 WebContents* initiator_tab = GetInitiatorTab(preview_dialog); 438 WebContents* initiator_tab = GetInitiatorTab(preview_dialog);
441 if (initiator_tab && preview_dialog->GetWebUI()) { 439 if (initiator_tab && preview_dialog->GetWebUI()) {
442 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( 440 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(
443 preview_dialog->GetWebUI()->GetController()); 441 preview_dialog->GetWebUI()->GetController());
444 print_preview_ui->SetInitiatorTabTitle( 442 print_preview_ui->SetInitiatorTabTitle(
445 PrintViewManager::FromWebContents(initiator_tab)->RenderSourceName()); 443 PrintViewManager::FromWebContents(initiator_tab)->RenderSourceName());
446 } 444 }
447 } 445 }
448 446
449 void PrintPreviewDialogController::AddObservers(WebContents* contents) { 447 void PrintPreviewDialogController::AddObservers(WebContents* contents,
448 TabType tab_type) {
450 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, 449 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED,
451 content::Source<WebContents>(contents)); 450 content::Source<WebContents>(contents));
452 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, 451 if (tab_type == PREVIEW_TAB) {
453 content::Source<NavigationController>(&contents->GetController())); 452 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
453 content::Source<NavigationController>(&contents->GetController()));
454 }
454 455
455 // Multiple sites may share the same RenderProcessHost, so check if this 456 // Multiple sites may share the same RenderProcessHost, so check if this
456 // notification has already been added. 457 // notification has already been added.
457 content::Source<content::RenderProcessHost> rph_source( 458 content::Source<content::RenderProcessHost> rph_source(
458 contents->GetRenderProcessHost()); 459 contents->GetRenderProcessHost());
459 if (!registrar_.IsRegistered(this, 460 if (!registrar_.IsRegistered(this,
460 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, rph_source)) { 461 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, rph_source)) {
461 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, 462 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED,
462 rph_source); 463 rph_source);
463 } 464 }
464 } 465 }
465 466
466 void PrintPreviewDialogController::RemoveObservers(WebContents* contents) { 467 void PrintPreviewDialogController::RemoveObservers(WebContents* contents,
468 TabType tab_type) {
467 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, 469 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED,
468 content::Source<WebContents>(contents)); 470 content::Source<WebContents>(contents));
469 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, 471 if (tab_type == PREVIEW_TAB) {
470 content::Source<NavigationController>(&contents->GetController())); 472 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
473 content::Source<NavigationController>(&contents->GetController()));
474 }
471 475
472 // Multiple sites may share the same RenderProcessHost, so check if this 476 // Multiple sites may share the same RenderProcessHost, so check if this
473 // notification has already been added. 477 // notification has already been added.
474 content::Source<content::RenderProcessHost> rph_source( 478 content::Source<content::RenderProcessHost> rph_source(
475 contents->GetRenderProcessHost()); 479 contents->GetRenderProcessHost());
476 if (registrar_.IsRegistered(this, 480 if (registrar_.IsRegistered(this,
477 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, rph_source)) { 481 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, rph_source)) {
478 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, 482 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED,
479 rph_source); 483 rph_source);
480 } 484 }
481 } 485 }
482 486
483 void PrintPreviewDialogController::RemoveInitiatorTab( 487 void PrintPreviewDialogController::RemoveInitiatorTab(
484 WebContents* initiator_tab) { 488 WebContents* initiator_tab) {
485 WebContents* preview_dialog = GetPrintPreviewForContents(initiator_tab); 489 WebContents* preview_dialog = GetPrintPreviewForContents(initiator_tab);
486 DCHECK(preview_dialog); 490 DCHECK(preview_dialog);
487 // Update the map entry first, so when the print preview dialog gets destroyed 491 // Update the map entry first, so when the print preview dialog gets destroyed
488 // and reaches RemovePreviewDialog(), it does not attempt to also remove the 492 // and reaches RemovePreviewDialog(), it does not attempt to also remove the
489 // initiator tab's observers. 493 // initiator tab's observers.
490 preview_dialog_map_[preview_dialog] = NULL; 494 preview_dialog_map_[preview_dialog] = NULL;
491 RemoveObservers(initiator_tab); 495 RemoveObservers(initiator_tab, INITIATOR_TAB);
492 496
493 PrintViewManager::FromWebContents(initiator_tab)->PrintPreviewDone(); 497 PrintViewManager::FromWebContents(initiator_tab)->PrintPreviewDone();
494 498
495 // Initiator tab is closed. Close the print preview dialog too. 499 // Initiator tab is closed. Close the print preview dialog too.
496 PrintPreviewUI* print_preview_ui = 500 PrintPreviewUI* print_preview_ui =
497 static_cast<PrintPreviewUI*>(preview_dialog->GetWebUI()->GetController()); 501 static_cast<PrintPreviewUI*>(preview_dialog->GetWebUI()->GetController());
498 if (print_preview_ui) 502 if (print_preview_ui)
499 print_preview_ui->OnInitiatorTabClosed(); 503 print_preview_ui->OnInitiatorTabClosed();
500 } 504 }
501 505
502 void PrintPreviewDialogController::RemovePreviewDialog( 506 void PrintPreviewDialogController::RemovePreviewDialog(
503 WebContents* preview_dialog) { 507 WebContents* preview_dialog) {
504 // Remove the initiator tab's observers before erasing the mapping. 508 // Remove the initiator tab's observers before erasing the mapping.
505 WebContents* initiator_tab = GetInitiatorTab(preview_dialog); 509 WebContents* initiator_tab = GetInitiatorTab(preview_dialog);
506 if (initiator_tab) { 510 if (initiator_tab) {
507 RemoveObservers(initiator_tab); 511 RemoveObservers(initiator_tab, INITIATOR_TAB);
508 PrintViewManager::FromWebContents(initiator_tab)->PrintPreviewDone(); 512 PrintViewManager::FromWebContents(initiator_tab)->PrintPreviewDone();
509 } 513 }
510 514
511 // Print preview WebContents is destroyed. Notify |PrintPreviewUI| to abort 515 // Print preview WebContents is destroyed. Notify |PrintPreviewUI| to abort
512 // the initiator tab preview request. 516 // the initiator tab preview request.
513 PrintPreviewUI* print_preview_ui = 517 PrintPreviewUI* print_preview_ui =
514 static_cast<PrintPreviewUI*>(preview_dialog->GetWebUI()->GetController()); 518 static_cast<PrintPreviewUI*>(preview_dialog->GetWebUI()->GetController());
515 if (print_preview_ui) 519 if (print_preview_ui)
516 print_preview_ui->OnPrintPreviewDialogDestroyed(); 520 print_preview_ui->OnPrintPreviewDialogDestroyed();
517 521
518 preview_dialog_map_.erase(preview_dialog); 522 preview_dialog_map_.erase(preview_dialog);
519 RemoveObservers(preview_dialog); 523 RemoveObservers(preview_dialog, PREVIEW_TAB);
520 } 524 }
521 525
522 } // namespace printing 526 } // namespace printing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698