1 // Copyright (c) 2012 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.
5 cr.define('cloudprint', function() {
8 /** Namespace which contains a method to parse cloud destinations directly. */
9 function CloudDestinationParser() {};
12 * Enumeration of cloud destination field names.
16 CloudDestinationParser.Field_ = {
17 LAST_ACCESS: 'accessTime',
18 CAPABILITIES: 'capabilities',
19 CONNECTION_STATUS: 'connectionStatus',
20 DISPLAY_NAME: 'displayName',
22 IS_TOS_ACCEPTED: 'isTosAccepted',
28 * Special tag that denotes whether the destination has been recently used.
33 CloudDestinationParser.RECENT_TAG_ = '^recent';
36 * Special tag that denotes whether the destination is owned by the user.
41 CloudDestinationParser.OWNED_TAG_ = '^own';
44 * Enumeration of cloud destination types that are supported by print preview.
48 CloudDestinationParser.CloudType_ = {
49 ANDROID: 'ANDROID_CHROME_SNAPSHOT',
51 IOS: 'IOS_CHROME_SNAPSHOT'
55 * Parses a destination from JSON from a Google Cloud Print search or printer
57 * @param {!Object} json Object that represents a Google Cloud Print search or
59 * @param {!print_preview.Destination.Origin} origin The origin of the
61 * @return {!print_preview.Destination} Parsed destination.
63 CloudDestinationParser.parse = function(json, origin) {
64 if (!json.hasOwnProperty(CloudDestinationParser.Field_.ID) ||
65 !json.hasOwnProperty(CloudDestinationParser.Field_.TYPE) ||
66 !json.hasOwnProperty(CloudDestinationParser.Field_.DISPLAY_NAME)) {
67 throw Error('Cloud destination does not have an ID or a display name');
69 var id = json[CloudDestinationParser.Field_.ID];
70 var tags = json[CloudDestinationParser.Field_.TAGS] || [];
71 var connectionStatus =
72 json[CloudDestinationParser.Field_.CONNECTION_STATUS] ||
73 print_preview.Destination.ConnectionStatus.UNKNOWN;
74 var optionalParams = {
76 isOwned: arrayContains(tags, CloudDestinationParser.OWNED_TAG_),
77 lastAccessTime: parseInt(
78 json[CloudDestinationParser.Field_.LAST_ACCESS], 10) || Date.now(),
79 isTosAccepted: (id == print_preview.Destination.GooglePromotedId.FEDEX) ?
80 json[CloudDestinationParser.Field_.IS_TOS_ACCEPTED] : null,
83 var cloudDest = new print_preview.Destination(
85 CloudDestinationParser.parseType_(
86 json[CloudDestinationParser.Field_.TYPE]),
88 json[CloudDestinationParser.Field_.DISPLAY_NAME],
89 arrayContains(tags, CloudDestinationParser.RECENT_TAG_) /*isRecent*/,
92 if (json.hasOwnProperty(CloudDestinationParser.Field_.CAPABILITIES)) {
93 cloudDest.capabilities = /*@type {!print_preview.Cdd}*/ (
94 json[CloudDestinationParser.Field_.CAPABILITIES]);
100 * Parses the destination type.
101 * @param {string} typeStr Destination type given by the Google Cloud Print
103 * @return {!print_preview.Destination.Type} Destination type.
106 CloudDestinationParser.parseType_ = function(typeStr) {
107 if (typeStr == CloudDestinationParser.CloudType_.ANDROID ||
108 typeStr == CloudDestinationParser.CloudType_.IOS) {
109 return print_preview.Destination.Type.MOBILE;
110 } else if (typeStr == CloudDestinationParser.CloudType_.DOCS) {
111 return print_preview.Destination.Type.GOOGLE_PROMOTED;
113 return print_preview.Destination.Type.GOOGLE;
119 CloudDestinationParser: CloudDestinationParser