Skip to content

Commit

Permalink
Bug 1265818 - Part 2 - Add scriptable methods for getting and setting…
Browse files Browse the repository at this point in the history
… PresStates from JS. r=jst

This will allow the session store to store and restore scroll positions (and pinch zoom on Android) for past session history entries as well, whereas today only the scroll position of the current page is saved.

As a LayoutHistoryState saves its PresStates in a hash table that doesn't allow direct access to its contents if you don't already know the entry's key, we provide a function to iterate over all stored PresStates and retrieve their keys, which can then be used to get access to each individual PresState in turn. Since nsPresState is little more than a fancy struct and we don't want to have to turn it into a full-blown XPCOM-compatible interface, we just pass the scroll/zoom-related values we're interested in as in/out parameters from/to JS via the LayoutHistoryState.

We also require a helper method for initialising an SHEntry's LayoutHistoryState, since normally this doesn't happen until the PresShell wants to capture the history state in it. We on the other hand require a LayoutHistoryState to be present immediately after creation of a fresh SHEntry object, so we can feed it the session store data during history restoration.

MozReview-Commit-ID: FfZf8KDsVWl
***

--HG--
extra : rebase_source : 0b3f729bff3ac24680d6fe8a0fb796979886170b
  • Loading branch information
buttercookie42 committed Mar 25, 2017
1 parent 3d605b1 commit 0e57568
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 1 deletion.
6 changes: 6 additions & 0 deletions docshell/shistory/nsISHEntry.idl
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ interface nsISHEntry : nsISupports
/** LayoutHistoryState for scroll position and form values */
attribute nsILayoutHistoryState layoutHistoryState;

/**
* Initialises the LayoutHistoryState if it doesn't already exist
* and returns a reference to it.
*/
nsILayoutHistoryState initLayoutHistoryState();

/** parent of this entry */
attribute nsISHEntry parent;

Expand Down
13 changes: 13 additions & 0 deletions docshell/shistory/nsSHEntry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,19 @@ nsSHEntry::SetLayoutHistoryState(nsILayoutHistoryState* aState)
return NS_OK;
}

NS_IMETHODIMP
nsSHEntry::InitLayoutHistoryState(nsILayoutHistoryState** aState)
{
if (!mShared->mLayoutHistoryState) {
nsCOMPtr<nsILayoutHistoryState> historyState;
historyState = NS_NewLayoutHistoryState();
nsresult rv = SetLayoutHistoryState(historyState);
NS_ENSURE_SUCCESS(rv, rv);
}

return GetLayoutHistoryState(aState);
}

NS_IMETHODIMP
nsSHEntry::GetLoadType(uint32_t* aResult)
{
Expand Down
32 changes: 31 additions & 1 deletion layout/base/nsILayoutHistoryState.idl
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,39 @@ native constBool(const bool);
template<typename> struct already_AddRefed;
%}

[uuid(aef27cb3-4df9-4eeb-b0b0-ac56cf861d04)]
[scriptable, uuid(aef27cb3-4df9-4eeb-b0b0-ac56cf861d04)]
interface nsILayoutHistoryState : nsISupports
{
/**
* Whether this LayoutHistoryState contains any PresStates.
*/
readonly attribute boolean hasStates;

/**
* Get the keys of all PresStates held by this LayoutHistoryState.
* Note: Check hasStates first.
*/
void getKeys([optional] out uint32_t aCount,
[array, size_is(aCount), retval] out string aKeys);

/*
* Attempts to get the data of the PresState corresponding to
* the passed key. Throws if no data could be found.
*/
void getPresState(in ACString aKey,
out float aScrollX, out float aScrollY,
out boolean aAllowScrollOriginDowngrade,
out float aRes, out boolean aScaleToRes);

/**
* Constructs a new nsPresState object based on the supplied data
* and adds it to the LayoutHistoryState.
*/
void addNewPresState(in ACString aKey,
in float aScrollX, in float aScrollY,
in boolean aAllowScrollOriginDowngrade,
in float aRes, in boolean aScaleToRes);

// Native only interface, converted from the original nsILayoutHistoryState.h

/**
Expand Down
65 changes: 65 additions & 0 deletions layout/base/nsLayoutHistoryState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,71 @@ NS_IMPL_ISUPPORTS(nsLayoutHistoryState,
nsILayoutHistoryState,
nsISupportsWeakReference)

NS_IMETHODIMP
nsLayoutHistoryState::GetHasStates(bool* aHasStates)
{
*aHasStates = HasStates();
return NS_OK;
}

NS_IMETHODIMP
nsLayoutHistoryState::GetKeys(uint32_t* aCount, char*** aKeys)
{
if (!HasStates()) {
return NS_ERROR_FAILURE;
}

char** keys =
static_cast<char**>(moz_xmalloc(sizeof(char*) * mStates.Count()));
*aCount = mStates.Count();
*aKeys = keys;

for (auto iter = mStates.Iter(); !iter.Done(); iter.Next()) {
*keys = ToNewCString(iter.Key());
keys++;
}

return NS_OK;
}

NS_IMETHODIMP
nsLayoutHistoryState::GetPresState(const nsACString& aKey,
float* aScrollX, float* aScrollY,
bool* aAllowScrollOriginDowngrade,
float* aRes, bool* aScaleToRes)
{
nsPresState* state = GetState(nsCString(aKey));

if (!state) {
return NS_ERROR_FAILURE;
}

*aScrollX = state->GetScrollPosition().x;
*aScrollY = state->GetScrollPosition().y;
*aAllowScrollOriginDowngrade = state->GetAllowScrollOriginDowngrade();
*aRes = state->GetResolution();
*aScaleToRes = state->GetScaleToResolution();

return NS_OK;
}

NS_IMETHODIMP
nsLayoutHistoryState::AddNewPresState(const nsACString& aKey,
float aScrollX, float aScrollY,
bool aAllowScrollOriginDowngrade,
float aRes, bool aScaleToRes)
{
nsPresState* newState = new nsPresState();
newState->SetScrollState(nsPoint(aScrollX, aScrollY));
newState->SetAllowScrollOriginDowngrade(aAllowScrollOriginDowngrade);
newState->SetResolution(aRes);
newState->SetScaleToResolution(aScaleToRes);

mStates.Put(nsCString(aKey), newState);

return NS_OK;
}

void
nsLayoutHistoryState::AddState(const nsCString& aStateKey, nsPresState* aState)
{
Expand Down

0 comments on commit 0e57568

Please sign in to comment.