This source file includes following definitions.
- ack_pending_
- OnMessageReceived
- FocusedNodeChanged
- DidFinishLoad
- HandleWebAccessibilityEvent
- HandleAXEvent
- GetType
- SendPendingAccessibilityEvents
- SendLocationChanges
- OnDoDefaultAction
- OnScrollToMakeVisible
- OnScrollToPoint
- OnSetTextSelection
- OnEventsAck
- OnSetFocus
- OnFatalError
#include "content/renderer/accessibility/renderer_accessibility_complete.h"
#include <queue>
#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "content/renderer/accessibility/blink_ax_enum_conversion.h"
#include "content/renderer/render_view_impl.h"
#include "third_party/WebKit/public/web/WebAXObject.h"
#include "third_party/WebKit/public/web/WebDocument.h"
#include "third_party/WebKit/public/web/WebFrame.h"
#include "third_party/WebKit/public/web/WebInputElement.h"
#include "third_party/WebKit/public/web/WebNode.h"
#include "third_party/WebKit/public/web/WebView.h"
#include "ui/accessibility/ax_tree.h"
using blink::WebAXObject;
using blink::WebDocument;
using blink::WebFrame;
using blink::WebNode;
using blink::WebPoint;
using blink::WebRect;
using blink::WebSize;
using blink::WebView;
namespace content {
RendererAccessibilityComplete::RendererAccessibilityComplete(
RenderViewImpl* render_view)
: RendererAccessibility(render_view),
weak_factory_(this),
tree_source_(render_view),
serializer_(&tree_source_),
last_scroll_offset_(gfx::Size()),
ack_pending_(false) {
WebAXObject::enableAccessibility();
#if !defined(OS_ANDROID)
WebAXObject::enableInlineTextBoxAccessibility();
#endif
const WebDocument& document = GetMainDocument();
if (!document.isNull()) {
HandleAXEvent(document.accessibilityObject(),
ui::AX_EVENT_LAYOUT_COMPLETE);
}
}
RendererAccessibilityComplete::~RendererAccessibilityComplete() {
}
bool RendererAccessibilityComplete::OnMessageReceived(
const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(RendererAccessibilityComplete, message)
IPC_MESSAGE_HANDLER(AccessibilityMsg_SetFocus, OnSetFocus)
IPC_MESSAGE_HANDLER(AccessibilityMsg_DoDefaultAction,
OnDoDefaultAction)
IPC_MESSAGE_HANDLER(AccessibilityMsg_Events_ACK,
OnEventsAck)
IPC_MESSAGE_HANDLER(AccessibilityMsg_ScrollToMakeVisible,
OnScrollToMakeVisible)
IPC_MESSAGE_HANDLER(AccessibilityMsg_ScrollToPoint,
OnScrollToPoint)
IPC_MESSAGE_HANDLER(AccessibilityMsg_SetTextSelection,
OnSetTextSelection)
IPC_MESSAGE_HANDLER(AccessibilityMsg_FatalError, OnFatalError)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}
void RendererAccessibilityComplete::FocusedNodeChanged(const WebNode& node) {
const WebDocument& document = GetMainDocument();
if (document.isNull())
return;
if (node.isNull()) {
HandleAXEvent(document.accessibilityObject(), ui::AX_EVENT_BLUR);
}
}
void RendererAccessibilityComplete::DidFinishLoad(blink::WebLocalFrame* frame) {
const WebDocument& document = GetMainDocument();
if (document.isNull())
return;
}
void RendererAccessibilityComplete::HandleWebAccessibilityEvent(
const blink::WebAXObject& obj, blink::WebAXEvent event) {
HandleAXEvent(obj, AXEventFromBlink(event));
}
void RendererAccessibilityComplete::HandleAXEvent(
const blink::WebAXObject& obj, ui::AXEvent event) {
const WebDocument& document = GetMainDocument();
if (document.isNull())
return;
gfx::Size scroll_offset = document.frame()->scrollOffset();
if (scroll_offset != last_scroll_offset_) {
last_scroll_offset_ = scroll_offset;
if (!obj.equals(document.accessibilityObject())) {
HandleAXEvent(document.accessibilityObject(),
ui::AX_EVENT_LAYOUT_COMPLETE);
}
}
AccessibilityHostMsg_EventParams acc_event;
acc_event.id = obj.axID();
acc_event.event_type = event;
for (uint32 i = 0; i < pending_events_.size(); ++i) {
if (pending_events_[i].id == acc_event.id &&
pending_events_[i].event_type ==
acc_event.event_type) {
return;
}
}
pending_events_.push_back(acc_event);
if (!ack_pending_ && !weak_factory_.HasWeakPtrs()) {
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&RendererAccessibilityComplete::
SendPendingAccessibilityEvents,
weak_factory_.GetWeakPtr()));
}
}
RendererAccessibilityType RendererAccessibilityComplete::GetType() {
return RendererAccessibilityTypeComplete;
}
void RendererAccessibilityComplete::SendPendingAccessibilityEvents() {
const WebDocument& document = GetMainDocument();
if (document.isNull())
return;
if (pending_events_.empty())
return;
if (render_view_->is_swapped_out())
return;
ack_pending_ = true;
std::vector<AccessibilityHostMsg_EventParams> src_events =
pending_events_;
pending_events_.clear();
std::vector<AccessibilityHostMsg_EventParams> event_msgs;
for (size_t i = 0; i < src_events.size(); ++i) {
AccessibilityHostMsg_EventParams& event =
src_events[i];
WebAXObject obj = document.accessibilityObjectFromID(
event.id);
if (!obj.updateBackingStoreAndCheckValidity())
continue;
if (event.event_type ==
ui::AX_EVENT_SELECTED_CHILDREN_CHANGED) {
serializer_.DeleteClientSubtree(obj);
}
obj.startCachingComputedObjectAttributesUntilTreeMutates();
AccessibilityHostMsg_EventParams event_msg;
event_msg.event_type = event.event_type;
event_msg.id = event.id;
serializer_.SerializeChanges(obj, &event_msg.update);
event_msgs.push_back(event_msg);
#ifndef NDEBUG
ui::AXTree tree;
tree.Unserialize(event_msg.update);
VLOG(0) << "Accessibility update: \n"
<< "routing id=" << routing_id()
<< " event="
<< AccessibilityEventToString(event.event_type)
<< "\n" << tree.ToString();
#endif
}
Send(new AccessibilityHostMsg_Events(routing_id(), event_msgs));
SendLocationChanges();
}
void RendererAccessibilityComplete::SendLocationChanges() {
std::vector<AccessibilityHostMsg_LocationChangeParams> messages;
base::hash_map<int, gfx::Rect> new_locations;
std::queue<WebAXObject> objs_to_explore;
objs_to_explore.push(tree_source_.GetRoot());
while (objs_to_explore.size()) {
WebAXObject obj = objs_to_explore.front();
objs_to_explore.pop();
int id = obj.axID();
base::hash_map<int, gfx::Rect>::iterator iter = locations_.find(id);
if (iter == locations_.end())
continue;
gfx::Rect new_location = obj.boundingBoxRect();
if (iter != locations_.end() && iter->second != new_location) {
AccessibilityHostMsg_LocationChangeParams message;
message.id = id;
message.new_location = new_location;
messages.push_back(message);
}
new_locations[id] = new_location;
}
locations_.swap(new_locations);
Send(new AccessibilityHostMsg_LocationChanges(routing_id(), messages));
}
void RendererAccessibilityComplete::OnDoDefaultAction(int acc_obj_id) {
const WebDocument& document = GetMainDocument();
if (document.isNull())
return;
WebAXObject obj = document.accessibilityObjectFromID(acc_obj_id);
if (obj.isDetached()) {
#ifndef NDEBUG
LOG(WARNING) << "DoDefaultAction on invalid object id " << acc_obj_id;
#endif
return;
}
obj.performDefaultAction();
}
void RendererAccessibilityComplete::OnScrollToMakeVisible(
int acc_obj_id, gfx::Rect subfocus) {
const WebDocument& document = GetMainDocument();
if (document.isNull())
return;
WebAXObject obj = document.accessibilityObjectFromID(acc_obj_id);
if (obj.isDetached()) {
#ifndef NDEBUG
LOG(WARNING) << "ScrollToMakeVisible on invalid object id " << acc_obj_id;
#endif
return;
}
obj.scrollToMakeVisibleWithSubFocus(
WebRect(subfocus.x(), subfocus.y(),
subfocus.width(), subfocus.height()));
HandleAXEvent(document.accessibilityObject(),
ui::AX_EVENT_LAYOUT_COMPLETE);
}
void RendererAccessibilityComplete::OnScrollToPoint(
int acc_obj_id, gfx::Point point) {
const WebDocument& document = GetMainDocument();
if (document.isNull())
return;
WebAXObject obj = document.accessibilityObjectFromID(acc_obj_id);
if (obj.isDetached()) {
#ifndef NDEBUG
LOG(WARNING) << "ScrollToPoint on invalid object id " << acc_obj_id;
#endif
return;
}
obj.scrollToGlobalPoint(WebPoint(point.x(), point.y()));
HandleAXEvent(document.accessibilityObject(),
ui::AX_EVENT_LAYOUT_COMPLETE);
}
void RendererAccessibilityComplete::OnSetTextSelection(
int acc_obj_id, int start_offset, int end_offset) {
const WebDocument& document = GetMainDocument();
if (document.isNull())
return;
WebAXObject obj = document.accessibilityObjectFromID(acc_obj_id);
if (obj.isDetached()) {
#ifndef NDEBUG
LOG(WARNING) << "SetTextSelection on invalid object id " << acc_obj_id;
#endif
return;
}
blink::WebNode node = obj.node();
if (!node.isNull() && node.isElementNode()) {
blink::WebElement element = node.to<blink::WebElement>();
blink::WebInputElement* input_element =
blink::toWebInputElement(&element);
if (input_element && input_element->isTextField())
input_element->setSelectionRange(start_offset, end_offset);
}
}
void RendererAccessibilityComplete::OnEventsAck() {
DCHECK(ack_pending_);
ack_pending_ = false;
SendPendingAccessibilityEvents();
}
void RendererAccessibilityComplete::OnSetFocus(int acc_obj_id) {
const WebDocument& document = GetMainDocument();
if (document.isNull())
return;
WebAXObject obj = document.accessibilityObjectFromID(acc_obj_id);
if (obj.isDetached()) {
#ifndef NDEBUG
LOG(WARNING) << "OnSetAccessibilityFocus on invalid object id "
<< acc_obj_id;
#endif
return;
}
WebAXObject root = document.accessibilityObject();
if (root.isDetached()) {
#ifndef NDEBUG
LOG(WARNING) << "OnSetAccessibilityFocus but root is invalid";
#endif
return;
}
if (acc_obj_id == root.axID())
render_view()->GetWebView()->clearFocusedElement();
else
obj.setFocused(true);
}
void RendererAccessibilityComplete::OnFatalError() {
CHECK(false) << "Invalid accessibility tree.";
}
}