// Copyright 2020 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/policy/reporting/install_event_log_collector_base.h"

#include "ash/constants/ash_switches.h"
#include "ash/constants/chrome_pref_names.h"
#include "base/check_deref.h"
#include "base/command_line.h"
#include "chrome/browser/sessions/exit_type_service.h"
#include "chromeos/ash/components/network/network_handler.h"
#include "chromeos/ash/components/network/network_state.h"
#include "chromeos/ash/components/network/network_state_handler.h"
#include "chromeos/ash/components/network/network_type_pattern.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/network_service_instance.h"

namespace policy {

InstallEventLogCollectorBase::InstallEventLogCollectorBase(
    PrefService* local_state,
    Profile* profile)
    : online_(GetOnlineState()),
      profile_(profile),
      local_state_(CHECK_DEREF(local_state)) {
  chromeos::PowerManagerClient::Get()->AddObserver(this);
  content::GetNetworkConnectionTracker()->AddNetworkConnectionObserver(this);
}

InstallEventLogCollectorBase::~InstallEventLogCollectorBase() {
  chromeos::PowerManagerClient::Get()->RemoveObserver(this);
  content::GetNetworkConnectionTracker()->RemoveNetworkConnectionObserver(this);
}

bool InstallEventLogCollectorBase::GetOnlineState() {
  ash::NetworkStateHandler::NetworkStateList network_state_list;
  ash::NetworkHandler::Get()->network_state_handler()->GetNetworkListByType(
      ash::NetworkTypePattern::Default(), true /* configured_only */,
      false /* visible_only */, 0 /* limit */, &network_state_list);

  for (const ash::NetworkState* network_state : network_state_list) {
    if (network_state->connection_state() == shill::kStateOnline) {
      return true;
    }
  }
  return false;
}

void InstallEventLogCollectorBase::OnLogin() {
  // Don't log in case session is restared or recovered from crash.
  if (base::CommandLine::ForCurrentProcess()->HasSwitch(
          ash::switches::kLoginUser) ||
      ExitTypeService::GetLastSessionExitType(profile_) == ExitType::kCrashed) {
    return;
  }

  online_ = GetOnlineState();
  OnLoginInternal();
}

void InstallEventLogCollectorBase::OnLogout() {
  // Don't log in case session is restared.
  if (local_state_->GetBoolean(ash::chrome_prefs::kWasRestarted)) {
    return;
  }

  OnLogoutInternal();
}

void InstallEventLogCollectorBase::OnConnectionChanged(
    net::NetworkChangeNotifier::ConnectionType type) {
  const bool currently_online = GetOnlineState();
  if (currently_online == online_) {
    return;
  }
  online_ = currently_online;

  OnConnectionStateChanged(type);
}
}  // namespace policy
