OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/printing/print_web_view_helper.h" | 5 #include "chrome/renderer/printing/print_web_view_helper.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 const char kPageLoadScriptFormat[] = | 56 const char kPageLoadScriptFormat[] = |
57 "document.open(); document.write(%s); document.close();"; | 57 "document.open(); document.write(%s); document.close();"; |
58 | 58 |
59 const char kPageSetupScriptFormat[] = "setup(%s);"; | 59 const char kPageSetupScriptFormat[] = "setup(%s);"; |
60 | 60 |
61 void ExecuteScript(WebKit::WebFrame* frame, | 61 void ExecuteScript(WebKit::WebFrame* frame, |
62 const char* script_format, | 62 const char* script_format, |
63 const base::Value& parameters) { | 63 const base::Value& parameters) { |
64 std::string json; | 64 std::string json; |
65 base::JSONWriter::Write(¶meters, &json); | 65 base::JSONWriter::Write(¶meters, &json); |
66 std::string script = StringPrintf(script_format, json.c_str()); | 66 std::string script = base::StringPrintf(script_format, json.c_str()); |
67 frame->executeScript(WebKit::WebString(UTF8ToUTF16(script))); | 67 frame->executeScript(WebKit::WebString(UTF8ToUTF16(script))); |
68 } | 68 } |
69 | 69 |
70 int GetDPI(const PrintMsg_Print_Params* print_params) { | 70 int GetDPI(const PrintMsg_Print_Params* print_params) { |
71 #if defined(OS_MACOSX) | 71 #if defined(OS_MACOSX) |
72 // On the Mac, the printable area is in points, don't do any scaling based | 72 // On the Mac, the printable area is in points, don't do any scaling based |
73 // on dpi. | 73 // on dpi. |
74 return kPointsPerInch; | 74 return kPointsPerInch; |
75 #else | 75 #else |
76 return static_cast<int>(print_params->dpi); | 76 return static_cast<int>(print_params->dpi); |
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
431 IDR_PRINT_PREVIEW_PAGE)); | 431 IDR_PRINT_PREVIEW_PAGE)); |
432 // Load page with script to avoid async operations. | 432 // Load page with script to avoid async operations. |
433 ExecuteScript(frame, kPageLoadScriptFormat, html); | 433 ExecuteScript(frame, kPageLoadScriptFormat, html); |
434 | 434 |
435 scoped_ptr<base::DictionaryValue> options(header_footer_info.DeepCopy()); | 435 scoped_ptr<base::DictionaryValue> options(header_footer_info.DeepCopy()); |
436 options->SetDouble("width", page_size.width); | 436 options->SetDouble("width", page_size.width); |
437 options->SetDouble("height", page_size.height); | 437 options->SetDouble("height", page_size.height); |
438 options->SetDouble("topMargin", page_layout.margin_top); | 438 options->SetDouble("topMargin", page_layout.margin_top); |
439 options->SetDouble("bottomMargin", page_layout.margin_bottom); | 439 options->SetDouble("bottomMargin", page_layout.margin_bottom); |
440 options->SetString("pageNumber", | 440 options->SetString("pageNumber", |
441 StringPrintf("%d/%d", page_number, total_pages)); | 441 base::StringPrintf("%d/%d", page_number, total_pages)); |
442 | 442 |
443 ExecuteScript(frame, kPageSetupScriptFormat, *options); | 443 ExecuteScript(frame, kPageSetupScriptFormat, *options); |
444 | 444 |
445 WebKit::WebPrintParams webkit_params(page_size); | 445 WebKit::WebPrintParams webkit_params(page_size); |
446 webkit_params.printerDPI = GetDPI(¶ms); | 446 webkit_params.printerDPI = GetDPI(¶ms); |
447 | 447 |
448 frame->printBegin(webkit_params, WebKit::WebNode(), NULL); | 448 frame->printBegin(webkit_params, WebKit::WebNode(), NULL); |
449 frame->printPage(0, canvas); | 449 frame->printPage(0, canvas); |
450 frame->printEnd(); | 450 frame->printEnd(); |
451 | 451 |
(...skipping 1489 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1941 } | 1941 } |
1942 | 1942 |
1943 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { | 1943 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { |
1944 prep_frame_view_.reset(); | 1944 prep_frame_view_.reset(); |
1945 metafile_.reset(); | 1945 metafile_.reset(); |
1946 pages_to_render_.clear(); | 1946 pages_to_render_.clear(); |
1947 error_ = PREVIEW_ERROR_NONE; | 1947 error_ = PREVIEW_ERROR_NONE; |
1948 } | 1948 } |
1949 | 1949 |
1950 } // namespace printing | 1950 } // namespace printing |
OLD | NEW |