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 "base/android/jni_array.h" | |
9 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
10 #include "jni/AwPdfExporter_jni.h" | 11 #include "jni/AwPdfExporter_jni.h" |
11 #include "printing/print_settings.h" | 12 #include "printing/print_settings.h" |
12 #include "printing/units.h" | 13 #include "printing/units.h" |
13 | 14 |
14 using base::android::JavaParamRef; | 15 using base::android::JavaParamRef; |
15 using base::android::JavaRef; | 16 using base::android::JavaRef; |
16 using base::android::ScopedJavaLocalRef; | 17 using base::android::ScopedJavaLocalRef; |
17 | 18 |
18 namespace android_webview { | 19 namespace android_webview { |
19 | 20 |
21 namespace { | |
22 | |
23 void GetPageRanges(JNIEnv* env, | |
24 jintArray int_arr, | |
25 printing::PageRanges* range_vector) { | |
26 std::vector<int> pages; | |
27 base::android::JavaIntArrayToIntVector(env, int_arr, &pages); | |
28 for (int page : pages) { | |
29 printing::PageRange range; | |
30 range.from = page; | |
31 range.to = page; | |
32 range_vector->push_back(range); | |
33 } | |
34 } | |
35 | |
36 } // anonymous namespace | |
Lei Zhang
2017/03/22 22:26:43
no "anonymous " - bad copy+paste from line 81?
Shimi Zhang
2017/03/23 23:21:56
Done.
| |
37 | |
20 AwPdfExporter::AwPdfExporter(JNIEnv* env, | 38 AwPdfExporter::AwPdfExporter(JNIEnv* env, |
21 const JavaRef<jobject>& obj, | 39 const JavaRef<jobject>& obj, |
22 content::WebContents* web_contents) | 40 content::WebContents* web_contents) |
23 : java_ref_(env, obj), web_contents_(web_contents) { | 41 : java_ref_(env, obj), web_contents_(web_contents) { |
24 DCHECK(!obj.is_null()); | 42 DCHECK(!obj.is_null()); |
25 Java_AwPdfExporter_setNativeAwPdfExporter( | 43 Java_AwPdfExporter_setNativeAwPdfExporter( |
26 env, obj, reinterpret_cast<intptr_t>(this)); | 44 env, obj, reinterpret_cast<intptr_t>(this)); |
27 } | 45 } |
28 | 46 |
29 AwPdfExporter::~AwPdfExporter() { | 47 AwPdfExporter::~AwPdfExporter() { |
30 JNIEnv* env = base::android::AttachCurrentThread(); | 48 JNIEnv* env = base::android::AttachCurrentThread(); |
31 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); | 49 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); |
32 if (obj.is_null()) | 50 if (obj.is_null()) |
33 return; | 51 return; |
34 // Clear the Java peer's weak pointer to |this| object. | 52 // Clear the Java peer's weak pointer to |this| object. |
35 Java_AwPdfExporter_setNativeAwPdfExporter(env, obj, 0); | 53 Java_AwPdfExporter_setNativeAwPdfExporter(env, obj, 0); |
36 } | 54 } |
37 | 55 |
38 void AwPdfExporter::ExportToPdf(JNIEnv* env, | 56 void AwPdfExporter::ExportToPdf(JNIEnv* env, |
39 const JavaParamRef<jobject>& obj, | 57 const JavaParamRef<jobject>& obj, |
40 int fd, | 58 int fd, |
59 jintArray pages, | |
41 const JavaParamRef<jobject>& cancel_signal) { | 60 const JavaParamRef<jobject>& cancel_signal) { |
42 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 61 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
43 printing::PrintSettings print_settings; | 62 printing::PrintSettings print_settings; |
44 InitPdfSettings(env, obj, print_settings); | 63 printing::PageRanges page_ranges; |
64 GetPageRanges(env, pages, &page_ranges); | |
65 InitPdfSettings(env, obj, page_ranges, print_settings); | |
45 AwPrintManager* print_manager = | 66 AwPrintManager* print_manager = |
46 AwPrintManager::CreateForWebContents( | 67 AwPrintManager::CreateForWebContents( |
47 web_contents_, print_settings, base::FileDescriptor(fd, false), | 68 web_contents_, print_settings, base::FileDescriptor(fd, false), |
48 base::Bind(&AwPdfExporter::DidExportPdf, base::Unretained(this))); | 69 base::Bind(&AwPdfExporter::DidExportPdf, base::Unretained(this))); |
49 | 70 |
50 if (!print_manager->PrintNow()) | 71 if (!print_manager->PrintNow()) |
51 DidExportPdf(fd, false); | 72 DidExportPdf(fd, false); |
52 } | 73 } |
53 | 74 |
54 namespace { | 75 namespace { |
55 // Converts from 1/1000 of inches to device units using DPI. | 76 // Converts from 1/1000 of inches to device units using DPI. |
56 int MilsToDots(int val, int dpi) { | 77 int MilsToDots(int val, int dpi) { |
57 return static_cast<int>(printing::ConvertUnitDouble(val, 1000.0, dpi)); | 78 return static_cast<int>(printing::ConvertUnitDouble(val, 1000.0, dpi)); |
58 } | 79 } |
80 | |
59 } // anonymous namespace | 81 } // anonymous namespace |
60 | 82 |
61 void AwPdfExporter::InitPdfSettings(JNIEnv* env, | 83 void AwPdfExporter::InitPdfSettings(JNIEnv* env, |
62 const JavaRef<jobject>& obj, | 84 const JavaRef<jobject>& obj, |
85 const printing::PageRanges& page_ranges, | |
63 printing::PrintSettings& settings) { | 86 printing::PrintSettings& settings) { |
64 int dpi = Java_AwPdfExporter_getDpi(env, obj); | 87 int dpi = Java_AwPdfExporter_getDpi(env, obj); |
65 int width = Java_AwPdfExporter_getPageWidth(env, obj); | 88 int width = Java_AwPdfExporter_getPageWidth(env, obj); |
66 int height = Java_AwPdfExporter_getPageHeight(env, obj); | 89 int height = Java_AwPdfExporter_getPageHeight(env, obj); |
67 gfx::Size physical_size_device_units; | 90 gfx::Size physical_size_device_units; |
68 int width_in_dots = MilsToDots(width, dpi); | 91 int width_in_dots = MilsToDots(width, dpi); |
69 int height_in_dots = MilsToDots(height, dpi); | 92 int height_in_dots = MilsToDots(height, dpi); |
70 physical_size_device_units.SetSize(width_in_dots, height_in_dots); | 93 physical_size_device_units.SetSize(width_in_dots, height_in_dots); |
71 | 94 |
72 gfx::Rect printable_area_device_units; | 95 gfx::Rect printable_area_device_units; |
73 // Assume full page is printable for now. | 96 // Assume full page is printable for now. |
74 printable_area_device_units.SetRect(0, 0, width_in_dots, height_in_dots); | 97 printable_area_device_units.SetRect(0, 0, width_in_dots, height_in_dots); |
75 | 98 |
99 if (page_ranges.size() > 0) | |
Lei Zhang
2017/03/22 22:26:43
!page_range.empty()
Shimi Zhang
2017/03/23 23:21:56
Done.
| |
100 settings.set_ranges(page_ranges); | |
101 | |
76 settings.set_dpi(dpi); | 102 settings.set_dpi(dpi); |
77 // TODO(sgurun) verify that the value for newly added parameter for | 103 // TODO(sgurun) verify that the value for newly added parameter for |
78 // (i.e. landscape_needs_flip) is correct. | 104 // (i.e. landscape_needs_flip) is correct. |
79 settings.SetPrinterPrintableArea(physical_size_device_units, | 105 settings.SetPrinterPrintableArea(physical_size_device_units, |
80 printable_area_device_units, | 106 printable_area_device_units, |
81 true); | 107 true); |
82 | 108 |
83 printing::PageMargins margins; | 109 printing::PageMargins margins; |
84 margins.left = | 110 margins.left = |
85 MilsToDots(Java_AwPdfExporter_getLeftMargin(env, obj), dpi); | 111 MilsToDots(Java_AwPdfExporter_getLeftMargin(env, obj), dpi); |
(...skipping 13 matching lines...) Expand all Loading... | |
99 if (obj.is_null()) | 125 if (obj.is_null()) |
100 return; | 126 return; |
101 Java_AwPdfExporter_didExportPdf(env, obj, success); | 127 Java_AwPdfExporter_didExportPdf(env, obj, success); |
102 } | 128 } |
103 | 129 |
104 bool RegisterAwPdfExporter(JNIEnv* env) { | 130 bool RegisterAwPdfExporter(JNIEnv* env) { |
105 return RegisterNativesImpl(env); | 131 return RegisterNativesImpl(env); |
106 } | 132 } |
107 | 133 |
108 } // namespace android_webview | 134 } // namespace android_webview |
OLD | NEW |