| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "android_webview/native/aw_pdf_exporter.h" | 5 #include "android_webview/browser/aw_pdf_exporter.h" |
| 6 | 6 |
| 7 #include "android_webview/browser/aw_print_manager.h" | 7 #include "android_webview/browser/aw_print_manager.h" |
| 8 #include "base/android/jni_android.h" | 8 #include "base/android/jni_android.h" |
| 9 #include "base/android/jni_array.h" | 9 #include "base/android/jni_array.h" |
| 10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "jni/AwPdfExporter_jni.h" | 11 #include "jni/AwPdfExporter_jni.h" |
| 12 #include "printing/print_settings.h" | 12 #include "printing/print_settings.h" |
| 13 #include "printing/units.h" | 13 #include "printing/units.h" |
| 14 | 14 |
| 15 using base::android::JavaParamRef; | 15 using base::android::JavaParamRef; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 33 } | 33 } |
| 34 } | 34 } |
| 35 | 35 |
| 36 } // namespace | 36 } // namespace |
| 37 | 37 |
| 38 AwPdfExporter::AwPdfExporter(JNIEnv* env, | 38 AwPdfExporter::AwPdfExporter(JNIEnv* env, |
| 39 const JavaRef<jobject>& obj, | 39 const JavaRef<jobject>& obj, |
| 40 content::WebContents* web_contents) | 40 content::WebContents* web_contents) |
| 41 : java_ref_(env, obj), web_contents_(web_contents) { | 41 : java_ref_(env, obj), web_contents_(web_contents) { |
| 42 DCHECK(!obj.is_null()); | 42 DCHECK(!obj.is_null()); |
| 43 Java_AwPdfExporter_setNativeAwPdfExporter( | 43 Java_AwPdfExporter_setNativeAwPdfExporter(env, obj, |
| 44 env, obj, reinterpret_cast<intptr_t>(this)); | 44 reinterpret_cast<intptr_t>(this)); |
| 45 } | 45 } |
| 46 | 46 |
| 47 AwPdfExporter::~AwPdfExporter() { | 47 AwPdfExporter::~AwPdfExporter() { |
| 48 JNIEnv* env = base::android::AttachCurrentThread(); | 48 JNIEnv* env = base::android::AttachCurrentThread(); |
| 49 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); | 49 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); |
| 50 if (obj.is_null()) | 50 if (obj.is_null()) |
| 51 return; | 51 return; |
| 52 // Clear the Java peer's weak pointer to |this| object. | 52 // Clear the Java peer's weak pointer to |this| object. |
| 53 Java_AwPdfExporter_setNativeAwPdfExporter(env, obj, 0); | 53 Java_AwPdfExporter_setNativeAwPdfExporter(env, obj, 0); |
| 54 } | 54 } |
| 55 | 55 |
| 56 void AwPdfExporter::ExportToPdf(JNIEnv* env, | 56 void AwPdfExporter::ExportToPdf(JNIEnv* env, |
| 57 const JavaParamRef<jobject>& obj, | 57 const JavaParamRef<jobject>& obj, |
| 58 int fd, | 58 int fd, |
| 59 jintArray pages, | 59 jintArray pages, |
| 60 const JavaParamRef<jobject>& cancel_signal) { | 60 const JavaParamRef<jobject>& cancel_signal) { |
| 61 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 61 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 62 printing::PrintSettings print_settings; | 62 printing::PrintSettings print_settings; |
| 63 printing::PageRanges page_ranges; | 63 printing::PageRanges page_ranges; |
| 64 GetPageRanges(env, pages, &page_ranges); | 64 GetPageRanges(env, pages, &page_ranges); |
| 65 InitPdfSettings(env, obj, page_ranges, print_settings); | 65 InitPdfSettings(env, obj, page_ranges, print_settings); |
| 66 AwPrintManager* print_manager = | 66 AwPrintManager* print_manager = AwPrintManager::CreateForWebContents( |
| 67 AwPrintManager::CreateForWebContents( | 67 web_contents_, print_settings, base::FileDescriptor(fd, false), |
| 68 web_contents_, print_settings, base::FileDescriptor(fd, false), | 68 base::Bind(&AwPdfExporter::DidExportPdf, base::Unretained(this))); |
| 69 base::Bind(&AwPdfExporter::DidExportPdf, base::Unretained(this))); | |
| 70 | 69 |
| 71 if (!print_manager->PrintNow()) | 70 if (!print_manager->PrintNow()) |
| 72 DidExportPdf(fd, false); | 71 DidExportPdf(fd, false); |
| 73 } | 72 } |
| 74 | 73 |
| 75 namespace { | 74 namespace { |
| 76 // Converts from 1/1000 of inches to device units using DPI. | 75 // Converts from 1/1000 of inches to device units using DPI. |
| 77 int MilsToDots(int val, int dpi) { | 76 int MilsToDots(int val, int dpi) { |
| 78 return static_cast<int>(printing::ConvertUnitDouble(val, 1000.0, dpi)); | 77 return static_cast<int>(printing::ConvertUnitDouble(val, 1000.0, dpi)); |
| 79 } | 78 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 95 // Assume full page is printable for now. | 94 // Assume full page is printable for now. |
| 96 printable_area_device_units.SetRect(0, 0, width_in_dots, height_in_dots); | 95 printable_area_device_units.SetRect(0, 0, width_in_dots, height_in_dots); |
| 97 | 96 |
| 98 if (!page_ranges.empty()) | 97 if (!page_ranges.empty()) |
| 99 settings.set_ranges(page_ranges); | 98 settings.set_ranges(page_ranges); |
| 100 | 99 |
| 101 settings.set_dpi(dpi); | 100 settings.set_dpi(dpi); |
| 102 // TODO(sgurun) verify that the value for newly added parameter for | 101 // TODO(sgurun) verify that the value for newly added parameter for |
| 103 // (i.e. landscape_needs_flip) is correct. | 102 // (i.e. landscape_needs_flip) is correct. |
| 104 settings.SetPrinterPrintableArea(physical_size_device_units, | 103 settings.SetPrinterPrintableArea(physical_size_device_units, |
| 105 printable_area_device_units, | 104 printable_area_device_units, true); |
| 106 true); | |
| 107 | 105 |
| 108 printing::PageMargins margins; | 106 printing::PageMargins margins; |
| 109 margins.left = | 107 margins.left = MilsToDots(Java_AwPdfExporter_getLeftMargin(env, obj), dpi); |
| 110 MilsToDots(Java_AwPdfExporter_getLeftMargin(env, obj), dpi); | 108 margins.right = MilsToDots(Java_AwPdfExporter_getRightMargin(env, obj), dpi); |
| 111 margins.right = | 109 margins.top = MilsToDots(Java_AwPdfExporter_getTopMargin(env, obj), dpi); |
| 112 MilsToDots(Java_AwPdfExporter_getRightMargin(env, obj), dpi); | |
| 113 margins.top = | |
| 114 MilsToDots(Java_AwPdfExporter_getTopMargin(env, obj), dpi); | |
| 115 margins.bottom = | 110 margins.bottom = |
| 116 MilsToDots(Java_AwPdfExporter_getBottomMargin(env, obj), dpi); | 111 MilsToDots(Java_AwPdfExporter_getBottomMargin(env, obj), dpi); |
| 117 settings.SetCustomMargins(margins); | 112 settings.SetCustomMargins(margins); |
| 118 settings.set_should_print_backgrounds(true); | 113 settings.set_should_print_backgrounds(true); |
| 119 } | 114 } |
| 120 | 115 |
| 121 void AwPdfExporter::DidExportPdf(int fd, bool success) { | 116 void AwPdfExporter::DidExportPdf(int fd, bool success) { |
| 122 JNIEnv* env = base::android::AttachCurrentThread(); | 117 JNIEnv* env = base::android::AttachCurrentThread(); |
| 123 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); | 118 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); |
| 124 if (obj.is_null()) | 119 if (obj.is_null()) |
| 125 return; | 120 return; |
| 126 Java_AwPdfExporter_didExportPdf(env, obj, success); | 121 Java_AwPdfExporter_didExportPdf(env, obj, success); |
| 127 } | 122 } |
| 128 | 123 |
| 129 bool RegisterAwPdfExporter(JNIEnv* env) { | 124 bool RegisterAwPdfExporter(JNIEnv* env) { |
| 130 return RegisterNativesImpl(env); | 125 return RegisterNativesImpl(env); |
| 131 } | 126 } |
| 132 | 127 |
| 133 } // namespace android_webview | 128 } // namespace android_webview |
| OLD | NEW |