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 "printing/printing_context_android.h" | 5 #include "printing/printing_context_android.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/android/jni_android.h" | 11 #include "base/android/jni_android.h" |
12 #include "base/android/jni_array.h" | 12 #include "base/android/jni_array.h" |
13 #include "base/android/jni_string.h" | 13 #include "base/android/jni_string.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
16 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
17 #include "base/values.h" | 17 #include "base/values.h" |
18 #include "jni/PrintingContext_jni.h" | 18 #include "jni/PrintingContext_jni.h" |
19 #include "printing/metafile.h" | 19 #include "printing/metafile.h" |
20 #include "printing/print_job_constants.h" | 20 #include "printing/print_job_constants.h" |
21 #include "printing/units.h" | 21 #include "printing/units.h" |
22 #include "third_party/icu/source/i18n/unicode/ulocdata.h" | 22 #include "third_party/icu/source/i18n/unicode/ulocdata.h" |
23 | 23 |
24 using base::android::JavaParamRef; | 24 using base::android::JavaParamRef; |
25 using base::android::ScopedJavaLocalRef; | 25 using base::android::ScopedJavaLocalRef; |
26 | 26 |
| 27 namespace printing { |
| 28 |
27 namespace { | 29 namespace { |
28 | 30 |
29 // 1 inch in mils. | 31 // 1 inch in mils. |
30 const int kInchToMil = 1000; | 32 const int kInchToMil = 1000; |
31 | 33 |
32 inline int Round(double x) { | 34 int Round(double x) { |
33 return static_cast<int>(x + 0.5); | 35 return static_cast<int>(x + 0.5); |
34 } | 36 } |
35 | 37 |
36 // Sets the page sizes for a |PrintSettings| object. |width| and |height| | 38 // Sets the page sizes for a |PrintSettings| object. |width| and |height| |
37 // arguments should be in device units. | 39 // arguments should be in device units. |
38 void SetSizes( | 40 void SetSizes(PrintSettings* settings, int dpi, int width, int height) { |
39 printing::PrintSettings* settings, int dpi, int width, int height) { | |
40 gfx::Size physical_size_device_units(width, height); | 41 gfx::Size physical_size_device_units(width, height); |
41 // Assume full page is printable for now. | 42 // Assume full page is printable for now. |
42 gfx::Rect printable_area_device_units(0, 0, width, height); | 43 gfx::Rect printable_area_device_units(0, 0, width, height); |
43 | 44 |
44 settings->set_dpi(dpi); | 45 settings->set_dpi(dpi); |
45 settings->SetPrinterPrintableArea(physical_size_device_units, | 46 settings->SetPrinterPrintableArea(physical_size_device_units, |
46 printable_area_device_units, | 47 printable_area_device_units, |
47 false); | 48 false); |
48 } | 49 } |
49 | 50 |
50 void GetPageRanges(JNIEnv* env, | 51 void GetPageRanges(JNIEnv* env, jintArray int_arr, PageRanges* range_vector) { |
51 jintArray int_arr, | |
52 printing::PageRanges& range_vector) { | |
53 std::vector<int> pages; | 52 std::vector<int> pages; |
54 base::android::JavaIntArrayToIntVector(env, int_arr, &pages); | 53 base::android::JavaIntArrayToIntVector(env, int_arr, &pages); |
55 for (std::vector<int>::const_iterator it = pages.begin(); | 54 for (int page : pages) { |
56 it != pages.end(); | 55 PageRange range; |
57 ++it) { | 56 range.from = page; |
58 printing::PageRange range; | 57 range.to = page; |
59 range.from = *it; | 58 range_vector->push_back(range); |
60 range.to = *it; | |
61 range_vector.push_back(range); | |
62 } | 59 } |
63 } | 60 } |
64 | 61 |
65 } // namespace | 62 } // namespace |
66 | 63 |
67 namespace printing { | |
68 | |
69 // static | 64 // static |
70 std::unique_ptr<PrintingContext> PrintingContext::Create(Delegate* delegate) { | 65 std::unique_ptr<PrintingContext> PrintingContext::Create(Delegate* delegate) { |
71 return base::WrapUnique(new PrintingContextAndroid(delegate)); | 66 return base::WrapUnique(new PrintingContextAndroid(delegate)); |
72 } | 67 } |
73 | 68 |
74 // static | 69 // static |
75 void PrintingContextAndroid::PdfWritingDone(int fd, bool success) { | 70 void PrintingContextAndroid::PdfWritingDone(int fd, bool success) { |
76 JNIEnv* env = base::android::AttachCurrentThread(); | 71 JNIEnv* env = base::android::AttachCurrentThread(); |
77 Java_PrintingContext_pdfWritingDone(env, fd, success); | 72 Java_PrintingContext_pdfWritingDone(env, fd, success); |
78 } | 73 } |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 } | 114 } |
120 | 115 |
121 // We use device name variable to store the file descriptor. This is hacky | 116 // We use device name variable to store the file descriptor. This is hacky |
122 // but necessary. Since device name is not necessary for the upstream | 117 // but necessary. Since device name is not necessary for the upstream |
123 // printing code for Android, this is harmless. | 118 // printing code for Android, this is harmless. |
124 int fd = Java_PrintingContext_getFileDescriptor(env, j_printing_context_); | 119 int fd = Java_PrintingContext_getFileDescriptor(env, j_printing_context_); |
125 settings_.set_device_name(base::IntToString16(fd)); | 120 settings_.set_device_name(base::IntToString16(fd)); |
126 | 121 |
127 ScopedJavaLocalRef<jintArray> intArr = | 122 ScopedJavaLocalRef<jintArray> intArr = |
128 Java_PrintingContext_getPages(env, j_printing_context_); | 123 Java_PrintingContext_getPages(env, j_printing_context_); |
129 if (intArr.obj() != NULL) { | 124 if (intArr.obj()) { |
130 PageRanges range_vector; | 125 PageRanges range_vector; |
131 GetPageRanges(env, intArr.obj(), range_vector); | 126 GetPageRanges(env, intArr.obj(), &range_vector); |
132 settings_.set_ranges(range_vector); | 127 settings_.set_ranges(range_vector); |
133 } | 128 } |
134 | 129 |
135 int dpi = Java_PrintingContext_getDpi(env, j_printing_context_); | 130 int dpi = Java_PrintingContext_getDpi(env, j_printing_context_); |
136 int width = Java_PrintingContext_getWidth(env, j_printing_context_); | 131 int width = Java_PrintingContext_getWidth(env, j_printing_context_); |
137 int height = Java_PrintingContext_getHeight(env, j_printing_context_); | 132 int height = Java_PrintingContext_getHeight(env, j_printing_context_); |
138 width = Round(ConvertUnitDouble(width, kInchToMil, 1.0) * dpi); | 133 width = Round(ConvertUnitDouble(width, kInchToMil, 1.0) * dpi); |
139 height = Round(ConvertUnitDouble(height, kInchToMil, 1.0) * dpi); | 134 height = Round(ConvertUnitDouble(height, kInchToMil, 1.0) * dpi); |
140 SetSizes(&settings_, dpi, width, height); | 135 SetSizes(&settings_, dpi, width, height); |
141 | 136 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 bool show_system_dialog, | 186 bool show_system_dialog, |
192 int page_count) { | 187 int page_count) { |
193 DCHECK(!show_system_dialog); | 188 DCHECK(!show_system_dialog); |
194 DCHECK(!in_print_job_); | 189 DCHECK(!in_print_job_); |
195 | 190 |
196 // Intentional No-op. | 191 // Intentional No-op. |
197 | 192 |
198 return OK; | 193 return OK; |
199 } | 194 } |
200 | 195 |
201 PrintingContext::Result PrintingContextAndroid::InitWithSettings( | |
202 const PrintSettings& settings) { | |
203 DCHECK(!in_print_job_); | |
204 | |
205 settings_ = settings; | |
206 | |
207 return OK; | |
208 } | |
209 | |
210 PrintingContext::Result PrintingContextAndroid::NewDocument( | 196 PrintingContext::Result PrintingContextAndroid::NewDocument( |
211 const base::string16& document_name) { | 197 const base::string16& document_name) { |
212 DCHECK(!in_print_job_); | 198 DCHECK(!in_print_job_); |
213 in_print_job_ = true; | 199 in_print_job_ = true; |
214 | 200 |
215 return OK; | 201 return OK; |
216 } | 202 } |
217 | 203 |
218 PrintingContext::Result PrintingContextAndroid::NewPage() { | 204 PrintingContext::Result PrintingContextAndroid::NewPage() { |
219 if (abort_printing_) | 205 if (abort_printing_) |
(...skipping 28 matching lines...) Expand all Loading... |
248 abort_printing_ = true; | 234 abort_printing_ = true; |
249 in_print_job_ = false; | 235 in_print_job_ = false; |
250 } | 236 } |
251 | 237 |
252 void PrintingContextAndroid::ReleaseContext() { | 238 void PrintingContextAndroid::ReleaseContext() { |
253 // Intentional No-op. | 239 // Intentional No-op. |
254 } | 240 } |
255 | 241 |
256 gfx::NativeDrawingContext PrintingContextAndroid::context() const { | 242 gfx::NativeDrawingContext PrintingContextAndroid::context() const { |
257 // Intentional No-op. | 243 // Intentional No-op. |
258 return NULL; | 244 return nullptr; |
259 } | 245 } |
260 | 246 |
261 // static | 247 // static |
262 bool PrintingContextAndroid::RegisterPrintingContext(JNIEnv* env) { | 248 bool PrintingContextAndroid::RegisterPrintingContext(JNIEnv* env) { |
263 return RegisterNativesImpl(env); | 249 return RegisterNativesImpl(env); |
264 } | 250 } |
265 | 251 |
266 } // namespace printing | 252 } // namespace printing |
OLD | NEW |