| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/cloud_devices/printer_description.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/json/json_reader.h" |
| 10 #include "base/json/json_writer.h" |
| 11 #include "base/strings/string_util.h" |
| 12 #include "base/values.h" |
| 13 #include "components/cloud_devices/cloud_device_description_consts.h" |
| 14 #include "components/cloud_devices/description_items_inl.h" |
| 15 |
| 16 namespace cloud_devices { |
| 17 |
| 18 namespace printer { |
| 19 |
| 20 namespace { |
| 21 |
| 22 const int32 kMaxPageNumber = 1000000; |
| 23 |
| 24 const char kSectionPrinter[] = "printer"; |
| 25 |
| 26 const char kCustomName[] = "custom_display_name"; |
| 27 const char kKeyContentType[] = "content_type"; |
| 28 const char kKeyName[] = "name"; |
| 29 const char kKeyType[] = "type"; |
| 30 const char kKeyVendorId[] = "vendor_id"; |
| 31 |
| 32 // extern is required to be used in templates. |
| 33 extern const char kOptionCollate[] = "collate"; |
| 34 extern const char kOptionColor[] = "color"; |
| 35 extern const char kOptionContentType[] = "supported_content_type"; |
| 36 extern const char kOptionCopies[] = "copies"; |
| 37 extern const char kOptionDpi[] = "dpi"; |
| 38 extern const char kOptionDuplex[] = "duplex"; |
| 39 extern const char kOptionFitToPage[] = "fit_to_page"; |
| 40 extern const char kOptionMargins[] = "margins"; |
| 41 extern const char kOptionMediaSize[] = "media_size"; |
| 42 extern const char kOptionPageOrientation[] = "page_orientation"; |
| 43 extern const char kOptionPageRange[] = "page_range"; |
| 44 extern const char kOptionReverse[] = "reverse_order"; |
| 45 |
| 46 const char kMargineBottomMicrons[] = "bottom_microns"; |
| 47 const char kMargineLeftMicrons[] = "left_microns"; |
| 48 const char kMargineRightMicrons[] = "right_microns"; |
| 49 const char kMargineTopMicrons[] = "top_microns"; |
| 50 |
| 51 const char kDpiHorizontal[] = "horizontal_dpi"; |
| 52 const char kDpiVertical[] = "vertical_dpi"; |
| 53 |
| 54 const char kMediaWidth[] = "width_microns"; |
| 55 const char kMediaHeight[] = "height_microns"; |
| 56 const char kMediaIsContinuous[] = "is_continuous_feed"; |
| 57 |
| 58 const char kPageRangeInterval[] = "interval"; |
| 59 const char kPageRangeEnd[] = "end"; |
| 60 const char kPageRangeStart[] = "start"; |
| 61 |
| 62 const char kTypeColorColor[] = "STANDARD_COLOR"; |
| 63 const char kTypeColorMonochrome[] = "STANDARD_MONOCHROME"; |
| 64 const char kTypeColorCustomColor[] = "CUSTOM_COLOR"; |
| 65 const char kTypeColorCustomMonochrome[] = "CUSTOM_MONOCHROME"; |
| 66 const char kTypeColorAuto[] = "AUTO"; |
| 67 |
| 68 const char kTypeDuplexLongEdge[] = "LONG_EDGE"; |
| 69 const char kTypeDuplexNoDuplex[] = "NO_DUPLEX"; |
| 70 const char kTypeDuplexShortEdge[] = "SHORT_EDGE"; |
| 71 |
| 72 const char kTypeFitToPageFillPage[] = "FILL_PAGE"; |
| 73 const char kTypeFitToPageFitToPage[] = "FIT_TO_PAGE"; |
| 74 const char kTypeFitToPageGrowToPage[] = "GROW_TO_PAGE"; |
| 75 const char kTypeFitToPageNoFitting[] = "NO_FITTING"; |
| 76 const char kTypeFitToPageShrinkToPage[] = "SHRINK_TO_PAGE"; |
| 77 |
| 78 const char kTypeMarginsBorderless[] = "BORDERLESS"; |
| 79 const char kTypeMarginsCustom[] = "CUSTOM"; |
| 80 const char kTypeMarginsStandard[] = "STANDARD"; |
| 81 const char kTypeOrientationAuto[] = "AUTO"; |
| 82 |
| 83 const char kTypeOrientationLandscape[] = "LANDSCAPE"; |
| 84 const char kTypeOrientationPortrait[] = "PORTRAIT"; |
| 85 |
| 86 template <class IdType> |
| 87 struct TypePair { |
| 88 IdType id; |
| 89 const char* const json_name; |
| 90 static const TypePair kTypeMap[]; |
| 91 }; |
| 92 |
| 93 template<> |
| 94 const TypePair<ColorType> TypePair<ColorType>::kTypeMap[] = { |
| 95 { STANDARD_COLOR, kTypeColorColor }, |
| 96 { STANDARD_MONOCHROME, kTypeColorMonochrome }, |
| 97 { CUSTOM_COLOR, kTypeColorCustomColor }, |
| 98 { CUSTOM_MONOCHROME, kTypeColorCustomMonochrome }, |
| 99 { AUTO_COLOR, kTypeColorAuto }, |
| 100 }; |
| 101 |
| 102 template<> |
| 103 const TypePair<DuplexType> |
| 104 TypePair<DuplexType>::kTypeMap[] = { |
| 105 { NO_DUPLEX, kTypeDuplexNoDuplex }, |
| 106 { LONG_EDGE, kTypeDuplexLongEdge }, |
| 107 { SHORT_EDGE, kTypeDuplexShortEdge }, |
| 108 }; |
| 109 |
| 110 template<> |
| 111 const TypePair<OrientationType> |
| 112 TypePair<OrientationType>::kTypeMap[] = { |
| 113 { PORTRAIT, kTypeOrientationPortrait }, |
| 114 { LANDSCAPE, kTypeOrientationLandscape }, |
| 115 { AUTO_ORIENTATION, kTypeOrientationAuto }, |
| 116 }; |
| 117 |
| 118 template<> |
| 119 const TypePair<MarginsType> |
| 120 TypePair<MarginsType>::kTypeMap[] = { |
| 121 { NO_MARGINS, kTypeMarginsBorderless }, |
| 122 { STANDARD_MARGINS, kTypeMarginsStandard }, |
| 123 { CUSTOM_MARGINS, kTypeMarginsCustom }, |
| 124 }; |
| 125 |
| 126 template<> |
| 127 const TypePair<FitToPageType> |
| 128 TypePair<FitToPageType>::kTypeMap[] = { |
| 129 { NO_FITTING, kTypeFitToPageNoFitting }, |
| 130 { FIT_TO_PAGE, kTypeFitToPageFitToPage }, |
| 131 { GROW_TO_PAGE, kTypeFitToPageGrowToPage }, |
| 132 { SHRINK_TO_PAGE, kTypeFitToPageShrinkToPage }, |
| 133 { FILL_PAGE, kTypeFitToPageFillPage }, |
| 134 }; |
| 135 |
| 136 |
| 137 template<> |
| 138 const TypePair<MediaType> |
| 139 TypePair<MediaType>::kTypeMap[] = { |
| 140 #define MAP_CLOUD_PRINT_MEDIA_TYPE(type) { type, #type } |
| 141 { CUSTOM_MEDIA, "CUSTOM" }, |
| 142 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_INDEX_3X5), |
| 143 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_PERSONAL), |
| 144 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_MONARCH), |
| 145 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_NUMBER_9), |
| 146 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_INDEX_4X6), |
| 147 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_NUMBER_10), |
| 148 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_A2), |
| 149 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_NUMBER_11), |
| 150 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_NUMBER_12), |
| 151 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_5X7), |
| 152 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_INDEX_5X8), |
| 153 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_NUMBER_14), |
| 154 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_INVOICE), |
| 155 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_INDEX_4X6_EXT), |
| 156 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_6X9), |
| 157 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_C5), |
| 158 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_7X9), |
| 159 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_EXECUTIVE), |
| 160 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_GOVT_LETTER), |
| 161 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_GOVT_LEGAL), |
| 162 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_QUARTO), |
| 163 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_LETTER), |
| 164 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_FANFOLD_EUR), |
| 165 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_LETTER_PLUS), |
| 166 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_FOOLSCAP), |
| 167 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_LEGAL), |
| 168 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_SUPER_A), |
| 169 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_9X11), |
| 170 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_ARCH_A), |
| 171 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_LETTER_EXTRA), |
| 172 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_LEGAL_EXTRA), |
| 173 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_10X11), |
| 174 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_10X13), |
| 175 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_10X14), |
| 176 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_10X15), |
| 177 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_11X12), |
| 178 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_EDP), |
| 179 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_FANFOLD_US), |
| 180 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_11X15), |
| 181 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_LEDGER), |
| 182 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_EUR_EDP), |
| 183 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_ARCH_B), |
| 184 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_12X19), |
| 185 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_B_PLUS), |
| 186 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_SUPER_B), |
| 187 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_C), |
| 188 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_ARCH_C), |
| 189 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_D), |
| 190 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_ARCH_D), |
| 191 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_ASME_F), |
| 192 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_WIDE_FORMAT), |
| 193 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_E), |
| 194 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_ARCH_E), |
| 195 MAP_CLOUD_PRINT_MEDIA_TYPE(NA_F), |
| 196 MAP_CLOUD_PRINT_MEDIA_TYPE(ROC_16K), |
| 197 MAP_CLOUD_PRINT_MEDIA_TYPE(ROC_8K), |
| 198 MAP_CLOUD_PRINT_MEDIA_TYPE(PRC_32K), |
| 199 MAP_CLOUD_PRINT_MEDIA_TYPE(PRC_1), |
| 200 MAP_CLOUD_PRINT_MEDIA_TYPE(PRC_2), |
| 201 MAP_CLOUD_PRINT_MEDIA_TYPE(PRC_4), |
| 202 MAP_CLOUD_PRINT_MEDIA_TYPE(PRC_5), |
| 203 MAP_CLOUD_PRINT_MEDIA_TYPE(PRC_8), |
| 204 MAP_CLOUD_PRINT_MEDIA_TYPE(PRC_6), |
| 205 MAP_CLOUD_PRINT_MEDIA_TYPE(PRC_3), |
| 206 MAP_CLOUD_PRINT_MEDIA_TYPE(PRC_16K), |
| 207 MAP_CLOUD_PRINT_MEDIA_TYPE(PRC_7), |
| 208 MAP_CLOUD_PRINT_MEDIA_TYPE(OM_JUURO_KU_KAI), |
| 209 MAP_CLOUD_PRINT_MEDIA_TYPE(OM_PA_KAI), |
| 210 MAP_CLOUD_PRINT_MEDIA_TYPE(OM_DAI_PA_KAI), |
| 211 MAP_CLOUD_PRINT_MEDIA_TYPE(PRC_10), |
| 212 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_A10), |
| 213 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_A9), |
| 214 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_A8), |
| 215 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_A7), |
| 216 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_A6), |
| 217 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_A5), |
| 218 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_A5_EXTRA), |
| 219 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_A4), |
| 220 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_A4_TAB), |
| 221 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_A4_EXTRA), |
| 222 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_A3), |
| 223 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_A4X3), |
| 224 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_A4X4), |
| 225 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_A4X5), |
| 226 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_A4X6), |
| 227 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_A4X7), |
| 228 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_A4X8), |
| 229 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_A4X9), |
| 230 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_A3_EXTRA), |
| 231 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_A2), |
| 232 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_A3X3), |
| 233 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_A3X4), |
| 234 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_A3X5), |
| 235 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_A3X6), |
| 236 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_A3X7), |
| 237 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_A1), |
| 238 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_A2X3), |
| 239 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_A2X4), |
| 240 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_A2X5), |
| 241 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_A0), |
| 242 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_A1X3), |
| 243 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_A1X4), |
| 244 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_2A0), |
| 245 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_A0X3), |
| 246 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_B10), |
| 247 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_B9), |
| 248 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_B8), |
| 249 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_B7), |
| 250 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_B6), |
| 251 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_B6C4), |
| 252 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_B5), |
| 253 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_B5_EXTRA), |
| 254 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_B4), |
| 255 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_B3), |
| 256 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_B2), |
| 257 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_B1), |
| 258 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_B0), |
| 259 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_C10), |
| 260 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_C9), |
| 261 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_C8), |
| 262 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_C7), |
| 263 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_C7C6), |
| 264 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_C6), |
| 265 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_C6C5), |
| 266 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_C5), |
| 267 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_C4), |
| 268 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_C3), |
| 269 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_C2), |
| 270 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_C1), |
| 271 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_C0), |
| 272 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_DL), |
| 273 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_RA2), |
| 274 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_SRA2), |
| 275 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_RA1), |
| 276 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_SRA1), |
| 277 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_RA0), |
| 278 MAP_CLOUD_PRINT_MEDIA_TYPE(ISO_SRA0), |
| 279 MAP_CLOUD_PRINT_MEDIA_TYPE(JIS_B10), |
| 280 MAP_CLOUD_PRINT_MEDIA_TYPE(JIS_B9), |
| 281 MAP_CLOUD_PRINT_MEDIA_TYPE(JIS_B8), |
| 282 MAP_CLOUD_PRINT_MEDIA_TYPE(JIS_B7), |
| 283 MAP_CLOUD_PRINT_MEDIA_TYPE(JIS_B6), |
| 284 MAP_CLOUD_PRINT_MEDIA_TYPE(JIS_B5), |
| 285 MAP_CLOUD_PRINT_MEDIA_TYPE(JIS_B4), |
| 286 MAP_CLOUD_PRINT_MEDIA_TYPE(JIS_B3), |
| 287 MAP_CLOUD_PRINT_MEDIA_TYPE(JIS_B2), |
| 288 MAP_CLOUD_PRINT_MEDIA_TYPE(JIS_B1), |
| 289 MAP_CLOUD_PRINT_MEDIA_TYPE(JIS_B0), |
| 290 MAP_CLOUD_PRINT_MEDIA_TYPE(JIS_EXEC), |
| 291 MAP_CLOUD_PRINT_MEDIA_TYPE(JPN_CHOU4), |
| 292 MAP_CLOUD_PRINT_MEDIA_TYPE(JPN_HAGAKI), |
| 293 MAP_CLOUD_PRINT_MEDIA_TYPE(JPN_YOU4), |
| 294 MAP_CLOUD_PRINT_MEDIA_TYPE(JPN_CHOU2), |
| 295 MAP_CLOUD_PRINT_MEDIA_TYPE(JPN_CHOU3), |
| 296 MAP_CLOUD_PRINT_MEDIA_TYPE(JPN_OUFUKU), |
| 297 MAP_CLOUD_PRINT_MEDIA_TYPE(JPN_KAHU), |
| 298 MAP_CLOUD_PRINT_MEDIA_TYPE(JPN_KAKU2), |
| 299 MAP_CLOUD_PRINT_MEDIA_TYPE(OM_SMALL_PHOTO), |
| 300 MAP_CLOUD_PRINT_MEDIA_TYPE(OM_ITALIAN), |
| 301 MAP_CLOUD_PRINT_MEDIA_TYPE(OM_POSTFIX), |
| 302 MAP_CLOUD_PRINT_MEDIA_TYPE(OM_LARGE_PHOTO), |
| 303 MAP_CLOUD_PRINT_MEDIA_TYPE(OM_FOLIO), |
| 304 MAP_CLOUD_PRINT_MEDIA_TYPE(OM_FOLIO_SP), |
| 305 MAP_CLOUD_PRINT_MEDIA_TYPE(OM_INVITE), |
| 306 #undef MAP_CLOUD_PRINT_MEDIA_TYPE |
| 307 }; |
| 308 |
| 309 template<class IdType> |
| 310 std::string TypeToString(IdType id) { |
| 311 for (size_t i = 0; i < arraysize(TypePair<IdType>::kTypeMap); ++i) { |
| 312 if (id == TypePair<IdType>::kTypeMap[i].id) |
| 313 return TypePair<IdType>::kTypeMap[i].json_name; |
| 314 } |
| 315 NOTREACHED(); |
| 316 return std::string(); |
| 317 } |
| 318 |
| 319 template<class IdType> |
| 320 bool TypeFromString(const std::string& type, IdType* id) { |
| 321 for (size_t i = 0; i < arraysize(TypePair<IdType>::kTypeMap); ++i) { |
| 322 if (type == TypePair<IdType>::kTypeMap[i].json_name) { |
| 323 *id = TypePair<IdType>::kTypeMap[i].id; |
| 324 return true; |
| 325 } |
| 326 } |
| 327 return false; |
| 328 } |
| 329 |
| 330 } // namespace |
| 331 |
| 332 Color::Color() : type(AUTO_COLOR) {} |
| 333 |
| 334 Color::Color(ColorType type) : type(type) {} |
| 335 |
| 336 bool Color::operator==(const Color& other) const { |
| 337 return type == other.type && vendor_id == other.vendor_id && |
| 338 custom_display_name == other.custom_display_name; |
| 339 } |
| 340 |
| 341 bool Color::IsValid() const { |
| 342 if (type != CUSTOM_COLOR && type != CUSTOM_MONOCHROME) |
| 343 return true; |
| 344 return !vendor_id.empty() && !custom_display_name.empty(); |
| 345 } |
| 346 |
| 347 Margins::Margins() |
| 348 : type(STANDARD_MARGINS), |
| 349 top_microns(0), |
| 350 right_microns(0), |
| 351 bottom_microns(0), |
| 352 left_microns(0) { |
| 353 } |
| 354 |
| 355 Margins::Margins(MarginsType type, |
| 356 int32 top_microns, |
| 357 int32 right_microns, |
| 358 int32 bottom_microns, |
| 359 int32 left_microns) |
| 360 : type(type), |
| 361 top_microns(top_microns), |
| 362 right_microns(right_microns), |
| 363 bottom_microns(bottom_microns), |
| 364 left_microns(left_microns) { |
| 365 } |
| 366 |
| 367 bool Margins::operator==(const Margins& other) const { |
| 368 return type == other.type && |
| 369 top_microns == other.top_microns && |
| 370 right_microns == other.right_microns && |
| 371 bottom_microns == other.bottom_microns; |
| 372 } |
| 373 |
| 374 Dpi::Dpi() : horizontal(0), vertical(0) {} |
| 375 |
| 376 Dpi::Dpi(int32 horizontal, int32 vertical) |
| 377 : horizontal(horizontal), vertical(vertical) {} |
| 378 |
| 379 bool Dpi::operator==(const Dpi& other) const { |
| 380 return horizontal == other.horizontal && vertical == other.vertical; |
| 381 } |
| 382 |
| 383 Media::Media() |
| 384 : type(CUSTOM_MEDIA), |
| 385 width_microns(0), |
| 386 height_microns(0), |
| 387 is_continuous_feed(false) { |
| 388 |
| 389 } |
| 390 |
| 391 Media::Media(MediaType type, int32 width_microns, int32 height_microns) |
| 392 : type(type), |
| 393 width_microns(width_microns), |
| 394 height_microns(height_microns), |
| 395 is_continuous_feed(width_microns <= 0 || height_microns <= 0) { |
| 396 } |
| 397 |
| 398 Media::Media(const std::string& custom_display_name, int32 width_microns, |
| 399 int32 height_microns) |
| 400 : type(CUSTOM_MEDIA), |
| 401 width_microns(width_microns), |
| 402 height_microns(height_microns), |
| 403 is_continuous_feed(width_microns <= 0 || height_microns <= 0), |
| 404 custom_display_name(custom_display_name) { |
| 405 } |
| 406 |
| 407 bool Media::IsValid() const { |
| 408 if (is_continuous_feed) { |
| 409 if (width_microns <= 0 && height_microns <= 0) |
| 410 return false; |
| 411 } else { |
| 412 if (width_microns <= 0 || height_microns <= 0) |
| 413 return false; |
| 414 } |
| 415 return true; |
| 416 } |
| 417 |
| 418 bool Media::operator==(const Media& other) const { |
| 419 return type == other.type && |
| 420 width_microns == other.width_microns && |
| 421 height_microns == other.height_microns && |
| 422 is_continuous_feed == other.is_continuous_feed; |
| 423 } |
| 424 |
| 425 Interval::Interval() : start(0), end(0) {} |
| 426 |
| 427 Interval::Interval(int32 start, int32 end) |
| 428 : start(start), end(end) {} |
| 429 |
| 430 Interval::Interval(int32 start) |
| 431 : start(start), end(kMaxPageNumber) {} |
| 432 |
| 433 bool Interval::operator==(const Interval& other) const { |
| 434 return start == other.start && end == other.end; |
| 435 } |
| 436 |
| 437 template<const char* kName> |
| 438 class ItemsTraits { |
| 439 public: |
| 440 static std::string GetItemPath() { |
| 441 std::string result = kSectionPrinter; |
| 442 result += '.'; |
| 443 result += kName; |
| 444 return result; |
| 445 } |
| 446 }; |
| 447 |
| 448 class NoValueValidation { |
| 449 public: |
| 450 template <class Option> |
| 451 static bool IsValid(const Option&) { |
| 452 return true; |
| 453 } |
| 454 }; |
| 455 |
| 456 class ContentTypeTraits : public NoValueValidation, |
| 457 public ItemsTraits<kOptionContentType> { |
| 458 public: |
| 459 static bool Load(const base::DictionaryValue& dict, ContentType* option) { |
| 460 return dict.GetString(kKeyContentType, option); |
| 461 } |
| 462 |
| 463 static void Save(ContentType option, base::DictionaryValue* dict) { |
| 464 dict->SetString(kKeyContentType, option); |
| 465 } |
| 466 }; |
| 467 |
| 468 class ColorTraits : public ItemsTraits<kOptionColor> { |
| 469 public: |
| 470 static bool IsValid(const Color& option) { |
| 471 return option.IsValid(); |
| 472 } |
| 473 |
| 474 static bool Load(const base::DictionaryValue& dict, Color* option) { |
| 475 std::string type_str; |
| 476 if (!dict.GetString(kKeyType, &type_str)) |
| 477 return false; |
| 478 if (!TypeFromString(type_str, &option->type)) |
| 479 return false; |
| 480 dict.GetString(kKeyVendorId, &option->vendor_id); |
| 481 dict.GetString(kCustomName, &option->custom_display_name); |
| 482 return true; |
| 483 } |
| 484 |
| 485 static void Save(const Color& option, base::DictionaryValue* dict) { |
| 486 dict->SetString(kKeyType, TypeToString(option.type)); |
| 487 if (!option.vendor_id.empty()) |
| 488 dict->SetString(kKeyVendorId, option.vendor_id); |
| 489 if (!option.custom_display_name.empty()) |
| 490 dict->SetString(kCustomName, option.custom_display_name); |
| 491 } |
| 492 }; |
| 493 |
| 494 class DuplexTraits : public NoValueValidation, |
| 495 public ItemsTraits<kOptionDuplex> { |
| 496 public: |
| 497 static bool Load(const base::DictionaryValue& dict, DuplexType* option) { |
| 498 std::string type_str; |
| 499 return dict.GetString(kKeyType, &type_str) && |
| 500 TypeFromString(type_str, option); |
| 501 } |
| 502 |
| 503 static void Save(DuplexType option, base::DictionaryValue* dict) { |
| 504 dict->SetString(kKeyType, TypeToString(option)); |
| 505 } |
| 506 }; |
| 507 |
| 508 class OrientationTraits : public NoValueValidation, |
| 509 public ItemsTraits<kOptionPageOrientation> { |
| 510 public: |
| 511 static bool Load(const base::DictionaryValue& dict, OrientationType* option) { |
| 512 std::string type_str; |
| 513 return dict.GetString(kKeyType, &type_str) && |
| 514 TypeFromString(type_str, option); |
| 515 } |
| 516 |
| 517 static void Save(OrientationType option, base::DictionaryValue* dict) { |
| 518 dict->SetString(kKeyType, TypeToString(option)); |
| 519 } |
| 520 }; |
| 521 |
| 522 class CopiesTraits : public ItemsTraits<kOptionCopies> { |
| 523 public: |
| 524 static bool IsValid(int32 option) { |
| 525 return option >= 1; |
| 526 } |
| 527 |
| 528 static bool Load(const base::DictionaryValue& dict, int32* option) { |
| 529 return dict.GetInteger(kOptionCopies, option); |
| 530 } |
| 531 |
| 532 static void Save(int32 option, base::DictionaryValue* dict) { |
| 533 dict->SetInteger(kOptionCopies, option); |
| 534 } |
| 535 }; |
| 536 |
| 537 class MarginsTraits : public NoValueValidation, |
| 538 public ItemsTraits<kOptionMargins> { |
| 539 public: |
| 540 static bool Load(const base::DictionaryValue& dict, Margins* option) { |
| 541 std::string type_str; |
| 542 if (!dict.GetString(kKeyType, &type_str)) |
| 543 return false; |
| 544 if (!TypeFromString(type_str, &option->type)) |
| 545 return false; |
| 546 return |
| 547 dict.GetInteger(kMargineTopMicrons, &option->top_microns) && |
| 548 dict.GetInteger(kMargineRightMicrons, &option->right_microns) && |
| 549 dict.GetInteger(kMargineBottomMicrons, &option->bottom_microns) && |
| 550 dict.GetInteger(kMargineLeftMicrons, &option->left_microns); |
| 551 } |
| 552 |
| 553 static void Save(const Margins& option, base::DictionaryValue* dict) { |
| 554 dict->SetString(kKeyType, TypeToString(option.type)); |
| 555 dict->SetInteger(kMargineTopMicrons, option.top_microns); |
| 556 dict->SetInteger(kMargineRightMicrons, option.right_microns); |
| 557 dict->SetInteger(kMargineBottomMicrons, option.bottom_microns); |
| 558 dict->SetInteger(kMargineLeftMicrons, option.left_microns); |
| 559 } |
| 560 }; |
| 561 |
| 562 class DpiTraits : public ItemsTraits<kOptionDpi> { |
| 563 public: |
| 564 static bool IsValid(const Dpi& option) { |
| 565 return option.horizontal && option.vertical > 0; |
| 566 } |
| 567 |
| 568 static bool Load(const base::DictionaryValue& dict, Dpi* option) { |
| 569 if (!dict.GetInteger(kDpiHorizontal, &option->horizontal) || |
| 570 !dict.GetInteger(kDpiVertical, &option->vertical)) { |
| 571 return false; |
| 572 } |
| 573 return true; |
| 574 } |
| 575 |
| 576 static void Save(const Dpi& option, base::DictionaryValue* dict) { |
| 577 dict->SetInteger(kDpiHorizontal, option.horizontal); |
| 578 dict->SetInteger(kDpiVertical, option.vertical); |
| 579 } |
| 580 }; |
| 581 |
| 582 class FitToPageTraits : public NoValueValidation, |
| 583 public ItemsTraits<kOptionFitToPage> { |
| 584 public: |
| 585 static bool Load(const base::DictionaryValue& dict, FitToPageType* option) { |
| 586 std::string type_str; |
| 587 return dict.GetString(kKeyType, &type_str) && |
| 588 TypeFromString(type_str, option); |
| 589 } |
| 590 |
| 591 static void Save(FitToPageType option, base::DictionaryValue* dict) { |
| 592 dict->SetString(kKeyType, TypeToString(option)); |
| 593 } |
| 594 }; |
| 595 |
| 596 class PageRangeTraits : public ItemsTraits<kOptionPageRange> { |
| 597 public: |
| 598 static bool IsValid(const PageRange& option) { |
| 599 for (size_t i = 0; i < option.size(); ++i) { |
| 600 if (option[i].start < 1 || option[i].end < 1) { |
| 601 return false; |
| 602 } |
| 603 } |
| 604 return true; |
| 605 } |
| 606 |
| 607 static bool Load(const base::DictionaryValue& dict, PageRange* option) { |
| 608 const base::ListValue* list = NULL; |
| 609 if (!dict.GetList(kPageRangeInterval, &list)) |
| 610 return false; |
| 611 for (size_t i = 0; i < list->GetSize(); ++i) { |
| 612 const base::DictionaryValue* interval = NULL; |
| 613 if (!list->GetDictionary(i, &interval)) |
| 614 return false; |
| 615 Interval new_interval(1, kMaxPageNumber); |
| 616 interval->GetInteger(kPageRangeStart, &new_interval.start); |
| 617 interval->GetInteger(kPageRangeEnd, &new_interval.end); |
| 618 option->push_back(new_interval); |
| 619 } |
| 620 return true; |
| 621 } |
| 622 |
| 623 static void Save(const PageRange& option, base::DictionaryValue* dict) { |
| 624 if (!option.empty()) { |
| 625 base::ListValue* list = new base::ListValue; |
| 626 dict->Set(kPageRangeInterval, list); |
| 627 for (size_t i = 0; i < option.size(); ++i) { |
| 628 base::DictionaryValue* interval = new base::DictionaryValue; |
| 629 list->Append(interval); |
| 630 interval->SetInteger(kPageRangeStart, option[i].start); |
| 631 if (option[i].end < kMaxPageNumber) |
| 632 interval->SetInteger(kPageRangeEnd, option[i].end); |
| 633 } |
| 634 } |
| 635 } |
| 636 }; |
| 637 |
| 638 class MediaTraits : public ItemsTraits<kOptionMediaSize> { |
| 639 public: |
| 640 static bool IsValid(const Media& option) { |
| 641 return option.IsValid(); |
| 642 } |
| 643 |
| 644 static bool Load(const base::DictionaryValue& dict, Media* option) { |
| 645 std::string type_str; |
| 646 if (dict.GetString(kKeyName, &type_str)) { |
| 647 if (!TypeFromString(type_str, &option->type)) |
| 648 return false; |
| 649 } |
| 650 |
| 651 dict.GetInteger(kMediaWidth, &option->width_microns); |
| 652 dict.GetInteger(kMediaHeight, &option->height_microns); |
| 653 dict.GetBoolean(kMediaIsContinuous, &option->is_continuous_feed); |
| 654 dict.GetString(kCustomName, &option->custom_display_name); |
| 655 return true; |
| 656 } |
| 657 |
| 658 static void Save(const Media& option, base::DictionaryValue* dict) { |
| 659 if (option.type != CUSTOM_MEDIA) |
| 660 dict->SetString(kKeyName, TypeToString(option.type)); |
| 661 if (!option.custom_display_name.empty()) |
| 662 dict->SetString(kCustomName, option.custom_display_name); |
| 663 if (option.width_microns > 0) |
| 664 dict->SetInteger(kMediaWidth, option.width_microns); |
| 665 if (option.height_microns > 0) |
| 666 dict->SetInteger(kMediaHeight, option.height_microns); |
| 667 if (option.is_continuous_feed) |
| 668 dict->SetBoolean(kMediaIsContinuous, true); |
| 669 } |
| 670 }; |
| 671 |
| 672 class CollateTraits : public NoValueValidation, |
| 673 public ItemsTraits<kOptionCollate> { |
| 674 public: |
| 675 static const bool kDefault = true; |
| 676 |
| 677 static bool Load(const base::DictionaryValue& dict, bool* option) { |
| 678 return dict.GetBoolean(kOptionCollate, option); |
| 679 } |
| 680 |
| 681 static void Save(bool option, base::DictionaryValue* dict) { |
| 682 dict->SetBoolean(kOptionCollate, option); |
| 683 } |
| 684 }; |
| 685 |
| 686 class ReverseTraits : public NoValueValidation, |
| 687 public ItemsTraits<kOptionReverse> { |
| 688 public: |
| 689 static const bool kDefault = false; |
| 690 |
| 691 static bool Load(const base::DictionaryValue& dict, bool* option) { |
| 692 return dict.GetBoolean(kOptionReverse, option); |
| 693 } |
| 694 |
| 695 static void Save(bool option, base::DictionaryValue* dict) { |
| 696 dict->SetBoolean(kOptionReverse, option); |
| 697 } |
| 698 }; |
| 699 |
| 700 } // namespace printer |
| 701 |
| 702 using namespace printer; |
| 703 |
| 704 template class ListCapability<ContentType, ContentTypeTraits>; |
| 705 template class SelectionCapability<Color, ColorTraits>; |
| 706 template class SelectionCapability<DuplexType, DuplexTraits>; |
| 707 template class SelectionCapability<OrientationType, OrientationTraits>; |
| 708 template class SelectionCapability<Margins, MarginsTraits>; |
| 709 template class SelectionCapability<Dpi, DpiTraits>; |
| 710 template class SelectionCapability<FitToPageType, FitToPageTraits>; |
| 711 template class SelectionCapability<Media, MediaTraits>; |
| 712 template class EmptyCapability<class CopiesTraits>; |
| 713 template class EmptyCapability<class PageRangeTraits>; |
| 714 template class BooleanCapability<class CollateTraits>; |
| 715 template class BooleanCapability<class ReverseTraits>; |
| 716 |
| 717 template class TicketItem<Color, ColorTraits>; |
| 718 template class TicketItem<DuplexType, DuplexTraits>; |
| 719 template class TicketItem<OrientationType, OrientationTraits>; |
| 720 template class TicketItem<Margins, MarginsTraits>; |
| 721 template class TicketItem<Dpi, DpiTraits>; |
| 722 template class TicketItem<FitToPageType, FitToPageTraits>; |
| 723 template class TicketItem<Media, MediaTraits>; |
| 724 template class TicketItem<int32, CopiesTraits>; |
| 725 template class TicketItem<PageRange, PageRangeTraits>; |
| 726 template class TicketItem<bool, CollateTraits>; |
| 727 template class TicketItem<bool, ReverseTraits>; |
| 728 |
| 729 } // namespace cloud_devices |
| OLD | NEW |