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 CAPABILITIES: 'capabilities',
18 CONNECTION_STATUS: 'connectionStatus',
19 DESCRIPTION: 'description',
20 DISPLAY_NAME: 'displayName',
22 IS_TOS_ACCEPTED: 'isTosAccepted',
23 LAST_ACCESS: 'accessTime',
29 * Special tag that denotes whether the destination has been recently used.
34 CloudDestinationParser.RECENT_TAG_ = '^recent';
37 * Special tag that denotes whether the destination is owned by the user.
42 CloudDestinationParser.OWNED_TAG_ = '^own';
45 * Enumeration of cloud destination types that are supported by print preview.
49 CloudDestinationParser.CloudType_ = {
50 ANDROID: 'ANDROID_CHROME_SNAPSHOT',
52 IOS: 'IOS_CHROME_SNAPSHOT'
56 * Parses a destination from JSON from a Google Cloud Print search or printer
58 * @param {!Object} json Object that represents a Google Cloud Print search or
60 * @param {!print_preview.Destination.Origin} origin The origin of the
62 * @param {string} account The account this destination is registered for or
63 * empty string, if origin != COOKIES.
64 * @return {!print_preview.Destination} Parsed destination.
66 CloudDestinationParser.parse = function(json, origin, account) {
67 if (!json.hasOwnProperty(CloudDestinationParser.Field_.ID) ||
68 !json.hasOwnProperty(CloudDestinationParser.Field_.TYPE) ||
69 !json.hasOwnProperty(CloudDestinationParser.Field_.DISPLAY_NAME)) {
70 throw Error('Cloud destination does not have an ID or a display name');
72 var id = json[CloudDestinationParser.Field_.ID];
73 var tags = json[CloudDestinationParser.Field_.TAGS] || [];
74 var connectionStatus =
75 json[CloudDestinationParser.Field_.CONNECTION_STATUS] ||
76 print_preview.Destination.ConnectionStatus.UNKNOWN;
77 var optionalParams = {
80 isOwned: arrayContains(tags, CloudDestinationParser.OWNED_TAG_),
81 lastAccessTime: parseInt(
82 json[CloudDestinationParser.Field_.LAST_ACCESS], 10) || Date.now(),
83 isTosAccepted: (id == print_preview.Destination.GooglePromotedId.FEDEX) ?
84 json[CloudDestinationParser.Field_.IS_TOS_ACCEPTED] : null,
86 description: json[CloudDestinationParser.Field_.DESCRIPTION]
88 var cloudDest = new print_preview.Destination(
90 CloudDestinationParser.parseType_(
91 json[CloudDestinationParser.Field_.TYPE]),
93 json[CloudDestinationParser.Field_.DISPLAY_NAME],
94 arrayContains(tags, CloudDestinationParser.RECENT_TAG_) /*isRecent*/,
97 if (json.hasOwnProperty(CloudDestinationParser.Field_.CAPABILITIES)) {
98 cloudDest.capabilities = /** @type {!print_preview.Cdd} */(
99 json[CloudDestinationParser.Field_.CAPABILITIES]);
105 * Parses the destination type.
106 * @param {string} typeStr Destination type given by the Google Cloud Print
108 * @return {!print_preview.Destination.Type} Destination type.
111 CloudDestinationParser.parseType_ = function(typeStr) {
112 if (typeStr == CloudDestinationParser.CloudType_.ANDROID ||
113 typeStr == CloudDestinationParser.CloudType_.IOS) {
114 return print_preview.Destination.Type.MOBILE;
115 } else if (typeStr == CloudDestinationParser.CloudType_.DOCS) {
116 return print_preview.Destination.Type.GOOGLE_PROMOTED;
118 return print_preview.Destination.Type.GOOGLE;
122 /** Namespace which contains a method to parse printer sharing invitation. */
123 function InvitationParser() {};
126 * Enumeration of invitation field names.
130 InvitationParser.Field_ = {
132 RECEIVER: 'receiver',
137 * Enumeration of cloud destination types that are supported by print preview.
141 InvitationParser.AclType_ = {
149 * Parses printer sharing invitation from JSON from GCP invite API response.
150 * @param {!Object} json Object that represents a invitation search response.
151 * @param {string} account The account this invitation is sent for.
152 * @return {!print_preview.Invitation} Parsed invitation.
154 InvitationParser.parse = function(json, account) {
155 if (!json.hasOwnProperty(InvitationParser.Field_.SENDER) ||
156 !json.hasOwnProperty(InvitationParser.Field_.RECEIVER) ||
157 !json.hasOwnProperty(InvitationParser.Field_.PRINTER)) {
158 throw Error('Invitation does not have necessary info.');
161 var nameFormatter = function(name, scope) {
162 return name && scope ? (name + ' (' + scope + ')') : (name || scope);
165 var sender = json[InvitationParser.Field_.SENDER];
166 var senderName = nameFormatter(sender['name'], sender['email']);
168 var receiver = json[InvitationParser.Field_.RECEIVER];
169 var receiverName = '';
170 var receiverType = receiver['type'];
171 if (receiverType == InvitationParser.AclType_.USER) {
172 // It's a personal invitation, empty name indicates just that.
173 } else if (receiverType == InvitationParser.AclType_.GROUP ||
174 receiverType == InvitationParser.AclType_.DOMAIN) {
175 receiverName = nameFormatter(receiver['name'], receiver['scope']);
177 throw Error('Invitation of unsupported receiver type');
180 var destination = cloudprint.CloudDestinationParser.parse(
181 json[InvitationParser.Field_.PRINTER],
182 print_preview.Destination.Origin.COOKIES,
185 return new print_preview.Invitation(
186 senderName, receiverName, destination, receiver, account);
191 CloudDestinationParser: CloudDestinationParser,
192 InvitationParser: InvitationParser