Chromium Code Reviews| 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 #if defined(USE_SKIA) | |
| 8 #include <algorithm> | |
| 9 #include <cmath> | |
| 10 #endif | |
| 7 #include <string> | 11 #include <string> |
| 8 | 12 |
| 9 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 10 #include "base/logging.h" | 14 #include "base/logging.h" |
| 11 #include "base/metrics/histogram.h" | 15 #include "base/metrics/histogram.h" |
| 12 #include "base/process_util.h" | 16 #include "base/process_util.h" |
| 13 #include "base/utf_string_conversions.h" | 17 #include "base/utf_string_conversions.h" |
| 14 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
| 15 #include "chrome/common/print_messages.h" | 19 #include "chrome/common/print_messages.h" |
| 16 #include "chrome/common/render_messages.h" | 20 #include "chrome/common/render_messages.h" |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" | 33 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" |
| 30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" | 34 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" |
| 31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h" | 35 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h" |
| 32 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 36 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 33 #include "ui/base/l10n/l10n_util.h" | 37 #include "ui/base/l10n/l10n_util.h" |
| 34 | 38 |
| 35 #if defined(OS_POSIX) | 39 #if defined(OS_POSIX) |
| 36 #include "content/common/view_messages.h" | 40 #include "content/common/view_messages.h" |
| 37 #endif | 41 #endif |
| 38 | 42 |
| 43 #if defined(USE_SKIA) | |
| 44 #include "base/string_number_conversions.h" | |
| 45 #include "skia/ext/vector_canvas.h" | |
| 46 #include "skia/ext/vector_platform_device_skia.h" | |
| 47 #include "third_party/skia/include/core/SkTypeface.h" | |
| 48 #endif // USE_SKIA | |
| 49 | |
| 50 using base::Time; | |
| 39 using printing::ConvertPixelsToPoint; | 51 using printing::ConvertPixelsToPoint; |
| 40 using printing::ConvertPixelsToPointDouble; | 52 using printing::ConvertPixelsToPointDouble; |
| 41 using printing::ConvertUnit; | 53 using printing::ConvertUnit; |
| 42 using printing::ConvertUnitDouble; | 54 using printing::ConvertUnitDouble; |
| 55 using printing::GetDPI; | |
| 56 using printing::GetSegmentWidth; | |
| 43 using WebKit::WebConsoleMessage; | 57 using WebKit::WebConsoleMessage; |
| 44 using WebKit::WebDocument; | 58 using WebKit::WebDocument; |
| 45 using WebKit::WebElement; | 59 using WebKit::WebElement; |
| 46 using WebKit::WebFrame; | 60 using WebKit::WebFrame; |
| 47 using WebKit::WebNode; | 61 using WebKit::WebNode; |
| 48 using WebKit::WebSize; | 62 using WebKit::WebSize; |
| 49 using WebKit::WebString; | 63 using WebKit::WebString; |
| 50 using WebKit::WebURLRequest; | 64 using WebKit::WebURLRequest; |
| 51 using WebKit::WebView; | 65 using WebKit::WebView; |
| 52 | 66 |
| 53 namespace { | 67 namespace { |
| 54 | 68 |
| 55 const double kMinDpi = 1.0; | 69 const double kMinDpi = 1.0; |
| 56 | 70 |
| 57 int GetDPI(const PrintMsg_Print_Params* print_params) { | |
| 58 #if defined(OS_MACOSX) | |
| 59 // On the Mac, the printable area is in points, don't do any scaling based | |
| 60 // on dpi. | |
| 61 return printing::kPointsPerInch; | |
| 62 #else | |
| 63 return static_cast<int>(print_params->dpi); | |
| 64 #endif // defined(OS_MACOSX) | |
| 65 } | |
| 66 | |
| 67 bool PrintMsg_Print_Params_IsEmpty(const PrintMsg_Print_Params& params) { | 71 bool PrintMsg_Print_Params_IsEmpty(const PrintMsg_Print_Params& params) { |
| 68 return !params.document_cookie && !params.desired_dpi && !params.max_shrink && | 72 return !params.document_cookie && !params.desired_dpi && !params.max_shrink && |
| 69 !params.min_shrink && !params.dpi && params.printable_size.IsEmpty() && | 73 !params.min_shrink && !params.dpi && params.printable_size.IsEmpty() && |
| 70 !params.selection_only && params.page_size.IsEmpty() && | 74 !params.selection_only && params.page_size.IsEmpty() && |
| 71 !params.margin_top && !params.margin_left && | 75 !params.margin_top && !params.margin_left && |
| 72 !params.supports_alpha_blend; | 76 !params.supports_alpha_blend; |
| 73 } | 77 } |
| 74 | 78 |
| 75 bool PrintMsg_Print_Params_IsEqual( | 79 bool PrintMsg_Print_Params_IsEqual( |
| 76 const PrintMsg_PrintPages_Params& oldParams, | 80 const PrintMsg_PrintPages_Params& oldParams, |
| 77 const PrintMsg_PrintPages_Params& newParams) { | 81 const PrintMsg_PrintPages_Params& newParams) { |
| 78 return oldParams.params.desired_dpi == newParams.params.desired_dpi && | 82 return oldParams.params.desired_dpi == newParams.params.desired_dpi && |
| 79 oldParams.params.max_shrink == newParams.params.max_shrink && | 83 oldParams.params.max_shrink == newParams.params.max_shrink && |
| 80 oldParams.params.min_shrink == newParams.params.min_shrink && | 84 oldParams.params.min_shrink == newParams.params.min_shrink && |
| 81 oldParams.params.dpi == newParams.params.dpi && | 85 oldParams.params.dpi == newParams.params.dpi && |
| 82 oldParams.params.printable_size == newParams.params.printable_size && | 86 oldParams.params.printable_size == newParams.params.printable_size && |
| 83 oldParams.params.selection_only == newParams.params.selection_only && | 87 oldParams.params.selection_only == newParams.params.selection_only && |
| 84 oldParams.params.page_size == newParams.params.page_size && | 88 oldParams.params.page_size == newParams.params.page_size && |
| 85 oldParams.params.margin_top == newParams.params.margin_top && | 89 oldParams.params.margin_top == newParams.params.margin_top && |
| 86 oldParams.params.margin_left == newParams.params.margin_left && | 90 oldParams.params.margin_left == newParams.params.margin_left && |
| 87 oldParams.params.supports_alpha_blend == | 91 oldParams.params.supports_alpha_blend == |
| 88 newParams.params.supports_alpha_blend && | 92 newParams.params.supports_alpha_blend && |
| 89 oldParams.pages.size() == newParams.pages.size() && | 93 oldParams.pages.size() == newParams.pages.size() && |
| 94 oldParams.params.display_header_footer == | |
| 95 newParams.params.display_header_footer && | |
| 90 std::equal(oldParams.pages.begin(), oldParams.pages.end(), | 96 std::equal(oldParams.pages.begin(), oldParams.pages.end(), |
| 91 newParams.pages.begin()); | 97 newParams.pages.begin()); |
| 92 } | 98 } |
| 93 | 99 |
| 94 void CalculatePrintCanvasSize(const PrintMsg_Print_Params& print_params, | 100 void CalculatePrintCanvasSize(const PrintMsg_Print_Params& print_params, |
| 95 gfx::Size* result) { | 101 gfx::Size* result) { |
| 96 int dpi = GetDPI(&print_params); | 102 int dpi = GetDPI(&print_params); |
| 97 result->set_width(ConvertUnit(print_params.printable_size.width(), dpi, | 103 result->set_width(ConvertUnit(print_params.printable_size.width(), dpi, |
| 98 print_params.desired_dpi)); | 104 print_params.desired_dpi)); |
| 99 | 105 |
| 100 result->set_height(ConvertUnit(print_params.printable_size.height(), dpi, | 106 result->set_height(ConvertUnit(print_params.printable_size.height(), dpi, |
| 101 print_params.desired_dpi)); | 107 print_params.desired_dpi)); |
| 102 } | 108 } |
| 103 | 109 |
| 110 #if defined(USE_SKIA) | |
| 111 // Given a text, the positions, and the paint object, this method gets the | |
| 112 // coordinates and prints the text at those coordinates on the canvas. | |
| 113 void PrintHeaderFooterText( | |
| 114 string16 text, | |
| 115 skia::VectorCanvas* canvas, | |
| 116 SkPaint paint, | |
| 117 float webkit_scale_factor, | |
| 118 const PageSizeMargins& page_layout, | |
| 119 printing::HorizontalHeaderFooterPosition horizontal_position, | |
| 120 printing::VerticalHeaderFooterPosition vertical_position, | |
| 121 SkScalar offset_to_baseline) { | |
| 122 size_t text_byte_length = text.length() * sizeof(char16); | |
| 123 // Get the (x, y) coordinate from where printing of the current text should | |
| 124 // start depending on the horizontal alignment (LEFT, RIGHT, CENTER) and | |
| 125 // vertical alignment (TOP, BOTTOM). | |
| 126 SkScalar text_width_in_points = paint.measureText(text.c_str(), | |
| 127 text_byte_length); | |
| 128 SkScalar x = 0; | |
| 129 switch (horizontal_position) { | |
| 130 case printing::LEFT: | |
| 131 x = printing::kSettingHeaderFooterInterstice - page_layout.margin_left; | |
| 132 break; | |
| 133 case printing::RIGHT: | |
| 134 x = page_layout.content_width + page_layout.margin_right - | |
| 135 printing::kSettingHeaderFooterInterstice - text_width_in_points; | |
| 136 break; | |
| 137 case printing::CENTER: { | |
| 138 SkScalar available_width = GetSegmentWidth( | |
| 139 page_layout.margin_left + page_layout.margin_right + | |
| 140 page_layout.content_width); | |
| 141 x = available_width - page_layout.margin_left + | |
| 142 (available_width - text_width_in_points) / 2; | |
| 143 break; | |
| 144 } | |
| 145 default: | |
| 146 NOTREACHED(); | |
| 147 } | |
| 148 | |
| 149 SkScalar y = 0; | |
| 150 switch (vertical_position) { | |
| 151 case printing::TOP: | |
| 152 y = printing::kSettingHeaderFooterInterstice - | |
| 153 page_layout.margin_top - offset_to_baseline; | |
| 154 break; | |
| 155 case printing::BOTTOM: | |
| 156 y = page_layout.margin_bottom + page_layout.content_height - | |
| 157 printing::kSettingHeaderFooterInterstice - offset_to_baseline; | |
| 158 break; | |
| 159 default: | |
| 160 NOTREACHED(); | |
| 161 } | |
| 162 | |
| 163 x = x / webkit_scale_factor; | |
| 164 y = y / webkit_scale_factor; | |
| 165 paint.setTextSize(paint.getTextSize() / webkit_scale_factor); | |
| 166 canvas->drawText(text.c_str(), text_byte_length, x, y, paint); | |
| 167 } | |
| 168 | |
| 169 // Prints the headers onto the |canvas| if there is enough space to print them. | |
| 170 void PrintHeaders(skia::VectorCanvas* canvas, | |
| 171 SkPaint paint, | |
| 172 float webkit_scale_factor, | |
| 173 const PageSizeMargins& page_layout, | |
| 174 const DictionaryValue& header_footer_info) { | |
| 175 string16 date; | |
| 176 string16 title; | |
| 177 if (!header_footer_info.GetString(printing::kSettingHeaderFooterTitle, | |
| 178 &title) || | |
| 179 !header_footer_info.GetString(printing::kSettingHeaderFooterDate, | |
| 180 &date)) { | |
| 181 NOTREACHED(); | |
| 182 } | |
| 183 string16 header_text = date + title; | |
| 184 | |
| 185 SkRect header_bounds; | |
| 186 paint.measureText(header_text.c_str(), header_text.length() * sizeof(char16), | |
| 187 &header_bounds, 0); | |
| 188 SkScalar text_height = | |
| 189 printing::kSettingHeaderFooterInterstice + header_bounds.height(); | |
| 190 if (text_height > page_layout.margin_top) | |
| 191 return; | |
| 192 | |
| 193 PrintHeaderFooterText(date, canvas, paint, webkit_scale_factor, page_layout, | |
| 194 printing::LEFT, printing::TOP, header_bounds.top()); | |
| 195 PrintHeaderFooterText(title, canvas, paint, webkit_scale_factor, page_layout, | |
| 196 printing::CENTER, printing::TOP, header_bounds.top()); | |
| 197 } | |
| 198 | |
| 199 // Prints the footers onto the |canvas| if there is enough space to print them. | |
| 200 void PrintFooters(skia::VectorCanvas* canvas, | |
|
vandebo (ex-Chrome)
2011/08/11 23:32:13
PrintHeaders and PrintFooters don't do much. I th
Aayush Kumar
2011/08/12 08:35:40
Done.
| |
| 201 SkPaint paint, | |
| 202 int page_number, | |
| 203 int total_pages, | |
| 204 float webkit_scale_factor, | |
| 205 const PageSizeMargins& page_layout, | |
| 206 const DictionaryValue& header_footer_info) { | |
| 207 string16 page_of_total_pages = base::IntToString16(page_number) + | |
| 208 UTF8ToUTF16("/") + | |
| 209 base::IntToString16(total_pages); | |
| 210 string16 url; | |
| 211 if (!header_footer_info.GetString(printing::kSettingHeaderFooterURL, | |
| 212 &url)) { | |
| 213 NOTREACHED(); | |
| 214 } | |
| 215 string16 footer_text = page_of_total_pages + url; | |
| 216 | |
| 217 SkRect footer_bounds; | |
| 218 paint.measureText(footer_text.c_str(), footer_text.length() * sizeof(char16), | |
| 219 &footer_bounds, 0); | |
| 220 SkScalar text_height = | |
| 221 printing::kSettingHeaderFooterInterstice + footer_bounds.height(); | |
| 222 if (text_height > page_layout.margin_bottom) | |
| 223 return; | |
| 224 | |
| 225 PrintHeaderFooterText(page_of_total_pages, canvas, paint, | |
| 226 webkit_scale_factor, page_layout, printing::RIGHT, | |
| 227 printing::BOTTOM, footer_bounds.bottom()); | |
| 228 PrintHeaderFooterText(url, canvas, paint, webkit_scale_factor, page_layout, | |
| 229 printing::LEFT, printing::BOTTOM, footer_bounds.bottom() ); | |
| 230 } | |
| 231 #endif // USE_SKIA | |
| 232 | |
| 104 } // namespace | 233 } // namespace |
| 105 | 234 |
| 235 #if defined(USE_SKIA) | |
| 236 // static - Not anonymous so that platform implementations can use it. | |
| 237 void PrintWebViewHelper::PrintHeaderAndFooter( | |
| 238 SkDevice* device, | |
| 239 skia::VectorCanvas* canvas, | |
| 240 int page_number, | |
| 241 int total_pages, | |
| 242 float webkit_scale_factor, | |
| 243 const PageSizeMargins& page_layout, | |
| 244 const DictionaryValue& header_footer_info) { | |
| 245 static_cast<skia::VectorPlatformDeviceSkia*>(device)->setDrawingArea( | |
| 246 SkPDFDevice::kMargin_DrawingArea); | |
| 247 | |
| 248 SkPaint paint; | |
| 249 paint.setColor(SK_ColorBLACK); | |
| 250 paint.setTextEncoding(SkPaint::kUTF16_TextEncoding); | |
| 251 paint.setTextSize(printing::kSettingHeaderFooterFontSize); | |
| 252 paint.setTypeface(SkTypeface::CreateFromName( | |
| 253 printing::kSettingHeaderFooterFontName, SkTypeface::kNormal)); | |
| 254 | |
| 255 PrintHeaders(canvas, paint, webkit_scale_factor, page_layout, | |
| 256 header_footer_info); | |
| 257 PrintFooters(canvas, paint, page_number, total_pages, webkit_scale_factor, | |
| 258 page_layout, header_footer_info); | |
| 259 | |
| 260 static_cast<skia::VectorPlatformDeviceSkia*>(device)->setDrawingArea( | |
| 261 SkPDFDevice::kContent_DrawingArea); | |
| 262 } | |
| 263 #endif // USE_SKIA | |
| 264 | |
| 106 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( | 265 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( |
| 107 const PrintMsg_Print_Params& print_params, | 266 const PrintMsg_Print_Params& print_params, |
| 108 WebFrame* frame, | 267 WebFrame* frame, |
| 109 WebNode* node) | 268 WebNode* node) |
| 110 : frame_(frame), | 269 : frame_(frame), |
| 111 web_view_(frame->view()), | 270 web_view_(frame->view()), |
| 112 dpi_(static_cast<int>(print_params.dpi)), | 271 dpi_(static_cast<int>(print_params.dpi)), |
| 113 expected_pages_count_(0), | 272 expected_pages_count_(0), |
| 114 use_browser_overlays_(true), | 273 use_browser_overlays_(true), |
| 115 finished_(false) { | 274 finished_(false) { |
| (...skipping 642 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 758 return false; | 917 return false; |
| 759 } | 918 } |
| 760 return true; | 919 return true; |
| 761 } | 920 } |
| 762 | 921 |
| 763 bool PrintWebViewHelper::UpdatePrintSettings( | 922 bool PrintWebViewHelper::UpdatePrintSettings( |
| 764 const DictionaryValue& job_settings) { | 923 const DictionaryValue& job_settings) { |
| 765 PrintMsg_PrintPages_Params settings; | 924 PrintMsg_PrintPages_Params settings; |
| 766 | 925 |
| 767 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), | 926 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), |
| 768 print_pages_params_->params.document_cookie, job_settings, &settings)); | 927 print_pages_params_->params.document_cookie, job_settings, &settings)); |
| 769 | 928 |
| 770 if (settings.params.dpi < kMinDpi || !settings.params.document_cookie) | 929 if (settings.params.dpi < kMinDpi || !settings.params.document_cookie) |
| 771 return false; | 930 return false; |
| 772 | 931 |
| 773 if (!UpdatePrintSettingsRequestId(job_settings, &(settings.params))) | 932 if (!UpdatePrintSettingsRequestId(job_settings, &(settings.params))) |
| 774 return false; | 933 return false; |
| 775 | 934 |
| 776 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); | 935 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); |
| 936 | |
| 937 // Getting Header and Footer settings. | |
| 938 bool display_header_footer = false; | |
| 939 if (!job_settings.GetBoolean(printing::kSettingHeaderFooterEnabled, | |
| 940 &display_header_footer)) { | |
| 941 NOTREACHED(); | |
| 942 } | |
| 943 print_pages_params_->params.display_header_footer = display_header_footer; | |
| 944 if (display_header_footer) { | |
| 945 header_footer_info_.reset(new DictionaryValue()); | |
| 946 header_footer_info_->SetString(printing::kSettingHeaderFooterDate, | |
| 947 print_pages_params_->params.date); | |
| 948 header_footer_info_->SetString(printing::kSettingHeaderFooterURL, | |
| 949 print_pages_params_->params.url); | |
| 950 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle, | |
| 951 print_pages_params_->params.title); | |
| 952 } | |
| 953 | |
| 777 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(), | 954 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(), |
| 778 settings.params.document_cookie)); | 955 settings.params.document_cookie)); |
| 779 return true; | 956 return true; |
| 780 } | 957 } |
| 781 | 958 |
| 782 bool PrintWebViewHelper::GetPrintSettingsFromUser(WebKit::WebFrame* frame, | 959 bool PrintWebViewHelper::GetPrintSettingsFromUser(WebKit::WebFrame* frame, |
| 783 int expected_pages_count, | 960 int expected_pages_count, |
| 784 bool use_browser_overlays) { | 961 bool use_browser_overlays) { |
| 785 PrintHostMsg_ScriptedPrint_Params params; | 962 PrintHostMsg_ScriptedPrint_Params params; |
| 786 PrintMsg_PrintPages_Params print_settings; | 963 PrintMsg_PrintPages_Params print_settings; |
| (...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1123 const gfx::Size& | 1300 const gfx::Size& |
| 1124 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const { | 1301 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const { |
| 1125 return prep_frame_view_->GetPrintCanvasSize(); | 1302 return prep_frame_view_->GetPrintCanvasSize(); |
| 1126 } | 1303 } |
| 1127 | 1304 |
| 1128 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { | 1305 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { |
| 1129 prep_frame_view_.reset(); | 1306 prep_frame_view_.reset(); |
| 1130 metafile_.reset(); | 1307 metafile_.reset(); |
| 1131 rendered_pages_.clear(); | 1308 rendered_pages_.clear(); |
| 1132 } | 1309 } |
| OLD | NEW |