| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 /** | |
| 6 * @fileoverview | |
| 7 * Functions related to the 'home screen' for Chromoting. | |
| 8 */ | |
| 9 | |
| 10 'use strict'; | |
| 11 | |
| 12 /** @suppress {duplicate} */ | |
| 13 var remoting = remoting || {}; | |
| 14 | |
| 15 (function() { | |
| 16 | |
| 17 /** | |
| 18 * Query the Remoting Directory for the user's list of hosts. | |
| 19 * | |
| 20 * @return {void} Nothing. | |
| 21 */ | |
| 22 remoting.refreshHostList = function() { | |
| 23 // Fetch a new Access Token for the user, if necessary. | |
| 24 if (remoting.oauth2.needsNewAccessToken()) { | |
| 25 remoting.oauth2.refreshAccessToken(function(xhr) { | |
| 26 if (remoting.oauth2.needsNewAccessToken()) { | |
| 27 // Failed to get access token | |
| 28 console.error('refreshHostList: OAuth2 token fetch failed'); | |
| 29 remoting.hostList.showError(remoting.Error.AUTHENTICATION_FAILED); | |
| 30 return; | |
| 31 } | |
| 32 remoting.refreshHostList(); | |
| 33 }); | |
| 34 return; | |
| 35 } | |
| 36 | |
| 37 var headers = { | |
| 38 'Authorization': 'OAuth ' + remoting.oauth2.getAccessToken() | |
| 39 }; | |
| 40 | |
| 41 var xhr = remoting.xhr.get( | |
| 42 'https://www.googleapis.com/chromoting/v1/@me/hosts', | |
| 43 parseHostListResponse_, | |
| 44 '', | |
| 45 headers); | |
| 46 } | |
| 47 | |
| 48 /** | |
| 49 * Handle the results of the host list request. A success response will | |
| 50 * include a JSON-encoded list of host descriptions, which we display if we're | |
| 51 * able to successfully parse it. | |
| 52 * | |
| 53 * @param {XMLHttpRequest} xhr The XHR object for the host list request. | |
| 54 * @return {void} Nothing. | |
| 55 */ | |
| 56 function parseHostListResponse_(xhr) { | |
| 57 // Ignore host list responses if we're not on the Home screen. This mainly | |
| 58 // ensures that errors don't cause an unexpected mode switch. | |
| 59 if (remoting.currentMode != remoting.AppMode.HOME) { | |
| 60 return; | |
| 61 } | |
| 62 | |
| 63 if (xhr.readyState != 4) { | |
| 64 return; | |
| 65 } | |
| 66 | |
| 67 try { | |
| 68 if (xhr.status == 200) { | |
| 69 var parsed_response = | |
| 70 /** @type {{data: {items: Array}}} */ JSON.parse(xhr.responseText); | |
| 71 if (parsed_response.data && parsed_response.data.items) { | |
| 72 remoting.hostList.update(parsed_response.data.items); | |
| 73 } | |
| 74 } else { | |
| 75 // Some other error. Log for now, pretty-print in future. | |
| 76 console.error('Bad status on host list query: ', xhr); | |
| 77 var errorResponse = | |
| 78 /** @type {{error: {code: *, message: *}}} */ | |
| 79 JSON.parse(xhr.responseText); | |
| 80 if (errorResponse.error && | |
| 81 errorResponse.error.code && | |
| 82 errorResponse.error.message) { | |
| 83 remoting.debug.log('Error code ' + errorResponse.error.code); | |
| 84 remoting.debug.log('Error message ' + errorResponse.error.message); | |
| 85 } else { | |
| 86 remoting.debug.log('Error response: ' + xhr.responseText); | |
| 87 } | |
| 88 | |
| 89 // For most errors in the 4xx range, tell the user to re-authorize us. | |
| 90 if (xhr.status == 403) { | |
| 91 // The user's account is not enabled for Me2Me, so fail silently. | |
| 92 } else if (xhr.status >= 400 && xhr.status <= 499) { | |
| 93 remoting.hostList.showError(remoting.Error.GENERIC); | |
| 94 } | |
| 95 } | |
| 96 } catch (er) { | |
| 97 console.error('Error processing response: ', xhr); | |
| 98 } | |
| 99 } | |
| 100 | |
| 101 }()); | |
| OLD | NEW |