| 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 /** | 5 /** |
| 6 * WallpaperManager constructor. | 6 * WallpaperManager constructor. |
| 7 * | 7 * |
| 8 * WallpaperManager objects encapsulate the functionality of the wallpaper | 8 * WallpaperManager objects encapsulate the functionality of the wallpaper |
| 9 * manager extension. | 9 * manager extension. |
| 10 * | 10 * |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 $('error-container').hidden = false; | 165 $('error-container').hidden = false; |
| 166 }; | 166 }; |
| 167 | 167 |
| 168 /** | 168 /** |
| 169 * Sets manifest loaded from server. Called after manifest is successfully | 169 * Sets manifest loaded from server. Called after manifest is successfully |
| 170 * loaded. | 170 * loaded. |
| 171 * @param {object} manifest The parsed manifest file. | 171 * @param {object} manifest The parsed manifest file. |
| 172 */ | 172 */ |
| 173 WallpaperManager.prototype.onLoadManifestSuccess_ = function(manifest) { | 173 WallpaperManager.prototype.onLoadManifestSuccess_ = function(manifest) { |
| 174 this.manifest_ = manifest; | 174 this.manifest_ = manifest; |
| 175 WallpaperUtil.saveToLocalStorage(Constants.AccessManifestKey, manifest); | 175 WallpaperUtil.saveToStorage(Constants.AccessManifestKey, manifest, false); |
| 176 this.initDom_(); | 176 this.initDom_(); |
| 177 }; | 177 }; |
| 178 | 178 |
| 179 // Sets manifest to previously saved object if any and shows connection error. | 179 // Sets manifest to previously saved object if any and shows connection error. |
| 180 // Called after manifest failed to load. | 180 // Called after manifest failed to load. |
| 181 WallpaperManager.prototype.onLoadManifestFailed_ = function() { | 181 WallpaperManager.prototype.onLoadManifestFailed_ = function() { |
| 182 var accessManifestKey = Constants.AccessManifestKey; | 182 var accessManifestKey = Constants.AccessManifestKey; |
| 183 var self = this; | 183 var self = this; |
| 184 Constants.WallpaperLocalStorage.get(accessManifestKey, function(items) { | 184 Constants.WallpaperLocalStorage.get(accessManifestKey, function(items) { |
| 185 self.manifest_ = items[accessManifestKey] ? items[accessManifestKey] : {}; | 185 self.manifest_ = items[accessManifestKey] ? items[accessManifestKey] : {}; |
| 186 self.showError_(str('connectionFailed')); | 186 self.showError_(str('connectionFailed')); |
| 187 self.initDom_(); | 187 self.initDom_(); |
| 188 $('wallpaper-grid').classList.add('image-picker-offline'); | 188 $('wallpaper-grid').classList.add('image-picker-offline'); |
| 189 }); | 189 }); |
| 190 }; | 190 }; |
| 191 | 191 |
| 192 /** | 192 /** |
| 193 * Toggle surprise me feature of wallpaper picker. It fires an storage | 193 * Toggle surprise me feature of wallpaper picker. It fires an storage |
| 194 * onChanged event. Event handler for that event is in event_page.js. | 194 * onChanged event. Event handler for that event is in event_page.js. |
| 195 * @private | 195 * @private |
| 196 */ | 196 */ |
| 197 WallpaperManager.prototype.toggleSurpriseMe_ = function() { | 197 WallpaperManager.prototype.toggleSurpriseMe_ = function() { |
| 198 var checkbox = $('surprise-me').querySelector('#checkbox'); | 198 var checkbox = $('surprise-me').querySelector('#checkbox'); |
| 199 var shouldEnable = !checkbox.classList.contains('checked'); | 199 var shouldEnable = !checkbox.classList.contains('checked'); |
| 200 WallpaperUtil.saveToLocalStorage(Constants.AccessSurpriseMeEnabledKey, | 200 WallpaperUtil.saveToStorage(Constants.AccessSurpriseMeEnabledKey, |
| 201 shouldEnable, | 201 shouldEnable, false, function() { |
| 202 function() { | |
| 203 if (chrome.runtime.lastError == null) { | 202 if (chrome.runtime.lastError == null) { |
| 204 if (shouldEnable) { | 203 if (shouldEnable) { |
| 205 checkbox.classList.add('checked'); | 204 checkbox.classList.add('checked'); |
| 206 } else { | 205 } else { |
| 207 checkbox.classList.remove('checked'); | 206 checkbox.classList.remove('checked'); |
| 208 } | 207 } |
| 209 $('categories-list').disabled = shouldEnable; | 208 $('categories-list').disabled = shouldEnable; |
| 210 $('wallpaper-grid').disabled = shouldEnable; | 209 $('wallpaper-grid').disabled = shouldEnable; |
| 211 } else { | 210 } else { |
| 212 // TODO(bshe): show error message to user. | 211 // TODO(bshe): show error message to user. |
| (...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 484 Constants.HighResolutionSuffix; | 483 Constants.HighResolutionSuffix; |
| 485 var selectedGridItem = this.wallpaperGrid_.getListItem(selectedItem); | 484 var selectedGridItem = this.wallpaperGrid_.getListItem(selectedItem); |
| 486 | 485 |
| 487 chrome.wallpaperPrivate.setWallpaperIfExists(wallpaperURL, | 486 chrome.wallpaperPrivate.setWallpaperIfExists(wallpaperURL, |
| 488 selectedItem.layout, | 487 selectedItem.layout, |
| 489 selectedItem.source, | 488 selectedItem.source, |
| 490 function(exists) { | 489 function(exists) { |
| 491 if (exists) { | 490 if (exists) { |
| 492 self.currentWallpaper_ = wallpaperURL; | 491 self.currentWallpaper_ = wallpaperURL; |
| 493 self.wallpaperGrid_.activeItem = selectedItem; | 492 self.wallpaperGrid_.activeItem = selectedItem; |
| 493 WallpaperUtil.saveWallpaperInfo(wallpaperURL, selectedItem.layout, |
| 494 selectedItem.source); |
| 494 return; | 495 return; |
| 495 } | 496 } |
| 496 | 497 |
| 497 // Falls back to request wallpaper from server. | 498 // Falls back to request wallpaper from server. |
| 498 if (self.wallpaperRequest_) | 499 if (self.wallpaperRequest_) |
| 499 self.wallpaperRequest_.abort(); | 500 self.wallpaperRequest_.abort(); |
| 500 | 501 |
| 501 self.wallpaperRequest_ = new XMLHttpRequest(); | 502 self.wallpaperRequest_ = new XMLHttpRequest(); |
| 502 self.progressManager_.reset(self.wallpaperRequest_, selectedGridItem); | 503 self.progressManager_.reset(self.wallpaperRequest_, selectedGridItem); |
| 503 | 504 |
| 504 var onSuccess = function(xhr) { | 505 var onSuccess = function(xhr) { |
| 505 var image = xhr.response; | 506 var image = xhr.response; |
| 506 chrome.wallpaperPrivate.setWallpaper(image, selectedItem.layout, | 507 chrome.wallpaperPrivate.setWallpaper(image, selectedItem.layout, |
| 507 wallpaperURL, | 508 wallpaperURL, |
| 508 self.onFinished_.bind(self, selectedGridItem, selectedItem)); | 509 self.onFinished_.bind(self, selectedGridItem, selectedItem)); |
| 509 self.currentWallpaper_ = wallpaperURL; | 510 self.currentWallpaper_ = wallpaperURL; |
| 511 WallpaperUtil.saveWallpaperInfo(wallpaperURL, selectedItem.layout, |
| 512 selectedItem.source); |
| 510 self.wallpaperRequest_ = null; | 513 self.wallpaperRequest_ = null; |
| 511 }; | 514 }; |
| 512 var onFailure = function() { | 515 var onFailure = function() { |
| 513 self.progressManager_.hideProgressBar(selectedGridItem); | 516 self.progressManager_.hideProgressBar(selectedGridItem); |
| 514 self.showError_(str('downloadFailed')); | 517 self.showError_(str('downloadFailed')); |
| 515 self.wallpaperRequest_ = null; | 518 self.wallpaperRequest_ = null; |
| 516 }; | 519 }; |
| 517 WallpaperUtil.fetchURL(wallpaperURL, 'arraybuffer', onSuccess, | 520 WallpaperUtil.fetchURL(wallpaperURL, 'arraybuffer', onSuccess, |
| 518 onFailure, self.wallpaperRequest_); | 521 onFailure, self.wallpaperRequest_); |
| 519 }); | 522 }); |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 722 var wallpaperInfo = { | 725 var wallpaperInfo = { |
| 723 baseURL: fileName, | 726 baseURL: fileName, |
| 724 layout: layout, | 727 layout: layout, |
| 725 source: Constants.WallpaperSourceEnum.Custom, | 728 source: Constants.WallpaperSourceEnum.Custom, |
| 726 availableOffline: true | 729 availableOffline: true |
| 727 }; | 730 }; |
| 728 self.wallpaperGrid_.dataModel.splice(0, 0, wallpaperInfo); | 731 self.wallpaperGrid_.dataModel.splice(0, 0, wallpaperInfo); |
| 729 self.wallpaperGrid_.selectedItem = wallpaperInfo; | 732 self.wallpaperGrid_.selectedItem = wallpaperInfo; |
| 730 self.wallpaperGrid_.activeItem = wallpaperInfo; | 733 self.wallpaperGrid_.activeItem = wallpaperInfo; |
| 731 self.currentWallpaper_ = fileName; | 734 self.currentWallpaper_ = fileName; |
| 732 WallpaperUtil.saveToLocalStorage(self.currentWallpaper_, | 735 WallpaperUtil.saveToStorage(self.currentWallpaper_, layout, |
| 733 layout); | 736 false); |
| 734 }; | 737 }; |
| 735 | 738 |
| 736 fileWriter.onerror = errorHandler; | 739 fileWriter.onerror = errorHandler; |
| 737 | 740 |
| 738 var blob = new Blob([new Int8Array(thumbnail)], | 741 var blob = new Blob([new Int8Array(thumbnail)], |
| 739 {'type' : 'image\/jpeg'}); | 742 {'type' : 'image\/jpeg'}); |
| 740 fileWriter.write(blob); | 743 fileWriter.write(blob); |
| 741 }, errorHandler); | 744 }, errorHandler); |
| 742 }, errorHandler); | 745 }, errorHandler); |
| 743 }; | 746 }; |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 818 success, | 821 success, |
| 819 failure) { | 822 failure) { |
| 820 var self = this; | 823 var self = this; |
| 821 var onFinished = function(opt_thumbnail) { | 824 var onFinished = function(opt_thumbnail) { |
| 822 if (chrome.runtime.lastError != undefined) { | 825 if (chrome.runtime.lastError != undefined) { |
| 823 self.showError_(chrome.runtime.lastError.message); | 826 self.showError_(chrome.runtime.lastError.message); |
| 824 $('set-wallpaper-layout').disabled = true; | 827 $('set-wallpaper-layout').disabled = true; |
| 825 failure(); | 828 failure(); |
| 826 } else { | 829 } else { |
| 827 success(opt_thumbnail); | 830 success(opt_thumbnail); |
| 831 // Custom wallpapers are not synced yet. If login on a different |
| 832 // computer after set a custom wallpaper, wallpaper wont change by sync. |
| 833 WallpaperUtil.saveWallpaperInfo(fileName, layout, |
| 834 Constants.WallpaperSourceEnum.Custom); |
| 828 } | 835 } |
| 829 }; | 836 }; |
| 830 | 837 |
| 831 chrome.wallpaperPrivate.setCustomWallpaper(wallpaper, layout, | 838 chrome.wallpaperPrivate.setCustomWallpaper(wallpaper, layout, |
| 832 generateThumbnail, | 839 generateThumbnail, |
| 833 fileName, onFinished); | 840 fileName, onFinished); |
| 834 }; | 841 }; |
| 835 | 842 |
| 836 /** | 843 /** |
| 837 * Sets wallpaper finished. Displays error message if any. | 844 * Sets wallpaper finished. Displays error message if any. |
| (...skipping 22 matching lines...) Expand all Loading... |
| 860 */ | 867 */ |
| 861 WallpaperManager.prototype.onWallpaperLayoutChanged_ = function() { | 868 WallpaperManager.prototype.onWallpaperLayoutChanged_ = function() { |
| 862 var layout = getSelectedLayout(); | 869 var layout = getSelectedLayout(); |
| 863 var self = this; | 870 var self = this; |
| 864 chrome.wallpaperPrivate.setCustomWallpaperLayout(layout, function() { | 871 chrome.wallpaperPrivate.setCustomWallpaperLayout(layout, function() { |
| 865 if (chrome.runtime.lastError != undefined) { | 872 if (chrome.runtime.lastError != undefined) { |
| 866 self.showError_(chrome.runtime.lastError.message); | 873 self.showError_(chrome.runtime.lastError.message); |
| 867 self.removeCustomWallpaper(fileName); | 874 self.removeCustomWallpaper(fileName); |
| 868 $('set-wallpaper-layout').disabled = true; | 875 $('set-wallpaper-layout').disabled = true; |
| 869 } else { | 876 } else { |
| 870 WallpaperUtil.saveToLocalStorage(self.currentWallpaper_, layout); | 877 WallpaperUtil.saveToStorage(self.currentWallpaper_, layout, false); |
| 871 } | 878 } |
| 872 }); | 879 }); |
| 873 }; | 880 }; |
| 874 | 881 |
| 875 /** | 882 /** |
| 876 * Handles user clicking on a different category. | 883 * Handles user clicking on a different category. |
| 877 */ | 884 */ |
| 878 WallpaperManager.prototype.onCategoriesChange_ = function() { | 885 WallpaperManager.prototype.onCategoriesChange_ = function() { |
| 879 var categoriesList = this.categoriesList_; | 886 var categoriesList = this.categoriesList_; |
| 880 var selectedIndex = categoriesList.selectionModel.selectedIndex; | 887 var selectedIndex = categoriesList.selectionModel.selectedIndex; |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 974 } | 981 } |
| 975 } | 982 } |
| 976 } | 983 } |
| 977 this.wallpaperGrid_.dataModel = wallpapersDataModel; | 984 this.wallpaperGrid_.dataModel = wallpapersDataModel; |
| 978 this.wallpaperGrid_.selectedItem = selectedItem; | 985 this.wallpaperGrid_.selectedItem = selectedItem; |
| 979 this.wallpaperGrid_.activeItem = selectedItem; | 986 this.wallpaperGrid_.activeItem = selectedItem; |
| 980 } | 987 } |
| 981 }; | 988 }; |
| 982 | 989 |
| 983 })(); | 990 })(); |
| OLD | NEW |