| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "android_webview/native/aw_pdf_exporter.h" | |
| 6 | |
| 7 #include "android_webview/browser/aw_print_manager.h" | |
| 8 #include "base/android/jni_android.h" | |
| 9 #include "base/android/jni_array.h" | |
| 10 #include "content/public/browser/browser_thread.h" | |
| 11 #include "jni/AwPdfExporter_jni.h" | |
| 12 #include "printing/print_settings.h" | |
| 13 #include "printing/units.h" | |
| 14 | |
| 15 using base::android::JavaParamRef; | |
| 16 using base::android::JavaRef; | |
| 17 using base::android::ScopedJavaLocalRef; | |
| 18 | |
| 19 namespace android_webview { | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 void GetPageRanges(JNIEnv* env, | |
| 24 jintArray int_arr, | |
| 25 printing::PageRanges* range_vector) { | |
| 26 std::vector<int> pages; | |
| 27 base::android::JavaIntArrayToIntVector(env, int_arr, &pages); | |
| 28 for (int page : pages) { | |
| 29 printing::PageRange range; | |
| 30 range.from = page; | |
| 31 range.to = page; | |
| 32 range_vector->push_back(range); | |
| 33 } | |
| 34 } | |
| 35 | |
| 36 } // namespace | |
| 37 | |
| 38 AwPdfExporter::AwPdfExporter(JNIEnv* env, | |
| 39 const JavaRef<jobject>& obj, | |
| 40 content::WebContents* web_contents) | |
| 41 : java_ref_(env, obj), web_contents_(web_contents) { | |
| 42 DCHECK(!obj.is_null()); | |
| 43 Java_AwPdfExporter_setNativeAwPdfExporter( | |
| 44 env, obj, reinterpret_cast<intptr_t>(this)); | |
| 45 } | |
| 46 | |
| 47 AwPdfExporter::~AwPdfExporter() { | |
| 48 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 49 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); | |
| 50 if (obj.is_null()) | |
| 51 return; | |
| 52 // Clear the Java peer's weak pointer to |this| object. | |
| 53 Java_AwPdfExporter_setNativeAwPdfExporter(env, obj, 0); | |
| 54 } | |
| 55 | |
| 56 void AwPdfExporter::ExportToPdf(JNIEnv* env, | |
| 57 const JavaParamRef<jobject>& obj, | |
| 58 int fd, | |
| 59 jintArray pages, | |
| 60 const JavaParamRef<jobject>& cancel_signal) { | |
| 61 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 62 printing::PrintSettings print_settings; | |
| 63 printing::PageRanges page_ranges; | |
| 64 GetPageRanges(env, pages, &page_ranges); | |
| 65 InitPdfSettings(env, obj, page_ranges, print_settings); | |
| 66 AwPrintManager* print_manager = | |
| 67 AwPrintManager::CreateForWebContents( | |
| 68 web_contents_, print_settings, base::FileDescriptor(fd, false), | |
| 69 base::Bind(&AwPdfExporter::DidExportPdf, base::Unretained(this))); | |
| 70 | |
| 71 if (!print_manager->PrintNow()) | |
| 72 DidExportPdf(fd, false); | |
| 73 } | |
| 74 | |
| 75 namespace { | |
| 76 // Converts from 1/1000 of inches to device units using DPI. | |
| 77 int MilsToDots(int val, int dpi) { | |
| 78 return static_cast<int>(printing::ConvertUnitDouble(val, 1000.0, dpi)); | |
| 79 } | |
| 80 } // namespace | |
| 81 | |
| 82 void AwPdfExporter::InitPdfSettings(JNIEnv* env, | |
| 83 const JavaRef<jobject>& obj, | |
| 84 const printing::PageRanges& page_ranges, | |
| 85 printing::PrintSettings& settings) { | |
| 86 int dpi = Java_AwPdfExporter_getDpi(env, obj); | |
| 87 int width = Java_AwPdfExporter_getPageWidth(env, obj); | |
| 88 int height = Java_AwPdfExporter_getPageHeight(env, obj); | |
| 89 gfx::Size physical_size_device_units; | |
| 90 int width_in_dots = MilsToDots(width, dpi); | |
| 91 int height_in_dots = MilsToDots(height, dpi); | |
| 92 physical_size_device_units.SetSize(width_in_dots, height_in_dots); | |
| 93 | |
| 94 gfx::Rect printable_area_device_units; | |
| 95 // Assume full page is printable for now. | |
| 96 printable_area_device_units.SetRect(0, 0, width_in_dots, height_in_dots); | |
| 97 | |
| 98 if (!page_ranges.empty()) | |
| 99 settings.set_ranges(page_ranges); | |
| 100 | |
| 101 settings.set_dpi(dpi); | |
| 102 // TODO(sgurun) verify that the value for newly added parameter for | |
| 103 // (i.e. landscape_needs_flip) is correct. | |
| 104 settings.SetPrinterPrintableArea(physical_size_device_units, | |
| 105 printable_area_device_units, | |
| 106 true); | |
| 107 | |
| 108 printing::PageMargins margins; | |
| 109 margins.left = | |
| 110 MilsToDots(Java_AwPdfExporter_getLeftMargin(env, obj), dpi); | |
| 111 margins.right = | |
| 112 MilsToDots(Java_AwPdfExporter_getRightMargin(env, obj), dpi); | |
| 113 margins.top = | |
| 114 MilsToDots(Java_AwPdfExporter_getTopMargin(env, obj), dpi); | |
| 115 margins.bottom = | |
| 116 MilsToDots(Java_AwPdfExporter_getBottomMargin(env, obj), dpi); | |
| 117 settings.SetCustomMargins(margins); | |
| 118 settings.set_should_print_backgrounds(true); | |
| 119 } | |
| 120 | |
| 121 void AwPdfExporter::DidExportPdf(int fd, bool success) { | |
| 122 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 123 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); | |
| 124 if (obj.is_null()) | |
| 125 return; | |
| 126 Java_AwPdfExporter_didExportPdf(env, obj, success); | |
| 127 } | |
| 128 | |
| 129 bool RegisterAwPdfExporter(JNIEnv* env) { | |
| 130 return RegisterNativesImpl(env); | |
| 131 } | |
| 132 | |
| 133 } // namespace android_webview | |
| OLD | NEW |