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/print_preview_message_handler.h" | 5 #include "chrome/browser/printing/print_preview_message_handler.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 printing::PrintPreviewTabController::GetInstance(); | 56 printing::PrintPreviewTabController::GetInstance(); |
57 if (!tab_controller) | 57 if (!tab_controller) |
58 return NULL; | 58 return NULL; |
59 return tab_controller->GetPrintPreviewForTab(tab_contents()); | 59 return tab_controller->GetPrintPreviewForTab(tab_contents()); |
60 } | 60 } |
61 | 61 |
62 void PrintPreviewMessageHandler::OnRequestPrintPreview() { | 62 void PrintPreviewMessageHandler::OnRequestPrintPreview() { |
63 PrintPreviewTabController::PrintPreview(tab_contents()); | 63 PrintPreviewTabController::PrintPreview(tab_contents()); |
64 } | 64 } |
65 | 65 |
66 void PrintPreviewMessageHandler::OnDidGetPreviewPageCount(int page_count) { | 66 void PrintPreviewMessageHandler::OnDidGetPreviewPageCount(int document_cookie, |
| 67 int page_count) { |
67 if (page_count <= 0) | 68 if (page_count <= 0) |
68 return; | 69 return; |
69 TabContents* print_preview_tab = GetPrintPreviewTab(); | 70 TabContents* print_preview_tab = GetPrintPreviewTab(); |
70 if (!print_preview_tab) | 71 if (!print_preview_tab) |
71 return; | 72 return; |
72 | 73 |
73 PrintPreviewUI* print_preview_ui = | 74 PrintPreviewUI* print_preview_ui = |
74 static_cast<PrintPreviewUI*>(print_preview_tab->web_ui()); | 75 static_cast<PrintPreviewUI*>(print_preview_tab->web_ui()); |
75 print_preview_ui->OnDidGetPreviewPageCount(page_count); | 76 print_preview_ui->OnDidGetPreviewPageCount(document_cookie, page_count); |
76 } | 77 } |
77 | 78 |
78 void PrintPreviewMessageHandler::OnDidPreviewPage(int page_number, | 79 void PrintPreviewMessageHandler::OnDidPreviewPage(int page_number) { |
79 bool* cancel) { | 80 RenderViewHost* rvh = tab_contents()->render_view_host(); |
80 TabContents* print_preview_tab = GetPrintPreviewTab(); | 81 TabContents* print_preview_tab = GetPrintPreviewTab(); |
81 if (!print_preview_tab) { | 82 if (!(print_preview_tab && print_preview_tab->web_ui())) { |
82 // Can't find print preview tab means we should cancel. | 83 // Can't find print preview tab means we should abort. |
83 *cancel = true; | 84 rvh->Send(new PrintMsg_AbortPreview(rvh->routing_id())); |
84 return; | 85 return; |
85 } | 86 } |
86 | 87 |
87 PrintPreviewUI* print_preview_ui = | 88 PrintPreviewUI* print_preview_ui = |
88 static_cast<PrintPreviewUI*>(print_preview_tab->web_ui()); | 89 static_cast<PrintPreviewUI*>(print_preview_tab->web_ui()); |
89 bool has_pending = print_preview_ui->HasPendingRequests(); | 90 bool has_pending = print_preview_ui->HasPendingRequests(); |
90 if (!has_pending && page_number >= 0) | 91 if (has_pending) { |
| 92 // Cancel. Next print preview request will cancel the current one. |
| 93 // Just do the required maintainance work here. |
| 94 StopWorker(print_preview_ui->document_cookie()); |
| 95 print_preview_ui->OnPrintPreviewCancelled(); |
| 96 return; |
| 97 } |
| 98 |
| 99 // Continue |
| 100 if (page_number >= 0) |
91 print_preview_ui->OnDidPreviewPage(page_number); | 101 print_preview_ui->OnDidPreviewPage(page_number); |
92 *cancel = has_pending; | 102 rvh->Send(new PrintMsg_ContinuePreview(rvh->routing_id())); |
93 } | 103 } |
94 | 104 |
95 void PrintPreviewMessageHandler::OnPagesReadyForPreview( | 105 void PrintPreviewMessageHandler::OnPagesReadyForPreview( |
96 const PrintHostMsg_DidPreviewDocument_Params& params) { | 106 const PrintHostMsg_DidPreviewDocument_Params& params) { |
97 // Always need to stop the worker and send PrintMsg_PrintingDone. | |
98 StopWorker(params.document_cookie); | 107 StopWorker(params.document_cookie); |
99 | 108 |
100 RenderViewHost* rvh = tab_contents()->render_view_host(); | |
101 rvh->Send(new PrintMsg_PrintingDone(rvh->routing_id(), true)); | |
102 | |
103 // Get the print preview tab. | 109 // Get the print preview tab. |
104 TabContents* print_preview_tab = GetPrintPreviewTab(); | 110 TabContents* print_preview_tab = GetPrintPreviewTab(); |
105 // User might have closed it already. | 111 // User might have closed it already. |
106 if (!print_preview_tab) | 112 if (!print_preview_tab) |
107 return; | 113 return; |
108 | 114 |
109 PrintPreviewUI* print_preview_ui = | 115 PrintPreviewUI* print_preview_ui = |
110 static_cast<PrintPreviewUI*>(print_preview_tab->web_ui()); | 116 static_cast<PrintPreviewUI*>(print_preview_tab->web_ui()); |
111 | 117 |
112 TabContentsWrapper* wrapper = | 118 TabContentsWrapper* wrapper = |
113 TabContentsWrapper::GetCurrentWrapperForContents(print_preview_tab); | 119 TabContentsWrapper::GetCurrentWrapperForContents(print_preview_tab); |
114 | 120 |
115 if (params.reuse_existing_data) { | 121 if (params.reuse_existing_data) { |
116 // Need to match normal rendering where we are expected to send this. | 122 // Need to match normal rendering where we are expected to send this. |
117 print_preview_ui->OnDidGetPreviewPageCount(params.expected_pages_count); | 123 print_preview_ui->OnDidGetPreviewPageCount(params.document_cookie, |
| 124 params.expected_pages_count); |
118 | 125 |
119 print_preview_ui->OnPreviewDataIsAvailable( | 126 print_preview_ui->OnPreviewDataIsAvailable( |
120 params.expected_pages_count, | 127 params.expected_pages_count, |
121 wrapper->print_view_manager()->RenderSourceName(), | 128 wrapper->print_view_manager()->RenderSourceName(), |
122 params.modifiable, | 129 params.modifiable, |
123 params.preview_request_id); | 130 params.preview_request_id); |
124 return; | 131 return; |
125 } | 132 } |
126 | 133 |
127 base::SharedMemory* shared_buf = | 134 base::SharedMemory* shared_buf = |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 HasTabContents(wrapper)) { | 175 HasTabContents(wrapper)) { |
169 // Preview tab was hidden to serve the print request. | 176 // Preview tab was hidden to serve the print request. |
170 delete wrapper; | 177 delete wrapper; |
171 } else { | 178 } else { |
172 PrintPreviewUI* print_preview_ui = | 179 PrintPreviewUI* print_preview_ui = |
173 static_cast<PrintPreviewUI*>(print_preview_tab->web_ui()); | 180 static_cast<PrintPreviewUI*>(print_preview_tab->web_ui()); |
174 print_preview_ui->OnPrintPreviewFailed(); | 181 print_preview_ui->OnPrintPreviewFailed(); |
175 } | 182 } |
176 } | 183 } |
177 | 184 |
178 void PrintPreviewMessageHandler::OnPrintPreviewCancelled(int document_cookie) { | |
179 // Always need to stop the worker. | |
180 StopWorker(document_cookie); | |
181 | |
182 TabContents* print_preview_tab = GetPrintPreviewTab(); | |
183 if (!print_preview_tab) | |
184 return; | |
185 | |
186 PrintPreviewUI* print_preview_ui = | |
187 static_cast<PrintPreviewUI*>(print_preview_tab->web_ui()); | |
188 print_preview_ui->OnPrintPreviewCancelled(); | |
189 } | |
190 | |
191 bool PrintPreviewMessageHandler::OnMessageReceived( | 185 bool PrintPreviewMessageHandler::OnMessageReceived( |
192 const IPC::Message& message) { | 186 const IPC::Message& message) { |
193 bool handled = true; | 187 bool handled = true; |
194 IPC_BEGIN_MESSAGE_MAP(PrintPreviewMessageHandler, message) | 188 IPC_BEGIN_MESSAGE_MAP(PrintPreviewMessageHandler, message) |
195 IPC_MESSAGE_HANDLER(PrintHostMsg_RequestPrintPreview, | 189 IPC_MESSAGE_HANDLER(PrintHostMsg_RequestPrintPreview, |
196 OnRequestPrintPreview) | 190 OnRequestPrintPreview) |
197 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPreviewPageCount, | 191 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPreviewPageCount, |
198 OnDidGetPreviewPageCount) | 192 OnDidGetPreviewPageCount) |
199 IPC_MESSAGE_HANDLER(PrintHostMsg_DidPreviewPage, | 193 IPC_MESSAGE_HANDLER(PrintHostMsg_DidPreviewPage, |
200 OnDidPreviewPage) | 194 OnDidPreviewPage) |
201 IPC_MESSAGE_HANDLER(PrintHostMsg_PagesReadyForPreview, | 195 IPC_MESSAGE_HANDLER(PrintHostMsg_PagesReadyForPreview, |
202 OnPagesReadyForPreview) | 196 OnPagesReadyForPreview) |
203 IPC_MESSAGE_HANDLER(PrintHostMsg_PrintPreviewFailed, | 197 IPC_MESSAGE_HANDLER(PrintHostMsg_PrintPreviewFailed, |
204 OnPrintPreviewFailed) | 198 OnPrintPreviewFailed) |
205 IPC_MESSAGE_HANDLER(PrintHostMsg_PrintPreviewCancelled, | |
206 OnPrintPreviewCancelled) | |
207 IPC_MESSAGE_UNHANDLED(handled = false) | 199 IPC_MESSAGE_UNHANDLED(handled = false) |
208 IPC_END_MESSAGE_MAP() | 200 IPC_END_MESSAGE_MAP() |
209 return handled; | 201 return handled; |
210 } | 202 } |
211 | 203 |
212 void PrintPreviewMessageHandler::DidStartLoading() { | 204 void PrintPreviewMessageHandler::DidStartLoading() { |
213 if (tab_contents()->delegate() && | 205 if (tab_contents()->delegate() && |
214 printing::PrintPreviewTabController::IsPrintPreviewTab(tab_contents())) { | 206 printing::PrintPreviewTabController::IsPrintPreviewTab(tab_contents())) { |
215 tab_contents()->SetContentRestrictions(CONTENT_RESTRICTION_PRINT); | 207 tab_contents()->SetContentRestrictions(CONTENT_RESTRICTION_PRINT); |
216 } | 208 } |
217 } | 209 } |
218 | 210 |
219 } // namespace printing | 211 } // namespace printing |
OLD | NEW |