OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 cr.define('cloudprint', function() { | 5 cr.define('cloudprint', function() { |
6 'use strict'; | 6 'use strict'; |
7 | 7 |
8 /** Namespace which contains a method to parse cloud destinations directly. */ | 8 /** Namespace which contains a method to parse cloud destinations directly. */ |
9 function CloudDestinationParser() {} | 9 function CloudDestinationParser() {} |
10 | 10 |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 } | 70 } |
71 var id = json[CloudDestinationParser.Field_.ID]; | 71 var id = json[CloudDestinationParser.Field_.ID]; |
72 var tags = json[CloudDestinationParser.Field_.TAGS] || []; | 72 var tags = json[CloudDestinationParser.Field_.TAGS] || []; |
73 var connectionStatus = | 73 var connectionStatus = |
74 json[CloudDestinationParser.Field_.CONNECTION_STATUS] || | 74 json[CloudDestinationParser.Field_.CONNECTION_STATUS] || |
75 print_preview.DestinationConnectionStatus.UNKNOWN; | 75 print_preview.DestinationConnectionStatus.UNKNOWN; |
76 var optionalParams = { | 76 var optionalParams = { |
77 account: account, | 77 account: account, |
78 tags: tags, | 78 tags: tags, |
79 isOwned: arrayContains(tags, CloudDestinationParser.OWNED_TAG_), | 79 isOwned: arrayContains(tags, CloudDestinationParser.OWNED_TAG_), |
80 lastAccessTime: parseInt( | 80 lastAccessTime: |
81 json[CloudDestinationParser.Field_.LAST_ACCESS], 10) || Date.now(), | 81 parseInt(json[CloudDestinationParser.Field_.LAST_ACCESS], 10) || |
| 82 Date.now(), |
82 cloudID: id, | 83 cloudID: id, |
83 description: json[CloudDestinationParser.Field_.DESCRIPTION] | 84 description: json[CloudDestinationParser.Field_.DESCRIPTION] |
84 }; | 85 }; |
85 var cloudDest = new print_preview.Destination( | 86 var cloudDest = new print_preview.Destination( |
86 id, | 87 id, |
87 CloudDestinationParser.parseType_( | 88 CloudDestinationParser.parseType_( |
88 json[CloudDestinationParser.Field_.TYPE]), | 89 json[CloudDestinationParser.Field_.TYPE]), |
89 origin, | 90 origin, json[CloudDestinationParser.Field_.DISPLAY_NAME], |
90 json[CloudDestinationParser.Field_.DISPLAY_NAME], | |
91 arrayContains(tags, CloudDestinationParser.RECENT_TAG_) /*isRecent*/, | 91 arrayContains(tags, CloudDestinationParser.RECENT_TAG_) /*isRecent*/, |
92 connectionStatus, | 92 connectionStatus, optionalParams); |
93 optionalParams); | |
94 if (json.hasOwnProperty(CloudDestinationParser.Field_.CAPABILITIES)) { | 93 if (json.hasOwnProperty(CloudDestinationParser.Field_.CAPABILITIES)) { |
95 cloudDest.capabilities = /** @type {!print_preview.Cdd} */( | 94 cloudDest.capabilities = /** @type {!print_preview.Cdd} */ ( |
96 json[CloudDestinationParser.Field_.CAPABILITIES]); | 95 json[CloudDestinationParser.Field_.CAPABILITIES]); |
97 } | 96 } |
98 return cloudDest; | 97 return cloudDest; |
99 }; | 98 }; |
100 | 99 |
101 /** | 100 /** |
102 * Parses the destination type. | 101 * Parses the destination type. |
103 * @param {string} typeStr Destination type given by the Google Cloud Print | 102 * @param {string} typeStr Destination type given by the Google Cloud Print |
104 * server. | 103 * server. |
105 * @return {!print_preview.DestinationType} Destination type. | 104 * @return {!print_preview.DestinationType} Destination type. |
(...skipping 11 matching lines...) Expand all Loading... |
117 }; | 116 }; |
118 | 117 |
119 /** Namespace which contains a method to parse printer sharing invitation. */ | 118 /** Namespace which contains a method to parse printer sharing invitation. */ |
120 function InvitationParser() {} | 119 function InvitationParser() {} |
121 | 120 |
122 /** | 121 /** |
123 * Enumeration of invitation field names. | 122 * Enumeration of invitation field names. |
124 * @enum {string} | 123 * @enum {string} |
125 * @private | 124 * @private |
126 */ | 125 */ |
127 InvitationParser.Field_ = { | 126 InvitationParser |
128 PRINTER: 'printer', | 127 .Field_ = {PRINTER: 'printer', RECEIVER: 'receiver', SENDER: 'sender'}; |
129 RECEIVER: 'receiver', | |
130 SENDER: 'sender' | |
131 }; | |
132 | 128 |
133 /** | 129 /** |
134 * Enumeration of cloud destination types that are supported by print preview. | 130 * Enumeration of cloud destination types that are supported by print preview. |
135 * @enum {string} | 131 * @enum {string} |
136 * @private | 132 * @private |
137 */ | 133 */ |
138 InvitationParser.AclType_ = { | 134 InvitationParser.AclType_ = |
139 DOMAIN: 'DOMAIN', | 135 {DOMAIN: 'DOMAIN', GROUP: 'GROUP', PUBLIC: 'PUBLIC', USER: 'USER'}; |
140 GROUP: 'GROUP', | |
141 PUBLIC: 'PUBLIC', | |
142 USER: 'USER' | |
143 }; | |
144 | 136 |
145 /** | 137 /** |
146 * Parses printer sharing invitation from JSON from GCP invite API response. | 138 * Parses printer sharing invitation from JSON from GCP invite API response. |
147 * @param {!Object} json Object that represents a invitation search response. | 139 * @param {!Object} json Object that represents a invitation search response. |
148 * @param {string} account The account this invitation is sent for. | 140 * @param {string} account The account this invitation is sent for. |
149 * @return {!print_preview.Invitation} Parsed invitation. | 141 * @return {!print_preview.Invitation} Parsed invitation. |
150 */ | 142 */ |
151 InvitationParser.parse = function(json, account) { | 143 InvitationParser.parse = function(json, account) { |
152 if (!json.hasOwnProperty(InvitationParser.Field_.SENDER) || | 144 if (!json.hasOwnProperty(InvitationParser.Field_.SENDER) || |
153 !json.hasOwnProperty(InvitationParser.Field_.RECEIVER) || | 145 !json.hasOwnProperty(InvitationParser.Field_.RECEIVER) || |
154 !json.hasOwnProperty(InvitationParser.Field_.PRINTER)) { | 146 !json.hasOwnProperty(InvitationParser.Field_.PRINTER)) { |
155 throw Error('Invitation does not have necessary info.'); | 147 throw Error('Invitation does not have necessary info.'); |
156 } | 148 } |
157 | 149 |
158 var nameFormatter = function(name, scope) { | 150 var nameFormatter = function(name, scope) { |
159 return name && scope ? (name + ' (' + scope + ')') : (name || scope); | 151 return name && scope ? (name + ' (' + scope + ')') : (name || scope); |
160 }; | 152 }; |
161 | 153 |
162 var sender = json[InvitationParser.Field_.SENDER]; | 154 var sender = json[InvitationParser.Field_.SENDER]; |
163 var senderName = nameFormatter(sender['name'], sender['email']); | 155 var senderName = nameFormatter(sender['name'], sender['email']); |
164 | 156 |
165 var receiver = json[InvitationParser.Field_.RECEIVER]; | 157 var receiver = json[InvitationParser.Field_.RECEIVER]; |
166 var receiverName = ''; | 158 var receiverName = ''; |
167 var receiverType = receiver['type']; | 159 var receiverType = receiver['type']; |
168 if (receiverType == InvitationParser.AclType_.USER) { | 160 if (receiverType == InvitationParser.AclType_.USER) { |
169 // It's a personal invitation, empty name indicates just that. | 161 // It's a personal invitation, empty name indicates just that. |
170 } else if (receiverType == InvitationParser.AclType_.GROUP || | 162 } else if ( |
171 receiverType == InvitationParser.AclType_.DOMAIN) { | 163 receiverType == InvitationParser.AclType_.GROUP || |
| 164 receiverType == InvitationParser.AclType_.DOMAIN) { |
172 receiverName = nameFormatter(receiver['name'], receiver['scope']); | 165 receiverName = nameFormatter(receiver['name'], receiver['scope']); |
173 } else { | 166 } else { |
174 throw Error('Invitation of unsupported receiver type'); | 167 throw Error('Invitation of unsupported receiver type'); |
175 } | 168 } |
176 | 169 |
177 var destination = cloudprint.CloudDestinationParser.parse( | 170 var destination = cloudprint.CloudDestinationParser.parse( |
178 json[InvitationParser.Field_.PRINTER], | 171 json[InvitationParser.Field_.PRINTER], |
179 print_preview.DestinationOrigin.COOKIES, | 172 print_preview.DestinationOrigin.COOKIES, account); |
180 account); | |
181 | 173 |
182 return new print_preview.Invitation( | 174 return new print_preview.Invitation( |
183 senderName, receiverName, destination, receiver, account); | 175 senderName, receiverName, destination, receiver, account); |
184 }; | 176 }; |
185 | 177 |
186 // Export | 178 // Export |
187 return { | 179 return { |
188 CloudDestinationParser: CloudDestinationParser, | 180 CloudDestinationParser: CloudDestinationParser, |
189 InvitationParser: InvitationParser | 181 InvitationParser: InvitationParser |
190 }; | 182 }; |
191 }); | 183 }); |
OLD | NEW |