Skip to content

Commit

Permalink
personalization: Respect Google Photos enablement state.
Browse files Browse the repository at this point in the history
If the user is not allowed to access Google Photos due to enterprise
policy restrictions, no additional requests to the Google Photos server
should be made. Later CLs will add specific UI for this case.

Bug: b:225243368
Change-Id: I102e419d97f356a6fd41216347cec1d63c179d5d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3534541
Reviewed-by: Jeffrey Young <cowmoo@chromium.org>
Commit-Queue: David Black <dmblack@google.com>
Cr-Commit-Position: refs/heads/main@{#983435}
  • Loading branch information
David Black authored and Chromium LUCI CQ committed Mar 21, 2022
1 parent 579df5d commit 87bd824
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 15 deletions.
Expand Up @@ -6,7 +6,7 @@ import {assert} from 'chrome://resources/js/assert.m.js';
import {Action} from 'chrome://resources/js/cr/ui/store.js';
import {FilePath} from 'chrome://resources/mojo/mojo/public/mojom/base/file_path.mojom-webui.js';

import {CurrentWallpaper, GooglePhotosAlbum, GooglePhotosPhoto, WallpaperCollection, WallpaperImage} from '../personalization_app.mojom-webui.js';
import {CurrentWallpaper, GooglePhotosAlbum, GooglePhotosEnablementState, GooglePhotosPhoto, WallpaperCollection, WallpaperImage} from '../personalization_app.mojom-webui.js';
import {DisplayableImage} from '../personalization_reducers.js';

/**
Expand All @@ -19,6 +19,7 @@ export enum WallpaperActionName {
BEGIN_LOAD_GOOGLE_PHOTOS_ALBUM = 'begin_load_google_photos_album',
BEGIN_LOAD_GOOGLE_PHOTOS_ALBUMS = 'begin_load_google_photos_albums',
BEGIN_LOAD_GOOGLE_PHOTOS_COUNT = 'begin_load_google_photos_count',
BEGIN_LOAD_GOOGLE_PHOTOS_ENABLED = 'begin_load_google_photos_enabled',
BEGIN_LOAD_GOOGLE_PHOTOS_PHOTOS = 'begin_load_google_photos_photos',
BEGIN_LOAD_IMAGES_FOR_COLLECTIONS = 'begin_load_images_for_collections',
BEGIN_LOAD_LOCAL_IMAGES = 'begin_load_local_images',
Expand All @@ -31,6 +32,7 @@ export enum WallpaperActionName {
SET_DAILY_REFRESH_COLLECTION_ID = 'set_daily_refresh_collection_id',
SET_GOOGLE_PHOTOS_ALBUM = 'set_google_photos_album',
SET_GOOGLE_PHOTOS_COUNT = 'set_google_photos_count',
SET_GOOGLE_PHOTOS_ENABLED = 'set_google_photos_enabled',
SET_IMAGES_FOR_COLLECTION = 'set_images_for_collection',
SET_LOCAL_IMAGES = 'set_local_images',
SET_LOCAL_IMAGE_DATA = 'set_local_image_data',
Expand All @@ -42,12 +44,13 @@ export enum WallpaperActionName {
export type WallpaperActions =
AppendGooglePhotosAlbumsAction|AppendGooglePhotosPhotosAction|
BeginLoadGooglePhotosAlbumAction|BeginLoadGooglePhotosAlbumsAction|
BeginLoadGooglePhotosCountAction|BeginLoadGooglePhotosPhotosAction|
BeginLoadImagesForCollectionsAction|BeginLoadLocalImagesAction|
BeginLoadLocalImageDataAction|BeginUpdateDailyRefreshImageAction|
BeginLoadSelectedImageAction|BeginSelectImageAction|EndSelectImageAction|
SetCollectionsAction|SetDailyRefreshCollectionIdAction|
SetGooglePhotosAlbumAction|SetGooglePhotosCountAction|
BeginLoadGooglePhotosCountAction|BeginLoadGooglePhotosEnabledAction|
BeginLoadGooglePhotosPhotosAction|BeginLoadImagesForCollectionsAction|
BeginLoadLocalImagesAction|BeginLoadLocalImageDataAction|
BeginUpdateDailyRefreshImageAction|BeginLoadSelectedImageAction|
BeginSelectImageAction|EndSelectImageAction|SetCollectionsAction|
SetDailyRefreshCollectionIdAction|SetGooglePhotosAlbumAction|
SetGooglePhotosCountAction|SetGooglePhotosEnabledAction|
SetImagesForCollectionAction|SetLocalImageDataAction|SetLocalImagesAction|
SetUpdatedDailyRefreshImageAction|SetSelectedImageAction|
SetFullscreenEnabledAction;
Expand Down Expand Up @@ -130,9 +133,23 @@ export function beginLoadGooglePhotosCountAction():
return {name: WallpaperActionName.BEGIN_LOAD_GOOGLE_PHOTOS_COUNT};
}

export type BeginLoadGooglePhotosEnabledAction = Action&{
name: WallpaperActionName.BEGIN_LOAD_GOOGLE_PHOTOS_ENABLED;
};

/**
* Notify that the app is loading whether the user is allowed to access Google
* Photos.
*/
export function beginLoadGooglePhotosEnabledAction():
BeginLoadGooglePhotosEnabledAction {
return {name: WallpaperActionName.BEGIN_LOAD_GOOGLE_PHOTOS_ENABLED};
}

export type BeginLoadGooglePhotosPhotosAction = Action&{
name: WallpaperActionName.BEGIN_LOAD_GOOGLE_PHOTOS_PHOTOS;
};

/**
* Notify that the app is loading the list of Google Photos photos.
*/
Expand Down Expand Up @@ -160,6 +177,7 @@ export function beginLoadImagesForCollectionsAction(
export type BeginLoadLocalImagesAction = Action&{
name: WallpaperActionName.BEGIN_LOAD_LOCAL_IMAGES;
};

/**
* Notify that app is loading local image list.
*/
Expand Down Expand Up @@ -294,6 +312,17 @@ export function setGooglePhotosCountAction(count: number|
return {count, name: WallpaperActionName.SET_GOOGLE_PHOTOS_COUNT};
}

export type SetGooglePhotosEnabledAction = Action&{
name: WallpaperActionName.SET_GOOGLE_PHOTOS_ENABLED;
enabled: GooglePhotosEnablementState;
};

/** Sets whether the user is allowed to access Google Photos. */
export function setGooglePhotosEnabledAction(
enabled: GooglePhotosEnablementState): SetGooglePhotosEnabledAction {
return {enabled, name: WallpaperActionName.SET_GOOGLE_PHOTOS_ENABLED};
}

export type SetImagesForCollectionAction = Action&{
name: WallpaperActionName.SET_IMAGES_FOR_COLLECTION;
collectionId: string;
Expand Down
Expand Up @@ -7,7 +7,7 @@ import {loadTimeData} from 'chrome://resources/js/load_time_data.m.js';
import {FilePath} from 'chrome://resources/mojo/mojo/public/mojom/base/file_path.mojom-webui.js';

import {isNonEmptyArray} from '../../common/utils.js';
import {FetchGooglePhotosAlbumsResponse, FetchGooglePhotosPhotosResponse, GooglePhotosAlbum, GooglePhotosPhoto, WallpaperCollection, WallpaperImage, WallpaperLayout, WallpaperProviderInterface, WallpaperType} from '../personalization_app.mojom-webui.js';
import {FetchGooglePhotosAlbumsResponse, FetchGooglePhotosPhotosResponse, GooglePhotosAlbum, GooglePhotosEnablementState, GooglePhotosPhoto, WallpaperCollection, WallpaperImage, WallpaperLayout, WallpaperProviderInterface, WallpaperType} from '../personalization_app.mojom-webui.js';
import {PersonalizationStore} from '../personalization_store.js';
import {appendMaxResolutionSuffix, isFilePath, isGooglePhotosPhoto, isWallpaperImage} from '../utils.js';

Expand Down Expand Up @@ -128,6 +128,18 @@ export async function fetchGooglePhotosAlbums(
store.dispatch(action.appendGooglePhotosAlbumsAction(albums, resumeToken));
}

/** Fetches whether the user is allowed to access Google Photos. */
async function fetchGooglePhotosEnabled(
provider: WallpaperProviderInterface,
store: PersonalizationStore): Promise<void> {
store.dispatch(action.beginLoadGooglePhotosEnabledAction());
const {state} = await provider.fetchGooglePhotosEnabled();
if (state === GooglePhotosEnablementState.kError) {
console.warn('Failed to fetch Google Photos enabled');
}
store.dispatch(action.setGooglePhotosEnabledAction(state));
}

/** Fetches the count of Google Photos photos and saves it to the store. */
async function fetchGooglePhotosCount(
provider: WallpaperProviderInterface,
Expand Down Expand Up @@ -348,7 +360,19 @@ export async function initializeBackdropData(
export async function initializeGooglePhotosData(
provider: WallpaperProviderInterface,
store: PersonalizationStore): Promise<void> {
await fetchGooglePhotosCount(provider, store);
// Fetch whether the user is allowed to access Google Photos.
await fetchGooglePhotosEnabled(provider, store);

// Only proceed to fetch Google Photos data if the user is allowed.
const enabled = store.data.wallpaper.googlePhotos.enabled;
if (enabled === GooglePhotosEnablementState.kEnabled) {
await fetchGooglePhotosCount(provider, store);
} else {
store.beginBatchUpdate();
store.dispatch(action.beginLoadGooglePhotosCountAction());
store.dispatch(action.setGooglePhotosCountAction(null));
store.endBatchUpdate();
}

// If the count of Google Photos photos is zero or null, it's not necesssary
// to query the server for the list of albums/photos.
Expand Down
Expand Up @@ -183,6 +183,24 @@ function loadingReducer(
count: false,
},
};
case WallpaperActionName.BEGIN_LOAD_GOOGLE_PHOTOS_ENABLED:
assert(state.googlePhotos.enabled === false);
return {
...state,
googlePhotos: {
...state.googlePhotos,
enabled: true,
},
};
case WallpaperActionName.SET_GOOGLE_PHOTOS_ENABLED:
assert(state.googlePhotos.enabled === true);
return {
...state,
googlePhotos: {
...state.googlePhotos,
enabled: false,
},
};
case WallpaperActionName.BEGIN_LOAD_GOOGLE_PHOTOS_PHOTOS:
assert(state.googlePhotos.photos === false);
return {
Expand Down Expand Up @@ -388,6 +406,17 @@ function googlePhotosReducer(
...state,
count: action.count,
};
case WallpaperActionName.BEGIN_LOAD_GOOGLE_PHOTOS_ENABLED:
// Whether the user is allowed to access Google Photos should be loaded
// only once.
assert(state.enabled === undefined);
return state;
case WallpaperActionName.SET_GOOGLE_PHOTOS_ENABLED:
assert(action.enabled !== undefined);
return {
...state,
enabled: action.enabled,
};
case WallpaperActionName.BEGIN_LOAD_GOOGLE_PHOTOS_PHOTOS:
// The list of photos should be loaded only while additional photos exist.
assert(state.photos === undefined || state.resumeTokens.photos);
Expand Down
Expand Up @@ -3,7 +3,7 @@
// found in the LICENSE file.
import {FilePath} from 'chrome://resources/mojo/mojo/public/mojom/base/file_path.mojom-webui.js';

import {CurrentWallpaper, GooglePhotosAlbum, GooglePhotosPhoto, WallpaperCollection, WallpaperImage} from '../personalization_app.mojom-webui.js';
import {CurrentWallpaper, GooglePhotosAlbum, GooglePhotosEnablementState, GooglePhotosPhoto, WallpaperCollection, WallpaperImage} from '../personalization_app.mojom-webui.js';

/**
* Stores collections and images from backdrop server.
Expand All @@ -16,6 +16,8 @@ export interface BackdropState {

/**
* Stores Google Photos state.
* |enabled| is whether the user is allowed to access Google Photos. It is
* undefined only until it has been initialized.
* |count| is the count of Google Photos photos. It is undefined only until it
* has been initialized, then either null (in error state) or a valid integer.
* |albums| is the list of Google Photos albums. It is undefined only until it
Expand All @@ -27,6 +29,7 @@ export interface BackdropState {
* initialized, then either null (in error state) or a valid Array.
*/
export interface GooglePhotosState {
enabled: GooglePhotosEnablementState|undefined;
count: number|null|undefined;
albums: GooglePhotosAlbum[]|null|undefined;
photos: GooglePhotosPhoto[]|null|undefined;
Expand Down Expand Up @@ -60,7 +63,7 @@ export interface LoadingState {
selected: boolean;
setImage: number;
googlePhotos: {
count: boolean; albums: boolean; photos: boolean;
enabled: boolean; count: boolean; albums: boolean; photos: boolean;
photosByAlbumId: Record<string, boolean>;
};
}
Expand Down Expand Up @@ -100,6 +103,7 @@ export function emptyState(): WallpaperState {
selected: false,
setImage: 0,
googlePhotos: {
enabled: false,
count: false,
albums: false,
photos: false,
Expand All @@ -112,6 +116,7 @@ export function emptyState(): WallpaperState {
dailyRefresh: {collectionId: null},
fullscreen: false,
googlePhotos: {
enabled: undefined,
count: undefined,
albums: undefined,
photos: undefined,
Expand Down

0 comments on commit 87bd824

Please sign in to comment.