summaryrefslogtreecommitdiffstats
path: root/chromium/chrome/browser/resources/print_preview/data/destination.js
blob: 403becbf6d138ea51e03a6cbf8408d455897080d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

cr.define('print_preview', function() {
  'use strict';

  /**
   * Enumeration of the types of destinations.
   * @enum {string}
   */
  const DestinationType = {
    GOOGLE: 'google',
    GOOGLE_PROMOTED: 'google_promoted',
    LOCAL: 'local',
    MOBILE: 'mobile',
  };

  /**
   * Enumeration of the origin types for cloud destinations.
   * @enum {string}
   */
  const DestinationOrigin = {
    LOCAL: 'local',
    COOKIES: 'cookies',
    // <if expr="chromeos">
    DEVICE: 'device',
    // </if>
    PRIVET: 'privet',
    EXTENSION: 'extension',
    CROS: 'chrome_os',
  };

  /**
   * Cloud Print origins.
   * @const {!Array<!print_preview.DestinationOrigin>}
   */
  const CloudOrigins = [
    DestinationOrigin.COOKIES,
    // <if expr="chromeos">
    DestinationOrigin.DEVICE,
    // </if>
  ];

  /**
   * Enumeration of the connection statuses of printer destinations.
   * @enum {string}
   */
  const DestinationConnectionStatus = {
    DORMANT: 'DORMANT',
    OFFLINE: 'OFFLINE',
    ONLINE: 'ONLINE',
    UNKNOWN: 'UNKNOWN',
    UNREGISTERED: 'UNREGISTERED',
  };

  /**
   * Enumeration specifying whether a destination is provisional and the reason
   * the destination is provisional.
   * @enum {string}
   */
  const DestinationProvisionalType = {
    // Destination is not provisional.
    NONE: 'NONE',
    // User has to grant USB access for the destination to its provider.
    // Used for destinations with extension origin.
    NEEDS_USB_PERMISSION: 'NEEDS_USB_PERMISSION',
  };

  /**
   * Enumeration specifying the status of a destination's 2018 certificate.
   * Values UNKNOWN and YES are returned directly by the GCP server.
   * @enum {string}
   */
  const DestinationCertificateStatus = {
    // Destination is not a cloud printer or no status was retrieved.
    NONE: 'NONE',
    // Printer does not have a valid 2018 certificate. Currently unused, to be
    // sent by GCP server.
    NO: 'NO',
    // Printer may or may not have a valid certificate. Sent by GCP server.
    UNKNOWN: 'UNKNOWN',
    // Printer has a valid 2018 certificate. Sent by GCP server.
    YES: 'YES',
  };

  /**
   * @typedef {{
   *   display_name: (string),
   *   type: (string | undefined),
   *   value: (number | string | boolean),
   *   is_default: (boolean | undefined),
   * }}
   */
  let VendorCapabilitySelectOption;

  /**
   * Specifies a custom vendor capability.
   * @typedef {{
   *   id: (string),
   *   display_name: (string),
   *   localized_display_name: (string | undefined),
   *   type: (string),
   *   select_cap: ({
   *     option: (Array<!print_preview.VendorCapabilitySelectOption>|undefined),
   *   }|undefined),
   *   typed_value_cap: ({
   *     default: (number | string | boolean | undefined),
   *   }|undefined),
   *   range_cap: ({
   *     default: (number),
   *   }),
   * }}
   */
  let VendorCapability;

  /**
   * Capabilities of a print destination represented in a CDD.
   * Pin capability is not a part of standard CDD description and is defined
   * only on Chrome OS.
   *
   * @typedef {{
   *   vendor_capability: !Array<!print_preview.VendorCapability>,
   *   collate: ({default: (boolean|undefined)}|undefined),
   *   color: ({
   *     option: !Array<{
   *       type: (string|undefined),
   *       vendor_id: (string|undefined),
   *       custom_display_name: (string|undefined),
   *       is_default: (boolean|undefined)
   *     }>
   *   }|undefined),
   *   copies: ({default: (number|undefined),
   *             max: (number|undefined)}|undefined),
   *   duplex: ({option: !Array<{type: (string|undefined),
   *                             is_default: (boolean|undefined)}>}|undefined),
   *   page_orientation: ({
   *     option: !Array<{type: (string|undefined),
   *                      is_default: (boolean|undefined)}>
   *   }|undefined),
   *   media_size: ({
   *     option: !Array<{
   *       type: (string|undefined),
   *       vendor_id: (string|undefined),
   *       custom_display_name: (string|undefined),
   *       is_default: (boolean|undefined)
   *     }>
   *   }|undefined),
   *   dpi: ({
   *     option: !Array<{
   *       vendor_id: (string|undefined),
   *       horizontal_dpi: number,
   *       vertical_dpi: number,
   *       is_default: (boolean|undefined)
   *     }>
   *   }|undefined),
   *   pin: ({supported: (boolean|undefined)}|undefined)
   * }}
   */
  let CddCapabilities;

  /**
   * The CDD (Cloud Device Description) describes the capabilities of a print
   * destination.
   *
   * @typedef {{
   *   version: string,
   *   printer: !print_preview.CddCapabilities,
   * }}
   */
  let Cdd;

  /**
   * Enumeration of color modes used by Chromium.
   * @enum {number}
   */
  const ColorMode = {
    GRAY: 1,
    COLOR: 2,
  };

  // <if expr="chromeos">
  /**
   * Enumeration of color mode restrictions used by Chromium.
   * This has to coincide with |printing::ColorModeRestriction| as defined in
   * printing/backend/printing_restrictions.h
   * @enum {number}
   */
  const ColorModeRestriction = {
    UNSET: 0x0,
    MONOCHROME: 0x1,
    COLOR: 0x2,
  };

  /**
   * Enumeration of duplex mode restrictions used by Chromium.
   * This has to coincide with |printing::DuplexModeRestriction| as defined in
   * printing/backend/printing_restrictions.h
   * @enum {number}
   */
  const DuplexModeRestriction = {
    UNSET: 0x0,
    SIMPLEX: 0x1,
    LONG_EDGE: 0x2,
    SHORT_EDGE: 0x4,
    DUPLEX: 0x6,
  };

  /**
   * Enumeration of PIN printing mode restrictions used by Chromium.
   * This has to coincide with |printing::PinModeRestriction| as defined in
   * printing/backend/printing_restrictions.h
   * @enum {number}
   */
  const PinModeRestriction = {
    UNSET: 0,
    PIN: 1,
    NO_PIN: 2,
  };

  /**
   * Policies affecting a destination.
   * @typedef {{
   *   allowedColorModes: ?print_preview.ColorModeRestriction,
   *   allowedDuplexModes: ?print_preview.DuplexModeRestriction,
   *   allowedPinMode: ?print_preview.PinModeRestriction,
   *   defaultColorMode: ?print_preview.ColorModeRestriction,
   *   defaultDuplexMode: ?print_preview.DuplexModeRestriction,
   *   defaultPinMode: ?print_preview.PinModeRestriction,
   * }}
   */
  let Policies;
  // </if>

  /**
   * @typedef {{id: string,
   *            origin: print_preview.DestinationOrigin,
   *            account: string,
   *            capabilities: ?print_preview.Cdd,
   *            displayName: string,
   *            extensionId: string,
   *            extensionName: string}}
   */
  let RecentDestination;

  /**
   * Creates a |RecentDestination| to represent |destination| in the app
   * state.
   * @param {!print_preview.Destination} destination The destination to store.
   * @return {!print_preview.RecentDestination}
   */
  function makeRecentDestination(destination) {
    return {
      id: destination.id,
      origin: destination.origin,
      account: destination.account || '',
      capabilities: destination.capabilities,
      displayName: destination.displayName || '',
      extensionId: destination.extensionId || '',
      extensionName: destination.extensionName || '',
    };
  }

  /**
   * @param {string} id Destination id.
   * @param {!print_preview.DestinationOrigin} origin Destination origin.
   * @param {string} account User account destination is registered for.
   * @return {string} A key that maps to a destination with the selected |id|,
   *     |origin|, and |account|.
   */
  function createDestinationKey(id, origin, account) {
    return `${id}/${origin}/${account}`;
  }

  /**
   * @param {!print_preview.RecentDestination} recentDestination
   * @return {string} A key that maps to a destination with parameters matching
   *     |recentDestination|.
   */
  function createRecentDestinationKey(recentDestination) {
    return print_preview.createDestinationKey(
        recentDestination.id, recentDestination.origin,
        recentDestination.account);
  }

  class Destination {
    /**
     * Print destination data object that holds data for both local and cloud
     * destinations.
     * @param {string} id ID of the destination.
     * @param {!print_preview.DestinationType} type Type of the destination.
     * @param {!print_preview.DestinationOrigin} origin Origin of the
     *     destination.
     * @param {string} displayName Display name of the destination.
     * @param {!print_preview.DestinationConnectionStatus} connectionStatus
     *     Connection status of the print destination.
     * @param {{tags: (Array<string>|undefined),
     *          isOwned: (boolean|undefined),
     *          isEnterprisePrinter: (boolean|undefined),
     *          account: (string|undefined),
     *          lastAccessTime: (number|undefined),
     *          cloudID: (string|undefined),
     *          provisionalType:
     *              (print_preview.DestinationProvisionalType|undefined),
     *          extensionId: (string|undefined),
     *          extensionName: (string|undefined),
     *          description: (string|undefined),
     *          certificateStatus:
     *              (print_preview.DestinationCertificateStatus|undefined),
     *          policies: (print_preview.Policies|undefined),
     *         }=} opt_params Optional
     *     parameters for the destination.
     */
    constructor(id, type, origin, displayName, connectionStatus, opt_params) {
      /**
       * ID of the destination.
       * @private {string}
       */
      this.id_ = id;

      /**
       * Type of the destination.
       * @private {!print_preview.DestinationType}
       */
      this.type_ = type;

      /**
       * Origin of the destination.
       * @private {!print_preview.DestinationOrigin}
       */
      this.origin_ = origin;

      /**
       * Display name of the destination.
       * @private {string}
       */
      this.displayName_ = displayName || '';

      /**
       * Tags associated with the destination.
       * @private {!Array<string>}
       */
      this.tags_ = (opt_params && opt_params.tags) || [];

      /**
       * Print capabilities of the destination.
       * @private {?print_preview.Cdd}
       */
      this.capabilities_ = null;

      /**
       * Policies affecting the destination.
       * @private {?print_preview.Policies}
       */
      this.policies_ = (opt_params && opt_params.policies) || null;

      /**
       * Whether the destination is owned by the user.
       * @private {boolean}
       */
      this.isOwned_ = (opt_params && opt_params.isOwned) || false;

      /**
       * Whether the destination is an enterprise policy controlled printer.
       * @private {boolean}
       */
      this.isEnterprisePrinter_ =
          (opt_params && opt_params.isEnterprisePrinter) || false;

      /**
       * Account this destination is registered for, if known.
       * @private {string}
       */
      this.account_ = (opt_params && opt_params.account) || '';

      /**
       * Cache of destination location fetched from tags.
       * @private {?string}
       */
      this.location_ = null;

      /**
       * Printer description.
       * @private {string}
       */
      this.description_ = (opt_params && opt_params.description) || '';

      /**
       * Connection status of the destination.
       * @private {!print_preview.DestinationConnectionStatus}
       */
      this.connectionStatus_ = connectionStatus;

      /**
       * Number of milliseconds since the epoch when the printer was last
       * accessed.
       * @private {number}
       */
      this.lastAccessTime_ =
          (opt_params && opt_params.lastAccessTime) || Date.now();

      /**
       * Cloud ID for Privet printers.
       * @private {string}
       */
      this.cloudID_ = (opt_params && opt_params.cloudID) || '';

      /**
       * Extension ID for extension managed printers.
       * @private {string}
       */
      this.extensionId_ = (opt_params && opt_params.extensionId) || '';

      /**
       * Extension name for extension managed printers.
       * @private {string}
       */
      this.extensionName_ = (opt_params && opt_params.extensionName) || '';

      /**
       * Different from {@code print_preview.DestinationProvisionalType.NONE} if
       * the destination is provisional. Provisional destinations cannot be
       * selected as they are, but have to be resolved first (i.e. extra steps
       * have to be taken to get actual destination properties, which should
       * replace the provisional ones). Provisional destination resolvment flow
       * will be started when the user attempts to select the destination in
       * search UI.
       * @private {print_preview.DestinationProvisionalType}
       */
      this.provisionalType_ = (opt_params && opt_params.provisionalType) ||
          DestinationProvisionalType.NONE;

      /**
       * Printer 2018 certificate status
       * @private {print_preview.DestinationCertificateStatus}
       */
      this.certificateStatus_ = opt_params && opt_params.certificateStatus ||
          print_preview.DestinationCertificateStatus.NONE;

      assert(
          this.provisionalType_ !=
                  DestinationProvisionalType.NEEDS_USB_PERMISSION ||
              this.isExtension,
          'Provisional USB destination only supprted with extension origin.');

      /**
       * @private {!Array<string>} List of capability types considered color.
       * @const
       */
      this.COLOR_TYPES_ = ['STANDARD_COLOR', 'CUSTOM_COLOR'];

      /**
       * @private {!Array<string>} List of capability types considered
       *     monochrome.
       * @const
       */
      this.MONOCHROME_TYPES_ = ['STANDARD_MONOCHROME', 'CUSTOM_MONOCHROME'];
    }

    /** @return {string} ID of the destination. */
    get id() {
      return this.id_;
    }

    /** @return {!print_preview.DestinationType} Type of the destination. */
    get type() {
      return this.type_;
    }

    /**
     * @return {!print_preview.DestinationOrigin} Origin of the destination.
     */
    get origin() {
      return this.origin_;
    }

    /** @return {string} Display name of the destination. */
    get displayName() {
      return this.displayName_;
    }

    /**
     * @return {boolean} Whether the user owns the destination. Only applies to
     *     cloud-based destinations.
     */
    get isOwned() {
      return this.isOwned_;
    }

    /**
     * @return {string} Account this destination is registered for, if known.
     */
    get account() {
      return this.account_;
    }

    /** @return {boolean} Whether the destination is local or cloud-based. */
    get isLocal() {
      return this.origin_ == DestinationOrigin.LOCAL ||
          this.origin_ == DestinationOrigin.EXTENSION ||
          this.origin_ == DestinationOrigin.CROS ||
          (this.origin_ == DestinationOrigin.PRIVET &&
           this.connectionStatus_ != DestinationConnectionStatus.UNREGISTERED);
    }

    /** @return {boolean} Whether the destination is a Privet local printer */
    get isPrivet() {
      return this.origin_ == DestinationOrigin.PRIVET;
    }

    /**
     * @return {boolean} Whether the destination is an extension managed
     *     printer.
     */
    get isExtension() {
      return this.origin_ == DestinationOrigin.EXTENSION;
    }

    /**
     * @return {string} The location of the destination, or an empty string if
     *     the location is unknown.
     */
    get location() {
      if (this.location_ == null) {
        this.location_ = '';
        this.tags_.some(tag => {
          return Destination.LOCATION_TAG_PREFIXES.some(prefix => {
            if (tag.startsWith(prefix)) {
              this.location_ = tag.substring(prefix.length) || '';
              return true;
            }
          });
        });
      }
      return this.location_;
    }

    /**
     * @return {string} The description of the destination, or an empty string,
     *     if it was not provided.
     */
    get description() {
      return this.description_;
    }

    /**
     * @return {string} Most relevant string to help user to identify this
     *     destination.
     */
    get hint() {
      if (this.id_ == Destination.GooglePromotedId.DOCS) {
        return this.account_;
      }
      return this.location || this.extensionName || this.description;
    }

    /** @return {!Array<string>} Tags associated with the destination. */
    get tags() {
      return this.tags_.slice(0);
    }

    /** @return {string} Cloud ID associated with the destination */
    get cloudID() {
      return this.cloudID_;
    }

    /**
     * @return {string} Extension ID associated with the destination. Non-empty
     *     only for extension managed printers.
     */
    get extensionId() {
      return this.extensionId_;
    }

    /**
     * @return {string} Extension name associated with the destination.
     *     Non-empty only for extension managed printers.
     */
    get extensionName() {
      return this.extensionName_;
    }

    /** @return {?print_preview.Cdd} Print capabilities of the destination. */
    get capabilities() {
      return this.capabilities_;
    }

    /**
     * @param {?print_preview.Cdd} capabilities Print capabilities of the
     *     destination.
     */
    set capabilities(capabilities) {
      if (capabilities) {
        this.capabilities_ = capabilities;
      }
    }

    // <if expr="chromeos">
    /**
     * @return {?print_preview.Policies} Print policies affecting the
     *     destination.
     */
    get policies() {
      return this.policies_;
    }

    /**
     * @param {?print_preview.Policies} policies Print policies affecting the
     *     destination.
     */
    set policies(policies) {
      this.policies_ = policies;
    }
    // </if>

    /**
     * @return {!print_preview.DestinationConnectionStatus} Connection status
     *     of the print destination.
     */
    get connectionStatus() {
      return this.connectionStatus_;
    }

    /**
     * @param {!print_preview.DestinationConnectionStatus} status Connection
     *     status of the print destination.
     */
    set connectionStatus(status) {
      this.connectionStatus_ = status;
    }

    /**
     * @return {boolean} Whether the destination has an invalid 2018
     *     certificate.
     */
    get hasInvalidCertificate() {
      return this.certificateStatus_ ==
          print_preview.DestinationCertificateStatus.NO;
    }

    /**
     * @return {boolean} Whether the destination should display an invalid
     *     certificate UI warning in the selection dialog and cause a UI
     *     warning to appear in the preview area when selected.
     */
    get shouldShowInvalidCertificateError() {
      return this.certificateStatus_ ==
          print_preview.DestinationCertificateStatus.NO &&
          !loadTimeData.getBoolean('isEnterpriseManaged');
    }

    /** @return {boolean} Whether the destination is considered offline. */
    get isOffline() {
      return [
        print_preview.DestinationConnectionStatus.OFFLINE,
        print_preview.DestinationConnectionStatus.DORMANT
      ].includes(this.connectionStatus_);
    }

    /**
     * @return {boolean} Whether the destination is offline or has an invalid
     *     certificate.
     */
    get isOfflineOrInvalid() {
      return this.isOffline || this.shouldShowInvalidCertificateError;
    }

    /** @return {boolean} Whether the destination is ready to be selected. */
    get readyForSelection() {
      return (!cr.isChromeOS || this.origin_ != DestinationOrigin.CROS ||
              this.capabilities_ != null) &&
          !this.isProvisional;
    }

    /**
     * @return {string} Human readable status for a destination that is offline
     *     or has a bad certificate.
     */
    get connectionStatusText() {
      if (!this.isOfflineOrInvalid) {
        return '';
      }
      const offlineDurationMs = Date.now() - this.lastAccessTime_;
      let statusMessageId;
      if (this.shouldShowInvalidCertificateError) {
        statusMessageId = 'noLongerSupported';
      } else if (offlineDurationMs > 31622400000.0) {  // One year.
        statusMessageId = 'offlineForYear';
      } else if (offlineDurationMs > 2678400000.0) {  // One month.
        statusMessageId = 'offlineForMonth';
      } else if (offlineDurationMs > 604800000.0) {  // One week.
        statusMessageId = 'offlineForWeek';
      } else {
        statusMessageId = 'offline';
      }
      return loadTimeData.getString(statusMessageId);
    }

    /**
     * @return {number} Number of milliseconds since the epoch when the printer
     *     was last accessed.
     */
    get lastAccessTime() {
      return this.lastAccessTime_;
    }

    /** @return {string} Path to the SVG for the destination's icon. */
    get icon() {
      if (this.id_ == Destination.GooglePromotedId.DOCS) {
        return 'print-preview:save-to-drive';
      }
      if (this.id_ == Destination.GooglePromotedId.SAVE_AS_PDF) {
        return 'cr:insert-drive-file';
      }
      if (this.isEnterprisePrinter) {
        return 'print-preview:business';
      }
      if (this.isLocal) {
        return 'print-preview:print';
      }
      if (this.type_ == print_preview.DestinationType.MOBILE && this.isOwned_) {
        return 'print-preview:smartphone';
      }
      if (this.type_ == print_preview.DestinationType.MOBILE) {
        return 'print-preview:smartphone';
      }
      if (this.isOwned_) {
        return 'print-preview:print';
      }
      return 'print-preview:printer-shared';
    }

    /**
     * @return {!Array<string>} Properties (besides display name) to match
     *     search queries against.
     */
    get extraPropertiesToMatch() {
      return [this.location, this.description];
    }

    /**
     * Matches a query against the destination.
     * @param {!RegExp} query Query to match against the destination.
     * @return {boolean} {@code true} if the query matches this destination,
     *     {@code false} otherwise.
     */
    matches(query) {
      return !!this.displayName_.match(query) ||
          !!this.extensionName_.match(query) ||
          this.extraPropertiesToMatch.some(p => p.match(query));
    }

    /**
     * Gets the destination's provisional type.
     * @return {print_preview.DestinationProvisionalType}
     */
    get provisionalType() {
      return this.provisionalType_;
    }

    /**
     * Gets the destination's certificate status.
     * @return {print_preview.DestinationCertificateStatus}
     */
    get certificateStatus() {
      return this.certificateStatus_;
    }

    /**
     * Whether the destinaion is provisional.
     * @return {boolean}
     */
    get isProvisional() {
      return this.provisionalType_ != DestinationProvisionalType.NONE;
    }

    /**
     * Whether the printer is enterprise policy controlled printer.
     * @return {boolean}
     */
    get isEnterprisePrinter() {
      return this.isEnterprisePrinter_;
    }

    /**
     * @return {Object} Color capability of this destination.
     * @private
     */
    colorCapability_() {
      return this.capabilities && this.capabilities.printer &&
              this.capabilities.printer.color ?
          this.capabilities.printer.color :
          null;
    }

    // <if expr="chromeos">
    /**
     * @return {?print_preview.ColorModeRestriction} Color mode set by policy.
     */
    get colorPolicy() {
      return this.policies && this.policies.allowedColorModes ?
          this.policies.allowedColorModes :
          null;
    }

    /**
     * @return {?print_preview.DuplexModeRestriction} Duplex modes allowed by
     *     policy.
     */
    get duplexPolicy() {
      return this.policies && this.policies.allowedDuplexModes ?
          this.policies.allowedDuplexModes :
          null;
    }

    /**
     * @return {?print_preview.PinModeRestriction} Pin mode allowed by policy.
     */
    get pinPolicy() {
      return this.policies && this.policies.allowedPinModes ?
          this.policies.allowedPinModes :
          null;
    }
    // </if>

    /**
     * @return {boolean} Whether the printer supports both black and white and
     *     color printing.
     */
    get hasColorCapability() {
      const capability = this.colorCapability_();
      if (!capability || !capability.option) {
        return false;
      }
      let hasColor = false;
      let hasMonochrome = false;
      capability.option.forEach(option => {
        const type = assert(option.type);
        hasColor = hasColor || this.COLOR_TYPES_.includes(option.type);
        hasMonochrome =
            hasMonochrome || this.MONOCHROME_TYPES_.includes(option.type);
      });
      return hasColor && hasMonochrome;
    }

    // <if expr="chromeos">
    /**
     * @return {?print_preview.ColorModeRestriction} Value of default color
     *     setting given by policy.
     */
    get defaultColorPolicy() {
      return this.policies && this.policies.defaultColorMode;
    }

    /**
     * @return {?print_preview.DuplexModeRestriction} Value of default duplex
     *     setting given by policy.
     */
    get defaultDuplexPolicy() {
      return this.policies && this.policies.defaultDuplexMode;
    }

    /**
     * @return {?print_preview.PinModeRestriction} Value of default pin setting
     *     given by policy.
     */
    get defaultPinPolicy() {
      return this.policies && this.policies.defaultPinMode;
    }
    // </if>

    /**
     * @param {boolean} isColor Whether to use a color printing mode.
     * @return {Object} Selected color option.
     */
    getSelectedColorOption(isColor) {
      const typesToLookFor =
          isColor ? this.COLOR_TYPES_ : this.MONOCHROME_TYPES_;
      const capability = this.colorCapability_();
      if (!capability || !capability.option) {
        return null;
      }
      for (let i = 0; i < typesToLookFor.length; i++) {
        const matchingOptions = capability.option.filter(option => {
          return option.type == typesToLookFor[i];
        });
        if (matchingOptions.length > 0) {
          return matchingOptions[0];
        }
      }
      return null;
    }

    /**
     * @param {boolean} isColor Whether to use a color printing mode.
     * @return {number} Native color model of the destination.
     */
    getNativeColorModel(isColor) {
      // For non-local printers or printers without capability, native color
      // model is ignored.
      const capability = this.colorCapability_();
      if (!capability || !capability.option || !this.isLocal) {
        return isColor ? print_preview.ColorMode.COLOR :
                         print_preview.ColorMode.GRAY;
      }
      const selected = this.getSelectedColorOption(isColor);
      const mode = parseInt(selected ? selected.vendor_id : null, 10);
      if (isNaN(mode)) {
        return isColor ? print_preview.ColorMode.COLOR :
                         print_preview.ColorMode.GRAY;
      }
      return mode;
    }

    /**
     * @return {Object} The default color option for the destination.
     */
    get defaultColorOption() {
      const capability = this.colorCapability_();
      if (!capability || !capability.option) {
        return null;
      }
      const defaultOptions = capability.option.filter(option => {
        return option.is_default;
      });
      return defaultOptions.length != 0 ? defaultOptions[0] : null;
    }

    /** @return {string} A unique identifier for this destination. */
    get key() {
      return `${this.id_}/${this.origin_}/${this.account_}`;
    }
  }

  /**
   * Prefix of the location destination tag.
   * @type {!Array<string>}
   * @const
   */
  Destination.LOCATION_TAG_PREFIXES =
      ['__cp__location=', '__cp__printer-location='];

  /**
   * Enumeration of Google-promoted destination IDs.
   * @enum {string}
   */
  Destination.GooglePromotedId = {
    DOCS: '__google__docs',
    SAVE_AS_PDF: 'Save as PDF'
  };

  // Export
  return {
    CddCapabilities: CddCapabilities,
    Cdd: Cdd,
    CloudOrigins: CloudOrigins,
    ColorMode: ColorMode,
    createDestinationKey: createDestinationKey,
    createRecentDestinationKey: createRecentDestinationKey,
    DestinationCertificateStatus: DestinationCertificateStatus,
    DestinationConnectionStatus: DestinationConnectionStatus,
    Destination: Destination,
    DestinationOrigin: DestinationOrigin,
    DestinationProvisionalType: DestinationProvisionalType,
    DestinationType: DestinationType,
    makeRecentDestination: makeRecentDestination,
    RecentDestination: RecentDestination,
    VendorCapabilitySelectOption: VendorCapabilitySelectOption,
    VendorCapability: VendorCapability,

    // <if expr="chromeos">
    ColorModeRestriction: ColorModeRestriction,
    DuplexModeRestriction: DuplexModeRestriction,
    PinModeRestriction: PinModeRestriction,
    Policies: Policies,
    // </if>
  };
});