- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / print_preview / data / cloud_parsers.js
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.
4
5 cr.define('cloudprint', function() {
6   'use strict';
7
8   /** Namespace which contains a method to parse cloud destinations directly. */
9   function CloudDestinationParser() {};
10
11   /**
12    * Enumeration of cloud destination field names.
13    * @enum {string}
14    * @private
15    */
16   CloudDestinationParser.Field_ = {
17     LAST_ACCESS: 'accessTime',
18     CAPABILITIES: 'capabilities',
19     CONNECTION_STATUS: 'connectionStatus',
20     DISPLAY_NAME: 'displayName',
21     ID: 'id',
22     IS_TOS_ACCEPTED: 'isTosAccepted',
23     TAGS: 'tags',
24     TYPE: 'type'
25   };
26
27   /**
28    * Special tag that denotes whether the destination has been recently used.
29    * @type {string}
30    * @const
31    * @private
32    */
33   CloudDestinationParser.RECENT_TAG_ = '^recent';
34
35   /**
36    * Special tag that denotes whether the destination is owned by the user.
37    * @type {string}
38    * @const
39    * @private
40    */
41   CloudDestinationParser.OWNED_TAG_ = '^own';
42
43   /**
44    * Enumeration of cloud destination types that are supported by print preview.
45    * @enum {string}
46    * @private
47    */
48   CloudDestinationParser.CloudType_ = {
49     ANDROID: 'ANDROID_CHROME_SNAPSHOT',
50     DOCS: 'DOCS',
51     IOS: 'IOS_CHROME_SNAPSHOT'
52   };
53
54   /**
55    * Parses a destination from JSON from a Google Cloud Print search or printer
56    * response.
57    * @param {!Object} json Object that represents a Google Cloud Print search or
58    *     printer response.
59    * @param {!print_preview.Destination.Origin} origin The origin of the
60    *     response.
61    * @return {!print_preview.Destination} Parsed destination.
62    */
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');
68     }
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 = {
75       tags: tags,
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
81     };
82     var cloudDest = new print_preview.Destination(
83         id,
84         CloudDestinationParser.parseType_(
85             json[CloudDestinationParser.Field_.TYPE]),
86         origin,
87         json[CloudDestinationParser.Field_.DISPLAY_NAME],
88         arrayContains(tags, CloudDestinationParser.RECENT_TAG_) /*isRecent*/,
89         connectionStatus,
90         optionalParams);
91     if (json.hasOwnProperty(CloudDestinationParser.Field_.CAPABILITIES)) {
92       cloudDest.capabilities = /*@type {!print_preview.Cdd}*/ (
93           json[CloudDestinationParser.Field_.CAPABILITIES]);
94     }
95     return cloudDest;
96   };
97
98   /**
99    * Parses the destination type.
100    * @param {string} typeStr Destination type given by the Google Cloud Print
101    *     server.
102    * @return {!print_preview.Destination.Type} Destination type.
103    * @private
104    */
105   CloudDestinationParser.parseType_ = function(typeStr) {
106     if (typeStr == CloudDestinationParser.CloudType_.ANDROID ||
107         typeStr == CloudDestinationParser.CloudType_.IOS) {
108       return print_preview.Destination.Type.MOBILE;
109     } else if (typeStr == CloudDestinationParser.CloudType_.DOCS) {
110       return print_preview.Destination.Type.GOOGLE_PROMOTED;
111     } else {
112       return print_preview.Destination.Type.GOOGLE;
113     }
114   };
115
116   // Export
117   return {
118     CloudDestinationParser: CloudDestinationParser
119   };
120 });