Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / html / HTMLVideoElement.cpp
1 /*
2  * Copyright (C) 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "core/html/HTMLVideoElement.h"
28
29 #include "CSSPropertyNames.h"
30 #include "HTMLNames.h"
31 #include "bindings/v8/ExceptionState.h"
32 #include "core/dom/Attribute.h"
33 #include "core/dom/Document.h"
34 #include "core/dom/ExceptionCode.h"
35 #include "core/html/HTMLImageLoader.h"
36 #include "core/html/canvas/CanvasRenderingContext.h"
37 #include "core/html/parser/HTMLParserIdioms.h"
38 #include "core/frame/Settings.h"
39 #include "core/rendering/RenderImage.h"
40 #include "core/rendering/RenderVideo.h"
41 #include "platform/UserGestureIndicator.h"
42
43 namespace WebCore {
44
45 using namespace HTMLNames;
46
47 inline HTMLVideoElement::HTMLVideoElement(Document& document)
48     : HTMLMediaElement(videoTag, document)
49 {
50     ScriptWrappable::init(this);
51     if (document.settings())
52         m_defaultPosterURL = AtomicString(document.settings()->defaultVideoPosterURL());
53 }
54
55 PassRefPtrWillBeRawPtr<HTMLVideoElement> HTMLVideoElement::create(Document& document)
56 {
57     RefPtrWillBeRawPtr<HTMLVideoElement> videoElement(adoptRefWillBeRefCountedGarbageCollected(new HTMLVideoElement(document)));
58     videoElement->suspendIfNeeded();
59     return videoElement.release();
60 }
61
62 bool HTMLVideoElement::rendererIsNeeded(const RenderStyle& style)
63 {
64     return HTMLElement::rendererIsNeeded(style);
65 }
66
67 RenderObject* HTMLVideoElement::createRenderer(RenderStyle*)
68 {
69     return new RenderVideo(this);
70 }
71
72 void HTMLVideoElement::attach(const AttachContext& context)
73 {
74     HTMLMediaElement::attach(context);
75
76     updateDisplayState();
77     if (shouldDisplayPosterImage()) {
78         if (!m_imageLoader)
79             m_imageLoader = adoptPtr(new HTMLImageLoader(this));
80         m_imageLoader->updateFromElement();
81         if (renderer())
82             toRenderImage(renderer())->imageResource()->setImageResource(m_imageLoader->image());
83     }
84 }
85
86 void HTMLVideoElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStylePropertySet* style)
87 {
88     if (name == widthAttr)
89         addHTMLLengthToStyle(style, CSSPropertyWidth, value);
90     else if (name == heightAttr)
91         addHTMLLengthToStyle(style, CSSPropertyHeight, value);
92     else
93         HTMLMediaElement::collectStyleForPresentationAttribute(name, value, style);
94 }
95
96 bool HTMLVideoElement::isPresentationAttribute(const QualifiedName& name) const
97 {
98     if (name == widthAttr || name == heightAttr)
99         return true;
100     return HTMLMediaElement::isPresentationAttribute(name);
101 }
102
103 void HTMLVideoElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
104 {
105     if (name == posterAttr) {
106         // Force a poster recalc by setting m_displayMode to Unknown directly before calling updateDisplayState.
107         HTMLMediaElement::setDisplayMode(Unknown);
108         updateDisplayState();
109         if (shouldDisplayPosterImage()) {
110             if (!m_imageLoader)
111                 m_imageLoader = adoptPtr(new HTMLImageLoader(this));
112             m_imageLoader->updateFromElementIgnoringPreviousError();
113         } else {
114             if (renderer())
115                 toRenderImage(renderer())->imageResource()->setImageResource(0);
116         }
117         // Notify the player when the poster image URL changes.
118         if (player())
119             player()->setPoster(posterImageURL());
120     } else
121         HTMLMediaElement::parseAttribute(name, value);
122 }
123
124 bool HTMLVideoElement::supportsFullscreen() const
125 {
126     if (!document().page())
127         return false;
128
129     if (!player())
130         return false;
131
132     return true;
133 }
134
135 unsigned HTMLVideoElement::videoWidth() const
136 {
137     if (!player())
138         return 0;
139     return player()->naturalSize().width();
140 }
141
142 unsigned HTMLVideoElement::videoHeight() const
143 {
144     if (!player())
145         return 0;
146     return player()->naturalSize().height();
147 }
148
149 bool HTMLVideoElement::isURLAttribute(const Attribute& attribute) const
150 {
151     return attribute.name() == posterAttr || HTMLMediaElement::isURLAttribute(attribute);
152 }
153
154 const AtomicString HTMLVideoElement::imageSourceURL() const
155 {
156     const AtomicString& url = getAttribute(posterAttr);
157     if (!stripLeadingAndTrailingHTMLSpaces(url).isEmpty())
158         return url;
159     return m_defaultPosterURL;
160 }
161
162 void HTMLVideoElement::setDisplayMode(DisplayMode mode)
163 {
164     DisplayMode oldMode = displayMode();
165     KURL poster = posterImageURL();
166
167     if (!poster.isEmpty()) {
168         // We have a poster path, but only show it until the user triggers display by playing or seeking and the
169         // media engine has something to display.
170         // Don't show the poster if there is a seek operation or
171         // the video has restarted because of loop attribute
172         if (mode == Video && oldMode == Poster && !hasAvailableVideoFrame())
173             mode = PosterWaitingForVideo;
174     }
175
176     HTMLMediaElement::setDisplayMode(mode);
177
178     if (renderer() && displayMode() != oldMode)
179         renderer()->updateFromElement();
180 }
181
182 void HTMLVideoElement::updateDisplayState()
183 {
184     if (posterImageURL().isEmpty())
185         setDisplayMode(Video);
186     else if (displayMode() < Poster)
187         setDisplayMode(Poster);
188 }
189
190 void HTMLVideoElement::paintCurrentFrameInContext(GraphicsContext* context, const IntRect& destRect) const
191 {
192     MediaPlayer* player = HTMLMediaElement::player();
193     if (!player)
194         return;
195     player->paint(context, destRect);
196 }
197
198 bool HTMLVideoElement::copyVideoTextureToPlatformTexture(blink::WebGraphicsContext3D* context, Platform3DObject texture, GLint level, GLenum type, GLenum internalFormat, bool premultiplyAlpha, bool flipY)
199 {
200     if (!player())
201         return false;
202     return player()->copyVideoTextureToPlatformTexture(context, texture, level, type, internalFormat, premultiplyAlpha, flipY);
203 }
204
205 bool HTMLVideoElement::hasAvailableVideoFrame() const
206 {
207     if (!player())
208         return false;
209
210     return player()->hasVideo() && player()->readyState() >= MediaPlayer::HaveCurrentData;
211 }
212
213 void HTMLVideoElement::webkitEnterFullscreen(ExceptionState& exceptionState)
214 {
215     if (isFullscreen())
216         return;
217
218     if (!supportsFullscreen()) {
219         exceptionState.throwDOMException(InvalidStateError, "This element does not support fullscreen mode.");
220         return;
221     }
222
223     enterFullscreen();
224 }
225
226 void HTMLVideoElement::webkitExitFullscreen()
227 {
228     if (isFullscreen())
229         exitFullscreen();
230 }
231
232 bool HTMLVideoElement::webkitSupportsFullscreen()
233 {
234     return supportsFullscreen();
235 }
236
237 bool HTMLVideoElement::webkitDisplayingFullscreen()
238 {
239     return isFullscreen();
240 }
241
242 void HTMLVideoElement::didMoveToNewDocument(Document& oldDocument)
243 {
244     if (m_imageLoader)
245         m_imageLoader->elementDidMoveToNewDocument();
246     HTMLMediaElement::didMoveToNewDocument(oldDocument);
247 }
248
249 unsigned HTMLVideoElement::webkitDecodedFrameCount() const
250 {
251     if (!player())
252         return 0;
253
254     return player()->decodedFrameCount();
255 }
256
257 unsigned HTMLVideoElement::webkitDroppedFrameCount() const
258 {
259     if (!player())
260         return 0;
261
262     return player()->droppedFrameCount();
263 }
264
265 KURL HTMLVideoElement::posterImageURL() const
266 {
267     String url = stripLeadingAndTrailingHTMLSpaces(imageSourceURL());
268     if (url.isEmpty())
269         return KURL();
270     return document().completeURL(url);
271 }
272
273 KURL HTMLVideoElement::mediaPlayerPosterURL()
274 {
275     return posterImageURL();
276 }
277
278 PassRefPtr<Image> HTMLVideoElement::getSourceImageForCanvas(SourceImageMode mode, SourceImageStatus* status) const
279 {
280     if (!hasAvailableVideoFrame()) {
281         *status = InvalidSourceImageStatus;
282         return nullptr;
283     }
284
285     IntSize intrinsicSize(videoWidth(), videoHeight());
286     OwnPtr<ImageBuffer> imageBuffer = ImageBuffer::create(intrinsicSize);
287     if (!imageBuffer) {
288         *status = InvalidSourceImageStatus;
289         return nullptr;
290     }
291
292     paintCurrentFrameInContext(imageBuffer->context(), IntRect(IntPoint(0, 0), intrinsicSize));
293
294     *status = NormalSourceImageStatus;
295     return imageBuffer->copyImage(mode == CopySourceImageIfVolatile ? CopyBackingStore : DontCopyBackingStore, Unscaled);
296 }
297
298 bool HTMLVideoElement::wouldTaintOrigin(SecurityOrigin* destinationSecurityOrigin) const
299 {
300     return !hasSingleSecurityOrigin() || (!(player() && player()->didPassCORSAccessCheck()) && destinationSecurityOrigin->taintsCanvas(currentSrc()));
301 }
302
303 FloatSize HTMLVideoElement::sourceSize() const
304 {
305     return FloatSize(videoWidth(), videoHeight());
306 }
307
308 }