1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/printing/background_printing_manager.h"
#include "chrome/browser/printing/print_job.h"
#include "chrome/browser/printing/print_preview_tab_controller.h"
#include "chrome/browser/sessions/restore_tab_helper.h"
#include "chrome/browser/tabs/tab_strip_model.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
#include "chrome/common/chrome_notification_types.h"
#include "content/browser/browser_thread.h"
#include "content/browser/renderer_host/render_view_host.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_source.h"
namespace printing {
BackgroundPrintingManager::BackgroundPrintingManager() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
}
BackgroundPrintingManager::~BackgroundPrintingManager() {
DCHECK(CalledOnValidThread());
// The might be some TabContentsWrappers still in |printing_tabs_| at
// this point. E.g. when the last remaining tab is a print preview tab and
// tries to print. In which case it will fail to print.
// TODO(thestig) handle this case better.
}
void BackgroundPrintingManager::OwnPrintPreviewTab(
TabContentsWrapper* preview_tab) {
DCHECK(CalledOnValidThread());
DCHECK(PrintPreviewTabController::IsPrintPreviewTab(preview_tab));
CHECK(!HasPrintPreviewTab(preview_tab));
printing_tabs_.insert(preview_tab);
registrar_.Add(this, chrome::NOTIFICATION_PRINT_JOB_RELEASED,
content::Source<TabContentsWrapper>(preview_tab));
// OwnInitiatorTabContents() may have already added this notification.
TabContents* preview_contents = preview_tab->tab_contents();
if (!registrar_.IsRegistered(
this,
content::NOTIFICATION_TAB_CONTENTS_DESTROYED,
content::Source<TabContents>(preview_contents))) {
registrar_.Add(this, content::NOTIFICATION_TAB_CONTENTS_DESTROYED,
content::Source<TabContents>(preview_contents));
}
// If a tab that is printing crashes, the user cannot destroy it since it is
// not in any tab strip. Thus listen for crashes here and delete the tab.
//
// Multiple sites may share the same RenderProcessHost, so check if this
// notification has already been added.
RenderProcessHost* rph = preview_tab->render_view_host()->process();
if (!registrar_.IsRegistered(this,
content::NOTIFICATION_RENDERER_PROCESS_CLOSED,
content::Source<RenderProcessHost>(rph))) {
registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED,
content::Source<RenderProcessHost>(rph));
}
RemoveFromTabStrip(preview_tab);
// Activate the initiator tab.
PrintPreviewTabController* tab_controller =
PrintPreviewTabController::GetInstance();
if (!tab_controller)
return;
TabContentsWrapper* initiator_tab =
tab_controller->GetInitiatorTab(preview_tab);
if (!initiator_tab)
return;
static_cast<RenderViewHostDelegate*>(
initiator_tab->tab_contents())->Activate();
}
bool BackgroundPrintingManager::OwnInitiatorTab(
TabContentsWrapper* initiator_tab) {
DCHECK(CalledOnValidThread());
DCHECK(!PrintPreviewTabController::IsPrintPreviewTab(initiator_tab));
bool has_initiator_tab = false;
for (TabContentsWrapperMap::iterator it = map_.begin(); it != map_.end();
++it) {
if (it->second == initiator_tab) {
has_initiator_tab = true;
break;
}
}
CHECK(!has_initiator_tab);
PrintPreviewTabController* tab_controller =
PrintPreviewTabController::GetInstance();
if (!tab_controller)
return false;
TabContentsWrapper* preview_tab =
tab_controller->GetPrintPreviewForTab(initiator_tab);
if (!preview_tab)
return false;
map_[preview_tab] = initiator_tab;
// OwnPrintPreviewTab() may have already added this notification.
TabContents* preview_contents = preview_tab->tab_contents();
if (!registrar_.IsRegistered(
this,
content::NOTIFICATION_TAB_CONTENTS_DESTROYED,
content::Source<TabContents>(preview_contents))) {
registrar_.Add(this, content::NOTIFICATION_TAB_CONTENTS_DESTROYED,
content::Source<TabContents>(preview_contents));
}
RemoveFromTabStrip(initiator_tab);
return true;
}
void BackgroundPrintingManager::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
switch (type) {
case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: {
OnRendererProcessClosed(content::Source<RenderProcessHost>(source).ptr());
break;
}
case chrome::NOTIFICATION_PRINT_JOB_RELEASED: {
OnPrintJobReleased(content::Source<TabContentsWrapper>(source).ptr());
break;
}
case content::NOTIFICATION_TAB_CONTENTS_DESTROYED: {
OnTabContentsDestroyed(
TabContentsWrapper::GetCurrentWrapperForContents(
content::Source<TabContents>(source).ptr()));
break;
}
default: {
NOTREACHED();
break;
}
}
}
void BackgroundPrintingManager::OnRendererProcessClosed(
RenderProcessHost* rph) {
TabContentsWrapperSet preview_tabs_pending_deletion;
TabContentsWrapperSet::const_iterator it;
for (it = begin(); it != end(); ++it) {
TabContentsWrapper* preview_tab = *it;
if (preview_tab->render_view_host()->process() == rph) {
preview_tabs_pending_deletion.insert(preview_tab);
}
}
for (it = preview_tabs_pending_deletion.begin();
it != preview_tabs_pending_deletion.end();
++it) {
DeletePreviewTab(*it);
}
}
void BackgroundPrintingManager::OnPrintJobReleased(
TabContentsWrapper* preview_tab) {
DeletePreviewTab(preview_tab);
}
void BackgroundPrintingManager::OnTabContentsDestroyed(
TabContentsWrapper* preview_tab) {
bool is_owned_printing_tab = HasPrintPreviewTab(preview_tab);
bool is_preview_tab_for_owned_initator_tab =
(map_.find(preview_tab) != map_.end());
DCHECK(is_owned_printing_tab || is_preview_tab_for_owned_initator_tab);
// Always need to remove this notification since the tab is gone.
registrar_.Remove(this, content::NOTIFICATION_TAB_CONTENTS_DESTROYED,
content::Source<TabContents>(preview_tab->tab_contents()));
// Delete the associated initiator tab if one exists.
if (is_preview_tab_for_owned_initator_tab) {
TabContentsWrapper* initiator_tab = map_[preview_tab];
map_.erase(preview_tab);
MessageLoop::current()->DeleteSoon(FROM_HERE, initiator_tab);
}
// If |preview_tab| is not owned, then we are done.
if (!is_owned_printing_tab)
return;
// Remove NOTIFICATION_RENDERER_PROCESS_CLOSED if |preview_tab| is the last
// TabContents associated with |rph|.
bool shared_rph = HasSharedRenderProcessHost(printing_tabs_, preview_tab) ||
HasSharedRenderProcessHost(printing_tabs_pending_deletion_, preview_tab);
if (!shared_rph) {
RenderProcessHost* rph = preview_tab->render_view_host()->process();
registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED,
content::Source<RenderProcessHost>(rph));
}
// Remove other notifications and remove the tab from its
// TabContentsWrapperSet.
if (printing_tabs_.find(preview_tab) != printing_tabs_.end()) {
registrar_.Remove(this, chrome::NOTIFICATION_PRINT_JOB_RELEASED,
content::Source<TabContentsWrapper>(preview_tab));
printing_tabs_.erase(preview_tab);
} else {
// DeletePreviewTab already deleted the notification.
printing_tabs_pending_deletion_.erase(preview_tab);
}
}
void BackgroundPrintingManager::RemoveFromTabStrip(TabContentsWrapper* tab) {
Browser* browser = BrowserList::FindBrowserWithID(
tab->restore_tab_helper()->window_id().id());
DCHECK(browser);
TabStripModel* tabstrip = browser->tabstrip_model();
int index = tabstrip->GetIndexOfTabContents(tab);
if (index == TabStripModel::kNoTab)
return;
tabstrip->DetachTabContentsAt(index);
}
void BackgroundPrintingManager::DeletePreviewTab(TabContentsWrapper* tab) {
registrar_.Remove(this, chrome::NOTIFICATION_PRINT_JOB_RELEASED,
content::Source<TabContentsWrapper>(tab));
printing_tabs_.erase(tab);
printing_tabs_pending_deletion_.insert(tab);
MessageLoop::current()->DeleteSoon(FROM_HERE, tab);
}
bool BackgroundPrintingManager::HasSharedRenderProcessHost(
const TabContentsWrapperSet& set,
TabContentsWrapper* tab) {
RenderProcessHost* rph = tab->render_view_host()->process();
for (TabContentsWrapperSet::const_iterator it = set.begin();
it != set.end();
++it) {
TabContentsWrapper* iter_tab = *it;
if ((iter_tab != tab) &&
(iter_tab->render_view_host()->process() == rph)) {
return true;
}
}
return false;
}
BackgroundPrintingManager::TabContentsWrapperSet::const_iterator
BackgroundPrintingManager::begin() {
return printing_tabs_.begin();
}
BackgroundPrintingManager::TabContentsWrapperSet::const_iterator
BackgroundPrintingManager::end() {
return printing_tabs_.end();
}
bool BackgroundPrintingManager::HasPrintPreviewTab(
TabContentsWrapper* preview_tab) {
if (printing_tabs_.find(preview_tab) != printing_tabs_.end())
return true;
return printing_tabs_pending_deletion_.find(preview_tab) !=
printing_tabs_pending_deletion_.end();
}
} // namespace printing
|