Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / android_webview / native / aw_pdf_exporter.cc
blobda477ce684977d0b06d08bf1d174bcf7c1b049c5
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 CreatePdfSettings(env, obj);
41 AwPrintManager* print_manager =
42 AwPrintManager::CreateForWebContents(
43 web_contents_, *print_settings_, base::FileDescriptor(fd, false),
44 base::Bind(&AwPdfExporter::DidExportPdf, base::Unretained(this)));
46 if (!print_manager->PrintNow())
47 DidExportPdf(fd, false);
50 namespace {
51 // Converts from 1/1000 of inches to device units using DPI.
52 int MilsToDots(int val, int dpi) {
53 return static_cast<int>(printing::ConvertUnitDouble(val, 1000.0, dpi));
55 } // anonymous namespace
57 void AwPdfExporter::CreatePdfSettings(JNIEnv* env, jobject obj) {
58 print_settings_.reset(new printing::PrintSettings);
59 int dpi = Java_AwPdfExporter_getDpi(env, obj);
60 int width = Java_AwPdfExporter_getPageWidth(env, obj);
61 int height = Java_AwPdfExporter_getPageHeight(env, obj);
62 gfx::Size physical_size_device_units;
63 int width_in_dots = MilsToDots(width, dpi);
64 int height_in_dots = MilsToDots(height, dpi);
65 physical_size_device_units.SetSize(width_in_dots, height_in_dots);
67 gfx::Rect printable_area_device_units;
68 // Assume full page is printable for now.
69 printable_area_device_units.SetRect(0, 0, width_in_dots, height_in_dots);
71 print_settings_->set_dpi(dpi);
72 // TODO(sgurun) verify that the value for newly added parameter for
73 // (i.e. landscape_needs_flip) is correct.
74 print_settings_->SetPrinterPrintableArea(physical_size_device_units,
75 printable_area_device_units,
76 true);
78 printing::PageMargins margins;
79 margins.left =
80 MilsToDots(Java_AwPdfExporter_getLeftMargin(env, obj), dpi);
81 margins.right =
82 MilsToDots(Java_AwPdfExporter_getRightMargin(env, obj), dpi);
83 margins.top =
84 MilsToDots(Java_AwPdfExporter_getTopMargin(env, obj), dpi);
85 margins.bottom =
86 MilsToDots(Java_AwPdfExporter_getBottomMargin(env, obj), dpi);
87 print_settings_->SetCustomMargins(margins);
88 print_settings_->set_should_print_backgrounds(true);
91 void AwPdfExporter::DidExportPdf(int fd, bool success) {
92 JNIEnv* env = base::android::AttachCurrentThread();
93 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
94 if (obj.is_null())
95 return;
96 Java_AwPdfExporter_didExportPdf(env, obj.obj(), success);
99 bool RegisterAwPdfExporter(JNIEnv* env) {
100 return RegisterNativesImpl(env);
103 } // namespace android_webview