Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/background_printing_manager.h" | 5 #include "chrome/browser/printing/background_printing_manager.h" |
| 6 | 6 |
| 7 #include "chrome/browser/printing/print_job.h" | 7 #include "chrome/browser/printing/print_job.h" |
| 8 #include "chrome/browser/printing/print_preview_tab_controller.h" | 8 #include "chrome/browser/printing/print_preview_tab_controller.h" |
| 9 #include "chrome/browser/sessions/restore_tab_helper.h" | 9 #include "chrome/browser/sessions/restore_tab_helper.h" |
| 10 #include "chrome/browser/tabs/tab_strip_model.h" | 10 #include "chrome/browser/tabs/tab_strip_model.h" |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 22 } | 22 } |
| 23 | 23 |
| 24 BackgroundPrintingManager::~BackgroundPrintingManager() { | 24 BackgroundPrintingManager::~BackgroundPrintingManager() { |
| 25 DCHECK(CalledOnValidThread()); | 25 DCHECK(CalledOnValidThread()); |
| 26 // The might be some TabContentsWrappers still in |printing_contents_| at | 26 // The might be some TabContentsWrappers still in |printing_contents_| at |
| 27 // this point. E.g. when the last remaining tab is a print preview tab and | 27 // this point. E.g. when the last remaining tab is a print preview tab and |
| 28 // tries to print. In which case it will fail to print. | 28 // tries to print. In which case it will fail to print. |
| 29 // TODO(thestig) handle this case better. | 29 // TODO(thestig) handle this case better. |
| 30 } | 30 } |
| 31 | 31 |
| 32 void BackgroundPrintingManager::OwnTabContents(TabContentsWrapper* contents) { | 32 void BackgroundPrintingManager::OwnPreviewTabContents( |
| 33 TabContentsWrapper* contents) { | |
| 33 DCHECK(CalledOnValidThread()); | 34 DCHECK(CalledOnValidThread()); |
| 34 DCHECK(printing::PrintPreviewTabController::IsPrintPreviewTab( | 35 DCHECK(printing::PrintPreviewTabController::IsPrintPreviewTab( |
| 35 contents->tab_contents())); | 36 contents->tab_contents())); |
| 36 CHECK(printing_contents_.find(contents) == printing_contents_.end()); | 37 CHECK(printing_contents_.find(contents) == printing_contents_.end()); |
| 37 | 38 |
| 38 printing_contents_.insert(contents); | 39 // Find the initiator tab. |
| 40 TabContents* initiator_tab = NULL; | |
| 41 TabContentsWrapper* initiator_tab_wrapper = NULL; | |
| 42 printing::PrintPreviewTabController* tab_controller = | |
| 43 printing::PrintPreviewTabController::GetInstance(); | |
| 44 if (tab_controller) | |
| 45 initiator_tab = tab_controller->GetInitiatorTab(contents->tab_contents()); | |
| 46 if (initiator_tab) { | |
| 47 initiator_tab_wrapper = | |
| 48 TabContentsWrapper::GetCurrentWrapperForContents(initiator_tab); | |
| 49 } | |
| 39 | 50 |
| 40 registrar_.Add(this, chrome::NOTIFICATION_PRINT_JOB_RELEASED, | 51 bool initiator_tab_exists = initiator_tab_wrapper && |
| 41 Source<TabContentsWrapper>(contents)); | 52 !ReleaseInitiatorTabContents(initiator_tab_wrapper); |
|
Lei Zhang
2011/08/27 01:05:06
Why do you attempt to destroy the initiator tab he
| |
| 42 registrar_.Add(this, content::NOTIFICATION_TAB_CONTENTS_DESTROYED, | 53 OwnTabContents(contents, contents, &printing_contents_); |
| 43 Source<TabContents>(contents->tab_contents())); | 54 // Activate the initiator tab if it exists. |
| 55 if (initiator_tab_exists) | |
| 56 static_cast<RenderViewHostDelegate*>(initiator_tab)->Activate(); | |
| 57 } | |
| 44 | 58 |
| 45 // Detach |contents| from its tab strip. | 59 bool BackgroundPrintingManager::OwnInitiatorTabContents( |
| 46 Browser* browser = BrowserList::FindBrowserWithID( | 60 TabContentsWrapper* contents) { |
| 47 contents->restore_tab_helper()->window_id().id()); | 61 DCHECK(CalledOnValidThread()); |
| 48 DCHECK(browser); | 62 DCHECK(!printing::PrintPreviewTabController::IsPrintPreviewTab( |
| 63 contents->tab_contents())); | |
| 64 CHECK(initiator_contents_.find(contents) == initiator_contents_.end()); | |
| 49 | 65 |
| 50 TabStripModel* tabstrip = browser->tabstrip_model(); | |
| 51 tabstrip->DetachTabContentsAt(tabstrip->GetIndexOfTabContents(contents)); | |
| 52 | |
| 53 // Activate the initiator tab. | |
| 54 printing::PrintPreviewTabController* tab_controller = | 66 printing::PrintPreviewTabController* tab_controller = |
| 55 printing::PrintPreviewTabController::GetInstance(); | 67 printing::PrintPreviewTabController::GetInstance(); |
| 56 if (!tab_controller) | 68 if (!tab_controller) |
| 57 return; | 69 return false; |
| 58 TabContents* initiator_tab = tab_controller->GetInitiatorTab( | 70 |
| 59 contents->tab_contents()); | 71 TabContents* preview_tab = |
| 60 if (!initiator_tab) | 72 tab_controller->GetPrintPreviewForTab(contents->tab_contents()); |
| 61 return; | 73 if (!preview_tab) |
| 62 static_cast<RenderViewHostDelegate*>(initiator_tab)->Activate(); | 74 return false; |
| 75 | |
| 76 TabContentsWrapper* preview_contents = | |
| 77 TabContentsWrapper::GetCurrentWrapperForContents(preview_tab); | |
| 78 | |
| 79 OwnTabContents(contents, preview_contents, &initiator_contents_); | |
| 80 | |
| 81 return true; | |
| 82 } | |
| 83 | |
| 84 bool BackgroundPrintingManager::ReleaseInitiatorTabContents( | |
| 85 TabContentsWrapper* contents) { | |
| 86 if (initiator_contents_.find(contents) == initiator_contents_.end()) | |
| 87 return false; | |
| 88 | |
| 89 initiator_contents_.erase(contents); | |
| 90 MessageLoop::current()->DeleteSoon(FROM_HERE, contents); | |
| 91 return true; | |
| 63 } | 92 } |
| 64 | 93 |
| 65 void BackgroundPrintingManager::Observe(int type, | 94 void BackgroundPrintingManager::Observe(int type, |
| 66 const NotificationSource& source, | 95 const NotificationSource& source, |
| 67 const NotificationDetails& details) { | 96 const NotificationDetails& details) { |
| 68 switch (type) { | 97 switch (type) { |
| 69 case chrome::NOTIFICATION_PRINT_JOB_RELEASED: { | 98 case chrome::NOTIFICATION_PRINT_JOB_RELEASED: { |
| 70 TabContentsWrapper* tab = Source<TabContentsWrapper>(source).ptr(); | 99 TabContentsWrapper* tab = Source<TabContentsWrapper>(source).ptr(); |
| 71 registrar_.Remove(this, chrome::NOTIFICATION_PRINT_JOB_RELEASED, | 100 registrar_.Remove(this, chrome::NOTIFICATION_PRINT_JOB_RELEASED, |
| 72 Source<TabContentsWrapper>(tab)); | 101 Source<TabContentsWrapper>(tab)); |
| 73 | 102 |
| 74 // This might be happening in the middle of a RenderViewGone() loop. | 103 // This might be happening in the middle of a RenderViewGone() loop. |
| 75 // Deleting |contents| later so the RenderViewGone() loop can finish. | 104 // Deleting |contents| later so the RenderViewGone() loop can finish. |
| 76 MessageLoop::current()->DeleteSoon(FROM_HERE, tab); | 105 MessageLoop::current()->DeleteSoon(FROM_HERE, tab); |
| 106 ReleaseInitiatorForPreviewTabContents(tab); | |
| 77 break; | 107 break; |
| 78 } | 108 } |
| 79 case content::NOTIFICATION_TAB_CONTENTS_DESTROYED: { | 109 case content::NOTIFICATION_TAB_CONTENTS_DESTROYED: { |
| 80 TabContentsWrapper* tab = | 110 TabContentsWrapper* tab = |
| 81 TabContentsWrapper::GetCurrentWrapperForContents( | 111 TabContentsWrapper::GetCurrentWrapperForContents( |
| 82 Source<TabContents>(source).ptr()); | 112 Source<TabContents>(source).ptr()); |
| 83 if (registrar_.IsRegistered(this, chrome::NOTIFICATION_PRINT_JOB_RELEASED, | 113 if (registrar_.IsRegistered(this, chrome::NOTIFICATION_PRINT_JOB_RELEASED, |
| 84 Source<TabContentsWrapper>(tab))) { | 114 Source<TabContentsWrapper>(tab))) { |
| 85 registrar_.Remove(this, chrome::NOTIFICATION_PRINT_JOB_RELEASED, | 115 registrar_.Remove(this, chrome::NOTIFICATION_PRINT_JOB_RELEASED, |
| 86 Source<TabContentsWrapper>(tab)); | 116 Source<TabContentsWrapper>(tab)); |
| 87 } | 117 } |
| 88 registrar_.Remove(this, content::NOTIFICATION_TAB_CONTENTS_DESTROYED, | 118 registrar_.Remove(this, content::NOTIFICATION_TAB_CONTENTS_DESTROYED, |
| 89 Source<TabContents>(tab->tab_contents())); | 119 Source<TabContents>(tab->tab_contents())); |
| 90 printing_contents_.erase(tab); | 120 printing_contents_.erase(tab); |
| 121 ReleaseInitiatorForPreviewTabContents(tab); | |
| 91 break; | 122 break; |
| 92 } | 123 } |
| 93 default: { | 124 default: { |
| 94 NOTREACHED(); | 125 NOTREACHED(); |
| 95 break; | 126 break; |
| 96 } | 127 } |
| 97 } | 128 } |
| 98 } | 129 } |
| 99 | 130 |
| 100 std::set<TabContentsWrapper*>::const_iterator | 131 bool BackgroundPrintingManager::ReleaseInitiatorForPreviewTabContents( |
| 132 TabContentsWrapper* preview_content) { | |
| 133 DCHECK(CalledOnValidThread()); | |
| 134 printing::PrintPreviewTabController* tab_controller = | |
| 135 printing::PrintPreviewTabController::GetInstance(); | |
| 136 | |
| 137 if (!tab_controller) | |
| 138 return false; | |
| 139 | |
| 140 TabContents* initiator_tab = tab_controller->GetInitiatorTab( | |
| 141 preview_content->tab_contents()); | |
| 142 if (!initiator_tab) | |
| 143 return false; | |
| 144 | |
| 145 TabContentsWrapper* initiator_wrapper = | |
| 146 TabContentsWrapper::GetCurrentWrapperForContents(initiator_tab); | |
| 147 | |
| 148 return ReleaseInitiatorTabContents(initiator_wrapper); | |
| 149 } | |
| 150 | |
| 151 void BackgroundPrintingManager::OwnTabContents( | |
| 152 TabContentsWrapper* contents, | |
| 153 TabContentsWrapper* source_contents, | |
| 154 TabContentsWrapperSet* wrapper_set) { | |
| 155 CHECK(source_contents); | |
| 156 DCHECK(printing::PrintPreviewTabController::IsPrintPreviewTab( | |
| 157 source_contents->tab_contents())); | |
| 158 | |
| 159 wrapper_set->insert(contents); | |
| 160 | |
| 161 if (!registrar_.IsRegistered(this, chrome::NOTIFICATION_PRINT_JOB_RELEASED, | |
| 162 Source<TabContentsWrapper>(source_contents))) { | |
| 163 registrar_.Add(this, chrome::NOTIFICATION_PRINT_JOB_RELEASED, | |
| 164 Source<TabContentsWrapper>(source_contents)); | |
| 165 } | |
| 166 if (!registrar_.IsRegistered( | |
| 167 this, content::NOTIFICATION_TAB_CONTENTS_DESTROYED, | |
| 168 Source<TabContents>(source_contents->tab_contents()))) { | |
| 169 registrar_.Add(this, content::NOTIFICATION_TAB_CONTENTS_DESTROYED, | |
| 170 Source<TabContents>(source_contents->tab_contents())); | |
| 171 } | |
| 172 | |
| 173 // Detach |contents| from its tab strip. | |
| 174 Browser* browser = BrowserList::FindBrowserWithID( | |
| 175 contents->restore_tab_helper()->window_id().id()); | |
| 176 DCHECK(browser); | |
| 177 | |
| 178 TabStripModel* tabstrip = browser->tabstrip_model(); | |
| 179 tabstrip->DetachTabContentsAt(tabstrip->GetIndexOfTabContents(contents)); | |
| 180 } | |
| 181 | |
| 182 BackgroundPrintingManager::TabContentsWrapperSet::const_iterator | |
| 101 BackgroundPrintingManager::begin() { | 183 BackgroundPrintingManager::begin() { |
| 102 return printing_contents_.begin(); | 184 return printing_contents_.begin(); |
| 103 } | 185 } |
| 104 | 186 |
| 105 std::set<TabContentsWrapper*>::const_iterator | 187 BackgroundPrintingManager::TabContentsWrapperSet::const_iterator |
| 106 BackgroundPrintingManager::end() { | 188 BackgroundPrintingManager::end() { |
| 107 return printing_contents_.end(); | 189 return printing_contents_.end(); |
| 108 } | 190 } |
| 109 | 191 |
| 110 bool BackgroundPrintingManager::HasTabContents(TabContentsWrapper* entry) { | 192 bool BackgroundPrintingManager::HasPreviewTabContents( |
| 193 TabContentsWrapper* entry) { | |
| 111 return printing_contents_.find(entry) != printing_contents_.end(); | 194 return printing_contents_.find(entry) != printing_contents_.end(); |
| 112 } | 195 } |
| 113 | 196 |
| 114 } // namespace printing | 197 } // namespace printing |
| OLD | NEW |