[iOS] Allow additional setup after the web view configuration is reset (#34087)

This adds a temporary WebClient method–exposed through `BraveWebView`–that will allow Brave to perform additional changes to the underlying `WKWebViewConfiguration ` when said configuration is reset in `WKWebViewConfigurationProvider`.  This is a requirement for being able to add the `internal` scheme handler and adjust a few WebKit preferences still controlled by Brave when the `UseProfileWebViewConfiguration` feature flag is enabled.
This commit is contained in:
Kyle Hickinson
2026-02-24 09:42:55 -05:00
committed by GitHub
parent 86d1683688
commit ea5c7c79f5
9 changed files with 78 additions and 4 deletions
+14 -1
View File
@@ -14,7 +14,18 @@ namespace web {
class WebState;
}
// Add methods to override in BraveWebClient for BraveCRWWKNavigationHandler
@class WKWebViewConfiguration;
// Add methods to override in BraveWebClient.
//
// `ShouldBlockJavaScript`, `GetUserAgentForRequest`, and
// `ShouldBlockUniversalLinks` will be called from BraveCRWWKNavigationHandler
// to allow us to implement a Brave specific features during web navigation
//
// `DidResetConfiguration` will be called from
// `BraveWKWebViewConfigurationProvider` to allow us to do some post-reset setup
// for Brave-specific configuration updates. This additional method will be
// removed in the future
#define IsBrowserLockdownModeEnabled \
ShouldBlockJavaScript(web::WebState* web_state, NSURLRequest* request); \
virtual NSString* GetUserAgentForRequest(web::WebState* web_state, \
@@ -22,6 +33,8 @@ class WebState;
NSURLRequest* request); \
virtual bool ShouldBlockUniversalLinks(web::WebState* web_state, \
NSURLRequest* request); \
virtual void DidResetConfiguration(web::BrowserState* browser_state, \
WKWebViewConfiguration* configuration); \
virtual bool IsBrowserLockdownModeEnabled
#include <ios/web/public/web_client.h> // IWYU pragma: export
#undef IsBrowserLockdownModeEnabled
+6
View File
@@ -21,4 +21,10 @@ bool WebClient::ShouldBlockUniversalLinks(web::WebState* web_state,
return false;
}
void WebClient::DidResetConfiguration(web::BrowserState* browser_state,
WKWebViewConfiguration* configuration) {
// Called when the underlying WKWebViewConfiguration is reset.
// Will be handled in BraveWebClient
}
} // namespace web
@@ -13,9 +13,7 @@
#define browser_state_ \
browser_state_; \
friend class BraveWKWebViewConfigurationProvider
#define ResetWithWebViewConfiguration \
Unused() {} \
virtual void ResetWithWebViewConfiguration
#define ResetWithWebViewConfiguration virtual ResetWithWebViewConfiguration
#include <ios/web/web_state/ui/wk_web_view_configuration_provider.h> // IWYU pragma: export
#undef ResetWithWebViewConfiguration
#undef browser_state_
@@ -3,8 +3,11 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */
#include "ios/web/web_state/ui/wk_web_view_configuration_provider.h"
#include "base/notreached.h"
#include "base/supports_user_data.h"
#include "ios/web/public/web_client.h"
// Replace the `WKWebViewConfigurationProvider` constructor call with the Brave
// subclass inside of `WKWebViewConfigurationProvider::FromBrowserState`
@@ -46,6 +49,22 @@ void BraveWKWebViewConfigurationProvider::ResetWithWebViewConfiguration(
// Reset fullscreen to default as it wasn't set in Brave
[[configuration_ preferences] setElementFullscreenEnabled:NO];
// Add Brave-specific adjustments to the WKWebViewConfiguration here
configuration_.dataDetectorTypes = WKDataDetectorTypePhoneNumber;
// Explicitly pass in the private configuration so that it can be mutated
// correctly prior to being used in WebState. This is a temporary measure
// and can be removed once:
// - The `internal` scheme is removed and interstitials are converted to WebUI
// https://github.com/brave/brave-browser/issues/53028
// - We replace WebKit's built in safe browsing implementation with Chromiums
// https://github.com/brave/brave-browser/issues/53029
// - We replace our custom HTTPS-only upgrade and can remove the assignment of
// `WKWebViewConfiguration.upgradeKnownHostsToHTTPS`
// https://github.com/brave/brave-browser/issues/53030
GetWebClient()->DidResetConfiguration(browser_state_, configuration_);
}
} // namespace web
@@ -22,6 +22,10 @@ NS_ASSUME_NONNULL_BEGIN
@protocol AIChatUIHandlerBridge;
@protocol WalletPageHandlerBridge;
@protocol AIChatAssociatedContentPageFetcher;
@protocol ProfileBridge;
typedef void (^ResetConfigurationCallback)(id<ProfileBridge>,
WKWebViewConfiguration*);
CWV_EXPORT
@interface BraveNavigationAction : CWVNavigationAction
@@ -99,6 +103,9 @@ CWV_EXPORT
CWV_EXPORT
@interface BraveWebView : CWVWebView
@property(nonatomic, class, nullable)
ResetConfigurationCallback didResetConfiguration;
// This web view's navigation delegate.
@property(nonatomic, weak, nullable) id<BraveWebViewNavigationDelegate>
navigationDelegate;
@@ -59,6 +59,8 @@
namespace {
ResetConfigurationCallback gDidResetConfigurationCallback;
class BraveWebViewWebStatePolicyDecider : public web::WebStatePolicyDecider {
public:
BraveWebViewWebStatePolicyDecider(web::WebState* web_state,
@@ -188,6 +190,14 @@ class BraveWebViewHolder : public web::WebStateUserData<BraveWebViewHolder> {
return holder->web_view();
}
+ (ResetConfigurationCallback)didResetConfiguration {
return gDidResetConfigurationCallback;
}
+ (void)setDidResetConfiguration:(ResetConfigurationCallback)callback {
gDidResetConfigurationCallback = callback;
}
- (void)resetWebStateWithCoder:(NSCoder*)coder
WKConfiguration:(WKWebViewConfiguration*)wkConfiguration
createdWebView:(WKWebView**)createdWebView {
+2
View File
@@ -18,6 +18,7 @@ source_set("web") {
"//brave/components/brave_component_updater/browser:public",
"//brave/components/brave_user_agent/browser",
"//brave/components/constants",
"//brave/ios/browser/api/profile",
"//brave/ios/browser/api/web_view",
"//brave/ios/browser/application_context",
"//brave/ios/browser/component_updater:zxcvbn_data_component_installer",
@@ -27,6 +28,7 @@ source_set("web") {
"//ios/chrome/browser/application_context/model",
"//ios/chrome/browser/shared/model/application_context",
"//ios/chrome/browser/shared/model/paths",
"//ios/chrome/browser/shared/model/profile",
"//ios/chrome/browser/shared/model/url",
"//ios/chrome/browser/shared/model/url:constants",
"//ios/chrome/browser/web/model:web_internal",
+5
View File
@@ -12,6 +12,8 @@
#include "ios/chrome/browser/web/model/chrome_web_client.h"
@class WKWebViewConfiguration;
class BraveWebClient : public ChromeWebClient {
public:
BraveWebClient();
@@ -58,6 +60,9 @@ class BraveWebClient : public ChromeWebClient {
base::OnceCallback<void(NSArray<NSURL*>*)> completion)
const override API_AVAILABLE(ios(18.4));
void DidResetConfiguration(web::BrowserState* browser_state,
WKWebViewConfiguration* configuration) override;
private:
std::string legacy_user_agent_;
};
+14
View File
@@ -5,12 +5,15 @@
#import "brave/ios/browser/web/brave_web_client.h"
#import <WebKit/WebKit.h>
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/ios/ns_error_util.h"
#include "base/notimplemented.h"
#include "base/strings/sys_string_conversions.h"
#include "brave/components/constants/url_constants.h"
#include "brave/ios/browser/api/profile/profile_bridge_impl.h"
#include "brave/ios/browser/api/web_view/brave_web_view_internal.h"
#include "brave/ios/browser/ui/web_view/features.h"
#include "brave/ios/browser/web/brave_web_main_parts.h"
@@ -19,6 +22,7 @@
#include "components/autofill/ios/form_util/form_handlers_java_script_feature.h"
#include "components/password_manager/ios/password_manager_java_script_feature.h"
#import "components/translate/ios/browser/translate_java_script_feature.h"
#include "ios/chrome/browser/shared/model/profile/profile_ios.h"
#include "ios/chrome/browser/shared/model/url/chrome_url_constants.h"
#include "ios/chrome/browser/web/model/chrome_web_client.h"
#import "ios/components/security_interstitials/ios_security_interstitial_java_script_feature.h"
@@ -212,3 +216,13 @@ void BraveWebClient::RunOpenPanel(
API_AVAILABLE(ios(18.4)) {
NOTIMPLEMENTED();
}
void BraveWebClient::DidResetConfiguration(web::BrowserState* browser_state,
WKWebViewConfiguration* config) {
if (BraveWebView.didResetConfiguration) {
auto* profile = ProfileIOS::FromBrowserState(browser_state);
ProfileBridgeImpl* profile_bridge =
[[ProfileBridgeImpl alloc] initWithProfile:profile];
BraveWebView.didResetConfiguration(profile_bridge, config);
}
}