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

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: rebase 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"
11 #include "base/lazy_instance.h"
12 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
13 #include "base/metrics/histogram.h"
14 #include "base/prefs/pref_service.h" 11 #include "base/prefs/pref_service.h"
15 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
16 #include "base/timer/timer.h" 13 #include "base/timer/timer.h"
17 #include "chrome/browser/browser_process.h" 14 #include "chrome/browser/browser_process.h"
18 #include "chrome/browser/chrome_notification_types.h" 15 #include "chrome/browser/chrome_notification_types.h"
19 #include "chrome/browser/printing/print_error_dialog.h"
20 #include "chrome/browser/printing/print_job.h" 16 #include "chrome/browser/printing/print_job.h"
21 #include "chrome/browser/printing/print_job_manager.h" 17 #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" 18 #include "chrome/browser/printing/printer_query.h"
25 #include "chrome/browser/profiles/profile.h" 19 #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"
28 #include "chrome/common/pref_names.h" 20 #include "chrome/common/pref_names.h"
29 #include "chrome/common/print_messages.h" 21 #include "chrome/common/print_messages.h"
30 #include "content/public/browser/browser_thread.h" 22 #include "content/public/browser/browser_thread.h"
31 #include "content/public/browser/notification_details.h" 23 #include "content/public/browser/notification_details.h"
32 #include "content/public/browser/notification_service.h" 24 #include "content/public/browser/notification_service.h"
33 #include "content/public/browser/notification_source.h" 25 #include "content/public/browser/notification_source.h"
34 #include "content/public/browser/render_view_host.h" 26 #include "content/public/browser/render_view_host.h"
35 #include "content/public/browser/web_contents.h" 27 #include "content/public/browser/web_contents.h"
36 #include "content/public/browser/web_contents_view.h" 28 #include "content/public/browser/web_contents_view.h"
37 #include "grit/generated_resources.h" 29 #include "grit/generated_resources.h"
38 #include "printing/metafile.h"
39 #include "printing/metafile_impl.h" 30 #include "printing/metafile_impl.h"
40 #include "printing/print_destination_interface.h"
41 #include "printing/printed_document.h" 31 #include "printing/printed_document.h"
42 #include "ui/base/l10n/l10n_util.h" 32 #include "ui/base/l10n/l10n_util.h"
43 33
34 #if defined(OS_WIN)
35 #include "base/command_line.h"
36 #include "chrome/common/chrome_switches.h"
37 #endif
38
39 #if defined(ENABLE_FULL_PRINTING)
40 #include "chrome/browser/printing/print_error_dialog.h"
41 #endif
42
44 using base::TimeDelta; 43 using base::TimeDelta;
45 using content::BrowserThread; 44 using content::BrowserThread;
46 using content::WebContents;
47
48 DEFINE_WEB_CONTENTS_USER_DATA_KEY(printing::PrintViewManager);
49 45
50 namespace { 46 namespace {
51 47
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. 48 // Limits memory usage by raster to 64 MiB.
60 const int kMaxRasterSizeInPixels = 16*1024*1024; 49 const int kMaxRasterSizeInPixels = 16*1024*1024;
61 50
62 } // namespace 51 } // namespace
63 52
64 namespace printing { 53 namespace printing {
65 54
66 PrintViewManager::PrintViewManager(content::WebContents* web_contents) 55 PrintViewManagerBase::PrintViewManagerBase(content::WebContents* web_contents)
67 : content::WebContentsObserver(web_contents), 56 : content::WebContentsObserver(web_contents),
68 number_pages_(0), 57 number_pages_(0),
69 printing_succeeded_(false), 58 printing_succeeded_(false),
70 inside_inner_message_loop_(false), 59 inside_inner_message_loop_(false),
71 observer_(NULL),
72 cookie_(0), 60 cookie_(0),
73 print_preview_state_(NOT_PREVIEWING),
74 scripted_print_preview_rph_(NULL),
75 tab_content_blocked_(false) { 61 tab_content_blocked_(false) {
76 #if defined(OS_POSIX) && !defined(OS_MACOSX) 62 #if defined(OS_POSIX) && !defined(OS_MACOSX)
77 expecting_first_page_ = true; 63 expecting_first_page_ = true;
78 #endif 64 #endif
79 registrar_.Add(this, chrome::NOTIFICATION_CONTENT_BLOCKED_STATE_CHANGED, 65 registrar_.Add(this, chrome::NOTIFICATION_CONTENT_BLOCKED_STATE_CHANGED,
80 content::Source<content::WebContents>(web_contents)); 66 content::Source<content::WebContents>(web_contents));
81 Profile* profile = 67 Profile* profile =
82 Profile::FromBrowserContext(web_contents->GetBrowserContext()); 68 Profile::FromBrowserContext(web_contents->GetBrowserContext());
83 printing_enabled_.Init( 69 printing_enabled_.Init(
84 prefs::kPrintingEnabled, 70 prefs::kPrintingEnabled,
85 profile->GetPrefs(), 71 profile->GetPrefs(),
86 base::Bind(&PrintViewManager::UpdateScriptedPrintingBlocked, 72 base::Bind(&PrintViewManagerBase::UpdateScriptedPrintingBlocked,
87 base::Unretained(this))); 73 base::Unretained(this)));
88 } 74 }
89 75
90 PrintViewManager::~PrintViewManager() { 76 PrintViewManagerBase::~PrintViewManagerBase() {
91 DCHECK_EQ(NOT_PREVIEWING, print_preview_state_);
92 ReleasePrinterQuery(); 77 ReleasePrinterQuery();
93 DisconnectFromCurrentPrintJob(); 78 DisconnectFromCurrentPrintJob();
94 } 79 }
95 80
96 bool PrintViewManager::PrintNow() { 81 bool PrintViewManagerBase::PrintNow() {
97 return PrintNowInternal(new PrintMsg_PrintPages(routing_id())); 82 return PrintNowInternal(new PrintMsg_PrintPages(routing_id()));
98 } 83 }
99 84
100 bool PrintViewManager::PrintForSystemDialogNow() { 85 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( 86 Send(new PrintMsg_SetScriptedPrintingBlocked(
173 routing_id(), 87 routing_id(),
174 !printing_enabled_.GetValue() || tab_content_blocked_)); 88 !printing_enabled_.GetValue() || tab_content_blocked_));
175 } 89 }
176 90
177 void PrintViewManager::set_observer(PrintViewManagerObserver* observer) { 91 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. 92 // Cancel the current job, wait for the worker to finish.
184 TerminatePrintJob(true); 93 TerminatePrintJob(true);
185 } 94 }
186 95
187 void PrintViewManager::RenderProcessGone(base::TerminationStatus status) { 96 void PrintViewManagerBase::RenderProcessGone(base::TerminationStatus status) {
188 print_preview_state_ = NOT_PREVIEWING;
189 ReleasePrinterQuery(); 97 ReleasePrinterQuery();
190 98
191 if (!print_job_.get()) 99 if (!print_job_.get())
192 return; 100 return;
193 101
194 scoped_refptr<PrintedDocument> document(print_job_->document()); 102 scoped_refptr<PrintedDocument> document(print_job_->document());
195 if (document.get()) { 103 if (document.get()) {
196 // If IsComplete() returns false, the document isn't completely rendered. 104 // If IsComplete() returns false, the document isn't completely rendered.
197 // Since our renderer is gone, there's nothing to do, cancel it. Otherwise, 105 // Since our renderer is gone, there's nothing to do, cancel it. Otherwise,
198 // the print job may finish without problem. 106 // the print job may finish without problem.
199 TerminatePrintJob(!document->IsComplete()); 107 TerminatePrintJob(!document->IsComplete());
200 } 108 }
201 } 109 }
202 110
203 string16 PrintViewManager::RenderSourceName() { 111 string16 PrintViewManagerBase::RenderSourceName() {
204 string16 name(web_contents()->GetTitle()); 112 string16 name(web_contents()->GetTitle());
205 if (name.empty()) 113 if (name.empty())
206 name = l10n_util::GetStringUTF16(IDS_DEFAULT_PRINT_DOCUMENT_TITLE); 114 name = l10n_util::GetStringUTF16(IDS_DEFAULT_PRINT_DOCUMENT_TITLE);
207 return name; 115 return name;
208 } 116 }
209 117
210 void PrintViewManager::OnDidGetPrintedPagesCount(int cookie, int number_pages) { 118 void PrintViewManagerBase::OnDidGetPrintedPagesCount(int cookie,
119 int number_pages) {
211 DCHECK_GT(cookie, 0); 120 DCHECK_GT(cookie, 0);
212 DCHECK_GT(number_pages, 0); 121 DCHECK_GT(number_pages, 0);
213 number_pages_ = number_pages; 122 number_pages_ = number_pages;
214 OpportunisticallyCreatePrintJob(cookie); 123 OpportunisticallyCreatePrintJob(cookie);
215 } 124 }
216 125
217 void PrintViewManager::OnDidGetDocumentCookie(int cookie) { 126 void PrintViewManagerBase::OnDidGetDocumentCookie(int cookie) {
218 cookie_ = cookie; 127 cookie_ = cookie;
219 } 128 }
220 129
221 void PrintViewManager::OnDidShowPrintDialog() { 130 void PrintViewManagerBase::OnDidPrintPage(
222 if (observer_)
223 observer_->OnPrintDialogShown();
224 }
225
226 void PrintViewManager::OnDidPrintPage(
227 const PrintHostMsg_DidPrintPage_Params& params) { 131 const PrintHostMsg_DidPrintPage_Params& params) {
228 if (!OpportunisticallyCreatePrintJob(params.document_cookie)) 132 if (!OpportunisticallyCreatePrintJob(params.document_cookie))
229 return; 133 return;
230 134
231 PrintedDocument* document = print_job_->document(); 135 PrintedDocument* document = print_job_->document();
232 if (!document || params.document_cookie != document->cookie()) { 136 if (!document || params.document_cookie != document->cookie()) {
233 // Out of sync. It may happen since we are completely asynchronous. Old 137 // 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. 138 // spurious messages can be received if one of the processes is overloaded.
235 return; 139 return;
236 } 140 }
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
283 // Update the rendered document. It will send notifications to the listener. 187 // Update the rendered document. It will send notifications to the listener.
284 document->SetPage(params.page_number, 188 document->SetPage(params.page_number,
285 metafile.release(), 189 metafile.release(),
286 params.actual_shrink, 190 params.actual_shrink,
287 params.page_size, 191 params.page_size,
288 params.content_area); 192 params.content_area);
289 193
290 ShouldQuitFromInnerMessageLoop(); 194 ShouldQuitFromInnerMessageLoop();
291 } 195 }
292 196
293 void PrintViewManager::OnPrintingFailed(int cookie) { 197 void PrintViewManagerBase::OnPrintingFailed(int cookie) {
294 if (cookie != cookie_) { 198 if (cookie != cookie_) {
295 NOTREACHED(); 199 NOTREACHED();
296 return; 200 return;
297 } 201 }
298 202
203 #if defined(ENABLE_FULL_PRINTING)
299 chrome::ShowPrintErrorDialog( 204 chrome::ShowPrintErrorDialog(
300 web_contents()->GetView()->GetTopLevelNativeWindow()); 205 web_contents()->GetView()->GetTopLevelNativeWindow());
206 #endif
301 207
302 ReleasePrinterQuery(); 208 ReleasePrinterQuery();
303 209
304 content::NotificationService::current()->Notify( 210 content::NotificationService::current()->Notify(
305 chrome::NOTIFICATION_PRINT_JOB_RELEASED, 211 chrome::NOTIFICATION_PRINT_JOB_RELEASED,
306 content::Source<content::WebContents>(web_contents()), 212 content::Source<content::WebContents>(web_contents()),
307 content::NotificationService::NoDetails()); 213 content::NotificationService::NoDetails());
308 } 214 }
309 215
310 void PrintViewManager::OnScriptedPrintPreview(bool source_is_modifiable, 216 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) { 217 content::RenderViewHost* render_view_host) {
360 UpdateScriptedPrintingBlocked(); 218 UpdateScriptedPrintingBlocked();
361 } 219 }
362 220
363 bool PrintViewManager::OnMessageReceived(const IPC::Message& message) { 221 bool PrintViewManagerBase::OnMessageReceived(const IPC::Message& message) {
364 bool handled = true; 222 bool handled = true;
365 IPC_BEGIN_MESSAGE_MAP(PrintViewManager, message) 223 IPC_BEGIN_MESSAGE_MAP(PrintViewManagerBase, message)
366 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPrintedPagesCount, 224 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPrintedPagesCount,
367 OnDidGetPrintedPagesCount) 225 OnDidGetPrintedPagesCount)
368 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetDocumentCookie, 226 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetDocumentCookie,
369 OnDidGetDocumentCookie) 227 OnDidGetDocumentCookie)
370 IPC_MESSAGE_HANDLER(PrintHostMsg_DidShowPrintDialog, OnDidShowPrintDialog)
371 IPC_MESSAGE_HANDLER(PrintHostMsg_DidPrintPage, OnDidPrintPage) 228 IPC_MESSAGE_HANDLER(PrintHostMsg_DidPrintPage, OnDidPrintPage)
372 IPC_MESSAGE_HANDLER(PrintHostMsg_PrintingFailed, OnPrintingFailed) 229 IPC_MESSAGE_HANDLER(PrintHostMsg_PrintingFailed, OnPrintingFailed)
373 IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_ScriptedPrintPreview,
374 OnScriptedPrintPreview)
375 IPC_MESSAGE_UNHANDLED(handled = false) 230 IPC_MESSAGE_UNHANDLED(handled = false)
376 IPC_END_MESSAGE_MAP() 231 IPC_END_MESSAGE_MAP()
377 return handled; 232 return handled;
378 } 233 }
379 234
380 void PrintViewManager::Observe(int type, 235 void PrintViewManagerBase::Observe(
381 const content::NotificationSource& source, 236 int type,
382 const content::NotificationDetails& details) { 237 const content::NotificationSource& source,
238 const content::NotificationDetails& details) {
383 switch (type) { 239 switch (type) {
384 case chrome::NOTIFICATION_PRINT_JOB_EVENT: { 240 case chrome::NOTIFICATION_PRINT_JOB_EVENT: {
385 OnNotifyPrintJobEvent(*content::Details<JobEventDetails>(details).ptr()); 241 OnNotifyPrintJobEvent(*content::Details<JobEventDetails>(details).ptr());
386 break; 242 break;
387 } 243 }
388 case chrome::NOTIFICATION_CONTENT_BLOCKED_STATE_CHANGED: { 244 case chrome::NOTIFICATION_CONTENT_BLOCKED_STATE_CHANGED: {
389 tab_content_blocked_ = *content::Details<const bool>(details).ptr(); 245 tab_content_blocked_ = *content::Details<const bool>(details).ptr();
390 UpdateScriptedPrintingBlocked(); 246 UpdateScriptedPrintingBlocked();
391 break; 247 break;
392 } 248 }
393 default: { 249 default: {
394 NOTREACHED(); 250 NOTREACHED();
395 break; 251 break;
396 } 252 }
397 } 253 }
398 } 254 }
399 255
400 void PrintViewManager::OnNotifyPrintJobEvent( 256 void PrintViewManagerBase::OnNotifyPrintJobEvent(
401 const JobEventDetails& event_details) { 257 const JobEventDetails& event_details) {
402 switch (event_details.type()) { 258 switch (event_details.type()) {
403 case JobEventDetails::FAILED: { 259 case JobEventDetails::FAILED: {
404 TerminatePrintJob(true); 260 TerminatePrintJob(true);
405 261
406 content::NotificationService::current()->Notify( 262 content::NotificationService::current()->Notify(
407 chrome::NOTIFICATION_PRINT_JOB_RELEASED, 263 chrome::NOTIFICATION_PRINT_JOB_RELEASED,
408 content::Source<content::WebContents>(web_contents()), 264 content::Source<content::WebContents>(web_contents()),
409 content::NotificationService::NoDetails()); 265 content::NotificationService::NoDetails());
410 break; 266 break;
(...skipping 28 matching lines...) Expand all
439 content::NotificationService::NoDetails()); 295 content::NotificationService::NoDetails());
440 break; 296 break;
441 } 297 }
442 default: { 298 default: {
443 NOTREACHED(); 299 NOTREACHED();
444 break; 300 break;
445 } 301 }
446 } 302 }
447 } 303 }
448 304
449 bool PrintViewManager::RenderAllMissingPagesNow() { 305 bool PrintViewManagerBase::RenderAllMissingPagesNow() {
450 if (!print_job_.get() || !print_job_->is_job_pending()) 306 if (!print_job_.get() || !print_job_->is_job_pending())
451 return false; 307 return false;
452 308
453 // We can't print if there is no renderer. 309 // We can't print if there is no renderer.
454 if (!web_contents() || 310 if (!web_contents() ||
455 !web_contents()->GetRenderViewHost() || 311 !web_contents()->GetRenderViewHost() ||
456 !web_contents()->GetRenderViewHost()->IsRenderViewLive()) { 312 !web_contents()->GetRenderViewHost()->IsRenderViewLive()) {
457 return false; 313 return false;
458 } 314 }
459 315
(...skipping 15 matching lines...) Expand all
475 // ShouldQuitFromInnerMessageLoop(). 331 // ShouldQuitFromInnerMessageLoop().
476 // BLOCKS until all the pages are received. (Need to enable recursive task) 332 // BLOCKS until all the pages are received. (Need to enable recursive task)
477 if (!RunInnerMessageLoop()) { 333 if (!RunInnerMessageLoop()) {
478 // This function is always called from DisconnectFromCurrentPrintJob() so we 334 // This function is always called from DisconnectFromCurrentPrintJob() so we
479 // know that the job will be stopped/canceled in any case. 335 // know that the job will be stopped/canceled in any case.
480 return false; 336 return false;
481 } 337 }
482 return true; 338 return true;
483 } 339 }
484 340
485 void PrintViewManager::ShouldQuitFromInnerMessageLoop() { 341 void PrintViewManagerBase::ShouldQuitFromInnerMessageLoop() {
486 // Look at the reason. 342 // Look at the reason.
487 DCHECK(print_job_->document()); 343 DCHECK(print_job_->document());
488 if (print_job_->document() && 344 if (print_job_->document() &&
489 print_job_->document()->IsComplete() && 345 print_job_->document()->IsComplete() &&
490 inside_inner_message_loop_) { 346 inside_inner_message_loop_) {
491 // We are in a message loop created by RenderAllMissingPagesNow. Quit from 347 // We are in a message loop created by RenderAllMissingPagesNow. Quit from
492 // it. 348 // it.
493 base::MessageLoop::current()->Quit(); 349 base::MessageLoop::current()->Quit();
494 inside_inner_message_loop_ = false; 350 inside_inner_message_loop_ = false;
495 } 351 }
496 } 352 }
497 353
498 bool PrintViewManager::CreateNewPrintJob(PrintJobWorkerOwner* job) { 354 bool PrintViewManagerBase::CreateNewPrintJob(PrintJobWorkerOwner* job) {
499 DCHECK(!inside_inner_message_loop_); 355 DCHECK(!inside_inner_message_loop_);
500 356
501 // Disconnect the current print_job_. 357 // Disconnect the current print_job_.
502 DisconnectFromCurrentPrintJob(); 358 DisconnectFromCurrentPrintJob();
503 359
504 // We can't print if there is no renderer. 360 // We can't print if there is no renderer.
505 if (!web_contents()->GetRenderViewHost() || 361 if (!web_contents()->GetRenderViewHost() ||
506 !web_contents()->GetRenderViewHost()->IsRenderViewLive()) { 362 !web_contents()->GetRenderViewHost()->IsRenderViewLive()) {
507 return false; 363 return false;
508 } 364 }
509 365
510 // Ask the renderer to generate the print preview, create the print preview 366 // 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. 367 // view and switch to it, initialize the printer and show the print dialog.
512 DCHECK(!print_job_.get()); 368 DCHECK(!print_job_.get());
513 DCHECK(job); 369 DCHECK(job);
514 if (!job) 370 if (!job)
515 return false; 371 return false;
516 372
517 print_job_ = new PrintJob(); 373 print_job_ = new PrintJob();
518 print_job_->Initialize(job, this, number_pages_); 374 print_job_->Initialize(job, this, number_pages_);
519 registrar_.Add(this, chrome::NOTIFICATION_PRINT_JOB_EVENT, 375 registrar_.Add(this, chrome::NOTIFICATION_PRINT_JOB_EVENT,
520 content::Source<PrintJob>(print_job_.get())); 376 content::Source<PrintJob>(print_job_.get()));
521 printing_succeeded_ = false; 377 printing_succeeded_ = false;
522 return true; 378 return true;
523 } 379 }
524 380
525 void PrintViewManager::DisconnectFromCurrentPrintJob() { 381 void PrintViewManagerBase::DisconnectFromCurrentPrintJob() {
526 // Make sure all the necessary rendered page are done. Don't bother with the 382 // Make sure all the necessary rendered page are done. Don't bother with the
527 // return value. 383 // return value.
528 bool result = RenderAllMissingPagesNow(); 384 bool result = RenderAllMissingPagesNow();
529 385
530 // Verify that assertion. 386 // Verify that assertion.
531 if (print_job_.get() && 387 if (print_job_.get() &&
532 print_job_->document() && 388 print_job_->document() &&
533 !print_job_->document()->IsComplete()) { 389 !print_job_->document()->IsComplete()) {
534 DCHECK(!result); 390 DCHECK(!result);
535 // That failed. 391 // That failed.
536 TerminatePrintJob(true); 392 TerminatePrintJob(true);
537 } else { 393 } else {
538 // DO NOT wait for the job to finish. 394 // DO NOT wait for the job to finish.
539 ReleasePrintJob(); 395 ReleasePrintJob();
540 } 396 }
541 #if defined(OS_POSIX) && !defined(OS_MACOSX) 397 #if defined(OS_POSIX) && !defined(OS_MACOSX)
542 expecting_first_page_ = true; 398 expecting_first_page_ = true;
543 #endif 399 #endif
544 } 400 }
545 401
546 void PrintViewManager::PrintingDone(bool success) { 402 void PrintViewManagerBase::PrintingDone(bool success) {
547 if (!print_job_.get()) 403 if (!print_job_.get())
548 return; 404 return;
549 Send(new PrintMsg_PrintingDone(routing_id(), success)); 405 Send(new PrintMsg_PrintingDone(routing_id(), success));
550 } 406 }
551 407
552 void PrintViewManager::TerminatePrintJob(bool cancel) { 408 void PrintViewManagerBase::TerminatePrintJob(bool cancel) {
553 if (!print_job_.get()) 409 if (!print_job_.get())
554 return; 410 return;
555 411
556 if (cancel) { 412 if (cancel) {
557 // We don't need the metafile data anymore because the printing is canceled. 413 // We don't need the metafile data anymore because the printing is canceled.
558 print_job_->Cancel(); 414 print_job_->Cancel();
559 inside_inner_message_loop_ = false; 415 inside_inner_message_loop_ = false;
560 } else { 416 } else {
561 DCHECK(!inside_inner_message_loop_); 417 DCHECK(!inside_inner_message_loop_);
562 DCHECK(!print_job_->document() || print_job_->document()->IsComplete()); 418 DCHECK(!print_job_->document() || print_job_->document()->IsComplete());
563 419
564 // WebContents is either dying or navigating elsewhere. We need to render 420 // 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 421 // 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: 422 // trick since it runs a blocking message loop:
567 print_job_->Stop(); 423 print_job_->Stop();
568 } 424 }
569 ReleasePrintJob(); 425 ReleasePrintJob();
570 } 426 }
571 427
572 void PrintViewManager::ReleasePrintJob() { 428 void PrintViewManagerBase::ReleasePrintJob() {
573 if (!print_job_.get()) 429 if (!print_job_.get())
574 return; 430 return;
575 431
576 PrintingDone(printing_succeeded_); 432 PrintingDone(printing_succeeded_);
577 433
578 registrar_.Remove(this, chrome::NOTIFICATION_PRINT_JOB_EVENT, 434 registrar_.Remove(this, chrome::NOTIFICATION_PRINT_JOB_EVENT,
579 content::Source<PrintJob>(print_job_.get())); 435 content::Source<PrintJob>(print_job_.get()));
580 print_job_->DisconnectSource(); 436 print_job_->DisconnectSource();
581 // Don't close the worker thread. 437 // Don't close the worker thread.
582 print_job_ = NULL; 438 print_job_ = NULL;
583 } 439 }
584 440
585 bool PrintViewManager::RunInnerMessageLoop() { 441 bool PrintViewManagerBase::RunInnerMessageLoop() {
586 // This value may actually be too low: 442 // This value may actually be too low:
587 // 443 //
588 // - If we're looping because of printer settings initialization, the premise 444 // - 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 445 // 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 446 // 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 447 // 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. 448 // real network error. Give the printer 60 seconds to comply.
593 // 449 //
594 // - If we're looping because of renderer page generation, the renderer could 450 // - 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 451 // be CPU bound, the page overly complex/large or the system just
(...skipping 16 matching lines...) Expand all
612 bool success = true; 468 bool success = true;
613 if (inside_inner_message_loop_) { 469 if (inside_inner_message_loop_) {
614 // Ok we timed out. That's sad. 470 // Ok we timed out. That's sad.
615 inside_inner_message_loop_ = false; 471 inside_inner_message_loop_ = false;
616 success = false; 472 success = false;
617 } 473 }
618 474
619 return success; 475 return success;
620 } 476 }
621 477
622 bool PrintViewManager::OpportunisticallyCreatePrintJob(int cookie) { 478 bool PrintViewManagerBase::OpportunisticallyCreatePrintJob(int cookie) {
623 if (print_job_.get()) 479 if (print_job_.get())
624 return true; 480 return true;
625 481
626 if (!cookie) { 482 if (!cookie) {
627 // Out of sync. It may happens since we are completely asynchronous. Old 483 // Out of sync. It may happens since we are completely asynchronous. Old
628 // spurious message can happen if one of the processes is overloaded. 484 // spurious message can happen if one of the processes is overloaded.
629 return false; 485 return false;
630 } 486 }
631 487
632 // The job was initiated by a script. Time to get the corresponding worker 488 // The job was initiated by a script. Time to get the corresponding worker
633 // thread. 489 // thread.
634 scoped_refptr<PrinterQuery> queued_query; 490 scoped_refptr<PrinterQuery> queued_query;
635 g_browser_process->print_job_manager()->PopPrinterQuery(cookie, 491 g_browser_process->print_job_manager()->PopPrinterQuery(cookie,
636 &queued_query); 492 &queued_query);
637 DCHECK(queued_query.get()); 493 DCHECK(queued_query.get());
638 if (!queued_query.get()) 494 if (!queued_query.get())
639 return false; 495 return false;
640 496
641 if (!CreateNewPrintJob(queued_query.get())) { 497 if (!CreateNewPrintJob(queued_query.get())) {
642 // Don't kill anything. 498 // Don't kill anything.
643 return false; 499 return false;
644 } 500 }
645 501
646 // Settings are already loaded. Go ahead. This will set 502 // Settings are already loaded. Go ahead. This will set
647 // print_job_->is_job_pending() to true. 503 // print_job_->is_job_pending() to true.
648 print_job_->StartPrinting(); 504 print_job_->StartPrinting();
649 return true; 505 return true;
650 } 506 }
651 507
652 bool PrintViewManager::PrintNowInternal(IPC::Message* message) { 508 bool PrintViewManagerBase::PrintNowInternal(IPC::Message* message) {
653 // Don't print / print preview interstitials. 509 // Don't print / print preview interstitials.
654 if (web_contents()->ShowingInterstitialPage()) { 510 if (web_contents()->ShowingInterstitialPage()) {
655 delete message; 511 delete message;
656 return false; 512 return false;
657 } 513 }
658 return Send(message); 514 return Send(message);
659 } 515 }
660 516
661 void PrintViewManager::ReleasePrinterQuery() { 517 void PrintViewManagerBase::ReleasePrinterQuery() {
662 if (!cookie_) 518 if (!cookie_)
663 return; 519 return;
664 520
665 int cookie = cookie_; 521 int cookie = cookie_;
666 cookie_ = 0; 522 cookie_ = 0;
667 g_browser_process->print_job_manager()->SetPrintDestination(NULL); 523 g_browser_process->print_job_manager()->SetPrintDestination(NULL);
668 524
669 525
670 printing::PrintJobManager* print_job_manager = 526 printing::PrintJobManager* print_job_manager =
671 g_browser_process->print_job_manager(); 527 g_browser_process->print_job_manager();
672 // May be NULL in tests. 528 // May be NULL in tests.
673 if (!print_job_manager) 529 if (!print_job_manager)
674 return; 530 return;
675 531
676 scoped_refptr<printing::PrinterQuery> printer_query; 532 scoped_refptr<printing::PrinterQuery> printer_query;
677 print_job_manager->PopPrinterQuery(cookie, &printer_query); 533 print_job_manager->PopPrinterQuery(cookie, &printer_query);
678 if (!printer_query.get()) 534 if (!printer_query.get())
679 return; 535 return;
680 BrowserThread::PostTask( 536 BrowserThread::PostTask(
681 BrowserThread::IO, FROM_HERE, 537 BrowserThread::IO, FROM_HERE,
682 base::Bind(&PrinterQuery::StopWorker, printer_query.get())); 538 base::Bind(&PrinterQuery::StopWorker, printer_query.get()));
683 } 539 }
684 540
685 } // namespace printing 541 } // namespace printing
OLDNEW
« no previous file with comments | « chrome/browser/printing/print_view_manager_base.h ('k') | chrome/browser/printing/print_view_manager_basic.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698