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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
// 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.
#ifndef CHROME_BROWSER_ASH_POWER_ML_ADAPTIVE_SCREEN_BRIGHTNESS_MANAGER_H_
#define CHROME_BROWSER_ASH_POWER_ML_ADAPTIVE_SCREEN_BRIGHTNESS_MANAGER_H_
#include <memory>
#include <optional>
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observation.h"
#include "base/time/time.h"
#include "chrome/browser/ash/power/ml/boot_clock.h"
#include "chrome/browser/ash/power/ml/screen_brightness_event.pb.h"
#include "chromeos/dbus/power/power_manager_client.h"
#include "chromeos/dbus/power_manager/power_supply_properties.pb.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "services/viz/public/mojom/compositing/video_detector_observer.mojom.h"
#include "ui/base/user_activity/user_activity_detector.h"
#include "ui/base/user_activity/user_activity_observer.h"
namespace base {
class RepeatingTimer;
} // namespace base
namespace ash {
class AccessibilityManager;
class MagnificationManager;
namespace power {
namespace ml {
class AdaptiveScreenBrightnessUkmLogger;
class RecentEventsCounter;
// AdaptiveScreenBrightnessManager logs screen brightness and other features
// periodically and also when the screen brightness changes.
class AdaptiveScreenBrightnessManager
: public ui::UserActivityObserver,
public chromeos::PowerManagerClient::Observer,
public viz::mojom::VideoDetectorObserver {
public:
// Duration of inactivity that marks the end of an activity.
static constexpr base::TimeDelta kInactivityDuration = base::Seconds(20);
// Interval at which data should be logged.
static constexpr base::TimeDelta kLoggingInterval = base::Minutes(10);
AdaptiveScreenBrightnessManager(
std::unique_ptr<AdaptiveScreenBrightnessUkmLogger> ukm_logger,
ui::UserActivityDetector* detector,
chromeos::PowerManagerClient* power_manager_client,
AccessibilityManager* accessibility_manager,
MagnificationManager* magnification_manager,
mojo::PendingReceiver<viz::mojom::VideoDetectorObserver> receiver,
std::unique_ptr<base::RepeatingTimer> periodic_timer);
AdaptiveScreenBrightnessManager(const AdaptiveScreenBrightnessManager&) =
delete;
AdaptiveScreenBrightnessManager& operator=(
const AdaptiveScreenBrightnessManager&) = delete;
~AdaptiveScreenBrightnessManager() override;
// Returns a new instance of AdaptiveScreenBrightnessManager.
static std::unique_ptr<AdaptiveScreenBrightnessManager> CreateInstance();
// ui::UserActivityObserver overrides:
void OnUserActivity(const ui::Event* event) override;
// chromeos::PowerManagerClient::Observer overrides:
void ScreenBrightnessChanged(
const power_manager::BacklightBrightnessChange& change) override;
void PowerChanged(const power_manager::PowerSupplyProperties& proto) override;
void LidEventReceived(chromeos::PowerManagerClient::LidState state,
base::TimeTicks timestamp) override;
void TabletModeEventReceived(chromeos::PowerManagerClient::TabletMode mode,
base::TimeTicks timestamp) override;
// viz::mojom::VideoDetectorObserver overrides:
void OnVideoActivityStarted() override;
void OnVideoActivityEnded() override;
private:
friend class AdaptiveScreenBrightnessManagerTest;
// Called when the periodic timer triggers.
void OnTimerFired();
// Updates lid state and tablet mode from received switch states.
void OnReceiveSwitchStates(
std::optional<chromeos::PowerManagerClient::SwitchStates> switch_states);
// Updates screen brightness percent from received value.
void OnReceiveScreenBrightnessPercent(
std::optional<double> screen_brightness_percent);
// Returns the night light temperature as a percentage in the range [0, 100].
// Returns nullopt when the night light is not enabled.
const std::optional<int> GetNightLightTemperaturePercent() const;
void LogEvent();
BootClock boot_clock_;
// Timer to trigger periodically for logging data.
const std::unique_ptr<base::RepeatingTimer> periodic_timer_;
const std::unique_ptr<AdaptiveScreenBrightnessUkmLogger> ukm_logger_;
base::ScopedObservation<ui::UserActivityDetector, ui::UserActivityObserver>
user_activity_observation_{this};
base::ScopedObservation<chromeos::PowerManagerClient,
chromeos::PowerManagerClient::Observer>
power_manager_client_observation_{this};
const raw_ptr<AccessibilityManager> accessibility_manager_;
const raw_ptr<MagnificationManager> magnification_manager_;
const mojo::Receiver<viz::mojom::VideoDetectorObserver> receiver_;
// Counters for user events.
const std::unique_ptr<RecentEventsCounter> mouse_counter_;
const std::unique_ptr<RecentEventsCounter> key_counter_;
const std::unique_ptr<RecentEventsCounter> stylus_counter_;
const std::unique_ptr<RecentEventsCounter> touch_counter_;
chromeos::PowerManagerClient::LidState lid_state_ =
chromeos::PowerManagerClient::LidState::NOT_PRESENT;
chromeos::PowerManagerClient::TabletMode tablet_mode_ =
chromeos::PowerManagerClient::TabletMode::UNSUPPORTED;
std::optional<power_manager::PowerSupplyProperties::ExternalPower>
external_power_;
// Battery percent. This is in the range [0.0, 100.0].
std::optional<float> battery_percent_;
// Both |screen_brightness_percent_| and |previous_screen_brightness_percent_|
// are values reported directly by powerd. They are percentages as double but
// are in the range of [0, 100]. When we convert these values to the fields in
// ScreenBrightnessEvent, we cast them to ints.
std::optional<double> screen_brightness_percent_;
std::optional<double> previous_screen_brightness_percent_;
std::optional<base::TimeDelta> last_event_time_since_boot_;
// The time (since boot) of the most recent active event. This is the end of
// the most recent period of activity.
std::optional<base::TimeDelta> last_activity_time_since_boot_;
// The time (since boot) of the start of the most recent period of activity.
std::optional<base::TimeDelta> start_activity_time_since_boot_;
std::optional<bool> is_video_playing_;
std::optional<ScreenBrightnessEvent_Event_Reason> reason_;
base::WeakPtrFactory<AdaptiveScreenBrightnessManager> weak_ptr_factory_{this};
};
} // namespace ml
} // namespace power
} // namespace ash
#endif // CHROME_BROWSER_ASH_POWER_ML_ADAPTIVE_SCREEN_BRIGHTNESS_MANAGER_H_
|