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