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
|
// Copyright 2022 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_ARC_IDLE_MANAGER_ARC_IDLE_MANAGER_H_
#define CHROME_BROWSER_ASH_ARC_IDLE_MANAGER_ARC_IDLE_MANAGER_H_
#include <memory>
#include <string>
#include "base/memory/raw_ptr.h"
#include "base/scoped_observation.h"
#include "base/sequence_checker.h"
#include "base/timer/elapsed_timer.h"
#include "chromeos/ash/components/throttle/throttle_service.h"
#include "chromeos/ash/experiences/arc/mojom/power.mojom.h"
#include "chromeos/ash/experiences/arc/power/arc_power_bridge.h"
#include "chromeos/ash/experiences/arc/session/connection_observer.h"
#include "components/keyed_service/core/keyed_service.h"
#include "ui/display/manager/display_configurator.h"
namespace arc {
class ArcBridgeService;
// This class holds a number of observers which watch for all conditions that
// gate the triggering of ARC's Idle (Doze) mode.
class ArcIdleManager : public KeyedService,
public ArcPowerBridge::Observer,
public ash::ThrottleService,
public display::DisplayConfigurator::Observer,
public ConnectionObserver<mojom::PowerInstance> {
public:
class Delegate {
public:
Delegate() = default;
virtual ~Delegate() = default;
Delegate(const Delegate&) = delete;
Delegate& operator=(const Delegate&) = delete;
// Switches Android's so-called "Interactive Mode" ON (for |enable| true)
// or OFF. The OFF setting is equivalent to the power-off button on a
// smartphone (screen updates are turned off, leading to a progressive
// power down of the system, including doze mode).
// Switches are made via the Android |bridge|.
virtual void SetIdleState(ArcPowerBridge* arc_power_bridge,
ArcBridgeService* bridge,
bool enable) = 0;
};
ArcIdleManager(content::BrowserContext* context, ArcBridgeService* bridge);
ArcIdleManager(const ArcIdleManager&) = delete;
ArcIdleManager& operator=(const ArcIdleManager&) = delete;
~ArcIdleManager() override;
// Returns singleton instance for the given BrowserContext, or nullptr if
// the browser |context| is not allowed to use ARC.
static ArcIdleManager* GetForBrowserContext(content::BrowserContext* context);
static ArcIdleManager* GetForBrowserContextForTesting(
content::BrowserContext* context);
static void EnsureFactoryBuilt();
// KeyedService:
void Shutdown() override;
// ConnectionObserver<mojom::PowerInstance>:
void OnConnectionReady() override;
void OnConnectionClosed() override;
// ArcPowerBridge::Observer
void OnVmResumed() override;
void OnWillDestroyArcPowerBridge() override;
// Replaces the delegate so we can monitor switches without touching actual
// power state, for unit test purposes.
void set_delegate_for_testing(std::unique_ptr<Delegate> delegate) {
delegate_ = std::move(delegate);
}
// ash::ThrottleService:
// This is the main idle toggle.
void ThrottleInstance(bool should_idle) override;
// DisplayConfigurator::Observer:
void OnPowerStateChanged(chromeos::DisplayPowerState power_state) override;
private:
void LogScreenOffTimer(bool toggle_timer);
void RequestDozeWithoutMetrics(bool enabled);
void RequestDoze(bool enabled);
bool first_idle_happened_ = false;
base::TimeDelta enable_delay_;
std::unique_ptr<Delegate> delegate_;
bool is_connected_ GUARDED_BY_CONTEXT(sequence_checker_) = false;
SEQUENCE_CHECKER(sequence_checker_);
// Owned by ArcServiceManager.
const raw_ptr<ArcBridgeService> bridge_;
raw_ptr<ArcPowerBridge> arc_power_bridge_;
base::ElapsedTimer interactive_off_span_timer_;
base::OneShotTimer enable_timer_;
base::ScopedObservation<ArcPowerBridge, ArcPowerBridge::Observer>
powerbridge_observation_{this};
// During review, the team considered whether this notification
// should come from ArcDisplayPowerObserver to preserve the wall
// of abstraction between ArcIdleManager and its observers.
// We decided this direct approach was the better way to go, as
// the display state change triggers immediate configuration requests
// in ArcIdleManager, and we already have a precedent for this
// in OnVmMresumed().
// In the future, if this pattern repeats frequently, may consider
// refactoring.
base::ScopedObservation<display::DisplayConfigurator,
display::DisplayConfigurator::Observer>
display_observation_{this};
base::WeakPtrFactory<ArcIdleManager> weak_ptr_factory_{this};
};
} // namespace arc
#endif // CHROME_BROWSER_ASH_ARC_IDLE_MANAGER_ARC_IDLE_MANAGER_H_
|