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 #include <algorithm> | |
| 8 #include <cmath> | |
| 7 #include <string> | 9 #include <string> |
| 8 | 10 |
| 9 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/i18n/time_formatting.h" | |
| 10 #include "base/logging.h" | 13 #include "base/logging.h" |
| 11 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
| 12 #include "base/process_util.h" | 15 #include "base/process_util.h" |
| 16 #include "base/string_number_conversions.h" | |
| 17 #include "base/time.h" | |
| 13 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
| 14 #include "chrome/common/chrome_switches.h" | 19 #include "chrome/common/chrome_switches.h" |
| 15 #include "chrome/common/print_messages.h" | 20 #include "chrome/common/print_messages.h" |
| 16 #include "chrome/common/render_messages.h" | 21 #include "chrome/common/render_messages.h" |
| 17 #include "chrome/common/url_constants.h" | 22 #include "chrome/common/url_constants.h" |
| 18 #include "chrome/renderer/prerender/prerender_helper.h" | 23 #include "chrome/renderer/prerender/prerender_helper.h" |
| 19 #include "content/renderer/render_view.h" | 24 #include "content/renderer/render_view.h" |
| 20 #include "grit/generated_resources.h" | 25 #include "grit/generated_resources.h" |
| 21 #include "printing/metafile_impl.h" | 26 #include "printing/metafile_impl.h" |
| 22 #include "printing/print_job_constants.h" | 27 #include "printing/print_job_constants.h" |
| 23 #include "printing/units.h" | 28 #include "printing/units.h" |
| 24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" | 29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" |
| 25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h" | 30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h" |
| 26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" | 32 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" |
| 28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 33 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" | 34 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" |
| 30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" | 35 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" |
| 31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" | 36 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" |
| 32 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h" | 37 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h" |
| 33 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 38 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 34 #include "ui/base/l10n/l10n_util.h" | 39 #include "ui/base/l10n/l10n_util.h" |
| 40 #include "ui/base/text/text_elider.h" | |
|
kmadhusu
2011/07/26 20:27:50
can this header file go inside the #if defined(USE
Aayush Kumar
2011/07/28 17:28:41
This is not skia specific and it will be used by C
| |
| 35 | 41 |
| 36 #if defined(OS_POSIX) | 42 #if defined(OS_POSIX) |
| 37 #include "content/common/view_messages.h" | 43 #include "content/common/view_messages.h" |
| 38 #endif | 44 #endif |
| 39 | 45 |
| 46 #if defined(USE_SKIA) | |
| 47 #include "skia/ext/vector_canvas.h" | |
| 48 #include "skia/ext/vector_platform_device_skia.h" | |
| 49 #include "third_party/skia/include/core/SkTypeface.h" | |
| 50 #endif | |
| 51 | |
| 52 using base::Time; | |
| 40 using printing::ConvertPixelsToPoint; | 53 using printing::ConvertPixelsToPoint; |
| 41 using printing::ConvertPixelsToPointDouble; | 54 using printing::ConvertPixelsToPointDouble; |
| 55 using printing::ConvertPointsToPixelDouble; | |
| 42 using printing::ConvertUnit; | 56 using printing::ConvertUnit; |
| 43 using printing::ConvertUnitDouble; | 57 using printing::ConvertUnitDouble; |
| 44 using WebKit::WebConsoleMessage; | 58 using WebKit::WebConsoleMessage; |
| 45 using WebKit::WebDocument; | 59 using WebKit::WebDocument; |
| 46 using WebKit::WebElement; | 60 using WebKit::WebElement; |
| 47 using WebKit::WebFrame; | 61 using WebKit::WebFrame; |
| 48 using WebKit::WebNode; | 62 using WebKit::WebNode; |
| 49 using WebKit::WebSize; | 63 using WebKit::WebSize; |
| 50 using WebKit::WebString; | 64 using WebKit::WebString; |
| 51 using WebKit::WebURLRequest; | 65 using WebKit::WebURLRequest; |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 81 oldParams.params.min_shrink == newParams.params.min_shrink && | 95 oldParams.params.min_shrink == newParams.params.min_shrink && |
| 82 oldParams.params.dpi == newParams.params.dpi && | 96 oldParams.params.dpi == newParams.params.dpi && |
| 83 oldParams.params.printable_size == newParams.params.printable_size && | 97 oldParams.params.printable_size == newParams.params.printable_size && |
| 84 oldParams.params.selection_only == newParams.params.selection_only && | 98 oldParams.params.selection_only == newParams.params.selection_only && |
| 85 oldParams.params.page_size == newParams.params.page_size && | 99 oldParams.params.page_size == newParams.params.page_size && |
| 86 oldParams.params.margin_top == newParams.params.margin_top && | 100 oldParams.params.margin_top == newParams.params.margin_top && |
| 87 oldParams.params.margin_left == newParams.params.margin_left && | 101 oldParams.params.margin_left == newParams.params.margin_left && |
| 88 oldParams.params.supports_alpha_blend == | 102 oldParams.params.supports_alpha_blend == |
| 89 newParams.params.supports_alpha_blend && | 103 newParams.params.supports_alpha_blend && |
| 90 oldParams.pages.size() == newParams.pages.size() && | 104 oldParams.pages.size() == newParams.pages.size() && |
| 105 oldParams.params.display_header_footer == | |
| 106 newParams.params.display_header_footer && | |
| 91 std::equal(oldParams.pages.begin(), oldParams.pages.end(), | 107 std::equal(oldParams.pages.begin(), oldParams.pages.end(), |
| 92 newParams.pages.begin()); | 108 newParams.pages.begin()); |
| 93 } | 109 } |
| 94 | 110 |
| 111 #if defined(USE_SKIA) | |
| 112 // Splits the horizontal width equally into segments with an interstice | |
| 113 // between each segment. Returns the width of this segment. | |
| 114 SkScalar GetSegmentWidth(const PageSizeMargins& page_layout) { | |
| 115 // Interstice is left at both ends of the page as well as between | |
| 116 // each region, so 1 is added. | |
| 117 return (page_layout.margin_left + | |
| 118 page_layout.content_width + | |
| 119 page_layout.margin_right - | |
| 120 (printing::kSettingHeaderFooterHorizontalRegions + 1) * | |
| 121 printing::kSettingHeaderFooterInterstice) / | |
| 122 printing::kSettingHeaderFooterHorizontalRegions; | |
| 123 } | |
| 124 | |
| 125 // Given a text, the positions, and the paint object, this method gets the | |
| 126 // coordinates and prints the text at those coordinates on the canvas. | |
| 127 void PrintHeaderFooterText( | |
| 128 string16 text, | |
| 129 SkPaint paint, | |
| 130 skia::VectorCanvas& canvas, | |
| 131 printing::HorizontalHeaderFooterPosition horizontal_position, | |
| 132 printing::VerticalHeaderFooterPosition vertical_position, | |
| 133 float webkit_scale_factor, | |
| 134 const PageSizeMargins& page_layout, | |
| 135 SkScalar max_text_width, | |
| 136 gfx::Font font, | |
| 137 SkScalar height_to_baseline) { | |
| 138 text = ui::ElideText(text, font, ConvertPointsToPixelDouble(max_text_width), | |
| 139 false); | |
| 140 | |
| 141 SkPoint text_location; | |
| 142 size_t text_byte_length = text.length() * sizeof(char16); | |
| 143 // Get the (x, y) coordinate from where printing of the current text should | |
| 144 // start depending on the horizontal alignment (LEFT, RIGHT, CENTER) and | |
| 145 // vertical alignment (TOP, BOTTOM). | |
| 146 SkScalar text_width_in_points = paint.measureText(text.c_str(), | |
| 147 text_byte_length); | |
| 148 SkScalar x = 0; | |
| 149 switch (horizontal_position) { | |
| 150 case printing::LEFT: { | |
| 151 x = -page_layout.margin_left + | |
|
kmadhusu
2011/07/26 20:27:50
Can you rewrite this statement as follows:
x = pr
Aayush Kumar
2011/07/28 17:28:41
Done.
| |
| 152 printing::kSettingHeaderFooterInterstice; | |
| 153 break; | |
| 154 } | |
| 155 case printing::RIGHT: { | |
| 156 x = (page_layout.content_width + page_layout.margin_right - | |
|
kmadhusu
2011/07/26 20:27:50
nit: This statement does not require paranthesis s
Aayush Kumar
2011/07/28 17:28:41
Done.
| |
| 157 (printing::kSettingHeaderFooterInterstice + text_width_in_points)); | |
| 158 break; | |
| 159 } | |
| 160 case printing::CENTER: { | |
| 161 SkScalar available_width = GetSegmentWidth(page_layout); | |
| 162 x = available_width - page_layout.margin_left + | |
| 163 (available_width - text_width_in_points) / 2; | |
| 164 break; | |
| 165 } | |
| 166 default: { | |
| 167 NOTREACHED(); | |
| 168 } | |
| 169 } | |
| 170 | |
| 171 SkScalar y = 0; | |
| 172 switch (vertical_position) { | |
| 173 case printing::TOP: { | |
| 174 y = -page_layout.margin_top - height_to_baseline + | |
| 175 printing::kSettingHeaderFooterInterstice; | |
|
kmadhusu
2011/07/26 20:27:50
How about:
y = printing::kSettingHeaderFooterInte
Aayush Kumar
2011/07/28 17:28:41
Done.
| |
| 176 break; | |
| 177 } | |
| 178 case printing::BOTTOM: { | |
| 179 y = page_layout.margin_bottom + page_layout.content_height - | |
| 180 printing::kSettingHeaderFooterInterstice - height_to_baseline; | |
| 181 break; | |
| 182 } | |
| 183 default: { | |
| 184 NOTREACHED(); | |
| 185 } | |
| 186 } | |
| 187 | |
| 188 text_location.set(x / webkit_scale_factor, y / webkit_scale_factor); | |
| 189 paint.setTextSize(paint.getTextSize() / webkit_scale_factor); | |
| 190 canvas.drawText(text.c_str(), text_byte_length, text_location.x(), | |
| 191 text_location.y(), paint); | |
|
kmadhusu
2011/07/26 20:27:50
style nit: Fix the indentation
Aayush Kumar
2011/07/28 17:28:41
Done.
| |
| 192 } | |
| 193 #endif // USE_SKIA | |
| 95 } // namespace | 194 } // namespace |
| 96 | 195 |
| 196 #if defined(USE_SKIA) | |
| 197 void PrintHeaderAndFooter(SkDevice* device, | |
| 198 skia::VectorCanvas& canvas, | |
| 199 int page_number, | |
| 200 int total_pages, | |
| 201 float webkit_scale_factor, | |
| 202 const PageSizeMargins& page_layout, | |
| 203 const DictionaryValue* header_footer_info) { | |
|
kmadhusu
2011/07/26 20:27:50
Can you pass header_footer_info as a "const Dictio
Aayush Kumar
2011/07/28 17:28:41
Done.
| |
| 204 // Set the drawing area to draw in the margins. | |
| 205 static_cast<skia::VectorPlatformDeviceSkia*>(device)->setDrawingArea( | |
| 206 SkPDFDevice::kMargin_DrawingArea); | |
| 207 | |
| 208 // Setting up styles for the headers and footers text. | |
| 209 SkPaint paint; | |
| 210 paint.setColor(SK_ColorBLACK); | |
| 211 paint.setTextEncoding(SkPaint::kUTF16_TextEncoding); | |
| 212 paint.setTextSize(printing::kSettingHeaderFooterFontSize); | |
| 213 gfx::Font font(UTF8ToUTF16(printing::kSettingHeaderFooterFontName), | |
| 214 ceil(ConvertPointsToPixelDouble( | |
| 215 printing::kSettingHeaderFooterFontSize))); | |
| 216 paint.setTypeface(SkTypeface::CreateFromName( | |
| 217 UTF16ToUTF8(font.GetFontName()).c_str(), SkTypeface::kNormal)); | |
| 218 | |
| 219 // Ensure we have enough space to print above and below the page to print | |
| 220 // headers. | |
| 221 string16 date = base::TimeFormatShortDateNumeric(Time::Now()); | |
| 222 string16 title; | |
| 223 if (!header_footer_info->GetString(printing::kSettingHeaderFooterTitle, | |
| 224 &title)) { | |
| 225 NOTREACHED(); | |
| 226 } | |
| 227 | |
| 228 SkRect header_bounds; | |
| 229 string16 header_text = date + title; | |
| 230 paint.measureText(header_text.c_str(), header_text.length() * sizeof(char16), | |
| 231 &header_bounds, 0); | |
| 232 | |
| 233 SkScalar text_height = | |
| 234 printing::kSettingHeaderFooterInterstice + header_bounds.height(); | |
| 235 | |
| 236 if (text_height > page_layout.margin_top) | |
| 237 return; | |
| 238 | |
| 239 // Ensure we have enough space to print above and below the page to print | |
| 240 // footers. | |
| 241 string16 page_of_total_pages = base::IntToString16(page_number) + | |
| 242 UTF8ToUTF16("/") + | |
| 243 base::IntToString16(total_pages); | |
| 244 std::string url; | |
| 245 if (!header_footer_info->GetString(printing::kSettingHeaderFooterURL, | |
| 246 &url)) { | |
| 247 NOTREACHED(); | |
| 248 } | |
| 249 | |
| 250 SkRect footer_bounds; | |
| 251 string16 footer_text = page_of_total_pages + UTF8ToUTF16(url); | |
| 252 paint.measureText(footer_text.c_str(), footer_text.length() * sizeof(char16), | |
| 253 &footer_bounds, 0); | |
| 254 | |
| 255 text_height = | |
| 256 printing::kSettingHeaderFooterInterstice + footer_bounds.height(); | |
| 257 | |
| 258 if (text_height > page_layout.margin_bottom) | |
| 259 return; | |
| 260 | |
| 261 SkScalar segment_width = GetSegmentWidth(page_layout); | |
| 262 // Print the Date. | |
| 263 PrintHeaderFooterText(date, paint, canvas, printing::LEFT, printing::TOP, | |
| 264 webkit_scale_factor, page_layout, segment_width, | |
| 265 font, header_bounds.top()); | |
| 266 | |
| 267 // Print the title. | |
| 268 SkScalar date_width = paint.measureText(date.c_str(), | |
| 269 date.length() * sizeof(char16)); | |
| 270 // Calculate the available title width. If the date string is not long | |
| 271 // enough, then, we increase the available space we have for the title. | |
| 272 // Assumes there is no header text to RIGHT of title. | |
| 273 SkScalar max_title_width = std::min(2 * segment_width, | |
| 274 2 * (segment_width - date_width) + | |
| 275 segment_width); | |
| 276 PrintHeaderFooterText(title, paint, canvas, printing::CENTER, | |
| 277 printing::TOP, webkit_scale_factor, page_layout, | |
| 278 max_title_width, font, header_bounds.top()); | |
| 279 | |
| 280 // Print the page numbers at the bottom right corner of page. | |
| 281 PrintHeaderFooterText(page_of_total_pages, paint, canvas, printing::RIGHT, | |
| 282 printing::BOTTOM, webkit_scale_factor, | |
| 283 page_layout, segment_width, font, | |
| 284 footer_bounds.bottom()); | |
| 285 | |
| 286 // Print the URL. | |
| 287 GURL gurl(url); | |
| 288 SkScalar page_width = paint.measureText( | |
| 289 page_of_total_pages.c_str(), | |
| 290 page_of_total_pages.length() * sizeof(char16)); | |
| 291 | |
| 292 // Calculate the available URL width. We increase the available URL width | |
| 293 // if the |page_of_total_pages| string isn't long enough. | |
| 294 // Assumes no footer text being printed in the CENTER. | |
| 295 SkScalar max_url_width = | |
| 296 (page_layout.content_width + page_layout.margin_left + | |
| 297 page_layout.margin_right) * 3 / 4; | |
| 298 max_url_width = std::min(max_url_width, | |
| 299 2 * (segment_width - page_width) + segment_width); | |
| 300 string16 url_elided = ui::ElideUrl(gurl, font, | |
| 301 ConvertPointsToPixelDouble(max_url_width), | |
| 302 std::string()); | |
| 303 PrintHeaderFooterText(url_elided, paint, canvas, printing::LEFT, | |
| 304 printing::BOTTOM, webkit_scale_factor, | |
| 305 page_layout, max_url_width, font, | |
| 306 footer_bounds.bottom()); | |
| 307 | |
| 308 // Restore the drawing area to draw in the content area. | |
| 309 static_cast<skia::VectorPlatformDeviceSkia*>(device)->setDrawingArea( | |
| 310 SkPDFDevice::kContent_DrawingArea); | |
| 311 } | |
| 312 #endif // USE_SKIA | |
| 313 | |
| 97 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( | 314 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( |
| 98 const PrintMsg_Print_Params& print_params, | 315 const PrintMsg_Print_Params& print_params, |
| 99 WebFrame* frame, | 316 WebFrame* frame, |
| 100 WebNode* node) | 317 WebNode* node) |
| 101 : frame_(frame), | 318 : frame_(frame), |
| 102 web_view_(frame->view()), | 319 web_view_(frame->view()), |
| 103 expected_pages_count_(0), | 320 expected_pages_count_(0), |
| 104 use_browser_overlays_(true), | 321 use_browser_overlays_(true), |
| 105 finished_(false) { | 322 finished_(false) { |
| 106 int dpi = GetDPI(&print_params); | 323 int dpi = GetDPI(&print_params); |
| (...skipping 612 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 719 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), | 936 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), |
| 720 print_pages_params_->params.document_cookie, job_settings, &settings)); | 937 print_pages_params_->params.document_cookie, job_settings, &settings)); |
| 721 | 938 |
| 722 if (settings.params.dpi < kMinDpi || !settings.params.document_cookie) | 939 if (settings.params.dpi < kMinDpi || !settings.params.document_cookie) |
| 723 return false; | 940 return false; |
| 724 | 941 |
| 725 if (!UpdatePrintSettingsRequestId(job_settings, &(settings.params))) | 942 if (!UpdatePrintSettingsRequestId(job_settings, &(settings.params))) |
| 726 return false; | 943 return false; |
| 727 | 944 |
| 728 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); | 945 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); |
| 946 // Getting Header and Footer settings. | |
| 947 bool display_header_footer; | |
| 948 if (!job_settings.GetBoolean(printing::kSettingHeaderFooter, | |
| 949 &display_header_footer)) { | |
| 950 NOTREACHED(); | |
| 951 } | |
| 952 print_pages_params_->params.display_header_footer = display_header_footer; | |
| 953 std::string url; | |
| 954 if (!job_settings.GetString(printing::kSettingHeaderFooterURL, &url)) | |
|
kmadhusu
2011/07/26 20:27:50
Can you merge the if conditions @line 948, 954 and
Aayush Kumar
2011/07/28 17:28:41
Done.
| |
| 955 NOTREACHED(); | |
| 956 header_footer_info_.reset(new DictionaryValue()); | |
| 957 header_footer_info_->SetString(printing::kSettingHeaderFooterURL, url); | |
| 958 string16 title; | |
| 959 if (!job_settings.GetString(printing::kSettingHeaderFooterTitle, &title)) | |
| 960 NOTREACHED(); | |
| 961 header_footer_info_->SetString(printing::kSettingHeaderFooterTitle, title); | |
| 962 | |
| 729 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(), | 963 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(), |
| 730 settings.params.document_cookie)); | 964 settings.params.document_cookie)); |
| 731 return true; | 965 return true; |
| 732 } | 966 } |
| 733 | 967 |
| 734 bool PrintWebViewHelper::GetPrintSettingsFromUser(WebKit::WebFrame* frame, | 968 bool PrintWebViewHelper::GetPrintSettingsFromUser(WebKit::WebFrame* frame, |
| 735 int expected_pages_count, | 969 int expected_pages_count, |
| 736 bool use_browser_overlays) { | 970 bool use_browser_overlays) { |
| 737 PrintHostMsg_ScriptedPrint_Params params; | 971 PrintHostMsg_ScriptedPrint_Params params; |
| 738 PrintMsg_PrintPages_Params print_settings; | 972 PrintMsg_PrintPages_Params print_settings; |
| (...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1031 const gfx::Size& | 1265 const gfx::Size& |
| 1032 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const { | 1266 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const { |
| 1033 return prep_frame_view_->GetPrintCanvasSize(); | 1267 return prep_frame_view_->GetPrintCanvasSize(); |
| 1034 } | 1268 } |
| 1035 | 1269 |
| 1036 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { | 1270 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { |
| 1037 prep_frame_view_.reset(); | 1271 prep_frame_view_.reset(); |
| 1038 metafile_.reset(); | 1272 metafile_.reset(); |
| 1039 rendered_pages_.clear(); | 1273 rendered_pages_.clear(); |
| 1040 } | 1274 } |
| OLD | NEW |