1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "third_party/blink/renderer/modules/accessibility/ax_media_element.h"
6
7 #include "third_party/blink/public/strings/grit/blink_strings.h"
8 #include "third_party/blink/renderer/core/html/media/html_media_element.h"
9 #include "third_party/blink/renderer/core/layout/layout_object.h"
10 #include "third_party/blink/renderer/platform/text/platform_locale.h"
11
12 namespace blink {
13
Create(LayoutObject * layout_object,AXObjectCacheImpl & ax_object_cache)14 AXObject* AccessibilityMediaElement::Create(
15 LayoutObject* layout_object,
16 AXObjectCacheImpl& ax_object_cache) {
17 DCHECK(layout_object->GetNode());
18 return MakeGarbageCollected<AccessibilityMediaElement>(layout_object,
19 ax_object_cache);
20 }
21
AccessibilityMediaElement(LayoutObject * layout_object,AXObjectCacheImpl & ax_object_cache)22 AccessibilityMediaElement::AccessibilityMediaElement(
23 LayoutObject* layout_object,
24 AXObjectCacheImpl& ax_object_cache)
25 : AXLayoutObject(layout_object, ax_object_cache) {}
26
TextAlternative(bool recursive,bool in_aria_labelled_by_traversal,AXObjectSet & visited,ax::mojom::NameFrom & name_from,AXRelatedObjectVector * related_objects,NameSources * name_sources) const27 String AccessibilityMediaElement::TextAlternative(
28 bool recursive,
29 bool in_aria_labelled_by_traversal,
30 AXObjectSet& visited,
31 ax::mojom::NameFrom& name_from,
32 AXRelatedObjectVector* related_objects,
33 NameSources* name_sources) const {
34 if (IsUnplayable()) {
35 HTMLMediaElement* element =
36 static_cast<HTMLMediaElement*>(layout_object_->GetNode());
37 return element->GetLocale().QueryString(IDS_MEDIA_PLAYBACK_ERROR);
38 }
39 return AXLayoutObject::TextAlternative(
40 recursive, in_aria_labelled_by_traversal, visited, name_from,
41 related_objects, name_sources);
42 }
43
CanHaveChildren() const44 bool AccessibilityMediaElement::CanHaveChildren() const {
45 return HasControls();
46 }
47
ComputeAccessibilityIsIgnored(IgnoredReasons * ignored_reasons) const48 bool AccessibilityMediaElement::ComputeAccessibilityIsIgnored(
49 IgnoredReasons* ignored_reasons) const {
50 return !HasControls() && HasEmptySource();
51 }
52
Restriction() const53 AXRestriction AccessibilityMediaElement::Restriction() const {
54 if (IsUnplayable())
55 return kRestrictionDisabled;
56
57 return AXNodeObject::Restriction();
58 }
59
HasControls() const60 bool AccessibilityMediaElement::HasControls() const {
61 return To<HTMLMediaElement>(GetNode())->ShouldShowControls();
62 }
63
HasEmptySource() const64 bool AccessibilityMediaElement::HasEmptySource() const {
65 return To<HTMLMediaElement>(GetNode())->getNetworkState() ==
66 HTMLMediaElement::kNetworkEmpty;
67 }
68
IsUnplayable() const69 bool AccessibilityMediaElement::IsUnplayable() const {
70 HTMLMediaElement* element =
71 static_cast<HTMLMediaElement*>(layout_object_->GetNode());
72 HTMLMediaElement::NetworkState network_state = element->getNetworkState();
73 return (element->error() ||
74 network_state == HTMLMediaElement::kNetworkEmpty ||
75 network_state == HTMLMediaElement::kNetworkNoSource);
76 }
77
78 } // namespace blink
79