| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/print_settings_conversion.h" | 5 #include "printing/print_settings_conversion.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <cmath> | 10 #include <cmath> |
| 11 #include <memory> | 11 #include <memory> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <utility> | 13 #include <utility> |
| 14 | 14 |
| 15 #include "base/memory/ptr_util.h" |
| 15 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 17 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 18 #include "base/values.h" | 19 #include "base/values.h" |
| 19 #include "printing/page_size_margins.h" | 20 #include "printing/page_size_margins.h" |
| 20 #include "printing/print_job_constants.h" | 21 #include "printing/print_job_constants.h" |
| 21 #include "printing/print_settings.h" | 22 #include "printing/print_settings.h" |
| 22 #include "printing/units.h" | 23 #include "printing/units.h" |
| 23 | 24 |
| 24 namespace printing { | 25 namespace printing { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 37 &page_size_margins->margin_left) || | 38 &page_size_margins->margin_left) || |
| 38 !custom_margins->GetDouble(kSettingMarginRight, | 39 !custom_margins->GetDouble(kSettingMarginRight, |
| 39 &page_size_margins->margin_right)) { | 40 &page_size_margins->margin_right)) { |
| 40 NOTREACHED(); | 41 NOTREACHED(); |
| 41 } | 42 } |
| 42 } | 43 } |
| 43 | 44 |
| 44 void SetMarginsToJobSettings(const std::string& json_path, | 45 void SetMarginsToJobSettings(const std::string& json_path, |
| 45 const PageMargins& margins, | 46 const PageMargins& margins, |
| 46 base::DictionaryValue* job_settings) { | 47 base::DictionaryValue* job_settings) { |
| 47 base::DictionaryValue* dict = new base::DictionaryValue; | 48 auto dict = base::MakeUnique<base::DictionaryValue>(); |
| 48 job_settings->Set(json_path, dict); | |
| 49 dict->SetInteger(kSettingMarginTop, margins.top); | 49 dict->SetInteger(kSettingMarginTop, margins.top); |
| 50 dict->SetInteger(kSettingMarginBottom, margins.bottom); | 50 dict->SetInteger(kSettingMarginBottom, margins.bottom); |
| 51 dict->SetInteger(kSettingMarginLeft, margins.left); | 51 dict->SetInteger(kSettingMarginLeft, margins.left); |
| 52 dict->SetInteger(kSettingMarginRight, margins.right); | 52 dict->SetInteger(kSettingMarginRight, margins.right); |
| 53 job_settings->Set(json_path, std::move(dict)); |
| 53 } | 54 } |
| 54 | 55 |
| 55 void SetSizeToJobSettings(const std::string& json_path, | 56 void SetSizeToJobSettings(const std::string& json_path, |
| 56 const gfx::Size& size, | 57 const gfx::Size& size, |
| 57 base::DictionaryValue* job_settings) { | 58 base::DictionaryValue* job_settings) { |
| 58 base::DictionaryValue* dict = new base::DictionaryValue; | 59 auto dict = base::MakeUnique<base::DictionaryValue>(); |
| 59 job_settings->Set(json_path, dict); | |
| 60 dict->SetInteger("width", size.width()); | 60 dict->SetInteger("width", size.width()); |
| 61 dict->SetInteger("height", size.height()); | 61 dict->SetInteger("height", size.height()); |
| 62 job_settings->Set(json_path, std::move(dict)); |
| 62 } | 63 } |
| 63 | 64 |
| 64 void SetRectToJobSettings(const std::string& json_path, | 65 void SetRectToJobSettings(const std::string& json_path, |
| 65 const gfx::Rect& rect, | 66 const gfx::Rect& rect, |
| 66 base::DictionaryValue* job_settings) { | 67 base::DictionaryValue* job_settings) { |
| 67 base::DictionaryValue* dict = new base::DictionaryValue; | 68 auto dict = base::MakeUnique<base::DictionaryValue>(); |
| 68 job_settings->Set(json_path, dict); | |
| 69 dict->SetInteger("x", rect.x()); | 69 dict->SetInteger("x", rect.x()); |
| 70 dict->SetInteger("y", rect.y()); | 70 dict->SetInteger("y", rect.y()); |
| 71 dict->SetInteger("width", rect.width()); | 71 dict->SetInteger("width", rect.width()); |
| 72 dict->SetInteger("height", rect.height()); | 72 dict->SetInteger("height", rect.height()); |
| 73 job_settings->Set(json_path, std::move(dict)); |
| 73 } | 74 } |
| 74 | 75 |
| 75 } // namespace | 76 } // namespace |
| 76 | 77 |
| 77 bool PrintSettingsFromJobSettings(const base::DictionaryValue& job_settings, | 78 bool PrintSettingsFromJobSettings(const base::DictionaryValue& job_settings, |
| 78 PrintSettings* settings) { | 79 PrintSettings* settings) { |
| 79 bool display_header_footer = false; | 80 bool display_header_footer = false; |
| 80 if (!job_settings.GetBoolean(kSettingHeaderFooterEnabled, | 81 if (!job_settings.GetBoolean(kSettingHeaderFooterEnabled, |
| 81 &display_header_footer)) { | 82 &display_header_footer)) { |
| 82 return false; | 83 return false; |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 222 job_settings->SetBoolean(kSettingHeaderFooterEnabled, | 223 job_settings->SetBoolean(kSettingHeaderFooterEnabled, |
| 223 settings.display_header_footer()); | 224 settings.display_header_footer()); |
| 224 job_settings->SetString(kSettingHeaderFooterTitle, settings.title()); | 225 job_settings->SetString(kSettingHeaderFooterTitle, settings.title()); |
| 225 job_settings->SetString(kSettingHeaderFooterURL, settings.url()); | 226 job_settings->SetString(kSettingHeaderFooterURL, settings.url()); |
| 226 job_settings->SetBoolean(kSettingShouldPrintBackgrounds, | 227 job_settings->SetBoolean(kSettingShouldPrintBackgrounds, |
| 227 settings.should_print_backgrounds()); | 228 settings.should_print_backgrounds()); |
| 228 job_settings->SetBoolean(kSettingShouldPrintSelectionOnly, | 229 job_settings->SetBoolean(kSettingShouldPrintSelectionOnly, |
| 229 settings.selection_only()); | 230 settings.selection_only()); |
| 230 job_settings->SetInteger(kSettingMarginsType, settings.margin_type()); | 231 job_settings->SetInteger(kSettingMarginsType, settings.margin_type()); |
| 231 if (!settings.ranges().empty()) { | 232 if (!settings.ranges().empty()) { |
| 232 base::ListValue* page_range_array = new base::ListValue; | 233 auto page_range_array = base::MakeUnique<base::ListValue>(); |
| 233 job_settings->Set(kSettingPageRange, page_range_array); | |
| 234 for (size_t i = 0; i < settings.ranges().size(); ++i) { | 234 for (size_t i = 0; i < settings.ranges().size(); ++i) { |
| 235 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue); | 235 auto dict = base::MakeUnique<base::DictionaryValue>(); |
| 236 dict->SetInteger(kSettingPageRangeFrom, settings.ranges()[i].from + 1); | 236 dict->SetInteger(kSettingPageRangeFrom, settings.ranges()[i].from + 1); |
| 237 dict->SetInteger(kSettingPageRangeTo, settings.ranges()[i].to + 1); | 237 dict->SetInteger(kSettingPageRangeTo, settings.ranges()[i].to + 1); |
| 238 page_range_array->Append(std::move(dict)); | 238 page_range_array->Append(std::move(dict)); |
| 239 } | 239 } |
| 240 job_settings->Set(kSettingPageRange, std::move(page_range_array)); |
| 240 } | 241 } |
| 241 | 242 |
| 242 job_settings->SetBoolean(kSettingCollate, settings.collate()); | 243 job_settings->SetBoolean(kSettingCollate, settings.collate()); |
| 243 job_settings->SetInteger(kSettingCopies, settings.copies()); | 244 job_settings->SetInteger(kSettingCopies, settings.copies()); |
| 244 job_settings->SetInteger(kSettingColor, settings.color()); | 245 job_settings->SetInteger(kSettingColor, settings.color()); |
| 245 job_settings->SetInteger(kSettingDuplexMode, settings.duplex_mode()); | 246 job_settings->SetInteger(kSettingDuplexMode, settings.duplex_mode()); |
| 246 job_settings->SetBoolean(kSettingLandscape, settings.landscape()); | 247 job_settings->SetBoolean(kSettingLandscape, settings.landscape()); |
| 247 job_settings->SetString(kSettingDeviceName, settings.device_name()); | 248 job_settings->SetString(kSettingDeviceName, settings.device_name()); |
| 248 | 249 |
| 249 // Following values are not read form JSON by InitSettings, so do not have | 250 // Following values are not read form JSON by InitSettings, so do not have |
| 250 // common public constants. So just serialize in "debug" section. | 251 // common public constants. So just serialize in "debug" section. |
| 251 base::DictionaryValue* debug = new base::DictionaryValue; | 252 auto debug = base::MakeUnique<base::DictionaryValue>(); |
| 252 job_settings->Set("debug", debug); | |
| 253 debug->SetInteger("dpi", settings.dpi()); | 253 debug->SetInteger("dpi", settings.dpi()); |
| 254 debug->SetInteger("deviceUnitsPerInch", settings.device_units_per_inch()); | 254 debug->SetInteger("deviceUnitsPerInch", settings.device_units_per_inch()); |
| 255 debug->SetBoolean("support_alpha_blend", settings.should_print_backgrounds()); | 255 debug->SetBoolean("support_alpha_blend", settings.should_print_backgrounds()); |
| 256 debug->SetString("media_vendor_id", settings.requested_media().vendor_id); | 256 debug->SetString("media_vendor_id", settings.requested_media().vendor_id); |
| 257 SetSizeToJobSettings( | 257 SetSizeToJobSettings("media_size", settings.requested_media().size_microns, |
| 258 "media_size", settings.requested_media().size_microns, debug); | 258 debug.get()); |
| 259 SetMarginsToJobSettings("requested_custom_margins_in_points", | 259 SetMarginsToJobSettings("requested_custom_margins_in_points", |
| 260 settings.requested_custom_margins_in_points(), | 260 settings.requested_custom_margins_in_points(), |
| 261 debug); | 261 debug.get()); |
| 262 const PageSetup& page_setup = settings.page_setup_device_units(); | 262 const PageSetup& page_setup = settings.page_setup_device_units(); |
| 263 SetMarginsToJobSettings( | 263 SetMarginsToJobSettings("effective_margins", page_setup.effective_margins(), |
| 264 "effective_margins", page_setup.effective_margins(), debug); | 264 debug.get()); |
| 265 SetSizeToJobSettings("physical_size", page_setup.physical_size(), debug); | 265 SetSizeToJobSettings("physical_size", page_setup.physical_size(), |
| 266 SetRectToJobSettings("overlay_area", page_setup.overlay_area(), debug); | 266 debug.get()); |
| 267 SetRectToJobSettings("content_area", page_setup.content_area(), debug); | 267 SetRectToJobSettings("overlay_area", page_setup.overlay_area(), debug.get()); |
| 268 SetRectToJobSettings("printable_area", page_setup.printable_area(), debug); | 268 SetRectToJobSettings("content_area", page_setup.content_area(), debug.get()); |
| 269 SetRectToJobSettings("printable_area", page_setup.printable_area(), |
| 270 debug.get()); |
| 271 job_settings->Set("debug", std::move(debug)); |
| 269 } | 272 } |
| 270 | 273 |
| 271 } // namespace printing | 274 } // namespace printing |
| OLD | NEW |