Chromium Code Reviews| 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..a0111f5e298dc52dae74a46b58907a2a3b4133d6 100644 |
| --- a/chrome/browser/printing/background_printing_manager.cc |
| +++ b/chrome/browser/printing/background_printing_manager.cc |
| @@ -29,37 +29,59 @@ BackgroundPrintingManager::~BackgroundPrintingManager() { |
| // TODO(thestig) handle this case better. |
| } |
| -void BackgroundPrintingManager::OwnTabContents(TabContentsWrapper* contents) { |
| - DCHECK(CalledOnValidThread()); |
|
Lei Zhang
2011/08/19 19:00:23
I would keep this here and do the check up front i
Sheridan Rawlins
2011/08/25 22:52:55
Done.
|
| +void BackgroundPrintingManager::OwnPreviewTabContents( |
| + TabContentsWrapper* content) { |
| DCHECK(printing::PrintPreviewTabController::IsPrintPreviewTab( |
| - contents->tab_contents())); |
| - CHECK(printing_contents_.find(contents) == printing_contents_.end()); |
| - |
| - printing_contents_.insert(contents); |
| + content->tab_contents())); |
| + CHECK(printing_contents_.find(content) == printing_contents_.end()); |
| - 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) |
|
Lei Zhang
2011/08/19 19:00:23
I highly prefer to handle the style where we handl
Sheridan Rawlins
2011/08/25 22:52:55
Done.
|
| + initiator_tab = tab_controller->GetInitiatorTab(content->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/19 19:00:23
Why do you call ReleaseInitiatorTabContents() here
Sheridan Rawlins
2011/08/25 22:52:55
Well, this is the place where we hope to switch to
|
| + OwnTabContents(content, content, &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* content) { |
| + DCHECK(!printing::PrintPreviewTabController::IsPrintPreviewTab( |
| + content->tab_contents())); |
| - // 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(); |
| + TabContents* preview_tab = |
| + tab_controller->GetPrintPreviewForTab(content->tab_contents()); |
| + if (!preview_tab) |
| + return false; |
| + TabContentsWrapper* preview_contents = |
| + TabContentsWrapper::GetCurrentWrapperForContents(preview_tab); |
| + |
| + OwnTabContents(content, preview_contents, &initiator_contents_); |
| + |
| + return true; |
| +} |
| + |
| +bool BackgroundPrintingManager::ReleaseInitiatorTabContents( |
| + TabContentsWrapper* content) { |
| + if (initiator_contents_.find(content) == initiator_contents_.end()) |
| + return false; |
| + |
| + initiator_contents_.erase(content); |
| + MessageLoop::current()->DeleteSoon(FROM_HERE, content); |
| + return true; |
| } |
| void BackgroundPrintingManager::Observe(int type, |
| @@ -74,6 +96,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 +111,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,6 +121,60 @@ void BackgroundPrintingManager::Observe(int type, |
| } |
| } |
| +bool BackgroundPrintingManager::ReleaseInitiatorForPreviewTabContents( |
| + TabContentsWrapper* preview_content) { |
| + 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) { |
| + DCHECK(CalledOnValidThread()); |
| + DCHECK(!source_contents || |
| + printing::PrintPreviewTabController::IsPrintPreviewTab( |
| + source_contents->tab_contents())); |
| + CHECK(wrapper_set->find(contents) == wrapper_set->end()); |
| + |
| + wrapper_set->insert(contents); |
| + |
| + if (source_contents) { |
|
Lei Zhang
2011/08/19 19:00:23
When is |source_contents| ever NULL? It seems you
Sheridan Rawlins
2011/08/25 22:52:55
Done.
|
| + 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)); |
| +} |
| + |
| std::set<TabContentsWrapper*>::const_iterator |
| BackgroundPrintingManager::begin() { |
| return printing_contents_.begin(); |
| @@ -107,8 +185,23 @@ std::set<TabContentsWrapper*>::const_iterator |
| return printing_contents_.end(); |
| } |
| +std::set<TabContentsWrapper*>::const_iterator |
| + BackgroundPrintingManager::begin_initiator() { |
| + return initiator_contents_.begin(); |
| +} |
| + |
| +std::set<TabContentsWrapper*>::const_iterator |
| + BackgroundPrintingManager::end_initiator() { |
| + return initiator_contents_.end(); |
| +} |
| + |
| bool BackgroundPrintingManager::HasTabContents(TabContentsWrapper* entry) { |
| return printing_contents_.find(entry) != printing_contents_.end(); |
| } |
| +bool BackgroundPrintingManager::HasInitiatorTabContents( |
| + TabContentsWrapper* entry) { |
| + return initiator_contents_.find(entry) != initiator_contents_.end(); |
| +} |
| + |
| } // namespace printing |