Chromium Code Reviews| 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 |
| 11 /** | 11 /** |
| 12 * Enumeration of cloud destination field names. | 12 * Enumeration of cloud destination field names. |
| 13 * @enum {string} | 13 * @enum {string} |
| 14 * @private | 14 * @private |
| 15 */ | 15 */ |
| 16 CloudDestinationParser.Field_ = { | 16 CloudDestinationParser.Field_ = { |
| 17 CAPABILITIES: 'capabilities', | 17 CAPABILITIES: 'capabilities', |
| 18 DISPLAY_NAME: 'displayName', | 18 DISPLAY_NAME: 'displayName', |
| 19 FORMAT: 'capsFormat', | 19 FORMAT: 'capsFormat', |
| 20 ID: 'id', | 20 ID: 'id', |
| 21 TAGS: 'tags' | 21 TAGS: 'tags', |
| 22 TYPE: 'type' | |
| 22 }; | 23 }; |
| 23 | 24 |
| 24 /** | 25 /** |
| 25 * Special tag that denotes whether the destination has been recently used. | 26 * Special tag that denotes whether the destination has been recently used. |
| 26 * @type {string} | 27 * @type {string} |
| 28 * @const | |
| 27 * @private | 29 * @private |
| 28 */ | 30 */ |
| 29 CloudDestinationParser.RECENT_TAG_ = '^recent'; | 31 CloudDestinationParser.RECENT_TAG_ = '^recent'; |
| 30 | 32 |
| 31 /** | 33 /** |
| 34 * Special tag that denotes whether the destination is owned by the user. | |
| 35 * @type {string} | |
| 36 * @const | |
| 37 * @private | |
| 38 */ | |
| 39 CloudDestinationParser.OWNED_TAG_ = '^own'; | |
| 40 | |
| 41 /** | |
| 42 * Enumeration of cloud destination types that are supported by print preview. | |
| 43 * @enum {string} | |
| 44 * @private | |
| 45 */ | |
| 46 CloudDestinationParser.CloudType_ = { | |
| 47 ANDROID: 'ANDROID_CHROME_SNAPSHOT', | |
| 48 DOCS: 'DOCS', | |
| 49 IOS: 'IOS_CHROME_SNAPSHOT' | |
| 50 }; | |
| 51 | |
| 52 /** | |
| 32 * Parses a destination from JSON from a Google Cloud Print search or printer | 53 * Parses a destination from JSON from a Google Cloud Print search or printer |
| 33 * response. | 54 * response. |
| 34 * @param {object} json Object that represents a Google Cloud Print search or | 55 * @param {!Object} json Object that represents a Google Cloud Print search or |
| 35 * printer response. | 56 * printer response. |
| 36 * @return {!print_preview.Destination} Parsed destination. | 57 * @return {!print_preview.Destination} Parsed destination. |
| 37 */ | 58 */ |
| 38 CloudDestinationParser.parse = function(json) { | 59 CloudDestinationParser.parse = function(json) { |
| 39 if (!json.hasOwnProperty(CloudDestinationParser.Field_.ID) || | 60 if (!json.hasOwnProperty(CloudDestinationParser.Field_.ID) || |
| 61 !json.hasOwnProperty(CloudDestinationParser.Field_.TYPE) || | |
| 40 !json.hasOwnProperty(CloudDestinationParser.Field_.DISPLAY_NAME)) { | 62 !json.hasOwnProperty(CloudDestinationParser.Field_.DISPLAY_NAME)) { |
| 41 throw Error('Cloud destination does not have an ID or a display name'); | 63 throw Error('Cloud destination does not have an ID or a display name'); |
| 42 } | 64 } |
| 43 var isRecent = arrayContains( | 65 var tags = json[CloudDestinationParser.Field_.TAGS] || []; |
| 44 json[CloudDestinationParser.Field_.TAGS] || [], | 66 var isRecent = arrayContains(tags, CloudDestinationParser.RECENT_TAG_); |
| 45 CloudDestinationParser.RECENT_TAG_); | 67 var isOwned = arrayContains(tags, CloudDestinationParser.OWNED_TAG_); |
| 46 var cloudDest = new print_preview.Destination( | 68 var cloudDest = new print_preview.Destination( |
| 47 json[CloudDestinationParser.Field_.ID], | 69 json[CloudDestinationParser.Field_.ID], |
| 70 CloudDestinationParser.parseType_( | |
| 71 json[CloudDestinationParser.Field_.TYPE]), | |
| 48 json[CloudDestinationParser.Field_.DISPLAY_NAME], | 72 json[CloudDestinationParser.Field_.DISPLAY_NAME], |
| 49 isRecent, | 73 isRecent, |
| 50 false /*isLocal*/, | 74 tags, |
| 51 json[CloudDestinationParser.Field_.TAGS] || []); | 75 isOwned); |
| 52 if (json.hasOwnProperty(CloudDestinationParser.Field_.CAPABILITIES) && | 76 if (json.hasOwnProperty(CloudDestinationParser.Field_.CAPABILITIES) && |
| 53 json.hasOwnProperty(CloudDestinationParser.Field_.FORMAT)) { | 77 json.hasOwnProperty(CloudDestinationParser.Field_.FORMAT)) { |
| 54 cloudDest.capabilities = CloudCapabilitiesParser.parse( | 78 cloudDest.capabilities = CloudCapabilitiesParser.parse( |
| 55 json[CloudDestinationParser.Field_.FORMAT], | 79 json[CloudDestinationParser.Field_.FORMAT], |
| 56 json[CloudDestinationParser.Field_.CAPABILITIES]); | 80 json[CloudDestinationParser.Field_.CAPABILITIES]); |
| 57 } | 81 } |
| 58 return cloudDest; | 82 return cloudDest; |
| 59 }; | 83 }; |
| 60 | 84 |
| 61 /** | 85 /** |
| 86 * Parses the destination type. | |
| 87 * @param {string} typeStr Destination type given by the Google Cloud Print | |
| 88 * server. | |
| 89 * @return {!print_preview.Destination.Type} Destination type. | |
| 90 * @private | |
| 91 */ | |
| 92 CloudDestinationParser.parseType_ = function(typeStr) { | |
| 93 if (typeStr == CloudDestinationParser.CloudType_.ANDROID || | |
| 94 typeStr == CloudDestinationParser.CloudType_.IOS) { | |
| 95 return print_preview.Destination.Type.MOBILE; | |
| 96 } else if (typeStr == CloudDestinationParser.CloudType_.DOCS) { | |
| 97 return print_preview.Destination.Type.GOOGLE_PROMOTED; | |
|
Vitaly Pavlenko
2014/09/18 02:39:48
There's no such thing like
print_preview.Destina
| |
| 98 } else { | |
| 99 return print_preview.Destination.Type.GOOGLE; | |
| 100 } | |
| 101 }; | |
| 102 | |
| 103 /** | |
| 62 * Namespace which contains a method to parse a cloud destination's print | 104 * Namespace which contains a method to parse a cloud destination's print |
| 63 * capabilities. | 105 * capabilities. |
| 64 */ | 106 */ |
| 65 function CloudCapabilitiesParser() {}; | 107 function CloudCapabilitiesParser() {}; |
| 66 | 108 |
| 67 /** | 109 /** |
| 68 * Enumeration of cloud destination print capabilities field names. | 110 * Enumeration of cloud destination print capabilities field names. |
| 69 * @enum {string} | 111 * @enum {string} |
| 70 * @private | 112 * @private |
| 71 */ | 113 */ |
| 72 CloudCapabilitiesParser.Field_ = { | 114 CloudCapabilitiesParser.Field_ = { |
| 73 CAP_ID: 'name', | 115 CAP_ID: 'name', |
| 74 DEFAULT: 'default', | 116 DEFAULT: 'default', |
| 75 IS_DEFAULT: 'default', | 117 IS_DEFAULT: 'default', |
| 76 OPTIONS: 'options', | 118 OPTIONS: 'options', |
| 77 OPTION_ID: 'name' | 119 OPTION_ID: 'name' |
| 78 }; | 120 }; |
| 79 | 121 |
| 80 /** | 122 /** |
| 81 * Parses print capabilities from an object in a given capabilities format. | 123 * Parses print capabilities from an object in a given capabilities format. |
| 82 * @param {print_preview.CloudCapabilities.Format} capsFormat Format of the | 124 * @param {print_preview.CloudCapabilities.Format} capsFormat Format of the |
| 83 * printer capabilities. | 125 * printer capabilities. |
| 84 * @param {object} json Object representing the cloud capabilities. | 126 * @param {!Array.<!Object>} json Object representing the cloud capabilities. |
| 85 * @return {!print_preview.CloudCapabilities} Parsed print capabilities. | 127 * @return {!print_preview.CloudCapabilities} Parsed print capabilities. |
| 86 */ | 128 */ |
| 87 CloudCapabilitiesParser.parse = function(capsFormat, json) { | 129 CloudCapabilitiesParser.parse = function(capsFormat, json) { |
| 88 var colorCapability = null; | 130 var colorCapability = null; |
| 89 var duplexCapability = null; | 131 var duplexCapability = null; |
| 90 var copiesCapability = null; | 132 var copiesCapability = null; |
| 91 var collateCapability = null; | 133 var collateCapability = null; |
| 92 for (var cap, i = 0; cap = json[i]; i++) { | 134 json.forEach(function(cap) { |
| 93 var capId = cap[CloudCapabilitiesParser.Field_.CAP_ID]; | 135 var capId = cap[CloudCapabilitiesParser.Field_.CAP_ID]; |
| 94 if (capId == print_preview.CollateCapability.Id[capsFormat]) { | 136 if (capId == print_preview.CollateCapability.Id[capsFormat]) { |
| 95 collateCapability = CloudCapabilitiesParser.parseCollate(capId, cap); | 137 collateCapability = CloudCapabilitiesParser.parseCollate(capId, cap); |
| 96 } else if (capId == print_preview.ColorCapability.Id[capsFormat]) { | 138 } else if (capId == print_preview.ColorCapability.Id[capsFormat]) { |
| 97 colorCapability = CloudCapabilitiesParser.parseColor(capId, cap); | 139 colorCapability = CloudCapabilitiesParser.parseColor(capId, cap); |
| 98 } else if (capId == print_preview.CopiesCapability.Id[capsFormat]) { | 140 } else if (capId == print_preview.CopiesCapability.Id[capsFormat]) { |
| 99 copiesCapability = new print_preview.CopiesCapability(capId); | 141 copiesCapability = new print_preview.CopiesCapability(capId); |
| 100 } else if (capId == print_preview.DuplexCapability.Id[capsFormat]) { | 142 } else if (capId == print_preview.DuplexCapability.Id[capsFormat]) { |
| 101 duplexCapability = CloudCapabilitiesParser.parseDuplex(capId, cap); | 143 duplexCapability = CloudCapabilitiesParser.parseDuplex(capId, cap); |
| 102 } | 144 } |
| 103 } | 145 }); |
| 104 return new print_preview.CloudCapabilities( | 146 return new print_preview.CloudCapabilities( |
| 105 collateCapability, colorCapability, copiesCapability, duplexCapability); | 147 collateCapability, colorCapability, copiesCapability, duplexCapability); |
| 106 }; | 148 }; |
| 107 | 149 |
| 108 /** | 150 /** |
| 109 * Parses a collate capability from the given object. | 151 * Parses a collate capability from the given object. |
| 110 * @param {string} capId Native ID of the given capability object. | 152 * @param {string} capId Native ID of the given capability object. |
| 111 * @param {object} Object that represents the collate capability. | 153 * @param {!Object} Object that represents the collate capability. |
| 112 * @return {print_preview.CollateCapability} Parsed collate capability or | 154 * @return {print_preview.CollateCapability} Parsed collate capability or |
| 113 * {@code null} if the given capability object was not a valid collate | 155 * {@code null} if the given capability object was not a valid collate |
| 114 * capability. | 156 * capability. |
| 115 */ | 157 */ |
| 116 CloudCapabilitiesParser.parseCollate = function(capId, cap) { | 158 CloudCapabilitiesParser.parseCollate = function(capId, cap) { |
| 117 var options = cap[CloudCapabilitiesParser.Field_.OPTIONS]; | 159 var options = cap[CloudCapabilitiesParser.Field_.OPTIONS]; |
| 118 var collateOption = null; | 160 var collateOption = null; |
| 119 var noCollateOption = null; | 161 var noCollateOption = null; |
| 120 var isCollateDefault = false; | 162 var isCollateDefault = false; |
| 121 for (var option, i = 0; option = options[i]; i++) { | 163 options.forEach(function(option) { |
| 122 var optionId = option[CloudCapabilitiesParser.Field_.OPTION_ID]; | 164 var optionId = option[CloudCapabilitiesParser.Field_.OPTION_ID]; |
| 123 if (!collateOption && | 165 if (!collateOption && |
| 124 print_preview.CollateCapability.COLLATE_REGEX.test(optionId)) { | 166 print_preview.CollateCapability.COLLATE_REGEX.test(optionId)) { |
| 125 collateOption = optionId; | 167 collateOption = optionId; |
| 126 isCollateDefault = !!option[CloudCapabilitiesParser.Field_.DEFAULT]; | 168 isCollateDefault = !!option[CloudCapabilitiesParser.Field_.DEFAULT]; |
| 127 } else if (!noCollateOption && | 169 } else if (!noCollateOption && |
| 128 print_preview.CollateCapability.NO_COLLATE_REGEX.test(optionId)) { | 170 print_preview.CollateCapability.NO_COLLATE_REGEX.test(optionId)) { |
| 129 noCollateOption = optionId; | 171 noCollateOption = optionId; |
| 130 } | 172 } |
| 131 } | 173 }); |
| 132 if (!collateOption || !noCollateOption) { | 174 if (!collateOption || !noCollateOption) { |
| 133 return null; | 175 return null; |
| 134 } | 176 } |
| 135 return new print_preview.CollateCapability( | 177 return new print_preview.CollateCapability( |
| 136 capId, collateOption, noCollateOption, isCollateDefault); | 178 capId, collateOption, noCollateOption, isCollateDefault); |
| 137 }; | 179 }; |
| 138 | 180 |
| 139 /** | 181 /** |
| 140 * Parses a color capability from the given object. | 182 * Parses a color capability from the given object. |
| 141 * @param {string} capId Native ID of the given capability object. | 183 * @param {string} capId Native ID of the given capability object. |
| 142 * @param {object} Object that represents the color capability. | 184 * @param {!Object} Object that represents the color capability. |
| 143 * @return {print_preview.ColorCapability} Parsed color capability or | 185 * @return {print_preview.ColorCapability} Parsed color capability or |
| 144 * {@code null} if the given capability object was not a valid color | 186 * {@code null} if the given capability object was not a valid color |
| 145 * capability. | 187 * capability. |
| 146 */ | 188 */ |
| 147 CloudCapabilitiesParser.parseColor = function(capId, cap) { | 189 CloudCapabilitiesParser.parseColor = function(capId, cap) { |
| 148 var options = cap[CloudCapabilitiesParser.Field_.OPTIONS]; | 190 var options = cap[CloudCapabilitiesParser.Field_.OPTIONS]; |
| 149 var colorOption = null; | 191 var colorOption = null; |
| 150 var bwOption = null; | 192 var bwOption = null; |
| 151 var isColorDefault = false; | 193 var isColorDefault = false; |
| 152 for (var option, i = 0; option = options[i]; i++) { | 194 options.forEach(function(option) { |
| 153 var optionId = option[CloudCapabilitiesParser.Field_.OPTION_ID]; | 195 var optionId = option[CloudCapabilitiesParser.Field_.OPTION_ID]; |
| 154 if (!colorOption && | 196 if (!colorOption && |
| 155 print_preview.ColorCapability.COLOR_REGEX.test(optionId)) { | 197 print_preview.ColorCapability.COLOR_REGEX.test(optionId)) { |
| 156 colorOption = optionId; | 198 colorOption = optionId; |
| 157 isColorDefault = !!option[CloudCapabilitiesParser.Field_.DEFAULT]; | 199 isColorDefault = !!option[CloudCapabilitiesParser.Field_.DEFAULT]; |
| 158 } else if (!bwOption && | 200 } else if (!bwOption && |
| 159 print_preview.ColorCapability.BW_REGEX.test(optionId)) { | 201 print_preview.ColorCapability.BW_REGEX.test(optionId)) { |
| 160 bwOption = optionId; | 202 bwOption = optionId; |
| 161 } | 203 } |
| 162 } | 204 }); |
| 163 if (!colorOption || !bwOption) { | 205 if (!colorOption || !bwOption) { |
| 164 return null; | 206 return null; |
| 165 } | 207 } |
| 166 return new print_preview.ColorCapability( | 208 return new print_preview.ColorCapability( |
| 167 capId, colorOption, bwOption, isColorDefault); | 209 capId, colorOption, bwOption, isColorDefault); |
| 168 }; | 210 }; |
| 169 | 211 |
| 170 /** | 212 /** |
| 171 * Parses a duplex capability from the given object. | 213 * Parses a duplex capability from the given object. |
| 172 * @param {string} capId Native ID of the given capability object. | 214 * @param {string} capId Native ID of the given capability object. |
| 173 * @param {object} Object that represents the duplex capability. | 215 * @param {!Object} Object that represents the duplex capability. |
| 174 * @return {print_preview.DuplexCapability} Parsed duplex capability or | 216 * @return {print_preview.DuplexCapability} Parsed duplex capability or |
| 175 * {@code null} if the given capability object was not a valid duplex | 217 * {@code null} if the given capability object was not a valid duplex |
| 176 * capability. | 218 * capability. |
| 177 */ | 219 */ |
| 178 CloudCapabilitiesParser.parseDuplex = function(capId, cap) { | 220 CloudCapabilitiesParser.parseDuplex = function(capId, cap) { |
| 179 var options = cap[CloudCapabilitiesParser.Field_.OPTIONS]; | 221 var options = cap[CloudCapabilitiesParser.Field_.OPTIONS]; |
| 180 var simplexOption = null; | 222 var simplexOption = null; |
| 181 var longEdgeOption = null; | 223 var longEdgeOption = null; |
| 182 var isDuplexDefault = false; | 224 var isDuplexDefault = false; |
| 183 for (var option, i = 0; option = options[i]; i++) { | 225 options.forEach(function(option) { |
| 184 var optionId = option[CloudCapabilitiesParser.Field_.OPTION_ID]; | 226 var optionId = option[CloudCapabilitiesParser.Field_.OPTION_ID]; |
| 185 if (!simplexOption && | 227 if (!simplexOption && |
| 186 print_preview.DuplexCapability.SIMPLEX_REGEX.test(optionId)) { | 228 print_preview.DuplexCapability.SIMPLEX_REGEX.test(optionId)) { |
| 187 simplexOption = optionId; | 229 simplexOption = optionId; |
| 188 } else if (!longEdgeOption && | 230 } else if (!longEdgeOption && |
| 189 print_preview.DuplexCapability.LONG_EDGE_REGEX.test(optionId)) { | 231 print_preview.DuplexCapability.LONG_EDGE_REGEX.test(optionId)) { |
| 190 longEdgeOption = optionId; | 232 longEdgeOption = optionId; |
| 191 isDuplexDefault = !!option[CloudCapabilitiesParser.Field_.DEFAULT]; | 233 isDuplexDefault = !!option[CloudCapabilitiesParser.Field_.DEFAULT]; |
| 192 } | 234 } |
| 193 } | 235 }); |
| 194 if (!simplexOption || !longEdgeOption) { | 236 if (!simplexOption || !longEdgeOption) { |
| 195 return null; | 237 return null; |
| 196 } | 238 } |
| 197 return new print_preview.DuplexCapability( | 239 return new print_preview.DuplexCapability( |
| 198 capId, simplexOption, longEdgeOption, isDuplexDefault); | 240 capId, simplexOption, longEdgeOption, isDuplexDefault); |
| 199 }; | 241 }; |
| 200 | 242 |
| 201 // Export | 243 // Export |
| 202 return { | 244 return { |
| 203 CloudCapabilitiesParser: CloudCapabilitiesParser, | 245 CloudCapabilitiesParser: CloudCapabilitiesParser, |
| 204 CloudDestinationParser: CloudDestinationParser | 246 CloudDestinationParser: CloudDestinationParser |
| 205 }; | 247 }; |
| 206 }); | 248 }); |
| 207 | 249 |
| OLD | NEW |