Commit 3c9c5df2 authored by wutao's avatar wutao Committed by Commit Bot

ambient: Adds checks on account type

This patch adds checks to only allow ambient mode with primary user and
certain email domains. In those cases, we do not show the ambient mode
Settings.

Bug: b/161481420, b/161866797
Test: new AmbientClientImplTest.*
Change-Id: Ibed75fb72a0da1e19e9980a232d63eeb78607540
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2311900Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Reviewed-by: default avatarKyle Horimoto <khorimoto@chromium.org>
Reviewed-by: default avatarXiaohui Chen <xiaohuic@chromium.org>
Commit-Queue: Tao Wu <wutao@chromium.org>
Cr-Commit-Position: refs/heads/master@{#794270}
parent 4decd521
......@@ -25,6 +25,7 @@
#include "ash/system/power/power_status.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/check.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
......@@ -35,6 +36,7 @@
#include "chromeos/dbus/power_manager/idle.pb.h"
#include "chromeos/services/assistant/public/cpp/assistant_service.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "ui/base/ui_base_types.h"
#include "ui/views/widget/widget.h"
#include "ui/wm/core/visibility_controller.h"
......@@ -109,6 +111,16 @@ bool IsInSessionUi(AmbientUiMode mode) {
return mode == AmbientUiMode::kInSessionUi;
}
bool IsAmbientModeEnabled() {
if (!AmbientClient::Get()->IsAmbientModeAllowed())
return false;
ash::SessionControllerImpl* controller = Shell::Get()->session_controller();
PrefService* prefs = controller->GetActivePrefService();
DCHECK(prefs);
return prefs->GetBoolean(ambient::prefs::kAmbientModeEnabled);
}
} // namespace
// AmbientController::InactivityMonitor----------------------------------
......@@ -256,7 +268,7 @@ void AmbientController::OnAutoShowTimeOut() {
}
void AmbientController::OnLockStateChanged(bool locked) {
if (!AmbientClient::Get()->IsAmbientModeAllowed()) {
if (!IsAmbientModeEnabled()) {
VLOG(1) << "Ambient mode is not allowed.";
return;
}
......@@ -339,7 +351,7 @@ void AmbientController::OnPowerStatusChanged() {
void AmbientController::ScreenIdleStateChanged(
const power_manager::ScreenIdleState& idle_state) {
if (!AmbientClient::Get()->IsAmbientModeAllowed())
if (!IsAmbientModeEnabled())
return;
if (!idle_state.dimmed())
......@@ -366,7 +378,9 @@ void AmbientController::RemoveAmbientViewDelegateObserver(
}
void AmbientController::ShowUi(AmbientUiMode mode) {
if (!AmbientClient::Get()->IsAmbientModeAllowed()) {
// TODO(meilinw): move the eligibility check to the idle entry point once
// implemented: b/149246117.
if (!IsAmbientModeEnabled()) {
LOG(WARNING) << "Ambient mode is not allowed.";
return;
}
......
......@@ -15,7 +15,7 @@
namespace ash {
// An implementation for test support.
// IsAmbientModeAllowedForProfile() returns true to run the unittests.
// IsAmbientModeAllowed() returns true to run the unittests.
class ASH_PUBLIC_EXPORT TestAmbientClient : public AmbientClient {
public:
explicit TestAmbientClient(device::TestWakeLockProvider* wake_lock_provider);
......
......@@ -24,6 +24,7 @@
#include "components/user_manager/user.h"
#include "components/user_manager/user_manager.h"
#include "content/public/browser/device_service.h"
#include "google_apis/gaia/gaia_auth_util.h"
#include "google_apis/gaia/google_service_auth_error.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
......@@ -37,12 +38,41 @@ const user_manager::User* GetActiveUser() {
return user_manager::UserManager::Get()->GetActiveUser();
}
const user_manager::User* GetPrimaryUser() {
return user_manager::UserManager::Get()->GetPrimaryUser();
}
Profile* GetProfileForActiveUser() {
const user_manager::User* const active_user = GetActiveUser();
DCHECK(active_user);
return chromeos::ProfileHelper::Get()->GetProfileByUser(active_user);
}
bool IsPrimaryUser() {
return GetActiveUser() == GetPrimaryUser();
}
bool HasPrimaryAccount(const Profile* profile) {
auto* identity_manager =
IdentityManagerFactory::GetForProfileIfExists(profile);
if (!identity_manager)
return false;
return identity_manager->HasPrimaryAccount(
signin::ConsentLevel::kNotRequired);
}
bool IsEmailDomainSupported(const user_manager::User* user) {
const std::string email = user->GetAccountId().GetUserEmail();
DCHECK(!email.empty());
constexpr char kGmailDomain[] = "gmail.com";
constexpr char kGooglemailDomain[] = "googlemail.com";
return (gaia::ExtractDomainName(email) == kGmailDomain ||
gaia::ExtractDomainName(email) == kGooglemailDomain ||
gaia::IsGoogleInternalAccountEmail(email));
}
} // namespace
AmbientClientImpl::AmbientClientImpl() = default;
......@@ -59,14 +89,19 @@ bool AmbientClientImpl::IsAmbientModeAllowed() {
if (!active_user || !active_user->HasGaiaAccount())
return false;
if (!IsPrimaryUser())
return false;
if (!IsEmailDomainSupported(active_user))
return false;
auto* profile = GetProfileForActiveUser();
if (!profile)
return false;
if (!profile->GetPrefs()->GetBoolean(
ash::ambient::prefs::kAmbientModeEnabled)) {
// Primary account might be missing during unittests.
if (!HasPrimaryAccount(profile))
return false;
}
if (!profile->IsRegularProfile())
return false;
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/ash/ambient/ambient_client_impl.h"
#include <memory>
#include "ash/public/cpp/ambient/ambient_client.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
#include "chrome/browser/chromeos/profiles/profile_helper.h"
#include "chrome/browser/signin/identity_test_environment_profile_adaptor.h"
#include "chrome/test/base/chrome_ash_test_base.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "chromeos/constants/chromeos_features.h"
#include "components/user_manager/scoped_user_manager.h"
constexpr char kTestProfileName[] = "user@gmail.com";
constexpr char kTestGaiaId[] = "1234567890";
class AmbientClientImplTest : public ChromeAshTestBase {
public:
AmbientClientImplTest() = default;
~AmbientClientImplTest() override = default;
void SetUp() override {
scoped_feature_list_.InitAndEnableFeature(
chromeos::features::kAmbientModeFeature);
AshTestBase::SetUp();
ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
profile_manager_ = std::make_unique<TestingProfileManager>(
TestingBrowserProcess::GetGlobal());
ASSERT_TRUE(profile_manager_->SetUp());
profile_ = profile_manager_->CreateTestingProfile(
kTestProfileName, /*prefs=*/{}, base::UTF8ToUTF16(kTestProfileName),
/*avatar_id=*/0, /*supervised_user_id=*/{},
IdentityTestEnvironmentProfileAdaptor::
GetIdentityTestEnvironmentFactories());
identity_test_env_adaptor_ =
std::make_unique<IdentityTestEnvironmentProfileAdaptor>(profile_);
user_manager_enabler_ = std::make_unique<user_manager::ScopedUserManager>(
std::make_unique<chromeos::FakeChromeUserManager>());
ambient_client_ = std::make_unique<AmbientClientImpl>();
}
void TearDown() override {
ambient_client_.reset();
user_manager_enabler_.reset();
identity_test_env_adaptor_.reset();
profile_ = nullptr;
profile_manager_->DeleteTestingProfile(kTestProfileName);
profile_manager_.reset();
AshTestBase::TearDown();
}
protected:
TestingProfile* profile() { return profile_; }
chromeos::FakeChromeUserManager* GetFakeUserManager() const {
return static_cast<chromeos::FakeChromeUserManager*>(
user_manager::UserManager::Get());
}
void AddAndLoginUser(const AccountId& account_id) {
GetFakeUserManager()->AddUser(account_id);
GetFakeUserManager()->LoginUser(account_id);
GetFakeUserManager()->SwitchActiveUser(account_id);
MaybeMakeAccountAsPrimaryAccount(account_id);
}
private:
signin::IdentityTestEnvironment* identity_test_env() {
return identity_test_env_adaptor_->identity_test_env();
}
void MaybeMakeAccountAsPrimaryAccount(const AccountId& account_id) {
if (!identity_test_env()->identity_manager()->HasPrimaryAccount(
signin::ConsentLevel::kNotRequired)) {
identity_test_env()->MakeUnconsentedPrimaryAccountAvailable(
account_id.GetUserEmail());
}
}
base::test::ScopedFeatureList scoped_feature_list_;
base::ScopedTempDir data_dir_;
std::unique_ptr<TestingProfileManager> profile_manager_;
// Owned by |profile_manager_|
TestingProfile* profile_ = nullptr;
std::unique_ptr<IdentityTestEnvironmentProfileAdaptor>
identity_test_env_adaptor_;
std::unique_ptr<user_manager::ScopedUserManager> user_manager_enabler_;
std::unique_ptr<AmbientClientImpl> ambient_client_;
};
TEST_F(AmbientClientImplTest, AllowedByPrimaryUser) {
AddAndLoginUser(AccountId::FromUserEmailGaiaId(
profile()->GetProfileUserName(), kTestGaiaId));
EXPECT_TRUE(ash::AmbientClient::Get()->IsAmbientModeAllowed());
}
TEST_F(AmbientClientImplTest, DisallowedByNonPrimaryUser) {
AddAndLoginUser(
AccountId::FromUserEmailGaiaId("user2@gmail.com", kTestGaiaId));
AddAndLoginUser(AccountId::FromUserEmailGaiaId(
profile()->GetProfileUserName(), kTestGaiaId));
EXPECT_FALSE(ash::AmbientClient::Get()->IsAmbientModeAllowed());
}
TEST_F(AmbientClientImplTest, DisallowedByEmailDomain) {
AddAndLoginUser(
AccountId::FromUserEmailGaiaId("user@gmailtest.com", kTestGaiaId));
EXPECT_FALSE(ash::AmbientClient::Get()->IsAmbientModeAllowed());
}
......@@ -4,6 +4,7 @@
#include "chrome/browser/ui/webui/settings/chromeos/personalization_section.h"
#include "ash/public/cpp/ambient/ambient_client.h"
#include "ash/public/cpp/ambient/ambient_prefs.h"
#include "base/bind.h"
#include "base/no_destructor.h"
......@@ -111,7 +112,8 @@ const std::vector<SearchConcept>& GetAmbientModeOffSearchConcepts() {
}
bool IsAmbientModeAllowed() {
return chromeos::features::IsAmbientModeEnabled();
return chromeos::features::IsAmbientModeEnabled() &&
ash::AmbientClient::Get()->IsAmbientModeAllowed();
}
GURL GetGooglePhotosURL() {
......
......@@ -4607,6 +4607,7 @@ test("unit_tests") {
"../browser/ui/app_list/test/fake_app_list_model_updater.h",
"../browser/ui/ash/accessibility/accessibility_controller_client_unittest.cc",
"../browser/ui/ash/accessibility/ax_tree_source_aura_unittest.cc",
"../browser/ui/ash/ambient/ambient_client_impl_unittest.cc",
"../browser/ui/ash/assistant/assistant_state_client_unittest.cc",
"../browser/ui/ash/assistant/conversation_starters_parser_unittest.cc",
"../browser/ui/ash/assistant/device_actions_unittest.cc",
......
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