Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS] Move web view delegate logic into a shared components #27746

Merged
merged 3 commits into from
Feb 27, 2025

Conversation

kylehickinson
Copy link
Collaborator

@kylehickinson kylehickinson commented Feb 20, 2025

This change moves a majority of logic that used to live in BVC+WKNavigationDelegate.swift into 3 separate protocols:

  • TabWebDelegate: Defines methods that match up with WKUIDelegate/WKNavigationDelegate methods, but will be expanded on to be methods that can only be handled by one implementer.
  • TabWebNavigationDelegate: Defines methods that match up with WKNavigationDelegate navigational movement methods, will eventually become TabWebObserver in the future and allow multiple observers
  • TabWebPolicyDecider: Defines the policy deciders of the WKNavigationDelegate

The biggest change is that these Tab prefixed methods simply pass in a Tab and no longer have access to the underlying WKWebView directly from the delegate method, this is to ensure we split out the WebKit-specific logic that will be only run when using WKWebView directly vs using Chromium web views via CWVWebView

Resolves brave/brave-browser#44126

Submitter Checklist:

  • I confirm that no security/privacy review is needed and no other type of reviews are needed, or that I have requested them
  • There is a ticket for my issue
  • Used Github auto-closing keywords in the PR description above
  • Wrote a good PR/commit description
  • Squashed any review feedback or "fixup" commits before merge, so that history is a record of what happened in the repo, not your PR
  • Added appropriate labels (QA/Yes or QA/No; release-notes/include or release-notes/exclude; OS/...) to the associated issue
  • Checked the PR locally:
    • npm run test -- brave_browser_tests, npm run test -- brave_unit_tests wiki
    • npm run presubmit wiki, npm run gn_check, npm run tslint
  • Ran git rebase master (if needed)

Reviewer Checklist:

  • A security review is not needed, or a link to one is included in the PR description
  • New files have MPL-2.0 license header
  • Adequate test coverage exists to prevent regressions
  • Major classes, functions and non-trivial code blocks are well-commented
  • Changes in component dependencies are properly reflected in gn
  • Code follows the style guide
  • Test plan is specified in PR before merging

After-merge Checklist:

Test Plan:

Re-verify/Sanity test:

  • Web browsing in general
  • JS alerts/prompts + prompt suppression
  • Basic auth navigation
  • Media device permission prompts (microphone, camera)
  • Downloads
  • Playlist streaming/offline downloads

@kylehickinson kylehickinson added CI/skip-android Do not run CI builds for Android CI/skip-macos-x64 Do not run CI builds for macOS x64 CI/skip-windows-x64 Do not run CI builds for Windows x64 CI/skip-macos-arm64 Do not run CI builds for macOS arm64 CI/skip-teamcity Do not run builds in TeamCity labels Feb 20, 2025
@kylehickinson kylehickinson self-assigned this Feb 20, 2025
@kylehickinson kylehickinson requested a review from a team as a code owner February 20, 2025 18:12
@kylehickinson kylehickinson force-pushed the ios-shared-web-delegates branch from 3090a2c to c46d993 Compare February 20, 2025 18:55
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests were already disabled for a long time

Copy link
Contributor

The security team is monitoring all repositories for certain keywords. This PR includes the word(s) "policy" and so security team members have been added as reviewers to take a look.

No need to request a full security review at this stage, the security team will take a look shortly and either clear the label or request more information/changes.

Notifications have already been sent, but if this is blocking your merge feel free to reach out directly to the security team on Slack so that we can expedite this check.

@kylehickinson kylehickinson force-pushed the ios-shared-web-delegates branch 2 times, most recently from 4ef3c1d to 536eb4b Compare February 21, 2025 17:30
@kylehickinson kylehickinson force-pushed the ios-shared-web-delegates branch from 536eb4b to 4cd9a38 Compare February 21, 2025 17:42
# Conflicts:
#	ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+WKNavigationDelegate.swift

# Conflicts:
#	ios/brave-ios/Sources/Brave/Frontend/Browser/BrowserViewController/BVC+WKNavigationDelegate.swift
@kylehickinson kylehickinson force-pushed the ios-shared-web-delegates branch from 4cd9a38 to 05a1e92 Compare February 25, 2025 16:01
Copy link
Contributor

[puLL-Merge] - brave/brave-core@27746

Description

This PR reorganizes web navigation and policy decisions in the iOS app to decouple the logic into more manageable components. The key changes include introducing protocol-based interfaces for web navigation, web policies, and user interactions. The changes also improve security by better isolating privileged calls and implementing safer protocols.

Changes

Changes

  1. BraveSkusAccountLink.swift:
  • Changed injectLocalStorage to take a Tab parameter instead of WKWebView
  1. BrowserPrompts.swift:
  • Changed frame parameter to origin in alert info structs
  • Removed legacy host title generation code
  • Refactored to use URL origin directly for titles
  1. New Files Added:
  • BVC+TabWebDelegate.swift: Handles user interaction with web content
  • BVC+TabWebNavigationDelegate.swift: Manages web navigation events
  • BVC+TabWebPolicyDecider.swift: Makes web policy decisions
  • UniversalLinkNavigationHelper.swift: Handles universal link navigation
  1. Files Removed:
  • TabManagerNavDelegate.swift: Functionality moved into new protocol-based components
  • TabSessionTests.swift: Tests split and moved into new test files
  1. BrowserViewController:
  • Refactored to use new protocol-based components
  • Improved tab, navigation and web view lifecycle management
sequenceDiagram
    participant User
    participant BVC as BrowserViewController
    participant TabWebDelegate
    participant TabWebNavigationDelegate 
    participant TabWebPolicyDecider
    participant WKWebView

    User->>BVC: Interacts with WebView
    BVC->>TabWebDelegate: Handles User Interactions
    BVC->>TabWebNavigationDelegate: Manages Navigation
    TabWebNavigationDelegate->>TabWebPolicyDecider: Makes Policy Decisions
    TabWebPolicyDecider->>WKWebView: Applies Navigation Policy
    WKWebView-->>BVC: Navigation Result
Loading

Security Hotspots

  1. Sensitive Data Exposure: Care should be taken when handling URL redirects and origin data, particularly in TabWebDelegate where origin information is exposed in alerts.

  2. Universal Link Handling: The new universal link navigation helper exposes sensitive domains - ensure lists are up-to-date and handling is secure.

Possible Issues

  1. Browser State Changes: The page zoom state reset functionality has changed and needs thorough testing during navigation.

  2. Memory Management: Many closures were restructured - memory management and lifecycle timing need validation.

let touchPoint = braveWebView.lastHitPoint
let touchRect = CGRect(origin: touchPoint, size: .zero)

// TODO: Find a way to add fixes #3323 and #2961 here:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can we prefix these with brave-ios while we are moving them (and below in same comment)? Ex. brave-ios#3323

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will address in the next PR 🙂

@kylehickinson kylehickinson merged commit 5114a08 into master Feb 27, 2025
18 checks passed
@kylehickinson kylehickinson deleted the ios-shared-web-delegates branch February 27, 2025 22:00
@github-actions github-actions bot added this to the 1.78.x - Nightly milestone Feb 27, 2025
@brave-builds
Copy link
Collaborator

Released in v1.78.4

@StephenHeaps
Copy link
Collaborator

If uplifted please uplift #27891

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI/skip-android Do not run CI builds for Android CI/skip-macos-arm64 Do not run CI builds for macOS arm64 CI/skip-macos-x64 Do not run CI builds for macOS x64 CI/skip-teamcity Do not run builds in TeamCity CI/skip-windows-x64 Do not run CI builds for Windows x64 puLL-Merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wrap WebKit related delegates and split off common web logic into shared components
7 participants