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 "components/cloud_devices/common/printer_description.h" | 5 #include "components/cloud_devices/common/printer_description.h" |
6 | 6 |
| 7 #include <stddef.h> |
| 8 |
7 #include <algorithm> | 9 #include <algorithm> |
8 | 10 |
9 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
10 #include "base/json/json_writer.h" | 12 #include "base/json/json_writer.h" |
| 13 #include "base/macros.h" |
11 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
12 #include "base/values.h" | 15 #include "base/values.h" |
13 #include "components/cloud_devices/common/cloud_device_description_consts.h" | 16 #include "components/cloud_devices/common/cloud_device_description_consts.h" |
14 #include "components/cloud_devices/common/description_items_inl.h" | 17 #include "components/cloud_devices/common/description_items_inl.h" |
15 | 18 |
16 namespace cloud_devices { | 19 namespace cloud_devices { |
17 | 20 |
18 namespace printer { | 21 namespace printer { |
19 | 22 |
20 namespace { | 23 namespace { |
21 | 24 |
22 const int32 kMaxPageNumber = 1000000; | 25 const int32_t kMaxPageNumber = 1000000; |
23 | 26 |
24 const char kSectionPrint[] = "print"; | 27 const char kSectionPrint[] = "print"; |
25 const char kSectionPrinter[] = "printer"; | 28 const char kSectionPrinter[] = "printer"; |
26 | 29 |
27 const char kCustomName[] = "custom_display_name"; | 30 const char kCustomName[] = "custom_display_name"; |
28 const char kKeyContentType[] = "content_type"; | 31 const char kKeyContentType[] = "content_type"; |
29 const char kKeyName[] = "name"; | 32 const char kKeyName[] = "name"; |
30 const char kKeyType[] = "type"; | 33 const char kKeyType[] = "type"; |
31 const char kKeyVendorId[] = "vendor_id"; | 34 const char kKeyVendorId[] = "vendor_id"; |
32 | 35 |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 | 148 |
146 const struct DocumentSheetBackNames { | 149 const struct DocumentSheetBackNames { |
147 DocumentSheetBack id; | 150 DocumentSheetBack id; |
148 const char* const json_name; | 151 const char* const json_name; |
149 } kDocumentSheetBackNames[] = { | 152 } kDocumentSheetBackNames[] = { |
150 {NORMAL, kTypeDocumentSheetBackNormal}, | 153 {NORMAL, kTypeDocumentSheetBackNormal}, |
151 {ROTATED, kTypeDocumentSheetBackRotated}, | 154 {ROTATED, kTypeDocumentSheetBackRotated}, |
152 {MANUAL_TUMBLE, kTypeDocumentSheetBackManualTumble}, | 155 {MANUAL_TUMBLE, kTypeDocumentSheetBackManualTumble}, |
153 {FLIPPED, kTypeDocumentSheetBackFlipped}}; | 156 {FLIPPED, kTypeDocumentSheetBackFlipped}}; |
154 | 157 |
155 const int32 kInchToUm = 25400; | 158 const int32_t kInchToUm = 25400; |
156 const int32 kMmToUm = 1000; | 159 const int32_t kMmToUm = 1000; |
157 const int32 kSizeTrasholdUm = 1000; | 160 const int32_t kSizeTrasholdUm = 1000; |
158 | 161 |
159 #define MAP_CLOUD_PRINT_MEDIA_TYPE(type, width, height, unit_um) \ | 162 #define MAP_CLOUD_PRINT_MEDIA_TYPE(type, width, height, unit_um) \ |
160 { \ | 163 { \ |
161 type, #type, static_cast<int>(width* unit_um + 0.5), \ | 164 type, #type, static_cast<int>(width* unit_um + 0.5), \ |
162 static_cast<int>(height* unit_um + 0.5) \ | 165 static_cast<int>(height* unit_um + 0.5) \ |
163 } | 166 } |
164 | 167 |
165 const struct MediaDefinition { | 168 const struct MediaDefinition { |
166 MediaType id; | 169 MediaType id; |
167 const char* const json_name; | 170 const char* const json_name; |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 | 340 |
338 const MediaDefinition& FindMediaByType(MediaType type) { | 341 const MediaDefinition& FindMediaByType(MediaType type) { |
339 for (size_t i = 0; i < arraysize(kMediaDefinitions); ++i) { | 342 for (size_t i = 0; i < arraysize(kMediaDefinitions); ++i) { |
340 if (kMediaDefinitions[i].id == type) | 343 if (kMediaDefinitions[i].id == type) |
341 return kMediaDefinitions[i]; | 344 return kMediaDefinitions[i]; |
342 } | 345 } |
343 NOTREACHED(); | 346 NOTREACHED(); |
344 return kMediaDefinitions[0]; | 347 return kMediaDefinitions[0]; |
345 } | 348 } |
346 | 349 |
347 const MediaDefinition* FindMediaBySize(int32 width_um, int32 height_um) { | 350 const MediaDefinition* FindMediaBySize(int32_t width_um, int32_t height_um) { |
348 const MediaDefinition* result = NULL; | 351 const MediaDefinition* result = NULL; |
349 for (size_t i = 0; i < arraysize(kMediaDefinitions); ++i) { | 352 for (size_t i = 0; i < arraysize(kMediaDefinitions); ++i) { |
350 int32 diff = std::max(std::abs(width_um - kMediaDefinitions[i].width_um), | 353 int32_t diff = |
351 std::abs(height_um - kMediaDefinitions[i].height_um)); | 354 std::max(std::abs(width_um - kMediaDefinitions[i].width_um), |
| 355 std::abs(height_um - kMediaDefinitions[i].height_um)); |
352 if (diff < kSizeTrasholdUm) | 356 if (diff < kSizeTrasholdUm) |
353 result = &kMediaDefinitions[i]; | 357 result = &kMediaDefinitions[i]; |
354 } | 358 } |
355 return result; | 359 return result; |
356 } | 360 } |
357 | 361 |
358 template <class T, class IdType> | 362 template <class T, class IdType> |
359 std::string TypeToString(const T& names, IdType id) { | 363 std::string TypeToString(const T& names, IdType id) { |
360 for (size_t i = 0; i < arraysize(names); ++i) { | 364 for (size_t i = 0; i < arraysize(names); ++i) { |
361 if (id == names[i].id) | 365 if (id == names[i].id) |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
399 if (type != CUSTOM_COLOR && type != CUSTOM_MONOCHROME) | 403 if (type != CUSTOM_COLOR && type != CUSTOM_MONOCHROME) |
400 return true; | 404 return true; |
401 return !vendor_id.empty() && !custom_display_name.empty(); | 405 return !vendor_id.empty() && !custom_display_name.empty(); |
402 } | 406 } |
403 | 407 |
404 Margins::Margins() | 408 Margins::Margins() |
405 : type(STANDARD_MARGINS), top_um(0), right_um(0), bottom_um(0), left_um(0) { | 409 : type(STANDARD_MARGINS), top_um(0), right_um(0), bottom_um(0), left_um(0) { |
406 } | 410 } |
407 | 411 |
408 Margins::Margins(MarginsType type, | 412 Margins::Margins(MarginsType type, |
409 int32 top_um, | 413 int32_t top_um, |
410 int32 right_um, | 414 int32_t right_um, |
411 int32 bottom_um, | 415 int32_t bottom_um, |
412 int32 left_um) | 416 int32_t left_um) |
413 : type(type), | 417 : type(type), |
414 top_um(top_um), | 418 top_um(top_um), |
415 right_um(right_um), | 419 right_um(right_um), |
416 bottom_um(bottom_um), | 420 bottom_um(bottom_um), |
417 left_um(left_um) { | 421 left_um(left_um) {} |
418 } | |
419 | 422 |
420 bool Margins::operator==(const Margins& other) const { | 423 bool Margins::operator==(const Margins& other) const { |
421 return type == other.type && top_um == other.top_um && | 424 return type == other.type && top_um == other.top_um && |
422 right_um == other.right_um && bottom_um == other.bottom_um; | 425 right_um == other.right_um && bottom_um == other.bottom_um; |
423 } | 426 } |
424 | 427 |
425 Dpi::Dpi() : horizontal(0), vertical(0) { | 428 Dpi::Dpi() : horizontal(0), vertical(0) { |
426 } | 429 } |
427 | 430 |
428 Dpi::Dpi(int32 horizontal, int32 vertical) | 431 Dpi::Dpi(int32_t horizontal, int32_t vertical) |
429 : horizontal(horizontal), vertical(vertical) { | 432 : horizontal(horizontal), vertical(vertical) {} |
430 } | |
431 | 433 |
432 bool Dpi::IsValid() const { | 434 bool Dpi::IsValid() const { |
433 return horizontal > 0 && vertical > 0; | 435 return horizontal > 0 && vertical > 0; |
434 } | 436 } |
435 | 437 |
436 bool Dpi::operator==(const Dpi& other) const { | 438 bool Dpi::operator==(const Dpi& other) const { |
437 return horizontal == other.horizontal && vertical == other.vertical; | 439 return horizontal == other.horizontal && vertical == other.vertical; |
438 } | 440 } |
439 | 441 |
440 Media::Media() | 442 Media::Media() |
441 : type(CUSTOM_MEDIA), width_um(0), height_um(0), is_continuous_feed(false) { | 443 : type(CUSTOM_MEDIA), width_um(0), height_um(0), is_continuous_feed(false) { |
442 } | 444 } |
443 | 445 |
444 Media::Media(MediaType type) | 446 Media::Media(MediaType type) |
445 : type(type), | 447 : type(type), |
446 width_um(0), | 448 width_um(0), |
447 height_um(0), | 449 height_um(0), |
448 is_continuous_feed(false) { | 450 is_continuous_feed(false) { |
449 const MediaDefinition& media = FindMediaByType(type); | 451 const MediaDefinition& media = FindMediaByType(type); |
450 width_um = media.width_um; | 452 width_um = media.width_um; |
451 height_um = media.height_um; | 453 height_um = media.height_um; |
452 is_continuous_feed = width_um <= 0 || height_um <= 0; | 454 is_continuous_feed = width_um <= 0 || height_um <= 0; |
453 } | 455 } |
454 | 456 |
455 Media::Media(MediaType type, int32 width_um, int32 height_um) | 457 Media::Media(MediaType type, int32_t width_um, int32_t height_um) |
456 : type(type), | 458 : type(type), |
457 width_um(width_um), | 459 width_um(width_um), |
458 height_um(height_um), | 460 height_um(height_um), |
459 is_continuous_feed(width_um <= 0 || height_um <= 0) { | 461 is_continuous_feed(width_um <= 0 || height_um <= 0) {} |
460 } | |
461 | 462 |
462 Media::Media(const std::string& custom_display_name, | 463 Media::Media(const std::string& custom_display_name, |
463 const std::string& vendor_id, | 464 const std::string& vendor_id, |
464 int32 width_um, | 465 int32_t width_um, |
465 int32 height_um) | 466 int32_t height_um) |
466 : type(CUSTOM_MEDIA), | 467 : type(CUSTOM_MEDIA), |
467 width_um(width_um), | 468 width_um(width_um), |
468 height_um(height_um), | 469 height_um(height_um), |
469 is_continuous_feed(width_um <= 0 || height_um <= 0), | 470 is_continuous_feed(width_um <= 0 || height_um <= 0), |
470 custom_display_name(custom_display_name), | 471 custom_display_name(custom_display_name), |
471 vendor_id(vendor_id) { | 472 vendor_id(vendor_id) {} |
472 } | |
473 | 473 |
474 bool Media::MatchBySize() { | 474 bool Media::MatchBySize() { |
475 const MediaDefinition* media = FindMediaBySize(width_um, height_um); | 475 const MediaDefinition* media = FindMediaBySize(width_um, height_um); |
476 if (!media) | 476 if (!media) |
477 return false; | 477 return false; |
478 type = media->id; | 478 type = media->id; |
479 return true; | 479 return true; |
480 } | 480 } |
481 | 481 |
482 bool Media::IsValid() const { | 482 bool Media::IsValid() const { |
483 if (is_continuous_feed) { | 483 if (is_continuous_feed) { |
484 if (width_um <= 0 && height_um <= 0) | 484 if (width_um <= 0 && height_um <= 0) |
485 return false; | 485 return false; |
486 } else { | 486 } else { |
487 if (width_um <= 0 || height_um <= 0) | 487 if (width_um <= 0 || height_um <= 0) |
488 return false; | 488 return false; |
489 } | 489 } |
490 return true; | 490 return true; |
491 } | 491 } |
492 | 492 |
493 bool Media::operator==(const Media& other) const { | 493 bool Media::operator==(const Media& other) const { |
494 return type == other.type && width_um == other.width_um && | 494 return type == other.type && width_um == other.width_um && |
495 height_um == other.height_um && | 495 height_um == other.height_um && |
496 is_continuous_feed == other.is_continuous_feed; | 496 is_continuous_feed == other.is_continuous_feed; |
497 } | 497 } |
498 | 498 |
499 Interval::Interval() : start(0), end(0) { | 499 Interval::Interval() : start(0), end(0) { |
500 } | 500 } |
501 | 501 |
502 Interval::Interval(int32 start, int32 end) : start(start), end(end) { | 502 Interval::Interval(int32_t start, int32_t end) : start(start), end(end) {} |
503 } | |
504 | 503 |
505 Interval::Interval(int32 start) : start(start), end(kMaxPageNumber) { | 504 Interval::Interval(int32_t start) : start(start), end(kMaxPageNumber) {} |
506 } | |
507 | 505 |
508 bool Interval::operator==(const Interval& other) const { | 506 bool Interval::operator==(const Interval& other) const { |
509 return start == other.start && end == other.end; | 507 return start == other.start && end == other.end; |
510 } | 508 } |
511 | 509 |
512 template <const char* kName> | 510 template <const char* kName> |
513 class ItemsTraits { | 511 class ItemsTraits { |
514 public: | 512 public: |
515 static std::string GetCapabilityPath() { | 513 static std::string GetCapabilityPath() { |
516 std::string result = kSectionPrinter; | 514 std::string result = kSectionPrinter; |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
628 TypeFromString(kOrientationNames, type_str, option); | 626 TypeFromString(kOrientationNames, type_str, option); |
629 } | 627 } |
630 | 628 |
631 static void Save(OrientationType option, base::DictionaryValue* dict) { | 629 static void Save(OrientationType option, base::DictionaryValue* dict) { |
632 dict->SetString(kKeyType, TypeToString(kOrientationNames, option)); | 630 dict->SetString(kKeyType, TypeToString(kOrientationNames, option)); |
633 } | 631 } |
634 }; | 632 }; |
635 | 633 |
636 class CopiesTraits : public ItemsTraits<kOptionCopies> { | 634 class CopiesTraits : public ItemsTraits<kOptionCopies> { |
637 public: | 635 public: |
638 static bool IsValid(int32 option) { return option >= 1; } | 636 static bool IsValid(int32_t option) { return option >= 1; } |
639 | 637 |
640 static bool Load(const base::DictionaryValue& dict, int32* option) { | 638 static bool Load(const base::DictionaryValue& dict, int32_t* option) { |
641 return dict.GetInteger(kOptionCopies, option); | 639 return dict.GetInteger(kOptionCopies, option); |
642 } | 640 } |
643 | 641 |
644 static void Save(int32 option, base::DictionaryValue* dict) { | 642 static void Save(int32_t option, base::DictionaryValue* dict) { |
645 dict->SetInteger(kOptionCopies, option); | 643 dict->SetInteger(kOptionCopies, option); |
646 } | 644 } |
647 }; | 645 }; |
648 | 646 |
649 class MarginsTraits : public NoValueValidation, | 647 class MarginsTraits : public NoValueValidation, |
650 public ItemsTraits<kOptionMargins> { | 648 public ItemsTraits<kOptionMargins> { |
651 public: | 649 public: |
652 static bool Load(const base::DictionaryValue& dict, Margins* option) { | 650 static bool Load(const base::DictionaryValue& dict, Margins* option) { |
653 std::string type_str; | 651 std::string type_str; |
654 if (!dict.GetString(kKeyType, &type_str)) | 652 if (!dict.GetString(kKeyType, &type_str)) |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
826 template class BooleanCapability<class ReverseTraits>; | 824 template class BooleanCapability<class ReverseTraits>; |
827 | 825 |
828 template class TicketItem<PwgRasterConfig, PwgRasterConfigTraits>; | 826 template class TicketItem<PwgRasterConfig, PwgRasterConfigTraits>; |
829 template class TicketItem<Color, ColorTraits>; | 827 template class TicketItem<Color, ColorTraits>; |
830 template class TicketItem<DuplexType, DuplexTraits>; | 828 template class TicketItem<DuplexType, DuplexTraits>; |
831 template class TicketItem<OrientationType, OrientationTraits>; | 829 template class TicketItem<OrientationType, OrientationTraits>; |
832 template class TicketItem<Margins, MarginsTraits>; | 830 template class TicketItem<Margins, MarginsTraits>; |
833 template class TicketItem<Dpi, DpiTraits>; | 831 template class TicketItem<Dpi, DpiTraits>; |
834 template class TicketItem<FitToPageType, FitToPageTraits>; | 832 template class TicketItem<FitToPageType, FitToPageTraits>; |
835 template class TicketItem<Media, MediaTraits>; | 833 template class TicketItem<Media, MediaTraits>; |
836 template class TicketItem<int32, CopiesTraits>; | 834 template class TicketItem<int32_t, CopiesTraits>; |
837 template class TicketItem<PageRange, PageRangeTraits>; | 835 template class TicketItem<PageRange, PageRangeTraits>; |
838 template class TicketItem<bool, CollateTraits>; | 836 template class TicketItem<bool, CollateTraits>; |
839 template class TicketItem<bool, ReverseTraits>; | 837 template class TicketItem<bool, ReverseTraits>; |
840 | 838 |
841 } // namespace cloud_devices | 839 } // namespace cloud_devices |
OLD | NEW |