| 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 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 i18nTemplate.process(this.document_, loadTimeData); | 246 i18nTemplate.process(this.document_, loadTimeData); |
| 247 this.initCategoriesList_(); | 247 this.initCategoriesList_(); |
| 248 this.initThumbnailsGrid_(); | 248 this.initThumbnailsGrid_(); |
| 249 this.presetCategory_(); | 249 this.presetCategory_(); |
| 250 | 250 |
| 251 $('file-selector').addEventListener( | 251 $('file-selector').addEventListener( |
| 252 'change', this.onFileSelectorChanged_.bind(this)); | 252 'change', this.onFileSelectorChanged_.bind(this)); |
| 253 $('set-wallpaper-layout').addEventListener( | 253 $('set-wallpaper-layout').addEventListener( |
| 254 'change', this.onWallpaperLayoutChanged_.bind(this)); | 254 'change', this.onWallpaperLayoutChanged_.bind(this)); |
| 255 | 255 |
| 256 if (loadTimeData.valueExists('wallpaperAppName')) { |
| 257 $('wallpaper-set-by-message').textContent = loadTimeData.getStringF( |
| 258 'currentWallpaperSetByMessage', str('wallpaperAppName')); |
| 259 } |
| 260 |
| 256 if (this.enableOnlineWallpaper_) { | 261 if (this.enableOnlineWallpaper_) { |
| 257 var self = this; | 262 var self = this; |
| 258 $('surprise-me').hidden = false; | 263 $('surprise-me').hidden = false; |
| 259 $('surprise-me').addEventListener('click', | 264 $('surprise-me').addEventListener('click', |
| 260 this.toggleSurpriseMe_.bind(this)); | 265 this.toggleSurpriseMe_.bind(this)); |
| 261 Constants.WallpaperSyncStorage.get(Constants.AccessSurpriseMeEnabledKey, | 266 Constants.WallpaperSyncStorage.get(Constants.AccessSurpriseMeEnabledKey, |
| 262 function(items) { | 267 function(items) { |
| 263 // Surprise me has been moved from local to sync storage, prefer | 268 // Surprise me has been moved from local to sync storage, prefer |
| 264 // values from sync, but if unset check local and update synced pref | 269 // values from sync, but if unset check local and update synced pref |
| 265 // if applicable. | 270 // if applicable. |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 345 return; | 350 return; |
| 346 this.removeCustomWallpaper(item.baseURL); | 351 this.removeCustomWallpaper(item.baseURL); |
| 347 wallpaperGrid.dataModel.splice(selectedIndex, 1); | 352 wallpaperGrid.dataModel.splice(selectedIndex, 1); |
| 348 // Calculate the number of remaining custom wallpapers. The add new button | 353 // Calculate the number of remaining custom wallpapers. The add new button |
| 349 // in data model needs to be excluded. | 354 // in data model needs to be excluded. |
| 350 var customWallpaperCount = wallpaperGrid.dataModel.length - 1; | 355 var customWallpaperCount = wallpaperGrid.dataModel.length - 1; |
| 351 if (customWallpaperCount == 0) { | 356 if (customWallpaperCount == 0) { |
| 352 // Active custom wallpaper is also copied in chronos data dir. It needs | 357 // Active custom wallpaper is also copied in chronos data dir. It needs |
| 353 // to be deleted. | 358 // to be deleted. |
| 354 chrome.wallpaperPrivate.resetWallpaper(); | 359 chrome.wallpaperPrivate.resetWallpaper(); |
| 360 this.onWallpaperChanged_(null, null); |
| 355 } else { | 361 } else { |
| 356 selectedIndex = Math.min(selectedIndex, customWallpaperCount - 1); | 362 selectedIndex = Math.min(selectedIndex, customWallpaperCount - 1); |
| 357 wallpaperGrid.selectionModel.selectedIndex = selectedIndex; | 363 wallpaperGrid.selectionModel.selectedIndex = selectedIndex; |
| 358 } | 364 } |
| 359 event.cancelBubble = true; | 365 event.cancelBubble = true; |
| 360 } | 366 } |
| 361 }; | 367 }; |
| 362 | 368 |
| 363 /** | 369 /** |
| 364 * Decides if a command can be executed on current target. | 370 * Decides if a command can be executed on current target. |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 466 this.wallpaperRequest_.addEventListener('loadend', function() { | 472 this.wallpaperRequest_.addEventListener('loadend', function() { |
| 467 // Close window on wallpaper loading finished. | 473 // Close window on wallpaper loading finished. |
| 468 window.close(); | 474 window.close(); |
| 469 }); | 475 }); |
| 470 } else { | 476 } else { |
| 471 window.close(); | 477 window.close(); |
| 472 } | 478 } |
| 473 }; | 479 }; |
| 474 | 480 |
| 475 /** | 481 /** |
| 482 * Moves the check mark to |activeItem| and hides the wallpaper set by third |
| 483 * party message if any. Called when wallpaper changed successfully. |
| 484 * @param {?Object} activeItem The active item in WallpaperThumbnailsGrid's |
| 485 * data model. |
| 486 * @param {?string} currentWallpaperURL The URL or filename of current |
| 487 * wallpaper. |
| 488 */ |
| 489 WallpaperManager.prototype.onWallpaperChanged_ = function( |
| 490 activeItem, currentWallpaperURL) { |
| 491 this.wallpaperGrid_.activeItem = activeItem; |
| 492 this.currentWallpaper_ = currentWallpaperURL; |
| 493 // Hides the wallpaper set by message. |
| 494 $('wallpaper-set-by-message').textContent = ''; |
| 495 }; |
| 496 |
| 497 /** |
| 476 * Sets wallpaper to the corresponding wallpaper of selected thumbnail. | 498 * Sets wallpaper to the corresponding wallpaper of selected thumbnail. |
| 477 * @param {{baseURL: string, layout: string, source: string, | 499 * @param {{baseURL: string, layout: string, source: string, |
| 478 * availableOffline: boolean, opt_dynamicURL: string, | 500 * availableOffline: boolean, opt_dynamicURL: string, |
| 479 * opt_author: string, opt_authorWebsite: string}} | 501 * opt_author: string, opt_authorWebsite: string}} |
| 480 * selectedItem the selected item in WallpaperThumbnailsGrid's data | 502 * selectedItem the selected item in WallpaperThumbnailsGrid's data |
| 481 * model. | 503 * model. |
| 482 */ | 504 */ |
| 483 WallpaperManager.prototype.setSelectedWallpaper_ = function(selectedItem) { | 505 WallpaperManager.prototype.setSelectedWallpaper_ = function(selectedItem) { |
| 484 var self = this; | 506 var self = this; |
| 485 switch (selectedItem.source) { | 507 switch (selectedItem.source) { |
| 486 case Constants.WallpaperSourceEnum.Custom: | 508 case Constants.WallpaperSourceEnum.Custom: |
| 487 var errorHandler = this.onFileSystemError_.bind(this); | 509 var errorHandler = this.onFileSystemError_.bind(this); |
| 488 var setActive = function() { | |
| 489 self.wallpaperGrid_.activeItem = selectedItem; | |
| 490 self.currentWallpaper_ = selectedItem.baseURL; | |
| 491 }; | |
| 492 var success = function(dirEntry) { | 510 var success = function(dirEntry) { |
| 493 dirEntry.getFile(selectedItem.baseURL, {create: false}, | 511 dirEntry.getFile(selectedItem.baseURL, {create: false}, |
| 494 function(fileEntry) { | 512 function(fileEntry) { |
| 495 fileEntry.file(function(file) { | 513 fileEntry.file(function(file) { |
| 496 var reader = new FileReader(); | 514 var reader = new FileReader(); |
| 497 reader.readAsArrayBuffer(file); | 515 reader.readAsArrayBuffer(file); |
| 498 reader.addEventListener('error', errorHandler); | 516 reader.addEventListener('error', errorHandler); |
| 499 reader.addEventListener('load', function(e) { | 517 reader.addEventListener('load', function(e) { |
| 500 self.setCustomWallpaper(e.target.result, | 518 self.setCustomWallpaper(e.target.result, |
| 501 selectedItem.layout, | 519 selectedItem.layout, |
| 502 false, selectedItem.baseURL, | 520 false, selectedItem.baseURL, |
| 503 setActive, errorHandler); | 521 self.onWallpaperChanged_.bind(self, |
| 522 selectedItem, selectedItem.baseURL), |
| 523 errorHandler); |
| 504 }); | 524 }); |
| 505 }, errorHandler); | 525 }, errorHandler); |
| 506 }, errorHandler); | 526 }, errorHandler); |
| 507 } | 527 } |
| 508 this.wallpaperDirs_.getDirectory(WallpaperDirNameEnum.ORIGINAL, | 528 this.wallpaperDirs_.getDirectory(WallpaperDirNameEnum.ORIGINAL, |
| 509 success, errorHandler); | 529 success, errorHandler); |
| 510 break; | 530 break; |
| 511 case Constants.WallpaperSourceEnum.OEM: | 531 case Constants.WallpaperSourceEnum.OEM: |
| 512 // Resets back to default wallpaper. | 532 // Resets back to default wallpaper. |
| 513 chrome.wallpaperPrivate.resetWallpaper(); | 533 chrome.wallpaperPrivate.resetWallpaper(); |
| 514 this.currentWallpaper_ = selectedItem.baseURL; | 534 this.onWallpaperChanged_(selectedItem, selectedItem.baseURL); |
| 515 this.wallpaperGrid_.activeItem = selectedItem; | |
| 516 WallpaperUtil.saveWallpaperInfo(wallpaperURL, selectedItem.layout, | 535 WallpaperUtil.saveWallpaperInfo(wallpaperURL, selectedItem.layout, |
| 517 selectedItem.source); | 536 selectedItem.source); |
| 518 break; | 537 break; |
| 519 case Constants.WallpaperSourceEnum.Online: | 538 case Constants.WallpaperSourceEnum.Online: |
| 520 var wallpaperURL = selectedItem.baseURL + | 539 var wallpaperURL = selectedItem.baseURL + |
| 521 Constants.HighResolutionSuffix; | 540 Constants.HighResolutionSuffix; |
| 522 var selectedGridItem = this.wallpaperGrid_.getListItem(selectedItem); | 541 var selectedGridItem = this.wallpaperGrid_.getListItem(selectedItem); |
| 523 | 542 |
| 524 chrome.wallpaperPrivate.setWallpaperIfExists(wallpaperURL, | 543 chrome.wallpaperPrivate.setWallpaperIfExists(wallpaperURL, |
| 525 selectedItem.layout, | 544 selectedItem.layout, |
| 526 function(exists) { | 545 function(exists) { |
| 527 if (exists) { | 546 if (exists) { |
| 528 self.currentWallpaper_ = wallpaperURL; | 547 self.onWallpaperChanged_(selectedItem, wallpaperURL); |
| 529 self.wallpaperGrid_.activeItem = selectedItem; | |
| 530 WallpaperUtil.saveWallpaperInfo(wallpaperURL, selectedItem.layout, | 548 WallpaperUtil.saveWallpaperInfo(wallpaperURL, selectedItem.layout, |
| 531 selectedItem.source); | 549 selectedItem.source); |
| 532 return; | 550 return; |
| 533 } | 551 } |
| 534 | 552 |
| 535 // Falls back to request wallpaper from server. | 553 // Falls back to request wallpaper from server. |
| 536 if (self.wallpaperRequest_) | 554 if (self.wallpaperRequest_) |
| 537 self.wallpaperRequest_.abort(); | 555 self.wallpaperRequest_.abort(); |
| 538 | 556 |
| 539 self.wallpaperRequest_ = new XMLHttpRequest(); | 557 self.wallpaperRequest_ = new XMLHttpRequest(); |
| 540 self.progressManager_.reset(self.wallpaperRequest_, selectedGridItem); | 558 self.progressManager_.reset(self.wallpaperRequest_, selectedGridItem); |
| 541 | 559 |
| 542 var onSuccess = function(xhr) { | 560 var onSuccess = function(xhr) { |
| 543 var image = xhr.response; | 561 var image = xhr.response; |
| 544 chrome.wallpaperPrivate.setWallpaper(image, selectedItem.layout, | 562 chrome.wallpaperPrivate.setWallpaper(image, selectedItem.layout, |
| 545 wallpaperURL, | 563 wallpaperURL, |
| 546 self.onFinished_.bind(self, selectedGridItem, selectedItem)); | 564 function() { |
| 547 self.currentWallpaper_ = wallpaperURL; | 565 self.progressManager_.hideProgressBar(selectedGridItem); |
| 566 |
| 567 if (chrome.runtime.lastError != undefined && |
| 568 chrome.runtime.lastError.message != |
| 569 str('canceledWallpaper')) { |
| 570 self.showError_(chrome.runtime.lastError.message); |
| 571 } else { |
| 572 self.onWallpaperChanged_(selectedItem, wallpaperURL); |
| 573 } |
| 574 }); |
| 548 WallpaperUtil.saveWallpaperInfo(wallpaperURL, selectedItem.layout, | 575 WallpaperUtil.saveWallpaperInfo(wallpaperURL, selectedItem.layout, |
| 549 selectedItem.source); | 576 selectedItem.source); |
| 550 self.wallpaperRequest_ = null; | 577 self.wallpaperRequest_ = null; |
| 551 }; | 578 }; |
| 552 var onFailure = function() { | 579 var onFailure = function() { |
| 553 self.progressManager_.hideProgressBar(selectedGridItem); | 580 self.progressManager_.hideProgressBar(selectedGridItem); |
| 554 self.showError_(str('downloadFailed')); | 581 self.showError_(str('downloadFailed')); |
| 555 self.wallpaperRequest_ = null; | 582 self.wallpaperRequest_ = null; |
| 556 }; | 583 }; |
| 557 WallpaperUtil.fetchURL(wallpaperURL, 'arraybuffer', onSuccess, | 584 WallpaperUtil.fetchURL(wallpaperURL, 'arraybuffer', onSuccess, |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 783 fileWriter.onwriteend = function(e) { | 810 fileWriter.onwriteend = function(e) { |
| 784 $('set-wallpaper-layout').disabled = false; | 811 $('set-wallpaper-layout').disabled = false; |
| 785 var wallpaperInfo = { | 812 var wallpaperInfo = { |
| 786 baseURL: fileName, | 813 baseURL: fileName, |
| 787 layout: layout, | 814 layout: layout, |
| 788 source: Constants.WallpaperSourceEnum.Custom, | 815 source: Constants.WallpaperSourceEnum.Custom, |
| 789 availableOffline: true | 816 availableOffline: true |
| 790 }; | 817 }; |
| 791 self.wallpaperGrid_.dataModel.splice(0, 0, wallpaperInfo); | 818 self.wallpaperGrid_.dataModel.splice(0, 0, wallpaperInfo); |
| 792 self.wallpaperGrid_.selectedItem = wallpaperInfo; | 819 self.wallpaperGrid_.selectedItem = wallpaperInfo; |
| 793 self.wallpaperGrid_.activeItem = wallpaperInfo; | 820 self.onWallpaperChanged_(wallpaperInfo, fileName); |
| 794 self.currentWallpaper_ = fileName; | |
| 795 WallpaperUtil.saveToStorage(self.currentWallpaper_, layout, | 821 WallpaperUtil.saveToStorage(self.currentWallpaper_, layout, |
| 796 false); | 822 false); |
| 797 }; | 823 }; |
| 798 | 824 |
| 799 fileWriter.onerror = errorHandler; | 825 fileWriter.onerror = errorHandler; |
| 800 | 826 |
| 801 var blob = new Blob([new Int8Array(thumbnail)], | 827 var blob = new Blob([new Int8Array(thumbnail)], |
| 802 {'type' : 'image\/jpeg'}); | 828 {'type' : 'image\/jpeg'}); |
| 803 fileWriter.write(blob); | 829 fileWriter.write(blob); |
| 804 }, errorHandler); | 830 }, errorHandler); |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 895 Constants.WallpaperSourceEnum.Custom); | 921 Constants.WallpaperSourceEnum.Custom); |
| 896 } | 922 } |
| 897 }; | 923 }; |
| 898 | 924 |
| 899 chrome.wallpaperPrivate.setCustomWallpaper(wallpaper, layout, | 925 chrome.wallpaperPrivate.setCustomWallpaper(wallpaper, layout, |
| 900 generateThumbnail, | 926 generateThumbnail, |
| 901 fileName, onFinished); | 927 fileName, onFinished); |
| 902 }; | 928 }; |
| 903 | 929 |
| 904 /** | 930 /** |
| 905 * Sets wallpaper finished. Displays error message if any. | |
| 906 * @param {WallpaperThumbnailsGridItem=} opt_selectedGridItem The wallpaper | |
| 907 * thumbnail grid item. It extends from cr.ui.ListItem. | |
| 908 * @param {{baseURL: string, layout: string, source: string, | |
| 909 * availableOffline: boolean, opt_dynamicURL: string, | |
| 910 * opt_author: string, opt_authorWebsite: string}=} | |
| 911 * opt_selectedItem the selected item in WallpaperThumbnailsGrid's data | |
| 912 * model. | |
| 913 */ | |
| 914 WallpaperManager.prototype.onFinished_ = function(opt_selectedGridItem, | |
| 915 opt_selectedItem) { | |
| 916 if (opt_selectedGridItem) | |
| 917 this.progressManager_.hideProgressBar(opt_selectedGridItem); | |
| 918 | |
| 919 if (chrome.runtime.lastError != undefined && | |
| 920 chrome.runtime.lastError.message != str('canceledWallpaper')) { | |
| 921 this.showError_(chrome.runtime.lastError.message); | |
| 922 } else if (opt_selectedItem) { | |
| 923 this.wallpaperGrid_.activeItem = opt_selectedItem; | |
| 924 } | |
| 925 }; | |
| 926 | |
| 927 /** | |
| 928 * Handles the layout setting change of custom wallpaper. | 931 * Handles the layout setting change of custom wallpaper. |
| 929 */ | 932 */ |
| 930 WallpaperManager.prototype.onWallpaperLayoutChanged_ = function() { | 933 WallpaperManager.prototype.onWallpaperLayoutChanged_ = function() { |
| 931 var layout = getSelectedLayout(); | 934 var layout = getSelectedLayout(); |
| 932 var self = this; | 935 var self = this; |
| 933 chrome.wallpaperPrivate.setCustomWallpaperLayout(layout, function() { | 936 chrome.wallpaperPrivate.setCustomWallpaperLayout(layout, function() { |
| 934 if (chrome.runtime.lastError != undefined && | 937 if (chrome.runtime.lastError != undefined && |
| 935 chrome.runtime.lastError.message != str('canceledWallpaper')) { | 938 chrome.runtime.lastError.message != str('canceledWallpaper')) { |
| 936 self.showError_(chrome.runtime.lastError.message); | 939 self.showError_(chrome.runtime.lastError.message); |
| 937 self.removeCustomWallpaper(fileName); | 940 self.removeCustomWallpaper(fileName); |
| 938 $('set-wallpaper-layout').disabled = true; | 941 $('set-wallpaper-layout').disabled = true; |
| 939 } else { | 942 } else { |
| 940 WallpaperUtil.saveToStorage(self.currentWallpaper_, layout, false); | 943 WallpaperUtil.saveToStorage(self.currentWallpaper_, layout, false); |
| 944 self.onWallpaperChanged_(self.wallpaperGrid_.activeItem, |
| 945 self.currentWallpaper_); |
| 941 } | 946 } |
| 942 }); | 947 }); |
| 943 }; | 948 }; |
| 944 | 949 |
| 945 /** | 950 /** |
| 946 * Handles user clicking on a different category. | 951 * Handles user clicking on a different category. |
| 947 */ | 952 */ |
| 948 WallpaperManager.prototype.onCategoriesChange_ = function() { | 953 WallpaperManager.prototype.onCategoriesChange_ = function() { |
| 949 var categoriesList = this.categoriesList_; | 954 var categoriesList = this.categoriesList_; |
| 950 var selectedIndex = categoriesList.selectionModel.selectedIndex; | 955 var selectedIndex = categoriesList.selectionModel.selectedIndex; |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1055 } | 1060 } |
| 1056 } | 1061 } |
| 1057 } | 1062 } |
| 1058 this.wallpaperGrid_.dataModel = wallpapersDataModel; | 1063 this.wallpaperGrid_.dataModel = wallpapersDataModel; |
| 1059 this.wallpaperGrid_.selectedItem = selectedItem; | 1064 this.wallpaperGrid_.selectedItem = selectedItem; |
| 1060 this.wallpaperGrid_.activeItem = selectedItem; | 1065 this.wallpaperGrid_.activeItem = selectedItem; |
| 1061 } | 1066 } |
| 1062 }; | 1067 }; |
| 1063 | 1068 |
| 1064 })(); | 1069 })(); |
| OLD | NEW |