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
|
// Copyright 2023 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/arc/vmm/arc_system_state_observation.h"
#include "chrome/browser/ash/app_list/arc/arc_app_utils.h"
#include "chrome/browser/ash/arc/idle_manager/arc_background_service_observer.h"
#include "chrome/browser/ash/arc/idle_manager/arc_window_observer.h"
#include "chrome/browser/ash/arc/instance_throttle/arc_active_window_throttle_observer.h"
#include "chromeos/ash/experiences/arc/app/arc_app_constants.h"
#include "chromeos/ash/experiences/arc/session/arc_bridge_service.h"
#include "chromeos/ash/experiences/arc/session/arc_service_manager.h"
namespace arc {
ArcSystemStateObservation::ArcSystemStateObservation(
content::BrowserContext* context)
: ThrottleService(context) {
// Observe ARC running state. PlayStore ready roughly means the ARC app is
// ready to be launched by the user.
app_prefs_observation_.Observe(ArcAppListPrefs::Get(context));
// TODO(sstan): Use ARC window observer after it's landed.
AddObserver(std::make_unique<ArcActiveWindowThrottleObserver>());
// Observe background service in ARC side.
AddObserver(std::make_unique<ArcBackgroundServiceObserver>());
// Observe ARC window in ash.
AddObserver(std::make_unique<ArcWindowObserver>());
StartObservers();
}
ArcSystemStateObservation::~ArcSystemStateObservation() = default;
void ArcSystemStateObservation::ThrottleInstance(bool should_throttle) {
// ARC system or app is active.
if (!should_throttle) {
last_peace_timestamp_.reset();
DVLOG(1) << "ARC is active, reset peace timestamp.";
if (!active_callback_.is_null()) {
active_callback_.Run();
}
return;
}
// Only update when ARC is running. If ARC haven't booted, the "inactive"
// state does not make any sense.
if (arc_running_) {
// ARC system and app is not active.
last_peace_timestamp_ = base::Time::Now();
DVLOG(1) << "ARC is not active, time recording start at "
<< last_peace_timestamp_.value();
}
}
void ArcSystemStateObservation::OnAppStatesChanged(
const std::string& id,
const ArcAppListPrefs::AppInfo& app_info) {
if (id != arc::kPlayStoreAppId || app_info.ready == arc_running_) {
return;
}
if (app_info.ready) {
arc_running_ = true;
if (should_throttle()) {
last_peace_timestamp_ = base::Time::Now();
}
} else {
arc_running_ = false;
last_peace_timestamp_.reset();
}
}
void ArcSystemStateObservation::OnArcAppListPrefsDestroyed() {
app_prefs_observation_.Reset();
}
std::optional<base::TimeDelta> ArcSystemStateObservation::GetPeaceDuration() {
if (!last_peace_timestamp_.has_value()) {
return std::nullopt;
}
return base::Time::Now() - *last_peace_timestamp_;
}
void ArcSystemStateObservation::SetDurationResetCallback(
base::RepeatingClosure cb) {
active_callback_ = std::move(cb);
}
base::WeakPtr<ArcSystemStateObservation>
ArcSystemStateObservation::GetWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
} // namespace arc
|