File: MediaDocument.cpp

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 (262 lines) | stat: -rw-r--r-- 9,374 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "core/html/MediaDocument.h"

#include "bindings/core/v8/ExceptionState.h"
#include "core/HTMLNames.h"
#include "core/dom/ElementTraversal.h"
#include "core/dom/RawDataDocumentParser.h"
#include "core/dom/shadow/ShadowRoot.h"
#include "core/events/Event.h"
#include "core/events/EventListener.h"
#include "core/events/KeyboardEvent.h"
#include "core/frame/LocalFrame.h"
#include "core/frame/UseCounter.h"
#include "core/html/HTMLAnchorElement.h"
#include "core/html/HTMLBodyElement.h"
#include "core/html/HTMLContentElement.h"
#include "core/html/HTMLDivElement.h"
#include "core/html/HTMLHeadElement.h"
#include "core/html/HTMLHtmlElement.h"
#include "core/html/HTMLMetaElement.h"
#include "core/html/HTMLSourceElement.h"
#include "core/html/HTMLStyleElement.h"
#include "core/html/HTMLVideoElement.h"
#include "core/loader/DocumentLoader.h"
#include "core/loader/FrameLoader.h"
#include "core/loader/FrameLoaderClient.h"
#include "platform/Histogram.h"
#include "platform/KeyboardCodes.h"
#include "platform/text/PlatformLocale.h"

namespace blink {

using namespace HTMLNames;

// Enums used for UMA histogram.
enum MediaDocumentDownloadButtonValue {
  MediaDocumentDownloadButtonShown,
  MediaDocumentDownloadButtonClicked,
  // Only append new enums here.
  MediaDocumentDownloadButtonMax
};

void recordDownloadMetric(MediaDocumentDownloadButtonValue value) {
  DEFINE_STATIC_LOCAL(
      EnumerationHistogram, mediaDocumentDownloadButtonHistogram,
      ("Blink.MediaDocument.DownloadButton", MediaDocumentDownloadButtonMax));
  mediaDocumentDownloadButtonHistogram.count(value);
}

// FIXME: Share more code with PluginDocumentParser.
class MediaDocumentParser : public RawDataDocumentParser {
 public:
  static MediaDocumentParser* create(MediaDocument* document) {
    return new MediaDocumentParser(document);
  }

 private:
  explicit MediaDocumentParser(Document* document)
      : RawDataDocumentParser(document), m_didBuildDocumentStructure(false) {}

  void appendBytes(const char*, size_t) override;

  void createDocumentStructure();

  bool m_didBuildDocumentStructure;
};

class MediaDownloadEventListener final : public EventListener {
 public:
  static MediaDownloadEventListener* create() {
    return new MediaDownloadEventListener();
  }

  bool operator==(const EventListener& other) const override {
    return this == &other;
  }

 private:
  MediaDownloadEventListener()
      : EventListener(CPPEventListenerType), m_clicked(false) {}

  void handleEvent(ExecutionContext* context, Event* event) override {
    if (!m_clicked) {
      recordDownloadMetric(MediaDocumentDownloadButtonClicked);
      m_clicked = true;
    }
  }

  bool m_clicked;
};

void MediaDocumentParser::createDocumentStructure() {
  DCHECK(document());
  HTMLHtmlElement* rootElement = HTMLHtmlElement::create(*document());
  document()->appendChild(rootElement);
  rootElement->insertedByParser();

  if (isDetached())
    return;  // runScriptsAtDocumentElementAvailable can detach the frame.

  HTMLHeadElement* head = HTMLHeadElement::create(*document());
  HTMLMetaElement* meta = HTMLMetaElement::create(*document());
  meta->setAttribute(nameAttr, "viewport");
  meta->setAttribute(contentAttr, "width=device-width");
  head->appendChild(meta);

  HTMLVideoElement* media = HTMLVideoElement::create(*document());
  media->setAttribute(controlsAttr, "");
  media->setAttribute(autoplayAttr, "");
  media->setAttribute(nameAttr, "media");

  HTMLSourceElement* source = HTMLSourceElement::create(*document());
  source->setSrc(document()->url());

  if (DocumentLoader* loader = document()->loader())
    source->setType(loader->responseMIMEType());

  media->appendChild(source);

  HTMLBodyElement* body = HTMLBodyElement::create(*document());
  body->setAttribute(styleAttr, "margin: 0px;");

  document()->willInsertBody();

  HTMLDivElement* div = HTMLDivElement::create(*document());
  // Style sheets for media controls are lazily loaded until a media element is
  // encountered.  As a result, elements encountered before the media element
  // will not get the right style at first if we put the styles in
  // mediacontrols.css. To solve this issue, set the styles inline so that they
  // will be applied when the page loads.  See w3c example on how to centering
  // an element: https://www.w3.org/Style/Examples/007/center.en.html
  div->setAttribute(styleAttr,
                    "display: flex;"
                    "flex-direction: column;"
                    "justify-content: center;"
                    "align-items: center;"
                    "min-height: min-content;"
                    "height: 100%;");
  HTMLContentElement* content = HTMLContentElement::create(*document());
  div->appendChild(content);

  if (RuntimeEnabledFeatures::mediaDocumentDownloadButtonEnabled()) {
    HTMLAnchorElement* anchor = HTMLAnchorElement::create(*document());
    anchor->setAttribute(downloadAttr, "");
    anchor->setURL(document()->url());
    anchor->setTextContent(
        document()
            ->getCachedLocale(document()->contentLanguage())
            .queryString(WebLocalizedString::DownloadButtonLabel)
            .upper());
    // Using CSS style according to Android material design.
    anchor->setAttribute(
        styleAttr,
        "display: inline-block;"
        "margin-top: 32px;"
        "padding: 0 16px 0 16px;"
        "height: 36px;"
        "background: #000000;"
        "-webkit-tap-highlight-color: rgba(255, 255, 255, 0.12);"
        "font-family: Roboto;"
        "font-size: 14px;"
        "border-radius: 5px;"
        "color: white;"
        "font-weight: 500;"
        "text-decoration: none;"
        "line-height: 36px;");
    EventListener* listener = MediaDownloadEventListener::create();
    anchor->addEventListener(EventTypeNames::click, listener, false);
    HTMLDivElement* buttonContainer = HTMLDivElement::create(*document());
    buttonContainer->setAttribute(styleAttr,
                                  "text-align: center;"
                                  "height: 0;"
                                  "flex: none");
    buttonContainer->appendChild(anchor);
    div->appendChild(buttonContainer);
    recordDownloadMetric(MediaDocumentDownloadButtonShown);
  }

  // According to
  // https://html.spec.whatwg.org/multipage/browsers.html#read-media,
  // MediaDocument should have a single child which is the video element. Use
  // shadow root to hide all the elements we added here.
  ShadowRoot& shadowRoot = body->ensureUserAgentShadowRoot();
  shadowRoot.appendChild(div);
  body->appendChild(media);
  rootElement->appendChild(head);
  rootElement->appendChild(body);

  m_didBuildDocumentStructure = true;
}

void MediaDocumentParser::appendBytes(const char*, size_t) {
  if (m_didBuildDocumentStructure)
    return;

  LocalFrame* frame = document()->frame();
  if (!frame->loader().client()->allowMedia(document()->url()))
    return;

  createDocumentStructure();
  finish();
}

MediaDocument::MediaDocument(const DocumentInit& initializer)
    : HTMLDocument(initializer, MediaDocumentClass) {
  setCompatibilityMode(QuirksMode);
  lockCompatibilityMode();
  UseCounter::count(*this, UseCounter::MediaDocument);
  if (!isInMainFrame())
    UseCounter::count(*this, UseCounter::MediaDocumentInFrame);
}

DocumentParser* MediaDocument::createParser() {
  return MediaDocumentParser::create(this);
}

void MediaDocument::defaultEventHandler(Event* event) {
  Node* targetNode = event->target()->toNode();
  if (!targetNode)
    return;

  if (event->type() == EventTypeNames::keydown && event->isKeyboardEvent()) {
    HTMLVideoElement* video =
        Traversal<HTMLVideoElement>::firstWithin(*targetNode);
    if (!video)
      return;

    KeyboardEvent* keyboardEvent = toKeyboardEvent(event);
    if (keyboardEvent->key() == " " ||
        keyboardEvent->keyCode() == VKEY_MEDIA_PLAY_PAUSE) {
      // space or media key (play/pause)
      video->togglePlayState();
      event->setDefaultHandled();
    }
  }
}

}  // namespace blink