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

feat: extend navigationHistory API #42014

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

vitekcerny
Copy link

@vitekcerny vitekcerny commented May 1, 2024

Description of Change

This PR builds on #41577 and adds 3 more functions to webContents.navigationHistory.
The motivation is to provide an API for developers of browser-type applications.

New API:

  • navigationHistory.deleteEntryAtIndex(index) - Deletes the navigation entry at the given index.
  • navigationHistory.getHistory() - Returns webContents complete history.
  • navigationHistory.replaceHistory(newHistory, index) - Replaces current history with newHistory and sets the active index to index.

This enables browser-type applications to save (getHistory) and restore (replaceHistory) browsing sessions.
Also resolves these feature requests: #33899 #41250

Checklist

Release Notes

Notes: Extended navigationHistory API with 3 new functions to allow restoring browsing sessions and for better history management.

@electron-cation electron-cation bot added the new-pr 🌱 PR opened in the last 24 hours label May 1, 2024
@vitekcerny vitekcerny marked this pull request as ready for review May 1, 2024 13:02
@electron-cation electron-cation bot removed the new-pr 🌱 PR opened in the last 24 hours label May 8, 2024
@codebytere codebytere requested a review from a team May 13, 2024 08:30
@codebytere codebytere added the semver/minor backwards-compatible functionality label May 13, 2024
@codebytere
Copy link
Member

@alicelovescake would you be able to help review this?

Copy link
Member

@codebytere codebytere left a comment

Choose a reason for hiding this comment

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

At a high level, without taking a deeper look at the implementation details, I think these methods will be useful. I'll try to more thoroughly review sometime soon, but we'll need to add some tests for these changes regardless!

if (history_item.Get("url", &url))
entry->SetURL(GURL(url));
if (history_item.Get("title", &title))
entry->SetTitle(title);
Copy link
Contributor

Choose a reason for hiding this comment

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

My concern here is that ReplaceHistory may not correctly handle other important properties of the navigation entries, potentially leading to unexpected behavior or inconsistencies in the navigation history. In Electron's version of NavigationEntry, we are just exposing url and title. But Chromium's NavigationEntry has page state with scroll positions and other client side info/ referrer url and other properties that might introduce inconsistencies if we just change the url and title. What do you think?

@@ -2492,6 +2492,58 @@ content::NavigationEntry* WebContents::GetNavigationEntryAtIndex(
return web_contents()->GetController().GetEntryAtIndex(index);
}

bool WebContents::DeleteNavigationEntryAtIndex(int index) {
if (index < 0 || index >= GetHistoryLength())
Copy link
Contributor

Choose a reason for hiding this comment

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

You can use CanGoToIndex

// If the history is empty, it contains only one entry and that is
// "InitialEntry"
if (history_length == 1 && controller.GetEntryAtIndex(0)->IsInitialEntry())
return std::vector<content::NavigationEntry*>();
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice call out!

getEntryAtIndex: this._getNavigationEntryAtIndex.bind(this),
deleteEntryAtIndex: this._deleteNavigationEntryAtIndex.bind(this),
getHistory: this._getHistory.bind(this),
replaceHistory: this._replaceHistory.bind(this)
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think of renaming to just navigationHistory.get() and navigationHistory.replace() to reduce redundancy?

return false;

controller.PruneAllButLastCommitted();
controller.Restore(index, content::RestoreType::kRestored, &history);
Copy link
Contributor

Choose a reason for hiding this comment

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

My gut says this API is a security concern in general and correct me if you disagree. We are allowing the override of the navigation history without any input validation. Anyone could potentially inject malicious scripts the the url or page titles. We are also circumventing user's privacy/ tracking mechanisms if they didn't explicitly intend to navigate to a particular entry.

@vitekcerny vitekcerny force-pushed the extend-navigationHistory branch 2 times, most recently from a80ffb0 to 3ec3bf4 Compare May 19, 2024 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-review/requested 🗳 semver/minor backwards-compatible functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants