Home | History | Annotate | Download | only in native
      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.
      4 
      5 #include "android_webview/native/aw_pdf_exporter.h"
      6 
      7 #include "android_webview/browser/browser_view_renderer.h"
      8 #include "android_webview/browser/renderer_host/print_manager.h"
      9 #include "base/android/jni_android.h"
     10 #include "base/logging.h"
     11 #include "content/public/browser/browser_thread.h"
     12 #include "content/public/browser/web_contents.h"
     13 #include "jni/AwPdfExporter_jni.h"
     14 #include "printing/print_settings.h"
     15 #include "printing/units.h"
     16 
     17 using base::android::AttachCurrentThread;
     18 using base::android::ScopedJavaGlobalRef;
     19 using content::BrowserThread;
     20 using content::WebContents;
     21 using printing::ConvertUnitDouble;
     22 using printing::PageMargins;
     23 using printing::PrintSettings;
     24 
     25 namespace android_webview {
     26 
     27 AwPdfExporter::AwPdfExporter(JNIEnv* env,
     28                              jobject obj,
     29                              BrowserViewRenderer* view_renderer,
     30                              WebContents* web_contents)
     31     : java_ref_(env, obj),
     32       view_renderer_(view_renderer),
     33       web_contents_(web_contents) {
     34   DCHECK(obj);
     35   Java_AwPdfExporter_setNativeAwPdfExporter(
     36       env, obj, reinterpret_cast<jint>(this));
     37 }
     38 
     39 AwPdfExporter::~AwPdfExporter() {
     40   JNIEnv* env = AttachCurrentThread();
     41   ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
     42   if (obj.is_null())
     43     return;
     44   // Clear the Java peer's weak pointer to |this| object.
     45   Java_AwPdfExporter_setNativeAwPdfExporter(env, obj.obj(), 0);
     46 }
     47 
     48 void AwPdfExporter::ExportToPdf(JNIEnv* env,
     49                                 jobject obj,
     50                                 int fd,
     51                                 jobject cancel_signal) {
     52   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     53   CreatePdfSettings(env, obj);
     54   print_manager_.reset(
     55       new PrintManager(web_contents_, print_settings_.get(), fd, this));
     56   if (!print_manager_->PrintNow())
     57     DidExportPdf(false);
     58 }
     59 
     60 namespace {
     61 // Converts from 1/1000 of inches to device units using DPI.
     62 int MilsToDots(int val, int dpi) {
     63   return static_cast<int>(ConvertUnitDouble(val, 1000.0, dpi));
     64 }
     65 
     66 }  // anonymous namespace
     67 
     68 void AwPdfExporter::CreatePdfSettings(JNIEnv* env, jobject obj) {
     69   print_settings_.reset(new PrintSettings);
     70   int dpi = Java_AwPdfExporter_getDpi(env, obj);
     71   int width = Java_AwPdfExporter_getPageWidth(env, obj);
     72   int height = Java_AwPdfExporter_getPageHeight(env, obj);
     73   gfx::Size physical_size_device_units;
     74   int width_in_dots = MilsToDots(width, dpi);
     75   int height_in_dots = MilsToDots(height, dpi);
     76   physical_size_device_units.SetSize(width_in_dots, height_in_dots);
     77 
     78   gfx::Rect printable_area_device_units;
     79   // Assume full page is printable for now.
     80   printable_area_device_units.SetRect(0, 0, width_in_dots, height_in_dots);
     81 
     82   print_settings_->set_dpi(dpi);
     83   // TODO(sgurun) verify that the value for newly added parameter for
     84   // (i.e. landscape_needs_flip) is correct.
     85   print_settings_->SetPrinterPrintableArea(physical_size_device_units,
     86                                            printable_area_device_units,
     87                                            true);
     88 
     89   PageMargins margins;
     90   margins.left =
     91       MilsToDots(Java_AwPdfExporter_getLeftMargin(env, obj), dpi);
     92   margins.right =
     93       MilsToDots(Java_AwPdfExporter_getRightMargin(env, obj), dpi);
     94   margins.top =
     95       MilsToDots(Java_AwPdfExporter_getTopMargin(env, obj), dpi);
     96   margins.bottom =
     97       MilsToDots(Java_AwPdfExporter_getBottomMargin(env, obj), dpi);
     98   print_settings_->SetCustomMargins(margins);
     99   print_settings_->set_should_print_backgrounds(true);
    100 }
    101 
    102 void AwPdfExporter::DidExportPdf(bool success) {
    103   JNIEnv* env = AttachCurrentThread();
    104   ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
    105   if (obj.is_null())
    106     return;
    107   Java_AwPdfExporter_didExportPdf(env, obj.obj(), success);
    108 }
    109 
    110 bool AwPdfExporter::IsCancelled() {
    111   // TODO(sgurun) implement. Needs connecting with the |cancel_signal| passed
    112   // in the constructor.
    113   return false;
    114 }
    115 
    116 bool RegisterAwPdfExporter(JNIEnv* env) {
    117   return RegisterNativesImpl(env) >= 0;
    118 }
    119 
    120 }  // namespace android_webview
    121