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> | |
|
dpapad
2011/07/19 20:34:01
Space between words.
Aayush Kumar
2011/07/21 21:58:53
Done.
| |
| 7 #include <string> | 8 #include <string> |
| 8 | 9 |
| 9 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/i18n/time_formatting.h" | |
| 10 #include "base/logging.h" | 12 #include "base/logging.h" |
| 11 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
| 12 #include "base/process_util.h" | 14 #include "base/process_util.h" |
| 15 #include "base/string_number_conversions.h" | |
| 16 #include "base/time.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" |
| 17 #include "chrome/common/url_constants.h" | 21 #include "chrome/common/url_constants.h" |
| 18 #include "chrome/renderer/prerender/prerender_helper.h" | 22 #include "chrome/renderer/prerender/prerender_helper.h" |
| 19 #include "content/renderer/render_view.h" | 23 #include "content/renderer/render_view.h" |
| 20 #include "grit/generated_resources.h" | 24 #include "grit/generated_resources.h" |
| 21 #include "printing/metafile_impl.h" | 25 #include "printing/metafile_impl.h" |
| 22 #include "printing/print_job_constants.h" | 26 #include "printing/print_job_constants.h" |
| 23 #include "printing/units.h" | 27 #include "printing/units.h" |
| 28 #include "skia/ext/vector_platform_device_skia.h" | |
| 29 #include "third_party/skia/include/core/SkTypeface.h" | |
| 24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" | 30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" |
| 25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h" | 31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h" |
| 26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 32 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" | 33 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" |
| 28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 34 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" | 35 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" |
| 30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" | 36 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" |
| 31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" | 37 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" |
| 32 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h" | 38 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h" |
| 33 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 39 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 34 #include "ui/base/l10n/l10n_util.h" | 40 #include "ui/base/l10n/l10n_util.h" |
| 41 #include "ui/base/text/text_elider.h" | |
| 35 | 42 |
| 36 #if defined(OS_POSIX) | 43 #if defined(OS_POSIX) |
| 37 #include "content/common/view_messages.h" | 44 #include "content/common/view_messages.h" |
| 38 #endif | 45 #endif |
| 39 | 46 |
| 47 using base::Time; | |
| 40 using printing::ConvertPixelsToPoint; | 48 using printing::ConvertPixelsToPoint; |
| 41 using printing::ConvertPixelsToPointDouble; | 49 using printing::ConvertPixelsToPointDouble; |
| 42 using printing::ConvertUnit; | 50 using printing::ConvertUnit; |
| 43 using printing::ConvertUnitDouble; | 51 using printing::ConvertUnitDouble; |
| 44 using WebKit::WebConsoleMessage; | 52 using WebKit::WebConsoleMessage; |
| 45 using WebKit::WebDocument; | 53 using WebKit::WebDocument; |
| 46 using WebKit::WebElement; | 54 using WebKit::WebElement; |
| 47 using WebKit::WebFrame; | 55 using WebKit::WebFrame; |
| 48 using WebKit::WebNode; | 56 using WebKit::WebNode; |
| 49 using WebKit::WebSize; | 57 using WebKit::WebSize; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 81 oldParams.params.min_shrink == newParams.params.min_shrink && | 89 oldParams.params.min_shrink == newParams.params.min_shrink && |
| 82 oldParams.params.dpi == newParams.params.dpi && | 90 oldParams.params.dpi == newParams.params.dpi && |
| 83 oldParams.params.printable_size == newParams.params.printable_size && | 91 oldParams.params.printable_size == newParams.params.printable_size && |
| 84 oldParams.params.selection_only == newParams.params.selection_only && | 92 oldParams.params.selection_only == newParams.params.selection_only && |
| 85 oldParams.params.page_size == newParams.params.page_size && | 93 oldParams.params.page_size == newParams.params.page_size && |
| 86 oldParams.params.margin_top == newParams.params.margin_top && | 94 oldParams.params.margin_top == newParams.params.margin_top && |
| 87 oldParams.params.margin_left == newParams.params.margin_left && | 95 oldParams.params.margin_left == newParams.params.margin_left && |
| 88 oldParams.params.supports_alpha_blend == | 96 oldParams.params.supports_alpha_blend == |
| 89 newParams.params.supports_alpha_blend && | 97 newParams.params.supports_alpha_blend && |
| 90 oldParams.pages.size() == newParams.pages.size() && | 98 oldParams.pages.size() == newParams.pages.size() && |
| 99 oldParams.params.display_header_footer == | |
| 100 newParams.params.display_header_footer && | |
| 91 std::equal(oldParams.pages.begin(), oldParams.pages.end(), | 101 std::equal(oldParams.pages.begin(), oldParams.pages.end(), |
| 92 newParams.pages.begin()); | 102 newParams.pages.begin()); |
| 93 } | 103 } |
| 94 | 104 |
| 105 // Gets the byte length of the |text|. | |
| 106 size_t GetString16ByteLength(string16 text) { | |
| 107 return text.length() * sizeof(char16); | |
| 108 } | |
| 109 | |
| 110 // Splits the horizontal width equally into three segments with an interstice | |
| 111 // between each segment. Returns the width of this segment. | |
| 112 SkScalar GetSegmentWidth(PageSizeMargins& page_size_margins) { | |
| 113 return (page_size_margins.margin_left + | |
| 114 page_size_margins.content_width + | |
| 115 page_size_margins.margin_right - | |
| 116 (printing::kSettingHeaderFooterHorizontalRegions + 1) * | |
| 117 printing::kSettingHeaderFooterInterstice) / | |
| 118 printing::kSettingHeaderFooterHorizontalRegions; | |
| 119 } | |
| 120 | |
| 121 // Gets the (x,y) coordinate from where we want to start printing the current | |
|
vandebo (ex-Chrome)
2011/07/19 21:31:20
nit: (x, y)
Aayush Kumar
2011/07/21 21:58:53
Done.
| |
| 122 // text depending on the alignment horizontal alignment (LEFT, RIGHT, CENTER) | |
| 123 // and vertical alignment (TOP, BOTTOM). | |
| 124 SkPoint GetHeaderFooterPosition( | |
| 125 string16 text, | |
| 126 SkPaint paint, | |
| 127 printing::HorizontalHeaderFooterPosition hor_pos, | |
| 128 printing::VerticalHeaderFooterPosition ver_pos, | |
| 129 PageSizeMargins& page_size_margins) { | |
| 130 | |
| 131 SkScalar text_width_in_points = paint.measureText( | |
| 132 text.c_str(), GetString16ByteLength(text)); | |
| 133 | |
| 134 SkScalar x(0); | |
|
vandebo (ex-Chrome)
2011/07/19 21:31:20
nit: = 0; is easier to understand.
Aayush Kumar
2011/07/21 21:58:53
Done.
| |
| 135 switch (hor_pos) { | |
| 136 case printing::LEFT: { | |
| 137 x = page_size_margins.margin_left * (-1) + | |
| 138 printing::kSettingHeaderFooterInterstice; | |
| 139 break; | |
| 140 } | |
| 141 case printing::RIGHT: { | |
| 142 x = ((page_size_margins.content_width + page_size_margins.margin_right) - | |
| 143 (printing::kSettingHeaderFooterInterstice + text_width_in_points)); | |
| 144 break; | |
| 145 } | |
| 146 case printing::CENTER: { | |
| 147 SkScalar available_width = GetSegmentWidth(page_size_margins); | |
| 148 x = (available_width - page_size_margins.margin_left + | |
| 149 (available_width - text_width_in_points) / 2); | |
| 150 break; | |
| 151 } | |
| 152 default: { | |
| 153 NOTREACHED(); | |
| 154 } | |
| 155 } | |
| 156 | |
| 157 SkScalar y(0); | |
| 158 switch (ver_pos) { | |
| 159 case printing::TOP: { | |
| 160 y = page_size_margins.margin_top * (-1) + | |
| 161 printing::kSettingHeaderFooterInterstice + | |
| 162 paint.getTextSize(); | |
|
vandebo (ex-Chrome)
2011/07/19 21:31:20
As we talked about, getTextSize() doesn't tell you
Aayush Kumar
2011/07/21 21:58:53
Done.
| |
| 163 break; | |
| 164 } | |
| 165 case printing::BOTTOM: { | |
| 166 y = page_size_margins.margin_bottom + page_size_margins.content_height - | |
| 167 printing::kSettingHeaderFooterInterstice; | |
| 168 break; | |
| 169 } | |
| 170 default: { | |
| 171 NOTREACHED(); | |
| 172 } | |
| 173 } | |
| 174 | |
| 175 SkPoint point; | |
| 176 point.set(x, y); | |
| 177 return point; | |
| 178 } | |
| 179 | |
| 180 // Given a text, the positions and the paint object, this method gets the | |
| 181 // coordinates and prints the text at those co-ordinates on the canvas. | |
| 182 void PrintHeaderFooterText( | |
| 183 string16 text, | |
| 184 SkPaint paint, | |
| 185 const SkRefPtr<skia::VectorCanvas>& canvas, | |
| 186 printing::HorizontalHeaderFooterPosition hor_pos, | |
| 187 printing::VerticalHeaderFooterPosition ver_pos, | |
| 188 float webkit_scale_factor, | |
| 189 PageSizeMargins& page_size_margins) { | |
| 190 SkPoint point = GetHeaderFooterPosition(text, paint, hor_pos, ver_pos, | |
| 191 page_size_margins); | |
| 192 point.set(point.x() / webkit_scale_factor, point.y() / webkit_scale_factor); | |
| 193 size_t byte_length = GetString16ByteLength(text); | |
| 194 | |
| 195 canvas->drawText(text.c_str(), byte_length, point.x(), point.y(), paint); | |
| 196 } | |
| 197 | |
| 95 } // namespace | 198 } // namespace |
| 96 | 199 |
| 200 // Given the device and canvas to draw on, prints the appropriate headers | |
| 201 // and footers on to the canvas. | |
| 202 void PrintHeaderAndFooter(SkDevice *device, | |
|
vandebo (ex-Chrome)
2011/07/19 21:31:20
Arguments should line up.
vandebo (ex-Chrome)
2011/07/19 21:31:20
Maybe this function should take an SkPDFDevice*, d
Aayush Kumar
2011/07/21 21:58:53
Done.
Aayush Kumar
2011/07/21 21:58:53
Done.
| |
| 203 const SkRefPtr<skia::VectorCanvas>& canvas, | |
| 204 int page_number, int total_pages, float webkit_scale_factor, | |
| 205 PageSizeMargins& page_size_margins, | |
| 206 const DictionaryValue* header_footer_info) { | |
| 207 // Set the drawing area to draw in the margins. | |
| 208 ((skia::VectorPlatformDeviceSkia* )device) -> | |
|
vandebo (ex-Chrome)
2011/07/19 21:31:20
Use C++ style casts.
Aayush Kumar
2011/07/21 21:58:53
Done.
| |
| 209 setDrawingArea(SkPDFDevice::kMargin_DrawingArea); | |
| 210 | |
| 211 // Setting up styles for the headers and footers text. | |
| 212 SkPaint paint; | |
| 213 paint.setColor(SK_ColorBLACK); | |
| 214 paint.setTextSize(SkIntToScalar(printing::kSettingHeaderFooterFontSize) / | |
| 215 webkit_scale_factor); | |
| 216 paint.setTypeface(SkTypeface::CreateFromName(NULL, SkTypeface::kNormal)); | |
| 217 paint.setTextEncoding(SkPaint::kUTF16_TextEncoding); | |
| 218 | |
| 219 // Ensuring we have enough space to print above and below the page to print | |
|
vandebo (ex-Chrome)
2011/07/19 21:31:20
Is it necessary to do this? If margins are 0, the
Aayush Kumar
2011/07/21 21:58:53
I am afraid if the margins are more than '0', but,
| |
| 220 // headers and footers. | |
| 221 if (GetHeaderFooterPosition(UTF8ToUTF16("dummy_string"), paint, | |
| 222 printing::LEFT, printing::TOP, page_size_margins).y() > 0) { | |
| 223 return; | |
| 224 } | |
| 225 if (page_size_margins.margin_bottom - paint.getTextSize() - | |
| 226 printing::kSettingHeaderFooterInterstice < 0) { | |
| 227 return; | |
| 228 } | |
| 229 | |
| 230 SkScalar segment_width = GetSegmentWidth(page_size_margins); | |
| 231 // Printing the Date. | |
| 232 string16 date = base::TimeFormatShortDateNumeric(Time::Now()); | |
| 233 date = ui::ElideText(date, paint, segment_width, false); | |
| 234 PrintHeaderFooterText(date, paint, canvas, printing::LEFT, printing::TOP, | |
| 235 webkit_scale_factor, page_size_margins); | |
| 236 | |
| 237 // Printing the title. | |
| 238 string16 title; | |
| 239 if (!header_footer_info->GetString(printing::kSettingHeaderFooterTitle, | |
| 240 &title)) { | |
| 241 NOTREACHED(); | |
| 242 } | |
| 243 | |
| 244 SkScalar date_width = paint.measureText(date.c_str(), | |
| 245 GetString16ByteLength(date)); | |
| 246 // Calculating the available title width. If the date string is not long | |
| 247 // enough, then, we increase the available space we have for the title. | |
| 248 // Assumes there is no header text to RIGHT of title. | |
| 249 SkScalar max_title_width = std::min(2 * segment_width, | |
| 250 2 * (segment_width - date_width) + | |
| 251 segment_width); | |
| 252 title = ui::ElideText(title, paint, max_title_width, false); | |
| 253 PrintHeaderFooterText(title, paint, canvas, printing::CENTER, | |
| 254 printing::TOP, webkit_scale_factor, | |
| 255 page_size_margins); | |
| 256 | |
| 257 // Printing the page numbers at the bottom right corner of page. | |
| 258 string16 page_on_page_total = base::IntToString16(page_number) + | |
| 259 UTF8ToUTF16("/") + | |
| 260 base::IntToString16(total_pages); | |
| 261 page_on_page_total = ui::ElideText(page_on_page_total, paint, segment_width, | |
| 262 false); | |
| 263 PrintHeaderFooterText(page_on_page_total, paint, canvas, printing::RIGHT, | |
| 264 printing::BOTTOM, webkit_scale_factor, | |
| 265 page_size_margins); | |
| 266 | |
| 267 // Printing the URL. | |
| 268 std::string url; | |
| 269 if (!header_footer_info->GetString(printing::kSettingHeaderFooterURL, | |
| 270 &url)) { | |
| 271 NOTREACHED(); | |
| 272 } | |
| 273 GURL gurl(url); | |
| 274 SkScalar page_width = paint.measureText( | |
| 275 page_on_page_total.c_str(), | |
| 276 GetString16ByteLength(page_on_page_total)); | |
| 277 | |
| 278 // Calculating the available URL width. We increase the available URL width | |
| 279 // if the |page_on_page_total| string isn't long enough. | |
| 280 // Assumes no footer text being printed in the CENTER. | |
| 281 SkScalar max_url_width = 3 * (page_size_margins.content_width + | |
| 282 page_size_margins.margin_left + page_size_margins.margin_right) / 4; | |
| 283 max_url_width = std::min(max_url_width, | |
| 284 2 * (segment_width - page_width) + segment_width); | |
| 285 string16 url_elided = ui::ElideUrl(gurl, paint, max_url_width, | |
| 286 std::string()); | |
| 287 PrintHeaderFooterText(url_elided, paint, canvas, printing::LEFT, | |
| 288 printing::BOTTOM, webkit_scale_factor, | |
| 289 page_size_margins); | |
| 290 | |
| 291 // Restore the drawing area to draw in the content area. | |
| 292 ((skia::VectorPlatformDeviceSkia* )device)-> | |
| 293 setDrawingArea(SkPDFDevice::kContent_DrawingArea); | |
| 294 } | |
| 295 | |
| 97 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( | 296 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( |
| 98 const PrintMsg_Print_Params& print_params, | 297 const PrintMsg_Print_Params& print_params, |
| 99 WebFrame* frame, | 298 WebFrame* frame, |
| 100 WebNode* node) | 299 WebNode* node) |
| 101 : frame_(frame), | 300 : frame_(frame), |
| 102 web_view_(frame->view()), | 301 web_view_(frame->view()), |
| 103 expected_pages_count_(0), | 302 expected_pages_count_(0), |
| 104 use_browser_overlays_(true), | 303 use_browser_overlays_(true), |
| 105 finished_(false) { | 304 finished_(false) { |
| 106 int dpi = GetDPI(&print_params); | 305 int dpi = GetDPI(&print_params); |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 254 render_view()->webview()->mainFrame(); | 453 render_view()->webview()->mainFrame(); |
| 255 return true; | 454 return true; |
| 256 } | 455 } |
| 257 | 456 |
| 258 void PrintWebViewHelper::OnPrintPages() { | 457 void PrintWebViewHelper::OnPrintPages() { |
| 259 WebFrame* frame; | 458 WebFrame* frame; |
| 260 if (GetPrintFrame(&frame)) | 459 if (GetPrintFrame(&frame)) |
| 261 Print(frame, NULL); | 460 Print(frame, NULL); |
| 262 } | 461 } |
| 263 | 462 |
| 264 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { | 463 void PrintWebViewHelper::OnPrintPreview( |
| 464 const DictionaryValue& settings, | |
| 465 const DictionaryValue& header_footer_info) { | |
| 265 DCHECK(is_preview_); | 466 DCHECK(is_preview_); |
| 266 print_preview_context_.OnPrintPreview(); | 467 print_preview_context_.OnPrintPreview(); |
| 468 header_footer_info_ = header_footer_info.DeepCopy(); | |
| 267 | 469 |
| 268 if (!InitPrintSettings(print_preview_context_.frame(), | 470 if (!InitPrintSettings(print_preview_context_.frame(), |
| 269 print_preview_context_.node())) { | 471 print_preview_context_.node())) { |
| 270 NOTREACHED(); | 472 NOTREACHED(); |
| 271 return; | 473 return; |
| 272 } | 474 } |
| 273 | 475 |
| 274 if (!UpdatePrintSettings(settings)) { | 476 if (!UpdatePrintSettings(settings)) { |
| 275 DidFinishPrinting(FAIL_PREVIEW); | 477 DidFinishPrinting(FAIL_PREVIEW); |
| 276 return; | 478 return; |
| (...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 545 return true; | 747 return true; |
| 546 } | 748 } |
| 547 #endif // OS_MACOSX || OS_WIN | 749 #endif // OS_MACOSX || OS_WIN |
| 548 | 750 |
| 549 void PrintWebViewHelper::didStopLoading() { | 751 void PrintWebViewHelper::didStopLoading() { |
| 550 PrintMsg_PrintPages_Params* params = print_pages_params_.get(); | 752 PrintMsg_PrintPages_Params* params = print_pages_params_.get(); |
| 551 DCHECK(params != NULL); | 753 DCHECK(params != NULL); |
| 552 PrintPages(*params, print_web_view_->mainFrame(), NULL); | 754 PrintPages(*params, print_web_view_->mainFrame(), NULL); |
| 553 } | 755 } |
| 554 | 756 |
| 555 void PrintWebViewHelper::GetPageSizeAndMarginsInPoints( | 757 PageSizeMargins PrintWebViewHelper::GetPageSizeAndMarginsInPoints( |
| 556 WebFrame* frame, | 758 WebFrame* frame, |
| 557 int page_index, | 759 int page_index, |
| 558 const PrintMsg_Print_Params& default_params, | 760 const PrintMsg_Print_Params& default_params) { |
| 559 double* content_width_in_points, | |
| 560 double* content_height_in_points, | |
| 561 double* margin_top_in_points, | |
| 562 double* margin_right_in_points, | |
| 563 double* margin_bottom_in_points, | |
| 564 double* margin_left_in_points) { | |
| 565 int dpi = GetDPI(&default_params); | 761 int dpi = GetDPI(&default_params); |
| 566 | 762 |
| 567 WebSize page_size_in_pixels( | 763 WebSize page_size_in_pixels( |
| 568 ConvertUnit(default_params.page_size.width(), | 764 ConvertUnit(default_params.page_size.width(), |
| 569 dpi, printing::kPixelsPerInch), | 765 dpi, printing::kPixelsPerInch), |
| 570 ConvertUnit(default_params.page_size.height(), | 766 ConvertUnit(default_params.page_size.height(), |
| 571 dpi, printing::kPixelsPerInch)); | 767 dpi, printing::kPixelsPerInch)); |
| 572 int margin_top_in_pixels = ConvertUnit( | 768 int margin_top_in_pixels = ConvertUnit( |
| 573 default_params.margin_top, | 769 default_params.margin_top, |
| 574 dpi, printing::kPixelsPerInch); | 770 dpi, printing::kPixelsPerInch); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 586 | 782 |
| 587 if (frame) { | 783 if (frame) { |
| 588 frame->pageSizeAndMarginsInPixels(page_index, | 784 frame->pageSizeAndMarginsInPixels(page_index, |
| 589 page_size_in_pixels, | 785 page_size_in_pixels, |
| 590 margin_top_in_pixels, | 786 margin_top_in_pixels, |
| 591 margin_right_in_pixels, | 787 margin_right_in_pixels, |
| 592 margin_bottom_in_pixels, | 788 margin_bottom_in_pixels, |
| 593 margin_left_in_pixels); | 789 margin_left_in_pixels); |
| 594 } | 790 } |
| 595 | 791 |
| 596 *content_width_in_points = ConvertPixelsToPoint(page_size_in_pixels.width - | 792 PageSizeMargins page_size_margins_in_points; |
| 597 margin_left_in_pixels - | 793 page_size_margins_in_points.content_width = ConvertPixelsToPoint( |
| 598 margin_right_in_pixels); | 794 page_size_in_pixels.width - margin_left_in_pixels - |
| 599 *content_height_in_points = ConvertPixelsToPoint(page_size_in_pixels.height - | 795 margin_right_in_pixels); |
| 600 margin_top_in_pixels - | 796 page_size_margins_in_points.content_height = ConvertPixelsToPoint( |
| 601 margin_bottom_in_pixels); | 797 page_size_in_pixels.height - margin_top_in_pixels - |
| 798 margin_bottom_in_pixels); | |
| 602 | 799 |
| 603 // Invalid page size and/or margins. We just use the default setting. | 800 // Invalid page size and/or margins. We just use the default setting. |
| 604 if (*content_width_in_points < 1.0 || *content_height_in_points < 1.0) { | 801 if (page_size_margins_in_points.content_width < 1.0 || |
| 605 GetPageSizeAndMarginsInPoints(NULL, | 802 page_size_margins_in_points.content_height < 1.0) { |
| 606 page_index, | 803 return GetPageSizeAndMarginsInPoints(NULL, page_index, default_params); |
| 607 default_params, | |
| 608 content_width_in_points, | |
| 609 content_height_in_points, | |
| 610 margin_top_in_points, | |
| 611 margin_right_in_points, | |
| 612 margin_bottom_in_points, | |
| 613 margin_left_in_points); | |
| 614 return; | |
| 615 } | 804 } |
| 616 | 805 |
| 617 if (margin_top_in_points) | 806 page_size_margins_in_points.margin_top = |
| 618 *margin_top_in_points = | 807 ConvertPixelsToPointDouble(margin_top_in_pixels); |
| 619 ConvertPixelsToPointDouble(margin_top_in_pixels); | 808 page_size_margins_in_points.margin_right = |
| 620 if (margin_right_in_points) | 809 ConvertPixelsToPointDouble(margin_right_in_pixels); |
| 621 *margin_right_in_points = | 810 page_size_margins_in_points.margin_bottom = |
| 622 ConvertPixelsToPointDouble(margin_right_in_pixels); | 811 ConvertPixelsToPointDouble(margin_bottom_in_pixels); |
| 623 if (margin_bottom_in_points) | 812 page_size_margins_in_points.margin_left = |
| 624 *margin_bottom_in_points = | 813 ConvertPixelsToPointDouble(margin_left_in_pixels); |
| 625 ConvertPixelsToPointDouble(margin_bottom_in_pixels); | 814 return page_size_margins_in_points; |
| 626 if (margin_left_in_points) | |
| 627 *margin_left_in_points = | |
| 628 ConvertPixelsToPointDouble(margin_left_in_pixels); | |
| 629 } | 815 } |
| 630 | 816 |
| 631 void PrintWebViewHelper::UpdatePrintableSizeInPrintParameters( | 817 void PrintWebViewHelper::UpdatePrintableSizeInPrintParameters( |
| 632 WebFrame* frame, | 818 WebFrame* frame, |
| 633 WebNode* node, | 819 WebNode* node, |
| 634 PrintMsg_Print_Params* params) { | 820 PrintMsg_Print_Params* params) { |
| 635 double content_width_in_points; | |
| 636 double content_height_in_points; | |
| 637 double margin_top_in_points; | |
| 638 double margin_right_in_points; | |
| 639 double margin_bottom_in_points; | |
| 640 double margin_left_in_points; | |
| 641 PrepareFrameAndViewForPrint prepare(*params, frame, node); | 821 PrepareFrameAndViewForPrint prepare(*params, frame, node); |
| 642 PrintWebViewHelper::GetPageSizeAndMarginsInPoints(frame, 0, *params, | 822 PageSizeMargins page_size_margins_in_points = |
| 643 &content_width_in_points, &content_height_in_points, | 823 PrintWebViewHelper::GetPageSizeAndMarginsInPoints(frame, 0, *params); |
| 644 &margin_top_in_points, &margin_right_in_points, | |
| 645 &margin_bottom_in_points, &margin_left_in_points); | |
| 646 int dpi = GetDPI(params); | 824 int dpi = GetDPI(params); |
| 647 params->printable_size = gfx::Size( | 825 params->printable_size = gfx::Size( |
| 648 static_cast<int>(ConvertUnitDouble(content_width_in_points, | 826 static_cast<int>(ConvertUnitDouble( |
| 827 page_size_margins_in_points.content_width, | |
| 649 printing::kPointsPerInch, dpi)), | 828 printing::kPointsPerInch, dpi)), |
| 650 static_cast<int>(ConvertUnitDouble(content_height_in_points, | 829 static_cast<int>(ConvertUnitDouble( |
| 830 page_size_margins_in_points.content_height, | |
| 651 printing::kPointsPerInch, dpi))); | 831 printing::kPointsPerInch, dpi))); |
| 652 | 832 |
| 653 double page_width_in_points = content_width_in_points + | 833 double page_width_in_points = page_size_margins_in_points.content_width + |
| 654 margin_left_in_points + margin_right_in_points; | 834 page_size_margins_in_points.margin_left + |
| 655 double page_height_in_points = content_height_in_points + | 835 page_size_margins_in_points.margin_right; |
| 656 margin_top_in_points + margin_bottom_in_points; | 836 double page_height_in_points = page_size_margins_in_points.content_height + |
| 837 page_size_margins_in_points.margin_top + | |
| 838 page_size_margins_in_points.margin_bottom; | |
| 657 | 839 |
| 658 params->page_size = gfx::Size( | 840 params->page_size = gfx::Size( |
| 659 static_cast<int>(ConvertUnitDouble( | 841 static_cast<int>(ConvertUnitDouble( |
| 660 page_width_in_points, printing::kPointsPerInch, dpi)), | 842 page_width_in_points, printing::kPointsPerInch, dpi)), |
| 661 static_cast<int>(ConvertUnitDouble( | 843 static_cast<int>(ConvertUnitDouble( |
| 662 page_height_in_points, printing::kPointsPerInch, dpi))); | 844 page_height_in_points, printing::kPointsPerInch, dpi))); |
| 663 | 845 |
| 664 params->margin_top = static_cast<int>(ConvertUnitDouble( | 846 params->margin_top = static_cast<int>(ConvertUnitDouble( |
| 665 margin_top_in_points, printing::kPointsPerInch, dpi)); | 847 page_size_margins_in_points.margin_top, printing::kPointsPerInch, dpi)); |
| 666 params->margin_left = static_cast<int>(ConvertUnitDouble( | 848 params->margin_left = static_cast<int>(ConvertUnitDouble( |
| 667 margin_left_in_points, printing::kPointsPerInch, dpi)); | 849 page_size_margins_in_points.margin_left, printing::kPointsPerInch, dpi)); |
| 668 } | 850 } |
| 669 | 851 |
| 670 bool PrintWebViewHelper::InitPrintSettings(WebKit::WebFrame* frame, | 852 bool PrintWebViewHelper::InitPrintSettings(WebKit::WebFrame* frame, |
| 671 WebKit::WebNode* node) { | 853 WebKit::WebNode* node) { |
| 672 DCHECK(frame); | 854 DCHECK(frame); |
| 673 PrintMsg_PrintPages_Params settings; | 855 PrintMsg_PrintPages_Params settings; |
| 674 | 856 |
| 675 // TODO(abodenha@chromium.org) It doesn't make sense to do this if our | 857 // TODO(abodenha@chromium.org) It doesn't make sense to do this if our |
| 676 // "default" is a cloud based printer. Split InitPrintSettings up | 858 // "default" is a cloud based printer. Split InitPrintSettings up |
| 677 // so that we can avoid the overhead of unneeded calls into the native | 859 // so that we can avoid the overhead of unneeded calls into the native |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 713 settings.params.margin_top = 150; | 895 settings.params.margin_top = 150; |
| 714 settings.params.margin_left = 150; | 896 settings.params.margin_left = 150; |
| 715 settings.params.dpi = 300.0; | 897 settings.params.dpi = 300.0; |
| 716 settings.params.min_shrink = 1.25; | 898 settings.params.min_shrink = 1.25; |
| 717 settings.params.max_shrink = 2.0; | 899 settings.params.max_shrink = 2.0; |
| 718 settings.params.desired_dpi = 72; | 900 settings.params.desired_dpi = 72; |
| 719 settings.params.selection_only = false; | 901 settings.params.selection_only = false; |
| 720 settings.params.supports_alpha_blend = false; | 902 settings.params.supports_alpha_blend = false; |
| 721 // TODO(abodenha@chromium.org) Parse page ranges from the job_settings. | 903 // TODO(abodenha@chromium.org) Parse page ranges from the job_settings. |
| 722 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); | 904 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); |
| 905 print_pages_params_->params.display_header_footer = false; | |
| 723 return true; | 906 return true; |
| 724 } | 907 } |
| 725 | 908 |
| 726 bool PrintWebViewHelper::UpdatePrintSettingsLocal( | 909 bool PrintWebViewHelper::UpdatePrintSettingsLocal( |
| 727 const DictionaryValue& job_settings) { | 910 const DictionaryValue& job_settings) { |
| 728 PrintMsg_PrintPages_Params settings; | 911 PrintMsg_PrintPages_Params settings; |
| 729 | 912 |
| 730 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), | 913 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), |
| 731 print_pages_params_->params.document_cookie, job_settings, &settings)); | 914 print_pages_params_->params.document_cookie, job_settings, &settings)); |
| 732 | 915 |
| 733 if (settings.params.dpi < kMinDpi || !settings.params.document_cookie) | 916 if (settings.params.dpi < kMinDpi || !settings.params.document_cookie) |
| 734 return false; | 917 return false; |
| 735 | 918 |
| 736 if (!job_settings.GetInteger(printing::kPreviewRequestID, | 919 if (!job_settings.GetInteger(printing::kPreviewRequestID, |
| 737 &settings.params.preview_request_id)) { | 920 &settings.params.preview_request_id)) { |
| 738 NOTREACHED(); | 921 NOTREACHED(); |
| 739 return false; | 922 return false; |
| 740 } | 923 } |
| 741 | 924 |
| 742 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); | 925 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); |
| 926 bool header_footer; | |
| 927 if (!job_settings.GetBoolean(printing::kSettingHeaderFooter, &header_footer)) | |
| 928 NOTREACHED(); | |
| 929 print_pages_params_->params.display_header_footer = header_footer; | |
| 930 | |
| 743 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(), | 931 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(), |
| 744 settings.params.document_cookie)); | 932 settings.params.document_cookie)); |
| 745 return true; | 933 return true; |
| 746 } | 934 } |
| 747 | 935 |
| 748 bool PrintWebViewHelper::UpdatePrintSettings( | 936 bool PrintWebViewHelper::UpdatePrintSettings( |
| 749 const DictionaryValue& job_settings) { | 937 const DictionaryValue& job_settings) { |
| 750 if (job_settings.HasKey(printing::kSettingCloudPrintId)) { | 938 if (job_settings.HasKey(printing::kSettingCloudPrintId)) { |
| 751 return UpdatePrintSettingsCloud(job_settings); | 939 return UpdatePrintSettingsCloud(job_settings); |
| 752 } else { | 940 } else { |
| (...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1054 const gfx::Size& | 1242 const gfx::Size& |
| 1055 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const { | 1243 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const { |
| 1056 return prep_frame_view_->GetPrintCanvasSize(); | 1244 return prep_frame_view_->GetPrintCanvasSize(); |
| 1057 } | 1245 } |
| 1058 | 1246 |
| 1059 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { | 1247 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { |
| 1060 prep_frame_view_.reset(); | 1248 prep_frame_view_.reset(); |
| 1061 metafile_.reset(); | 1249 metafile_.reset(); |
| 1062 rendered_pages_.clear(); | 1250 rendered_pages_.clear(); |
| 1063 } | 1251 } |
| OLD | NEW |