Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(489)

Side by Side Diff: chrome/browser/printing/print_view_manager_base.cc

Issue 22577010: Printing: Add a basic printing mode without print preview and cloud print. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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/print_view_manager.h" 5 #include "chrome/browser/printing/print_view_manager_base.h"
6 6
7 #include <map> 7 #include <map>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/lazy_instance.h"
12 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
13 #include "base/metrics/histogram.h" 12 #include "base/metrics/histogram.h"
14 #include "base/prefs/pref_service.h" 13 #include "base/prefs/pref_service.h"
15 #include "base/strings/utf_string_conversions.h" 14 #include "base/strings/utf_string_conversions.h"
16 #include "base/timer/timer.h" 15 #include "base/timer/timer.h"
17 #include "chrome/browser/browser_process.h" 16 #include "chrome/browser/browser_process.h"
18 #include "chrome/browser/chrome_notification_types.h" 17 #include "chrome/browser/chrome_notification_types.h"
19 #include "chrome/browser/printing/print_error_dialog.h"
20 #include "chrome/browser/printing/print_job.h" 18 #include "chrome/browser/printing/print_job.h"
21 #include "chrome/browser/printing/print_job_manager.h" 19 #include "chrome/browser/printing/print_job_manager.h"
22 #include "chrome/browser/printing/print_preview_dialog_controller.h"
23 #include "chrome/browser/printing/print_view_manager_observer.h"
24 #include "chrome/browser/printing/printer_query.h" 20 #include "chrome/browser/printing/printer_query.h"
25 #include "chrome/browser/profiles/profile.h" 21 #include "chrome/browser/profiles/profile.h"
26 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h"
27 #include "chrome/common/chrome_switches.h" 22 #include "chrome/common/chrome_switches.h"
28 #include "chrome/common/pref_names.h" 23 #include "chrome/common/pref_names.h"
29 #include "chrome/common/print_messages.h" 24 #include "chrome/common/print_messages.h"
30 #include "content/public/browser/browser_thread.h" 25 #include "content/public/browser/browser_thread.h"
31 #include "content/public/browser/notification_details.h" 26 #include "content/public/browser/notification_details.h"
32 #include "content/public/browser/notification_service.h" 27 #include "content/public/browser/notification_service.h"
33 #include "content/public/browser/notification_source.h" 28 #include "content/public/browser/notification_source.h"
34 #include "content/public/browser/render_view_host.h" 29 #include "content/public/browser/render_view_host.h"
35 #include "content/public/browser/web_contents.h" 30 #include "content/public/browser/web_contents.h"
36 #include "content/public/browser/web_contents_view.h" 31 #include "content/public/browser/web_contents_view.h"
37 #include "grit/generated_resources.h" 32 #include "grit/generated_resources.h"
38 #include "printing/metafile.h"
39 #include "printing/metafile_impl.h" 33 #include "printing/metafile_impl.h"
40 #include "printing/print_destination_interface.h"
41 #include "printing/printed_document.h" 34 #include "printing/printed_document.h"
42 #include "ui/base/l10n/l10n_util.h" 35 #include "ui/base/l10n/l10n_util.h"
43 36
37 #if defined(ENABLE_FULL_PRINTING)
38 #include "chrome/browser/printing/print_error_dialog.h"
39 #endif
40
44 using base::TimeDelta; 41 using base::TimeDelta;
45 using content::BrowserThread; 42 using content::BrowserThread;
46 using content::WebContents;
47
48 DEFINE_WEB_CONTENTS_USER_DATA_KEY(printing::PrintViewManager);
49 43
50 namespace { 44 namespace {
51 45
52 // Keeps track of pending scripted print preview closures.
53 // No locking, only access on the UI thread.
54 typedef std::map<content::RenderProcessHost*, base::Closure>
55 ScriptedPrintPreviewClosureMap;
56 static base::LazyInstance<ScriptedPrintPreviewClosureMap>
57 g_scripted_print_preview_closure_map = LAZY_INSTANCE_INITIALIZER;
58
59 // Limits memory usage by raster to 64 MiB. 46 // Limits memory usage by raster to 64 MiB.
60 const int kMaxRasterSizeInPixels = 16*1024*1024; 47 const int kMaxRasterSizeInPixels = 16*1024*1024;
61 48
62 } // namespace 49 } // namespace
63 50
64 namespace printing { 51 namespace printing {
65 52
66 PrintViewManager::PrintViewManager(content::WebContents* web_contents) 53 PrintViewManagerBase::PrintViewManagerBase(content::WebContents* web_contents)
67 : content::WebContentsObserver(web_contents), 54 : content::WebContentsObserver(web_contents),
68 number_pages_(0), 55 number_pages_(0),
69 printing_succeeded_(false), 56 printing_succeeded_(false),
70 inside_inner_message_loop_(false), 57 inside_inner_message_loop_(false),
71 observer_(NULL),
72 cookie_(0), 58 cookie_(0),
73 print_preview_state_(NOT_PREVIEWING),
74 scripted_print_preview_rph_(NULL),
75 tab_content_blocked_(false) { 59 tab_content_blocked_(false) {
76 #if defined(OS_POSIX) && !defined(OS_MACOSX) 60 #if defined(OS_POSIX) && !defined(OS_MACOSX)
77 expecting_first_page_ = true; 61 expecting_first_page_ = true;
78 #endif 62 #endif
79 registrar_.Add(this, chrome::NOTIFICATION_CONTENT_BLOCKED_STATE_CHANGED, 63 registrar_.Add(this, chrome::NOTIFICATION_CONTENT_BLOCKED_STATE_CHANGED,
80 content::Source<content::WebContents>(web_contents)); 64 content::Source<content::WebContents>(web_contents));
81 Profile* profile = 65 Profile* profile =
82 Profile::FromBrowserContext(web_contents->GetBrowserContext()); 66 Profile::FromBrowserContext(web_contents->GetBrowserContext());
83 printing_enabled_.Init( 67 printing_enabled_.Init(
84 prefs::kPrintingEnabled, 68 prefs::kPrintingEnabled,
85 profile->GetPrefs(), 69 profile->GetPrefs(),
86 base::Bind(&PrintViewManager::UpdateScriptedPrintingBlocked, 70 base::Bind(&PrintViewManagerBase::UpdateScriptedPrintingBlocked,
87 base::Unretained(this))); 71 base::Unretained(this)));
88 } 72 }
89 73
90 PrintViewManager::~PrintViewManager() { 74 PrintViewManagerBase::~PrintViewManagerBase() {
91 DCHECK_EQ(NOT_PREVIEWING, print_preview_state_);
92 ReleasePrinterQuery(); 75 ReleasePrinterQuery();
93 DisconnectFromCurrentPrintJob(); 76 DisconnectFromCurrentPrintJob();
94 } 77 }
95 78
96 bool PrintViewManager::PrintNow() { 79 bool PrintViewManagerBase::PrintNow() {
97 return PrintNowInternal(new PrintMsg_PrintPages(routing_id())); 80 return PrintNowInternal(new PrintMsg_PrintPages(routing_id()));
98 } 81 }
99 82
100 bool PrintViewManager::PrintForSystemDialogNow() { 83 void PrintViewManagerBase::UpdateScriptedPrintingBlocked() {
101 return PrintNowInternal(new PrintMsg_PrintForSystemDialog(routing_id()));
102 }
103
104 bool PrintViewManager::AdvancedPrintNow() {
105 PrintPreviewDialogController* dialog_controller =
106 PrintPreviewDialogController::GetInstance();
107 if (!dialog_controller)
108 return false;
109 WebContents* print_preview_dialog =
110 dialog_controller->GetPrintPreviewForContents(web_contents());
111 if (print_preview_dialog) {
112 if (!print_preview_dialog->GetWebUI())
113 return false;
114 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(
115 print_preview_dialog->GetWebUI()->GetController());
116 print_preview_ui->OnShowSystemDialog();
117 return true;
118 } else {
119 return PrintNow();
120 }
121 }
122
123 bool PrintViewManager::PrintToDestination() {
124 // TODO(mad): Remove this once we can send user metrics from the metro driver.
125 // crbug.com/142330
126 UMA_HISTOGRAM_ENUMERATION("Metro.Print", 0, 2);
127 // TODO(mad): Use a passed in destination interface instead.
128 g_browser_process->print_job_manager()->SetPrintDestination(
129 printing::CreatePrintDestination());
130 return PrintNowInternal(new PrintMsg_PrintPages(routing_id()));
131 }
132
133 bool PrintViewManager::PrintPreviewNow(bool selection_only) {
134 // Users can send print commands all they want and it is beyond
135 // PrintViewManager's control. Just ignore the extra commands.
136 // See http://crbug.com/136842 for example.
137 if (print_preview_state_ != NOT_PREVIEWING)
138 return false;
139
140 if (!PrintNowInternal(new PrintMsg_InitiatePrintPreview(routing_id(),
141 selection_only))) {
142 return false;
143 }
144
145 print_preview_state_ = USER_INITIATED_PREVIEW;
146 return true;
147 }
148
149 void PrintViewManager::PrintPreviewForWebNode() {
150 DCHECK_EQ(NOT_PREVIEWING, print_preview_state_);
151 print_preview_state_ = USER_INITIATED_PREVIEW;
152 }
153
154 void PrintViewManager::PrintPreviewDone() {
155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
156 DCHECK_NE(NOT_PREVIEWING, print_preview_state_);
157
158 if (print_preview_state_ == SCRIPTED_PREVIEW) {
159 ScriptedPrintPreviewClosureMap& map =
160 g_scripted_print_preview_closure_map.Get();
161 ScriptedPrintPreviewClosureMap::iterator it =
162 map.find(scripted_print_preview_rph_);
163 CHECK(it != map.end());
164 it->second.Run();
165 map.erase(scripted_print_preview_rph_);
166 scripted_print_preview_rph_ = NULL;
167 }
168 print_preview_state_ = NOT_PREVIEWING;
169 }
170
171 void PrintViewManager::UpdateScriptedPrintingBlocked() {
172 Send(new PrintMsg_SetScriptedPrintingBlocked( 84 Send(new PrintMsg_SetScriptedPrintingBlocked(
173 routing_id(), 85 routing_id(),
174 !printing_enabled_.GetValue() || tab_content_blocked_)); 86 !printing_enabled_.GetValue() || tab_content_blocked_));
175 } 87 }
176 88
177 void PrintViewManager::set_observer(PrintViewManagerObserver* observer) { 89 void PrintViewManagerBase::StopNavigation() {
178 DCHECK(!observer || !observer_);
179 observer_ = observer;
180 }
181
182 void PrintViewManager::StopNavigation() {
183 // Cancel the current job, wait for the worker to finish. 90 // Cancel the current job, wait for the worker to finish.
184 TerminatePrintJob(true); 91 TerminatePrintJob(true);
185 } 92 }
186 93
187 void PrintViewManager::RenderProcessGone(base::TerminationStatus status) { 94 void PrintViewManagerBase::RenderProcessGone(base::TerminationStatus status) {
188 print_preview_state_ = NOT_PREVIEWING;
189 ReleasePrinterQuery(); 95 ReleasePrinterQuery();
190 96
191 if (!print_job_.get()) 97 if (!print_job_.get())
192 return; 98 return;
193 99
194 scoped_refptr<PrintedDocument> document(print_job_->document()); 100 scoped_refptr<PrintedDocument> document(print_job_->document());
195 if (document.get()) { 101 if (document.get()) {
196 // If IsComplete() returns false, the document isn't completely rendered. 102 // If IsComplete() returns false, the document isn't completely rendered.
197 // Since our renderer is gone, there's nothing to do, cancel it. Otherwise, 103 // Since our renderer is gone, there's nothing to do, cancel it. Otherwise,
198 // the print job may finish without problem. 104 // the print job may finish without problem.
199 TerminatePrintJob(!document->IsComplete()); 105 TerminatePrintJob(!document->IsComplete());
200 } 106 }
201 } 107 }
202 108
203 string16 PrintViewManager::RenderSourceName() { 109 string16 PrintViewManagerBase::RenderSourceName() {
204 string16 name(web_contents()->GetTitle()); 110 string16 name(web_contents()->GetTitle());
205 if (name.empty()) 111 if (name.empty())
206 name = l10n_util::GetStringUTF16(IDS_DEFAULT_PRINT_DOCUMENT_TITLE); 112 name = l10n_util::GetStringUTF16(IDS_DEFAULT_PRINT_DOCUMENT_TITLE);
207 return name; 113 return name;
208 } 114 }
209 115
210 void PrintViewManager::OnDidGetPrintedPagesCount(int cookie, int number_pages) { 116 void PrintViewManagerBase::OnDidGetPrintedPagesCount(int cookie,
117 int number_pages) {
211 DCHECK_GT(cookie, 0); 118 DCHECK_GT(cookie, 0);
212 DCHECK_GT(number_pages, 0); 119 DCHECK_GT(number_pages, 0);
213 number_pages_ = number_pages; 120 number_pages_ = number_pages;
214 OpportunisticallyCreatePrintJob(cookie); 121 OpportunisticallyCreatePrintJob(cookie);
215 } 122 }
216 123
217 void PrintViewManager::OnDidGetDocumentCookie(int cookie) { 124 void PrintViewManagerBase::OnDidGetDocumentCookie(int cookie) {
218 cookie_ = cookie; 125 cookie_ = cookie;
219 } 126 }
220 127
221 void PrintViewManager::OnDidShowPrintDialog() { 128 void PrintViewManagerBase::OnDidPrintPage(
222 if (observer_)
223 observer_->OnPrintDialogShown();
224 }
225
226 void PrintViewManager::OnDidPrintPage(
227 const PrintHostMsg_DidPrintPage_Params& params) { 129 const PrintHostMsg_DidPrintPage_Params& params) {
228 if (!OpportunisticallyCreatePrintJob(params.document_cookie)) 130 if (!OpportunisticallyCreatePrintJob(params.document_cookie))
229 return; 131 return;
230 132
231 PrintedDocument* document = print_job_->document(); 133 PrintedDocument* document = print_job_->document();
232 if (!document || params.document_cookie != document->cookie()) { 134 if (!document || params.document_cookie != document->cookie()) {
233 // Out of sync. It may happen since we are completely asynchronous. Old 135 // Out of sync. It may happen since we are completely asynchronous. Old
234 // spurious messages can be received if one of the processes is overloaded. 136 // spurious messages can be received if one of the processes is overloaded.
235 return; 137 return;
236 } 138 }
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
283 // Update the rendered document. It will send notifications to the listener. 185 // Update the rendered document. It will send notifications to the listener.
284 document->SetPage(params.page_number, 186 document->SetPage(params.page_number,
285 metafile.release(), 187 metafile.release(),
286 params.actual_shrink, 188 params.actual_shrink,
287 params.page_size, 189 params.page_size,
288 params.content_area); 190 params.content_area);
289 191
290 ShouldQuitFromInnerMessageLoop(); 192 ShouldQuitFromInnerMessageLoop();
291 } 193 }
292 194
293 void PrintViewManager::OnPrintingFailed(int cookie) { 195 void PrintViewManagerBase::OnPrintingFailed(int cookie) {
294 if (cookie != cookie_) { 196 if (cookie != cookie_) {
295 NOTREACHED(); 197 NOTREACHED();
296 return; 198 return;
297 } 199 }
298 200
201 #if defined(ENABLE_FULL_PRINTING)
299 chrome::ShowPrintErrorDialog( 202 chrome::ShowPrintErrorDialog(
300 web_contents()->GetView()->GetTopLevelNativeWindow()); 203 web_contents()->GetView()->GetTopLevelNativeWindow());
204 #endif
301 205
302 ReleasePrinterQuery(); 206 ReleasePrinterQuery();
303 207
304 content::NotificationService::current()->Notify( 208 content::NotificationService::current()->Notify(
305 chrome::NOTIFICATION_PRINT_JOB_RELEASED, 209 chrome::NOTIFICATION_PRINT_JOB_RELEASED,
306 content::Source<content::WebContents>(web_contents()), 210 content::Source<content::WebContents>(web_contents()),
307 content::NotificationService::NoDetails()); 211 content::NotificationService::NoDetails());
308 } 212 }
309 213
310 void PrintViewManager::OnScriptedPrintPreview(bool source_is_modifiable, 214 void PrintViewManagerBase::DidStartLoading(
311 IPC::Message* reply_msg) {
312 BrowserThread::CurrentlyOn(BrowserThread::UI);
313 ScriptedPrintPreviewClosureMap& map =
314 g_scripted_print_preview_closure_map.Get();
315 content::RenderProcessHost* rph = web_contents()->GetRenderProcessHost();
316
317 // This should always be 0 once we get modal window.print().
318 if (map.count(rph) != 0) {
319 // Renderer already handling window.print() in another View.
320 Send(reply_msg);
321 return;
322 }
323 if (print_preview_state_ != NOT_PREVIEWING) {
324 // If a user initiated print dialog is already open, ignore the scripted
325 // print message.
326 DCHECK_EQ(USER_INITIATED_PREVIEW, print_preview_state_);
327 Send(reply_msg);
328 return;
329 }
330
331 PrintPreviewDialogController* dialog_controller =
332 PrintPreviewDialogController::GetInstance();
333 if (!dialog_controller) {
334 Send(reply_msg);
335 return;
336 }
337
338 print_preview_state_ = SCRIPTED_PREVIEW;
339 base::Closure callback =
340 base::Bind(&PrintViewManager::OnScriptedPrintPreviewReply,
341 base::Unretained(this),
342 reply_msg);
343 map[rph] = callback;
344 scripted_print_preview_rph_ = rph;
345
346 dialog_controller->PrintPreview(web_contents());
347 PrintHostMsg_RequestPrintPreview_Params params;
348 params.is_modifiable = source_is_modifiable;
349 PrintPreviewUI::SetInitialParams(
350 dialog_controller->GetPrintPreviewForContents(web_contents()), params);
351 }
352
353 void PrintViewManager::OnScriptedPrintPreviewReply(IPC::Message* reply_msg) {
354 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
355 Send(reply_msg);
356 }
357
358 void PrintViewManager::DidStartLoading(
359 content::RenderViewHost* render_view_host) { 215 content::RenderViewHost* render_view_host) {
360 UpdateScriptedPrintingBlocked(); 216 UpdateScriptedPrintingBlocked();
361 } 217 }
362 218
363 bool PrintViewManager::OnMessageReceived(const IPC::Message& message) { 219 bool PrintViewManagerBase::OnMessageReceived(const IPC::Message& message) {
364 bool handled = true; 220 bool handled = true;
365 IPC_BEGIN_MESSAGE_MAP(PrintViewManager, message) 221 IPC_BEGIN_MESSAGE_MAP(PrintViewManagerBase, message)
366 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPrintedPagesCount, 222 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPrintedPagesCount,
367 OnDidGetPrintedPagesCount) 223 OnDidGetPrintedPagesCount)
368 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetDocumentCookie, 224 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetDocumentCookie,
369 OnDidGetDocumentCookie) 225 OnDidGetDocumentCookie)
370 IPC_MESSAGE_HANDLER(PrintHostMsg_DidShowPrintDialog, OnDidShowPrintDialog)
371 IPC_MESSAGE_HANDLER(PrintHostMsg_DidPrintPage, OnDidPrintPage) 226 IPC_MESSAGE_HANDLER(PrintHostMsg_DidPrintPage, OnDidPrintPage)
372 IPC_MESSAGE_HANDLER(PrintHostMsg_PrintingFailed, OnPrintingFailed) 227 IPC_MESSAGE_HANDLER(PrintHostMsg_PrintingFailed, OnPrintingFailed)
373 IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_ScriptedPrintPreview,
374 OnScriptedPrintPreview)
375 IPC_MESSAGE_UNHANDLED(handled = false) 228 IPC_MESSAGE_UNHANDLED(handled = false)
376 IPC_END_MESSAGE_MAP() 229 IPC_END_MESSAGE_MAP()
377 return handled; 230 return handled;
378 } 231 }
379 232
380 void PrintViewManager::Observe(int type, 233 void PrintViewManagerBase::Observe(
381 const content::NotificationSource& source, 234 int type,
382 const content::NotificationDetails& details) { 235 const content::NotificationSource& source,
236 const content::NotificationDetails& details) {
383 switch (type) { 237 switch (type) {
384 case chrome::NOTIFICATION_PRINT_JOB_EVENT: { 238 case chrome::NOTIFICATION_PRINT_JOB_EVENT: {
385 OnNotifyPrintJobEvent(*content::Details<JobEventDetails>(details).ptr()); 239 OnNotifyPrintJobEvent(*content::Details<JobEventDetails>(details).ptr());
386 break; 240 break;
387 } 241 }
388 case chrome::NOTIFICATION_CONTENT_BLOCKED_STATE_CHANGED: { 242 case chrome::NOTIFICATION_CONTENT_BLOCKED_STATE_CHANGED: {
389 tab_content_blocked_ = *content::Details<const bool>(details).ptr(); 243 tab_content_blocked_ = *content::Details<const bool>(details).ptr();
390 UpdateScriptedPrintingBlocked(); 244 UpdateScriptedPrintingBlocked();
391 break; 245 break;
392 } 246 }
393 default: { 247 default: {
394 NOTREACHED(); 248 NOTREACHED();
395 break; 249 break;
396 } 250 }
397 } 251 }
398 } 252 }
399 253
400 void PrintViewManager::OnNotifyPrintJobEvent( 254 void PrintViewManagerBase::OnNotifyPrintJobEvent(
401 const JobEventDetails& event_details) { 255 const JobEventDetails& event_details) {
402 switch (event_details.type()) { 256 switch (event_details.type()) {
403 case JobEventDetails::FAILED: { 257 case JobEventDetails::FAILED: {
404 TerminatePrintJob(true); 258 TerminatePrintJob(true);
405 259
406 content::NotificationService::current()->Notify( 260 content::NotificationService::current()->Notify(
407 chrome::NOTIFICATION_PRINT_JOB_RELEASED, 261 chrome::NOTIFICATION_PRINT_JOB_RELEASED,
408 content::Source<content::WebContents>(web_contents()), 262 content::Source<content::WebContents>(web_contents()),
409 content::NotificationService::NoDetails()); 263 content::NotificationService::NoDetails());
410 break; 264 break;
(...skipping 28 matching lines...) Expand all
439 content::NotificationService::NoDetails()); 293 content::NotificationService::NoDetails());
440 break; 294 break;
441 } 295 }
442 default: { 296 default: {
443 NOTREACHED(); 297 NOTREACHED();
444 break; 298 break;
445 } 299 }
446 } 300 }
447 } 301 }
448 302
449 bool PrintViewManager::RenderAllMissingPagesNow() { 303 bool PrintViewManagerBase::RenderAllMissingPagesNow() {
450 if (!print_job_.get() || !print_job_->is_job_pending()) 304 if (!print_job_.get() || !print_job_->is_job_pending())
451 return false; 305 return false;
452 306
453 // We can't print if there is no renderer. 307 // We can't print if there is no renderer.
454 if (!web_contents() || 308 if (!web_contents() ||
455 !web_contents()->GetRenderViewHost() || 309 !web_contents()->GetRenderViewHost() ||
456 !web_contents()->GetRenderViewHost()->IsRenderViewLive()) { 310 !web_contents()->GetRenderViewHost()->IsRenderViewLive()) {
457 return false; 311 return false;
458 } 312 }
459 313
(...skipping 15 matching lines...) Expand all
475 // ShouldQuitFromInnerMessageLoop(). 329 // ShouldQuitFromInnerMessageLoop().
476 // BLOCKS until all the pages are received. (Need to enable recursive task) 330 // BLOCKS until all the pages are received. (Need to enable recursive task)
477 if (!RunInnerMessageLoop()) { 331 if (!RunInnerMessageLoop()) {
478 // This function is always called from DisconnectFromCurrentPrintJob() so we 332 // This function is always called from DisconnectFromCurrentPrintJob() so we
479 // know that the job will be stopped/canceled in any case. 333 // know that the job will be stopped/canceled in any case.
480 return false; 334 return false;
481 } 335 }
482 return true; 336 return true;
483 } 337 }
484 338
485 void PrintViewManager::ShouldQuitFromInnerMessageLoop() { 339 void PrintViewManagerBase::ShouldQuitFromInnerMessageLoop() {
486 // Look at the reason. 340 // Look at the reason.
487 DCHECK(print_job_->document()); 341 DCHECK(print_job_->document());
488 if (print_job_->document() && 342 if (print_job_->document() &&
489 print_job_->document()->IsComplete() && 343 print_job_->document()->IsComplete() &&
490 inside_inner_message_loop_) { 344 inside_inner_message_loop_) {
491 // We are in a message loop created by RenderAllMissingPagesNow. Quit from 345 // We are in a message loop created by RenderAllMissingPagesNow. Quit from
492 // it. 346 // it.
493 base::MessageLoop::current()->Quit(); 347 base::MessageLoop::current()->Quit();
494 inside_inner_message_loop_ = false; 348 inside_inner_message_loop_ = false;
495 } 349 }
496 } 350 }
497 351
498 bool PrintViewManager::CreateNewPrintJob(PrintJobWorkerOwner* job) { 352 bool PrintViewManagerBase::CreateNewPrintJob(PrintJobWorkerOwner* job) {
499 DCHECK(!inside_inner_message_loop_); 353 DCHECK(!inside_inner_message_loop_);
500 354
501 // Disconnect the current print_job_. 355 // Disconnect the current print_job_.
502 DisconnectFromCurrentPrintJob(); 356 DisconnectFromCurrentPrintJob();
503 357
504 // We can't print if there is no renderer. 358 // We can't print if there is no renderer.
505 if (!web_contents()->GetRenderViewHost() || 359 if (!web_contents()->GetRenderViewHost() ||
506 !web_contents()->GetRenderViewHost()->IsRenderViewLive()) { 360 !web_contents()->GetRenderViewHost()->IsRenderViewLive()) {
507 return false; 361 return false;
508 } 362 }
509 363
510 // Ask the renderer to generate the print preview, create the print preview 364 // Ask the renderer to generate the print preview, create the print preview
511 // view and switch to it, initialize the printer and show the print dialog. 365 // view and switch to it, initialize the printer and show the print dialog.
512 DCHECK(!print_job_.get()); 366 DCHECK(!print_job_.get());
513 DCHECK(job); 367 DCHECK(job);
514 if (!job) 368 if (!job)
515 return false; 369 return false;
516 370
517 print_job_ = new PrintJob(); 371 print_job_ = new PrintJob();
518 print_job_->Initialize(job, this, number_pages_); 372 print_job_->Initialize(job, this, number_pages_);
519 registrar_.Add(this, chrome::NOTIFICATION_PRINT_JOB_EVENT, 373 registrar_.Add(this, chrome::NOTIFICATION_PRINT_JOB_EVENT,
520 content::Source<PrintJob>(print_job_.get())); 374 content::Source<PrintJob>(print_job_.get()));
521 printing_succeeded_ = false; 375 printing_succeeded_ = false;
522 return true; 376 return true;
523 } 377 }
524 378
525 void PrintViewManager::DisconnectFromCurrentPrintJob() { 379 void PrintViewManagerBase::DisconnectFromCurrentPrintJob() {
526 // Make sure all the necessary rendered page are done. Don't bother with the 380 // Make sure all the necessary rendered page are done. Don't bother with the
527 // return value. 381 // return value.
528 bool result = RenderAllMissingPagesNow(); 382 bool result = RenderAllMissingPagesNow();
529 383
530 // Verify that assertion. 384 // Verify that assertion.
531 if (print_job_.get() && 385 if (print_job_.get() &&
532 print_job_->document() && 386 print_job_->document() &&
533 !print_job_->document()->IsComplete()) { 387 !print_job_->document()->IsComplete()) {
534 DCHECK(!result); 388 DCHECK(!result);
535 // That failed. 389 // That failed.
536 TerminatePrintJob(true); 390 TerminatePrintJob(true);
537 } else { 391 } else {
538 // DO NOT wait for the job to finish. 392 // DO NOT wait for the job to finish.
539 ReleasePrintJob(); 393 ReleasePrintJob();
540 } 394 }
541 #if defined(OS_POSIX) && !defined(OS_MACOSX) 395 #if defined(OS_POSIX) && !defined(OS_MACOSX)
542 expecting_first_page_ = true; 396 expecting_first_page_ = true;
543 #endif 397 #endif
544 } 398 }
545 399
546 void PrintViewManager::PrintingDone(bool success) { 400 void PrintViewManagerBase::PrintingDone(bool success) {
547 if (!print_job_.get()) 401 if (!print_job_.get())
548 return; 402 return;
549 Send(new PrintMsg_PrintingDone(routing_id(), success)); 403 Send(new PrintMsg_PrintingDone(routing_id(), success));
550 } 404 }
551 405
552 void PrintViewManager::TerminatePrintJob(bool cancel) { 406 void PrintViewManagerBase::TerminatePrintJob(bool cancel) {
553 if (!print_job_.get()) 407 if (!print_job_.get())
554 return; 408 return;
555 409
556 if (cancel) { 410 if (cancel) {
557 // We don't need the metafile data anymore because the printing is canceled. 411 // We don't need the metafile data anymore because the printing is canceled.
558 print_job_->Cancel(); 412 print_job_->Cancel();
559 inside_inner_message_loop_ = false; 413 inside_inner_message_loop_ = false;
560 } else { 414 } else {
561 DCHECK(!inside_inner_message_loop_); 415 DCHECK(!inside_inner_message_loop_);
562 DCHECK(!print_job_->document() || print_job_->document()->IsComplete()); 416 DCHECK(!print_job_->document() || print_job_->document()->IsComplete());
563 417
564 // WebContents is either dying or navigating elsewhere. We need to render 418 // WebContents is either dying or navigating elsewhere. We need to render
565 // all the pages in an hurry if a print job is still pending. This does the 419 // all the pages in an hurry if a print job is still pending. This does the
566 // trick since it runs a blocking message loop: 420 // trick since it runs a blocking message loop:
567 print_job_->Stop(); 421 print_job_->Stop();
568 } 422 }
569 ReleasePrintJob(); 423 ReleasePrintJob();
570 } 424 }
571 425
572 void PrintViewManager::ReleasePrintJob() { 426 void PrintViewManagerBase::ReleasePrintJob() {
573 if (!print_job_.get()) 427 if (!print_job_.get())
574 return; 428 return;
575 429
576 PrintingDone(printing_succeeded_); 430 PrintingDone(printing_succeeded_);
577 431
578 registrar_.Remove(this, chrome::NOTIFICATION_PRINT_JOB_EVENT, 432 registrar_.Remove(this, chrome::NOTIFICATION_PRINT_JOB_EVENT,
579 content::Source<PrintJob>(print_job_.get())); 433 content::Source<PrintJob>(print_job_.get()));
580 print_job_->DisconnectSource(); 434 print_job_->DisconnectSource();
581 // Don't close the worker thread. 435 // Don't close the worker thread.
582 print_job_ = NULL; 436 print_job_ = NULL;
583 } 437 }
584 438
585 bool PrintViewManager::RunInnerMessageLoop() { 439 bool PrintViewManagerBase::RunInnerMessageLoop() {
586 // This value may actually be too low: 440 // This value may actually be too low:
587 // 441 //
588 // - If we're looping because of printer settings initialization, the premise 442 // - If we're looping because of printer settings initialization, the premise
589 // here is that some poor users have their print server away on a VPN over a 443 // here is that some poor users have their print server away on a VPN over a
590 // slow connection. In this situation, the simple fact of opening the printer 444 // slow connection. In this situation, the simple fact of opening the printer
591 // can be dead slow. On the other side, we don't want to die infinitely for a 445 // can be dead slow. On the other side, we don't want to die infinitely for a
592 // real network error. Give the printer 60 seconds to comply. 446 // real network error. Give the printer 60 seconds to comply.
593 // 447 //
594 // - If we're looping because of renderer page generation, the renderer could 448 // - If we're looping because of renderer page generation, the renderer could
595 // be CPU bound, the page overly complex/large or the system just 449 // be CPU bound, the page overly complex/large or the system just
(...skipping 16 matching lines...) Expand all
612 bool success = true; 466 bool success = true;
613 if (inside_inner_message_loop_) { 467 if (inside_inner_message_loop_) {
614 // Ok we timed out. That's sad. 468 // Ok we timed out. That's sad.
615 inside_inner_message_loop_ = false; 469 inside_inner_message_loop_ = false;
616 success = false; 470 success = false;
617 } 471 }
618 472
619 return success; 473 return success;
620 } 474 }
621 475
622 bool PrintViewManager::OpportunisticallyCreatePrintJob(int cookie) { 476 bool PrintViewManagerBase::OpportunisticallyCreatePrintJob(int cookie) {
623 if (print_job_.get()) 477 if (print_job_.get())
624 return true; 478 return true;
625 479
626 if (!cookie) { 480 if (!cookie) {
627 // Out of sync. It may happens since we are completely asynchronous. Old 481 // Out of sync. It may happens since we are completely asynchronous. Old
628 // spurious message can happen if one of the processes is overloaded. 482 // spurious message can happen if one of the processes is overloaded.
629 return false; 483 return false;
630 } 484 }
631 485
632 // The job was initiated by a script. Time to get the corresponding worker 486 // The job was initiated by a script. Time to get the corresponding worker
633 // thread. 487 // thread.
634 scoped_refptr<PrinterQuery> queued_query; 488 scoped_refptr<PrinterQuery> queued_query;
635 g_browser_process->print_job_manager()->PopPrinterQuery(cookie, 489 g_browser_process->print_job_manager()->PopPrinterQuery(cookie,
636 &queued_query); 490 &queued_query);
637 DCHECK(queued_query.get()); 491 DCHECK(queued_query.get());
638 if (!queued_query.get()) 492 if (!queued_query.get())
639 return false; 493 return false;
640 494
641 if (!CreateNewPrintJob(queued_query.get())) { 495 if (!CreateNewPrintJob(queued_query.get())) {
642 // Don't kill anything. 496 // Don't kill anything.
643 return false; 497 return false;
644 } 498 }
645 499
646 // Settings are already loaded. Go ahead. This will set 500 // Settings are already loaded. Go ahead. This will set
647 // print_job_->is_job_pending() to true. 501 // print_job_->is_job_pending() to true.
648 print_job_->StartPrinting(); 502 print_job_->StartPrinting();
649 return true; 503 return true;
650 } 504 }
651 505
652 bool PrintViewManager::PrintNowInternal(IPC::Message* message) { 506 bool PrintViewManagerBase::PrintNowInternal(IPC::Message* message) {
653 // Don't print / print preview interstitials. 507 // Don't print / print preview interstitials.
654 if (web_contents()->ShowingInterstitialPage()) { 508 if (web_contents()->ShowingInterstitialPage()) {
655 delete message; 509 delete message;
656 return false; 510 return false;
657 } 511 }
658 return Send(message); 512 return Send(message);
659 } 513 }
660 514
661 void PrintViewManager::ReleasePrinterQuery() { 515 void PrintViewManagerBase::ReleasePrinterQuery() {
662 if (!cookie_) 516 if (!cookie_)
663 return; 517 return;
664 518
665 int cookie = cookie_; 519 int cookie = cookie_;
666 cookie_ = 0; 520 cookie_ = 0;
667 g_browser_process->print_job_manager()->SetPrintDestination(NULL); 521 g_browser_process->print_job_manager()->SetPrintDestination(NULL);
668 522
669 523
670 printing::PrintJobManager* print_job_manager = 524 printing::PrintJobManager* print_job_manager =
671 g_browser_process->print_job_manager(); 525 g_browser_process->print_job_manager();
672 // May be NULL in tests. 526 // May be NULL in tests.
673 if (!print_job_manager) 527 if (!print_job_manager)
674 return; 528 return;
675 529
676 scoped_refptr<printing::PrinterQuery> printer_query; 530 scoped_refptr<printing::PrinterQuery> printer_query;
677 print_job_manager->PopPrinterQuery(cookie, &printer_query); 531 print_job_manager->PopPrinterQuery(cookie, &printer_query);
678 if (!printer_query.get()) 532 if (!printer_query.get())
679 return; 533 return;
680 BrowserThread::PostTask( 534 BrowserThread::PostTask(
681 BrowserThread::IO, FROM_HERE, 535 BrowserThread::IO, FROM_HERE,
682 base::Bind(&PrinterQuery::StopWorker, printer_query.get())); 536 base::Bind(&PrinterQuery::StopWorker, printer_query.get()));
683 } 537 }
684 538
685 } // namespace printing 539 } // namespace printing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698