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

content / browser / compute_pressure / pressure_client_impl.cc [blame]

// 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 "content/browser/compute_pressure/pressure_client_impl.h"

#include "content/browser/compute_pressure/pressure_service_base.h"
#include "services/device/public/mojom/pressure_update.mojom.h"

namespace content {

PressureClientImpl::PressureClientImpl(PressureServiceBase* service)
    : service_(service) {}

PressureClientImpl::~PressureClientImpl() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}

void PressureClientImpl::OnPressureUpdated(
    device::mojom::PressureUpdatePtr update) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (service_->ShouldDeliverUpdate()) {
    client_remote_->OnPressureUpdated(std::move(update));
  }
}

// Disconnection handler for |client_receiver_| and |client_remote_|. If the
// PressureClient connection from //services or to Blink breaks, we should stop
// delivering updates.
void PressureClientImpl::Reset() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  client_receiver_.reset();
  client_remote_.reset();
}

mojo::PendingReceiver<device::mojom::PressureClient>
PressureClientImpl::BindNewPipeAndPassReceiver() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  auto pending_receiver = client_remote_.BindNewPipeAndPassReceiver();
  // base::Unretained is safe because Mojo guarantees the callback will not
  // be called after `client_remote_` is deallocated, and `client_remote_`
  // is owned by this class.
  client_remote_.set_disconnect_handler(
      base::BindRepeating(&PressureClientImpl::Reset, base::Unretained(this)));
  return pending_receiver;
}

void PressureClientImpl::BindReceiver(
    mojo::PendingReceiver<device::mojom::PressureClient> pending_receiver) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  client_receiver_.Bind(std::move(pending_receiver));
  client_receiver_.set_disconnect_handler(
      base::BindOnce(&PressureClientImpl::Reset, base::Unretained(this)));
}

}  // namespace content