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.
5 var WallpaperUtil = {};
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.
14 WallpaperUtil.saveToStorage = function(key, value, sync, opt_callback) {
18 Constants.WallpaperSyncStorage.set(items, opt_callback);
20 Constants.WallpaperLocalStorage.set(items, opt_callback);
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
28 * @param {string} layout The wallpaper layout.
29 * @param {string} source The wallpaper source.
31 WallpaperUtil.saveWallpaperInfo = function(url, layout, source) {
37 WallpaperUtil.saveToStorage(Constants.AccessLocalWallpaperInfoKey,
38 wallpaperInfo, false, function() {
39 WallpaperUtil.saveToStorage(Constants.AccessSyncWallpaperInfoKey,
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.
53 WallpaperUtil.fetchURL = function(url, type, onSuccess, onFailure, opt_xhr) {
58 xhr = new XMLHttpRequest();
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) {
65 if (this.status == 200) {
71 xhr.addEventListener('error', onFailure);
72 xhr.open('GET', url, true);
73 xhr.responseType = type;
81 * Sets wallpaper to online wallpaper specified by url and layout
82 * @param {string} url The url address where we should fetch resources.
83 * @param {string} layout The layout of online wallpaper.
84 * @param {function} onSuccess The success callback.
85 * @param {function} onFailure The failure callback.
87 WallpaperUtil.setOnlineWallpaper = function(url, layout, onSuccess, onFailure) {
89 chrome.wallpaperPrivate.setWallpaperIfExists(url, layout, function(exists) {
95 self.fetchURL(url, 'arraybuffer', function(xhr) {
96 if (xhr.response != null) {
97 chrome.wallpaperPrivate.setWallpaper(xhr.response, layout, url,
99 self.saveWallpaperInfo(url, layout,
100 Constants.WallpaperSourceEnum.Online);