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

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

Issue 8515017: Print Preview: Properly handle window.print(). (Closed)
Patch Set: Created 9 years, 1 month 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) 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/print_view_manager.h" 5 #include "chrome/browser/printing/print_view_manager.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/lazy_instance.h"
8 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
9 #include "base/utf_string_conversions.h" 10 #include "base/utf_string_conversions.h"
10 #include "chrome/browser/browser_process.h" 11 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/printing/print_job.h" 12 #include "chrome/browser/printing/print_job.h"
12 #include "chrome/browser/printing/print_job_manager.h" 13 #include "chrome/browser/printing/print_job_manager.h"
13 #include "chrome/browser/printing/print_preview_tab_controller.h" 14 #include "chrome/browser/printing/print_preview_tab_controller.h"
14 #include "chrome/browser/printing/printer_query.h" 15 #include "chrome/browser/printing/printer_query.h"
15 #include "chrome/browser/printing/print_view_manager_observer.h" 16 #include "chrome/browser/printing/print_view_manager_observer.h"
16 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" 17 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
17 #include "chrome/browser/ui/webui/print_preview_ui.h" 18 #include "chrome/browser/ui/webui/print_preview_ui.h"
(...skipping 25 matching lines...) Expand all
43 44
44 scoped_refptr<printing::PrinterQuery> printer_query; 45 scoped_refptr<printing::PrinterQuery> printer_query;
45 print_job_manager->PopPrinterQuery(cookie, &printer_query); 46 print_job_manager->PopPrinterQuery(cookie, &printer_query);
46 if (printer_query.get()) { 47 if (printer_query.get()) {
47 BrowserThread::PostTask( 48 BrowserThread::PostTask(
48 BrowserThread::IO, FROM_HERE, 49 BrowserThread::IO, FROM_HERE,
49 base::Bind(&printing::PrinterQuery::StopWorker, printer_query.get())); 50 base::Bind(&printing::PrinterQuery::StopWorker, printer_query.get()));
50 } 51 }
51 } 52 }
52 53
54 // Keeps track of pending scripted print preview closures.
55 // No locking, only access on the UI thread.
56 typedef std::map<RenderProcessHost*, base::Closure>
57 ScriptedPrintPreviewClosureMap;
58 static base::LazyInstance<ScriptedPrintPreviewClosureMap>
59 g_scripted_print_preview_closure_map(base::LINKER_INITIALIZED);
60
53 } // namespace 61 } // namespace
54 62
55 namespace printing { 63 namespace printing {
56 64
57 PrintViewManager::PrintViewManager(TabContentsWrapper* tab) 65 PrintViewManager::PrintViewManager(TabContentsWrapper* tab)
58 : TabContentsObserver(tab->tab_contents()), 66 : TabContentsObserver(tab->tab_contents()),
59 tab_(tab), 67 tab_(tab),
60 number_pages_(0), 68 number_pages_(0),
61 printing_succeeded_(false), 69 printing_succeeded_(false),
62 inside_inner_message_loop_(false), 70 inside_inner_message_loop_(false),
63 observer_(NULL), 71 observer_(NULL),
64 cookie_(0) { 72 cookie_(0),
73 print_preview_state_(NOT_PREVIEWING),
74 scripted_print_preview_rph_(NULL) {
65 #if defined(OS_POSIX) && !defined(OS_MACOSX) 75 #if defined(OS_POSIX) && !defined(OS_MACOSX)
66 expecting_first_page_ = true; 76 expecting_first_page_ = true;
67 #endif 77 #endif
68 } 78 }
69 79
70 PrintViewManager::~PrintViewManager() { 80 PrintViewManager::~PrintViewManager() {
81 DCHECK_EQ(NOT_PREVIEWING, print_preview_state_);
vandebo (ex-Chrome) 2011/11/10 23:59:59 What if you close a tab/window while previewing?
Lei Zhang 2011/11/11 11:45:11 RemoveInitiatorTab() will call PrintPreviewDone()
71 ReleasePrinterQuery(cookie_); 82 ReleasePrinterQuery(cookie_);
72 DisconnectFromCurrentPrintJob(); 83 DisconnectFromCurrentPrintJob();
73 } 84 }
74 85
75 bool PrintViewManager::PrintNow() { 86 bool PrintViewManager::PrintNow() {
76 return PrintNowInternal(new PrintMsg_PrintPages(routing_id())); 87 return PrintNowInternal(new PrintMsg_PrintPages(routing_id()));
77 } 88 }
78 89
79 bool PrintViewManager::PrintForSystemDialogNow() { 90 bool PrintViewManager::PrintForSystemDialogNow() {
80 return PrintNowInternal(new PrintMsg_PrintForSystemDialog(routing_id())); 91 return PrintNowInternal(new PrintMsg_PrintForSystemDialog(routing_id()));
(...skipping 15 matching lines...) Expand all
96 PrintPreviewUI* print_preview_ui = 107 PrintPreviewUI* print_preview_ui =
97 static_cast<PrintPreviewUI*>(print_preview_tab->web_ui()); 108 static_cast<PrintPreviewUI*>(print_preview_tab->web_ui());
98 print_preview_ui->OnShowSystemDialog(); 109 print_preview_ui->OnShowSystemDialog();
99 return true; 110 return true;
100 } else { 111 } else {
101 return PrintNow(); 112 return PrintNow();
102 } 113 }
103 } 114 }
104 115
105 bool PrintViewManager::PrintPreviewNow() { 116 bool PrintViewManager::PrintPreviewNow() {
117 if (print_preview_state_ != NOT_PREVIEWING) {
118 NOTREACHED();
119 return false;
120 }
121 print_preview_state_ = USER_INITIATED_PREVIEW;
106 return PrintNowInternal(new PrintMsg_InitiatePrintPreview(routing_id())); 122 return PrintNowInternal(new PrintMsg_InitiatePrintPreview(routing_id()));
107 } 123 }
108 124
125 void PrintViewManager::PrintPreviewDone() {
126 BrowserThread::CurrentlyOn(BrowserThread::UI);
127
128 // Check for spurious call.
vandebo (ex-Chrome) 2011/11/10 23:59:59 This makes me sad. If you can explain why there m
Lei Zhang 2011/11/11 11:45:11 This is a defensive move. I'll check whether it's
Lei Zhang 2011/11/11 22:34:26 Got rid of this and turned it into a DCHECK.
129 if (print_preview_state_ == NOT_PREVIEWING)
130 return;
131
132 if (print_preview_state_ == SCRIPTED_PREVIEW) {
133 ScriptedPrintPreviewClosureMap& map =
134 g_scripted_print_preview_closure_map.Get();
135 ScriptedPrintPreviewClosureMap::iterator it =
136 map.find(scripted_print_preview_rph_);
137 CHECK(it != map.end());
138 it->second.Run();
139 map.erase(scripted_print_preview_rph_);
140 scripted_print_preview_rph_ = NULL;
141 }
142 print_preview_state_ = NOT_PREVIEWING;
143 }
144
109 void PrintViewManager::PreviewPrintingRequestCancelled() { 145 void PrintViewManager::PreviewPrintingRequestCancelled() {
110 if (!tab_contents()) 146 if (!tab_contents())
111 return; 147 return;
112 RenderViewHost* rvh = tab_contents()->render_view_host(); 148 RenderViewHost* rvh = tab_contents()->render_view_host();
113 rvh->Send(new PrintMsg_PreviewPrintingRequestCancelled(rvh->routing_id())); 149 rvh->Send(new PrintMsg_PreviewPrintingRequestCancelled(rvh->routing_id()));
114 } 150 }
115 151
116 void PrintViewManager::set_observer(PrintViewManagerObserver* observer) { 152 void PrintViewManager::set_observer(PrintViewManagerObserver* observer) {
117 DCHECK(!observer || !observer_); 153 DCHECK(!observer || !observer_);
118 observer_ = observer; 154 observer_ = observer;
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 256
221 void PrintViewManager::OnPrintingFailed(int cookie) { 257 void PrintViewManager::OnPrintingFailed(int cookie) {
222 ReleasePrinterQuery(cookie); 258 ReleasePrinterQuery(cookie);
223 259
224 content::NotificationService::current()->Notify( 260 content::NotificationService::current()->Notify(
225 chrome::NOTIFICATION_PRINT_JOB_RELEASED, 261 chrome::NOTIFICATION_PRINT_JOB_RELEASED,
226 content::Source<TabContents>(tab_contents()), 262 content::Source<TabContents>(tab_contents()),
227 content::NotificationService::NoDetails()); 263 content::NotificationService::NoDetails());
228 } 264 }
229 265
266 void PrintViewManager::OnScriptedPrintPreview(IPC::Message* reply_msg) {
267 BrowserThread::CurrentlyOn(BrowserThread::UI);
268 if (print_preview_state_ != NOT_PREVIEWING) {
269 // If a user initiated print dialog is already open, ignore the scripted
270 // print message.
271 DCHECK_EQ(USER_INITIATED_PREVIEW, print_preview_state_);
272 Send(reply_msg);
273 return;
274 }
275
276 print_preview_state_ = SCRIPTED_PREVIEW;
277 scripted_print_preview_rph_ = tab_contents()->render_view_host()->process();
278 base::Closure callback =
279 base::Bind(&PrintViewManager::OnScriptedPrintPreviewReply,
280 base::Unretained(this),
281 reply_msg);
282 ScriptedPrintPreviewClosureMap& map =
283 g_scripted_print_preview_closure_map.Get();
284 CHECK_EQ(0U, map.count(scripted_print_preview_rph_));
285 map[scripted_print_preview_rph_] = callback;
286
287 PrintNowInternal(new PrintMsg_InitiatePrintPreview(routing_id()));
vandebo (ex-Chrome) 2011/11/10 23:59:59 This will trigger GetPrintFrame(), but the caller
Lei Zhang 2011/11/11 11:45:11 I'm surprised this didn't blow up, fixed.
288 }
289
290 void PrintViewManager::OnScriptedPrintPreviewReply(IPC::Message* reply_msg) {
291 BrowserThread::CurrentlyOn(BrowserThread::UI);
292 Send(reply_msg);
293 }
294
230 bool PrintViewManager::OnMessageReceived(const IPC::Message& message) { 295 bool PrintViewManager::OnMessageReceived(const IPC::Message& message) {
231 bool handled = true; 296 bool handled = true;
232 IPC_BEGIN_MESSAGE_MAP(PrintViewManager, message) 297 IPC_BEGIN_MESSAGE_MAP(PrintViewManager, message)
233 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPrintedPagesCount, 298 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPrintedPagesCount,
234 OnDidGetPrintedPagesCount) 299 OnDidGetPrintedPagesCount)
235 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetDocumentCookie, 300 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetDocumentCookie,
236 OnDidGetDocumentCookie) 301 OnDidGetDocumentCookie)
237 IPC_MESSAGE_HANDLER(PrintHostMsg_DidShowPrintDialog, OnDidShowPrintDialog) 302 IPC_MESSAGE_HANDLER(PrintHostMsg_DidShowPrintDialog, OnDidShowPrintDialog)
238 IPC_MESSAGE_HANDLER(PrintHostMsg_DidPrintPage, OnDidPrintPage) 303 IPC_MESSAGE_HANDLER(PrintHostMsg_DidPrintPage, OnDidPrintPage)
239 IPC_MESSAGE_HANDLER(PrintHostMsg_PrintingFailed, OnPrintingFailed) 304 IPC_MESSAGE_HANDLER(PrintHostMsg_PrintingFailed, OnPrintingFailed)
305 IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_ScriptedPrintPreview,
306 OnScriptedPrintPreview)
240 IPC_MESSAGE_UNHANDLED(handled = false) 307 IPC_MESSAGE_UNHANDLED(handled = false)
241 IPC_END_MESSAGE_MAP() 308 IPC_END_MESSAGE_MAP()
242 return handled; 309 return handled;
243 } 310 }
244 311
245 void PrintViewManager::Observe(int type, 312 void PrintViewManager::Observe(int type,
246 const content::NotificationSource& source, 313 const content::NotificationSource& source,
247 const content::NotificationDetails& details) { 314 const content::NotificationDetails& details) {
248 switch (type) { 315 switch (type) {
249 case chrome::NOTIFICATION_PRINT_JOB_EVENT: { 316 case chrome::NOTIFICATION_PRINT_JOB_EVENT: {
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after
511 } 578 }
512 579
513 bool PrintViewManager::PrintNowInternal(IPC::Message* message) { 580 bool PrintViewManager::PrintNowInternal(IPC::Message* message) {
514 // Don't print / print preview interstitials. 581 // Don't print / print preview interstitials.
515 if (tab_contents()->showing_interstitial_page()) 582 if (tab_contents()->showing_interstitial_page())
516 return false; 583 return false;
517 return Send(message); 584 return Send(message);
518 } 585 }
519 586
520 } // namespace printing 587 } // namespace printing
OLDNEW
« no previous file with comments | « chrome/browser/printing/print_view_manager.h ('k') | chrome/browser/ui/webui/print_preview_handler.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698