Commit 2b73f108 authored by mrefaat's avatar mrefaat Committed by Commit Bot

Create objective-c bridge for WebStatePolicyDecider

Created CRWWebStatePolicyDecider delegate.
Created WebStatePolicyDeciderBridge.
Created CRWTestWebStatePolicyDecider (fake test class).
Added unit tests for the newely created delegate/bridge.

This will be required by PreloadController as it's an objective-c class
and i'm going to make it a WebStatePolicyDecider (as part of effort to
move any logic related to app launching out of the CRWWebController.

Bug: 674991, 850760
Cq-Include-Trybots: luci.chromium.try:ios-simulator-full-configs;master.tryserver.chromium.mac:ios-simulator-cronet
Change-Id: I8e17d4d23fc4960609a19708148baa73381983c8
Reviewed-on: https://chromium-review.googlesource.com/1157135Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Commit-Queue: Mohammad Refaat <mrefaat@chromium.org>
Cr-Commit-Position: refs/heads/master@{#580037}
parent 86c0c539
......@@ -409,6 +409,7 @@ source_set("ios_web_web_state_unittests") {
"web_state/web_state_delegate_bridge_unittest.mm",
"web_state/web_state_impl_unittest.mm",
"web_state/web_state_observer_bridge_unittest.mm",
"web_state/web_state_policy_decider_bridge_unittest.mm",
"web_state/web_state_unittest.mm",
"web_state/web_view_internal_creation_util_unittest.mm",
"web_state/wk_web_view_security_util_unittest.mm",
......
......@@ -94,6 +94,7 @@ source_set("public") {
"web_state/web_state_observer.h",
"web_state/web_state_observer_bridge.h",
"web_state/web_state_policy_decider.h",
"web_state/web_state_policy_decider_bridge.h",
"web_state/web_state_user_data.h",
"web_thread.h",
"web_thread_delegate.h",
......
......@@ -24,6 +24,8 @@ source_set("fakes") {
]
sources = [
"crw_fake_web_state_policy_decider.h",
"crw_fake_web_state_policy_decider.mm",
"crw_test_js_injection_receiver.h",
"crw_test_js_injection_receiver.mm",
"crw_test_web_state_observer.h",
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_WEB_PUBLIC_TEST_FAKES_CRW_FAKE_WEB_STATE_POLICY_DECIDER_H_
#define IOS_WEB_PUBLIC_TEST_FAKES_CRW_FAKE_WEB_STATE_POLICY_DECIDER_H_
#import "ios/web/public/web_state/web_state_policy_decider_bridge.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
@class NSURLRequest;
@class NSURLResponse;
namespace web {
// Arguments passed to |shouldAllowRequest:requestInfo:|.
struct FakeShouldAllowRequestInfo {
FakeShouldAllowRequestInfo();
~FakeShouldAllowRequestInfo();
NSURLRequest* request = nil;
WebStatePolicyDecider::RequestInfo request_info;
};
// Arguments passed to |shouldAllowResponse:forMainFrame:|.
struct FakeShouldAllowResponseInfo {
NSURLResponse* response = nil;
BOOL for_main_frame = NO;
};
} // namespace web
// Test implementation of CRWWebStatePolicyDecider protocol.
@interface CRWFakeWebStatePolicyDecider : NSObject<CRWWebStatePolicyDecider>
// Arguments passed to |shouldAllowRequest:requestInfo:|.
@property(nonatomic, readonly)
web::FakeShouldAllowRequestInfo* shouldAllowRequestInfo;
// Arguments passed to |shouldAllowResponse:forMainFrame:|.
@property(nonatomic, readonly)
web::FakeShouldAllowResponseInfo* shouldAllowResponseInfo;
@end
#endif // IOS_WEB_PUBLIC_TEST_FAKES_CRW_FAKE_WEB_STATE_POLICY_DECIDER_H_
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/web/public/test/fakes/crw_fake_web_state_policy_decider.h"
#include <memory>
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace web {
FakeShouldAllowRequestInfo::FakeShouldAllowRequestInfo()
: request_info(ui::PageTransition::PAGE_TRANSITION_FIRST,
GURL::EmptyGURL(),
/*target_frame_is_main=*/false,
/*has_user_gesture=*/false) {}
FakeShouldAllowRequestInfo::~FakeShouldAllowRequestInfo() = default;
} // namespace web
@implementation CRWFakeWebStatePolicyDecider {
// Arguments passed to |shouldAllowRequest:requestInfo:|.
std::unique_ptr<web::FakeShouldAllowRequestInfo> _shouldAllowRequestInfo;
// Arguments passed to |shouldAllowResponse:forMainFrame:|.
std::unique_ptr<web::FakeShouldAllowResponseInfo> _shouldAllowResponseInfo;
}
- (web::FakeShouldAllowRequestInfo*)shouldAllowRequestInfo {
return _shouldAllowRequestInfo.get();
}
- (web::FakeShouldAllowResponseInfo*)shouldAllowResponseInfo {
return _shouldAllowResponseInfo.get();
}
#pragma mark CRWWebStatePolicyDecider methods -
- (BOOL)shouldAllowRequest:(NSURLRequest*)request
requestInfo:
(const web::WebStatePolicyDecider::RequestInfo&)requestInfo {
_shouldAllowRequestInfo = std::make_unique<web::FakeShouldAllowRequestInfo>();
_shouldAllowRequestInfo->request = request;
_shouldAllowRequestInfo->request_info = requestInfo;
return YES;
}
- (BOOL)shouldAllowResponse:(NSURLResponse*)response
forMainFrame:(BOOL)forMainFrame {
_shouldAllowResponseInfo =
std::make_unique<web::FakeShouldAllowResponseInfo>();
_shouldAllowResponseInfo->response = response;
_shouldAllowResponseInfo->for_main_frame = forMainFrame;
return YES;
}
@end
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_WEB_PUBLIC_WEB_STATE_WEB_STATE_POLICY_DECIDER_BRIDGE_H_
#define IOS_WEB_PUBLIC_WEB_STATE_WEB_STATE_POLICY_DECIDER_BRIDGE_H_
#import <Foundation/Foundation.h>
#import "ios/web/public/web_state/web_state_policy_decider.h"
// Objective-C interface for web::WebStatePolicyDecider.
@protocol CRWWebStatePolicyDecider<NSObject>
@optional
// Invoked by |WebStatePolicyDeciderBridge::ShouldAllowRequest|.
- (BOOL)shouldAllowRequest:(NSURLRequest*)request
requestInfo:
(const web::WebStatePolicyDecider::RequestInfo&)requestInfo;
// Invoked by |WebStatePolicyDeciderBridge::ShouldAllowResponse|.
- (BOOL)shouldAllowResponse:(NSURLResponse*)response
forMainFrame:(BOOL)forMainFrame;
@end
namespace web {
// Adapter to use an id<CRWWebStatePolicyDecider> as a
// web::WebStatePolicyDecider.
class WebStatePolicyDeciderBridge : public web::WebStatePolicyDecider {
public:
WebStatePolicyDeciderBridge(web::WebState* web_state,
id<CRWWebStatePolicyDecider> decider);
~WebStatePolicyDeciderBridge() override;
// web::WebStatePolicyDecider methods.
bool ShouldAllowRequest(NSURLRequest* request,
const RequestInfo& request_info) override;
bool ShouldAllowResponse(NSURLResponse* response,
bool for_main_frame) override;
private:
// CRWWebStatePolicyDecider which receives forwarded calls.
__weak id<CRWWebStatePolicyDecider> decider_ = nil;
DISALLOW_COPY_AND_ASSIGN(WebStatePolicyDeciderBridge);
};
} // web
#endif // IOS_WEB_PUBLIC_WEB_STATE_WEB_STATE_POLICY_DECIDER_BRIDGE_H_
......@@ -33,6 +33,7 @@ source_set("web_state") {
"web_state_observer.mm",
"web_state_observer_bridge.mm",
"web_state_policy_decider.mm",
"web_state_policy_decider_bridge.mm",
]
libs = [ "WebKit.framework" ]
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/web/public/web_state/web_state_policy_decider_bridge.h"
#import "ios/web/public/web_state/web_state.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace web {
WebStatePolicyDeciderBridge::WebStatePolicyDeciderBridge(
web::WebState* web_state,
id<CRWWebStatePolicyDecider> decider)
: WebStatePolicyDecider(web_state), decider_(decider) {}
WebStatePolicyDeciderBridge::~WebStatePolicyDeciderBridge() = default;
bool WebStatePolicyDeciderBridge::ShouldAllowRequest(
NSURLRequest* request,
const WebStatePolicyDecider::RequestInfo& request_info) {
if ([decider_
respondsToSelector:@selector(shouldAllowRequest:requestInfo:)]) {
return [decider_ shouldAllowRequest:request requestInfo:request_info];
}
return true;
}
bool WebStatePolicyDeciderBridge::ShouldAllowResponse(NSURLResponse* response,
bool for_main_frame) {
if ([decider_
respondsToSelector:@selector(shouldAllowResponse:forMainFrame:)]) {
return [decider_ shouldAllowResponse:response forMainFrame:for_main_frame];
}
return true;
}
} // namespace web
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/web/public/web_state/web_state_policy_decider_bridge.h"
#import "ios/web/public/test/fakes/crw_fake_web_state_policy_decider.h"
#import "ios/web/public/test/fakes/test_web_state.h"
#include "testing/platform_test.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace web {
// Test fixture to test WebStatePolicyDeciderBridge class.
class WebStatePolicyDeciderBridgeTest : public PlatformTest {
public:
WebStatePolicyDeciderBridgeTest()
: decider_([[CRWFakeWebStatePolicyDecider alloc] init]),
decider_bridge_(&test_web_state_, decider_) {}
protected:
web::TestWebState test_web_state_;
CRWFakeWebStatePolicyDecider* decider_;
WebStatePolicyDeciderBridge decider_bridge_;
};
// Tests |shouldAllowRequest:requestInfo:| forwarding.
TEST_F(WebStatePolicyDeciderBridgeTest, ShouldAllowRequest) {
ASSERT_FALSE([decider_ shouldAllowRequestInfo]);
NSURL* url = [NSURL URLWithString:@"http://test.url"];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
ui::PageTransition transition_type = ui::PageTransition::PAGE_TRANSITION_LINK;
bool target_frame_is_main = true;
bool has_user_gesture = false;
GURL source_url("http://source.url");
WebStatePolicyDecider::RequestInfo request_info(
transition_type, source_url, target_frame_is_main, has_user_gesture);
decider_bridge_.ShouldAllowRequest(request, request_info);
FakeShouldAllowRequestInfo* should_allow_request_info =
[decider_ shouldAllowRequestInfo];
ASSERT_TRUE(should_allow_request_info);
EXPECT_EQ(request, should_allow_request_info->request);
EXPECT_EQ(target_frame_is_main,
should_allow_request_info->request_info.target_frame_is_main);
EXPECT_EQ(has_user_gesture,
should_allow_request_info->request_info.has_user_gesture);
EXPECT_EQ(source_url, should_allow_request_info->request_info.source_url);
EXPECT_TRUE(ui::PageTransitionTypeIncludingQualifiersIs(
transition_type,
should_allow_request_info->request_info.transition_type));
}
// Tests |shouldAllowResponse:forMainFrame:| forwarding.
TEST_F(WebStatePolicyDeciderBridgeTest, ShouldAllowResponse) {
ASSERT_FALSE([decider_ shouldAllowResponseInfo]);
NSURL* url = [NSURL URLWithString:@"http://test.url"];
NSURLResponse* response = [[NSURLResponse alloc] initWithURL:url
MIMEType:@"text/html"
expectedContentLength:0
textEncodingName:nil];
bool for_main_frame = true;
decider_bridge_.ShouldAllowResponse(response, for_main_frame);
FakeShouldAllowResponseInfo* should_allow_response_info =
[decider_ shouldAllowResponseInfo];
ASSERT_TRUE(should_allow_response_info);
EXPECT_EQ(response, should_allow_response_info->response);
EXPECT_EQ(for_main_frame, should_allow_response_info->for_main_frame);
}
} // namespace web
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