Skip to content

Commit

Permalink
App history API to navigation API rename (1/n)
Browse files Browse the repository at this point in the history
See WICG/navigation-api#83 and WICG/navigation-api#203 for context.

This CL implements the following renames:

* window.appHistory → window.navigation
* appHistory.current → navigation.currentEntry
* appHistory.updateCurrent() → navigation.updateCurrentEntry()
* appHistory currentchange event → navigation currententrychange event
* appHistory.goTo() → navigation.traverseTo()

Notably, window.navigation has to be [Replaceable], as otherwise various internal tests that do `var navigation = ...` start failing.

No files are renamed at this point (including test files). No classes/IDL interfaces are renamed either.

Thus, the majority of this renaming is done in the web platform tests corpus, and to method names of the AppHistory C++ class.

Bug: 1300246
Change-Id: I29a01c6992c06a5d5099d831273ca8041281d557
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3510372
Reviewed-by: Avi Drissman <avi@chromium.org>
Reviewed-by: Nate Chapin <japhet@chromium.org>
Commit-Queue: Domenic Denicola <domenic@chromium.org>
Cr-Commit-Position: refs/heads/main@{#980648}
NOKEYCHECK=True
GitOrigin-RevId: 3ccf5749316b2bf4dc8105c176233ce5f1620cfd
  • Loading branch information
domenic authored and copybara-github committed Mar 14, 2022
1 parent 795f8ab commit 59f96ec
Show file tree
Hide file tree
Showing 314 changed files with 1,735 additions and 1,735 deletions.
77 changes: 40 additions & 37 deletions blink/renderer/core/app_history/app_history.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class NavigateReaction final : public ScriptFunction::Callable {
return ScriptValue();
}

AppHistory* app_history = AppHistory::appHistory(*window);
AppHistory* app_history = AppHistory::navigation(*window);
app_history->ongoing_navigation_signal_ = nullptr;

if (resolve_type_ == ResolveType::kFulfill) {
Expand Down Expand Up @@ -175,7 +175,7 @@ String DetermineNavigationType(WebFrameLoadType type) {

const char AppHistory::kSupplementName[] = "AppHistory";

AppHistory* AppHistory::appHistory(LocalDOMWindow& window) {
AppHistory* AppHistory::navigation(LocalDOMWindow& window) {
return RuntimeEnabledFeatures::AppHistoryEnabled(&window) ? From(window)
: nullptr;
}
Expand Down Expand Up @@ -244,7 +244,7 @@ void AppHistory::InitializeForNewWindow(
MakeGarbageCollected<AppHistoryEntry>(GetSupplementable(), entry));
}

current_index_ = base::checked_cast<wtf_size_t>(back_entries.size());
current_entry_index_ = base::checked_cast<wtf_size_t>(back_entries.size());
entries_.emplace_back(
MakeGarbageCollected<AppHistoryEntry>(GetSupplementable(), &current));

Expand Down Expand Up @@ -274,7 +274,7 @@ void AppHistory::CloneFromPrevious(AppHistory& previous) {
entries_.emplace_back(
MakeGarbageCollected<AppHistoryEntry>(GetSupplementable(), new_item));
}
current_index_ = previous.current_index_;
current_entry_index_ = previous.current_entry_index_;
PopulateKeySet();
}

Expand All @@ -285,37 +285,38 @@ void AppHistory::UpdateForNavigation(HistoryItem& item, WebFrameLoadType type) {
if (entries_.IsEmpty())
return;

AppHistoryEntry* old_current = current();
AppHistoryEntry* old_current = currentEntry();

HeapVector<Member<AppHistoryEntry>> disposed_entries;
if (type == WebFrameLoadType::kBackForward) {
// If this is a same-document back/forward navigation, the new current
// entry should already be present in entries_ and its key in
// keys_to_indices_.
DCHECK(keys_to_indices_.Contains(item.GetAppHistoryKey()));
current_index_ = keys_to_indices_.at(item.GetAppHistoryKey());
current_entry_index_ = keys_to_indices_.at(item.GetAppHistoryKey());
} else if (type == WebFrameLoadType::kStandard) {
// For a new back/forward entry, truncate any forward entries and prepare
// to append.
current_index_++;
for (wtf_size_t i = current_index_; i < entries_.size(); i++) {
current_entry_index_++;
for (wtf_size_t i = current_entry_index_; i < entries_.size(); i++) {
keys_to_indices_.erase(entries_[i]->key());
disposed_entries.push_back(entries_[i]);
}
entries_.resize(current_index_ + 1);
entries_.resize(current_entry_index_ + 1);
} else if (type == WebFrameLoadType::kReplaceCurrentItem) {
DCHECK_NE(current_index_, -1);
disposed_entries.push_back(entries_[current_index_]);
DCHECK_NE(current_entry_index_, -1);
disposed_entries.push_back(entries_[current_entry_index_]);
}

if (type == WebFrameLoadType::kStandard ||
type == WebFrameLoadType::kReplaceCurrentItem) {
// current_index_ is now correctly set (for type of
// WebFrameLoadType::kReplaceCurrentItem, it didn't change). Create the new
// current entry.
entries_[current_index_] =
entries_[current_entry_index_] =
MakeGarbageCollected<AppHistoryEntry>(GetSupplementable(), &item);
keys_to_indices_.insert(entries_[current_index_]->key(), current_index_);
keys_to_indices_.insert(entries_[current_entry_index_]->key(),
current_entry_index_);
}

// Note how reload types don't update the current entry or dispose any
Expand All @@ -326,14 +327,14 @@ void AppHistory::UpdateForNavigation(HistoryItem& item, WebFrameLoadType type) {
// ongoing_navigation_.
if (ongoing_navigation_) {
ongoing_navigation_->NotifyAboutTheCommittedToEntry(
entries_[current_index_]);
entries_[current_entry_index_]);
}

auto* init = AppHistoryCurrentChangeEventInit::Create();
init->setNavigationType(DetermineNavigationType(type));
init->setFrom(old_current);
DispatchEvent(*AppHistoryCurrentChangeEvent::Create(
event_type_names::kCurrentchange, init));
event_type_names::kCurrententrychange, init));

for (const auto& disposed_entry : disposed_entries) {
disposed_entry->DispatchEvent(*Event::Create(event_type_names::kDispose));
Expand Down Expand Up @@ -382,12 +383,12 @@ void AppHistory::SetEntriesForRestore(
entry_arrays->forward_entries.size() + 1));
for (const auto& item : entry_arrays->back_entries)
new_entries.emplace_back(GetEntryForRestore(item));
new_entries.emplace_back(current());
new_entries.emplace_back(currentEntry());
for (const auto& item : entry_arrays->forward_entries)
new_entries.emplace_back(GetEntryForRestore(item));

new_entries.swap(entries_);
current_index_ =
current_entry_index_ =
base::checked_cast<wtf_size_t>(entry_arrays->back_entries.size());
keys_to_indices_.clear();
PopulateKeySet();
Expand All @@ -407,12 +408,12 @@ void AppHistory::SetEntriesForRestore(
WrapPersistent(disposed_entries)));
}

AppHistoryEntry* AppHistory::current() const {
AppHistoryEntry* AppHistory::currentEntry() const {
// current_index_ is initialized to -1 and set >= 0 when entries_ is
// populated. It will still be negative if the appHistory of an initial empty
// document or opaque-origin document is accessed.
return !HasEntriesAndEventsDisabled() && current_index_ >= 0
? entries_[current_index_]
return !HasEntriesAndEventsDisabled() && current_entry_index_ >= 0
? entries_[current_entry_index_]
: nullptr;
}

Expand All @@ -421,9 +422,9 @@ HeapVector<Member<AppHistoryEntry>> AppHistory::entries() {
: entries_;
}

void AppHistory::updateCurrent(AppHistoryUpdateCurrentOptions* options,
ExceptionState& exception_state) {
AppHistoryEntry* current_entry = current();
void AppHistory::updateCurrentEntry(AppHistoryUpdateCurrentOptions* options,
ExceptionState& exception_state) {
AppHistoryEntry* current_entry = currentEntry();

if (!current_entry) {
exception_state.ThrowDOMException(
Expand All @@ -442,7 +443,7 @@ void AppHistory::updateCurrent(AppHistoryUpdateCurrentOptions* options,
auto* init = AppHistoryCurrentChangeEventInit::Create();
init->setFrom(current_entry);
DispatchEvent(*AppHistoryCurrentChangeEvent::Create(
event_type_names::kCurrentchange, init));
event_type_names::kCurrententrychange, init));
}

AppHistoryResult* AppHistory::navigate(ScriptState* script_state,
Expand Down Expand Up @@ -501,7 +502,7 @@ AppHistoryResult* AppHistory::reload(ScriptState* script_state,
exception_state.ClearException();
return result;
}
} else if (AppHistoryEntry* current_entry = current()) {
} else if (AppHistoryEntry* current_entry = currentEntry()) {
serialized_state = current_entry->GetItem()->GetAppHistoryState();
}
}
Expand Down Expand Up @@ -542,14 +543,14 @@ AppHistoryResult* AppHistory::PerformNonTraverseNavigation(
}

if (SerializedScriptValue* state = navigation->TakeSerializedState()) {
current()->GetItem()->SetAppHistoryState(state);
currentEntry()->GetItem()->SetAppHistoryState(state);
}
return navigation->GetAppHistoryResult();
}

AppHistoryResult* AppHistory::goTo(ScriptState* script_state,
const String& key,
AppHistoryNavigationOptions* options) {
AppHistoryResult* AppHistory::traverseTo(ScriptState* script_state,
const String& key,
AppHistoryNavigationOptions* options) {
if (DOMException* maybe_ex =
PerformSharedNavigationChecks("goTo()/back()/forward()")) {
return EarlyErrorResult(script_state, maybe_ex);
Expand All @@ -559,8 +560,8 @@ AppHistoryResult* AppHistory::goTo(ScriptState* script_state,
return EarlyErrorResult(script_state, DOMExceptionCode::kInvalidStateError,
"Invalid key");
}
if (key == current()->key()) {
return EarlySuccessResult(script_state, current());
if (key == currentEntry()->key()) {
return EarlySuccessResult(script_state, currentEntry());
}

auto previous_navigation = upcoming_traversals_.find(key);
Expand All @@ -580,12 +581,12 @@ AppHistoryResult* AppHistory::goTo(ScriptState* script_state,
}

bool AppHistory::canGoBack() const {
return !HasEntriesAndEventsDisabled() && current_index_ > 0;
return !HasEntriesAndEventsDisabled() && current_entry_index_ > 0;
}

bool AppHistory::canGoForward() const {
return !HasEntriesAndEventsDisabled() && current_index_ != -1 &&
static_cast<size_t>(current_index_) < entries_.size() - 1;
return !HasEntriesAndEventsDisabled() && current_entry_index_ != -1 &&
static_cast<size_t>(current_entry_index_) < entries_.size() - 1;
}

AppHistoryResult* AppHistory::back(ScriptState* script_state,
Expand All @@ -594,7 +595,8 @@ AppHistoryResult* AppHistory::back(ScriptState* script_state,
return EarlyErrorResult(script_state, DOMExceptionCode::kInvalidStateError,
"Cannot go back");
}
return goTo(script_state, entries_[current_index_ - 1]->key(), options);
return traverseTo(script_state, entries_[current_entry_index_ - 1]->key(),
options);
}

AppHistoryResult* AppHistory::forward(ScriptState* script_state,
Expand All @@ -603,7 +605,8 @@ AppHistoryResult* AppHistory::forward(ScriptState* script_state,
return EarlyErrorResult(script_state, DOMExceptionCode::kInvalidStateError,
"Cannot go forward");
}
return goTo(script_state, entries_[current_index_ + 1]->key(), options);
return traverseTo(script_state, entries_[current_entry_index_ + 1]->key(),
options);
}

DOMException* AppHistory::PerformSharedNavigationChecks(
Expand Down Expand Up @@ -761,7 +764,7 @@ AppHistory::DispatchResult AppHistory::DispatchNavigateEvent(
auto promise_list = navigate_event->GetNavigationActionPromisesList();
if (!promise_list.IsEmpty()) {
transition_ = MakeGarbageCollected<AppHistoryTransition>(
script_state, navigation_type, current());
script_state, navigation_type, currentEntry());
// In the spec, the URL and history update steps are not called for reloads.
// In our implementation, we call the corresponding function anyway, but
// |type| being a reload type makes it do none of the spec-relevant
Expand Down
16 changes: 8 additions & 8 deletions blink/renderer/core/app_history/app_history.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class CORE_EXPORT AppHistory final : public EventTargetWithInlineData,

public:
static const char kSupplementName[];
static AppHistory* appHistory(LocalDOMWindow&);
static AppHistory* navigation(LocalDOMWindow&);
// Unconditionally creates AppHistory, even if the RuntimeEnabledFeatures is
// disabled.
static AppHistory* From(LocalDOMWindow&);
Expand All @@ -67,9 +67,9 @@ class CORE_EXPORT AppHistory final : public EventTargetWithInlineData,
bool HasOngoingNavigation() const { return ongoing_navigation_signal_; }

// Web-exposed:
AppHistoryEntry* current() const;
AppHistoryEntry* currentEntry() const;
HeapVector<Member<AppHistoryEntry>> entries();
void updateCurrent(AppHistoryUpdateCurrentOptions*, ExceptionState&);
void updateCurrentEntry(AppHistoryUpdateCurrentOptions*, ExceptionState&);
AppHistoryTransition* transition() const { return transition_; }

bool canGoBack() const;
Expand All @@ -80,9 +80,9 @@ class CORE_EXPORT AppHistory final : public EventTargetWithInlineData,
AppHistoryNavigateOptions*);
AppHistoryResult* reload(ScriptState*, AppHistoryReloadOptions*);

AppHistoryResult* goTo(ScriptState*,
const String& key,
AppHistoryNavigationOptions*);
AppHistoryResult* traverseTo(ScriptState*,
const String& key,
AppHistoryNavigationOptions*);
AppHistoryResult* back(ScriptState*, AppHistoryNavigationOptions*);
AppHistoryResult* forward(ScriptState*, AppHistoryNavigationOptions*);

Expand All @@ -95,7 +95,7 @@ class CORE_EXPORT AppHistory final : public EventTargetWithInlineData,

DEFINE_ATTRIBUTE_EVENT_LISTENER(navigatesuccess, kNavigatesuccess)
DEFINE_ATTRIBUTE_EVENT_LISTENER(navigateerror, kNavigateerror)
DEFINE_ATTRIBUTE_EVENT_LISTENER(currentchange, kCurrentchange)
DEFINE_ATTRIBUTE_EVENT_LISTENER(currententrychange, kCurrententrychange)

enum class DispatchResult { kContinue, kAbort, kTransitionWhile };
DispatchResult DispatchNavigateEvent(const KURL& url,
Expand Down Expand Up @@ -151,7 +151,7 @@ class CORE_EXPORT AppHistory final : public EventTargetWithInlineData,

HeapVector<Member<AppHistoryEntry>> entries_;
HashMap<String, int> keys_to_indices_;
int current_index_ = -1;
int current_entry_index_ = -1;

Member<AppHistoryTransition> transition_;

Expand Down
8 changes: 4 additions & 4 deletions blink/renderer/core/app_history/app_history.idl
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
Exposed=Window,
RuntimeEnabled=AppHistory
] interface AppHistory : EventTarget {
readonly attribute AppHistoryEntry? current;
readonly attribute AppHistoryEntry? currentEntry;
sequence<AppHistoryEntry> entries();
[RaisesException, MeasureAs=AppHistory] void updateCurrent(AppHistoryUpdateCurrentOptions options);
[RaisesException, MeasureAs=AppHistory] void updateCurrentEntry(AppHistoryUpdateCurrentOptions options);
readonly attribute AppHistoryTransition? transition;

readonly attribute boolean canGoBack;
Expand All @@ -18,12 +18,12 @@
[CallWith=ScriptState, MeasureAs=AppHistory] AppHistoryResult navigate(USVString url, optional AppHistoryNavigateOptions options = {});
[CallWith=ScriptState, MeasureAs=AppHistory] AppHistoryResult reload(optional AppHistoryReloadOptions options = {});

[CallWith=ScriptState, MeasureAs=AppHistory] AppHistoryResult goTo(DOMString key, optional AppHistoryNavigationOptions options = {});
[CallWith=ScriptState, MeasureAs=AppHistory] AppHistoryResult traverseTo(DOMString key, optional AppHistoryNavigationOptions options = {});
[CallWith=ScriptState, MeasureAs=AppHistory] AppHistoryResult back(optional AppHistoryNavigationOptions options = {});
[CallWith=ScriptState, MeasureAs=AppHistory] AppHistoryResult forward(optional AppHistoryNavigationOptions options = {});

attribute EventHandler onnavigate;
attribute EventHandler onnavigatesuccess;
attribute EventHandler onnavigateerror;
attribute EventHandler oncurrentchange;
attribute EventHandler oncurrententrychange;
};
2 changes: 1 addition & 1 deletion blink/renderer/core/app_history/app_history_entry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ String AppHistoryEntry::id() const {
}

int64_t AppHistoryEntry::index() {
return DomWindow() ? AppHistory::appHistory(*DomWindow())->GetIndexFor(this)
return DomWindow() ? AppHistory::navigation(*DomWindow())->GetIndexFor(this)
: -1;
}

Expand Down
2 changes: 1 addition & 1 deletion blink/renderer/core/app_history/window_app_history.idl
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
[
ImplementedAs=AppHistory
] partial interface Window {
[RuntimeEnabled=AppHistory] readonly attribute AppHistory appHistory;
[RuntimeEnabled=AppHistory, Replaceable] readonly attribute AppHistory navigation;
};
2 changes: 1 addition & 1 deletion blink/renderer/core/events/event_type_names.json5
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
"crossoriginmessage",
"currentscreenchange",
"cuechange",
"currentchange",
"currententrychange",
"cut",
"datachannel",
"dblclick",
Expand Down
2 changes: 1 addition & 1 deletion blink/renderer/core/frame/history.cc
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ void History::StateObjectAdded(
return;
}

if (auto* app_history = AppHistory::appHistory(*DomWindow())) {
if (auto* app_history = AppHistory::navigation(*DomWindow())) {
if (app_history->DispatchNavigateEvent(
full_url, nullptr, NavigateEventType::kHistoryApi, type,
UserNavigationInvolvement::kNone, data.get(),
Expand Down
2 changes: 1 addition & 1 deletion blink/renderer/core/frame/local_frame_mojo_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1151,7 +1151,7 @@ void LocalFrameMojoHandler::GetCanonicalUrlForSharing(

void LocalFrameMojoHandler::SetAppHistoryEntriesForRestore(
mojom::blink::AppHistoryEntryArraysPtr entry_arrays) {
if (AppHistory* app_history = AppHistory::appHistory(*frame_->DomWindow()))
if (AppHistory* app_history = AppHistory::navigation(*frame_->DomWindow()))
app_history->SetEntriesForRestore(entry_arrays);
}

Expand Down
6 changes: 3 additions & 3 deletions blink/renderer/core/loader/document_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ void DocumentLoader::UpdateForSameDocumentNavigation(
if (should_send_stop_notification)
GetFrameLoader().Progress().ProgressCompleted();

if (auto* app_history = AppHistory::appHistory(*frame_->DomWindow()))
if (auto* app_history = AppHistory::navigation(*frame_->DomWindow()))
app_history->UpdateForNavigation(*history_item_, type);

// Aything except a history.pushState/replaceState is considered a new
Expand Down Expand Up @@ -1338,7 +1338,7 @@ mojom::CommitResult DocumentLoader::CommitSameDocumentNavigation(

mojom::blink::SameDocumentNavigationType same_document_navigation_type =
mojom::blink::SameDocumentNavigationType::kFragment;
if (auto* app_history = AppHistory::appHistory(*frame_->DomWindow())) {
if (auto* app_history = AppHistory::navigation(*frame_->DomWindow())) {
UserNavigationInvolvement involvement = UserNavigationInvolvement::kNone;
if (is_browser_initiated) {
involvement = UserNavigationInvolvement::kBrowserUI;
Expand Down Expand Up @@ -2412,7 +2412,7 @@ void DocumentLoader::CommitNavigation() {
!frame_->DomWindow()->GetSecurityOrigin()->IsOpaque()) {
AppHistory::From(*frame_->DomWindow())
->InitializeForNewWindow(*history_item_, load_type_, commit_reason_,
AppHistory::appHistory(*previous_window),
AppHistory::navigation(*previous_window),
app_history_back_entries_,
app_history_forward_entries_);
// Now that appHistory's entries array is initialized, we don't need to
Expand Down
Loading

0 comments on commit 59f96ec

Please sign in to comment.