[ios] Removed deprecated JS execution API.
BUG=595761 Review-Url: https://codereview.chromium.org/2281803002 Cr-Commit-Position: refs/heads/master@{#415406}
This commit is contained in:
ios/web
interstitials
html_web_interstitial_impl.hhtml_web_interstitial_impl.mmnative_web_interstitial_impl.hnative_web_interstitial_impl.mmweb_interstitial_impl.h
public
web_state
@ -39,8 +39,6 @@ class HtmlWebInterstitialImpl : public WebInterstitialImpl {
|
||||
// WebInterstitialImpl implementation:
|
||||
void PrepareForDisplay() override;
|
||||
WebInterstitialDelegate* GetDelegate() const override;
|
||||
void EvaluateJavaScript(NSString* script,
|
||||
JavaScriptCompletion completionHandler) override;
|
||||
void ExecuteJavaScript(NSString* script,
|
||||
JavaScriptResultBlock completion_handler) override;
|
||||
|
||||
|
@ -129,12 +129,6 @@ WebInterstitialDelegate* HtmlWebInterstitialImpl::GetDelegate() const {
|
||||
return delegate_.get();
|
||||
}
|
||||
|
||||
void HtmlWebInterstitialImpl::EvaluateJavaScript(
|
||||
NSString* script,
|
||||
JavaScriptCompletion completionHandler) {
|
||||
web::EvaluateJavaScript(web_view_, script, completionHandler);
|
||||
}
|
||||
|
||||
void HtmlWebInterstitialImpl::ExecuteJavaScript(
|
||||
NSString* script,
|
||||
JavaScriptResultBlock completion_handler) {
|
||||
|
@ -34,8 +34,6 @@ class NativeWebInterstitialImpl : public WebInterstitialImpl {
|
||||
// WebInterstitialImpl implementation:
|
||||
void PrepareForDisplay() override;
|
||||
WebInterstitialDelegate* GetDelegate() const override;
|
||||
void EvaluateJavaScript(NSString* script,
|
||||
JavaScriptCompletion completionHandler) override;
|
||||
void ExecuteJavaScript(NSString* script,
|
||||
JavaScriptResultBlock completion_handler) override;
|
||||
|
||||
|
@ -57,12 +57,6 @@ WebInterstitialDelegate* NativeWebInterstitialImpl::GetDelegate() const {
|
||||
return delegate_.get();
|
||||
}
|
||||
|
||||
void NativeWebInterstitialImpl::EvaluateJavaScript(
|
||||
NSString* script,
|
||||
JavaScriptCompletion completionHandler) {
|
||||
NOTREACHED() << "JavaScript cannot be evaluated on native interstitials.";
|
||||
}
|
||||
|
||||
void NativeWebInterstitialImpl::ExecuteJavaScript(
|
||||
NSString* script,
|
||||
JavaScriptResultBlock completion_handler) {
|
||||
|
@ -21,13 +21,6 @@ class WebInterstitialFacadeDelegate;
|
||||
class WebInterstitialImpl;
|
||||
class WebStateImpl;
|
||||
|
||||
// May be implemented in tests to run JavaScript on interstitials. This function
|
||||
// has access to private EvaluateJavaScript method to be used for testing.
|
||||
// DEPRECATED. TODO(crbug.com/595761): Remove this function.
|
||||
void EvaluateScriptForTesting(WebInterstitialImpl*,
|
||||
NSString*,
|
||||
JavaScriptCompletion);
|
||||
|
||||
// May be implemented in tests to run JavaScript on interstitials. This function
|
||||
// has access to private ExecuteJavaScript method to be used for testing.
|
||||
void ExecuteScriptForTesting(WebInterstitialImpl*,
|
||||
@ -74,13 +67,6 @@ class WebInterstitialImpl : public WebInterstitial, public WebStateObserver {
|
||||
// Convenience method for getting the WebStateImpl.
|
||||
WebStateImpl* GetWebStateImpl() const;
|
||||
|
||||
// Evaluates the given |script| on interstitial's web view if there is one.
|
||||
// Calls |completionHandler| with results of the evaluation.
|
||||
// The |completionHandler| can be nil. Must be used only for testing.
|
||||
// DEPRECATED. TODO(crbug.com/595761): Remove this method.
|
||||
virtual void EvaluateJavaScript(NSString* script,
|
||||
JavaScriptCompletion completionHandler) = 0;
|
||||
|
||||
// Executes the given |script| on interstitial's web view if there is one.
|
||||
// Calls |completionHandler| with results of the evaluation.
|
||||
// The |completionHandler| can be nil. Must be used only for testing.
|
||||
@ -100,11 +86,6 @@ class WebInterstitialImpl : public WebInterstitial, public WebStateObserver {
|
||||
// Whether or not either Proceed() or DontProceed() has been called.
|
||||
bool action_taken_;
|
||||
|
||||
// Must be implemented only for testing purposes.
|
||||
// DEPRECATED. TODO(crbug.com/595761): Remove this function.
|
||||
friend void web::EvaluateScriptForTesting(WebInterstitialImpl*,
|
||||
NSString*,
|
||||
JavaScriptCompletion);
|
||||
// Must be implemented only for testing purposes.
|
||||
friend void web::ExecuteScriptForTesting(WebInterstitialImpl*,
|
||||
NSString*,
|
||||
|
@ -9,11 +9,6 @@
|
||||
|
||||
namespace web {
|
||||
|
||||
// The type of the completion handler block that is called to inform about
|
||||
// JavaScript evaluation completion.
|
||||
// TODO(crbug.com/595761): Remove this completion handler.
|
||||
typedef void (^JavaScriptCompletion)(NSString*, NSError*);
|
||||
|
||||
// The type of the completion handler block that is called to inform about
|
||||
// JavaScript execution completion. id will be backed up by different classes
|
||||
// depending on resulting JS type: NSString (string), NSNumber (number or
|
||||
|
@ -30,11 +30,6 @@
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)evaluateJavaScript:(NSString*)script
|
||||
stringResultHandler:(web::JavaScriptCompletion)handler {
|
||||
web::EvaluateJavaScript(_webView, script, handler);
|
||||
}
|
||||
|
||||
- (void)executeJavaScript:(NSString*)script
|
||||
completionHandler:(web::JavaScriptResultBlock)completionHandler {
|
||||
web::ExecuteJavaScript(_webView, script, completionHandler);
|
||||
|
@ -11,14 +11,6 @@
|
||||
|
||||
@protocol CRWJSInjectionEvaluator
|
||||
|
||||
// Evaluates the supplied JavaScript in the WebView. Calls |completionHandler|
|
||||
// with results of the evaluation (which may be nil if the implementing object
|
||||
// has no way to run the evaluation or the evaluation returns a nil value)
|
||||
// or an NSError if there is an error. The |completionHandler| can be nil.
|
||||
// TODO(crbug.com/595761): Change this API to return |id| instead of string.
|
||||
- (void)evaluateJavaScript:(NSString*)script
|
||||
stringResultHandler:(web::JavaScriptCompletion)handler;
|
||||
|
||||
// Executes the supplied JavaScript in the WebView. Calls |completionHandler|
|
||||
// with results of the execution (which may be nil if the implementing object
|
||||
// has no way to run the execution or the execution returns a nil value)
|
||||
|
@ -25,12 +25,6 @@
|
||||
// missing. It also injects the dependencies' JavaScript if they are missing.
|
||||
- (void)inject;
|
||||
|
||||
// Evaluate the provided JavaScript asynchronously calling completionHandler
|
||||
// after execution. The |completionHandler| can be nil.
|
||||
// DEPRECATED. TODO(crbug.com/595761): Remove this API.
|
||||
- (void)evaluate:(NSString*)script
|
||||
stringResultHandler:(web::JavaScriptCompletion)completionHandler;
|
||||
|
||||
// Executes the supplied JavaScript asynchronously. Calls |completionHandler|
|
||||
// with results of the execution (which may be nil) or an NSError if there is an
|
||||
// error. The |completionHandler| can be nil.
|
||||
|
@ -56,11 +56,6 @@
|
||||
_injectObject.reset();
|
||||
}
|
||||
|
||||
- (void)evaluate:(NSString*)script
|
||||
stringResultHandler:(web::JavaScriptCompletion)completionHandler {
|
||||
[_receiver evaluateJavaScript:script stringResultHandler:completionHandler];
|
||||
}
|
||||
|
||||
- (void)executeJavaScript:(NSString*)script
|
||||
completionHandler:(web::JavaScriptResultBlock)completionHandler {
|
||||
[_receiver executeJavaScript:script completionHandler:completionHandler];
|
||||
|
@ -36,11 +36,6 @@
|
||||
#pragma mark -
|
||||
#pragma mark CRWJSInjectionEvaluatorMethods
|
||||
|
||||
- (void)evaluateJavaScript:(NSString*)script
|
||||
stringResultHandler:(web::JavaScriptCompletion)handler {
|
||||
[_evaluator evaluateJavaScript:script stringResultHandler:handler];
|
||||
}
|
||||
|
||||
- (void)executeJavaScript:(NSString*)script
|
||||
completionHandler:(web::JavaScriptResultBlock)completionHandler {
|
||||
[_evaluator executeJavaScript:script completionHandler:completionHandler];
|
||||
|
@ -207,9 +207,6 @@ class WebStateImpl;
|
||||
// Executes |script| in the web view, registering user interaction.
|
||||
- (void)executeUserJavaScript:(NSString*)script
|
||||
completionHandler:(web::JavaScriptResultBlock)completion;
|
||||
// Evaluates the user-entered |script| in the web view.
|
||||
// DEPRECATED. TODO(crbug.com/595761): Remove this API.
|
||||
- (void)evaluateUserJavaScript:(NSString*)script;
|
||||
|
||||
// Dismisses the soft keyboard.
|
||||
- (void)dismissKeyboard;
|
||||
|
@ -2485,12 +2485,6 @@ const NSTimeInterval kSnapshotOverlayTransition = 0.5;
|
||||
#pragma mark -
|
||||
#pragma mark CRWJSInjectionEvaluator Methods
|
||||
|
||||
- (void)evaluateJavaScript:(NSString*)script
|
||||
stringResultHandler:(web::JavaScriptCompletion)handler {
|
||||
NSString* safeScript = [self scriptByAddingWindowIDCheckForScript:script];
|
||||
web::EvaluateJavaScript(_webView, safeScript, handler);
|
||||
}
|
||||
|
||||
- (void)executeJavaScript:(NSString*)script
|
||||
completionHandler:(web::JavaScriptResultBlock)completionHandler {
|
||||
NSString* safeScript = [self scriptByAddingWindowIDCheckForScript:script];
|
||||
@ -2520,11 +2514,6 @@ const NSTimeInterval kSnapshotOverlayTransition = 0.5;
|
||||
[self executeJavaScript:script completionHandler:completion];
|
||||
}
|
||||
|
||||
// DEPRECATED. TODO(crbug.com/595761): Remove this API.
|
||||
- (void)evaluateUserJavaScript:(NSString*)script {
|
||||
[self executeUserJavaScript:script completionHandler:nil];
|
||||
}
|
||||
|
||||
- (BOOL)respondToMessage:(base::DictionaryValue*)message
|
||||
userIsInteracting:(BOOL)userIsInteracting
|
||||
originURL:(const GURL&)originURL {
|
||||
|
@ -30,13 +30,6 @@ enum JSEvaluationErrorCode {
|
||||
// Converts result of WKWebView script evaluation to base::Value.
|
||||
std::unique_ptr<base::Value> ValueResultFromWKResult(id result);
|
||||
|
||||
// Evaluates JavaScript on WKWebView. Provides evaluation result as a string.
|
||||
// If the web view cannot evaluate JS at the moment, |completion_handler| is
|
||||
// called with an NSError.
|
||||
void EvaluateJavaScript(WKWebView* web_view,
|
||||
NSString* script,
|
||||
JavaScriptCompletion completion_handler);
|
||||
|
||||
// Executes JavaScript on WKWebView. If the web view cannot execute JS at the
|
||||
// moment, |completion_handler| is called with an NSError.
|
||||
void ExecuteJavaScript(WKWebView* web_view,
|
||||
|
@ -13,33 +13,6 @@
|
||||
#include "base/strings/sys_string_conversions.h"
|
||||
#include "base/values.h"
|
||||
|
||||
namespace {
|
||||
|
||||
// Converts result of WKWebView script evaluation to a string.
|
||||
NSString* StringResultFromWKResult(id result) {
|
||||
if (!result)
|
||||
return @"";
|
||||
|
||||
CFTypeID result_type = CFGetTypeID(result);
|
||||
if (result_type == CFStringGetTypeID())
|
||||
return result;
|
||||
|
||||
if (result_type == CFNumberGetTypeID())
|
||||
return [result stringValue];
|
||||
|
||||
if (result_type == CFBooleanGetTypeID())
|
||||
return [result boolValue] ? @"true" : @"false";
|
||||
|
||||
if (result_type == CFNullGetTypeID())
|
||||
return @"";
|
||||
|
||||
// TODO(stuartmorgan): Stringify other types.
|
||||
NOTREACHED();
|
||||
return nil;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace web {
|
||||
|
||||
NSString* const kJSEvaluationErrorDomain = @"JSEvaluationError";
|
||||
@ -79,23 +52,6 @@ std::unique_ptr<base::Value> ValueResultFromWKResult(id wk_result) {
|
||||
return result;
|
||||
}
|
||||
|
||||
void EvaluateJavaScript(WKWebView* web_view,
|
||||
NSString* script,
|
||||
JavaScriptCompletion completion_handler) {
|
||||
void (^web_view_completion_handler)(id, NSError*) = nil;
|
||||
// Do not create a web_view_completion_handler if no |completion_handler| is
|
||||
// passed to this function. WKWebView guarantees to call all completion
|
||||
// handlers before deallocation. Passing nil as completion handler (when
|
||||
// appropriate) may speed up web view deallocation, because there will be no
|
||||
// need to call those completion handlers.
|
||||
if (completion_handler) {
|
||||
web_view_completion_handler = ^(id result, NSError* error) {
|
||||
completion_handler(StringResultFromWKResult(result), error);
|
||||
};
|
||||
}
|
||||
ExecuteJavaScript(web_view, script, web_view_completion_handler);
|
||||
}
|
||||
|
||||
void ExecuteJavaScript(WKWebView* web_view,
|
||||
NSString* script,
|
||||
JavaScriptResultBlock completion_handler) {
|
||||
|
@ -5,53 +5,18 @@
|
||||
#import "ios/web/web_state/ui/web_view_js_utils.h"
|
||||
|
||||
#include "base/logging.h"
|
||||
#import "base/mac/scoped_nsobject.h"
|
||||
#include "base/test/ios/wait_util.h"
|
||||
#include "base/values.h"
|
||||
#include "ios/web/public/test/test_browser_state.h"
|
||||
#import "ios/web/public/test/test_web_client.h"
|
||||
#import "ios/web/public/web_view_creation_util.h"
|
||||
#import "ios/web/web_state/web_view_internal_creation_util.h"
|
||||
#include "ios/web/public/test/web_test.h"
|
||||
#include "testing/gtest_mac.h"
|
||||
#include "testing/platform_test.h"
|
||||
#include "testing/gtest/include/gtest/gtest.h"
|
||||
|
||||
namespace web {
|
||||
|
||||
// Test fixture for web::EvaluateJavaScript testing.
|
||||
class WebViewJsUtilsTest : public web::WebTest {
|
||||
protected:
|
||||
void SetUp() override {
|
||||
web::WebTest::SetUp();
|
||||
web_view_.reset(web::CreateWKWebView(CGRectZero, GetBrowserState()));
|
||||
}
|
||||
// Synchronously returns result of web::EvaluateJavaScript call.
|
||||
NSString* EvaluateJavaScript(NSString* js) {
|
||||
__block bool evaluation_completed = false;
|
||||
__block base::scoped_nsobject<NSString> evaluation_result;
|
||||
web::EvaluateJavaScript(web_view_, js, ^(NSString* result, NSError* error) {
|
||||
DCHECK(!error);
|
||||
evaluation_result.reset([result copy]);
|
||||
evaluation_completed = true;
|
||||
});
|
||||
base::test::ios::WaitUntilCondition(^{
|
||||
return evaluation_completed;
|
||||
});
|
||||
return [[evaluation_result copy] autorelease];
|
||||
}
|
||||
|
||||
private:
|
||||
// WKWebView created for testing.
|
||||
base::scoped_nsobject<WKWebView> web_view_;
|
||||
};
|
||||
|
||||
// Tests that ValueResultFromWKResult converts nil value to nullptr.
|
||||
TEST_F(WebViewJsUtilsTest, ValueResultFromUndefinedWKResult) {
|
||||
TEST(WebViewJsUtilsTest, ValueResultFromUndefinedWKResult) {
|
||||
EXPECT_FALSE(ValueResultFromWKResult(nil));
|
||||
}
|
||||
|
||||
// Tests that ValueResultFromWKResult converts string to Value::TYPE_STRING.
|
||||
TEST_F(WebViewJsUtilsTest, ValueResultFromStringWKResult) {
|
||||
TEST(WebViewJsUtilsTest, ValueResultFromStringWKResult) {
|
||||
std::unique_ptr<base::Value> value(web::ValueResultFromWKResult(@"test"));
|
||||
EXPECT_TRUE(value);
|
||||
EXPECT_EQ(base::Value::TYPE_STRING, value->GetType());
|
||||
@ -63,7 +28,7 @@ TEST_F(WebViewJsUtilsTest, ValueResultFromStringWKResult) {
|
||||
// Tests that ValueResultFromWKResult converts inetger to Value::TYPE_DOUBLE.
|
||||
// NOTE: WKWebView API returns all numbers as kCFNumberFloat64Type, so there is
|
||||
// no way to tell if the result is integer or double.
|
||||
TEST_F(WebViewJsUtilsTest, ValueResultFromIntegerWKResult) {
|
||||
TEST(WebViewJsUtilsTest, ValueResultFromIntegerWKResult) {
|
||||
std::unique_ptr<base::Value> value(web::ValueResultFromWKResult(@1));
|
||||
EXPECT_TRUE(value);
|
||||
EXPECT_EQ(base::Value::TYPE_DOUBLE, value->GetType());
|
||||
@ -73,7 +38,7 @@ TEST_F(WebViewJsUtilsTest, ValueResultFromIntegerWKResult) {
|
||||
}
|
||||
|
||||
// Tests that ValueResultFromWKResult converts double to Value::TYPE_DOUBLE.
|
||||
TEST_F(WebViewJsUtilsTest, ValueResultFromDoubleWKResult) {
|
||||
TEST(WebViewJsUtilsTest, ValueResultFromDoubleWKResult) {
|
||||
std::unique_ptr<base::Value> value(web::ValueResultFromWKResult(@3.14));
|
||||
EXPECT_TRUE(value);
|
||||
EXPECT_EQ(base::Value::TYPE_DOUBLE, value->GetType());
|
||||
@ -83,7 +48,7 @@ TEST_F(WebViewJsUtilsTest, ValueResultFromDoubleWKResult) {
|
||||
}
|
||||
|
||||
// Tests that ValueResultFromWKResult converts bool to Value::TYPE_BOOLEAN.
|
||||
TEST_F(WebViewJsUtilsTest, ValueResultFromBoolWKResult) {
|
||||
TEST(WebViewJsUtilsTest, ValueResultFromBoolWKResult) {
|
||||
std::unique_ptr<base::Value> value(web::ValueResultFromWKResult(@YES));
|
||||
EXPECT_TRUE(value);
|
||||
EXPECT_EQ(base::Value::TYPE_BOOLEAN, value->GetType());
|
||||
@ -93,7 +58,7 @@ TEST_F(WebViewJsUtilsTest, ValueResultFromBoolWKResult) {
|
||||
}
|
||||
|
||||
// Tests that ValueResultFromWKResult converts null to Value::TYPE_NULL.
|
||||
TEST_F(WebViewJsUtilsTest, ValueResultFromNullWKResult) {
|
||||
TEST(WebViewJsUtilsTest, ValueResultFromNullWKResult) {
|
||||
std::unique_ptr<base::Value> value(
|
||||
web::ValueResultFromWKResult([NSNull null]));
|
||||
EXPECT_TRUE(value);
|
||||
@ -102,7 +67,7 @@ TEST_F(WebViewJsUtilsTest, ValueResultFromNullWKResult) {
|
||||
|
||||
// Tests that ValueResultFromWKResult converts NSDictionaries to properly
|
||||
// initialized base::DictionaryValue.
|
||||
TEST_F(WebViewJsUtilsTest, ValueResultFromDictionaryWKResult) {
|
||||
TEST(WebViewJsUtilsTest, ValueResultFromDictionaryWKResult) {
|
||||
NSDictionary* testDictionary =
|
||||
@{ @"Key1" : @"Value1",
|
||||
@"Key2" : @{@"Key3" : @42} };
|
||||
@ -126,33 +91,4 @@ TEST_F(WebViewJsUtilsTest, ValueResultFromDictionaryWKResult) {
|
||||
EXPECT_EQ(42, value3);
|
||||
}
|
||||
|
||||
// Tests that a script with undefined result correctly evaluates to string.
|
||||
TEST_F(WebViewJsUtilsTest, UndefinedEvaluation) {
|
||||
EXPECT_NSEQ(@"", EvaluateJavaScript(@"{}"));
|
||||
}
|
||||
|
||||
// Tests that a script with string result correctly evaluates to string.
|
||||
TEST_F(WebViewJsUtilsTest, StringEvaluation) {
|
||||
EXPECT_NSEQ(@"test", EvaluateJavaScript(@"'test'"));
|
||||
}
|
||||
|
||||
// Tests that a script with number result correctly evaluates to string.
|
||||
TEST_F(WebViewJsUtilsTest, NumberEvaluation) {
|
||||
EXPECT_NSEQ(@"-1", EvaluateJavaScript(@"-1"));
|
||||
EXPECT_NSEQ(@"0", EvaluateJavaScript(@"0"));
|
||||
EXPECT_NSEQ(@"1", EvaluateJavaScript(@"1"));
|
||||
EXPECT_NSEQ(@"3.14", EvaluateJavaScript(@"3.14"));
|
||||
}
|
||||
|
||||
// Tests that a script with bool result correctly evaluates to string.
|
||||
TEST_F(WebViewJsUtilsTest, BoolEvaluation) {
|
||||
EXPECT_NSEQ(@"true", EvaluateJavaScript(@"true"));
|
||||
EXPECT_NSEQ(@"false", EvaluateJavaScript(@"false"));
|
||||
}
|
||||
|
||||
// Tests that a script with null result correctly evaluates to empty string.
|
||||
TEST_F(WebViewJsUtilsTest, NullEvaluation) {
|
||||
EXPECT_NSEQ(@"", EvaluateJavaScript(@"null"));
|
||||
}
|
||||
|
||||
} // namespace web
|
||||
|
Reference in New Issue
Block a user