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    // 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) {
66        onSuccess(this);
67      } else {
68        onFailure();
69      }
70    });
71    xhr.addEventListener('error', onFailure);
72    xhr.open('GET', url, true);
73    xhr.responseType = type;
74    xhr.send(null);
75  } catch (e) {
76    onFailure();
77  }
78};
79
80/**
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.
86 */
87WallpaperUtil.setOnlineWallpaper = function(url, layout, onSuccess, onFailure) {
88  var self = this;
89  chrome.wallpaperPrivate.setWallpaperIfExists(url, layout, function(exists) {
90    if (exists) {
91      onSuccess();
92      return;
93    }
94
95    self.fetchURL(url, 'arraybuffer', function(xhr) {
96      if (xhr.response != null) {
97        chrome.wallpaperPrivate.setWallpaper(xhr.response, layout, url,
98                                             onSuccess);
99        self.saveWallpaperInfo(url, layout,
100                               Constants.WallpaperSourceEnum.Online);
101      } else {
102        onFailure();
103      }
104    }, onFailure);
105  });
106};
107
108/**
109 * Runs chrome.test.sendMessage in test environment. Does nothing if running
110 * in production environment.
111 *
112 * @param {string} message Test message to send.
113 */
114WallpaperUtil.testSendMessage = function(message) {
115  var test = chrome.test || window.top.chrome.test;
116  if (test)
117    test.sendMessage(message);
118};
119