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

Side by Side Diff: chrome/renderer/print_web_view_helper_mac.mm

Issue 276004: Wire up printing on the Mac (Closed)
Patch Set: Created 11 years, 2 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
(Empty)
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/renderer/print_web_view_helper.h"
6
7 #import <AppKit/AppKit.h>
8
9 #include "app/l10n_util.h"
10 #include "base/logging.h"
11 #include "base/process_util.h"
12 #include "base/scoped_cftyperef.h"
13 #include "chrome/common/render_messages.h"
14 #include "chrome/renderer/render_view.h"
15 #include "grit/generated_resources.h"
16 #include "printing/native_metafile.h"
17 #include "webkit/api/public/WebFrame.h"
18 #include "webkit/api/public/WebCanvas.h"
19 #include "webkit/api/public/WebConsoleMessage.h"
20
21 using WebKit::WebFrame;
22 using WebKit::WebCanvas;
23 using WebKit::WebConsoleMessage;
24 using WebKit::WebString;
25
26 // TODO(stuartmorgan): There's a fair amount of code here that is duplicated
27 // from _win that should instead be shared. Once Linux has a real print settings
28 // implementation, it's likely that this whole method can just be moved to the
29 // cross-platform file, and the slight divergences resolved/ifdef'd.
pink (ping after 24hrs) 2009/10/13 21:04:51 file a bug?
pink (ping after 24hrs) 2009/10/13 21:04:51 is it possible to highlight the parts that are mac
stuartmorgan 2009/10/13 22:07:43 Will do.
stuartmorgan 2009/10/13 22:07:43 That seems prone to getting out of date if tweaks
30 void PrintWebViewHelper::Print(WebFrame* frame, bool script_initiated) {
31 const int kMinSecondsToIgnoreJavascriptInitiatedPrint = 2;
32 const int kMaxSecondsToIgnoreJavascriptInitiatedPrint = 2 * 60; // 2 Minutes.
33
34 // If still not finished with earlier print request simply ignore.
35 if (IsPrinting())
36 return;
37
38 // TODO(maruel): Move this out of platform specific code.
39 // Check if there is script repeatedly trying to print and ignore it if too
40 // frequent. We use exponential wait time so for a page that calls print() in
41 // a loop the user will need to cancel the print dialog after 2 seconds, 4
42 // seconds, 8, ... up to the maximum of 2 minutes.
43 // This gives the user time to navigate from the page.
44 if (script_initiated && (user_cancelled_scripted_print_count_ > 0)) {
45 base::TimeDelta diff = base::Time::Now() - last_cancelled_script_print_;
46 int min_wait_seconds = std::min(
47 kMinSecondsToIgnoreJavascriptInitiatedPrint <<
48 (user_cancelled_scripted_print_count_ - 1),
49 kMaxSecondsToIgnoreJavascriptInitiatedPrint);
50 if (diff.InSeconds() < min_wait_seconds) {
51 WebString message(WebString::fromUTF8(
52 "Ignoring too frequent calls to print()."));
53 frame->addMessageToConsole(WebConsoleMessage(
54 WebConsoleMessage::LevelWarning,
55 message));
56 return;
57 }
58 }
59
60 // Retrieve the default print settings to calculate the expected number of
61 // pages.
62 ViewMsg_Print_Params default_settings;
63 bool user_cancelled_print = false;
64
65 IPC::SyncMessage* msg =
66 new ViewHostMsg_GetDefaultPrintSettings(routing_id(), &default_settings);
67 if (Send(msg)) {
68 msg = NULL;
69 if (default_settings.IsEmpty()) {
70 NOTREACHED() << "Couldn't get default print settings";
71 return;
72 }
73
74 // Continue only if the settings are valid.
75 if (default_settings.dpi && default_settings.document_cookie) {
76 int expected_pages_count = 0;
77
78 // Prepare once to calculate the estimated page count. This must be in
79 // a scope for itself (see comments on PrepareFrameAndViewForPrint).
80 {
81 PrepareFrameAndViewForPrint prep_frame_view(default_settings,
82 frame,
83 frame->view());
84 expected_pages_count = prep_frame_view.GetExpectedPageCount();
85 DCHECK(expected_pages_count);
86 }
87
88 // Ask the browser to show UI to retrieve the final print settings.
89 ViewMsg_PrintPages_Params print_settings;
90
91 ViewHostMsg_ScriptedPrint_Params params;
92
93 // The routing id is sent across as it is needed to look up the
94 // corresponding RenderViewHost instance to signal and reset the
95 // pump messages event.
96 params.routing_id = routing_id();
97 // host_window_ may be NULL at this point if the current window is a popup
98 // and the print() command has been issued from the parent. The receiver
99 // of this message has to deal with this.
100 params.host_window_id = render_view_->host_window();
101 params.cookie = default_settings.document_cookie;
102 params.has_selection = frame->hasSelection();
103 params.expected_pages_count = expected_pages_count;
104
105 msg = new ViewHostMsg_ScriptedPrint(params, &print_settings);
106 // TODO(stuartmorgan): This should use SendAndRunNestedMessageLoop, as on
107 // Windows, to prevent getting a "hung renderer" dialog, but if we do we
108 // never actually break out of the nested loop and continue with printing.
109 // Once that's fixed, switch back to SendAndRunNestedMessageLoop.
110 //if (render_view_->SendAndRunNestedMessageLoop(msg)) {
111 if (render_view_->Send(msg)) {
112 msg = NULL;
113
114 // If the settings are invalid, early quit.
115 if (print_settings.params.dpi &&
116 print_settings.params.document_cookie) {
117 if (print_settings.params.selection_only) {
118 CopyAndPrint(print_settings, frame);
119 } else {
120 // TODO: Always copy before printing.
121 PrintPages(print_settings, frame);
122 }
123
124 // Reset cancel counter on first successful print.
125 user_cancelled_scripted_print_count_ = 0;
126 return; // All went well.
127 } else {
128 user_cancelled_print = true;
129 }
130 } else {
131 // Send() failed.
132 NOTREACHED();
133 }
134 } else {
135 // Failed to get default settings.
136 NOTREACHED();
137 }
138 } else {
139 // Send() failed.
140 NOTREACHED();
141 }
142 if (script_initiated && user_cancelled_print) {
143 ++user_cancelled_scripted_print_count_;
144 last_cancelled_script_print_ = base::Time::Now();
145 }
146 // When |user_cancelled_print| is true, we treat it as success so that
147 // DidFinishPrinting() won't show any error alert.
148 // If |user_cancelled_print| is false and we reach here, there must be
149 // something wrong and hence is not success, DidFinishPrinting() should show
150 // an error alert.
151 // In both cases, we have to call DidFinishPrinting() here to release
152 // printing resources, since we don't need them anymore.
153 DidFinishPrinting(user_cancelled_print);
154 }
155
156 void PrintWebViewHelper::PrintPages(const ViewMsg_PrintPages_Params& params,
157 WebKit::WebFrame* frame) {
158 PrepareFrameAndViewForPrint prep_frame_view(params.params,
159 frame,
160 frame->view());
161 int page_count = prep_frame_view.GetExpectedPageCount();
162
163 Send(new ViewHostMsg_DidGetPrintedPagesCount(routing_id(),
164 params.params.document_cookie,
165 page_count));
166 if (!page_count)
167 return;
168
169 const gfx::Size& canvas_size = prep_frame_view.GetPrintCanvasSize();
170 ViewMsg_PrintPage_Params page_params;
171 page_params.params = params.params;
172 if (params.pages.empty()) {
173 for (int i = 0; i < page_count; ++i) {
174 page_params.page_number = i;
175 PrintPage(page_params, canvas_size, frame);
176 }
177 } else {
178 for (size_t i = 0; i < params.pages.size(); ++i) {
179 if (params.pages[i] >= page_count)
180 break;
181 page_params.page_number = params.pages[i];
182 PrintPage(page_params, canvas_size, frame);
183 }
184 }
185 }
186
187 void PrintWebViewHelper::PrintPage(const ViewMsg_PrintPage_Params& params,
188 const gfx::Size& canvas_size,
189 WebFrame* frame) {
190 printing::NativeMetafile metafile;
191 CGContextRef context = metafile.Init();
192
193 float scale_factor = frame->getPrintPageShrink(params.page_number);
194 metafile.StartPage(canvas_size.width(), canvas_size.height(), scale_factor);
195
196 // printPage can create autoreleased references to |canvas|. PDF contexts
197 // don't write all their data until they are destroyed, so we need to make
198 // certain that there are no lingering references.
199 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
200 frame->printPage(params.page_number, context);
201 [pool release];
202
203 metafile.FinishPage();
204 metafile.Close();
205
206 // Get the size of the compiled metafile.
207 ViewHostMsg_DidPrintPage_Params page_params;
208 page_params.data_size = 0;
209 page_params.page_number = params.page_number;
210 page_params.document_cookie = params.params.document_cookie;
211 page_params.actual_shrink = scale_factor;
212 base::SharedMemory shared_buf;
213
214 // Ask the browser to create the shared memory for us.
215 unsigned int buf_size = metafile.GetDataSize();
216 base::SharedMemoryHandle shared_mem_handle;
217 if (Send(new ViewHostMsg_AllocatePDFTransport(routing_id(), buf_size,
218 &shared_mem_handle))) {
219 if (base::SharedMemory::IsHandleValid(shared_mem_handle)) {
220 base::SharedMemory shared_buf(shared_mem_handle, false);
221 if (shared_buf.Map(buf_size)) {
222 metafile.GetData(shared_buf.memory(), buf_size);
223 page_params.data_size = buf_size;
224 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(),
225 &(page_params.metafile_data_handle));
226 } else {
227 NOTREACHED() << "Map failed";
228 }
229 } else {
230 NOTREACHED() << "Browser failed to allocate shared memory";
231 }
232 } else {
233 NOTREACHED() << "Browser allocation request message failed";
234 }
235
236 Send(new ViewHostMsg_DidPrintPage(routing_id(), page_params));
237 }
238
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698