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 "base/android/jni_android.h"
8 #include "base/logging.h"
9 #include "content/public/browser/browser_thread.h"
10 #include "content/public/browser/web_contents.h"
11 #include "jni/AwPdfExporter_jni.h"
12 #include "printing/print_settings.h"
13 #include "printing/units.h"
15 using base::android::AttachCurrentThread
;
16 using base::android::ScopedJavaGlobalRef
;
17 using content::BrowserThread
;
18 using content::WebContents
;
19 using printing::ConvertUnitDouble
;
20 using printing::PageMargins
;
21 using printing::PrintSettings
;
23 namespace android_webview
{
25 AwPdfExporter::AwPdfExporter(JNIEnv
* env
,
27 WebContents
* web_contents
)
28 : java_ref_(env
, obj
),
29 web_contents_(web_contents
) {
31 Java_AwPdfExporter_setNativeAwPdfExporter(
32 env
, obj
, reinterpret_cast<intptr_t>(this));
35 AwPdfExporter::~AwPdfExporter() {
36 JNIEnv
* env
= AttachCurrentThread();
37 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
40 // Clear the Java peer's weak pointer to |this| object.
41 Java_AwPdfExporter_setNativeAwPdfExporter(env
, obj
.obj(), 0);
44 void AwPdfExporter::ExportToPdf(JNIEnv
* env
,
47 jobject cancel_signal
) {
48 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
49 CreatePdfSettings(env
, obj
);
50 PrintManager
* print_manager
=
51 PrintManager::CreateForWebContents(
52 web_contents_
, print_settings_
.get(), fd
, this);
53 if (!print_manager
->PrintNow())
58 // Converts from 1/1000 of inches to device units using DPI.
59 int MilsToDots(int val
, int dpi
) {
60 return static_cast<int>(ConvertUnitDouble(val
, 1000.0, dpi
));
63 } // anonymous namespace
65 void AwPdfExporter::CreatePdfSettings(JNIEnv
* env
, jobject obj
) {
66 print_settings_
.reset(new PrintSettings
);
67 int dpi
= Java_AwPdfExporter_getDpi(env
, obj
);
68 int width
= Java_AwPdfExporter_getPageWidth(env
, obj
);
69 int height
= Java_AwPdfExporter_getPageHeight(env
, obj
);
70 gfx::Size physical_size_device_units
;
71 int width_in_dots
= MilsToDots(width
, dpi
);
72 int height_in_dots
= MilsToDots(height
, dpi
);
73 physical_size_device_units
.SetSize(width_in_dots
, height_in_dots
);
75 gfx::Rect printable_area_device_units
;
76 // Assume full page is printable for now.
77 printable_area_device_units
.SetRect(0, 0, width_in_dots
, height_in_dots
);
79 print_settings_
->set_dpi(dpi
);
80 // TODO(sgurun) verify that the value for newly added parameter for
81 // (i.e. landscape_needs_flip) is correct.
82 print_settings_
->SetPrinterPrintableArea(physical_size_device_units
,
83 printable_area_device_units
,
88 MilsToDots(Java_AwPdfExporter_getLeftMargin(env
, obj
), dpi
);
90 MilsToDots(Java_AwPdfExporter_getRightMargin(env
, obj
), dpi
);
92 MilsToDots(Java_AwPdfExporter_getTopMargin(env
, obj
), dpi
);
94 MilsToDots(Java_AwPdfExporter_getBottomMargin(env
, obj
), dpi
);
95 print_settings_
->SetCustomMargins(margins
);
96 print_settings_
->set_should_print_backgrounds(true);
99 void AwPdfExporter::DidExportPdf(bool success
) {
100 JNIEnv
* env
= AttachCurrentThread();
101 ScopedJavaLocalRef
<jobject
> obj
= java_ref_
.get(env
);
104 Java_AwPdfExporter_didExportPdf(env
, obj
.obj(), success
);
107 bool AwPdfExporter::IsCancelled() {
108 // TODO(sgurun) implement. Needs connecting with the |cancel_signal| passed
109 // in the constructor.
113 bool RegisterAwPdfExporter(JNIEnv
* env
) {
114 return RegisterNativesImpl(env
);
117 } // namespace android_webview