File: controller.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (98 lines) | stat: -rw-r--r-- 3,425 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/power/auto_screen_brightness/controller.h"

#include "base/time/default_tick_clock.h"
#include "chrome/browser/ash/power/auto_screen_brightness/adapter.h"
#include "chrome/browser/ash/power/auto_screen_brightness/als_reader.h"
#include "chrome/browser/ash/power/auto_screen_brightness/brightness_monitor_impl.h"
#include "chrome/browser/ash/power/auto_screen_brightness/gaussian_trainer.h"
#include "chrome/browser/ash/power/auto_screen_brightness/metrics_reporter.h"
#include "chrome/browser/ash/power/auto_screen_brightness/model_config_loader_impl.h"
#include "chrome/browser/ash/power/auto_screen_brightness/modeller_impl.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "components/session_manager/core/session_manager.h"

namespace ash {
namespace power {
namespace auto_screen_brightness {

Controller::Controller() {
  auto* session_manager = session_manager::SessionManager::Get();
  DCHECK(session_manager);

  if (!session_manager->sessions().empty()) {
    // If a user session has been created, we can use the primary user profile
    // to initialize all components immediately without waiting for
    // |OnUserSessionStarted| to be called.
    InitializeComponents();
    return;
  }

  // Wait for a user session to be created.
  session_manager->AddObserver(this);
  observing_session_manager_ = true;
}

Controller::~Controller() {
  if (observing_session_manager_) {
    auto* session_manager = session_manager::SessionManager::Get();
    if (session_manager)
      session_manager->RemoveObserver(this);
  }
}

void Controller::OnUserSessionStarted(bool /* is_primary_user */) {
  // The first sign-in user is the primary user, hence if |OnUserSessionStarted|
  // is called, the primary user profile should have been created. We will
  // ignore |is_primary_user|.
  if (is_initialized_)
    return;

  InitializeComponents();
}

void Controller::InitializeComponents() {
  DCHECK(!is_initialized_);
  is_initialized_ = true;

  Profile* const profile = ProfileManager::GetPrimaryUserProfile();
  DCHECK(profile);

  chromeos::PowerManagerClient* power_manager_client =
      chromeos::PowerManagerClient::Get();
  DCHECK(power_manager_client);

  metrics_reporter_ = std::make_unique<MetricsReporter>(
      power_manager_client, g_browser_process->local_state());

  als_reader_ = std::make_unique<AlsReader>();
  als_reader_->Init();

  brightness_monitor_ = std::make_unique<BrightnessMonitorImpl>();
  brightness_monitor_->Init();

  model_config_loader_ = std::make_unique<ModelConfigLoaderImpl>();

  ui::UserActivityDetector* user_activity_detector =
      ui::UserActivityDetector::Get();
  DCHECK(user_activity_detector);

  modeller_ = std::make_unique<ModellerImpl>(
      profile, als_reader_.get(), brightness_monitor_.get(),
      model_config_loader_.get(), user_activity_detector,
      std::make_unique<GaussianTrainer>());

  adapter_ = std::make_unique<Adapter>(
      profile, als_reader_.get(), brightness_monitor_.get(), modeller_.get(),
      model_config_loader_.get(), metrics_reporter_.get());
  adapter_->Init();
}

}  // namespace auto_screen_brightness
}  // namespace power
}  // namespace ash