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