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