Chromium Code Reviews| 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/browser_view_renderer.h" | |
| 8 #include "android_webview/browser/renderer_host/print_manager.h" | |
| 9 #include "base/android/jni_android.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "content/public/browser/browser_thread.h" | |
| 12 #include "content/public/browser/web_contents.h" | |
| 13 #include "jni/AwPdfExporter_jni.h" | |
| 14 #include "printing/print_settings.h" | |
| 15 #include "printing/units.h" | |
| 16 | |
| 17 using base::android::AttachCurrentThread; | |
| 18 using base::android::ScopedJavaGlobalRef; | |
| 19 using content::BrowserThread; | |
| 20 using content::WebContents; | |
| 21 using printing::ConvertUnitDouble; | |
| 22 using printing::PageMargins; | |
| 23 using printing::PrintSettings; | |
| 24 | |
| 25 namespace android_webview { | |
| 26 | |
| 27 AwPdfExporter::AwPdfExporter(JNIEnv* env, | |
| 28 jobject obj, | |
| 29 BrowserViewRenderer* view_renderer, | |
| 30 WebContents* web_contents) | |
| 31 : java_ref_(env, obj), | |
| 32 view_renderer_(view_renderer), | |
| 33 web_contents_(web_contents) { | |
| 34 | |
|
joth
2013/11/21 19:38:03
spurious \n
sgurun-gerrit only
2013/11/23 01:49:11
Done.
| |
| 35 DCHECK(obj); | |
| 36 Java_AwPdfExporter_setNativeAwPdfExporter( | |
| 37 env, obj, reinterpret_cast<jint>(this)); | |
| 38 } | |
| 39 | |
| 40 AwPdfExporter::~AwPdfExporter() { | |
| 41 JNIEnv* env = AttachCurrentThread(); | |
| 42 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); | |
| 43 if (obj.is_null()) | |
| 44 return; | |
| 45 // Clear the Java peer's weak pointer to |this| object. | |
| 46 Java_AwPdfExporter_setNativeAwPdfExporter(env, obj.obj(), 0); | |
| 47 } | |
| 48 | |
| 49 void AwPdfExporter::ExportToPdf(JNIEnv* env, | |
| 50 jobject obj, | |
| 51 int fd, | |
| 52 jobject cancel_signal) { | |
| 53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 54 CreatePdfSettings(env, obj); | |
| 55 print_manager_.reset( | |
| 56 new PrintManager(web_contents_, print_settings_.get(), fd, this)); | |
| 57 if (!print_manager_->PrintNow()) | |
| 58 DidExportPdf(false); | |
| 59 } | |
| 60 | |
| 61 namespace { | |
| 62 // Converts from 1/1000 of inches to device units using DPI. | |
| 63 int MilsToDots(int val, int dpi) { | |
| 64 return static_cast<int>(ConvertUnitDouble(val, 1000.0, dpi)); | |
| 65 } | |
| 66 | |
| 67 } // anonymous namespace | |
| 68 | |
| 69 void AwPdfExporter::CreatePdfSettings(JNIEnv* env, jobject obj) { | |
| 70 print_settings_.reset(new PrintSettings); | |
| 71 int dpi = Java_AwPdfExporter_getDpi(env, obj); | |
| 72 int width = Java_AwPdfExporter_getPageWidth(env, obj); | |
| 73 int height = Java_AwPdfExporter_getPageHeight(env, obj); | |
| 74 gfx::Size physical_size_device_units; | |
| 75 int width_in_dots = MilsToDots(width, dpi); | |
| 76 int height_in_dots = MilsToDots(height, dpi); | |
| 77 physical_size_device_units.SetSize(width_in_dots, height_in_dots); | |
| 78 | |
| 79 gfx::Rect printable_area_device_units; | |
| 80 // Assume full page is printable for now. | |
| 81 printable_area_device_units.SetRect(0, 0, width_in_dots, height_in_dots); | |
| 82 | |
| 83 print_settings_->set_dpi(dpi); | |
| 84 // TODO(sgurun) verify that the value for newly added parameter for | |
| 85 // (i.e. landscape_needs_flip) is correct. | |
| 86 print_settings_->SetPrinterPrintableArea(physical_size_device_units, | |
| 87 printable_area_device_units, | |
| 88 true); | |
| 89 | |
| 90 PageMargins margins; | |
| 91 margins.left = | |
| 92 MilsToDots(Java_AwPdfExporter_getLeftMargin(env, obj), dpi); | |
| 93 margins.right = | |
| 94 MilsToDots(Java_AwPdfExporter_getRightMargin(env, obj), dpi); | |
| 95 margins.top = | |
| 96 MilsToDots(Java_AwPdfExporter_getTopMargin(env, obj), dpi); | |
| 97 margins.bottom = | |
| 98 MilsToDots(Java_AwPdfExporter_getBottomMargin(env, obj), dpi); | |
| 99 print_settings_->SetCustomMargins(margins); | |
| 100 print_settings_->set_should_print_backgrounds(true); | |
| 101 } | |
| 102 | |
| 103 void AwPdfExporter::DidExportPdf(bool success) { | |
| 104 JNIEnv* env = AttachCurrentThread(); | |
| 105 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); | |
| 106 if (obj.is_null()) | |
| 107 return; | |
| 108 Java_AwPdfExporter_didExportPdf(env, obj.obj(), success); | |
| 109 } | |
| 110 | |
| 111 bool AwPdfExporter::IsCancelled() { | |
| 112 // TODO(sgurun) implement | |
|
joth
2013/11/21 19:38:03
TODO(sgurun): implement. Needs connecting with the
sgurun-gerrit only
2013/11/23 01:49:11
Done.
| |
| 113 return false; | |
| 114 } | |
| 115 | |
| 116 bool RegisterAwPdfExporter(JNIEnv* env) { | |
| 117 return RegisterNativesImpl(env) >= 0; | |
| 118 } | |
| 119 | |
| 120 } // namespace android_webview | |
| OLD | NEW |