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
|
// 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';
/**
* A data store that stores destinations and dispatches events when the data
* store changes.
* @param {!print_preview.NativeLayer} nativeLayer Used to fetch local print
* destinations.
* @param {!print_preview.UserInfo} userInfo User information repository.
* @param {!print_preview.AppState} appState Application state.
* @param {!print_preview.Metrics} metrics Metrics.
* @constructor
* @extends {cr.EventTarget}
*/
function DestinationStore(nativeLayer, userInfo, appState, metrics) {
cr.EventTarget.call(this);
/**
* Used to fetch local print destinations.
* @type {!print_preview.NativeLayer}
* @private
*/
this.nativeLayer_ = nativeLayer;
/**
* User information repository.
* @type {!print_preview.UserInfo}
* @private
*/
this.userInfo_ = userInfo;
/**
* Used to load and persist the selected destination.
* @type {!print_preview.AppState}
* @private
*/
this.appState_ = appState;
/**
* Used to track metrics.
* @type {!print_preview.AppState}
* @private
*/
this.metrics_ = metrics;
/**
* Internal backing store for the data store.
* @type {!Array.<!print_preview.Destination>}
* @private
*/
this.destinations_ = [];
/**
* Cache used for constant lookup of destinations by origin and id.
* @type {object.<string, !print_preview.Destination>}
* @private
*/
this.destinationMap_ = {};
/**
* Currently selected destination.
* @type {print_preview.Destination}
* @private
*/
this.selectedDestination_ = null;
/**
* Whether the destination store will auto select the destination that
* matches the last used destination stored in appState_.
* @type {boolean}
* @private
*/
this.isInAutoSelectMode_ = false;
/**
* Event tracker used to track event listeners of the destination store.
* @type {!EventTracker}
* @private
*/
this.tracker_ = new EventTracker();
/**
* Used to fetch cloud-based print destinations.
* @type {print_preview.CloudPrintInterface}
* @private
*/
this.cloudPrintInterface_ = null;
/**
* Maps user account to the list of origins for which destinations are
* already loaded.
* @type {!Object.<string, Array.<print_preview.Destination.Origin>>}
* @private
*/
this.loadedCloudOrigins_ = {};
/**
* ID of a timeout after the initial destination ID is set. If no inserted
* destination matches the initial destination ID after the specified
* timeout, the first destination in the store will be automatically
* selected.
* @type {?number}
* @private
*/
this.autoSelectTimeout_ = null;
/**
* Whether a search for local destinations is in progress.
* @type {boolean}
* @private
*/
this.isLocalDestinationSearchInProgress_ = false;
/**
* Whether the destination store has already loaded or is loading all local
* destinations.
* @type {boolean}
* @private
*/
this.hasLoadedAllLocalDestinations_ = false;
/**
* Whether a search for privet destinations is in progress.
* @type {boolean}
* @private
*/
this.isPrivetDestinationSearchInProgress_ = false;
/**
* Whether the destination store has already loaded or is loading all privet
* destinations.
* @type {boolean}
* @private
*/
this.hasLoadedAllPrivetDestinations_ = false;
/**
* ID of a timeout after the start of a privet search to end that privet
* search.
* @type {?number}
* @private
*/
this.privetSearchTimeout_ = null;
/**
* MDNS service name of destination that we are waiting to register.
* @type {?string}
* @private
*/
this.waitForRegisterDestination_ = null;
this.addEventListeners_();
this.reset_();
};
/**
* Event types dispatched by the data store.
* @enum {string}
*/
DestinationStore.EventType = {
DESTINATION_SEARCH_DONE:
'print_preview.DestinationStore.DESTINATION_SEARCH_DONE',
DESTINATION_SEARCH_STARTED:
'print_preview.DestinationStore.DESTINATION_SEARCH_STARTED',
DESTINATION_SELECT: 'print_preview.DestinationStore.DESTINATION_SELECT',
DESTINATIONS_INSERTED:
'print_preview.DestinationStore.DESTINATIONS_INSERTED',
CACHED_SELECTED_DESTINATION_INFO_READY:
'print_preview.DestinationStore.CACHED_SELECTED_DESTINATION_INFO_READY',
SELECTED_DESTINATION_CAPABILITIES_READY:
'print_preview.DestinationStore.SELECTED_DESTINATION_CAPABILITIES_READY'
};
/**
* Delay in milliseconds before the destination store ignores the initial
* destination ID and just selects any printer (since the initial destination
* was not found).
* @type {number}
* @const
* @private
*/
DestinationStore.AUTO_SELECT_TIMEOUT_ = 15000;
/**
* Amount of time spent searching for privet destination, in milliseconds.
* @type {number}
* @const
* @private
*/
DestinationStore.PRIVET_SEARCH_DURATION_ = 2000;
/**
* Localizes printer capabilities.
* @param {!Object} capabilities Printer capabilities to localize.
* @return {!Object} Localized capabilities.
* @private
*/
DestinationStore.localizeCapabilities_ = function(capabilities) {
var mediaSize = capabilities.printer.media_size;
if (mediaSize) {
var mediaDisplayNames = {
'ISO_A4': 'A4',
'ISO_A3': 'A3',
'NA_LETTER': 'Letter',
'NA_LEGAL': 'Legal',
'NA_LEDGER': 'Tabloid'
};
for (var i = 0, media; media = mediaSize.option[i]; i++) {
media.custom_display_name = mediaDisplayNames[media.name] || media.name;
}
}
return capabilities;
};
DestinationStore.prototype = {
__proto__: cr.EventTarget.prototype,
/**
* @param {string=} opt_account Account to filter destinations by. When
* omitted, all destinations are returned.
* @return {!Array.<!print_preview.Destination>} List of destinations
* accessible by the {@code account}.
*/
destinations: function(opt_account) {
if (opt_account) {
return this.destinations_.filter(function(destination) {
return !destination.account || destination.account == opt_account;
});
} else {
return this.destinations_.slice(0);
}
},
/**
* @return {print_preview.Destination} The currently selected destination or
* {@code null} if none is selected.
*/
get selectedDestination() {
return this.selectedDestination_;
},
/**
* @return {boolean} Whether a search for local destinations is in progress.
*/
get isLocalDestinationSearchInProgress() {
return this.isLocalDestinationSearchInProgress_ ||
this.isPrivetDestinationSearchInProgress_;
},
/**
* @return {boolean} Whether a search for cloud destinations is in progress.
*/
get isCloudDestinationSearchInProgress() {
return this.cloudPrintInterface_ &&
this.cloudPrintInterface_.isCloudDestinationSearchInProgress;
},
/**
* Initializes the destination store. Sets the initially selected
* destination. If any inserted destinations match this ID, that destination
* will be automatically selected. This method must be called after the
* print_preview.AppState has been initialized.
* @private
*/
init: function() {
this.isInAutoSelectMode_ = true;
if (!this.appState_.selectedDestinationId ||
!this.appState_.selectedDestinationOrigin) {
this.onAutoSelectFailed_();
} else {
var key = this.getDestinationKey_(
this.appState_.selectedDestinationOrigin,
this.appState_.selectedDestinationId,
this.appState_.selectedDestinationAccount);
var candidate = this.destinationMap_[key];
if (candidate != null) {
this.selectDestination(candidate);
} else if (this.appState_.selectedDestinationOrigin ==
print_preview.Destination.Origin.LOCAL) {
this.nativeLayer_.startGetLocalDestinationCapabilities(
this.appState_.selectedDestinationId);
} else if (this.cloudPrintInterface_ &&
(this.appState_.selectedDestinationOrigin ==
print_preview.Destination.Origin.COOKIES ||
this.appState_.selectedDestinationOrigin ==
print_preview.Destination.Origin.DEVICE)) {
this.cloudPrintInterface_.printer(
this.appState_.selectedDestinationId,
this.appState_.selectedDestinationOrigin,
this.appState_.selectedDestinationAccount);
} else if (this.appState_.selectedDestinationOrigin ==
print_preview.Destination.Origin.PRIVET) {
// TODO(noamsml): Resolve a specific printer instead of listing all
// privet printers in this case.
this.nativeLayer_.startGetPrivetDestinations();
var destinationName = this.appState_.selectedDestinationName || '';
// Create a fake selectedDestination_ that is not actually in the
// destination store. When the real destination is created, this
// destination will be overwritten.
this.selectedDestination_ = new print_preview.Destination(
this.appState_.selectedDestinationId,
print_preview.Destination.Type.LOCAL,
print_preview.Destination.Origin.PRIVET,
destinationName,
false /*isRecent*/,
print_preview.Destination.ConnectionStatus.ONLINE);
this.selectedDestination_.capabilities =
this.appState_.selectedDestinationCapabilities;
cr.dispatchSimpleEvent(
this,
DestinationStore.EventType.CACHED_SELECTED_DESTINATION_INFO_READY);
} else {
this.onAutoSelectFailed_();
}
}
},
/**
* Sets the destination store's Google Cloud Print interface.
* @param {!print_preview.CloudPrintInterface} cloudPrintInterface Interface
* to set.
*/
setCloudPrintInterface: function(cloudPrintInterface) {
this.cloudPrintInterface_ = cloudPrintInterface;
this.tracker_.add(
this.cloudPrintInterface_,
cloudprint.CloudPrintInterface.EventType.SEARCH_DONE,
this.onCloudPrintSearchDone_.bind(this));
this.tracker_.add(
this.cloudPrintInterface_,
cloudprint.CloudPrintInterface.EventType.SEARCH_FAILED,
this.onCloudPrintSearchDone_.bind(this));
this.tracker_.add(
this.cloudPrintInterface_,
cloudprint.CloudPrintInterface.EventType.PRINTER_DONE,
this.onCloudPrintPrinterDone_.bind(this));
this.tracker_.add(
this.cloudPrintInterface_,
cloudprint.CloudPrintInterface.EventType.PRINTER_FAILED,
this.onCloudPrintPrinterFailed_.bind(this));
},
/**
* @return {boolean} Whether only default cloud destinations have been
* loaded.
*/
hasOnlyDefaultCloudDestinations: function() {
// TODO: Move the logic to print_preview.
return this.destinations_.every(function(dest) {
return dest.isLocal ||
dest.id == print_preview.Destination.GooglePromotedId.DOCS ||
dest.id == print_preview.Destination.GooglePromotedId.FEDEX;
});
},
/** @param {!print_preview.Destination} Destination to select. */
selectDestination: function(destination) {
this.isInAutoSelectMode_ = false;
this.cancelAutoSelectTimeout_();
if (destination == this.selectedDestination_) {
return;
}
if (destination == null) {
this.selectedDestination_ = null;
cr.dispatchSimpleEvent(
this, DestinationStore.EventType.DESTINATION_SELECT);
return;
}
// Update and persist selected destination.
this.selectedDestination_ = destination;
this.selectedDestination_.isRecent = true;
if (destination.id == print_preview.Destination.GooglePromotedId.FEDEX &&
!destination.isTosAccepted) {
assert(this.cloudPrintInterface_ != null,
'Selected FedEx destination, but GCP API is not available');
destination.isTosAccepted = true;
this.cloudPrintInterface_.updatePrinterTosAcceptance(destination, true);
}
this.appState_.persistSelectedDestination(this.selectedDestination_);
// Adjust metrics.
if (destination.cloudID &&
this.destinations_.some(function(otherDestination) {
return otherDestination.cloudID == destination.cloudID &&
otherDestination != destination;
})) {
if (destination.isPrivet) {
this.metrics_.incrementDestinationSearchBucket(
print_preview.Metrics.DestinationSearchBucket.
PRIVET_DUPLICATE_SELECTED);
} else {
this.metrics_.incrementDestinationSearchBucket(
print_preview.Metrics.DestinationSearchBucket.
CLOUD_DUPLICATE_SELECTED);
}
}
// Notify about selected destination change.
cr.dispatchSimpleEvent(
this, DestinationStore.EventType.DESTINATION_SELECT);
// Request destination capabilities, of not known yet.
if (destination.capabilities == null) {
if (destination.isPrivet) {
this.nativeLayer_.startGetPrivetDestinationCapabilities(
destination.id);
}
else if (destination.isLocal) {
this.nativeLayer_.startGetLocalDestinationCapabilities(
destination.id);
} else {
assert(this.cloudPrintInterface_ != null,
'Cloud destination selected, but GCP is not enabled');
this.cloudPrintInterface_.printer(
destination.id, destination.origin, destination.account);
}
} else {
cr.dispatchSimpleEvent(
this,
DestinationStore.EventType.SELECTED_DESTINATION_CAPABILITIES_READY);
}
},
/**
* Selects 'Save to PDF' destination (since it always exists).
* @private
*/
selectDefaultDestination_: function() {
var destination = this.destinationMap_[this.getDestinationKey_(
print_preview.Destination.Origin.LOCAL,
print_preview.Destination.GooglePromotedId.SAVE_AS_PDF,
'')] || null;
assert(destination != null, 'Save to PDF printer not found');
this.selectDestination(destination);
},
/** Initiates loading of local print destinations. */
startLoadLocalDestinations: function() {
if (!this.hasLoadedAllLocalDestinations_) {
this.hasLoadedAllLocalDestinations_ = true;
this.nativeLayer_.startGetLocalDestinations();
this.isLocalDestinationSearchInProgress_ = true;
cr.dispatchSimpleEvent(
this, DestinationStore.EventType.DESTINATION_SEARCH_STARTED);
}
},
/** Initiates loading of privet print destinations. */
startLoadPrivetDestinations: function() {
if (!this.hasLoadedAllPrivetDestinations_) {
this.isPrivetDestinationSearchInProgress_ = true;
this.nativeLayer_.startGetPrivetDestinations();
cr.dispatchSimpleEvent(
this, DestinationStore.EventType.DESTINATION_SEARCH_STARTED);
this.privetSearchTimeout_ = setTimeout(
this.endPrivetPrinterSearch_.bind(this),
DestinationStore.PRIVET_SEARCH_DURATION_);
}
},
/**
* Initiates loading of cloud destinations.
* @param {print_preview.Destination.Origin=} opt_origin Search destinations
* for the specified origin only.
*/
startLoadCloudDestinations: function(opt_origin) {
if (this.cloudPrintInterface_ != null) {
var origins = this.loadedCloudOrigins_[this.userInfo_.activeUser] || [];
if (origins.length == 0 ||
(opt_origin && origins.indexOf(opt_origin) < 0)) {
this.cloudPrintInterface_.search(
this.userInfo_.activeUser, opt_origin);
cr.dispatchSimpleEvent(
this, DestinationStore.EventType.DESTINATION_SEARCH_STARTED);
}
}
},
/** Requests load of COOKIE based cloud destinations. */
reloadUserCookieBasedDestinations: function() {
var origins = this.loadedCloudOrigins_[this.userInfo_.activeUser] || [];
if (origins.indexOf(print_preview.Destination.Origin.COOKIES) >= 0) {
cr.dispatchSimpleEvent(
this, DestinationStore.EventType.DESTINATION_SEARCH_DONE);
} else {
this.startLoadCloudDestinations(
print_preview.Destination.Origin.COOKIES);
}
},
/**
* Wait for a privet device to be registered.
*/
waitForRegister: function(id) {
this.nativeLayer_.startGetPrivetDestinations();
this.waitForRegisterDestination_ = id;
},
/**
* Inserts {@code destination} to the data store and dispatches a
* DESTINATIONS_INSERTED event.
* @param {!print_preview.Destination} destination Print destination to
* insert.
* @private
*/
insertDestination_: function(destination) {
if (this.insertIntoStore_(destination)) {
this.destinationsInserted_(destination);
}
},
/**
* Inserts multiple {@code destinations} to the data store and dispatches
* single DESTINATIONS_INSERTED event.
* @param {!Array.<print_preview.Destination>} destinations Print
* destinations to insert.
* @private
*/
insertDestinations_: function(destinations) {
var inserted = false;
destinations.forEach(function(destination) {
inserted = this.insertIntoStore_(destination) || inserted;
}, this);
if (inserted) {
this.destinationsInserted_();
}
},
/**
* Dispatches DESTINATIONS_INSERTED event. In auto select mode, tries to
* update selected destination to match {@code appState_} settings.
* @param {print_preview.Destination=} opt_destination The only destination
* that was changed or skipped if possibly more than one destination was
* changed. Used as a hint to limit destination search scope in
* {@code isInAutoSelectMode_).
*/
destinationsInserted_: function(opt_destination) {
cr.dispatchSimpleEvent(
this, DestinationStore.EventType.DESTINATIONS_INSERTED);
if (this.isInAutoSelectMode_) {
var destinationsToSearch =
opt_destination && [opt_destination] || this.destinations_;
destinationsToSearch.some(function(destination) {
if (this.matchPersistedDestination_(destination)) {
this.selectDestination(destination);
return true;
}
}, this);
}
},
/**
* Updates an existing print destination with capabilities and display name
* information. If the destination doesn't already exist, it will be added.
* @param {!print_preview.Destination} destination Destination to update.
* @return {!print_preview.Destination} The existing destination that was
* updated or {@code null} if it was the new destination.
* @private
*/
updateDestination_: function(destination) {
assert(destination.constructor !== Array, 'Single printer expected');
var existingDestination = this.destinationMap_[this.getKey_(destination)];
if (existingDestination != null) {
existingDestination.capabilities = destination.capabilities;
} else {
this.insertDestination_(destination);
}
if (existingDestination == this.selectedDestination_ ||
destination == this.selectedDestination_) {
this.appState_.persistSelectedDestination(this.selectedDestination_);
cr.dispatchSimpleEvent(
this,
DestinationStore.EventType.SELECTED_DESTINATION_CAPABILITIES_READY);
}
return existingDestination;
},
/**
* Called when the search for Privet printers is done.
* @private
*/
endPrivetPrinterSearch_: function() {
this.nativeLayer_.stopGetPrivetDestinations();
this.isPrivetDestinationSearchInProgress_ = false;
this.hasLoadedAllPrivetDestinations_ = true;
cr.dispatchSimpleEvent(
this, DestinationStore.EventType.DESTINATION_SEARCH_DONE);
},
/**
* Inserts a destination into the store without dispatching any events.
* @return {boolean} Whether the inserted destination was not already in the
* store.
* @private
*/
insertIntoStore_: function(destination) {
var key = this.getKey_(destination);
var existingDestination = this.destinationMap_[key];
if (existingDestination == null) {
this.destinations_.push(destination);
this.destinationMap_[key] = destination;
return true;
} else if (existingDestination.connectionStatus ==
print_preview.Destination.ConnectionStatus.UNKNOWN &&
destination.connectionStatus !=
print_preview.Destination.ConnectionStatus.UNKNOWN) {
existingDestination.connectionStatus = destination.connectionStatus;
return true;
} else {
return false;
}
},
/**
* Binds handlers to events.
* @private
*/
addEventListeners_: function() {
this.tracker_.add(
this.nativeLayer_,
print_preview.NativeLayer.EventType.LOCAL_DESTINATIONS_SET,
this.onLocalDestinationsSet_.bind(this));
this.tracker_.add(
this.nativeLayer_,
print_preview.NativeLayer.EventType.CAPABILITIES_SET,
this.onLocalDestinationCapabilitiesSet_.bind(this));
this.tracker_.add(
this.nativeLayer_,
print_preview.NativeLayer.EventType.GET_CAPABILITIES_FAIL,
this.onGetCapabilitiesFail_.bind(this));
this.tracker_.add(
this.nativeLayer_,
print_preview.NativeLayer.EventType.DESTINATIONS_RELOAD,
this.onDestinationsReload_.bind(this));
this.tracker_.add(
this.nativeLayer_,
print_preview.NativeLayer.EventType.PRIVET_PRINTER_CHANGED,
this.onPrivetPrinterAdded_.bind(this));
this.tracker_.add(
this.nativeLayer_,
print_preview.NativeLayer.EventType.PRIVET_CAPABILITIES_SET,
this.onPrivetCapabilitiesSet_.bind(this));
},
/**
* Creates a local PDF print destination.
* @return {!print_preview.Destination} Created print destination.
* @private
*/
createLocalPdfPrintDestination_: function() {
return new print_preview.Destination(
print_preview.Destination.GooglePromotedId.SAVE_AS_PDF,
print_preview.Destination.Type.LOCAL,
print_preview.Destination.Origin.LOCAL,
localStrings.getString('printToPDF'),
false /*isRecent*/,
print_preview.Destination.ConnectionStatus.ONLINE);
},
/**
* Resets the state of the destination store to its initial state.
* @private
*/
reset_: function() {
this.destinations_ = [];
this.destinationMap_ = {};
this.selectDestination(null);
this.loadedCloudOrigins_ = {};
this.hasLoadedAllLocalDestinations_ = false;
// TODO(alekseys): Create PDF printer in the native code and send its
// capabilities back with other local printers.
this.insertDestination_(this.createLocalPdfPrintDestination_());
this.resetAutoSelectTimeout_();
},
/**
* Resets destination auto selection timeout.
* @private
*/
resetAutoSelectTimeout_: function() {
this.cancelAutoSelectTimeout_();
this.autoSelectTimeout_ =
setTimeout(this.onAutoSelectFailed_.bind(this),
DestinationStore.AUTO_SELECT_TIMEOUT_);
},
/**
* Cancels destination auto selection timeout.
* @private
*/
cancelAutoSelectTimeout_: function() {
if (this.autoSelectTimeout_ != null) {
clearTimeout(this.autoSelectTimeout_);
this.autoSelectTimeout_ = null;
}
},
/**
* Called when the local destinations have been got from the native layer.
* @param {Event} Contains the local destinations.
* @private
*/
onLocalDestinationsSet_: function(event) {
var localDestinations = event.destinationInfos.map(function(destInfo) {
return print_preview.LocalDestinationParser.parse(destInfo);
});
this.insertDestinations_(localDestinations);
this.isLocalDestinationSearchInProgress_ = false;
cr.dispatchSimpleEvent(
this, DestinationStore.EventType.DESTINATION_SEARCH_DONE);
},
/**
* Called when the native layer retrieves the capabilities for the selected
* local destination. Updates the destination with new capabilities if the
* destination already exists, otherwise it creates a new destination and
* then updates its capabilities.
* @param {Event} event Contains the capabilities of the local print
* destination.
* @private
*/
onLocalDestinationCapabilitiesSet_: function(event) {
var destinationId = event.settingsInfo['printerId'];
var key = this.getDestinationKey_(
print_preview.Destination.Origin.LOCAL,
destinationId,
'');
var destination = this.destinationMap_[key];
// Special case for PDF printer (until local printers capabilities are
// reported in CDD format too).
if (destinationId ==
print_preview.Destination.GooglePromotedId.SAVE_AS_PDF) {
if (destination) {
destination.capabilities = DestinationStore.localizeCapabilities_(
event.settingsInfo.capabilities);
}
} else {
var capabilities = print_preview.LocalCapabilitiesParser.parse(
event.settingsInfo);
if (destination) {
// In case there were multiple capabilities request for this local
// destination, just ignore the later ones.
if (destination.capabilities != null) {
return;
}
destination.capabilities = capabilities;
} else {
// TODO(rltoscano): This makes the assumption that the "deviceName" is
// the same as "printerName". We should include the "printerName" in
// the response. See http://crbug.com/132831.
destination = print_preview.LocalDestinationParser.parse(
{deviceName: destinationId, printerName: destinationId});
destination.capabilities = capabilities;
this.insertDestination_(destination);
}
}
if (this.selectedDestination_ &&
this.selectedDestination_.id == destinationId) {
cr.dispatchSimpleEvent(this,
DestinationStore.EventType.
SELECTED_DESTINATION_CAPABILITIES_READY);
}
},
/**
* Called when a request to get a local destination's print capabilities
* fails. If the destination is the initial destination, auto-select another
* destination instead.
* @param {Event} event Contains the destination ID that failed.
* @private
*/
onGetCapabilitiesFail_: function(event) {
console.error('Failed to get print capabilities for printer ' +
event.destinationId);
if (this.isInAutoSelectMode_ &&
this.sameAsPersistedDestination_(event.destinationId,
event.destinationOrigin)) {
this.selectDefaultDestination_();
}
},
/**
* Called when the /search call completes, either successfully or not.
* In case of success, stores fetched destinations.
* @param {Event} event Contains the request result.
* @private
*/
onCloudPrintSearchDone_: function(event) {
if (event.printers) {
this.insertDestinations_(event.printers);
}
if (event.searchDone) {
var origins = this.loadedCloudOrigins_[event.user] || [];
if (origins.indexOf(event.origin) < 0) {
this.loadedCloudOrigins_[event.user] = origins.concat([event.origin]);
}
}
cr.dispatchSimpleEvent(
this, DestinationStore.EventType.DESTINATION_SEARCH_DONE);
},
/**
* Called when /printer call completes. Updates the specified destination's
* print capabilities.
* @param {Event} event Contains detailed information about the
* destination.
* @private
*/
onCloudPrintPrinterDone_: function(event) {
this.updateDestination_(event.printer);
},
/**
* Called when the Google Cloud Print interface fails to lookup a
* destination. Selects another destination if the failed destination was
* the initial destination.
* @param {object} event Contains the ID of the destination that was failed
* to be looked up.
* @private
*/
onCloudPrintPrinterFailed_: function(event) {
if (this.isInAutoSelectMode_ &&
this.sameAsPersistedDestination_(event.destinationId,
event.destinationOrigin)) {
console.error(
'Failed to fetch last used printer caps: ' + event.destinationId);
this.selectDefaultDestination_();
}
},
/**
* Called when a Privet printer is added to the local network.
* @param {object} event Contains information about the added printer.
* @private
*/
onPrivetPrinterAdded_: function(event) {
if (event.printer.serviceName == this.waitForRegisterDestination_ &&
!event.printer.isUnregistered) {
this.waitForRegisterDestination_ = null;
this.onDestinationsReload_();
} else {
this.insertDestinations_(
print_preview.PrivetDestinationParser.parse(event.printer));
}
},
/**
* Called when capabilities for a privet printer are set.
* @param {object} event Contains the capabilities and printer ID.
* @private
*/
onPrivetCapabilitiesSet_: function(event) {
var destinationId = event.printerId;
var destinations =
print_preview.PrivetDestinationParser.parse(event.printer);
destinations.forEach(function(dest) {
dest.capabilities = event.capabilities;
this.updateDestination_(dest);
}, this);
},
/**
* Called from native layer after the user was requested to sign in, and did
* so successfully.
* @private
*/
onDestinationsReload_: function() {
this.reset_();
this.isInAutoSelectMode_ = true;
this.startLoadLocalDestinations();
this.startLoadCloudDestinations();
this.startLoadPrivetDestinations();
},
/**
* Called when auto-selection fails. Selects the first destination in store.
* @private
*/
onAutoSelectFailed_: function() {
this.cancelAutoSelectTimeout_();
this.selectDefaultDestination_();
},
// TODO(vitalybuka): Remove three next functions replacing Destination.id
// and Destination.origin by complex ID.
/**
* Returns key to be used with {@code destinationMap_}.
* @param {!print_preview.Destination.Origin} origin Destination origin.
* @return {string} id Destination id.
* @return {string} account User account destination is registered for.
* @private
*/
getDestinationKey_: function(origin, id, account) {
return origin + '/' + id + '/' + account;
},
/**
* Returns key to be used with {@code destinationMap_}.
* @param {!print_preview.Destination} destination Destination.
* @private
*/
getKey_: function(destination) {
return this.getDestinationKey_(
destination.origin, destination.id, destination.account);
},
/**
* @param {!print_preview.Destination} destination Destination to match.
* @return {boolean} Whether {@code destination} matches the last user
* selected one.
* @private
*/
matchPersistedDestination_: function(destination) {
return !this.appState_.selectedDestinationId ||
!this.appState_.selectedDestinationOrigin ||
this.sameAsPersistedDestination_(
destination.id, destination.origin);
},
/**
* @param {?string} id Id of the destination.
* @param {?string} origin Oring of the destination.
* @return {boolean} Whether destination is the same as initial.
* @private
*/
sameAsPersistedDestination_: function(id, origin) {
return id == this.appState_.selectedDestinationId &&
origin == this.appState_.selectedDestinationOrigin;
}
};
// Export
return {
DestinationStore: DestinationStore
};
});
|