Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(234)

Side by Side Diff: remoting/webapp/host_list.js

Issue 10221021: Protect all uses of JSON.parse against exceptions. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Reviewer comments. Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « remoting/webapp/host_controller.js ('k') | remoting/webapp/oauth2.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 /** 5 /**
6 * @fileoverview 6 * @fileoverview
7 * Class representing the host-list portion of the home screen UI. 7 * Class representing the host-list portion of the home screen UI.
8 */ 8 */
9 9
10 'use strict'; 10 'use strict';
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 * @private 42 * @private
43 */ 43 */
44 this.hosts_ = []; 44 this.hosts_ = [];
45 /** 45 /**
46 * @type {string} 46 * @type {string}
47 * @private 47 * @private
48 */ 48 */
49 this.lastError_ = ''; 49 this.lastError_ = '';
50 50
51 // Load the cache of the last host-list, if present. 51 // Load the cache of the last host-list, if present.
52 var cached = /** @type {string} */ 52 var cachedStr = /** @type {string} */
53 (window.localStorage.getItem(remoting.HostList.HOSTS_KEY)); 53 (window.localStorage.getItem(remoting.HostList.HOSTS_KEY));
54 if (cached) { 54 if (cachedStr) {
55 try { 55 var cached = jsonParseSafe(cachedStr);
56 this.hosts_ = /** @type {Array} */ JSON.parse(cached); 56 if (cached) {
57 } catch (err) { 57 this.hosts_ = /** @type {Array} */ cached;
58 console.error('Invalid host list cache:', /** @type {*} */(err)); 58 } else {
59 console.error('Invalid value for ' + remoting.HostList.HOSTS_KEY);
59 } 60 }
60 } 61 }
61 }; 62 };
62 63
63 /** 64 /**
64 * Search the host list for a host with the specified id. 65 * Search the host list for a host with the specified id.
65 * 66 *
66 * @param {string} hostId The unique id of the host. 67 * @param {string} hostId The unique id of the host.
67 * @return {remoting.Host?} The host, if any. 68 * @return {remoting.Host?} The host, if any.
68 */ 69 */
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 * @param {XMLHttpRequest} xhr The XHR object for the host list request. 115 * @param {XMLHttpRequest} xhr The XHR object for the host list request.
115 * @param {function(boolean):void} onDone The callback passed to |refresh|. 116 * @param {function(boolean):void} onDone The callback passed to |refresh|.
116 * @return {void} Nothing. 117 * @return {void} Nothing.
117 * @private 118 * @private
118 */ 119 */
119 remoting.HostList.prototype.parseHostListResponse_ = function(xhr, onDone) { 120 remoting.HostList.prototype.parseHostListResponse_ = function(xhr, onDone) {
120 this.hosts_ = []; 121 this.hosts_ = [];
121 this.lastError_ = ''; 122 this.lastError_ = '';
122 try { 123 try {
123 if (xhr.status == 200) { 124 if (xhr.status == 200) {
124 var parsed_response = 125 var response =
125 /** @type {{data: {items: Array}}} */ JSON.parse(xhr.responseText); 126 /** @type {{data: {items: Array}}} */ jsonParseSafe(xhr.responseText);
126 if (parsed_response.data && parsed_response.data.items) { 127 if (response && response.data && response.data.items) {
127 this.hosts_ = parsed_response.data.items; 128 this.hosts_ = response.data.items;
128 /** 129 /**
129 * @param {remoting.Host} a 130 * @param {remoting.Host} a
130 * @param {remoting.Host} b 131 * @param {remoting.Host} b
131 */ 132 */
132 var cmp = function(a, b) { 133 var cmp = function(a, b) {
133 if (a.status < b.status) { 134 if (a.status < b.status) {
134 return 1; 135 return 1;
135 } else if (b.status < a.status) { 136 } else if (b.status < a.status) {
136 return -1; 137 return -1;
137 } 138 }
138 return 0; 139 return 0;
139 }; 140 };
140 this.hosts_ = /** @type {Array} */ this.hosts_.sort(cmp); 141 this.hosts_ = /** @type {Array} */ this.hosts_.sort(cmp);
142 } else {
143 console.error('Invalid "hosts" response from server.');
141 } 144 }
142 } else { 145 } else {
143 // Some other error. 146 // Some other error.
144 console.error('Bad status on host list query: ', xhr); 147 console.error('Bad status on host list query: ', xhr);
145 if (xhr.status == 403) { 148 if (xhr.status == 403) {
146 // The user's account is not enabled for Me2Me, so fail silently. 149 // The user's account is not enabled for Me2Me, so fail silently.
147 } else if (xhr.status >= 400 && xhr.status < 500) { 150 } else if (xhr.status >= 400 && xhr.status < 500) {
148 // For other errors, tell the user to re-authorize us. 151 // For other errors, tell the user to re-authorize us.
149 this.lastError_ = remoting.Error.GENERIC; 152 this.lastError_ = remoting.Error.GENERIC;
150 } else if (xhr.status == 503) { 153 } else if (xhr.status == 503) {
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 JSON.stringify(this.hosts_)); 302 JSON.stringify(this.hosts_));
300 } 303 }
301 304
302 /** 305 /**
303 * Key name under which Me2Me hosts are cached. 306 * Key name under which Me2Me hosts are cached.
304 */ 307 */
305 remoting.HostList.HOSTS_KEY = 'me2me-cached-hosts'; 308 remoting.HostList.HOSTS_KEY = 'me2me-cached-hosts';
306 309
307 /** @type {remoting.HostList} */ 310 /** @type {remoting.HostList} */
308 remoting.hostList = null; 311 remoting.hostList = null;
OLDNEW
« no previous file with comments | « remoting/webapp/host_controller.js ('k') | remoting/webapp/oauth2.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698