Skip to content

Commit

Permalink
[ntp][modules][photos] Add support for dismiss and disable Photos module
Browse files Browse the repository at this point in the history
Strings from go/photos-ntp-strings (menu: screenshot/9zatnfM2mRXgjUG, dismissed toast: screenshot/AzgMrUARCKwzstT, disabled toast: screenshot/3wLvqGX4khCz2d9)

Bug: 1230867
Change-Id: Id00e1adbeb3cbde49c37079b66fffde2d8a487b5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3163429
Commit-Queue: Jeremy Selier <[email protected]>
Reviewed-by: Scott Violet <[email protected]>
Reviewed-by: Tibor Goldschwendt <[email protected]>
Reviewed-by: Greg Kerr <[email protected]>
Cr-Commit-Position: refs/heads/main@{#922411}
  • Loading branch information
jeremys authored and Chromium LUCI CQ committed Sep 17, 2021
1 parent b0278be commit 730b56d
Show file tree
Hide file tree
Showing 18 changed files with 213 additions and 9 deletions.
12 changes: 12 additions & 0 deletions chrome/app/generated_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -5960,6 +5960,18 @@ Keep your key file in a safe place. You will need it to create new versions of y
<message name="IDS_NTP_MODULES_PHOTOS_TITLE" desc="Title shown in the header of the photos module and various other UIs.">
Google Photos
</message>
<message name="IDS_NTP_MODULES_PHOTOS_MEMORIES_HIDE_TODAY" desc="Label used to hide Google Photos Memories from the new tab page for today.">
Hide memories for today
</message>
<message name="IDS_NTP_MODULES_PHOTOS_MEMORIES_HIDDEN_TODAY" desc="Confirmation text shown when Google Photos Memories were hidden from the new tab page for today.">
Memories hidden for today
</message>
<message name="IDS_NTP_MODULES_PHOTOS_MEMORIES_DISABLE" desc="Label used to disable Google Photos Memories from the new tab page.">
Never show memories
</message>
<message name="IDS_NTP_MODULES_PHOTOS_MEMORIES_DISABLED" desc="Confirmation text shown when the Google Photos Memories module is disabled from the new tab page.">
All memories hidden
</message>
<message name="IDS_NTP_MODULES_PHOTOS_INFO" desc="Text shown in the body of the info dialog of the Photos module.">
Your Memories from Google Photos are shown here. They are visible only when you’re signed in.
<ph name="BREAK">&lt;br&gt;</ph>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
9533441bac054532772d42e0fe29168272f13ecc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
d2edb67587ca46b7dfd5cfdbae81e0aa36eb7b18
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a826a30010c766cae45f25ac025236b811debf86
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
9533441bac054532772d42e0fe29168272f13ecc
4 changes: 4 additions & 0 deletions chrome/browser/new_tab_page/modules/photos/photos.mojom
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ struct Memory {
interface PhotosHandler {
// Fetches memories from Google Photos API.
GetMemories() => (array<Memory> memories);
// Dismissed module for fixed amount of time.
DismissModule();
// Restores the module immediately.
RestoreModule();
};
8 changes: 8 additions & 0 deletions chrome/browser/new_tab_page/modules/photos/photos_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,11 @@ void PhotosHandler::GetMemories(GetMemoriesCallback callback) {
PhotosServiceFactory::GetForProfile(profile_)->GetMemories(
std::move(callback));
}

void PhotosHandler::DismissModule() {
PhotosServiceFactory::GetForProfile(profile_)->DismissModule();
}

void PhotosHandler::RestoreModule() {
PhotosServiceFactory::GetForProfile(profile_)->RestoreModule();
}
2 changes: 2 additions & 0 deletions chrome/browser/new_tab_page/modules/photos/photos_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class PhotosHandler : public photos::mojom::PhotosHandler {

// photos::mojom::PhotosHandler:
void GetMemories(GetMemoriesCallback callback) override;
void DismissModule() override;
void RestoreModule() override;

private:
mojo::Receiver<photos::mojom::PhotosHandler> handler_;
Expand Down
40 changes: 38 additions & 2 deletions chrome/browser/new_tab_page/modules/photos/photos_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <memory>
#include <utility>

#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/signin/public/identity_manager/primary_account_access_token_fetcher.h"
#include "components/signin/public/identity_manager/scope_set.h"
Expand Down Expand Up @@ -62,13 +64,28 @@ constexpr net::NetworkTrafficAnnotationTag kTrafficAnnotation =
})");
} // namespace

// static
const char PhotosService::kLastDismissedTimePrefName[] =
"NewTabPage.Photos.LastDimissedTime";

// static
const base::TimeDelta PhotosService::kDismissDuration =
base::TimeDelta::FromDays(1);

PhotosService::~PhotosService() = default;

PhotosService::PhotosService(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
signin::IdentityManager* identity_manager)
signin::IdentityManager* identity_manager,
PrefService* pref_service)
: url_loader_factory_(std::move(url_loader_factory)),
identity_manager_(identity_manager) {}
identity_manager_(identity_manager),
pref_service_(pref_service) {}

// static
void PhotosService::RegisterProfilePrefs(PrefRegistrySimple* registry) {
registry->RegisterTimePref(kLastDismissedTimePrefName, base::Time());
}

void PhotosService::GetMemories(GetMemoriesCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
Expand All @@ -77,6 +94,17 @@ void PhotosService::GetMemories(GetMemoriesCallback callback) {
return;
}

// Bail if module is still dismissed.
if (!pref_service_->GetTime(kLastDismissedTimePrefName).is_null() &&
base::Time::Now() - pref_service_->GetTime(kLastDismissedTimePrefName) <
kDismissDuration) {
for (auto& callback : callbacks_) {
std::move(callback).Run(std::vector<photos::mojom::MemoryPtr>());
}
callbacks_.clear();
return;
}

signin::ScopeSet scopes;
scopes.insert(kPhotosScope);
scopes.insert(kPhotosImgScope);
Expand All @@ -88,6 +116,14 @@ void PhotosService::GetMemories(GetMemoriesCallback callback) {
signin::ConsentLevel::kSync);
}

void PhotosService::DismissModule() {
pref_service_->SetTime(kLastDismissedTimePrefName, base::Time::Now());
}

void PhotosService::RestoreModule() {
pref_service_->SetTime(kLastDismissedTimePrefName, base::Time());
}

void PhotosService::OnTokenReceived(GoogleServiceAuthError error,
signin::AccessTokenInfo token_info) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
Expand Down
16 changes: 15 additions & 1 deletion chrome/browser/new_tab_page/modules/photos/photos_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/simple_url_loader.h"

class PrefRegistrySimple;
class PrefService;

namespace signin {
class IdentityManager;
class PrimaryAccountAccessTokenFetcher;
Expand All @@ -25,15 +28,25 @@ class PrimaryAccountAccessTokenFetcher;
// Handles requests for user Google Photos data.
class PhotosService : public KeyedService {
public:
static const char kLastDismissedTimePrefName[];
static const base::TimeDelta kDismissDuration;

PhotosService(const PhotosService&) = delete;
PhotosService(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
signin::IdentityManager* identity_manager);
signin::IdentityManager* identity_manager,
PrefService* pref_service);
~PhotosService() override;

static void RegisterProfilePrefs(PrefRegistrySimple* registry);

using GetMemoriesCallback = photos::mojom::PhotosHandler::GetMemoriesCallback;
// Retrieves Google Photos memories from API.
void GetMemories(GetMemoriesCallback callback);
// Makes the service not return data for a specified amount of time.
void DismissModule();
// Makes the service return data again even if dimiss time is not yet over.
void RestoreModule();

private:
void OnTokenReceived(GoogleServiceAuthError error,
Expand All @@ -50,6 +63,7 @@ class PhotosService : public KeyedService {
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
std::vector<GetMemoriesCallback> callbacks_;
signin::IdentityManager* identity_manager_;
PrefService* pref_service_;
SEQUENCE_CHECKER(sequence_checker_);
base::WeakPtrFactory<PhotosService> weak_factory_{this};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ KeyedService* PhotosServiceFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
auto url_loader_factory = context->GetDefaultStoragePartition()
->GetURLLoaderFactoryForBrowserProcess();

auto* profile = Profile::FromBrowserContext(context);
return new PhotosService(url_loader_factory,
IdentityManagerFactory::GetForProfile(
Profile::FromBrowserContext(context)));
IdentityManagerFactory::GetForProfile(profile),
profile->GetPrefs());
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "base/test/mock_callback.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/test/base/testing_profile.h"
#include "components/prefs/testing_pref_service.h"
#include "components/search/ntp_features.h"
#include "components/signin/public/identity_manager/identity_test_environment.h"
#include "content/public/test/browser_task_environment.h"
Expand All @@ -27,9 +28,10 @@ class PhotosServiceTest : public testing::Test {
service_ = std::make_unique<PhotosService>(
base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
&test_url_loader_factory_),
identity_test_env.identity_manager());
identity_test_env.identity_manager(), &prefs_);
identity_test_env.MakePrimaryAccountAvailable("[email protected]",
signin::ConsentLevel::kSync);
service_->RegisterProfilePrefs(prefs_.registry());
}

void TearDown() override {
Expand All @@ -43,6 +45,7 @@ class PhotosServiceTest : public testing::Test {
std::unique_ptr<PhotosService> service_;
data_decoder::test::InProcessDataDecoder in_process_data_decoder_;
signin::IdentityTestEnvironment identity_test_env;
TestingPrefServiceSimple prefs_;
};

TEST_F(PhotosServiceTest, PassesDataOnSuccess) {
Expand All @@ -56,6 +59,9 @@ TEST_F(PhotosServiceTest, PassesDataOnSuccess) {
actual_memories = std::move(memories);
}));

// Make sure we are not in the dismissed time window by default.
prefs_.SetTime(PhotosService::kLastDismissedTimePrefName, base::Time::Now());
task_environment_.AdvanceClock(PhotosService::kDismissDuration);
service_->GetMemories(callback.Get());

identity_test_env.WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
Expand Down Expand Up @@ -294,3 +300,32 @@ TEST_F(PhotosServiceTest, PassesNoDataOnMissingItemKey) {

EXPECT_TRUE(actual_memories.empty());
}

TEST_F(PhotosServiceTest, PassesNoDataIfDismissed) {
bool empty_response = false;
base::MockCallback<PhotosService::GetMemoriesCallback> callback;

EXPECT_CALL(callback, Run(testing::_))
.Times(1)
.WillOnce(testing::Invoke(
[&empty_response](std::vector<photos::mojom::MemoryPtr> memories) {
empty_response = memories.empty();
}));

prefs_.SetTime(PhotosService::kLastDismissedTimePrefName, base::Time::Now());
service_->GetMemories(callback.Get());

EXPECT_TRUE(empty_response);
}

TEST_F(PhotosServiceTest, DismissModule) {
service_->DismissModule();
EXPECT_EQ(base::Time::Now(),
prefs_.GetTime(PhotosService::kLastDismissedTimePrefName));
}

TEST_F(PhotosServiceTest, RestoreModule) {
service_->RestoreModule();
EXPECT_EQ(base::Time(),
prefs_.GetTime(PhotosService::kLastDismissedTimePrefName));
}
2 changes: 2 additions & 0 deletions chrome/browser/prefs/browser_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@
#include "chrome/browser/metrics/tab_stats/tab_stats_tracker.h"
#include "chrome/browser/nearby_sharing/common/nearby_share_prefs.h"
#include "chrome/browser/new_tab_page/modules/drive/drive_service.h"
#include "chrome/browser/new_tab_page/modules/photos/photos_service.h"
#include "chrome/browser/new_tab_page/modules/task_module/task_module_service.h"
#include "chrome/browser/new_tab_page/promos/promo_service.h"
#include "chrome/browser/search/background/ntp_custom_background_service.h"
Expand Down Expand Up @@ -1257,6 +1258,7 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry,
NewTabPageUI::RegisterProfilePrefs(registry);
NewTabUI::RegisterProfilePrefs(registry);
ntp_tiles::CustomLinksManagerImpl::RegisterProfilePrefs(registry);
PhotosService::RegisterProfilePrefs(registry);
PinnedTabCodec::RegisterProfilePrefs(registry);
PromoService::RegisterProfilePrefs(registry);
settings::SettingsUI::RegisterProfilePrefs(registry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@
}
</style>
<ntp-module-header
show-info-button on-info-button-click="onInfoButtonClick_">
show-info-button on-info-button-click="onInfoButtonClick_"
show-dismiss-button on-dismiss-button-click="onDismissButtonClick_"
dismiss-text="[[i18n('modulesPhotosMemoriesHideToday')]]"
on-disable-button-click="onDisableButtonClick_"
disable-text="[[i18n('modulesPhotosMemoriesDisable')]]">
[[i18n('modulesPhotosTitle')]]
</ntp-module-header>
<div id="memories">
Expand Down
24 changes: 24 additions & 0 deletions chrome/browser/resources/new_tab_page/modules/photos/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,30 @@ class PhotosModuleElement extends mixinBehaviors
/** @type {InfoDialogElement} */ (this.$.infoDialogRender.get())
.showModal();
}

/** @private */
onDismissButtonClick_() {
PhotosProxy.getHandler().dismissModule();
this.dispatchEvent(new CustomEvent('dismiss-module', {
bubbles: true,
composed: true,
detail: {
message: loadTimeData.getString('modulesPhotosMemoriesHiddenToday'),
restoreCallback: () => PhotosProxy.getHandler().restoreModule(),
},
}));
}

/** @private */
onDisableButtonClick_() {
this.dispatchEvent(new CustomEvent('disable-module', {
bubbles: true,
composed: true,
detail: {
message: loadTimeData.getString('modulesPhotosMemoriesDisabled'),
},
}));
}
}

customElements.define(PhotosModuleElement.is, PhotosModuleElement);
Expand Down
7 changes: 7 additions & 0 deletions chrome/browser/ui/webui/new_tab_page/new_tab_page_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,13 @@ content::WebUIDataSource* CreateNewTabPageUiHtmlSource(
{"modulesPhotosInfo", IDS_NTP_MODULES_PHOTOS_INFO},
{"modulesPhotosSentence", IDS_NTP_MODULES_PHOTOS_TITLE},
{"modulesPhotosTitle", IDS_NTP_MODULES_PHOTOS_TITLE},
{"modulesPhotosMemoriesDisable", IDS_NTP_MODULES_PHOTOS_MEMORIES_DISABLE},
{"modulesPhotosMemoriesDisabled",
IDS_NTP_MODULES_PHOTOS_MEMORIES_DISABLED},
{"modulesPhotosMemoriesHideToday",
IDS_NTP_MODULES_PHOTOS_MEMORIES_HIDE_TODAY},
{"modulesPhotosMemoriesHiddenToday",
IDS_NTP_MODULES_PHOTOS_MEMORIES_HIDDEN_TODAY},
{"modulesTasksInfoTitle", IDS_NTP_MODULES_SHOPPING_TASKS_INFO_TITLE},
{"modulesTasksInfoClose", IDS_NTP_MODULES_SHOPPING_TASKS_INFO_CLOSE},
{"modulesCartHeaderNew", IDS_NTP_MODULES_CART_HEADER_CHIP_NEW},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ js_library("module_test") {
"//chrome/test/data/webui:test_browser_proxy",
"//chrome/test/data/webui:test_util",
"//ui/webui/resources/js:assert.m",
"//ui/webui/resources/js:load_time_data.m",
]
externs_list = [ "$externs_path/mocha-2.5.js" ]
}
Loading

0 comments on commit 730b56d

Please sign in to comment.