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 3// found in the LICENSE file. 4 5var WallpaperUtil = {}; 6 7/** 8 * Saves value to local storage that associates with key. 9 * @param {string} key The key that associates with value. 10 * @param {string} value The value to save to local storage. 11 * @param {boolen} sync True if the value is saved to sync storage. 12 * @param {function=} opt_callback The callback on success, or on failure. 13 */ 14WallpaperUtil.saveToStorage = function(key, value, sync, opt_callback) { 15 var items = {}; 16 items[key] = value; 17 if (sync) 18 Constants.WallpaperSyncStorage.set(items, opt_callback); 19 else 20 Constants.WallpaperLocalStorage.set(items, opt_callback); 21}; 22 23/** 24 * Saves user's wallpaper infomation to local and sync storage. Note that local 25 * value should be saved first. 26 * @param {string} url The url address of wallpaper. For custom wallpaper, it is 27 * the file name. 28 * @param {string} layout The wallpaper layout. 29 * @param {string} source The wallpaper source. 30 */ 31WallpaperUtil.saveWallpaperInfo = function(url, layout, source) { 32 var wallpaperInfo = { 33 url: url, 34 layout: layout, 35 source: source 36 }; 37 WallpaperUtil.saveToStorage(Constants.AccessLocalWallpaperInfoKey, 38 wallpaperInfo, false, function() { 39 WallpaperUtil.saveToStorage(Constants.AccessSyncWallpaperInfoKey, 40 wallpaperInfo, true); 41 }); 42}; 43 44/** 45 * Downloads resources from url. Calls onSuccess and opt_onFailure accordingly. 46 * @param {string} url The url address where we should fetch resources. 47 * @param {string} type The response type of XMLHttprequest. 48 * @param {function} onSuccess The success callback. It must be called with 49 * current XMLHttprequest object. 50 * @param {function} onFailure The failure callback. 51 * @param {XMLHttpRequest=} opt_xhr The XMLHttpRequest object. 52 */ 53WallpaperUtil.fetchURL = function(url, type, onSuccess, onFailure, opt_xhr) { 54 var xhr; 55 if (opt_xhr) 56 xhr = opt_xhr; 57 else 58 xhr = new XMLHttpRequest(); 59 60 try { 61 xhr.addEventListener('loadend', function(e) { 62 if (this.status == 200) { 63 onSuccess(this); 64 } else { 65 onFailure(); 66 } 67 }); 68 xhr.open('GET', url, true); 69 xhr.responseType = type; 70 xhr.send(null); 71 } catch (e) { 72 onFailure(); 73 } 74}; 75 76/** 77 * Sets wallpaper to online wallpaper specified by url and layout 78 * @param {string} url The url address where we should fetch resources. 79 * @param {string} layout The layout of online wallpaper. 80 * @param {function} onSuccess The success callback. 81 * @param {function} onFailure The failure callback. 82 */ 83WallpaperUtil.setOnlineWallpaper = function(url, layout, onSuccess, onFailure) { 84 var self = this; 85 chrome.wallpaperPrivate.setWallpaperIfExists(url, layout, 86 Constants.WallpaperSourceEnum.Online, function(exists) { 87 if (exists) { 88 onSuccess(); 89 return; 90 } 91 92 self.fetchURL(url, 'arraybuffer', function(xhr) { 93 if (xhr.response != null) { 94 chrome.wallpaperPrivate.setWallpaper(xhr.response, layout, url, 95 onSuccess); 96 self.saveWallpaperInfo(url, layout, 97 Constants.WallpaperSourceEnum.Online); 98 } else { 99 onFailure(); 100 } 101 }, onFailure); 102 }); 103}; 104