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

content / browser / idle / idle_manager_impl.h [blame]

// 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 CONTENT_BROWSER_IDLE_IDLE_MANAGER_IMPL_H_
#define CONTENT_BROWSER_IDLE_IDLE_MANAGER_IMPL_H_

#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observation.h"
#include "base/sequence_checker.h"
#include "base/time/time.h"
#include "content/common/content_export.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "mojo/public/cpp/bindings/remote_set.h"
#include "third_party/blink/public/mojom/idle/idle_manager.mojom.h"
#include "ui/base/idle/idle_polling_service.h"
#include "url/origin.h"

namespace content {

class RenderFrameHost;

class CONTENT_EXPORT IdleManagerImpl : public blink::mojom::IdleManager,
                                       public ui::IdlePollingService::Observer {
 public:
  explicit IdleManagerImpl(RenderFrameHost* render_frame_host);
  ~IdleManagerImpl() override;

  IdleManagerImpl(const IdleManagerImpl&) = delete;
  IdleManagerImpl& operator=(const IdleManagerImpl&) = delete;

  void CreateService(mojo::PendingReceiver<blink::mojom::IdleManager> receiver);

  // blink.mojom.IdleManager:
  void AddMonitor(mojo::PendingRemote<blink::mojom::IdleMonitor> monitor_remote,
                  AddMonitorCallback callback) final;

  void SetIdleOverride(bool is_user_active, bool is_screen_unlocked);
  void ClearIdleOverride();

 private:
  // Check permission controller to see if the notification permission is
  // enabled for this frame.
  bool HasPermission();

  // When a monitor's pipe closes and it has been removed from |monitors_|.
  void OnMonitorDisconnected(mojo::RemoteSetElementId id);

  // Notifies |monitors_| of the new idle state.
  void OnIdleStateChange(const ui::IdlePollingService::State& state) override;

  blink::mojom::IdleStatePtr CreateIdleState(
      const ui::IdlePollingService::State& state);
  blink::mojom::IdleStatePtr CheckIdleState();

  // |observer_| and |state_override_| are mutually exclusive as when DevTools
  // has provided an override we no longer need to poll for the actual state.
  base::ScopedObservation<ui::IdlePollingService,
                          ui::IdlePollingService::Observer>
      observer_{this};
  bool state_override_ = false;

  // Raw pointer is safe because this object is owned by |render_frame_host_|.
  const raw_ptr<RenderFrameHost> render_frame_host_;

  // Registered clients.
  mojo::ReceiverSet<blink::mojom::IdleManager> receivers_;

  blink::mojom::IdleStatePtr last_state_;

  // Registered IdleMonitor instances, added when clients call AddMonitor().
  mojo::RemoteSet<blink::mojom::IdleMonitor> monitors_;

  SEQUENCE_CHECKER(sequence_checker_);
  base::WeakPtrFactory<IdleManagerImpl> weak_factory_{this};
};

}  // namespace content

#endif  // CONTENT_BROWSER_IDLE_IDLE_MANAGER_IMPL_H_