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 | |
71 // Activate the initiator tab. | 69 // Activate the initiator tab. |
72 PrintPreviewTabController* tab_controller = | 70 PrintPreviewTabController* tab_controller = |
73 PrintPreviewTabController::GetInstance(); | 71 PrintPreviewTabController::GetInstance(); |
74 if (!tab_controller) | 72 if (!tab_controller) |
75 return; | 73 return; |
76 TabContentsWrapper* initiator_tab = | 74 TabContentsWrapper* initiator_tab = |
77 tab_controller->GetInitiatorTab(preview_tab); | 75 tab_controller->GetInitiatorTab(preview_tab); |
78 if (!initiator_tab) | 76 if (!initiator_tab) |
79 return; | 77 return; |
80 static_cast<RenderViewHostDelegate*>( | 78 static_cast<RenderViewHostDelegate*>( |
81 initiator_tab->tab_contents())->Activate(); | 79 initiator_tab->tab_contents())->Activate(); |
82 } | 80 } |
83 | 81 |
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 | |
123 void BackgroundPrintingManager::Observe( | 82 void BackgroundPrintingManager::Observe( |
124 int type, | 83 int type, |
125 const content::NotificationSource& source, | 84 const content::NotificationSource& source, |
126 const content::NotificationDetails& details) { | 85 const content::NotificationDetails& details) { |
127 switch (type) { | 86 switch (type) { |
128 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: { | 87 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: { |
129 OnRendererProcessClosed(content::Source<RenderProcessHost>(source).ptr()); | 88 OnRendererProcessClosed(content::Source<RenderProcessHost>(source).ptr()); |
130 break; | 89 break; |
131 } | 90 } |
132 case chrome::NOTIFICATION_PRINT_JOB_RELEASED: { | 91 case chrome::NOTIFICATION_PRINT_JOB_RELEASED: { |
(...skipping 30 matching lines...) Expand all Loading... |
163 } | 122 } |
164 } | 123 } |
165 | 124 |
166 void BackgroundPrintingManager::OnPrintJobReleased( | 125 void BackgroundPrintingManager::OnPrintJobReleased( |
167 TabContentsWrapper* preview_tab) { | 126 TabContentsWrapper* preview_tab) { |
168 DeletePreviewTab(preview_tab); | 127 DeletePreviewTab(preview_tab); |
169 } | 128 } |
170 | 129 |
171 void BackgroundPrintingManager::OnTabContentsDestroyed( | 130 void BackgroundPrintingManager::OnTabContentsDestroyed( |
172 TabContentsWrapper* preview_tab) { | 131 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 | |
178 // Always need to remove this notification since the tab is gone. | 132 // Always need to remove this notification since the tab is gone. |
179 registrar_.Remove(this, content::NOTIFICATION_TAB_CONTENTS_DESTROYED, | 133 registrar_.Remove(this, content::NOTIFICATION_TAB_CONTENTS_DESTROYED, |
180 content::Source<TabContents>(preview_tab->tab_contents())); | 134 content::Source<TabContents>(preview_tab->tab_contents())); |
181 | 135 |
182 // Delete the associated initiator tab if one exists. | 136 if (!HasPrintPreviewTab(preview_tab)) { |
183 if (is_preview_tab_for_owned_initator_tab) { | 137 NOTREACHED(); |
184 TabContentsWrapper* initiator_tab = map_[preview_tab]; | 138 return; |
185 map_.erase(preview_tab); | |
186 MessageLoop::current()->DeleteSoon(FROM_HERE, initiator_tab); | |
187 } | 139 } |
188 | 140 |
189 // If |preview_tab| is not owned, then we are done. | |
190 if (!is_owned_printing_tab) | |
191 return; | |
192 | |
193 // Remove NOTIFICATION_RENDERER_PROCESS_CLOSED if |preview_tab| is the last | 141 // Remove NOTIFICATION_RENDERER_PROCESS_CLOSED if |preview_tab| is the last |
194 // TabContents associated with |rph|. | 142 // TabContents associated with |rph|. |
195 bool shared_rph = HasSharedRenderProcessHost(printing_tabs_, preview_tab) || | 143 bool shared_rph = HasSharedRenderProcessHost(printing_tabs_, preview_tab) || |
196 HasSharedRenderProcessHost(printing_tabs_pending_deletion_, preview_tab); | 144 HasSharedRenderProcessHost(printing_tabs_pending_deletion_, preview_tab); |
197 if (!shared_rph) { | 145 if (!shared_rph) { |
198 RenderProcessHost* rph = preview_tab->render_view_host()->process(); | 146 RenderProcessHost* rph = preview_tab->render_view_host()->process(); |
199 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | 147 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
200 content::Source<RenderProcessHost>(rph)); | 148 content::Source<RenderProcessHost>(rph)); |
201 } | 149 } |
202 | 150 |
203 // Remove other notifications and remove the tab from its | 151 // Remove other notifications and remove the tab from its |
204 // TabContentsWrapperSet. | 152 // TabContentsWrapperSet. |
205 if (printing_tabs_.find(preview_tab) != printing_tabs_.end()) { | 153 if (printing_tabs_.find(preview_tab) != printing_tabs_.end()) { |
206 registrar_.Remove(this, chrome::NOTIFICATION_PRINT_JOB_RELEASED, | 154 registrar_.Remove(this, chrome::NOTIFICATION_PRINT_JOB_RELEASED, |
207 content::Source<TabContentsWrapper>(preview_tab)); | 155 content::Source<TabContentsWrapper>(preview_tab)); |
208 printing_tabs_.erase(preview_tab); | 156 printing_tabs_.erase(preview_tab); |
209 } else { | 157 } else { |
210 // DeletePreviewTab already deleted the notification. | 158 // DeletePreviewTab already deleted the notification. |
211 printing_tabs_pending_deletion_.erase(preview_tab); | 159 printing_tabs_pending_deletion_.erase(preview_tab); |
212 } | 160 } |
213 } | 161 } |
214 | 162 |
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 | |
227 void BackgroundPrintingManager::DeletePreviewTab(TabContentsWrapper* tab) { | 163 void BackgroundPrintingManager::DeletePreviewTab(TabContentsWrapper* tab) { |
228 registrar_.Remove(this, chrome::NOTIFICATION_PRINT_JOB_RELEASED, | 164 registrar_.Remove(this, chrome::NOTIFICATION_PRINT_JOB_RELEASED, |
229 content::Source<TabContentsWrapper>(tab)); | 165 content::Source<TabContentsWrapper>(tab)); |
230 printing_tabs_.erase(tab); | 166 printing_tabs_.erase(tab); |
231 printing_tabs_pending_deletion_.insert(tab); | 167 printing_tabs_pending_deletion_.insert(tab); |
232 MessageLoop::current()->DeleteSoon(FROM_HERE, tab); | 168 MessageLoop::current()->DeleteSoon(FROM_HERE, tab); |
233 } | 169 } |
234 | 170 |
235 bool BackgroundPrintingManager::HasSharedRenderProcessHost( | 171 bool BackgroundPrintingManager::HasSharedRenderProcessHost( |
236 const TabContentsWrapperSet& set, | 172 const TabContentsWrapperSet& set, |
(...skipping 23 matching lines...) Expand all Loading... |
260 | 196 |
261 bool BackgroundPrintingManager::HasPrintPreviewTab( | 197 bool BackgroundPrintingManager::HasPrintPreviewTab( |
262 TabContentsWrapper* preview_tab) { | 198 TabContentsWrapper* preview_tab) { |
263 if (printing_tabs_.find(preview_tab) != printing_tabs_.end()) | 199 if (printing_tabs_.find(preview_tab) != printing_tabs_.end()) |
264 return true; | 200 return true; |
265 return printing_tabs_pending_deletion_.find(preview_tab) != | 201 return printing_tabs_pending_deletion_.find(preview_tab) != |
266 printing_tabs_pending_deletion_.end(); | 202 printing_tabs_pending_deletion_.end(); |
267 } | 203 } |
268 | 204 |
269 } // namespace printing | 205 } // namespace printing |
OLD | NEW |