summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/modules/media_controls/elements/media_control_input_element.h
blob: 8f39a187026270b5402aa1a7102a9138f0ee4002 (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
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 2017 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_MODULES_MEDIA_CONTROLS_ELEMENTS_MEDIA_CONTROL_INPUT_ELEMENT_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_MEDIA_CONTROLS_ELEMENTS_MEDIA_CONTROL_INPUT_ELEMENT_H_

#include "third_party/blink/renderer/core/html/forms/html_input_element.h"
#include "third_party/blink/renderer/modules/media_controls/elements/media_control_element_base.h"
#include "third_party/blink/renderer/modules/modules_export.h"

namespace blink {

class MediaControlsImpl;

// MediaControlElementBase implementation based on an <input> element. Used by
// buttons and sliders.
class MODULES_EXPORT MediaControlInputElement : public HTMLInputElement,
                                                public MediaControlElementBase {
  USING_GARBAGE_COLLECTED_MIXIN(MediaControlInputElement);

 public:
  static bool ShouldRecordDisplayStates(const HTMLMediaElement&);

  // Creates an overflow menu element with the given button as a child.
  HTMLElement* CreateOverflowElement(MediaControlInputElement*);

  bool OverflowElementIsWanted();

  // Implements MediaControlElementBase.
  void SetOverflowElementIsWanted(bool) final;
  void MaybeRecordDisplayed() final;

  void Trace(blink::Visitor*) override;

  MediaControlInputElement* OverflowElementForTests() const {
    return overflow_element_;
  }

  // Get the size of the element in pixels or the default if we cannot get the
  // size because the element has not been layed out yet.
  WebSize GetSizeOrDefault() const override;
  bool IsDisabled() const override;

 protected:
  MediaControlInputElement(MediaControlsImpl&);

  // Returns a string that represents the button for metrics purposes. This
  // will be used as a suffix for histograms.
  virtual const char* GetNameForHistograms() const = 0;

  // Returns a string resource id of the media control element.
  // Subclasses should override this method to return the string resource id
  // of the overflow button.
  virtual int GetOverflowStringId() const;

  // Implements a default event handler to record interaction on click.
  void DefaultEventHandler(Event&) override;

  // Implements MediaControlElementBase.
  void UpdateShownState() final;

  // Updates the value of the Text string shown in the overflow menu.
  void UpdateOverflowString();

  // Record interaction if it wasn't recorded yet. It is used internally for
  // click events but also by some elements that have complex interaction logic.
  void MaybeRecordInteracted();

  // Returns whether this element is used for the overflow menu.
  bool IsOverflowElement() const;

  // Sets/removes a CSS class from this element based on |should_have_class|.
  void SetClass(const AtomicString& class_name, bool should_have_class);

  virtual void UpdateDisplayType();

  // Returns whether element is a button on the control panel.
  virtual bool IsControlPanelButton() const { return false; }

 private:
  friend class MediaControlInputElementTest;

  bool IsMouseFocusable() const override;
  bool IsMediaControlElement() const final;

  // Returns a string representation of the media control element. Used for
  // the overflow menu.
  String GetOverflowMenuString() const;

  // Returns a subtitle for the overflow menu text, or a null String if there
  // should not be a subtitle.
  virtual String GetOverflowMenuSubtitleString() const;

  // Create/update subtitle text on the overflow element. If a null String is
  // given, the subtitle element is removed.
  void UpdateOverflowSubtitleElement(String text);

  // Remove the subtitle text from the overflow element.
  void RemoveOverflowSubtitleElement();

  // Updates aria label on overflow_label_element_.
  void UpdateOverflowLabelAriaLabel(String);

  // Used for histograms, do not reorder.
  enum class CTREvent {
    kDisplayed = 0,
    kInteracted,
    kCount,
  };

  // Records the CTR event.
  void RecordCTREvent(CTREvent);

  // The copy of this element used for the overflow menu in the media controls.
  // Setting this pointer is optional so it may be null.
  Member<MediaControlInputElement> overflow_element_;

  // The overflow label element for the overflow_element_;
  Member<HTMLLabelElement> overflow_label_element_;

  // Contains the overflow text and its subtitle (if exists).
  Member<HTMLDivElement> overflow_menu_container_;

  // The text representation of the button within the overflow menu.
  Member<HTMLSpanElement> overflow_menu_text_;

  // The subtitle of the text within the overflow menu.
  Member<HTMLSpanElement> overflow_menu_subtitle_;

  // The aria label for the overflow element without subtitle text.
  String aria_label_;

  // Keeps track if the button was created for the purpose of the overflow menu.
  bool is_overflow_element_ = false;

  // Keeps track of whether the display/interaction have been recorded for the
  // CTR metrics.
  bool display_recorded_ = false;
  bool interaction_recorded_ = false;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_MEDIA_CONTROLS_ELEMENTS_MEDIA_CONTROL_INPUT_ELEMENT_H_