| 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/native/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 "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
| 10 #include "jni/AwPdfExporter_jni.h" | 10 #include "jni/AwPdfExporter_jni.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 return; | 30 return; |
| 31 // Clear the Java peer's weak pointer to |this| object. | 31 // Clear the Java peer's weak pointer to |this| object. |
| 32 Java_AwPdfExporter_setNativeAwPdfExporter(env, obj.obj(), 0); | 32 Java_AwPdfExporter_setNativeAwPdfExporter(env, obj.obj(), 0); |
| 33 } | 33 } |
| 34 | 34 |
| 35 void AwPdfExporter::ExportToPdf(JNIEnv* env, | 35 void AwPdfExporter::ExportToPdf(JNIEnv* env, |
| 36 jobject obj, | 36 jobject obj, |
| 37 int fd, | 37 int fd, |
| 38 jobject cancel_signal) { | 38 jobject cancel_signal) { |
| 39 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 39 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 40 CreatePdfSettings(env, obj); | 40 printing::PrintSettings print_settings; |
| 41 InitPdfSettings(env, obj, print_settings); |
| 41 AwPrintManager* print_manager = | 42 AwPrintManager* print_manager = |
| 42 AwPrintManager::CreateForWebContents( | 43 AwPrintManager::CreateForWebContents( |
| 43 web_contents_, *print_settings_, base::FileDescriptor(fd, false), | 44 web_contents_, print_settings, base::FileDescriptor(fd, false), |
| 44 base::Bind(&AwPdfExporter::DidExportPdf, base::Unretained(this))); | 45 base::Bind(&AwPdfExporter::DidExportPdf, base::Unretained(this))); |
| 45 | 46 |
| 46 if (!print_manager->PrintNow()) | 47 if (!print_manager->PrintNow()) |
| 47 DidExportPdf(fd, false); | 48 DidExportPdf(fd, false); |
| 48 } | 49 } |
| 49 | 50 |
| 50 namespace { | 51 namespace { |
| 51 // Converts from 1/1000 of inches to device units using DPI. | 52 // Converts from 1/1000 of inches to device units using DPI. |
| 52 int MilsToDots(int val, int dpi) { | 53 int MilsToDots(int val, int dpi) { |
| 53 return static_cast<int>(printing::ConvertUnitDouble(val, 1000.0, dpi)); | 54 return static_cast<int>(printing::ConvertUnitDouble(val, 1000.0, dpi)); |
| 54 } | 55 } |
| 55 } // anonymous namespace | 56 } // anonymous namespace |
| 56 | 57 |
| 57 void AwPdfExporter::CreatePdfSettings(JNIEnv* env, jobject obj) { | 58 void AwPdfExporter::InitPdfSettings(JNIEnv* env, |
| 58 print_settings_.reset(new printing::PrintSettings); | 59 jobject obj, |
| 60 printing::PrintSettings& settings) { |
| 59 int dpi = Java_AwPdfExporter_getDpi(env, obj); | 61 int dpi = Java_AwPdfExporter_getDpi(env, obj); |
| 60 int width = Java_AwPdfExporter_getPageWidth(env, obj); | 62 int width = Java_AwPdfExporter_getPageWidth(env, obj); |
| 61 int height = Java_AwPdfExporter_getPageHeight(env, obj); | 63 int height = Java_AwPdfExporter_getPageHeight(env, obj); |
| 62 gfx::Size physical_size_device_units; | 64 gfx::Size physical_size_device_units; |
| 63 int width_in_dots = MilsToDots(width, dpi); | 65 int width_in_dots = MilsToDots(width, dpi); |
| 64 int height_in_dots = MilsToDots(height, dpi); | 66 int height_in_dots = MilsToDots(height, dpi); |
| 65 physical_size_device_units.SetSize(width_in_dots, height_in_dots); | 67 physical_size_device_units.SetSize(width_in_dots, height_in_dots); |
| 66 | 68 |
| 67 gfx::Rect printable_area_device_units; | 69 gfx::Rect printable_area_device_units; |
| 68 // Assume full page is printable for now. | 70 // Assume full page is printable for now. |
| 69 printable_area_device_units.SetRect(0, 0, width_in_dots, height_in_dots); | 71 printable_area_device_units.SetRect(0, 0, width_in_dots, height_in_dots); |
| 70 | 72 |
| 71 print_settings_->set_dpi(dpi); | 73 settings.set_dpi(dpi); |
| 72 // TODO(sgurun) verify that the value for newly added parameter for | 74 // TODO(sgurun) verify that the value for newly added parameter for |
| 73 // (i.e. landscape_needs_flip) is correct. | 75 // (i.e. landscape_needs_flip) is correct. |
| 74 print_settings_->SetPrinterPrintableArea(physical_size_device_units, | 76 settings.SetPrinterPrintableArea(physical_size_device_units, |
| 75 printable_area_device_units, | 77 printable_area_device_units, |
| 76 true); | 78 true); |
| 77 | 79 |
| 78 printing::PageMargins margins; | 80 printing::PageMargins margins; |
| 79 margins.left = | 81 margins.left = |
| 80 MilsToDots(Java_AwPdfExporter_getLeftMargin(env, obj), dpi); | 82 MilsToDots(Java_AwPdfExporter_getLeftMargin(env, obj), dpi); |
| 81 margins.right = | 83 margins.right = |
| 82 MilsToDots(Java_AwPdfExporter_getRightMargin(env, obj), dpi); | 84 MilsToDots(Java_AwPdfExporter_getRightMargin(env, obj), dpi); |
| 83 margins.top = | 85 margins.top = |
| 84 MilsToDots(Java_AwPdfExporter_getTopMargin(env, obj), dpi); | 86 MilsToDots(Java_AwPdfExporter_getTopMargin(env, obj), dpi); |
| 85 margins.bottom = | 87 margins.bottom = |
| 86 MilsToDots(Java_AwPdfExporter_getBottomMargin(env, obj), dpi); | 88 MilsToDots(Java_AwPdfExporter_getBottomMargin(env, obj), dpi); |
| 87 print_settings_->SetCustomMargins(margins); | 89 settings.SetCustomMargins(margins); |
| 88 print_settings_->set_should_print_backgrounds(true); | 90 settings.set_should_print_backgrounds(true); |
| 89 } | 91 } |
| 90 | 92 |
| 91 void AwPdfExporter::DidExportPdf(int fd, bool success) { | 93 void AwPdfExporter::DidExportPdf(int fd, bool success) { |
| 92 JNIEnv* env = base::android::AttachCurrentThread(); | 94 JNIEnv* env = base::android::AttachCurrentThread(); |
| 93 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); | 95 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); |
| 94 if (obj.is_null()) | 96 if (obj.is_null()) |
| 95 return; | 97 return; |
| 96 Java_AwPdfExporter_didExportPdf(env, obj.obj(), success); | 98 Java_AwPdfExporter_didExportPdf(env, obj.obj(), success); |
| 97 } | 99 } |
| 98 | 100 |
| 99 bool RegisterAwPdfExporter(JNIEnv* env) { | 101 bool RegisterAwPdfExporter(JNIEnv* env) { |
| 100 return RegisterNativesImpl(env); | 102 return RegisterNativesImpl(env); |
| 101 } | 103 } |
| 102 | 104 |
| 103 } // namespace android_webview | 105 } // namespace android_webview |
| OLD | NEW |