feat: extend navigationHistory API (#42014)

* feat: extend navigationHistory API

* refactor: simplify index checking

* refactor: rename 'getHistory' and 'replaceHistory' methods of navigationHistory

* refactor: rename delete*() methods to remove*()

* feat: remove navigationHistory.replaceHistory()

* tests: add tests for removeEntryAtIndex and getAllEntries
This commit is contained in:
Vít Černý 2024-08-19 21:46:04 +02:00 committed by GitHub
parent 4c3014944c
commit 189675575c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 105 additions and 6 deletions

View file

@ -2496,6 +2496,31 @@ content::NavigationEntry* WebContents::GetNavigationEntryAtIndex(
return web_contents()->GetController().GetEntryAtIndex(index);
}
bool WebContents::RemoveNavigationEntryAtIndex(int index) {
if (!CanGoToIndex(index))
return false;
return web_contents()->GetController().RemoveEntryAtIndex(index);
}
std::vector<content::NavigationEntry*> WebContents::GetHistory() const {
const int history_length = GetHistoryLength();
auto& controller = web_contents()->GetController();
// 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*>();
std::vector<content::NavigationEntry*> history;
history.reserve(history_length);
for (int i = 0; i < history_length; i++)
history.push_back(controller.GetEntryAtIndex(i));
return history;
}
void WebContents::ClearHistory() {
// In some rare cases (normally while there is no real history) we are in a
// state where we can't prune navigation entries
@ -4295,6 +4320,9 @@ void WebContents::FillObjectTemplate(v8::Isolate* isolate,
.SetMethod("_getNavigationEntryAtIndex",
&WebContents::GetNavigationEntryAtIndex)
.SetMethod("_historyLength", &WebContents::GetHistoryLength)
.SetMethod("_removeNavigationEntryAtIndex",
&WebContents::RemoveNavigationEntryAtIndex)
.SetMethod("_getHistory", &WebContents::GetHistory)
.SetMethod("_clearHistory", &WebContents::ClearHistory)
.SetMethod("isCrashed", &WebContents::IsCrashed)
.SetMethod("forcefullyCrashRenderer",

View file

@ -204,6 +204,8 @@ class WebContents : public ExclusiveAccessContext,
void GoToIndex(int index);
int GetActiveIndex() const;
content::NavigationEntry* GetNavigationEntryAtIndex(int index) const;
bool RemoveNavigationEntryAtIndex(int index);
std::vector<content::NavigationEntry*> GetHistory() const;
void ClearHistory();
int GetHistoryLength() const;
const std::string GetWebRTCIPHandlingPolicy() const;