OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "extensions/renderer/api/automation/automation_api_helper.h" |
| 6 |
| 7 #include "content/public/renderer/render_view.h" |
| 8 #include "extensions/common/extension_messages.h" |
| 9 #include "third_party/WebKit/public/web/WebAXObject.h" |
| 10 #include "third_party/WebKit/public/web/WebDocument.h" |
| 11 #include "third_party/WebKit/public/web/WebElement.h" |
| 12 #include "third_party/WebKit/public/web/WebExceptionCode.h" |
| 13 #include "third_party/WebKit/public/web/WebFrame.h" |
| 14 #include "third_party/WebKit/public/web/WebNode.h" |
| 15 #include "third_party/WebKit/public/web/WebView.h" |
| 16 |
| 17 namespace extensions { |
| 18 |
| 19 AutomationApiHelper::AutomationApiHelper(content::RenderView* render_view) |
| 20 : content::RenderViewObserver(render_view) { |
| 21 } |
| 22 |
| 23 AutomationApiHelper::~AutomationApiHelper() { |
| 24 } |
| 25 |
| 26 bool AutomationApiHelper::OnMessageReceived(const IPC::Message& message) { |
| 27 bool handled = true; |
| 28 IPC_BEGIN_MESSAGE_MAP(AutomationApiHelper, message) |
| 29 IPC_MESSAGE_HANDLER(ExtensionMsg_AutomationQuerySelector, OnQuerySelector) |
| 30 IPC_MESSAGE_UNHANDLED(handled = false) |
| 31 IPC_END_MESSAGE_MAP() |
| 32 return handled; |
| 33 } |
| 34 |
| 35 void AutomationApiHelper::OnQuerySelector(int request_id, |
| 36 int acc_obj_id, |
| 37 const base::string16& selector) { |
| 38 if (!render_view() || !render_view()->GetWebView() || |
| 39 !render_view()->GetWebView()->mainFrame()) { |
| 40 Send(new ExtensionHostMsg_AutomationQuerySelector_Result( |
| 41 routing_id(), request_id, "No main frame", 0)); |
| 42 return; |
| 43 } |
| 44 blink::WebDocument document = |
| 45 render_view()->GetWebView()->mainFrame()->document(); |
| 46 if (document.isNull()) { |
| 47 Send(new ExtensionHostMsg_AutomationQuerySelector_Result( |
| 48 routing_id(), request_id, "No document", 0)); |
| 49 return; |
| 50 } |
| 51 blink::WebNode node = document; |
| 52 if (acc_obj_id > 0) { |
| 53 blink::WebAXObject obj = document.accessibilityObjectFromID(acc_obj_id); |
| 54 if (obj.isNull()) { |
| 55 Send(new ExtensionHostMsg_AutomationQuerySelector_Result( |
| 56 routing_id(), request_id, |
| 57 "querySelector sent on node which is no longer in the tree", 0)); |
| 58 return; |
| 59 } |
| 60 |
| 61 node = obj.node(); |
| 62 while (node.isNull()) { |
| 63 obj = obj.parentObject(); |
| 64 node = obj.node(); |
| 65 } |
| 66 } |
| 67 blink::WebString web_selector(selector); |
| 68 blink::WebExceptionCode ec = 0; |
| 69 blink::WebElement result = node.querySelector(web_selector, ec); |
| 70 int result_acc_obj_id = 0; |
| 71 if (!ec && !result.isNull()) { |
| 72 blink::WebAXObject ax_obj = result.accessibilityObject(); |
| 73 if (!ax_obj.isDetached()) { |
| 74 while (ax_obj.accessibilityIsIgnored()) |
| 75 ax_obj = ax_obj.parentObject(); |
| 76 |
| 77 result_acc_obj_id = ax_obj.axID(); |
| 78 } |
| 79 } |
| 80 Send(new ExtensionHostMsg_AutomationQuerySelector_Result( |
| 81 routing_id(), request_id, "", result_acc_obj_id)); |
| 82 } |
| 83 |
| 84 } // namespace extensions |
OLD | NEW |