Commit 841235c1 authored by Wenzhao Zang's avatar Wenzhao Zang Committed by Commit Bot

cros: Clean up old wallpaper picker code (Part I)

Remove the |useNewWallpaperPicker| checks, and modify some comments.

Will need follow-up CLs to:
1) Remove the flag in //ash.
2) Take a second pass to check which CSS styles/functions/variables
   are unused.

Bug: 812085
Change-Id: I7faa99c16715360a95feed50b5041d89c8c322a1
Reviewed-on: https://chromium-review.googlesource.com/c/1297308Reviewed-by: default avatarXiaoqian Dai <xdai@chromium.org>
Commit-Queue: Wenzhao (Colin) Zang <wzang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#602432}
parent 16826e3d
......@@ -25,8 +25,7 @@
AccessSyncWallpaperInfoKey: 'wallpaper-sync-info-key',
/**
* Key to access last changed date of a surprise me wallpaper (on the old
* picker) or a daily refresh wallpaper (on the new picker) in
* Key to access last changed date of a daily refresh wallpaper in
* chrome.storage.local or chrome.storage.sync.
*/
AccessLastSurpriseWallpaperChangedDate: 'wallpaper-last-changed-date-key',
......@@ -44,20 +43,19 @@
AccessSyncSurpriseMeEnabledKey: 'sync-surprise-me-enabled-key',
/**
* Key to access the info related to daily refresh feature (on the new
* wallpaper picker) in chrome.storage.local.
* Key to access the info related to daily refresh feature in
* chrome.storage.local.
*/
AccessLocalDailyRefreshInfoKey: 'daily-refresh-info-key',
/**
* Key to access the info related to daily refresh feature (on the new
* wallpaper picker) in chrome.storage.sync.
* Key to access the info related to daily refresh feature in
* chrome.storage.sync.
*/
AccessSyncDailyRefreshInfoKey: 'sync-daily-refresh-info-key',
/**
* Key to access the images info (on the new wallpaper picker) in
* chrome.storage.local.
* Key to access the images info in chrome.storage.local.
*/
AccessLocalImagesInfoKey: 'images-info-key',
......
......@@ -7,14 +7,13 @@ var wallpaperPickerWindow = null;
var surpriseWallpaper = null;
/**
* Returns information related to the wallpaper picker.
* @param {function} callback A callback function that takes two values:
* |useNewWallpaperPicker|: if the new wallpaper picker is enabled.
* Returns the highResolutionSuffix.
* @param {function} callback A callback function that takes one value:
* |highResolutionSuffix|: the suffix to append to the wallpaper urls.
*/
function getWallpaperPickerInfo(callback) {
function getHighResolutionSuffix(callback) {
chrome.wallpaperPrivate.getStrings(strings => {
callback(strings['useNewWallpaperPicker'], strings['highResolutionSuffix']);
callback(strings['highResolutionSuffix']);
});
}
......@@ -170,42 +169,8 @@ SurpriseWallpaper.prototype.setRandomWallpaper_ = function(dateString) {
});
};
getWallpaperPickerInfo((useNewWallpaperPicker, highResolutionSuffix) => {
if (useNewWallpaperPicker)
getHighResolutionSuffix(highResolutionSuffix => {
this.setRandomWallpaperFromServer_(onSuccess, highResolutionSuffix);
else
this.setRandomWallpaperFromManifest_(onSuccess, highResolutionSuffix);
});
};
/**
* Sets wallpaper to be a random one found in the stored manifest file. If the
* wallpaper download fails, retry one hour later. Wallpapers that are disabled
* for surprise me are excluded.
* @param {function} onSuccess The success callback.
* @param {string} suffix The url suffix for high resolution wallpaper.
* @private
*/
SurpriseWallpaper.prototype.setRandomWallpaperFromManifest_ = function(
onSuccess, suffix) {
Constants.WallpaperLocalStorage.get(
Constants.AccessLocalManifestKey, items => {
var manifest = items[Constants.AccessLocalManifestKey];
if (manifest && manifest.wallpaper_list) {
var filtered = manifest.wallpaper_list.filter(function(element) {
// Older version manifest do not have available_for_surprise_me
// field. In this case, no wallpaper should be filtered out.
return element.available_for_surprise_me ||
element.available_for_surprise_me == undefined;
});
var index = Math.floor(Math.random() * filtered.length);
var wallpaper = filtered[index];
var wallpaperUrl = wallpaper.base_url + suffix;
WallpaperUtil.setOnlineWallpaperWithoutPreview(
wallpaperUrl, wallpaper.default_layout,
onSuccess.bind(null, wallpaperUrl, wallpaper.default_layout),
this.retryLater_.bind(this));
}
});
};
......@@ -351,21 +316,11 @@ chrome.app.runtime.onLaunched.addListener(function() {
return;
}
getWallpaperPickerInfo((useNewWallpaperPicker, highResolutionSuffix) => {
var options =
useNewWallpaperPicker ?
{
var options = {
frame: 'none',
innerBounds: {width: 768, height: 512, minWidth: 768, minHeight: 512},
resizable: true,
alphaEnabled: true
} :
{
frame: 'none',
width: 574,
height: 420,
resizable: false,
alphaEnabled: true
};
chrome.app.window.create('main.html', options, function(window) {
......@@ -382,17 +337,13 @@ chrome.app.runtime.onLaunched.addListener(function() {
chrome.wallpaperPrivate.restoreMinimizedWindows();
}
});
if (useNewWallpaperPicker) {
// By design, the new wallpaper picker should never be shown on top of
// By design, the wallpaper picker should never be shown on top of
// another window.
wallpaperPickerWindow.contentWindow.addEventListener(
'focus', function() {
wallpaperPickerWindow.contentWindow.addEventListener('focus', function() {
chrome.wallpaperPrivate.minimizeInactiveWindows();
});
}
WallpaperUtil.testSendMessage('wallpaper-window-created');
});
});
});
chrome.syncFileSystem.onFileStatusChanged.addListener(function(detail) {
......@@ -433,7 +384,6 @@ chrome.syncFileSystem.onFileStatusChanged.addListener(function(detail) {
chrome.storage.onChanged.addListener(function(changes, namespace) {
WallpaperUtil.enabledSyncThemesCallback(function(syncEnabled) {
// Daily refresh feature is on the new wallpaper picker only.
var updateDailyRefreshStates = key => {
if (!changes[key])
return;
......@@ -474,17 +424,6 @@ chrome.storage.onChanged.addListener(function(changes, namespace) {
if (!wallpaperPickerWindow)
return;
var wpDocument = wallpaperPickerWindow.contentWindow.document;
var hideCheckMarkIfNeeded = () => {
getWallpaperPickerInfo(
(useNewWallpaperPicker, highResolutionSuffix) => {
// Do not hide the check mark on the new picker.
if (!useNewWallpaperPicker &&
wpDocument.querySelector('.check')) {
wpDocument.querySelector('.check').style.visibility =
'hidden';
}
});
};
if (!!appName) {
chrome.wallpaperPrivate.getStrings(function(strings) {
......@@ -493,7 +432,6 @@ chrome.storage.onChanged.addListener(function(changes, namespace) {
wpDocument.querySelector('#wallpaper-set-by-message').textContent =
message;
wpDocument.querySelector('#wallpaper-grid').classList.add('small');
hideCheckMarkIfNeeded();
wpDocument.querySelector('#checkbox').classList.remove('checked');
wpDocument.querySelector('#categories-list').disabled = false;
wpDocument.querySelector('#wallpaper-grid').disabled = false;
......@@ -511,7 +449,6 @@ chrome.storage.onChanged.addListener(function(changes, namespace) {
if (enable) {
wpDocument.querySelector('#checkbox')
.classList.add('checked');
hideCheckMarkIfNeeded();
} else {
wpDocument.querySelector('#checkbox')
.classList.remove('checked');
......@@ -519,15 +456,6 @@ chrome.storage.onChanged.addListener(function(changes, namespace) {
wpDocument.querySelector('.check').style.visibility =
'visible';
}
getWallpaperPickerInfo(
(useNewWallpaperPicker, highResolutionSuffix) => {
if (!useNewWallpaperPicker) {
wpDocument.querySelector('#wallpaper-grid').disabled =
enable;
wpDocument.querySelector('#categories-list').disabled =
enable;
}
});
});
}
};
......@@ -665,13 +593,6 @@ chrome.wallpaperPrivate.onWallpaperChangedBy3rdParty.addListener(function(
WallpaperUtil.saveWallpaperInfo(
fileName, layout, Constants.WallpaperSourceEnum.ThirdParty, appName);
getWallpaperPickerInfo((useNewWallpaperPicker, highResolutionSuffix) => {
// Surprise me/daily refresh should be auto-disabled if wallpaper is changed
// by third-party apps.
if (!useNewWallpaperPicker) {
SurpriseWallpaper.getInstance().disable();
return;
}
WallpaperUtil.saveDailyRefreshInfo(
{enabled: false, collectionId: null, resumeToken: null});
......@@ -681,5 +602,4 @@ chrome.wallpaperPrivate.onWallpaperChangedBy3rdParty.addListener(function(
{detail: {wallpaperFileName: fileName}});
wallpaperPickerWindow.contentWindow.dispatchEvent(event);
}
});
});
......@@ -391,15 +391,6 @@ WallpaperUtil.setOnlineWallpaperWithoutPreview = function(
});
};
/**
* Gets the suffix to append to the base url of an online wallpaper. The
* requested thumbnail is displayed in the wallpaper picker grid.
*/
WallpaperUtil.getOnlineWallpaperThumbnailSuffix = function() {
return loadTimeData.getBoolean('useNewWallpaperPicker') ? '' :
'_thumbnail.png';
};
/**
* Creates a blob of type 'image/png'.
* @param {string} data The image data.
......@@ -427,35 +418,22 @@ WallpaperUtil.displayImage = function(imageElement, data, opt_callback) {
};
/**
* Sets the value of the surprise me checkbox (or the daily refresh toggle on
* the new picker).
* Sets the value of the daily refresh toggle.
* @param {boolean} checked The value used to set the checkbox.
*/
WallpaperUtil.setSurpriseMeCheckboxValue = function(checked) {
if (loadTimeData.getBoolean('useNewWallpaperPicker')) {
document.querySelectorAll('.daily-refresh-slider').forEach(element => {
element.classList.toggle('checked', checked);
});
} else {
$('surprise-me')
.querySelector('#checkbox')
.classList.toggle('checked', checked);
}
};
/**
* Gets the value of the surprise me checkbox (or the value of the daily refresh
* toggle on the new picker).
* Gets the state of the daily refresh toggle.
* @return {boolean} The value of the checkbox.
*/
WallpaperUtil.getSurpriseMeCheckboxValue = function() {
if (loadTimeData.getBoolean('useNewWallpaperPicker')) {
return document.querySelector('.daily-refresh-slider')
.classList.contains('checked');
}
return $('surprise-me')
.querySelector('#checkbox')
.classList.contains('checked');
};
/**
......@@ -475,8 +453,7 @@ WallpaperUtil.displayThumbnail = function(imageElement, url, source) {
// operation within |WallpaperThumbnailsGridItem.decorate| hasn't
// completed. See http://crbug.com/792829.
var xhr = new XMLHttpRequest();
xhr.open(
'GET', url + WallpaperUtil.getOnlineWallpaperThumbnailSuffix(), true);
xhr.open('GET', url, true);
xhr.responseType = 'arraybuffer';
xhr.send(null);
xhr.addEventListener('load', function(e) {
......
......@@ -70,11 +70,9 @@ cr.define('wallpapers', function() {
var div = self.ownerDocument.createElement('div');
div.textContent = entry;
li.appendChild(div);
if (loadTimeData.getBoolean('useNewWallpaperPicker')) {
li.addEventListener('mousedown', e => {
e.preventDefault();
});
}
return li;
};
},
......
......@@ -86,9 +86,7 @@ cr.define('wallpapers', function() {
}
var imageEl = cr.doc.createElement('img');
if (loadTimeData.getBoolean('useNewWallpaperPicker')) {
// On the new picker, do not show the image until |cropImageToFitGrid_|
// is done.
// Do not show the image until |cropImageToFitGrid_| is done.
imageEl.style.visibility = 'hidden';
imageEl.setAttribute('aria-hidden', 'true');
this.setAttribute('aria-label', this.dataItem.ariaLabel);
......@@ -100,7 +98,6 @@ cr.define('wallpapers', function() {
this.addEventListener('mousedown', e => {
e.preventDefault();
});
}
imageEl.classList.add('thumbnail');
cr.defineProperty(imageEl, 'offline', cr.PropertyKind.BOOL_ATTR);
......@@ -119,10 +116,7 @@ cr.define('wallpapers', function() {
window.setTimeout(this.callback_.bind(this, this.dataModelId_), 0);
break;
case Constants.WallpaperSourceEnum.Custom:
if (loadTimeData.getBoolean('useNewWallpaperPicker'))
this.decorateCustomWallpaper_(imageEl, this.dataItem);
else
this.decorateCustomWallpaperForOldPicker_(imageEl, this.dataItem);
break;
case Constants.WallpaperSourceEnum.OEM:
case Constants.WallpaperSourceEnum.Online:
......@@ -141,8 +135,7 @@ cr.define('wallpapers', function() {
},
/**
* Initializes the grid item for custom wallpapers. Used by the new
* wallpaper picker.
* Initializes the grid item for custom wallpapers.
* @param {Object} imageElement The image element.
* @param {{filePath: string, baseURL: string, layout: string,
* source: string, availableOffline: boolean}
......@@ -181,45 +174,6 @@ cr.define('wallpapers', function() {
});
},
/**
* Initializes the grid item for custom wallpapers. Used by the old
* wallpaper picker (to be deprecated).
* @param {Object} imageElement The image element.
* @param {{filePath: string, baseURL: string, layout: string,
* source: string, availableOffline: boolean}
* dataItem The info related to the wallpaper image.
* @private
*/
decorateCustomWallpaperForOldPicker_(imageElement, dataItem) {
if (dataItem.source != Constants.WallpaperSourceEnum.Custom) {
console.error(
'|decorateCustomWallpaperForOldPicker_| is called but the ' +
'wallpaper source is not custom.');
return;
}
var errorHandler = e => {
console.error('Can not access file system.');
this.callback_(this.dataModelId_);
};
var setURL = fileEntry => {
imageElement.src = fileEntry.toURL();
this.callback_(this.dataModelId_, dataItem.wallpaperId, imageElement);
};
var wallpaperDirectories = WallpaperDirectories.getInstance();
var fallback = () => {
wallpaperDirectories.getDirectory(
Constants.WallpaperDirNameEnum.ORIGINAL, function(dirEntry) {
dirEntry.getFile(
dataItem.baseURL, {create: false}, setURL, errorHandler);
}, errorHandler);
};
var success = dirEntry => {
dirEntry.getFile(dataItem.baseURL, {create: false}, setURL, fallback);
};
wallpaperDirectories.getDirectory(
Constants.WallpaperDirNameEnum.THUMBNAIL, success, errorHandler);
},
/**
* Initializes the grid item for online or OEM wallpapers.
* @param {Object} imageElement The image element.
......@@ -248,11 +202,7 @@ cr.define('wallpapers', function() {
} else if (
dataItem.source == Constants.WallpaperSourceEnum.Online) {
var xhr = new XMLHttpRequest();
xhr.open(
'GET',
dataItem.baseURL +
WallpaperUtil.getOnlineWallpaperThumbnailSuffix(),
true);
xhr.open('GET', dataItem.baseURL, true);
xhr.responseType = 'arraybuffer';
xhr.send(null);
xhr.addEventListener('load', e => {
......@@ -443,22 +393,9 @@ cr.define('wallpapers', function() {
// item is constructed in function itemConstructor below.
this.pendingItems_ = 0;
// Only show the spinner on the old wallpaper picker.
if (!this.useNewWallpaperPicker_) {
this.style.visibility = 'hidden';
// If spinner is hidden, schedule to show the spinner after
// ShowSpinnerDelayMs delay. Otherwise, keep it spinning.
if ($('spinner-container').hidden) {
this.spinnerTimeout_ = window.setTimeout(function() {
$('spinner-container').hidden = false;
}, ShowSpinnerDelayMs);
}
}
// Add a daily refresh item as the first element of the grid when
// showing online wallpapers on the new wallpaper picker.
if (this.useNewWallpaperPicker_ &&
dataModel.item(0).source == Constants.WallpaperSourceEnum.Online) {
// showing online wallpapers.
if (dataModel.item(0).source == Constants.WallpaperSourceEnum.Online) {
dataModel.splice(
0, 0, {isDailyRefreshItem: true, availableOffline: false});
}
......@@ -542,10 +479,9 @@ cr.define('wallpapers', function() {
if (opt_wallpaperId && opt_thumbnail)
this.thumbnailList_[opt_wallpaperId] = opt_thumbnail;
if (opt_thumbnail && this.useNewWallpaperPicker_)
if (opt_thumbnail)
this.cropImageToFitGrid_(opt_thumbnail);
// Daily refresh item only exists in new wallpaper picker.
if (this.isShowingDailyRefresh) {
var dailyRefreshItemReady = this.dailyRefreshItem &&
this.dailyRefreshImages.length >= DAILY_REFRESH_IMAGES_NUM;
......@@ -592,11 +528,9 @@ cr.define('wallpapers', function() {
window.clearTimeout(this.dailyRefreshTimer_);
this.showNextImage_(0);
}
if (this.useNewWallpaperPicker_ &&
this.classList.contains('image-picker-offline')) {
if (this.classList.contains('image-picker-offline'))
this.highlightOfflineWallpapers();
}
}
},
/** @override */
......@@ -612,8 +546,6 @@ cr.define('wallpapers', function() {
this.checkmark_.tabIndex = 0;
this.dataModel = new ArrayDataModel([]);
this.thumbnailList_ = new ArrayDataModel([]);
this.useNewWallpaperPicker_ =
loadTimeData.getBoolean('useNewWallpaperPicker');
var self = this;
this.itemConstructor = function(value) {
var dataModelId = self.dataModelId_;
......@@ -768,10 +700,8 @@ cr.define('wallpapers', function() {
* the other wallpapers.
*/
highlightOfflineWallpapers: function() {
if (!this.useNewWallpaperPicker_ ||
!this.classList.contains('image-picker-offline')) {
if (!this.classList.contains('image-picker-offline'))
return;
}
chrome.wallpaperPrivate.getOfflineWallpaperList(list => {
var offlineWallpaperList = {};
......@@ -802,7 +732,6 @@ cr.define('wallpapers', function() {
// The active thumbnail maybe deleted in the above redraw(). Sets it again
// to make sure checkmark shows correctly.
this.updateActiveThumb_();
if (this.useNewWallpaperPicker_) {
if (this.dataModel) {
var scrollUp =
this.cachedScrollTop_ && this.cachedScrollTop_ > this.scrollTop;
......@@ -819,7 +748,6 @@ cr.define('wallpapers', function() {
wallpaperManager.onScrollPositionChanged(this.scrollTop);
this.cachedScrollTop_ = this.scrollTop;
}
}
};
return {WallpaperThumbnailsGrid: WallpaperThumbnailsGrid};
......
......@@ -5,7 +5,6 @@
var TestConstants = {
isPowerwashed: 0,
isUsingNewWallpaperPicker: false,
wallpaperUrl: 'https://test.com/test.jpg',
highResolutionSuffix: 'suffix',
// A dummy string which is used to mock an image.
......@@ -269,7 +268,6 @@ var chrome = {
wallpaperPrivate: {
getStrings: function(callback) {
callback({
useNewWallpaperPicker: TestConstants.isUsingNewWallpaperPicker,
highResolutionSuffix: TestConstants.highResolutionSuffix
});
},
......@@ -292,12 +290,7 @@ var chrome = {
{imageUrl: TestConstants.wallpaperUrl}, null /*nextResumeToken=*/);
}
},
runtime: {lastError: null},
commandLinePrivate: {
hasSwitch: function(arg, callback) {
callback(TestConstants.isUsingNewWallpaperPicker);
}
}
runtime: {lastError: null}
};
(function (exports) {
......
......@@ -123,12 +123,11 @@ function testSyncOnlineWallpaper() {
// Test the surprise wallpaper's UMA stats is recorded correctly.
function testSurpriseWallpaper() {
TestConstants.isUsingNewWallpaperPicker = false;
var mockSetWallpaperIfExists = mockController.createFunctionMock(
chrome.wallpaperPrivate, 'setWallpaperIfExists');
mockSetWallpaperIfExists.addExpectation(
TestConstants.wallpaperUrl + TestConstants.highResolutionSuffix, 'dummy',
false /*previewMode=*/);
TestConstants.wallpaperUrl + TestConstants.highResolutionSuffix,
'CENTER_CROPPED', false /*previewMode=*/);
mockSetWallpaperIfExists.callbackData = [true];
var mockRecordWallpaperUMA = mockController.createFunctionMock(
......@@ -137,15 +136,4 @@ function testSurpriseWallpaper() {
var dateString = new Date().toDateString();
SurpriseWallpaper.getInstance().setRandomWallpaper_(dateString);
// Repeat the above process for the new wallpaper picker. Test that the UMA
// stats is also recorded correctly.
TestConstants.isUsingNewWallpaperPicker = true;
mockSetWallpaperIfExists.addExpectation(
TestConstants.wallpaperUrl + TestConstants.highResolutionSuffix,
'CENTER_CROPPED', false /*previewMode=*/);
mockRecordWallpaperUMA.addExpectation(Constants.WallpaperSourceEnum.Daily);
var dateString = new Date().toDateString();
SurpriseWallpaper.getInstance().setRandomWallpaper_(dateString);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment