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