summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/html/media/video_wake_lock.h
blob: 33f6d17a4de785a88767fffad1710fdd596b8345 (plain)
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
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_HTML_MEDIA_VIDEO_WAKE_LOCK_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_HTML_MEDIA_VIDEO_WAKE_LOCK_H_

#include "mojo/public/cpp/bindings/remote.h"
#include "services/device/public/mojom/wake_lock.mojom-blink.h"
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/dom/events/native_event_listener.h"
#include "third_party/blink/renderer/core/execution_context/execution_context_lifecycle_state_observer.h"
#include "third_party/blink/renderer/core/html/media/remote_playback_observer.h"
#include "third_party/blink/renderer/core/page/page_visibility_observer.h"

namespace blink {

class HTMLVideoElement;

// This is implementing the wake lock logic related to a video element. It will
// take wake lock iif:
//  - the video is playing;
//  - the page is visible OR the video is in picture-in-picture;
//  - the video isn't being remoted;
//  - the execution context is neither paused nor destroyed.
// Each video element implements its own wake lock logic. The service will then
// merge all the requests and take the appropriate system wake lock.
// VideoWakeLock only uses "screen" related wake lock: it prevents the screen
// from locking on mobile or the lockscreen to show up on desktop.
class CORE_EXPORT VideoWakeLock final
    : public NativeEventListener,
      public PageVisibilityObserver,
      public RemotePlaybackObserver,
      public ExecutionContextLifecycleStateObserver {
  USING_GARBAGE_COLLECTED_MIXIN(VideoWakeLock);

 public:
  explicit VideoWakeLock(HTMLVideoElement&);

  void ElementDidMoveToNewDocument();

  void Trace(Visitor*) final;

  // EventListener implementation.
  void Invoke(ExecutionContext*, Event*) final;

  // RemotePlaybackObserver implementation.
  void OnRemotePlaybackStateChanged(
      mojom::blink::PresentationConnectionState) final;

  // ExecutionContextLifecycleStateObserver
  void ContextLifecycleStateChanged(mojom::FrameLifecycleState) override;
  void ContextDestroyed() override;

  bool active_for_tests() const { return active_; }

 private:
  // PageVisibilityObserver implementation.
  void PageVisibilityChanged() final;

  // Called when any state is changed. Will update active state and notify the
  // service if needed.
  void Update();

  // Return whether the wake lock should be active given the current state.
  bool ShouldBeActive() const;

  // Create a mojo pointer to the wake lock service if possible.
  void EnsureWakeLockService();

  // Called when the service is no longer connected.
  void OnConnectionError();

  // Notify the wake lock service of the current wake lock state.
  void UpdateWakeLockService();

  HTMLVideoElement& VideoElement() { return *video_element_; }
  const HTMLVideoElement& VideoElement() const { return *video_element_; }

  // `video_element_` owns |this|.
  Member<HTMLVideoElement> video_element_;

  mojo::Remote<device::mojom::blink::WakeLock> wake_lock_service_;

  bool playing_ = false;
  bool active_ = false;
  mojom::blink::PresentationConnectionState remote_playback_state_ =
      mojom::blink::PresentationConnectionState::CLOSED;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_HTML_MEDIA_VIDEO_WAKE_LOCK_H_