Fix infinite recursion on hiding panel when created during fullscreen mode.
[chromium-blink-merge.git] / chrome / browser / resources / print_preview / data / cloud_parsers.js
blobdee0339d77be3c0ff86a65acb02590bf8908614f
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() {
6 'use strict';
8 /** Namespace which contains a method to parse cloud destinations directly. */
9 function CloudDestinationParser() {};
11 /**
12 * Enumeration of cloud destination field names.
13 * @enum {string}
14 * @private
16 CloudDestinationParser.Field_ = {
17 CAPABILITIES: 'capabilities',
18 CONNECTION_STATUS: 'connectionStatus',
19 DESCRIPTION: 'description',
20 DISPLAY_NAME: 'displayName',
21 ID: 'id',
22 IS_TOS_ACCEPTED: 'isTosAccepted',
23 LAST_ACCESS: 'accessTime',
24 TAGS: 'tags',
25 TYPE: 'type'
28 /**
29 * Special tag that denotes whether the destination has been recently used.
30 * @type {string}
31 * @const
32 * @private
34 CloudDestinationParser.RECENT_TAG_ = '^recent';
36 /**
37 * Special tag that denotes whether the destination is owned by the user.
38 * @type {string}
39 * @const
40 * @private
42 CloudDestinationParser.OWNED_TAG_ = '^own';
44 /**
45 * Enumeration of cloud destination types that are supported by print preview.
46 * @enum {string}
47 * @private
49 CloudDestinationParser.CloudType_ = {
50 ANDROID: 'ANDROID_CHROME_SNAPSHOT',
51 DOCS: 'DOCS',
52 IOS: 'IOS_CHROME_SNAPSHOT'
55 /**
56 * Parses a destination from JSON from a Google Cloud Print search or printer
57 * response.
58 * @param {!Object} json Object that represents a Google Cloud Print search or
59 * printer response.
60 * @param {!print_preview.Destination.Origin} origin The origin of the
61 * response.
62 * @return {!print_preview.Destination} Parsed destination.
64 CloudDestinationParser.parse = function(json, origin) {
65 if (!json.hasOwnProperty(CloudDestinationParser.Field_.ID) ||
66 !json.hasOwnProperty(CloudDestinationParser.Field_.TYPE) ||
67 !json.hasOwnProperty(CloudDestinationParser.Field_.DISPLAY_NAME)) {
68 throw Error('Cloud destination does not have an ID or a display name');
70 var id = json[CloudDestinationParser.Field_.ID];
71 var tags = json[CloudDestinationParser.Field_.TAGS] || [];
72 var connectionStatus =
73 json[CloudDestinationParser.Field_.CONNECTION_STATUS] ||
74 print_preview.Destination.ConnectionStatus.UNKNOWN;
75 var optionalParams = {
76 tags: tags,
77 isOwned: arrayContains(tags, CloudDestinationParser.OWNED_TAG_),
78 lastAccessTime: parseInt(
79 json[CloudDestinationParser.Field_.LAST_ACCESS], 10) || Date.now(),
80 isTosAccepted: (id == print_preview.Destination.GooglePromotedId.FEDEX) ?
81 json[CloudDestinationParser.Field_.IS_TOS_ACCEPTED] : null,
82 cloudID: id,
83 description: json[CloudDestinationParser.Field_.DESCRIPTION]
85 var cloudDest = new print_preview.Destination(
86 id,
87 CloudDestinationParser.parseType_(
88 json[CloudDestinationParser.Field_.TYPE]),
89 origin,
90 json[CloudDestinationParser.Field_.DISPLAY_NAME],
91 arrayContains(tags, CloudDestinationParser.RECENT_TAG_) /*isRecent*/,
92 connectionStatus,
93 optionalParams);
94 if (json.hasOwnProperty(CloudDestinationParser.Field_.CAPABILITIES)) {
95 cloudDest.capabilities = /*@type {!print_preview.Cdd}*/ (
96 json[CloudDestinationParser.Field_.CAPABILITIES]);
98 return cloudDest;
102 * Parses the destination type.
103 * @param {string} typeStr Destination type given by the Google Cloud Print
104 * server.
105 * @return {!print_preview.Destination.Type} Destination type.
106 * @private
108 CloudDestinationParser.parseType_ = function(typeStr) {
109 if (typeStr == CloudDestinationParser.CloudType_.ANDROID ||
110 typeStr == CloudDestinationParser.CloudType_.IOS) {
111 return print_preview.Destination.Type.MOBILE;
112 } else if (typeStr == CloudDestinationParser.CloudType_.DOCS) {
113 return print_preview.Destination.Type.GOOGLE_PROMOTED;
114 } else {
115 return print_preview.Destination.Type.GOOGLE;
119 // Export
120 return {
121 CloudDestinationParser: CloudDestinationParser