device_activity_controller.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // Copyright 2021 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. #include "ash/components/device_activity/device_activity_controller.h"
  5. #include "ash/components/device_activity/daily_use_case_impl.h"
  6. #include "ash/components/device_activity/device_active_use_case.h"
  7. #include "ash/components/device_activity/device_activity_client.h"
  8. #include "ash/components/device_activity/fresnel_pref_names.h"
  9. #include "ash/components/device_activity/monthly_use_case_impl.h"
  10. #include "base/check_op.h"
  11. #include "base/metrics/histogram_functions.h"
  12. #include "base/rand_util.h"
  13. #include "base/time/time.h"
  14. #include "base/timer/timer.h"
  15. #include "chromeos/ash/components/dbus/session_manager/session_manager_client.h"
  16. #include "chromeos/ash/components/network/network_state.h"
  17. #include "chromeos/ash/components/network/network_state_handler.h"
  18. #include "components/prefs/pref_registry_simple.h"
  19. #include "components/version_info/channel.h"
  20. #include "google_apis/google_api_keys.h"
  21. #include "third_party/private_membership/src/private_membership_rlwe_client.h"
  22. namespace ash {
  23. namespace device_activity {
  24. namespace psm_rlwe = private_membership::rlwe;
  25. namespace {
  26. DeviceActivityController* g_ash_device_activity_controller = nullptr;
  27. // Policy device modes that should be classified as not being set.
  28. static const std::unordered_set<policy::DeviceMode>& DeviceModeNotSet() {
  29. static const std::unordered_set<policy::DeviceMode> kModeNotSet(
  30. {policy::DeviceMode::DEVICE_MODE_PENDING,
  31. policy::DeviceMode::DEVICE_MODE_NOT_SET});
  32. return kModeNotSet;
  33. }
  34. // Policy device modes that should be classified as consumer devices.
  35. static const std::unordered_set<policy::DeviceMode>& DeviceModeConsumer() {
  36. static const std::unordered_set<policy::DeviceMode> kModeConsumer(
  37. {policy::DeviceMode::DEVICE_MODE_CONSUMER,
  38. policy::DeviceMode::DEVICE_MODE_CONSUMER_KIOSK_AUTOLAUNCH});
  39. return kModeConsumer;
  40. }
  41. // Policy device modes that should be classified as enterprise devices.
  42. static const std::unordered_set<policy::DeviceMode>& DeviceModeEnterprise() {
  43. static const std::unordered_set<policy::DeviceMode> kModeEnterprise(
  44. {policy::DeviceMode::DEVICE_MODE_ENTERPRISE,
  45. policy::DeviceMode::DEVICE_MODE_ENTERPRISE_AD,
  46. policy::DeviceMode::DEVICE_MODE_DEMO});
  47. return kModeEnterprise;
  48. }
  49. // Production edge server for reporting device actives.
  50. // TODO(https://crbug.com/1267432): Enable passing base url as a runtime flag.
  51. const char kFresnelBaseUrl[] = "https://crosfresnel-pa.googleapis.com";
  52. // Count the number of PSM device active secret that is set.
  53. const char kDeviceActiveControllerPsmDeviceActiveSecretIsSet[] =
  54. "Ash.DeviceActiveController.PsmDeviceActiveSecretIsSet";
  55. void RecordPsmDeviceActiveSecretIsSet(bool is_set) {
  56. base::UmaHistogramBoolean(kDeviceActiveControllerPsmDeviceActiveSecretIsSet,
  57. is_set);
  58. }
  59. class PsmDelegateImpl : public PsmDelegate {
  60. public:
  61. PsmDelegateImpl() = default;
  62. PsmDelegateImpl(const PsmDelegateImpl&) = delete;
  63. PsmDelegateImpl& operator=(const PsmDelegateImpl&) = delete;
  64. ~PsmDelegateImpl() override = default;
  65. // PsmDelegate:
  66. rlwe::StatusOr<std::unique_ptr<psm_rlwe::PrivateMembershipRlweClient>>
  67. CreatePsmClient(
  68. psm_rlwe::RlweUseCase use_case,
  69. const std::vector<psm_rlwe::RlwePlaintextId>& plaintext_ids) override {
  70. return psm_rlwe::PrivateMembershipRlweClient::Create(use_case,
  71. plaintext_ids);
  72. }
  73. };
  74. } // namespace
  75. DeviceActivityController* DeviceActivityController::Get() {
  76. return g_ash_device_activity_controller;
  77. }
  78. // static
  79. void DeviceActivityController::RegisterPrefs(PrefRegistrySimple* registry) {
  80. const base::Time unix_epoch = base::Time::UnixEpoch();
  81. registry->RegisterTimePref(prefs::kDeviceActiveLastKnownDailyPingTimestamp,
  82. unix_epoch);
  83. registry->RegisterTimePref(prefs::kDeviceActiveLastKnownMonthlyPingTimestamp,
  84. unix_epoch);
  85. registry->RegisterTimePref(prefs::kDeviceActiveLastKnownAllTimePingTimestamp,
  86. unix_epoch);
  87. }
  88. // static
  89. base::TimeDelta DeviceActivityController::DetermineStartUpDelay(
  90. base::Time chrome_first_run_ts) {
  91. // Wait at least 10 minutes from the first chrome run sentinel file creation
  92. // time. This creation time is used as an indicator of when the device last
  93. // reset (powerwashed/recovery/RMA). PSM servers take 10 minutes from CheckIn
  94. // to return the correct response for CheckMembership requests, since the PSM
  95. // servers need to update their cache.
  96. //
  97. // This delay avoids the scenario where a device checks in, powerwashes, and
  98. // on device start up, gets the wrong check membership response.
  99. base::TimeDelta delay_on_first_chrome_run;
  100. base::Time current_ts = base::Time::Now();
  101. if (current_ts < (chrome_first_run_ts + base::Minutes(10))) {
  102. delay_on_first_chrome_run =
  103. chrome_first_run_ts + base::Minutes(10) - current_ts;
  104. }
  105. return delay_on_first_chrome_run;
  106. }
  107. // static
  108. MarketSegment DeviceActivityController::GetMarketSegment(
  109. policy::DeviceMode device_mode,
  110. policy::MarketSegment device_market_segment) {
  111. // Determine Fresnel market segment using the retrieved device policy
  112. // |device_mode| and |device_market_segment|.
  113. if (DeviceModeNotSet().count(device_mode)) {
  114. return MARKET_SEGMENT_UNKNOWN;
  115. }
  116. if (DeviceModeConsumer().count(device_mode)) {
  117. return MARKET_SEGMENT_CONSUMER;
  118. }
  119. if (DeviceModeEnterprise().count(device_mode)) {
  120. if (device_market_segment == policy::MarketSegment::ENTERPRISE) {
  121. return MARKET_SEGMENT_ENTERPRISE;
  122. }
  123. if (device_market_segment == policy::MarketSegment::EDUCATION) {
  124. return MARKET_SEGMENT_EDUCATION;
  125. }
  126. return MARKET_SEGMENT_ENTERPRISE_ENROLLED_BUT_UNKNOWN;
  127. }
  128. return MARKET_SEGMENT_UNKNOWN;
  129. }
  130. DeviceActivityController::DeviceActivityController(
  131. const ChromeDeviceMetadataParameters& chrome_passed_device_params,
  132. PrefService* local_state,
  133. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
  134. base::TimeDelta start_up_delay)
  135. : chrome_passed_device_params_(chrome_passed_device_params),
  136. statistics_provider_(
  137. chromeos::system::StatisticsProvider::GetInstance()) {
  138. DeviceActivityClient::RecordDeviceActivityMethodCalled(
  139. DeviceActivityClient::DeviceActivityMethod::
  140. kDeviceActivityControllerConstructor);
  141. DCHECK(!g_ash_device_activity_controller);
  142. g_ash_device_activity_controller = this;
  143. base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
  144. FROM_HERE,
  145. base::BindOnce(&device_activity::DeviceActivityController::Start,
  146. weak_factory_.GetWeakPtr(), local_state,
  147. url_loader_factory),
  148. start_up_delay);
  149. }
  150. DeviceActivityController::~DeviceActivityController() {
  151. DeviceActivityClient::RecordDeviceActivityMethodCalled(
  152. DeviceActivityClient::DeviceActivityMethod::
  153. kDeviceActivityControllerDestructor);
  154. DCHECK_EQ(this, g_ash_device_activity_controller);
  155. Stop();
  156. g_ash_device_activity_controller = nullptr;
  157. }
  158. void DeviceActivityController::Start(
  159. PrefService* local_state,
  160. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory) {
  161. DeviceActivityClient::RecordDeviceActivityMethodCalled(
  162. DeviceActivityClient::DeviceActivityMethod::
  163. kDeviceActivityControllerStart);
  164. // Wrap with callback from |psm_device_active_secret_| retrieval using
  165. // |SessionManagerClient| DBus.
  166. SessionManagerClient::Get()->GetPsmDeviceActiveSecret(base::BindOnce(
  167. &device_activity::DeviceActivityController::
  168. OnPsmDeviceActiveSecretFetched,
  169. weak_factory_.GetWeakPtr(), local_state, url_loader_factory));
  170. }
  171. void DeviceActivityController::OnPsmDeviceActiveSecretFetched(
  172. PrefService* local_state,
  173. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
  174. const std::string& psm_device_active_secret) {
  175. DeviceActivityClient::RecordDeviceActivityMethodCalled(
  176. DeviceActivityClient::DeviceActivityMethod::
  177. kDeviceActivityControllerOnPsmDeviceActiveSecretFetched);
  178. // In order for the device actives to be reported, the psm device active
  179. // secret must be retrieved successfully.
  180. if (psm_device_active_secret.empty()) {
  181. RecordPsmDeviceActiveSecretIsSet(false);
  182. VLOG(1) << "Can not generate PSM id without the psm device secret "
  183. "being defined.";
  184. return;
  185. }
  186. RecordPsmDeviceActiveSecretIsSet(true);
  187. // Continue when machine statistics are loaded, to avoid blocking.
  188. statistics_provider_->ScheduleOnMachineStatisticsLoaded(base::BindOnce(
  189. &device_activity::DeviceActivityController::OnMachineStatisticsLoaded,
  190. weak_factory_.GetWeakPtr(), local_state, url_loader_factory,
  191. psm_device_active_secret));
  192. }
  193. void DeviceActivityController::OnMachineStatisticsLoaded(
  194. PrefService* local_state,
  195. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
  196. const std::string& psm_device_active_secret) {
  197. DeviceActivityClient::RecordDeviceActivityMethodCalled(
  198. DeviceActivityClient::DeviceActivityMethod::
  199. kDeviceActivityControllerOnMachineStatisticsLoaded);
  200. // Initialize all device active use cases, sorted by
  201. // smallest to largest window. i.e. Daily > Monthly > First Active.
  202. std::vector<std::unique_ptr<DeviceActiveUseCase>> use_cases;
  203. use_cases.push_back(std::make_unique<DailyUseCaseImpl>(
  204. psm_device_active_secret, chrome_passed_device_params_, local_state));
  205. use_cases.push_back(std::make_unique<MonthlyUseCaseImpl>(
  206. psm_device_active_secret, chrome_passed_device_params_, local_state));
  207. da_client_network_ = std::make_unique<DeviceActivityClient>(
  208. NetworkHandler::Get()->network_state_handler(), url_loader_factory,
  209. std::make_unique<PsmDelegateImpl>(),
  210. std::make_unique<base::RepeatingTimer>(), kFresnelBaseUrl,
  211. google_apis::GetFresnelAPIKey(), std::move(use_cases));
  212. }
  213. void DeviceActivityController::Stop() {
  214. if (da_client_network_) {
  215. da_client_network_.reset();
  216. }
  217. }
  218. } // namespace device_activity
  219. } // namespace ash