layout/base/nsLayoutHistoryState.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/layout/base/nsLayoutHistoryState.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,96 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/*
    1.10 + * container for information saved in session history when the document
    1.11 + * is not
    1.12 + */
    1.13 +
    1.14 +#include "nsILayoutHistoryState.h"
    1.15 +#include "nsWeakReference.h"
    1.16 +#include "nsClassHashtable.h"
    1.17 +#include "nsPresState.h"
    1.18 +#include "mozilla/Attributes.h"
    1.19 +
    1.20 +class nsLayoutHistoryState MOZ_FINAL : public nsILayoutHistoryState,
    1.21 +                                       public nsSupportsWeakReference
    1.22 +{
    1.23 +public:
    1.24 +  nsLayoutHistoryState()
    1.25 +    : mScrollPositionOnly(false)
    1.26 +  {
    1.27 +  }
    1.28 +
    1.29 +  NS_DECL_ISUPPORTS
    1.30 +
    1.31 +  // nsILayoutHistoryState
    1.32 +  virtual void
    1.33 +  AddState(const nsCString& aKey, nsPresState* aState) MOZ_OVERRIDE;
    1.34 +  virtual nsPresState*
    1.35 +  GetState(const nsCString& aKey) MOZ_OVERRIDE;
    1.36 +  virtual void
    1.37 +  RemoveState(const nsCString& aKey) MOZ_OVERRIDE;
    1.38 +  virtual bool
    1.39 +  HasStates() const MOZ_OVERRIDE;
    1.40 +  virtual void
    1.41 +  SetScrollPositionOnly(const bool aFlag) MOZ_OVERRIDE;
    1.42 +
    1.43 +
    1.44 +private:
    1.45 +  ~nsLayoutHistoryState() {}
    1.46 +  bool mScrollPositionOnly;
    1.47 +
    1.48 +  nsClassHashtable<nsCStringHashKey,nsPresState> mStates;
    1.49 +};
    1.50 +
    1.51 +
    1.52 +already_AddRefed<nsILayoutHistoryState>
    1.53 +NS_NewLayoutHistoryState()
    1.54 +{
    1.55 +  nsRefPtr<nsLayoutHistoryState> state = new nsLayoutHistoryState();
    1.56 +  return state.forget();
    1.57 +}
    1.58 +
    1.59 +NS_IMPL_ISUPPORTS(nsLayoutHistoryState,
    1.60 +                  nsILayoutHistoryState,
    1.61 +                  nsISupportsWeakReference)
    1.62 +
    1.63 +void
    1.64 +nsLayoutHistoryState::AddState(const nsCString& aStateKey, nsPresState* aState)
    1.65 +{
    1.66 +  mStates.Put(aStateKey, aState);
    1.67 +}
    1.68 +
    1.69 +nsPresState*
    1.70 +nsLayoutHistoryState::GetState(const nsCString& aKey)
    1.71 +{
    1.72 +  nsPresState* state = nullptr;
    1.73 +  bool entryExists = mStates.Get(aKey, &state);
    1.74 +
    1.75 +  if (entryExists && mScrollPositionOnly) {
    1.76 +    // Ensure any state that shouldn't be restored is removed
    1.77 +    state->ClearNonScrollState();
    1.78 +  }
    1.79 +
    1.80 +  return state;
    1.81 +}
    1.82 +
    1.83 +void
    1.84 +nsLayoutHistoryState::RemoveState(const nsCString& aKey)
    1.85 +{
    1.86 +  mStates.Remove(aKey);
    1.87 +}
    1.88 +
    1.89 +bool
    1.90 +nsLayoutHistoryState::HasStates() const
    1.91 +{
    1.92 +  return mStates.Count() != 0;
    1.93 +}
    1.94 +
    1.95 +void
    1.96 +nsLayoutHistoryState::SetScrollPositionOnly(const bool aFlag)
    1.97 +{
    1.98 +  mScrollPositionOnly = aFlag;
    1.99 +}

mercurial