Commit 45f18e23 authored by Nektarios Paisios's avatar Nektarios Paisios Committed by Commit Bot

Sets the image annotation status when the service request has failed, cancelled, or is adult

Also ensures that an empty annotation will set the appropriate status

Bug: 905419
TBR: nasko@chromium.org
Change-Id: I09eb4ddc6489f015ff137564c89cbc88328e80cc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1506373
Commit-Queue: Nektarios Paisios <nektar@chromium.org>
Auto-Submit: Nektarios Paisios <nektar@chromium.org>
Reviewed-by: default avatarDominic Mazzoni <dmazzoni@chromium.org>
Cr-Commit-Position: refs/heads/master@{#638377}
parent c96b0b82
...@@ -49,6 +49,15 @@ std::string AXImageAnnotator::GetImageAnnotation( ...@@ -49,6 +49,15 @@ std::string AXImageAnnotator::GetImageAnnotation(
return std::string(); return std::string();
} }
ax::mojom::ImageAnnotationStatus AXImageAnnotator::GetImageAnnotationStatus(
blink::WebAXObject& image) const {
DCHECK(!image.IsDetached());
const auto lookup = image_annotations_.find(image.AxID());
if (lookup != image_annotations_.end())
return lookup->second.status();
return ax::mojom::ImageAnnotationStatus::kNone;
}
bool AXImageAnnotator::HasAnnotationInCache(blink::WebAXObject& image) const { bool AXImageAnnotator::HasAnnotationInCache(blink::WebAXObject& image) const {
DCHECK(!image.IsDetached()); DCHECK(!image.IsDetached());
if (!HasImageInCache(image)) if (!HasImageInCache(image))
...@@ -157,6 +166,7 @@ void AXImageAnnotator::MarkAllImagesDirty() { ...@@ -157,6 +166,7 @@ void AXImageAnnotator::MarkAllImagesDirty() {
AXImageAnnotator::ImageInfo::ImageInfo(const blink::WebAXObject& image) AXImageAnnotator::ImageInfo::ImageInfo(const blink::WebAXObject& image)
: image_processor_( : image_processor_(
base::BindRepeating(&AXImageAnnotator::GetImageData, image)), base::BindRepeating(&AXImageAnnotator::GetImageData, image)),
status_(ax::mojom::ImageAnnotationStatus::kAnnotationPending),
annotation_(base::nullopt) {} annotation_(base::nullopt) {}
AXImageAnnotator::ImageInfo::~ImageInfo() = default; AXImageAnnotator::ImageInfo::~ImageInfo() = default;
...@@ -167,7 +177,24 @@ AXImageAnnotator::ImageInfo::GetImageProcessor() { ...@@ -167,7 +177,24 @@ AXImageAnnotator::ImageInfo::GetImageProcessor() {
} }
bool AXImageAnnotator::ImageInfo::HasAnnotation() const { bool AXImageAnnotator::ImageInfo::HasAnnotation() const {
return annotation_.has_value(); switch (status_) {
case ax::mojom::ImageAnnotationStatus::kNone:
case ax::mojom::ImageAnnotationStatus::kIneligibleForAnnotation:
// The user hasn't requested an annotation yet, or a previously pending
// annotation request had been cancelled.
case ax::mojom::ImageAnnotationStatus::kEligibleForAnnotation:
case ax::mojom::ImageAnnotationStatus::kAnnotationPending:
return false;
case ax::mojom::ImageAnnotationStatus::kAnnotationSucceeded:
DCHECK(annotation_.has_value());
return true;
case ax::mojom::ImageAnnotationStatus::kAnnotationEmpty:
// Image has been classified as adult content.
case ax::mojom::ImageAnnotationStatus::kAnnotationAdult:
case ax::mojom::ImageAnnotationStatus::kAnnotationProcessFailed:
DCHECK(!annotation_.has_value());
return true;
}
} }
// static // static
...@@ -186,16 +213,43 @@ SkBitmap AXImageAnnotator::GetImageData(const blink::WebAXObject& image) { ...@@ -186,16 +213,43 @@ SkBitmap AXImageAnnotator::GetImageData(const blink::WebAXObject& image) {
void AXImageAnnotator::OnImageAnnotated( void AXImageAnnotator::OnImageAnnotated(
const blink::WebAXObject& image, const blink::WebAXObject& image,
image_annotation::mojom::AnnotateImageResultPtr result) { image_annotation::mojom::AnnotateImageResultPtr result) {
if (image.IsDetached())
return;
if (!base::ContainsKey(image_annotations_, image.AxID())) if (!base::ContainsKey(image_annotations_, image.AxID()))
return; return;
// TODO(nektar): Set the image annotation status on this image to Error.
if (result->is_error_code()) if (image.IsDetached()) {
image_annotations_.at(image.AxID())
.set_status(ax::mojom::ImageAnnotationStatus::kIneligibleForAnnotation);
// We should not mark dirty a detached object.
return; return;
}
if (result->is_error_code()) {
DLOG(WARNING) << "Image annotation error.";
switch (result->get_error_code()) {
case image_annotation::mojom::AnnotateImageError::kCanceled:
image_annotations_.at(image.AxID())
.set_status(
ax::mojom::ImageAnnotationStatus::kEligibleForAnnotation);
break;
case image_annotation::mojom::AnnotateImageError::kFailure:
image_annotations_.at(image.AxID())
.set_status(
ax::mojom::ImageAnnotationStatus::kAnnotationProcessFailed);
break;
case image_annotation::mojom::AnnotateImageError::kAdult:
image_annotations_.at(image.AxID())
.set_status(ax::mojom::ImageAnnotationStatus::kAnnotationAdult);
break;
}
render_accessibility_->MarkWebAXObjectDirty(image, false /* subtree */);
return;
}
if (!result->is_annotations()) { if (!result->is_annotations()) {
DLOG(WARNING) << "No image annotation results."; DLOG(WARNING) << "No image annotation results.";
image_annotations_.at(image.AxID())
.set_status(ax::mojom::ImageAnnotationStatus::kAnnotationEmpty);
render_accessibility_->MarkWebAXObjectDirty(image, false /* subtree */);
return; return;
} }
...@@ -229,9 +283,15 @@ void AXImageAnnotator::OnImageAnnotated( ...@@ -229,9 +283,15 @@ void AXImageAnnotator::OnImageAnnotated(
} }
} }
if (contextualized_strings.empty()) if (contextualized_strings.empty()) {
image_annotations_.at(image.AxID())
.set_status(ax::mojom::ImageAnnotationStatus::kAnnotationEmpty);
render_accessibility_->MarkWebAXObjectDirty(image, false /* subtree */);
return; return;
}
image_annotations_.at(image.AxID())
.set_status(ax::mojom::ImageAnnotationStatus::kAnnotationSucceeded);
// TODO(accessibility): join two sentences together in a more i18n-friendly // TODO(accessibility): join two sentences together in a more i18n-friendly
// way. Since this is intended for a screen reader, though, a period // way. Since this is intended for a screen reader, though, a period
// probably works in almost all languages. // probably works in almost all languages.
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include "services/image_annotation/public/cpp/image_processor.h" #include "services/image_annotation/public/cpp/image_processor.h"
#include "services/image_annotation/public/mojom/image_annotation.mojom.h" #include "services/image_annotation/public/mojom/image_annotation.mojom.h"
#include "third_party/skia/include/core/SkBitmap.h" #include "third_party/skia/include/core/SkBitmap.h"
#include "ui/accessibility/ax_enums.mojom.h"
namespace blink { namespace blink {
...@@ -42,6 +43,8 @@ class CONTENT_EXPORT AXImageAnnotator : public base::CheckedObserver { ...@@ -42,6 +43,8 @@ class CONTENT_EXPORT AXImageAnnotator : public base::CheckedObserver {
void Destroy(); void Destroy();
std::string GetImageAnnotation(blink::WebAXObject& image) const; std::string GetImageAnnotation(blink::WebAXObject& image) const;
ax::mojom::ImageAnnotationStatus GetImageAnnotationStatus(
blink::WebAXObject& image) const;
bool HasAnnotationInCache(blink::WebAXObject& image) const; bool HasAnnotationInCache(blink::WebAXObject& image) const;
bool HasImageInCache(const blink::WebAXObject& image) const; bool HasImageInCache(const blink::WebAXObject& image) const;
...@@ -59,6 +62,13 @@ class CONTENT_EXPORT AXImageAnnotator : public base::CheckedObserver { ...@@ -59,6 +62,13 @@ class CONTENT_EXPORT AXImageAnnotator : public base::CheckedObserver {
image_annotation::mojom::ImageProcessorPtr GetImageProcessor(); image_annotation::mojom::ImageProcessorPtr GetImageProcessor();
bool HasAnnotation() const; bool HasAnnotation() const;
ax::mojom::ImageAnnotationStatus status() const { return status_; }
void set_status(ax::mojom::ImageAnnotationStatus status) {
DCHECK_NE(status, ax::mojom::ImageAnnotationStatus::kNone);
status_ = status;
}
std::string annotation() const { std::string annotation() const {
DCHECK(annotation_.has_value()); DCHECK(annotation_.has_value());
return annotation_.value_or(""); return annotation_.value_or("");
...@@ -68,6 +78,7 @@ class CONTENT_EXPORT AXImageAnnotator : public base::CheckedObserver { ...@@ -68,6 +78,7 @@ class CONTENT_EXPORT AXImageAnnotator : public base::CheckedObserver {
private: private:
image_annotation::ImageProcessor image_processor_; image_annotation::ImageProcessor image_processor_;
ax::mojom::ImageAnnotationStatus status_;
base::Optional<std::string> annotation_; base::Optional<std::string> annotation_;
}; };
......
...@@ -1062,6 +1062,8 @@ void BlinkAXTreeSource::AddImageAnnotations(blink::WebAXObject src, ...@@ -1062,6 +1062,8 @@ void BlinkAXTreeSource::AddImageAnnotations(blink::WebAXObject src,
// unloaded images where the size is unknown. // unloaded images where the size is unknown.
if (dst->relative_bounds.bounds.width() < kMinImageAnnotationWidth || if (dst->relative_bounds.bounds.width() < kMinImageAnnotationWidth ||
dst->relative_bounds.bounds.height() < kMinImageAnnotationHeight) { dst->relative_bounds.bounds.height() < kMinImageAnnotationHeight) {
dst->SetImageAnnotationStatus(
ax::mojom::ImageAnnotationStatus::kIneligibleForAnnotation);
return; return;
} }
...@@ -1075,7 +1077,7 @@ void BlinkAXTreeSource::AddImageAnnotations(blink::WebAXObject src, ...@@ -1075,7 +1077,7 @@ void BlinkAXTreeSource::AddImageAnnotations(blink::WebAXObject src,
dst->AddStringAttribute(ax::mojom::StringAttribute::kImageAnnotation, dst->AddStringAttribute(ax::mojom::StringAttribute::kImageAnnotation,
image_annotator_->GetImageAnnotation(src)); image_annotator_->GetImageAnnotation(src));
dst->SetImageAnnotationStatus( dst->SetImageAnnotationStatus(
ax::mojom::ImageAnnotationStatus::kAnnotationSucceeded); image_annotator_->GetImageAnnotationStatus(src));
} else if (image_annotator_->HasImageInCache(src)) { } else if (image_annotator_->HasImageInCache(src)) {
image_annotator_->OnImageUpdated(src); image_annotator_->OnImageUpdated(src);
dst->SetImageAnnotationStatus( dst->SetImageAnnotationStatus(
......
...@@ -558,6 +558,9 @@ enum IntAttribute { ...@@ -558,6 +558,9 @@ enum IntAttribute {
kHasPopup, kHasPopup,
// Image annotation status, of type ImageAnnotationStatus.
kImageAnnotationStatus,
// Indicates if a form control has invalid input or // Indicates if a form control has invalid input or
// if an element has an aria-invalid attribute. // if an element has an aria-invalid attribute.
kInvalidState, kInvalidState,
...@@ -577,9 +580,6 @@ enum IntAttribute { ...@@ -577,9 +580,6 @@ enum IntAttribute {
// Focus traversal in views and Android. // Focus traversal in views and Android.
kPreviousFocusId, kPreviousFocusId,
kNextFocusId, kNextFocusId,
// Image annotation status, of type ImageAnnotationStatus.
kImageAnnotationStatus,
}; };
enum FloatAttribute { enum FloatAttribute {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment