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