Commit 11608e6c authored by Mike Dougherty's avatar Mike Dougherty Committed by Commit Bot

iOS: Add FindElementAtPoint to context_menu.js.

FindElementAtPoint asynchronously returns the DOM element details using
postMessage which can be used to communicate with the native app from
any document frame.

Bug: 228355
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: I52c6e08f168824f0f458b97965e627d75a93ffa6
Reviewed-on: https://chromium-review.googlesource.com/918404
Commit-Queue: Mike Dougherty <michaeldo@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#537914}
parent f2432a4c
......@@ -8,6 +8,8 @@
#import <Foundation/Foundation.h>
#import <WebKit/WebKit.h>
#include "base/compiler_specific.h"
@class CRWJSInjectionManager;
@class CRWJSInjectionReceiver;
......@@ -35,6 +37,12 @@ id ExecuteJavaScript(WKWebView* web_view,
// Fails if there was an error during script execution.
id ExecuteJavaScript(WKWebView* web_view, NSString* script);
// Synchronously loads |html| into |web_view|. Returns true is successful or
// false if the |web_view| never finishes loading.
bool LoadHtml(WKWebView* web_view,
NSString* html,
NSURL* base_url) WARN_UNUSED_RESULT;
} // namespace web
#endif // IOS_WEB_PUBLIC_TEST_JS_TEST_UTIL_H_
......@@ -17,6 +17,10 @@
#error "This file requires ARC support."
#endif
using testing::kWaitForJSCompletionTimeout;
using testing::kWaitForPageLoadTimeout;
using testing::WaitUntilConditionOrTimeout;
namespace web {
id ExecuteJavaScript(CRWJSInjectionManager* manager, NSString* script) {
......@@ -29,10 +33,9 @@ id ExecuteJavaScript(CRWJSInjectionManager* manager, NSString* script) {
completed = true;
}];
BOOL success = testing::WaitUntilConditionOrTimeout(
testing::kWaitForJSCompletionTimeout, ^{
return completed;
});
BOOL success = WaitUntilConditionOrTimeout(kWaitForJSCompletionTimeout, ^{
return completed;
});
// Log stack trace to provide some context.
EXPECT_TRUE(success)
<< "CRWJSInjectionManager failed to complete javascript execution.\n"
......@@ -64,10 +67,9 @@ id ExecuteJavaScript(WKWebView* web_view,
block_error = [script_error copy];
completed = true;
}];
BOOL success = testing::WaitUntilConditionOrTimeout(
testing::kWaitForJSCompletionTimeout, ^{
return completed;
});
BOOL success = WaitUntilConditionOrTimeout(kWaitForJSCompletionTimeout, ^{
return completed;
});
// Log stack trace to provide some context.
EXPECT_TRUE(success) << "WKWebView failed to complete javascript execution.\n"
<< base::SysNSStringToUTF8([[NSThread callStackSymbols]
......@@ -78,5 +80,13 @@ id ExecuteJavaScript(WKWebView* web_view,
return result;
}
bool LoadHtml(WKWebView* web_view, NSString* html, NSURL* base_url) {
[web_view loadHTMLString:html baseURL:base_url];
return WaitUntilConditionOrTimeout(kWaitForPageLoadTimeout, ^{
return !web_view.loading;
});
}
} // namespace web
......@@ -8,10 +8,16 @@
#import <Foundation/Foundation.h>
// Contains keys present in dictionary returned by __gCrWeb.getElementFromPoint
// JS API.
// and __gCrWeb.findElementAtPoint JS APIs.
namespace web {
// Required in findElementAtPoint response. (Not used by getElementFromPoint.)
// Represents a unique string request ID that is passed through directly from a
// call to findElementAtPoint to the response dictionary. The request ID should
// be used to correlate a response with a previous call to findElementAtPoint.
extern NSString* const kContextMenuElementRequestID;
// Optional key. Represents element's href attribute if present or parent's href
// if element is an image.
extern NSString* const kContextMenuElementHyperlink;
......
......@@ -10,6 +10,7 @@
namespace web {
NSString* const kContextMenuElementRequestID = @"requestID";
NSString* const kContextMenuElementHyperlink = @"href";
NSString* const kContextMenuElementSource = @"src";
NSString* const kContextMenuElementTitle = @"title";
......
......@@ -11,6 +11,44 @@ goog.provide('__crWeb.contextMenu');
/** Beginning of anonymous object */
(function() {
/**
* Finds the url of the image or link under the selected point. Sends the
* found element (or an empty object if no links or images are found) back to
* the application by posting a 'FindElementResultHandler' message.
* The object returned in the message is an object of the form {
* requestID, // identifier as passed to this function
* href, // URL of the link under the point
* innerText, // innerText of the link, if the selected element is a link
* src, // src of the image, if the selected element is an image
* title, // title of the image, if the selected
* referrerPolicy
* }
* where:
* <ul>
* <li>requestID is the value of the identifier passed to this function.
* <li>href, innerText are set if the selected element is a link.
* <li>src, title are set if the selected element is an image.
* <li>href is also set if the selected element is an image with a link.
* <li>referrerPolicy is the referrer policy to use for navigations away
* from the current page.
* </ul>
* @param {string} identifier An identifier which be returned in the result
* dictionary of this request.
* @param {number} x Horizontal center of the selected point in web view
* coordinates.
* @param {number} y Vertical center of the selected point in web view
* coordinates.
* @param {number} webViewWidth the width of web view.
* @param {number} webViewHeight the height of web view.
*/
__gCrWeb['findElementAtPoint'] =
function(requestID, x, y, webViewWidth, webViewHeight) {
var scale = getPageWidth() / webViewWidth;
var result = getElementFromPointInPageCoordinates(x * scale, y * scale);
result.requestID = requestID;
__gCrWeb.common.sendWebKitMessage('FindElementResultHandler', result);
};
/**
* Returns the url of the image or link under the selected point. Returns an
* empty object if no links or images are found.
......
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