| OLD | NEW |
| 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/imagecapture/ImageCapture.h" | 5 #include "modules/imagecapture/ImageCapture.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/CallbackPromiseAdapter.h" | 7 #include "bindings/core/v8/CallbackPromiseAdapter.h" |
| 8 #include "bindings/core/v8/ScriptPromiseResolver.h" | 8 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 9 #include "core/dom/DOMException.h" | 9 #include "core/dom/DOMException.h" |
| 10 #include "core/dom/ExceptionCode.h" | 10 #include "core/dom/ExceptionCode.h" |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 return promise; | 124 return promise; |
| 125 } | 125 } |
| 126 m_serviceRequests.insert(resolver); | 126 m_serviceRequests.insert(resolver); |
| 127 | 127 |
| 128 // m_streamTrack->component()->source()->id() is the renderer "name" of the | 128 // m_streamTrack->component()->source()->id() is the renderer "name" of the |
| 129 // camera; | 129 // camera; |
| 130 // TODO(mcasas) consider sending the security origin as well: | 130 // TODO(mcasas) consider sending the security origin as well: |
| 131 // scriptState->getExecutionContext()->getSecurityOrigin()->toString() | 131 // scriptState->getExecutionContext()->getSecurityOrigin()->toString() |
| 132 m_service->GetCapabilities( | 132 m_service->GetCapabilities( |
| 133 m_streamTrack->component()->source()->id(), | 133 m_streamTrack->component()->source()->id(), |
| 134 convertToBaseCallback(WTF::bind(&ImageCapture::onCapabilities, | 134 convertToBaseCallback(WTF::bind(&ImageCapture::onPhotoCapabilities, |
| 135 wrapPersistent(this), | 135 wrapPersistent(this), |
| 136 wrapPersistent(resolver)))); | 136 wrapPersistent(resolver)))); |
| 137 return promise; | 137 return promise; |
| 138 } | 138 } |
| 139 | 139 |
| 140 ScriptPromise ImageCapture::setOptions(ScriptState* scriptState, | 140 ScriptPromise ImageCapture::setOptions(ScriptState* scriptState, |
| 141 const PhotoSettings& photoSettings) { | 141 const PhotoSettings& photoSettings) { |
| 142 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 142 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 143 ScriptPromise promise = resolver->promise(); | 143 ScriptPromise promise = resolver->promise(); |
| 144 | 144 |
| 145 if (trackIsInactive(*m_streamTrack)) { | 145 if (trackIsInactive(*m_streamTrack)) { |
| 146 resolver->reject(DOMException::create( | 146 resolver->reject(DOMException::create( |
| 147 InvalidStateError, "The associated Track is in an invalid state.")); | 147 InvalidStateError, "The associated Track is in an invalid state.")); |
| 148 return promise; | 148 return promise; |
| 149 } | 149 } |
| 150 | 150 |
| 151 if (!m_service) { | 151 if (!m_service) { |
| 152 resolver->reject(DOMException::create(NotFoundError, kNoServiceError)); | 152 resolver->reject(DOMException::create(NotFoundError, kNoServiceError)); |
| 153 return promise; | 153 return promise; |
| 154 } | 154 } |
| 155 m_serviceRequests.insert(resolver); | 155 m_serviceRequests.insert(resolver); |
| 156 | 156 |
| 157 // TODO(mcasas): should be using a mojo::StructTraits instead. | 157 // TODO(mcasas): should be using a mojo::StructTraits instead. |
| 158 auto settings = media::mojom::blink::PhotoSettings::New(); | 158 auto settings = media::mojom::blink::PhotoSettings::New(); |
| 159 | 159 |
| 160 settings->has_zoom = photoSettings.hasZoom(); | |
| 161 if (settings->has_zoom) | |
| 162 settings->zoom = photoSettings.zoom(); | |
| 163 settings->has_height = photoSettings.hasImageHeight(); | 160 settings->has_height = photoSettings.hasImageHeight(); |
| 164 if (settings->has_height) | 161 if (settings->has_height) |
| 165 settings->height = photoSettings.imageHeight(); | 162 settings->height = photoSettings.imageHeight(); |
| 166 settings->has_width = photoSettings.hasImageWidth(); | 163 settings->has_width = photoSettings.hasImageWidth(); |
| 167 if (settings->has_width) | 164 if (settings->has_width) |
| 168 settings->width = photoSettings.imageWidth(); | 165 settings->width = photoSettings.imageWidth(); |
| 169 settings->has_focus_mode = photoSettings.hasFocusMode(); | 166 |
| 170 if (settings->has_focus_mode) | |
| 171 settings->focus_mode = parseMeteringMode(photoSettings.focusMode()); | |
| 172 settings->has_exposure_mode = photoSettings.hasExposureMode(); | |
| 173 if (settings->has_exposure_mode) | |
| 174 settings->exposure_mode = parseMeteringMode(photoSettings.exposureMode()); | |
| 175 settings->has_exposure_compensation = photoSettings.hasExposureCompensation(); | |
| 176 if (settings->has_exposure_compensation) | |
| 177 settings->exposure_compensation = photoSettings.exposureCompensation(); | |
| 178 settings->has_white_balance_mode = photoSettings.hasWhiteBalanceMode(); | |
| 179 if (settings->has_white_balance_mode) | |
| 180 settings->white_balance_mode = | |
| 181 parseMeteringMode(photoSettings.whiteBalanceMode()); | |
| 182 settings->has_iso = photoSettings.hasIso(); | |
| 183 if (settings->has_iso) | |
| 184 settings->iso = photoSettings.iso(); | |
| 185 settings->has_red_eye_reduction = photoSettings.hasRedEyeReduction(); | 167 settings->has_red_eye_reduction = photoSettings.hasRedEyeReduction(); |
| 186 if (settings->has_red_eye_reduction) | 168 if (settings->has_red_eye_reduction) |
| 187 settings->red_eye_reduction = photoSettings.redEyeReduction(); | 169 settings->red_eye_reduction = photoSettings.redEyeReduction(); |
| 188 settings->has_fill_light_mode = photoSettings.hasFillLightMode(); | 170 settings->has_fill_light_mode = photoSettings.hasFillLightMode(); |
| 189 if (settings->has_fill_light_mode) | 171 if (settings->has_fill_light_mode) { |
| 190 settings->fill_light_mode = | 172 settings->fill_light_mode = |
| 191 parseFillLightMode(photoSettings.fillLightMode()); | 173 parseFillLightMode(photoSettings.fillLightMode()); |
| 192 if (photoSettings.hasPointsOfInterest()) { | |
| 193 for (const auto& point : photoSettings.pointsOfInterest()) { | |
| 194 auto mojoPoint = media::mojom::blink::Point2D::New(); | |
| 195 mojoPoint->x = point.x(); | |
| 196 mojoPoint->y = point.y(); | |
| 197 settings->points_of_interest.push_back(std::move(mojoPoint)); | |
| 198 } | |
| 199 } | 174 } |
| 200 settings->has_color_temperature = photoSettings.hasColorTemperature(); | |
| 201 if (settings->has_color_temperature) | |
| 202 settings->color_temperature = photoSettings.colorTemperature(); | |
| 203 settings->has_brightness = photoSettings.hasBrightness(); | |
| 204 if (settings->has_brightness) | |
| 205 settings->brightness = photoSettings.brightness(); | |
| 206 settings->has_contrast = photoSettings.hasContrast(); | |
| 207 if (settings->has_contrast) | |
| 208 settings->contrast = photoSettings.contrast(); | |
| 209 settings->has_saturation = photoSettings.hasSaturation(); | |
| 210 if (settings->has_saturation) | |
| 211 settings->saturation = photoSettings.saturation(); | |
| 212 settings->has_sharpness = photoSettings.hasSharpness(); | |
| 213 if (settings->has_sharpness) | |
| 214 settings->sharpness = photoSettings.sharpness(); | |
| 215 | 175 |
| 216 m_service->SetOptions(m_streamTrack->component()->source()->id(), | 176 m_service->SetOptions(m_streamTrack->component()->source()->id(), |
| 217 std::move(settings), | 177 std::move(settings), |
| 218 convertToBaseCallback(WTF::bind( | 178 convertToBaseCallback(WTF::bind( |
| 219 &ImageCapture::onSetOptions, wrapPersistent(this), | 179 &ImageCapture::onSetOptions, wrapPersistent(this), |
| 220 wrapPersistent(resolver)))); | 180 wrapPersistent(resolver)))); |
| 221 return promise; | 181 return promise; |
| 222 } | 182 } |
| 223 | 183 |
| 224 ScriptPromise ImageCapture::takePhoto(ScriptState* scriptState) { | 184 ScriptPromise ImageCapture::takePhoto(ScriptState* scriptState) { |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 } | 273 } |
| 314 | 274 |
| 315 settings->has_focus_mode = | 275 settings->has_focus_mode = |
| 316 constraints.hasFocusMode() && constraints.focusMode().isString(); | 276 constraints.hasFocusMode() && constraints.focusMode().isString(); |
| 317 if (settings->has_focus_mode) { | 277 if (settings->has_focus_mode) { |
| 318 m_currentConstraints.setFocusMode(constraints.focusMode()); | 278 m_currentConstraints.setFocusMode(constraints.focusMode()); |
| 319 settings->focus_mode = | 279 settings->focus_mode = |
| 320 parseMeteringMode(constraints.focusMode().getAsString()); | 280 parseMeteringMode(constraints.focusMode().getAsString()); |
| 321 } | 281 } |
| 322 | 282 |
| 283 // TODO(mcasas): support ConstrainPoint2DParameters. |
| 284 if (constraints.hasPointsOfInterest() && |
| 285 constraints.pointsOfInterest().isPoint2DSequence()) { |
| 286 for (const auto& point : |
| 287 constraints.pointsOfInterest().getAsPoint2DSequence()) { |
| 288 auto mojoPoint = media::mojom::blink::Point2D::New(); |
| 289 mojoPoint->x = point.x(); |
| 290 mojoPoint->y = point.y(); |
| 291 settings->points_of_interest.push_back(std::move(mojoPoint)); |
| 292 } |
| 293 } |
| 294 |
| 323 // TODO(mcasas): support ConstrainDoubleRange where applicable. | 295 // TODO(mcasas): support ConstrainDoubleRange where applicable. |
| 324 settings->has_exposure_compensation = | 296 settings->has_exposure_compensation = |
| 325 constraints.hasExposureCompensation() && | 297 constraints.hasExposureCompensation() && |
| 326 constraints.exposureCompensation().isDouble(); | 298 constraints.exposureCompensation().isDouble(); |
| 327 if (settings->has_exposure_compensation) { | 299 if (settings->has_exposure_compensation) { |
| 328 m_currentConstraints.setExposureCompensation( | 300 m_currentConstraints.setExposureCompensation( |
| 329 constraints.exposureCompensation()); | 301 constraints.exposureCompensation()); |
| 330 settings->exposure_compensation = | 302 settings->exposure_compensation = |
| 331 constraints.exposureCompensation().getAsDouble(); | 303 constraints.exposureCompensation().getAsDouble(); |
| 332 } | 304 } |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 432 &ImageCapture::onServiceConnectionError, wrapWeakPersistent(this)))); | 404 &ImageCapture::onServiceConnectionError, wrapWeakPersistent(this)))); |
| 433 | 405 |
| 434 // Launch a retrieval of the current capabilities, which arrive asynchronously | 406 // Launch a retrieval of the current capabilities, which arrive asynchronously |
| 435 // to avoid blocking the main UI thread. | 407 // to avoid blocking the main UI thread. |
| 436 m_service->GetCapabilities( | 408 m_service->GetCapabilities( |
| 437 m_streamTrack->component()->source()->id(), | 409 m_streamTrack->component()->source()->id(), |
| 438 convertToBaseCallback(WTF::bind(&ImageCapture::onCapabilitiesUpdate, | 410 convertToBaseCallback(WTF::bind(&ImageCapture::onCapabilitiesUpdate, |
| 439 wrapPersistent(this)))); | 411 wrapPersistent(this)))); |
| 440 } | 412 } |
| 441 | 413 |
| 442 void ImageCapture::onCapabilities( | 414 void ImageCapture::onPhotoCapabilities( |
| 443 ScriptPromiseResolver* resolver, | 415 ScriptPromiseResolver* resolver, |
| 444 media::mojom::blink::PhotoCapabilitiesPtr capabilities) { | 416 media::mojom::blink::PhotoCapabilitiesPtr capabilities) { |
| 445 if (!m_serviceRequests.contains(resolver)) | 417 if (!m_serviceRequests.contains(resolver)) |
| 446 return; | 418 return; |
| 447 if (capabilities.is_null()) { | 419 if (capabilities.is_null()) { |
| 448 resolver->reject(DOMException::create(UnknownError, "platform error")); | 420 resolver->reject(DOMException::create(UnknownError, "platform error")); |
| 449 } else { | 421 } else { |
| 450 // Update the local capabilities cache. | 422 // Update the local capabilities cache. |
| 451 onCapabilitiesUpdate(capabilities.Clone()); | 423 onCapabilitiesUpdate(capabilities.Clone()); |
| 452 | 424 |
| 453 PhotoCapabilities* caps = PhotoCapabilities::create(); | 425 PhotoCapabilities* caps = PhotoCapabilities::create(); |
| 454 // TODO(mcasas): Remove the explicit MediaSettingsRange::create() when | 426 // TODO(mcasas): Remove the explicit MediaSettingsRange::create() when |
| 455 // mojo::StructTraits supports garbage-collected mappings, | 427 // mojo::StructTraits supports garbage-collected mappings, |
| 456 // https://crbug.com/700180. | 428 // https://crbug.com/700180. |
| 457 caps->setIso(MediaSettingsRange::create(std::move(capabilities->iso))); | |
| 458 caps->setImageHeight( | 429 caps->setImageHeight( |
| 459 MediaSettingsRange::create(std::move(capabilities->height))); | 430 MediaSettingsRange::create(std::move(capabilities->height))); |
| 460 caps->setImageWidth( | 431 caps->setImageWidth( |
| 461 MediaSettingsRange::create(std::move(capabilities->width))); | 432 MediaSettingsRange::create(std::move(capabilities->width))); |
| 462 caps->setZoom(MediaSettingsRange::create(std::move(capabilities->zoom))); | 433 caps->setFillLightMode(capabilities->fill_light_mode); |
| 463 caps->setExposureCompensation(MediaSettingsRange::create( | 434 caps->setRedEyeReduction(capabilities->red_eye_reduction); |
| 464 std::move(capabilities->exposure_compensation))); | |
| 465 caps->setColorTemperature( | |
| 466 MediaSettingsRange::create(std::move(capabilities->color_temperature))); | |
| 467 caps->setBrightness( | |
| 468 MediaSettingsRange::create(std::move(capabilities->brightness))); | |
| 469 caps->setContrast( | |
| 470 MediaSettingsRange::create(std::move(capabilities->contrast))); | |
| 471 caps->setSaturation( | |
| 472 MediaSettingsRange::create(std::move(capabilities->saturation))); | |
| 473 caps->setSharpness( | |
| 474 MediaSettingsRange::create(std::move(capabilities->sharpness))); | |
| 475 | 435 |
| 476 caps->setFocusMode(capabilities->focus_mode); | |
| 477 caps->setExposureMode(capabilities->exposure_mode); | |
| 478 caps->setWhiteBalanceMode(capabilities->white_balance_mode); | |
| 479 caps->setFillLightMode(capabilities->fill_light_mode); | |
| 480 | |
| 481 caps->setRedEyeReduction(capabilities->red_eye_reduction); | |
| 482 resolver->resolve(caps); | 436 resolver->resolve(caps); |
| 483 } | 437 } |
| 484 m_serviceRequests.erase(resolver); | 438 m_serviceRequests.erase(resolver); |
| 485 } | 439 } |
| 486 | 440 |
| 487 void ImageCapture::onSetOptions(ScriptPromiseResolver* resolver, bool result) { | 441 void ImageCapture::onSetOptions(ScriptPromiseResolver* resolver, bool result) { |
| 488 if (!m_serviceRequests.contains(resolver)) | 442 if (!m_serviceRequests.contains(resolver)) |
| 489 return; | 443 return; |
| 490 | 444 |
| 491 if (!result) { | 445 if (!result) { |
| 492 resolver->reject(DOMException::create(UnknownError, "setOptions failed")); | 446 resolver->reject(DOMException::create(UnknownError, "setOptions failed")); |
| 493 m_serviceRequests.erase(resolver); | 447 m_serviceRequests.erase(resolver); |
| 494 return; | 448 return; |
| 495 } | 449 } |
| 496 | 450 |
| 497 // Retrieve the current device status after setting the options. | 451 // Retrieve the current device status after setting the options. |
| 498 m_service->GetCapabilities( | 452 m_service->GetCapabilities( |
| 499 m_streamTrack->component()->source()->id(), | 453 m_streamTrack->component()->source()->id(), |
| 500 convertToBaseCallback(WTF::bind(&ImageCapture::onCapabilities, | 454 convertToBaseCallback(WTF::bind(&ImageCapture::onPhotoCapabilities, |
| 501 wrapPersistent(this), | 455 wrapPersistent(this), |
| 502 wrapPersistent(resolver)))); | 456 wrapPersistent(resolver)))); |
| 503 } | 457 } |
| 504 | 458 |
| 505 void ImageCapture::onTakePhoto(ScriptPromiseResolver* resolver, | 459 void ImageCapture::onTakePhoto(ScriptPromiseResolver* resolver, |
| 506 media::mojom::blink::BlobPtr blob) { | 460 media::mojom::blink::BlobPtr blob) { |
| 507 if (!m_serviceRequests.contains(resolver)) | 461 if (!m_serviceRequests.contains(resolver)) |
| 508 return; | 462 return; |
| 509 | 463 |
| 510 // TODO(mcasas): Should be using a mojo::StructTraits. | 464 // TODO(mcasas): Should be using a mojo::StructTraits. |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 577 void ImageCapture::onServiceConnectionError() { | 531 void ImageCapture::onServiceConnectionError() { |
| 578 m_service.reset(); | 532 m_service.reset(); |
| 579 for (ScriptPromiseResolver* resolver : m_serviceRequests) | 533 for (ScriptPromiseResolver* resolver : m_serviceRequests) |
| 580 resolver->reject(DOMException::create(NotFoundError, kNoServiceError)); | 534 resolver->reject(DOMException::create(NotFoundError, kNoServiceError)); |
| 581 m_serviceRequests.clear(); | 535 m_serviceRequests.clear(); |
| 582 } | 536 } |
| 583 | 537 |
| 584 DEFINE_TRACE(ImageCapture) { | 538 DEFINE_TRACE(ImageCapture) { |
| 585 visitor->trace(m_streamTrack); | 539 visitor->trace(m_streamTrack); |
| 586 visitor->trace(m_capabilities); | 540 visitor->trace(m_capabilities); |
| 541 visitor->trace(m_currentConstraints); |
| 587 visitor->trace(m_serviceRequests); | 542 visitor->trace(m_serviceRequests); |
| 588 EventTargetWithInlineData::trace(visitor); | 543 EventTargetWithInlineData::trace(visitor); |
| 589 ContextLifecycleObserver::trace(visitor); | 544 ContextLifecycleObserver::trace(visitor); |
| 590 } | 545 } |
| 591 | 546 |
| 592 } // namespace blink | 547 } // namespace blink |
| OLD | NEW |