| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 var WallpaperUtil = {}; | 5 var WallpaperUtil = {}; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Saves value to local storage that associates with key. | 8 * Saves value to local storage that associates with key. |
| 9 * @param {string} key The key that associates with value. | 9 * @param {string} key The key that associates with value. |
| 10 * @param {string} value The value to save to local storage. | 10 * @param {string} value The value to save to local storage. |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 * @param {XMLHttpRequest=} opt_xhr The XMLHttpRequest object. | 51 * @param {XMLHttpRequest=} opt_xhr The XMLHttpRequest object. |
| 52 */ | 52 */ |
| 53 WallpaperUtil.fetchURL = function(url, type, onSuccess, onFailure, opt_xhr) { | 53 WallpaperUtil.fetchURL = function(url, type, onSuccess, onFailure, opt_xhr) { |
| 54 var xhr; | 54 var xhr; |
| 55 if (opt_xhr) | 55 if (opt_xhr) |
| 56 xhr = opt_xhr; | 56 xhr = opt_xhr; |
| 57 else | 57 else |
| 58 xhr = new XMLHttpRequest(); | 58 xhr = new XMLHttpRequest(); |
| 59 | 59 |
| 60 try { | 60 try { |
| 61 xhr.addEventListener('loadend', function(e) { | 61 // Do not use loadend here to handle both success and failure case. It gets |
| 62 // complicated with abortion. Unexpected error message may show up. See |
| 63 // http://crbug.com/242581. |
| 64 xhr.addEventListener('load', function(e) { |
| 62 if (this.status == 200) { | 65 if (this.status == 200) { |
| 63 onSuccess(this); | 66 onSuccess(this); |
| 64 } else { | 67 } else { |
| 65 onFailure(); | 68 onFailure(); |
| 66 } | 69 } |
| 67 }); | 70 }); |
| 71 xhr.addEventListener('error', onFailure); |
| 68 xhr.open('GET', url, true); | 72 xhr.open('GET', url, true); |
| 69 xhr.responseType = type; | 73 xhr.responseType = type; |
| 70 xhr.send(null); | 74 xhr.send(null); |
| 71 } catch (e) { | 75 } catch (e) { |
| 72 onFailure(); | 76 onFailure(); |
| 73 } | 77 } |
| 74 }; | 78 }; |
| 75 | 79 |
| 76 /** | 80 /** |
| 77 * Sets wallpaper to online wallpaper specified by url and layout | 81 * Sets wallpaper to online wallpaper specified by url and layout |
| (...skipping 16 matching lines...) Expand all Loading... |
| 94 chrome.wallpaperPrivate.setWallpaper(xhr.response, layout, url, | 98 chrome.wallpaperPrivate.setWallpaper(xhr.response, layout, url, |
| 95 onSuccess); | 99 onSuccess); |
| 96 self.saveWallpaperInfo(url, layout, | 100 self.saveWallpaperInfo(url, layout, |
| 97 Constants.WallpaperSourceEnum.Online); | 101 Constants.WallpaperSourceEnum.Online); |
| 98 } else { | 102 } else { |
| 99 onFailure(); | 103 onFailure(); |
| 100 } | 104 } |
| 101 }, onFailure); | 105 }, onFailure); |
| 102 }); | 106 }); |
| 103 }; | 107 }; |
| OLD | NEW |