File: AutoplayUmaHelper.h

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (145 lines) | stat: -rw-r--r-- 4,692 bytes parent folder | download
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2016 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 AutoplayUmaHelper_h
#define AutoplayUmaHelper_h

#include "core/CoreExport.h"
#include "core/dom/ContextLifecycleObserver.h"
#include "core/events/EventListener.h"
#include "platform/heap/Handle.h"

#include <set>

namespace blink {

// These values are used for histograms. Do not reorder.
enum class AutoplaySource {
  // Autoplay comes from HTMLMediaElement `autoplay` attribute.
  Attribute = 0,
  // Autoplay comes from `play()` method.
  Method = 1,
  // This enum value must be last.
  NumberOfSources = 2,
};

// These values are used for histograms. Do not reorder.
enum class AutoplayUnmuteActionStatus {
  Failure = 0,
  Success = 1,
  NumberOfStatus = 2,
};

// These values are used for histograms. Do not reorder.
enum AutoplayBlockedReason {
  AutoplayBlockedReasonDataSaver = 0,
  AutoplayBlockedReasonSetting = 1,
  AutoplayBlockedReasonDataSaverAndSetting = 2,
  // Keey at the end.
  AutoplayBlockedReasonMax = 3
};

enum class CrossOriginAutoplayResult {
  AutoplayAllowed = 0,
  AutoplayBlocked = 1,
  PlayedWithGesture = 2,
  UserPaused = 3,
  // Keep at the end.
  NumberOfResults = 4,
};

class Document;
class ElementVisibilityObserver;
class HTMLMediaElement;

class CORE_EXPORT AutoplayUmaHelper : public EventListener,
                                      public ContextLifecycleObserver {
  USING_GARBAGE_COLLECTED_MIXIN(AutoplayUmaHelper);

 public:
  static AutoplayUmaHelper* create(HTMLMediaElement*);

  ~AutoplayUmaHelper();

  bool operator==(const EventListener&) const override;

  void contextDestroyed(ExecutionContext*) override;

  void onAutoplayInitiated(AutoplaySource);

  void recordCrossOriginAutoplayResult(CrossOriginAutoplayResult);
  void recordAutoplayUnmuteStatus(AutoplayUnmuteActionStatus);

  void didMoveToNewDocument(Document& oldDocument);

  bool isVisible() const { return m_isVisible; }

  bool hasSource() const { return m_source != AutoplaySource::NumberOfSources; }

  DECLARE_VIRTUAL_TRACE();

 private:
  friend class MockAutoplayUmaHelper;

  explicit AutoplayUmaHelper(HTMLMediaElement*);
  void handleEvent(ExecutionContext*, Event*) override;
  void handlePlayingEvent();
  void handlePauseEvent();
  virtual void handleContextDestroyed();  // Make virtual for testing.

  void maybeUnregisterContextDestroyedObserver();
  void maybeUnregisterMediaElementPauseListener();

  void maybeStartRecordingMutedVideoPlayMethodBecomeVisible();
  void maybeStopRecordingMutedVideoPlayMethodBecomeVisible(bool isVisible);

  void maybeStartRecordingMutedVideoOffscreenDuration();
  void maybeStopRecordingMutedVideoOffscreenDuration();

  void maybeRecordUserPausedAutoplayingCrossOriginVideo();

  void onVisibilityChangedForMutedVideoOffscreenDuration(bool isVisibile);
  void onVisibilityChangedForMutedVideoPlayMethodBecomeVisible(bool isVisible);

  bool shouldListenToContextDestroyed() const;
  bool shouldRecordUserPausedAutoplayingCrossOriginVideo() const;

  // The autoplay source. Use AutoplaySource::NumberOfSources for invalid
  // source.
  AutoplaySource m_source;
  // The media element this UMA helper is attached to. |m_element| owns |this|.
  Member<HTMLMediaElement> m_element;

  // The observer is used to observe whether a muted video autoplaying by play()
  // method become visible at some point.
  // The UMA is pending for recording as long as this observer is non-null.
  Member<ElementVisibilityObserver> m_mutedVideoPlayMethodVisibilityObserver;

  // -----------------------------------------------------------------------
  // Variables used for recording the duration of autoplay muted video playing
  // offscreen.  The variables are valid when
  // |m_autoplayOffscrenVisibilityObserver| is non-null.
  // The recording stops whenever the playback pauses or the page is unloaded.

  // The starting time of autoplaying muted video.
  int64_t m_mutedVideoAutoplayOffscreenStartTimeMS;

  // The duration an autoplaying muted video has been in offscreen.
  int64_t m_mutedVideoAutoplayOffscreenDurationMS;

  // Whether an autoplaying muted video is visible.
  bool m_isVisible;

  std::set<CrossOriginAutoplayResult> m_recordedCrossOriginAutoplayResults;

  // The observer is used to observer an autoplaying muted video changing it's
  // visibility, which is used for offscreen duration UMA.  The UMA is pending
  // for recording as long as this observer is non-null.
  Member<ElementVisibilityObserver>
      m_mutedVideoOffscreenDurationVisibilityObserver;
};

}  // namespace blink

#endif  // AutoplayUmaHelper_h