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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 // Multiple sites may share the same RenderProcessHost, so check if this | 59 // Multiple sites may share the same RenderProcessHost, so check if this |
60 // notification has already been added. | 60 // notification has already been added. |
61 RenderProcessHost* rph = preview_tab->render_view_host()->process(); | 61 RenderProcessHost* rph = preview_tab->render_view_host()->process(); |
62 if (!registrar_.IsRegistered(this, | 62 if (!registrar_.IsRegistered(this, |
63 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | 63 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
64 content::Source<RenderProcessHost>(rph))) { | 64 content::Source<RenderProcessHost>(rph))) { |
65 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | 65 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
66 content::Source<RenderProcessHost>(rph)); | 66 content::Source<RenderProcessHost>(rph)); |
67 } | 67 } |
68 | 68 |
| 69 RemoveFromTabStrip(preview_tab); |
| 70 |
69 // Activate the initiator tab. | 71 // Activate the initiator tab. |
70 PrintPreviewTabController* tab_controller = | 72 PrintPreviewTabController* tab_controller = |
71 PrintPreviewTabController::GetInstance(); | 73 PrintPreviewTabController::GetInstance(); |
72 if (!tab_controller) | 74 if (!tab_controller) |
73 return; | 75 return; |
74 TabContentsWrapper* initiator_tab = | 76 TabContentsWrapper* initiator_tab = |
75 tab_controller->GetInitiatorTab(preview_tab); | 77 tab_controller->GetInitiatorTab(preview_tab); |
76 if (!initiator_tab) | 78 if (!initiator_tab) |
77 return; | 79 return; |
78 static_cast<RenderViewHostDelegate*>( | 80 static_cast<RenderViewHostDelegate*>( |
79 initiator_tab->tab_contents())->Activate(); | 81 initiator_tab->tab_contents())->Activate(); |
80 } | 82 } |
81 | 83 |
| 84 bool BackgroundPrintingManager::OwnInitiatorTab( |
| 85 TabContentsWrapper* initiator_tab) { |
| 86 DCHECK(CalledOnValidThread()); |
| 87 DCHECK(!PrintPreviewTabController::IsPrintPreviewTab(initiator_tab)); |
| 88 bool has_initiator_tab = false; |
| 89 for (TabContentsWrapperMap::iterator it = map_.begin(); it != map_.end(); |
| 90 ++it) { |
| 91 if (it->second == initiator_tab) { |
| 92 has_initiator_tab = true; |
| 93 break; |
| 94 } |
| 95 } |
| 96 CHECK(!has_initiator_tab); |
| 97 |
| 98 PrintPreviewTabController* tab_controller = |
| 99 PrintPreviewTabController::GetInstance(); |
| 100 if (!tab_controller) |
| 101 return false; |
| 102 TabContentsWrapper* preview_tab = |
| 103 tab_controller->GetPrintPreviewForTab(initiator_tab); |
| 104 if (!preview_tab) |
| 105 return false; |
| 106 |
| 107 map_[preview_tab] = initiator_tab; |
| 108 |
| 109 // OwnPrintPreviewTab() may have already added this notification. |
| 110 TabContents* preview_contents = preview_tab->tab_contents(); |
| 111 if (!registrar_.IsRegistered( |
| 112 this, |
| 113 content::NOTIFICATION_TAB_CONTENTS_DESTROYED, |
| 114 content::Source<TabContents>(preview_contents))) { |
| 115 registrar_.Add(this, content::NOTIFICATION_TAB_CONTENTS_DESTROYED, |
| 116 content::Source<TabContents>(preview_contents)); |
| 117 } |
| 118 |
| 119 RemoveFromTabStrip(initiator_tab); |
| 120 return true; |
| 121 } |
| 122 |
82 void BackgroundPrintingManager::Observe( | 123 void BackgroundPrintingManager::Observe( |
83 int type, | 124 int type, |
84 const content::NotificationSource& source, | 125 const content::NotificationSource& source, |
85 const content::NotificationDetails& details) { | 126 const content::NotificationDetails& details) { |
86 switch (type) { | 127 switch (type) { |
87 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: { | 128 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: { |
88 OnRendererProcessClosed(content::Source<RenderProcessHost>(source).ptr()); | 129 OnRendererProcessClosed(content::Source<RenderProcessHost>(source).ptr()); |
89 break; | 130 break; |
90 } | 131 } |
91 case chrome::NOTIFICATION_PRINT_JOB_RELEASED: { | 132 case chrome::NOTIFICATION_PRINT_JOB_RELEASED: { |
(...skipping 30 matching lines...) Expand all Loading... |
122 } | 163 } |
123 } | 164 } |
124 | 165 |
125 void BackgroundPrintingManager::OnPrintJobReleased( | 166 void BackgroundPrintingManager::OnPrintJobReleased( |
126 TabContentsWrapper* preview_tab) { | 167 TabContentsWrapper* preview_tab) { |
127 DeletePreviewTab(preview_tab); | 168 DeletePreviewTab(preview_tab); |
128 } | 169 } |
129 | 170 |
130 void BackgroundPrintingManager::OnTabContentsDestroyed( | 171 void BackgroundPrintingManager::OnTabContentsDestroyed( |
131 TabContentsWrapper* preview_tab) { | 172 TabContentsWrapper* preview_tab) { |
| 173 bool is_owned_printing_tab = HasPrintPreviewTab(preview_tab); |
| 174 bool is_preview_tab_for_owned_initator_tab = |
| 175 (map_.find(preview_tab) != map_.end()); |
| 176 DCHECK(is_owned_printing_tab || is_preview_tab_for_owned_initator_tab); |
| 177 |
132 // Always need to remove this notification since the tab is gone. | 178 // Always need to remove this notification since the tab is gone. |
133 registrar_.Remove(this, content::NOTIFICATION_TAB_CONTENTS_DESTROYED, | 179 registrar_.Remove(this, content::NOTIFICATION_TAB_CONTENTS_DESTROYED, |
134 content::Source<TabContents>(preview_tab->tab_contents())); | 180 content::Source<TabContents>(preview_tab->tab_contents())); |
135 | 181 |
136 if (!HasPrintPreviewTab(preview_tab)) { | 182 // Delete the associated initiator tab if one exists. |
137 NOTREACHED(); | 183 if (is_preview_tab_for_owned_initator_tab) { |
| 184 TabContentsWrapper* initiator_tab = map_[preview_tab]; |
| 185 map_.erase(preview_tab); |
| 186 MessageLoop::current()->DeleteSoon(FROM_HERE, initiator_tab); |
| 187 } |
| 188 |
| 189 // If |preview_tab| is not owned, then we are done. |
| 190 if (!is_owned_printing_tab) |
138 return; | 191 return; |
139 } | |
140 | 192 |
141 // Remove NOTIFICATION_RENDERER_PROCESS_CLOSED if |preview_tab| is the last | 193 // Remove NOTIFICATION_RENDERER_PROCESS_CLOSED if |preview_tab| is the last |
142 // TabContents associated with |rph|. | 194 // TabContents associated with |rph|. |
143 bool shared_rph = HasSharedRenderProcessHost(printing_tabs_, preview_tab) || | 195 bool shared_rph = HasSharedRenderProcessHost(printing_tabs_, preview_tab) || |
144 HasSharedRenderProcessHost(printing_tabs_pending_deletion_, preview_tab); | 196 HasSharedRenderProcessHost(printing_tabs_pending_deletion_, preview_tab); |
145 if (!shared_rph) { | 197 if (!shared_rph) { |
146 RenderProcessHost* rph = preview_tab->render_view_host()->process(); | 198 RenderProcessHost* rph = preview_tab->render_view_host()->process(); |
147 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | 199 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
148 content::Source<RenderProcessHost>(rph)); | 200 content::Source<RenderProcessHost>(rph)); |
149 } | 201 } |
150 | 202 |
151 // Remove other notifications and remove the tab from its | 203 // Remove other notifications and remove the tab from its |
152 // TabContentsWrapperSet. | 204 // TabContentsWrapperSet. |
153 if (printing_tabs_.find(preview_tab) != printing_tabs_.end()) { | 205 if (printing_tabs_.find(preview_tab) != printing_tabs_.end()) { |
154 registrar_.Remove(this, chrome::NOTIFICATION_PRINT_JOB_RELEASED, | 206 registrar_.Remove(this, chrome::NOTIFICATION_PRINT_JOB_RELEASED, |
155 content::Source<TabContentsWrapper>(preview_tab)); | 207 content::Source<TabContentsWrapper>(preview_tab)); |
156 printing_tabs_.erase(preview_tab); | 208 printing_tabs_.erase(preview_tab); |
157 } else { | 209 } else { |
158 // DeletePreviewTab already deleted the notification. | 210 // DeletePreviewTab already deleted the notification. |
159 printing_tabs_pending_deletion_.erase(preview_tab); | 211 printing_tabs_pending_deletion_.erase(preview_tab); |
160 } | 212 } |
161 } | 213 } |
162 | 214 |
| 215 void BackgroundPrintingManager::RemoveFromTabStrip(TabContentsWrapper* tab) { |
| 216 Browser* browser = BrowserList::FindBrowserWithID( |
| 217 tab->restore_tab_helper()->window_id().id()); |
| 218 DCHECK(browser); |
| 219 |
| 220 TabStripModel* tabstrip = browser->tabstrip_model(); |
| 221 int index = tabstrip->GetIndexOfTabContents(tab); |
| 222 if (index == TabStripModel::kNoTab) |
| 223 return; |
| 224 tabstrip->DetachTabContentsAt(index); |
| 225 } |
| 226 |
163 void BackgroundPrintingManager::DeletePreviewTab(TabContentsWrapper* tab) { | 227 void BackgroundPrintingManager::DeletePreviewTab(TabContentsWrapper* tab) { |
164 registrar_.Remove(this, chrome::NOTIFICATION_PRINT_JOB_RELEASED, | 228 registrar_.Remove(this, chrome::NOTIFICATION_PRINT_JOB_RELEASED, |
165 content::Source<TabContentsWrapper>(tab)); | 229 content::Source<TabContentsWrapper>(tab)); |
166 printing_tabs_.erase(tab); | 230 printing_tabs_.erase(tab); |
167 printing_tabs_pending_deletion_.insert(tab); | 231 printing_tabs_pending_deletion_.insert(tab); |
168 MessageLoop::current()->DeleteSoon(FROM_HERE, tab); | 232 MessageLoop::current()->DeleteSoon(FROM_HERE, tab); |
169 } | 233 } |
170 | 234 |
171 bool BackgroundPrintingManager::HasSharedRenderProcessHost( | 235 bool BackgroundPrintingManager::HasSharedRenderProcessHost( |
172 const TabContentsWrapperSet& set, | 236 const TabContentsWrapperSet& set, |
(...skipping 23 matching lines...) Expand all Loading... |
196 | 260 |
197 bool BackgroundPrintingManager::HasPrintPreviewTab( | 261 bool BackgroundPrintingManager::HasPrintPreviewTab( |
198 TabContentsWrapper* preview_tab) { | 262 TabContentsWrapper* preview_tab) { |
199 if (printing_tabs_.find(preview_tab) != printing_tabs_.end()) | 263 if (printing_tabs_.find(preview_tab) != printing_tabs_.end()) |
200 return true; | 264 return true; |
201 return printing_tabs_pending_deletion_.find(preview_tab) != | 265 return printing_tabs_pending_deletion_.find(preview_tab) != |
202 printing_tabs_pending_deletion_.end(); | 266 printing_tabs_pending_deletion_.end(); |
203 } | 267 } |
204 | 268 |
205 } // namespace printing | 269 } // namespace printing |
OLD | NEW |