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

Unified Diff: chrome/browser/printing/background_printing_manager.cc

Issue 7574002: Be able to print items that do window.print(); window.close() (airline tix e.g.) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed Lei's comments. Created 9 years, 4 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/printing/background_printing_manager.cc
diff --git a/chrome/browser/printing/background_printing_manager.cc b/chrome/browser/printing/background_printing_manager.cc
index 478f1800322ba789d40d1dc73bac41381d37fae8..c3a56c58304932aa8fe145b38006e9b99d87708d 100644
--- a/chrome/browser/printing/background_printing_manager.cc
+++ b/chrome/browser/printing/background_printing_manager.cc
@@ -29,37 +29,66 @@ BackgroundPrintingManager::~BackgroundPrintingManager() {
// TODO(thestig) handle this case better.
}
-void BackgroundPrintingManager::OwnTabContents(TabContentsWrapper* contents) {
+void BackgroundPrintingManager::OwnPreviewTabContents(
+ TabContentsWrapper* contents) {
DCHECK(CalledOnValidThread());
DCHECK(printing::PrintPreviewTabController::IsPrintPreviewTab(
contents->tab_contents()));
CHECK(printing_contents_.find(contents) == printing_contents_.end());
- printing_contents_.insert(contents);
-
- registrar_.Add(this, chrome::NOTIFICATION_PRINT_JOB_RELEASED,
- Source<TabContentsWrapper>(contents));
- registrar_.Add(this, content::NOTIFICATION_TAB_CONTENTS_DESTROYED,
- Source<TabContents>(contents->tab_contents()));
+ // Find the initiator tab.
+ TabContents* initiator_tab = NULL;
+ TabContentsWrapper* initiator_tab_wrapper = NULL;
+ printing::PrintPreviewTabController* tab_controller =
+ printing::PrintPreviewTabController::GetInstance();
+ if (tab_controller)
+ initiator_tab = tab_controller->GetInitiatorTab(contents->tab_contents());
+ if (initiator_tab) {
+ initiator_tab_wrapper =
+ TabContentsWrapper::GetCurrentWrapperForContents(initiator_tab);
+ }
- // Detach |contents| from its tab strip.
- Browser* browser = BrowserList::FindBrowserWithID(
- contents->restore_tab_helper()->window_id().id());
- DCHECK(browser);
+ bool initiator_tab_exists = initiator_tab_wrapper &&
+ !ReleaseInitiatorTabContents(initiator_tab_wrapper);
Lei Zhang 2011/08/27 01:05:06 Why do you attempt to destroy the initiator tab he
+ OwnTabContents(contents, contents, &printing_contents_);
+ // Activate the initiator tab if it exists.
+ if (initiator_tab_exists)
+ static_cast<RenderViewHostDelegate*>(initiator_tab)->Activate();
+}
- TabStripModel* tabstrip = browser->tabstrip_model();
- tabstrip->DetachTabContentsAt(tabstrip->GetIndexOfTabContents(contents));
+bool BackgroundPrintingManager::OwnInitiatorTabContents(
+ TabContentsWrapper* contents) {
+ DCHECK(CalledOnValidThread());
+ DCHECK(!printing::PrintPreviewTabController::IsPrintPreviewTab(
+ contents->tab_contents()));
+ CHECK(initiator_contents_.find(contents) == initiator_contents_.end());
- // Activate the initiator tab.
printing::PrintPreviewTabController* tab_controller =
printing::PrintPreviewTabController::GetInstance();
if (!tab_controller)
- return;
- TabContents* initiator_tab = tab_controller->GetInitiatorTab(
- contents->tab_contents());
- if (!initiator_tab)
- return;
- static_cast<RenderViewHostDelegate*>(initiator_tab)->Activate();
+ return false;
+
+ TabContents* preview_tab =
+ tab_controller->GetPrintPreviewForTab(contents->tab_contents());
+ if (!preview_tab)
+ return false;
+
+ TabContentsWrapper* preview_contents =
+ TabContentsWrapper::GetCurrentWrapperForContents(preview_tab);
+
+ OwnTabContents(contents, preview_contents, &initiator_contents_);
+
+ return true;
+}
+
+bool BackgroundPrintingManager::ReleaseInitiatorTabContents(
+ TabContentsWrapper* contents) {
+ if (initiator_contents_.find(contents) == initiator_contents_.end())
+ return false;
+
+ initiator_contents_.erase(contents);
+ MessageLoop::current()->DeleteSoon(FROM_HERE, contents);
+ return true;
}
void BackgroundPrintingManager::Observe(int type,
@@ -74,6 +103,7 @@ void BackgroundPrintingManager::Observe(int type,
// This might be happening in the middle of a RenderViewGone() loop.
// Deleting |contents| later so the RenderViewGone() loop can finish.
MessageLoop::current()->DeleteSoon(FROM_HERE, tab);
+ ReleaseInitiatorForPreviewTabContents(tab);
break;
}
case content::NOTIFICATION_TAB_CONTENTS_DESTROYED: {
@@ -88,6 +118,7 @@ void BackgroundPrintingManager::Observe(int type,
registrar_.Remove(this, content::NOTIFICATION_TAB_CONTENTS_DESTROYED,
Source<TabContents>(tab->tab_contents()));
printing_contents_.erase(tab);
+ ReleaseInitiatorForPreviewTabContents(tab);
break;
}
default: {
@@ -97,17 +128,69 @@ void BackgroundPrintingManager::Observe(int type,
}
}
-std::set<TabContentsWrapper*>::const_iterator
+bool BackgroundPrintingManager::ReleaseInitiatorForPreviewTabContents(
+ TabContentsWrapper* preview_content) {
+ DCHECK(CalledOnValidThread());
+ printing::PrintPreviewTabController* tab_controller =
+ printing::PrintPreviewTabController::GetInstance();
+
+ if (!tab_controller)
+ return false;
+
+ TabContents* initiator_tab = tab_controller->GetInitiatorTab(
+ preview_content->tab_contents());
+ if (!initiator_tab)
+ return false;
+
+ TabContentsWrapper* initiator_wrapper =
+ TabContentsWrapper::GetCurrentWrapperForContents(initiator_tab);
+
+ return ReleaseInitiatorTabContents(initiator_wrapper);
+}
+
+void BackgroundPrintingManager::OwnTabContents(
+ TabContentsWrapper* contents,
+ TabContentsWrapper* source_contents,
+ TabContentsWrapperSet* wrapper_set) {
+ CHECK(source_contents);
+ DCHECK(printing::PrintPreviewTabController::IsPrintPreviewTab(
+ source_contents->tab_contents()));
+
+ wrapper_set->insert(contents);
+
+ if (!registrar_.IsRegistered(this, chrome::NOTIFICATION_PRINT_JOB_RELEASED,
+ Source<TabContentsWrapper>(source_contents))) {
+ registrar_.Add(this, chrome::NOTIFICATION_PRINT_JOB_RELEASED,
+ Source<TabContentsWrapper>(source_contents));
+ }
+ if (!registrar_.IsRegistered(
+ this, content::NOTIFICATION_TAB_CONTENTS_DESTROYED,
+ Source<TabContents>(source_contents->tab_contents()))) {
+ registrar_.Add(this, content::NOTIFICATION_TAB_CONTENTS_DESTROYED,
+ Source<TabContents>(source_contents->tab_contents()));
+ }
+
+ // Detach |contents| from its tab strip.
+ Browser* browser = BrowserList::FindBrowserWithID(
+ contents->restore_tab_helper()->window_id().id());
+ DCHECK(browser);
+
+ TabStripModel* tabstrip = browser->tabstrip_model();
+ tabstrip->DetachTabContentsAt(tabstrip->GetIndexOfTabContents(contents));
+}
+
+BackgroundPrintingManager::TabContentsWrapperSet::const_iterator
BackgroundPrintingManager::begin() {
return printing_contents_.begin();
}
-std::set<TabContentsWrapper*>::const_iterator
+BackgroundPrintingManager::TabContentsWrapperSet::const_iterator
BackgroundPrintingManager::end() {
return printing_contents_.end();
}
-bool BackgroundPrintingManager::HasTabContents(TabContentsWrapper* entry) {
+bool BackgroundPrintingManager::HasPreviewTabContents(
+ TabContentsWrapper* entry) {
return printing_contents_.find(entry) != printing_contents_.end();
}

Powered by Google App Engine
This is Rietveld 408576698