| 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 DCHECK(CalledOnValidThread()); | 33 TabContentsWrapper* content) { |
| 34 DCHECK(printing::PrintPreviewTabController::IsPrintPreviewTab( | 34 DCHECK(printing::PrintPreviewTabController::IsPrintPreviewTab( |
| 35 contents->tab_contents())); | 35 content->tab_contents())); |
| 36 CHECK(printing_contents_.find(contents) == printing_contents_.end()); | 36 CHECK(printing_contents_.find(content) == printing_contents_.end()); |
| 37 | 37 |
| 38 printing_contents_.insert(contents); | 38 // Find the initiator tab. |
| 39 | 39 TabContents* initiator_tab = NULL; |
| 40 registrar_.Add(this, chrome::NOTIFICATION_PRINT_JOB_RELEASED, | 40 TabContentsWrapper* initiator_tab_wrapper = NULL; |
| 41 Source<TabContentsWrapper>(contents)); | |
| 42 registrar_.Add(this, content::NOTIFICATION_TAB_CONTENTS_DESTROYED, | |
| 43 Source<TabContents>(contents->tab_contents())); | |
| 44 | |
| 45 // Detach |contents| from its tab strip. | |
| 46 Browser* browser = BrowserList::FindBrowserWithID( | |
| 47 contents->restore_tab_helper()->window_id().id()); | |
| 48 DCHECK(browser); | |
| 49 | |
| 50 TabStripModel* tabstrip = browser->tabstrip_model(); | |
| 51 tabstrip->DetachTabContentsAt(tabstrip->GetIndexOfTabContents(contents)); | |
| 52 | |
| 53 // Activate the initiator tab. | |
| 54 printing::PrintPreviewTabController* tab_controller = | 41 printing::PrintPreviewTabController* tab_controller = |
| 55 printing::PrintPreviewTabController::GetInstance(); | 42 printing::PrintPreviewTabController::GetInstance(); |
| 56 if (!tab_controller) | 43 if (tab_controller) |
| 57 return; | 44 initiator_tab = tab_controller->GetInitiatorTab(content->tab_contents()); |
| 58 TabContents* initiator_tab = tab_controller->GetInitiatorTab( | 45 if (initiator_tab) |
| 59 contents->tab_contents()); | 46 initiator_tab_wrapper = |
| 60 if (!initiator_tab) | 47 TabContentsWrapper::GetCurrentWrapperForContents(initiator_tab); |
| 61 return; | 48 if (initiator_tab_wrapper && |
| 62 static_cast<RenderViewHostDelegate*>(initiator_tab)->Activate(); | 49 !ReleaseInitiatorTabContents(initiator_tab_wrapper)) { |
| 50 OwnTabContents(content, content); |
| 51 |
| 52 // Activate the initiator tab. |
| 53 if (initiator_tab) |
| 54 static_cast<RenderViewHostDelegate*>(initiator_tab)->Activate(); |
| 55 } else { |
| 56 OwnTabContents(content, NULL); |
| 57 } |
| 58 } |
| 59 |
| 60 bool BackgroundPrintingManager::OwnInitiatorTabContents( |
| 61 TabContentsWrapper* content) { |
| 62 DCHECK(!printing::PrintPreviewTabController::IsPrintPreviewTab( |
| 63 content->tab_contents())); |
| 64 |
| 65 printing::PrintPreviewTabController* tab_controller = |
| 66 printing::PrintPreviewTabController::GetInstance(); |
| 67 TabContents* preview_tab = |
| 68 tab_controller->GetPrintPreviewForTab(content->tab_contents()); |
| 69 if (!preview_tab) |
| 70 return false; |
| 71 TabContentsWrapper* preview_contents = |
| 72 TabContentsWrapper::GetCurrentWrapperForContents(preview_tab); |
| 73 |
| 74 OwnTabContents(content, preview_contents); |
| 75 |
| 76 return true; |
| 77 } |
| 78 |
| 79 bool BackgroundPrintingManager::ReleaseInitiatorTabContents( |
| 80 TabContentsWrapper* content) { |
| 81 DLOG(INFO) << __FUNCTION__; |
| 82 |
| 83 if (printing_contents_.find(content) == printing_contents_.end()) |
| 84 return false; |
| 85 |
| 86 printing_contents_.erase(content); |
| 87 DLOG(INFO) << "deleting initiator: " << (void*) content; |
| 88 MessageLoop::current()->DeleteSoon(FROM_HERE, content); |
| 89 return true; |
| 63 } | 90 } |
| 64 | 91 |
| 65 void BackgroundPrintingManager::Observe(int type, | 92 void BackgroundPrintingManager::Observe(int type, |
| 66 const NotificationSource& source, | 93 const NotificationSource& source, |
| 67 const NotificationDetails& details) { | 94 const NotificationDetails& details) { |
| 68 switch (type) { | 95 switch (type) { |
| 69 case chrome::NOTIFICATION_PRINT_JOB_RELEASED: { | 96 case chrome::NOTIFICATION_PRINT_JOB_RELEASED: { |
| 97 DLOG(INFO) << "NOTIFICATION_PRINT_JOB_RELEASED"; |
| 70 TabContentsWrapper* tab = Source<TabContentsWrapper>(source).ptr(); | 98 TabContentsWrapper* tab = Source<TabContentsWrapper>(source).ptr(); |
| 71 registrar_.Remove(this, chrome::NOTIFICATION_PRINT_JOB_RELEASED, | 99 registrar_.Remove(this, chrome::NOTIFICATION_PRINT_JOB_RELEASED, |
| 72 Source<TabContentsWrapper>(tab)); | 100 Source<TabContentsWrapper>(tab)); |
| 73 | 101 |
| 74 // This might be happening in the middle of a RenderViewGone() loop. | 102 // This might be happening in the middle of a RenderViewGone() loop. |
| 75 // Deleting |contents| later so the RenderViewGone() loop can finish. | 103 // Deleting |contents| later so the RenderViewGone() loop can finish. |
| 76 MessageLoop::current()->DeleteSoon(FROM_HERE, tab); | 104 MessageLoop::current()->DeleteSoon(FROM_HERE, tab); |
| 105 |
| 106 printing::PrintPreviewTabController* tab_controller = |
| 107 printing::PrintPreviewTabController::GetInstance(); |
| 108 if (!tab_controller) { |
| 109 DLOG(INFO) << "!tab_controller"; |
| 110 break; |
| 111 } |
| 112 TabContents* initiator_tab = tab_controller->GetInitiatorTab( |
| 113 tab->tab_contents()); |
| 114 if (!initiator_tab) { |
| 115 DLOG(INFO) << "!initiator_tab"; |
| 116 break; |
| 117 } |
| 118 TabContentsWrapper* initiator_wrapper = |
| 119 TabContentsWrapper::GetCurrentWrapperForContents(initiator_tab); |
| 120 ReleaseInitiatorTabContents(initiator_wrapper); |
| 77 break; | 121 break; |
| 78 } | 122 } |
| 79 case content::NOTIFICATION_TAB_CONTENTS_DESTROYED: { | 123 case content::NOTIFICATION_TAB_CONTENTS_DESTROYED: { |
| 124 DLOG(INFO) << "NOTIFICATION_TAB_CONTENTS_DESTROYED"; |
| 80 TabContentsWrapper* tab = | 125 TabContentsWrapper* tab = |
| 81 TabContentsWrapper::GetCurrentWrapperForContents( | 126 TabContentsWrapper::GetCurrentWrapperForContents( |
| 82 Source<TabContents>(source).ptr()); | 127 Source<TabContents>(source).ptr()); |
| 83 if (registrar_.IsRegistered(this, chrome::NOTIFICATION_PRINT_JOB_RELEASED, | 128 if (registrar_.IsRegistered(this, chrome::NOTIFICATION_PRINT_JOB_RELEASED, |
| 84 Source<TabContentsWrapper>(tab))) { | 129 Source<TabContentsWrapper>(tab))) { |
| 85 registrar_.Remove(this, chrome::NOTIFICATION_PRINT_JOB_RELEASED, | 130 registrar_.Remove(this, chrome::NOTIFICATION_PRINT_JOB_RELEASED, |
| 86 Source<TabContentsWrapper>(tab)); | 131 Source<TabContentsWrapper>(tab)); |
| 87 } | 132 } |
| 88 registrar_.Remove(this, content::NOTIFICATION_TAB_CONTENTS_DESTROYED, | 133 registrar_.Remove(this, content::NOTIFICATION_TAB_CONTENTS_DESTROYED, |
| 89 Source<TabContents>(tab->tab_contents())); | 134 Source<TabContents>(tab->tab_contents())); |
| 90 printing_contents_.erase(tab); | 135 printing_contents_.erase(tab); |
| 136 printing::PrintPreviewTabController* tab_controller = |
| 137 printing::PrintPreviewTabController::GetInstance(); |
| 138 if (!tab_controller) { |
| 139 DLOG(INFO) << "!tab_controller"; |
| 140 break; |
| 141 } |
| 142 TabContents* initiator_tab = tab_controller->GetInitiatorTab( |
| 143 tab->tab_contents()); |
| 144 if (!initiator_tab) { |
| 145 DLOG(INFO) << "!initiator_tab"; |
| 146 break; |
| 147 } |
| 148 TabContentsWrapper* initiator_wrapper = |
| 149 TabContentsWrapper::GetCurrentWrapperForContents(initiator_tab); |
| 150 DLOG(INFO) << (void*)initiator_wrapper; |
| 151 ReleaseInitiatorTabContents(initiator_wrapper); |
| 91 break; | 152 break; |
| 92 } | 153 } |
| 93 default: { | 154 default: { |
| 94 NOTREACHED(); | 155 NOTREACHED(); |
| 95 break; | 156 break; |
| 96 } | 157 } |
| 97 } | 158 } |
| 98 } | 159 } |
| 99 | 160 |
| 161 void BackgroundPrintingManager::OwnTabContents( |
| 162 TabContentsWrapper* contents, TabContentsWrapper* source_contents) { |
| 163 DCHECK(CalledOnValidThread()); |
| 164 DCHECK(!source_contents || |
| 165 printing::PrintPreviewTabController::IsPrintPreviewTab( |
| 166 source_contents->tab_contents())); |
| 167 CHECK(printing_contents_.find(contents) == printing_contents_.end()); |
| 168 |
| 169 printing_contents_.insert(contents); |
| 170 |
| 171 if (source_contents) { |
| 172 // TODO(scr) delete non-preview tab if print is deleted or finishes. |
| 173 registrar_.Add(this, chrome::NOTIFICATION_PRINT_JOB_RELEASED, |
| 174 Source<TabContentsWrapper>(source_contents)); |
| 175 registrar_.Add(this, content::NOTIFICATION_TAB_CONTENTS_DESTROYED, |
| 176 Source<TabContents>(source_contents->tab_contents())); |
| 177 } |
| 178 |
| 179 // Detach |contents| from its tab strip. |
| 180 Browser* browser = BrowserList::FindBrowserWithID( |
| 181 contents->restore_tab_helper()->window_id().id()); |
| 182 DCHECK(browser); |
| 183 |
| 184 TabStripModel* tabstrip = browser->tabstrip_model(); |
| 185 tabstrip->DetachTabContentsAt(tabstrip->GetIndexOfTabContents(contents)); |
| 186 } |
| 187 |
| 100 std::set<TabContentsWrapper*>::const_iterator | 188 std::set<TabContentsWrapper*>::const_iterator |
| 101 BackgroundPrintingManager::begin() { | 189 BackgroundPrintingManager::begin() { |
| 102 return printing_contents_.begin(); | 190 return printing_contents_.begin(); |
| 103 } | 191 } |
| 104 | 192 |
| 105 std::set<TabContentsWrapper*>::const_iterator | 193 std::set<TabContentsWrapper*>::const_iterator |
| 106 BackgroundPrintingManager::end() { | 194 BackgroundPrintingManager::end() { |
| 107 return printing_contents_.end(); | 195 return printing_contents_.end(); |
| 108 } | 196 } |
| 109 | 197 |
| 110 bool BackgroundPrintingManager::HasTabContents(TabContentsWrapper* entry) { | 198 bool BackgroundPrintingManager::HasTabContents(TabContentsWrapper* entry) { |
| 111 return printing_contents_.find(entry) != printing_contents_.end(); | 199 return printing_contents_.find(entry) != printing_contents_.end(); |
| 112 } | 200 } |
| 113 | 201 |
| 114 } // namespace printing | 202 } // namespace printing |
| OLD | NEW |