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

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: Log errors if JSON parsing fails. 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
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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 cached = /** @type {string} */
53 (window.localStorage.getItem(remoting.HostList.HOSTS_KEY)); 53 (window.localStorage.getItem(remoting.HostList.HOSTS_KEY));
54 if (cached) { 54 if (cached) {
55 try { 55 var cached_parsed = jsonParseSafe(cached);
Wez 2012/04/28 00:00:42 nit: cached -> cachedStr, cached_parsed -> cached?
Jamie 2012/04/28 00:15:41 Done.
56 this.hosts_ = /** @type {Array} */ JSON.parse(cached); 56 if (cached_parsed) {
57 } catch (err) { 57 this.hosts_ = /** @type {Array} */ cached_parsed;
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 parsed_response =
Wez 2012/04/28 00:00:42 nit: parsed_response -> parsedResponse, or just re
Jamie 2012/04/28 00:15:41 Done.
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 (parsed_response &&
128 parsed_response.data &&
129 parsed_response.data.items) {
127 this.hosts_ = parsed_response.data.items; 130 this.hosts_ = parsed_response.data.items;
128 /** 131 /**
129 * @param {remoting.Host} a 132 * @param {remoting.Host} a
130 * @param {remoting.Host} b 133 * @param {remoting.Host} b
131 */ 134 */
132 var cmp = function(a, b) { 135 var cmp = function(a, b) {
133 if (a.status < b.status) { 136 if (a.status < b.status) {
134 return 1; 137 return 1;
135 } else if (b.status < a.status) { 138 } else if (b.status < a.status) {
136 return -1; 139 return -1;
137 } 140 }
138 return 0; 141 return 0;
139 }; 142 };
140 this.hosts_ = /** @type {Array} */ this.hosts_.sort(cmp); 143 this.hosts_ = /** @type {Array} */ this.hosts_.sort(cmp);
144 } else {
145 console.error('Invalid "hosts" response from server.');
Wez 2012/04/28 00:00:42 nit: Suggest "Invalid host list from server."
Jamie 2012/04/28 00:15:41 I'd prefer to keep the API mentioned explicitly in
141 } 146 }
142 } else { 147 } else {
143 // Some other error. 148 // Some other error.
144 console.error('Bad status on host list query: ', xhr); 149 console.error('Bad status on host list query: ', xhr);
145 if (xhr.status == 403) { 150 if (xhr.status == 403) {
146 // The user's account is not enabled for Me2Me, so fail silently. 151 // The user's account is not enabled for Me2Me, so fail silently.
147 } else if (xhr.status >= 400 && xhr.status < 500) { 152 } else if (xhr.status >= 400 && xhr.status < 500) {
148 // For other errors, tell the user to re-authorize us. 153 // For other errors, tell the user to re-authorize us.
149 this.lastError_ = remoting.Error.GENERIC; 154 this.lastError_ = remoting.Error.GENERIC;
150 } else if (xhr.status == 503) { 155 } else if (xhr.status == 503) {
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 JSON.stringify(this.hosts_)); 304 JSON.stringify(this.hosts_));
300 } 305 }
301 306
302 /** 307 /**
303 * Key name under which Me2Me hosts are cached. 308 * Key name under which Me2Me hosts are cached.
304 */ 309 */
305 remoting.HostList.HOSTS_KEY = 'me2me-cached-hosts'; 310 remoting.HostList.HOSTS_KEY = 'me2me-cached-hosts';
306 311
307 /** @type {remoting.HostList} */ 312 /** @type {remoting.HostList} */
308 remoting.hostList = null; 313 remoting.hostList = null;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698