1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "chrome/browser/chromeos/policy/status_collector/affiliated_session_service.h"
6 
7 #include "base/logging.h"
8 #include "chrome/browser/chromeos/profiles/profile_helper.h"
9 
10 namespace policy {
11 
12 namespace {
13 
14 constexpr base::TimeDelta kMinimumSuspendDuration =
15     base::TimeDelta::FromMinutes(1);
16 
IsPrimaryAndAffiliated(Profile * profile)17 bool IsPrimaryAndAffiliated(Profile* profile) {
18   user_manager::User* user =
19       chromeos::ProfileHelper::Get()->GetUserByProfile(profile);
20   bool is_primary = chromeos::ProfileHelper::Get()->IsPrimaryProfile(profile);
21   bool is_affiliated = user && user->IsAffiliated();
22   if (!is_primary || !is_affiliated) {
23     VLOG(1) << "The profile for the primary user is not associated with an "
24                "affiliated user.";
25   }
26   return is_primary && is_affiliated;
27 }
28 
29 }  // namespace
30 
AffiliatedSessionService(base::Clock * clock)31 AffiliatedSessionService::AffiliatedSessionService(base::Clock* clock)
32     : clock_(clock), session_manager_(session_manager::SessionManager::Get()) {
33   if (session_manager_) {
34     // To alleviate tight coupling in unit tests to DeviceStatusCollector.
35     session_manager_observer_.Add(session_manager_);
36     is_session_locked_ = session_manager_->IsScreenLocked();
37   }
38   power_manager_observer_.Add(chromeos::PowerManagerClient::Get());
39 }
40 
41 AffiliatedSessionService::~AffiliatedSessionService() = default;
42 
AddObserver(AffiliatedSessionService::Observer * observer)43 void AffiliatedSessionService::AddObserver(
44     AffiliatedSessionService::Observer* observer) {
45   observers_.AddObserver(observer);
46 }
47 
RemoveObserver(AffiliatedSessionService::Observer * observer)48 void AffiliatedSessionService::RemoveObserver(
49     AffiliatedSessionService::Observer* observer) {
50   observers_.RemoveObserver(observer);
51 }
52 
OnSessionStateChanged()53 void AffiliatedSessionService::OnSessionStateChanged() {
54   bool is_session_locked = session_manager_->IsScreenLocked();
55   if (is_session_locked_ == is_session_locked) {
56     return;
57   }
58   is_session_locked_ = is_session_locked;
59 
60   if (is_session_locked_) {
61     for (auto& observer : observers_) {
62       observer.OnLocked();
63     }
64   } else {
65     for (auto& observer : observers_) {
66       observer.OnUnlocked();
67     }
68   }
69 }
70 
OnUserProfileLoaded(const AccountId & account_id)71 void AffiliatedSessionService::OnUserProfileLoaded(
72     const AccountId& account_id) {
73   Profile* profile =
74       chromeos::ProfileHelper::Get()->GetProfileByAccountId(account_id);
75   if (!IsPrimaryAndAffiliated(profile)) {
76     return;
77   }
78   profile_observer_.Add(profile);
79   for (auto& observer : observers_) {
80     observer.OnAffiliatedLogin(profile);
81   }
82 }
83 
OnProfileWillBeDestroyed(Profile * profile)84 void AffiliatedSessionService::OnProfileWillBeDestroyed(Profile* profile) {
85   is_session_locked_ = false;
86   if (!IsPrimaryAndAffiliated(profile)) {
87     return;
88   }
89   for (auto& observer : observers_) {
90     observer.OnAffiliatedLogout(profile);
91   }
92   profile_observer_.Remove(profile);
93 }
94 
SuspendDone(const base::TimeDelta & sleep_duration)95 void AffiliatedSessionService::SuspendDone(
96     const base::TimeDelta& sleep_duration) {
97   if (sleep_duration < kMinimumSuspendDuration) {
98     return;
99   }
100   for (auto& observer : observers_) {
101     observer.OnResumeActive(clock_->Now() - sleep_duration);
102   }
103 }
104 
105 }  // namespace policy
106