Home | History | Annotate | Download | only in printing
      1 // Copyright (c) 2011 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 "printing/printing_context.h"
      6 
      7 #include "base/logging.h"
      8 #include "base/values.h"
      9 #include "printing/page_setup.h"
     10 #include "printing/page_size_margins.h"
     11 #include "printing/print_settings_initializer.h"
     12 #include "printing/units.h"
     13 
     14 namespace printing {
     15 
     16 namespace {
     17 const float kCloudPrintMarginInch = 0.25;
     18 }
     19 
     20 PrintingContext::PrintingContext(const std::string& app_locale)
     21     : dialog_box_dismissed_(false),
     22       in_print_job_(false),
     23       abort_printing_(false),
     24       app_locale_(app_locale) {
     25 }
     26 
     27 PrintingContext::~PrintingContext() {
     28 }
     29 
     30 void PrintingContext::set_margin_type(MarginType type) {
     31   DCHECK(type != CUSTOM_MARGINS);
     32   settings_.set_margin_type(type);
     33 }
     34 
     35 void PrintingContext::ResetSettings() {
     36   ReleaseContext();
     37 
     38   settings_.Clear();
     39 
     40   in_print_job_ = false;
     41   dialog_box_dismissed_ = false;
     42   abort_printing_ = false;
     43 }
     44 
     45 PrintingContext::Result PrintingContext::OnError() {
     46   ResetSettings();
     47   return abort_printing_ ? CANCEL : FAILED;
     48 }
     49 
     50 PrintingContext::Result PrintingContext::UpdatePrintSettings(
     51     const base::DictionaryValue& job_settings,
     52     const PageRanges& ranges) {
     53   ResetSettings();
     54 
     55   if (!PrintSettingsInitializer::InitSettings(job_settings, ranges,
     56                                               &settings_)) {
     57     NOTREACHED();
     58     return OnError();
     59   }
     60 
     61   bool print_to_pdf = false;
     62   bool is_cloud_dialog = false;
     63   bool print_with_privet = false;
     64 
     65   if (!job_settings.GetBoolean(kSettingPrintToPDF, &print_to_pdf) ||
     66       !job_settings.GetBoolean(kSettingCloudPrintDialog, &is_cloud_dialog) ||
     67       !job_settings.GetBoolean(kSettingPrintWithPrivet, &print_with_privet)) {
     68     NOTREACHED();
     69     return OnError();
     70   }
     71 
     72   bool print_to_cloud = job_settings.HasKey(kSettingCloudPrintId);
     73   bool open_in_external_preview =
     74       job_settings.HasKey(kSettingOpenPDFInPreview);
     75 
     76   if (!open_in_external_preview && (print_to_pdf || print_to_cloud ||
     77                                     is_cloud_dialog || print_with_privet)) {
     78     settings_.set_dpi(kDefaultPdfDpi);
     79     // Cloud print should get size and rect from capabilities received from
     80     // server.
     81     gfx::Size paper_size(GetPdfPaperSizeDeviceUnits());
     82     gfx::Rect paper_rect(0, 0, paper_size.width(), paper_size.height());
     83     if (print_to_cloud || print_with_privet) {
     84       paper_rect.Inset(
     85           kCloudPrintMarginInch * settings_.device_units_per_inch(),
     86           kCloudPrintMarginInch * settings_.device_units_per_inch());
     87     }
     88     DCHECK_EQ(settings_.device_units_per_inch(), kDefaultPdfDpi);
     89     settings_.SetPrinterPrintableArea(paper_size, paper_rect, true);
     90     return OK;
     91   }
     92 
     93   return UpdatePrinterSettings(open_in_external_preview);
     94 }
     95 
     96 }  // namespace printing
     97