Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(62)

Side by Side Diff: third_party/WebKit/Source/core/imagebitmap/ImageBitmapFactories.cpp

Issue 1455763002: Use union type in ImageBitmapFactories.idl (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix errors Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2013, Google Inc. All rights reserved. 2 * Copyright (c) 2013, Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 19 matching lines...) Expand all
30 30
31 #include "config.h" 31 #include "config.h"
32 #include "core/imagebitmap/ImageBitmapFactories.h" 32 #include "core/imagebitmap/ImageBitmapFactories.h"
33 33
34 #include "bindings/core/v8/ExceptionState.h" 34 #include "bindings/core/v8/ExceptionState.h"
35 #include "bindings/core/v8/ScriptPromiseResolver.h" 35 #include "bindings/core/v8/ScriptPromiseResolver.h"
36 #include "core/dom/ExecutionContext.h" 36 #include "core/dom/ExecutionContext.h"
37 #include "core/fileapi/Blob.h" 37 #include "core/fileapi/Blob.h"
38 #include "core/frame/ImageBitmap.h" 38 #include "core/frame/ImageBitmap.h"
39 #include "core/frame/LocalDOMWindow.h" 39 #include "core/frame/LocalDOMWindow.h"
40 #include "core/html/HTMLCanvasElement.h"
41 #include "core/html/HTMLImageElement.h"
42 #include "core/html/HTMLVideoElement.h"
40 #include "core/html/ImageData.h" 43 #include "core/html/ImageData.h"
41 #include "core/workers/WorkerGlobalScope.h" 44 #include "core/workers/WorkerGlobalScope.h"
42 #include "platform/SharedBuffer.h" 45 #include "platform/SharedBuffer.h"
43 #include "platform/graphics/ImageSource.h" 46 #include "platform/graphics/ImageSource.h"
44 #include "platform/graphics/StaticBitmapImage.h"
45 #include "public/platform/WebSize.h"
46 #include "third_party/skia/include/core/SkImage.h"
47 #include <v8.h> 47 #include <v8.h>
48 48
49 namespace blink { 49 namespace blink {
50 50
51 static ScriptPromise fulfillImageBitmap(ScriptState* scriptState, PassRefPtrWill BeRawPtr<ImageBitmap> imageBitmap) 51 static ScriptPromise fulfillImageBitmap(ScriptState* scriptState, PassRefPtrWill BeRawPtr<ImageBitmap> imageBitmap)
52 { 52 {
53 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; 53 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ;
54 ScriptPromise promise = resolver->promise(); 54 ScriptPromise promise = resolver->promise();
55 if (imageBitmap) { 55 if (imageBitmap) {
56 resolver->resolve(imageBitmap); 56 resolver->resolve(imageBitmap);
57 } else { 57 } else {
58 resolver->reject(ScriptValue(scriptState, v8::Null(scriptState->isolate( )))); 58 resolver->reject(ScriptValue(scriptState, v8::Null(scriptState->isolate( ))));
59 } 59 }
60 return promise; 60 return promise;
61 } 61 }
62 62
63 ScriptPromise ImageBitmapFactories::createImageBitmap(ScriptState* scriptState, EventTarget& eventTarget, Blob* blob, ExceptionState& exceptionState) 63 static inline ImageBitmapSource* toImageBitmapSourceInternal(const ImageBitmapSo urceUnion& value)
64 { 64 {
65 ImageBitmapLoader* loader = ImageBitmapFactories::ImageBitmapLoader::create( from(eventTarget), IntRect(), scriptState); 65 if (value.isHTMLImageElement())
66 ScriptPromise promise = loader->promise(); 66 return value.getAsHTMLImageElement().get();
67 from(eventTarget).addLoader(loader); 67 if (value.isHTMLVideoElement())
68 loader->loadBlobAsync(eventTarget.executionContext(), blob); 68 return value.getAsHTMLVideoElement().get();
69 return promise; 69 if (value.isHTMLCanvasElement())
70 return value.getAsHTMLCanvasElement().get();
71 if (value.isBlob())
72 return value.getAsBlob();
73 if (value.isImageData())
74 return value.getAsImageData();
75 if (value.isImageBitmap())
76 return value.getAsImageBitmap().get();
77 ASSERT_NOT_REACHED();
78 return nullptr;
70 } 79 }
71 80
72 ScriptPromise ImageBitmapFactories::createImageBitmap(ScriptState* scriptState, EventTarget& eventTarget, Blob* blob, int sx, int sy, int sw, int sh, ExceptionS tate& exceptionState) 81 ScriptPromise ImageBitmapFactories::createImageBitmap(ScriptState* scriptState, EventTarget& eventTarget, const ImageBitmapSourceUnion& bitmapSource, ExceptionS tate& exceptionState)
73 { 82 {
83 ImageBitmapSource* bitmapSourceInternal = toImageBitmapSourceInternal(bitmap Source);
84 if (bitmapSourceInternal->isBlob()) {
85 return createImageBitmap(scriptState, eventTarget, bitmapSourceInternal, 0, 0, 1, 1, exceptionState);
86 }
87 IntSize srcSize = bitmapSourceInternal->bitmapSourceSize();
88 return createImageBitmap(scriptState, eventTarget, bitmapSourceInternal, 0, 0, srcSize.width(), srcSize.height(), exceptionState);
89 }
90
91 ScriptPromise ImageBitmapFactories::createImageBitmap(ScriptState* scriptState, EventTarget& eventTarget, const ImageBitmapSourceUnion& bitmapSource, int sx, in t sy, int sw, int sh, ExceptionState& exceptionState)
92 {
93 ImageBitmapSource* bitmapSourceInternal = toImageBitmapSourceInternal(bitmap Source);
94 return createImageBitmap(scriptState, eventTarget, bitmapSourceInternal, sx, sy, sw, sh, exceptionState);
95 }
96
97 ScriptPromise ImageBitmapFactories::createImageBitmap(ScriptState* scriptState, EventTarget& eventTarget, ImageBitmapSource* bitmapSource, int sx, int sy, int s w, int sh, ExceptionState& exceptionState)
98 {
99 if (bitmapSource->isHTMLImageElement()) {
Justin Novosad 2015/11/24 17:03:13 This would be a good place to use polymorphism. So
xidachen 2015/11/25 17:01:54 This has been done, but with exception of the case
100 ASSERT(eventTarget.toDOMWindow());
101 HTMLImageElement* image = static_cast<HTMLImageElement*>(bitmapSource);
102 if (!image->cachedImage()) {
103 exceptionState.throwDOMException(InvalidStateError, "No image can be retrieved from the provided element.");
104 return ScriptPromise();
105 }
106 if (image->cachedImage()->image()->isSVGImage()) {
107 exceptionState.throwDOMException(InvalidStateError, "The image eleme nt contains an SVG image, which is unsupported.");
108 return ScriptPromise();
109 }
110 }
111
112 if (bitmapSource->isHTMLVideoElement()) {
113 ASSERT(eventTarget.toDOMWindow());
114 HTMLVideoElement* video = static_cast<HTMLVideoElement*>(bitmapSource);
115 if (video->networkState() == HTMLMediaElement::NETWORK_EMPTY) {
116 exceptionState.throwDOMException(InvalidStateError, "The provided el ement has not retrieved data.");
117 return ScriptPromise();
118 }
119 if (video->readyState() <= HTMLMediaElement::HAVE_METADATA) {
120 exceptionState.throwDOMException(InvalidStateError, "The provided el ement's player has no current data.");
121 return ScriptPromise();
122 }
123 }
124
125 if (bitmapSource->isCanvasElement()) {
126 ASSERT(eventTarget.toDOMWindow());
127 HTMLCanvasElement* canvas = static_cast<HTMLCanvasElement*>(bitmapSource );
128 if (!canvas->originClean()) {
129 exceptionState.throwSecurityError("The canvas element provided is ta inted with cross-origin data.");
130 return ScriptPromise();
131 }
132 }
133
74 if (!sw || !sh) { 134 if (!sw || !sh) {
75 exceptionState.throwDOMException(IndexSizeError, String::format("The sou rce %s provided is 0.", sw ? "height" : "width")); 135 exceptionState.throwDOMException(IndexSizeError, String::format("The sou rce %s provided is 0.", sw ? "height" : "width"));
76 return ScriptPromise(); 136 return ScriptPromise();
77 } 137 }
78 ImageBitmapLoader* loader = ImageBitmapFactories::ImageBitmapLoader::create( from(eventTarget), IntRect(sx, sy, sw, sh), scriptState);
79 ScriptPromise promise = loader->promise();
80 from(eventTarget).addLoader(loader);
81 loader->loadBlobAsync(eventTarget.executionContext(), blob);
82 return promise;
83 }
84 138
85 ScriptPromise ImageBitmapFactories::createImageBitmap(ScriptState* scriptState, EventTarget& eventTarget, ImageData* data, ExceptionState& exceptionState) 139 if (bitmapSource->isBlob()) {
86 { 140 Blob* blob = static_cast<Blob*>(bitmapSource);
87 return createImageBitmap(scriptState, eventTarget, data, 0, 0, data->width() , data->height(), exceptionState); 141 ImageBitmapLoader* loader = ImageBitmapFactories::ImageBitmapLoader::cre ate(from(eventTarget), IntRect(sx, sy, sw, sh), scriptState);
88 } 142 ScriptPromise promise = loader->promise();
143 from(eventTarget).addLoader(loader);
144 loader->loadBlobAsync(eventTarget.executionContext(), blob);
145 return promise;
146 }
89 147
90 ScriptPromise ImageBitmapFactories::createImageBitmap(ScriptState* scriptState, EventTarget& eventTarget, ImageData* data, int sx, int sy, int sw, int sh, Excep tionState& exceptionState) 148 if (bitmapSource->isImageData()) {
91 { 149 ImageData* data = static_cast<ImageData*>(bitmapSource);
92 if (!sw || !sh) { 150 if (data->data()->bufferBase()->isNeutered()) {
93 exceptionState.throwDOMException(IndexSizeError, String::format("The sou rce %s provided is 0.", sw ? "height" : "width")); 151 exceptionState.throwDOMException(InvalidStateError, "The source data has been neutered.");
94 return ScriptPromise(); 152 return ScriptPromise();
153 }
154 return fulfillImageBitmap(scriptState, ImageBitmap::create(data, IntRect (sx, sy, sw, sh)));
95 } 155 }
96 if (data->data()->bufferBase()->isNeutered()) { 156
97 exceptionState.throwDOMException(InvalidStateError, "The source data has been neutered."); 157 if (bitmapSource->isHTMLImageElement()) {
98 return ScriptPromise(); 158 HTMLImageElement* image = static_cast<HTMLImageElement*>(bitmapSource);
159 if (!image->cachedImage()->image()->currentFrameHasSingleSecurityOrigin( )) {
160 exceptionState.throwSecurityError("The source image contains image d ata from multiple origins.");
Justin Novosad 2015/11/24 17:03:13 The security model is wrong here. Please file a bu
xidachen 2015/11/25 17:01:54 Could you give me the link to the specs so that I
161 return ScriptPromise();
162 }
163 Document* document = eventTarget.toDOMWindow()->document();
164 if (!image->cachedImage()->passesAccessControlCheck(document->securityOr igin()) && document->securityOrigin()->taintsCanvas(image->src())) {
165 exceptionState.throwSecurityError("Cross-origin access to the source image is denied.");
166 return ScriptPromise();
167 }
168 return fulfillImageBitmap(scriptState, ImageBitmap::create(image, IntRec t(sx, sy, sw, sh)));
99 } 169 }
100 // FIXME: make ImageBitmap creation asynchronous crbug.com/258082
101 return fulfillImageBitmap(scriptState, ImageBitmap::create(data, IntRect(sx, sy, sw, sh)));
102 }
103 170
104 ScriptPromise ImageBitmapFactories::createImageBitmap(ScriptState* scriptState, EventTarget& eventTarget, ImageBitmap* bitmap, ExceptionState& exceptionState) 171 if (bitmapSource->isHTMLVideoElement()) {
105 { 172 HTMLVideoElement* video = static_cast<HTMLVideoElement*>(bitmapSource);
106 return createImageBitmap(scriptState, eventTarget, bitmap, 0, 0, bitmap->wid th(), bitmap->height(), exceptionState); 173 if (!video->hasSingleSecurityOrigin()) {
107 } 174 exceptionState.throwSecurityError("The source video contains image d ata from multiple origins.");
Justin Novosad 2015/11/24 17:03:13 Same here
175 return ScriptPromise();
176 }
177 if (!video->webMediaPlayer()->didPassCORSAccessCheck()
178 && eventTarget.toDOMWindow()->document()->securityOrigin()->taintsCa nvas(video->currentSrc())) {
179 exceptionState.throwSecurityError("Cross-origin access to the source video is denied.");
180 return ScriptPromise();
181 }
182 return fulfillImageBitmap(scriptState, ImageBitmap::create(video, IntRec t(sx, sy, sw, sh)));
183 }
108 184
109 ScriptPromise ImageBitmapFactories::createImageBitmap(ScriptState* scriptState, EventTarget& eventTarget, ImageBitmap* bitmap, int sx, int sy, int sw, int sh, E xceptionState& exceptionState) 185 if (bitmapSource->isCanvasElement()) {
110 { 186 HTMLCanvasElement* canvas = static_cast<HTMLCanvasElement*>(bitmapSource );
111 if (!sw || !sh) { 187 return fulfillImageBitmap(scriptState, canvas->isPaintable() ? ImageBitm ap::create(canvas, IntRect(sx, sy, sw, sh)) : nullptr);
112 exceptionState.throwDOMException(IndexSizeError, String::format("The sou rce %s provided is 0.", sw ? "height" : "width"));
113 return ScriptPromise();
114 } 188 }
115 // FIXME: make ImageBitmap creation asynchronous crbug.com/258082 189
190 // Every type has been taken care of except ImageBitmap
191 ImageBitmap* bitmap = static_cast<ImageBitmap*>(bitmapSource);
116 return fulfillImageBitmap(scriptState, ImageBitmap::create(bitmap, IntRect(s x, sy, sw, sh))); 192 return fulfillImageBitmap(scriptState, ImageBitmap::create(bitmap, IntRect(s x, sy, sw, sh)));
117 } 193 }
118 194
119 const char* ImageBitmapFactories::supplementName() 195 const char* ImageBitmapFactories::supplementName()
120 { 196 {
121 return "ImageBitmapFactories"; 197 return "ImageBitmapFactories";
122 } 198 }
123 199
124 ImageBitmapFactories& ImageBitmapFactories::from(EventTarget& eventTarget) 200 ImageBitmapFactories& ImageBitmapFactories::from(EventTarget& eventTarget)
125 { 201 {
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 rejectPromise(); 288 rejectPromise();
213 } 289 }
214 290
215 DEFINE_TRACE(ImageBitmapFactories::ImageBitmapLoader) 291 DEFINE_TRACE(ImageBitmapFactories::ImageBitmapLoader)
216 { 292 {
217 visitor->trace(m_factory); 293 visitor->trace(m_factory);
218 visitor->trace(m_resolver); 294 visitor->trace(m_resolver);
219 } 295 }
220 296
221 } // namespace blink 297 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698