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

Side by Side Diff: third_party/WebKit/Source/modules/shapedetection/FaceDetector.cpp

Issue 2441953002: Shape Detection: Add two layout tests for face detection (Closed)
Patch Set: reillyg@ comments on naming and description Created 4 years, 1 month 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
« no previous file with comments | « third_party/WebKit/LayoutTests/fast/shapedetection/shapedetection-security-test.html ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "modules/shapedetection/FaceDetector.h" 5 #include "modules/shapedetection/FaceDetector.h"
6 6
7 #include "core/dom/DOMException.h" 7 #include "core/dom/DOMException.h"
8 #include "core/dom/DOMRect.h" 8 #include "core/dom/DOMRect.h"
9 #include "core/dom/Document.h" 9 #include "core/dom/Document.h"
10 #include "core/fetch/ImageResource.h" 10 #include "core/fetch/ImageResource.h"
11 #include "core/frame/LocalDOMWindow.h" 11 #include "core/frame/LocalDOMWindow.h"
12 #include "core/frame/LocalFrame.h" 12 #include "core/frame/LocalFrame.h"
13 #include "core/html/HTMLImageElement.h" 13 #include "core/html/HTMLImageElement.h"
14 #include "platform/graphics/Image.h" 14 #include "platform/graphics/Image.h"
15 #include "public/platform/InterfaceProvider.h" 15 #include "public/platform/InterfaceProvider.h"
16 #include "third_party/skia/include/core/SkImage.h" 16 #include "third_party/skia/include/core/SkImage.h"
17 #include "third_party/skia/include/core/SkImageInfo.h" 17 #include "third_party/skia/include/core/SkImageInfo.h"
18 18
19 namespace blink { 19 namespace blink {
20 20
21 namespace { 21 namespace {
22 22
23 mojo::ScopedSharedBufferHandle getSharedBufferHandle( 23 mojo::ScopedSharedBufferHandle getSharedBufferHandle(
24 const HTMLImageElement* img) { 24 const HTMLImageElement* img,
25 ScriptPromiseResolver* resolver) {
25 ImageResource* const imageResource = img->cachedImage(); 26 ImageResource* const imageResource = img->cachedImage();
26 if (!imageResource) { 27 // TODO(xianglu): Add test case for undecodable images.
27 DLOG(ERROR) << "Failed to convert HTMLImageElement to ImageSource."; 28 if (!imageResource || imageResource->errorOccurred()) {
29 resolver->reject(DOMException::create(
30 InvalidStateError, "Failed to load or decode HTMLImageElement."));
28 return mojo::ScopedSharedBufferHandle(); 31 return mojo::ScopedSharedBufferHandle();
29 } 32 }
30 33
31 Image* const blinkImage = imageResource->getImage(); 34 Image* const blinkImage = imageResource->getImage();
32 if (!blinkImage) { 35 if (!blinkImage) {
33 DLOG(ERROR) << "Failed to convert ImageSource to blink::Image."; 36 resolver->reject(DOMException::create(
37 InvalidStateError, "Failed to get image from resource."));
34 return mojo::ScopedSharedBufferHandle(); 38 return mojo::ScopedSharedBufferHandle();
35 } 39 }
36 40
37 const sk_sp<SkImage> image = blinkImage->imageForCurrentFrame(); 41 const sk_sp<SkImage> image = blinkImage->imageForCurrentFrame();
38 DCHECK_EQ(img->naturalWidth(), image->width()); 42 DCHECK_EQ(img->naturalWidth(), image->width());
39 DCHECK_EQ(img->naturalHeight(), image->height()); 43 DCHECK_EQ(img->naturalHeight(), image->height());
40 44
41 if (!image) { 45 if (!image) {
42 DLOG(ERROR) << "Failed to convert blink::Image to sk_sp<SkImage>."; 46 resolver->reject(DOMException::create(
47 InvalidStateError, "Failed to get image from current frame."));
43 return mojo::ScopedSharedBufferHandle(); 48 return mojo::ScopedSharedBufferHandle();
44 } 49 }
45 50
46 const SkImageInfo skiaInfo = 51 const SkImageInfo skiaInfo =
47 SkImageInfo::MakeN32(image->width(), image->height(), image->alphaType()); 52 SkImageInfo::MakeN32(image->width(), image->height(), image->alphaType());
48 53
49 const uint32_t allocationSize = skiaInfo.getSafeSize(skiaInfo.minRowBytes()); 54 const uint32_t allocationSize = skiaInfo.getSafeSize(skiaInfo.minRowBytes());
50 55
51 mojo::ScopedSharedBufferHandle sharedBufferHandle = 56 mojo::ScopedSharedBufferHandle sharedBufferHandle =
52 mojo::SharedBufferHandle::Create(allocationSize); 57 mojo::SharedBufferHandle::Create(allocationSize);
53 if (!sharedBufferHandle.is_valid()) { 58 if (!sharedBufferHandle.is_valid()) {
54 // TODO(xianglu): Do something when the image is too large. 59 DLOG(ERROR) << "Requested allocation : " << allocationSize
55 DLOG(ERROR) << "Failed to create a sharedBufferHandle. allocationSize = " 60 << "B, larger than |mojo::edk::kMaxSharedBufferSize| == 16MB ";
56 << allocationSize << "bytes. limit = 16777216"; 61 // TODO(xianglu): For now we reject the promise if the image is too large.
62 // But consider resizing the image to remove restriction on the user side.
63 // Also, add layouttest for this case later.
64 resolver->reject(
65 DOMException::create(InvalidStateError, "Image exceeds size limit."));
57 return mojo::ScopedSharedBufferHandle(); 66 return mojo::ScopedSharedBufferHandle();
58 } 67 }
59 68
60 const mojo::ScopedSharedBufferMapping mappedBuffer = 69 const mojo::ScopedSharedBufferMapping mappedBuffer =
61 sharedBufferHandle->Map(allocationSize); 70 sharedBufferHandle->Map(allocationSize);
62 71
63 const SkPixmap pixmap(skiaInfo, mappedBuffer.get(), skiaInfo.minRowBytes()); 72 const SkPixmap pixmap(skiaInfo, mappedBuffer.get(), skiaInfo.minRowBytes());
64 if (!image->readPixels(pixmap, 0, 0)) { 73 if (!image->readPixels(pixmap, 0, 0)) {
65 DLOG(ERROR) << "Failed to read pixels from sk_sp<SkImage>."; 74 resolver->reject(DOMException::create(
75 InvalidStateError,
76 "Failed to read pixels: Unable to decompress or unsupported format."));
66 return mojo::ScopedSharedBufferHandle(); 77 return mojo::ScopedSharedBufferHandle();
67 } 78 }
68 79
69 return sharedBufferHandle; 80 return sharedBufferHandle;
70 } 81 }
71 82
72 } // anonymous namespace 83 } // anonymous namespace
73 84
74 FaceDetector* FaceDetector::create(ScriptState* scriptState) { 85 FaceDetector* FaceDetector::create(ScriptState* scriptState) {
75 return new FaceDetector(*scriptState->domWindow()->frame()); 86 return new FaceDetector(*scriptState->domWindow()->frame());
76 } 87 }
77 88
78 FaceDetector::FaceDetector(LocalFrame& frame) { 89 FaceDetector::FaceDetector(LocalFrame& frame) {
79 DCHECK(!m_service.is_bound()); 90 DCHECK(!m_service.is_bound());
80 DCHECK(frame.interfaceProvider()); 91 DCHECK(frame.interfaceProvider());
81 frame.interfaceProvider()->getInterface(mojo::GetProxy(&m_service)); 92 frame.interfaceProvider()->getInterface(mojo::GetProxy(&m_service));
82 } 93 }
83 94
84 ScriptPromise FaceDetector::detect(ScriptState* scriptState, 95 ScriptPromise FaceDetector::detect(ScriptState* scriptState,
85 const HTMLImageElement* img) { 96 const HTMLImageElement* img) {
86 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); 97 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
87 ScriptPromise promise = resolver->promise(); 98 ScriptPromise promise = resolver->promise();
88 99
89 if (!m_service) { 100 // TODO(xianglu): Add test cases for cross-origin-images.
101 if (img->wouldTaintOrigin(
102 scriptState->getExecutionContext()->getSecurityOrigin())) {
90 resolver->reject(DOMException::create( 103 resolver->reject(DOMException::create(
91 NotFoundError, "Face detection service unavailable.")); 104 SecurityError, "Image source from a different origin."));
92 return promise; 105 return promise;
93 } 106 }
94 107
95 if (!img) { 108 if (img->bitmapSourceSize().isZero()) {
96 resolver->reject(DOMException::create( 109 resolver->reject(
97 SyntaxError, "The provided HTMLImageElement is empty.")); 110 DOMException::create(InvalidStateError, "HTMLImageElement is empty."));
98 return promise; 111 return promise;
99 } 112 }
100 113
101 // TODO(xianglu): Add security check when the spec is ready.
102 // https://crbug.com/646083
103 mojo::ScopedSharedBufferHandle sharedBufferHandle = 114 mojo::ScopedSharedBufferHandle sharedBufferHandle =
104 getSharedBufferHandle(img); 115 getSharedBufferHandle(img, resolver);
105 if (!sharedBufferHandle->is_valid()) { 116 if (!sharedBufferHandle->is_valid())
117 return promise;
118
119 if (!m_service) {
106 resolver->reject(DOMException::create( 120 resolver->reject(DOMException::create(
107 SyntaxError, "Request for sharedBufferHandle failed.")); 121 NotSupportedError, "Face detection service unavailable."));
108 return promise; 122 return promise;
109 } 123 }
110 124
111 m_serviceRequests.add(resolver); 125 m_serviceRequests.add(resolver);
112 DCHECK(m_service.is_bound()); 126 DCHECK(m_service.is_bound());
113 m_service->DetectFace(std::move(sharedBufferHandle), img->naturalWidth(), 127 m_service->DetectFace(std::move(sharedBufferHandle), img->naturalWidth(),
114 img->naturalHeight(), 128 img->naturalHeight(),
115 convertToBaseCallback(WTF::bind( 129 convertToBaseCallback(WTF::bind(
116 &FaceDetector::onDetectFace, wrapPersistent(this), 130 &FaceDetector::onDetectFace, wrapPersistent(this),
117 wrapPersistent(resolver)))); 131 wrapPersistent(resolver))));
(...skipping 16 matching lines...) Expand all
134 148
135 resolver->resolve(detectedFaces); 149 resolver->resolve(detectedFaces);
136 m_serviceRequests.remove(resolver); 150 m_serviceRequests.remove(resolver);
137 } 151 }
138 152
139 DEFINE_TRACE(FaceDetector) { 153 DEFINE_TRACE(FaceDetector) {
140 visitor->trace(m_serviceRequests); 154 visitor->trace(m_serviceRequests);
141 } 155 }
142 156
143 } // namespace blink 157 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/LayoutTests/fast/shapedetection/shapedetection-security-test.html ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698