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

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: Use promise_test() instead of async_test() Created 4 years, 2 months 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 // 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 if (!imageResource) {
27 DLOG(ERROR) << "Failed to convert HTMLImageElement to ImageSource."; 28 resolver->reject(DOMException::create(
29 InvalidStateError,
30 "Failed to acquire ImageResource from 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();
mcasas 2016/10/21 23:43:16 I'm not 100% sure, but I think before doing this w
xianglu 2016/10/22 02:19:09 I agree. However, the testing image ("imported/wpt
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 blink::Image from ImageResource."));
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,
48 "Failed to convert blink::Image to sk_sp<SkImage>."));
43 return mojo::ScopedSharedBufferHandle(); 49 return mojo::ScopedSharedBufferHandle();
44 } 50 }
45 51
46 const SkImageInfo skiaInfo = 52 const SkImageInfo skiaInfo =
47 SkImageInfo::MakeN32(image->width(), image->height(), image->alphaType()); 53 SkImageInfo::MakeN32(image->width(), image->height(), image->alphaType());
48 54
49 const uint32_t allocationSize = skiaInfo.getSafeSize(skiaInfo.minRowBytes()); 55 const uint32_t allocationSize = skiaInfo.getSafeSize(skiaInfo.minRowBytes());
50 56
51 mojo::ScopedSharedBufferHandle sharedBufferHandle = 57 mojo::ScopedSharedBufferHandle sharedBufferHandle =
52 mojo::SharedBufferHandle::Create(allocationSize); 58 mojo::SharedBufferHandle::Create(allocationSize);
53 if (!sharedBufferHandle.is_valid()) { 59 if (!sharedBufferHandle.is_valid()) {
54 // TODO(xianglu): Do something when the image is too large. 60 DLOG(ERROR) << "AllocationSize = " << allocationSize
55 DLOG(ERROR) << "Failed to create a sharedBufferHandle. allocationSize = " 61 << "bytes. limit = 16777216, referred from "
56 << allocationSize << "bytes. limit = 16777216"; 62 "https://cs.chromium.org/chromium/src/"
63 "mojo/edk/system/broker_host.cc?l=24";
64 // TODO(xianglu): For now we reject the promise if the image is too large.
65 // But consider resizing the image to remove restriction on the user side.
66 // Also, add layouttest for this case later.
67 resolver->reject(DOMException::create(
68 InvalidStateError,
69 "HTMLImageElement is too large to fit in mojo shared buffer."));
57 return mojo::ScopedSharedBufferHandle(); 70 return mojo::ScopedSharedBufferHandle();
58 } 71 }
59 72
60 const mojo::ScopedSharedBufferMapping mappedBuffer = 73 const mojo::ScopedSharedBufferMapping mappedBuffer =
61 sharedBufferHandle->Map(allocationSize); 74 sharedBufferHandle->Map(allocationSize);
62 75
63 const SkPixmap pixmap(skiaInfo, mappedBuffer.get(), skiaInfo.minRowBytes()); 76 const SkPixmap pixmap(skiaInfo, mappedBuffer.get(), skiaInfo.minRowBytes());
64 if (!image->readPixels(pixmap, 0, 0)) { 77 if (!image->readPixels(pixmap, 0, 0)) {
65 DLOG(ERROR) << "Failed to read pixels from sk_sp<SkImage>."; 78 resolver->reject(DOMException::create(
79 InvalidStateError,
80 "Image is broken. Failed to read pixels from sk_sp<SkImage>."));
66 return mojo::ScopedSharedBufferHandle(); 81 return mojo::ScopedSharedBufferHandle();
67 } 82 }
68 83
69 return sharedBufferHandle; 84 return sharedBufferHandle;
70 } 85 }
71 86
72 } // anonymous namespace 87 } // anonymous namespace
73 88
74 FaceDetector* FaceDetector::create(ScriptState* scriptState) { 89 FaceDetector* FaceDetector::create(ScriptState* scriptState) {
75 return new FaceDetector(*scriptState->domWindow()->frame()); 90 return new FaceDetector(*scriptState->domWindow()->frame());
76 } 91 }
77 92
78 FaceDetector::FaceDetector(LocalFrame& frame) { 93 FaceDetector::FaceDetector(LocalFrame& frame) {
79 DCHECK(!m_service.is_bound()); 94 DCHECK(!m_service.is_bound());
80 DCHECK(frame.interfaceProvider()); 95 DCHECK(frame.interfaceProvider());
81 frame.interfaceProvider()->getInterface(mojo::GetProxy(&m_service)); 96 frame.interfaceProvider()->getInterface(mojo::GetProxy(&m_service));
82 } 97 }
83 98
84 ScriptPromise FaceDetector::detect(ScriptState* scriptState, 99 ScriptPromise FaceDetector::detect(ScriptState* scriptState,
85 const HTMLImageElement* img) { 100 const HTMLImageElement* img) {
86 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); 101 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
87 ScriptPromise promise = resolver->promise(); 102 ScriptPromise promise = resolver->promise();
88 103
89 if (!m_service) { 104 // TODO(xianglu): Add test cases for cross-origin-images.
105 if (img->wouldTaintOrigin(
106 scriptState->getExecutionContext()->getSecurityOrigin())) {
90 resolver->reject(DOMException::create( 107 resolver->reject(DOMException::create(
91 NotFoundError, "Face detection service unavailable.")); 108 SecurityError, "Image source from a different origin."));
92 return promise; 109 return promise;
93 } 110 }
94 111
95 if (!img) { 112 if (img->bitmapSourceSize().isZero()) {
96 resolver->reject(DOMException::create( 113 resolver->reject(
97 SyntaxError, "The provided HTMLImageElement is empty.")); 114 DOMException::create(InvalidStateError, "HTMLImageElement is empty."));
98 return promise; 115 return promise;
99 } 116 }
100 117
101 // TODO(xianglu): Add security check when the spec is ready.
102 // https://crbug.com/646083
103 mojo::ScopedSharedBufferHandle sharedBufferHandle = 118 mojo::ScopedSharedBufferHandle sharedBufferHandle =
104 getSharedBufferHandle(img); 119 getSharedBufferHandle(img, resolver);
105 if (!sharedBufferHandle->is_valid()) { 120 if (!sharedBufferHandle->is_valid())
121 return promise;
122
123 if (!m_service) {
106 resolver->reject(DOMException::create( 124 resolver->reject(DOMException::create(
107 SyntaxError, "Request for sharedBufferHandle failed.")); 125 NotSupportedError, "Face detection service unavailable."));
108 return promise; 126 return promise;
109 } 127 }
110 128
111 m_serviceRequests.add(resolver); 129 m_serviceRequests.add(resolver);
112 DCHECK(m_service.is_bound()); 130 DCHECK(m_service.is_bound());
113 m_service->DetectFace(std::move(sharedBufferHandle), img->naturalWidth(), 131 m_service->DetectFace(std::move(sharedBufferHandle), img->naturalWidth(),
114 img->naturalHeight(), 132 img->naturalHeight(),
115 convertToBaseCallback(WTF::bind( 133 convertToBaseCallback(WTF::bind(
116 &FaceDetector::onDetectFace, wrapPersistent(this), 134 &FaceDetector::onDetectFace, wrapPersistent(this),
117 wrapPersistent(resolver)))); 135 wrapPersistent(resolver))));
(...skipping 16 matching lines...) Expand all
134 152
135 resolver->resolve(detectedFaces); 153 resolver->resolve(detectedFaces);
136 m_serviceRequests.remove(resolver); 154 m_serviceRequests.remove(resolver);
137 } 155 }
138 156
139 DEFINE_TRACE(FaceDetector) { 157 DEFINE_TRACE(FaceDetector) {
140 visitor->trace(m_serviceRequests); 158 visitor->trace(m_serviceRequests);
141 } 159 }
142 160
143 } // namespace blink 161 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698