Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/html/HTMLSourceElement.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "mozilla/dom/HTMLSourceElement.h"
8
#include "mozilla/dom/HTMLSourceElementBinding.h"
9
10
#include "mozilla/dom/HTMLImageElement.h"
11
#include "mozilla/dom/HTMLMediaElement.h"
12
#include "mozilla/dom/ResponsiveImageSelector.h"
13
#include "mozilla/dom/MediaList.h"
14
#include "mozilla/dom/MediaSource.h"
15
16
#include "nsGkAtoms.h"
17
18
#include "mozilla/dom/BlobURLProtocolHandler.h"
19
#include "mozilla/Preferences.h"
20
21
NS_IMPL_NS_NEW_HTML_ELEMENT(Source)
22
23
namespace mozilla {
24
namespace dom {
25
26
HTMLSourceElement::HTMLSourceElement(already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
27
  : nsGenericHTMLElement(std::move(aNodeInfo))
28
0
{
29
0
}
30
31
HTMLSourceElement::~HTMLSourceElement()
32
0
{
33
0
}
34
35
NS_IMPL_CYCLE_COLLECTION_INHERITED(HTMLSourceElement, nsGenericHTMLElement,
36
                                   mSrcMediaSource)
37
38
NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED_0(HTMLSourceElement, nsGenericHTMLElement)
39
40
NS_IMPL_ELEMENT_CLONE(HTMLSourceElement)
41
42
bool
43
HTMLSourceElement::MatchesCurrentMedia()
44
0
{
45
0
  if (mMediaList) {
46
0
    nsPresContext* pctx = OwnerDoc()->GetPresContext();
47
0
    return pctx && mMediaList->Matches(pctx);
48
0
  }
49
0
50
0
  // No media specified
51
0
  return true;
52
0
}
53
54
/* static */ bool
55
HTMLSourceElement::WouldMatchMediaForDocument(const nsAString& aMedia,
56
                                              const nsIDocument *aDocument)
57
0
{
58
0
  if (aMedia.IsEmpty()) {
59
0
    return true;
60
0
  }
61
0
62
0
  nsPresContext* pctx = aDocument->GetPresContext();
63
0
64
0
  RefPtr<MediaList> mediaList = MediaList::Create(aMedia);
65
0
  return pctx && mediaList->Matches(pctx);
66
0
}
67
68
void
69
HTMLSourceElement::UpdateMediaList(const nsAttrValue* aValue)
70
0
{
71
0
  mMediaList = nullptr;
72
0
  nsString mediaStr;
73
0
  if (!aValue || (mediaStr = aValue->GetStringValue()).IsEmpty()) {
74
0
    return;
75
0
  }
76
0
77
0
  mMediaList = MediaList::Create(mediaStr);
78
0
}
79
80
nsresult
81
HTMLSourceElement::AfterSetAttr(int32_t aNameSpaceID, nsAtom* aName,
82
                                const nsAttrValue* aValue,
83
                                const nsAttrValue* aOldValue,
84
                                nsIPrincipal* aMaybeScriptedPrincipal,
85
                                bool aNotify)
86
0
{
87
0
  if (aNameSpaceID == kNameSpaceID_None && aName == nsGkAtoms::srcset) {
88
0
    mSrcsetTriggeringPrincipal = nsContentUtils::GetAttrTriggeringPrincipal(
89
0
        this, aValue ? aValue->GetStringValue() : EmptyString(),
90
0
        aMaybeScriptedPrincipal);
91
0
  }
92
0
  // If we are associated with a <picture> with a valid <img>, notify it of
93
0
  // responsive parameter changes
94
0
  Element *parent = nsINode::GetParentElement();
95
0
  if (aNameSpaceID == kNameSpaceID_None &&
96
0
      (aName == nsGkAtoms::srcset ||
97
0
       aName == nsGkAtoms::sizes ||
98
0
       aName == nsGkAtoms::media ||
99
0
       aName == nsGkAtoms::type) &&
100
0
      parent && parent->IsHTMLElement(nsGkAtoms::picture)) {
101
0
    nsString strVal = aValue ? aValue->GetStringValue() : EmptyString();
102
0
    // Find all img siblings after this <source> and notify them of the change
103
0
    nsCOMPtr<nsIContent> sibling = AsContent();
104
0
    while ( (sibling = sibling->GetNextSibling()) ) {
105
0
      if (sibling->IsHTMLElement(nsGkAtoms::img)) {
106
0
        HTMLImageElement *img = static_cast<HTMLImageElement*>(sibling.get());
107
0
        if (aName == nsGkAtoms::srcset) {
108
0
          img->PictureSourceSrcsetChanged(AsContent(), strVal, aNotify);
109
0
        } else if (aName == nsGkAtoms::sizes) {
110
0
          img->PictureSourceSizesChanged(AsContent(), strVal, aNotify);
111
0
        } else if (aName == nsGkAtoms::media) {
112
0
          UpdateMediaList(aValue);
113
0
          img->PictureSourceMediaOrTypeChanged(AsContent(), aNotify);
114
0
        } else if (aName == nsGkAtoms::type) {
115
0
          img->PictureSourceMediaOrTypeChanged(AsContent(), aNotify);
116
0
        }
117
0
      }
118
0
    }
119
0
120
0
  } else if (aNameSpaceID == kNameSpaceID_None && aName == nsGkAtoms::media) {
121
0
    UpdateMediaList(aValue);
122
0
  } else if (aNameSpaceID == kNameSpaceID_None && aName == nsGkAtoms::src) {
123
0
    mSrcTriggeringPrincipal = nsContentUtils::GetAttrTriggeringPrincipal(
124
0
        this, aValue ? aValue->GetStringValue() : EmptyString(),
125
0
        aMaybeScriptedPrincipal);
126
0
    mSrcMediaSource = nullptr;
127
0
    if (aValue) {
128
0
      nsString srcStr = aValue->GetStringValue();
129
0
      nsCOMPtr<nsIURI> uri;
130
0
      NewURIFromString(srcStr, getter_AddRefs(uri));
131
0
      if (uri && IsMediaSourceURI(uri)) {
132
0
        NS_GetSourceForMediaSourceURI(uri, getter_AddRefs(mSrcMediaSource));
133
0
      }
134
0
    }
135
0
  }
136
0
137
0
  return nsGenericHTMLElement::AfterSetAttr(aNameSpaceID, aName,
138
0
                                            aValue, aOldValue,
139
0
                                            aMaybeScriptedPrincipal,
140
0
                                            aNotify);
141
0
}
142
143
nsresult
144
HTMLSourceElement::BindToTree(nsIDocument *aDocument,
145
                              nsIContent *aParent,
146
                              nsIContent *aBindingParent)
147
0
{
148
0
  nsresult rv = nsGenericHTMLElement::BindToTree(aDocument,
149
0
                                                 aParent,
150
0
                                                 aBindingParent);
151
0
  NS_ENSURE_SUCCESS(rv, rv);
152
0
153
0
  if (auto* media = HTMLMediaElement::FromNodeOrNull(aParent)) {
154
0
    media->NotifyAddedSource();
155
0
  }
156
0
157
0
  return NS_OK;
158
0
}
159
160
JSObject*
161
HTMLSourceElement::WrapNode(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
162
0
{
163
0
  return HTMLSourceElement_Binding::Wrap(aCx, this, aGivenProto);
164
0
}
165
166
} // namespace dom
167
} // namespace mozilla