| 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/renderer/print_web_view_helper.h" | 5 #include "chrome/renderer/print_web_view_helper.h" |
| 6 | 6 |
| 7 #include "base/file_descriptor_posix.h" | 7 #include "base/file_descriptor_posix.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| 11 #include "chrome/common/print_messages.h" | 11 #include "chrome/common/print_messages.h" |
| 12 #include "content/common/view_messages.h" | 12 #include "content/common/view_messages.h" |
| 13 #include "printing/metafile.h" | 13 #include "printing/metafile.h" |
| 14 #include "printing/metafile_impl.h" | 14 #include "printing/metafile_impl.h" |
| 15 #include "printing/metafile_skia_wrapper.h" | 15 #include "printing/metafile_skia_wrapper.h" |
| 16 #include "skia/ext/vector_canvas.h" | 16 #include "skia/ext/vector_canvas.h" |
| 17 #include "third_party/skia/include/core/SkRefCnt.h" |
| 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 18 #include "ui/gfx/point.h" | 19 #include "ui/gfx/point.h" |
| 19 | 20 |
| 20 #if !defined(OS_CHROMEOS) | 21 #if !defined(OS_CHROMEOS) |
| 21 #include "base/process_util.h" | 22 #include "base/process_util.h" |
| 22 #endif // !defined(OS_CHROMEOS) | 23 #endif // !defined(OS_CHROMEOS) |
| 23 | 24 |
| 24 using WebKit::WebFrame; | 25 using WebKit::WebFrame; |
| 25 using WebKit::WebNode; | 26 using WebKit::WebNode; |
| 26 | 27 |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 #endif // defined(OS_CHROMEOS) | 150 #endif // defined(OS_CHROMEOS) |
| 150 } | 151 } |
| 151 | 152 |
| 152 bool PrintWebViewHelper::RenderPages(const PrintMsg_PrintPages_Params& params, | 153 bool PrintWebViewHelper::RenderPages(const PrintMsg_PrintPages_Params& params, |
| 153 WebKit::WebFrame* frame, | 154 WebKit::WebFrame* frame, |
| 154 WebKit::WebNode* node, | 155 WebKit::WebNode* node, |
| 155 bool send_expected_page_count, | 156 bool send_expected_page_count, |
| 156 int* page_count, | 157 int* page_count, |
| 157 printing::Metafile* metafile) { | 158 printing::Metafile* metafile) { |
| 158 PrintMsg_Print_Params printParams = params.params; | 159 PrintMsg_Print_Params printParams = params.params; |
| 159 scoped_ptr<skia::VectorCanvas> canvas; | |
| 160 | 160 |
| 161 UpdatePrintableSizeInPrintParameters(frame, node, &printParams); | 161 UpdatePrintableSizeInPrintParameters(frame, node, &printParams); |
| 162 | 162 |
| 163 { | 163 PrepareFrameAndViewForPrint prep_frame_view(printParams, frame, node, |
| 164 // Hack - when |prep_frame_view| goes out of scope, PrintEnd() gets called. | 164 frame->view()); |
| 165 // Doing this before closing |metafile| below ensures | 165 *page_count = prep_frame_view.GetExpectedPageCount(); |
| 166 // webkit::ppapi::PluginInstance::PrintEnd() has a valid canvas/metafile to | 166 if (send_expected_page_count) { |
| 167 // save the final output to. See pepper_plugin_instance.cc for the whole | 167 Send(new PrintHostMsg_DidGetPrintedPagesCount(routing_id(), |
| 168 // story. | 168 printParams.document_cookie, |
| 169 PrepareFrameAndViewForPrint prep_frame_view(printParams, | 169 *page_count)); |
| 170 frame, | 170 } |
| 171 node, | 171 if (!*page_count) |
| 172 frame->view()); | 172 return false; |
| 173 *page_count = prep_frame_view.GetExpectedPageCount(); | 173 |
| 174 if (send_expected_page_count) { | 174 PrintMsg_PrintPage_Params page_params; |
| 175 Send(new PrintHostMsg_DidGetPrintedPagesCount(routing_id(), | 175 page_params.params = printParams; |
| 176 printParams.document_cookie, | 176 const gfx::Size& canvas_size = prep_frame_view.GetPrintCanvasSize(); |
| 177 *page_count)); | 177 if (params.pages.empty()) { |
| 178 for (int i = 0; i < *page_count; ++i) { |
| 179 page_params.page_number = i; |
| 180 PrintPageInternal(page_params, canvas_size, frame, metafile); |
| 178 } | 181 } |
| 179 if (!*page_count) | 182 } else { |
| 180 return false; | 183 for (size_t i = 0; i < params.pages.size(); ++i) { |
| 181 | 184 page_params.page_number = params.pages[i]; |
| 182 PrintMsg_PrintPage_Params page_params; | 185 PrintPageInternal(page_params, canvas_size, frame, metafile); |
| 183 page_params.params = printParams; | |
| 184 const gfx::Size& canvas_size = prep_frame_view.GetPrintCanvasSize(); | |
| 185 if (params.pages.empty()) { | |
| 186 for (int i = 0; i < *page_count; ++i) { | |
| 187 page_params.page_number = i; | |
| 188 PrintPageInternal(page_params, canvas_size, frame, metafile, &canvas); | |
| 189 } | |
| 190 } else { | |
| 191 for (size_t i = 0; i < params.pages.size(); ++i) { | |
| 192 page_params.page_number = params.pages[i]; | |
| 193 PrintPageInternal(page_params, canvas_size, frame, metafile, &canvas); | |
| 194 } | |
| 195 } | 186 } |
| 196 } | 187 } |
| 197 | 188 |
| 198 metafile->FinishDocument(); | 189 metafile->FinishDocument(); |
| 199 | 190 |
| 200 return true; | 191 return true; |
| 201 } | 192 } |
| 202 | 193 |
| 203 void PrintWebViewHelper::PrintPageInternal( | 194 void PrintWebViewHelper::PrintPageInternal( |
| 204 const PrintMsg_PrintPage_Params& params, | 195 const PrintMsg_PrintPage_Params& params, |
| 205 const gfx::Size& canvas_size, | 196 const gfx::Size& canvas_size, |
| 206 WebFrame* frame, | 197 WebFrame* frame, |
| 207 printing::Metafile* metafile, | 198 printing::Metafile* metafile) { |
| 208 scoped_ptr<skia::VectorCanvas>* canvas) { | |
| 209 double content_width_in_points; | 199 double content_width_in_points; |
| 210 double content_height_in_points; | 200 double content_height_in_points; |
| 211 double margin_top_in_points; | 201 double margin_top_in_points; |
| 212 double margin_right_in_points; | 202 double margin_right_in_points; |
| 213 double margin_bottom_in_points; | 203 double margin_bottom_in_points; |
| 214 double margin_left_in_points; | 204 double margin_left_in_points; |
| 215 GetPageSizeAndMarginsInPoints(frame, | 205 GetPageSizeAndMarginsInPoints(frame, |
| 216 params.page_number, | 206 params.page_number, |
| 217 params.params, | 207 params.params, |
| 218 &content_width_in_points, | 208 &content_width_in_points, |
| 219 &content_height_in_points, | 209 &content_height_in_points, |
| 220 &margin_top_in_points, | 210 &margin_top_in_points, |
| 221 &margin_right_in_points, | 211 &margin_right_in_points, |
| 222 &margin_bottom_in_points, | 212 &margin_bottom_in_points, |
| 223 &margin_left_in_points); | 213 &margin_left_in_points); |
| 224 | 214 |
| 225 gfx::Size page_size( | 215 gfx::Size page_size( |
| 226 content_width_in_points + margin_right_in_points + | 216 content_width_in_points + margin_right_in_points + |
| 227 margin_left_in_points, | 217 margin_left_in_points, |
| 228 content_height_in_points + margin_top_in_points + | 218 content_height_in_points + margin_top_in_points + |
| 229 margin_bottom_in_points); | 219 margin_bottom_in_points); |
| 230 gfx::Rect content_area(margin_left_in_points, margin_top_in_points, | 220 gfx::Rect content_area(margin_left_in_points, margin_top_in_points, |
| 231 content_width_in_points, content_height_in_points); | 221 content_width_in_points, content_height_in_points); |
| 232 | 222 |
| 233 skia::PlatformDevice* device = metafile->StartPageForVectorCanvas( | 223 skia::PlatformDevice* device = metafile->StartPageForVectorCanvas( |
| 234 page_size, content_area, 1.0f); | 224 page_size, content_area, 1.0f); |
| 235 if (!device) | 225 if (!device) |
| 236 return; | 226 return; |
| 237 | 227 |
| 238 canvas->reset(new skia::VectorCanvas(device)); | 228 // The printPage method take a reference to the canvas we pass down, so it |
| 239 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(canvas->get(), metafile); | 229 // can't be a stack object. |
| 240 frame->printPage(params.page_number, canvas->get()); | 230 SkRefPtr<skia::VectorCanvas> canvas = new skia::VectorCanvas(device); |
| 231 canvas->unref(); // SkRefPtr and new both took a reference. |
| 232 printing::MetafileSkiaWrapper::SetMetafileOnCanvas(canvas.get(), metafile); |
| 233 frame->printPage(params.page_number, canvas.get()); |
| 241 | 234 |
| 242 // TODO(myhuang): We should handle transformation for paper margins. | |
| 243 // TODO(myhuang): We should render the header and the footer. | 235 // TODO(myhuang): We should render the header and the footer. |
| 244 | 236 |
| 245 // Done printing. Close the device context to retrieve the compiled metafile. | 237 // Done printing. Close the device context to retrieve the compiled metafile. |
| 246 if (!metafile->FinishPage()) | 238 if (!metafile->FinishPage()) |
| 247 NOTREACHED() << "metafile failed"; | 239 NOTREACHED() << "metafile failed"; |
| 248 } | 240 } |
| OLD | NEW |