Roll src/third_party/WebKit 06cb9e9:a978ee5 (svn 202558:202559)
[chromium-blink-merge.git] / android_webview / native / aw_pdf_exporter.cc
blob48fa9f991fbf6d830ff74121f6a168b39de43f31
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.
5 #include "android_webview/native/aw_pdf_exporter.h"
7 #include "android_webview/browser/aw_print_manager.h"
8 #include "base/android/jni_android.h"
9 #include "content/public/browser/browser_thread.h"
10 #include "jni/AwPdfExporter_jni.h"
11 #include "printing/print_settings.h"
12 #include "printing/units.h"
14 namespace android_webview {
16 AwPdfExporter::AwPdfExporter(JNIEnv* env,
17 jobject obj,
18 content::WebContents* web_contents)
19 : java_ref_(env, obj),
20 web_contents_(web_contents) {
21 DCHECK(obj);
22 Java_AwPdfExporter_setNativeAwPdfExporter(
23 env, obj, reinterpret_cast<intptr_t>(this));
26 AwPdfExporter::~AwPdfExporter() {
27 JNIEnv* env = base::android::AttachCurrentThread();
28 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
29 if (obj.is_null())
30 return;
31 // Clear the Java peer's weak pointer to |this| object.
32 Java_AwPdfExporter_setNativeAwPdfExporter(env, obj.obj(), 0);
35 void AwPdfExporter::ExportToPdf(JNIEnv* env,
36 jobject obj,
37 int fd,
38 jobject cancel_signal) {
39 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
40 printing::PrintSettings print_settings;
41 InitPdfSettings(env, obj, print_settings);
42 AwPrintManager* print_manager =
43 AwPrintManager::CreateForWebContents(
44 web_contents_, print_settings, base::FileDescriptor(fd, false),
45 base::Bind(&AwPdfExporter::DidExportPdf, base::Unretained(this)));
47 if (!print_manager->PrintNow())
48 DidExportPdf(fd, false);
51 namespace {
52 // Converts from 1/1000 of inches to device units using DPI.
53 int MilsToDots(int val, int dpi) {
54 return static_cast<int>(printing::ConvertUnitDouble(val, 1000.0, dpi));
56 } // anonymous namespace
58 void AwPdfExporter::InitPdfSettings(JNIEnv* env,
59 jobject obj,
60 printing::PrintSettings& settings) {
61 int dpi = Java_AwPdfExporter_getDpi(env, obj);
62 int width = Java_AwPdfExporter_getPageWidth(env, obj);
63 int height = Java_AwPdfExporter_getPageHeight(env, obj);
64 gfx::Size physical_size_device_units;
65 int width_in_dots = MilsToDots(width, dpi);
66 int height_in_dots = MilsToDots(height, dpi);
67 physical_size_device_units.SetSize(width_in_dots, height_in_dots);
69 gfx::Rect printable_area_device_units;
70 // Assume full page is printable for now.
71 printable_area_device_units.SetRect(0, 0, width_in_dots, height_in_dots);
73 settings.set_dpi(dpi);
74 // TODO(sgurun) verify that the value for newly added parameter for
75 // (i.e. landscape_needs_flip) is correct.
76 settings.SetPrinterPrintableArea(physical_size_device_units,
77 printable_area_device_units,
78 true);
80 printing::PageMargins margins;
81 margins.left =
82 MilsToDots(Java_AwPdfExporter_getLeftMargin(env, obj), dpi);
83 margins.right =
84 MilsToDots(Java_AwPdfExporter_getRightMargin(env, obj), dpi);
85 margins.top =
86 MilsToDots(Java_AwPdfExporter_getTopMargin(env, obj), dpi);
87 margins.bottom =
88 MilsToDots(Java_AwPdfExporter_getBottomMargin(env, obj), dpi);
89 settings.SetCustomMargins(margins);
90 settings.set_should_print_backgrounds(true);
93 void AwPdfExporter::DidExportPdf(int fd, bool success) {
94 JNIEnv* env = base::android::AttachCurrentThread();
95 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
96 if (obj.is_null())
97 return;
98 Java_AwPdfExporter_didExportPdf(env, obj.obj(), success);
101 bool RegisterAwPdfExporter(JNIEnv* env) {
102 return RegisterNativesImpl(env);
105 } // namespace android_webview