nsSHEntryShared.cpp 8.95 KB
Newer Older
1
2
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
4
5
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7

#include "nsSHEntryShared.h"
8

9
10
11
12
13
#include "nsArray.h"
#include "nsDocShellEditorData.h"
#include "nsIContentViewer.h"
#include "nsIDocShell.h"
#include "nsIDocShellTreeItem.h"
14
#include "mozilla/dom/Document.h"
15
#include "nsILayoutHistoryState.h"
16
#include "nsIWebNavigation.h"
17
#include "nsSHistory.h"
18
#include "nsThreadUtils.h"
19

20
#include "mozilla/Attributes.h"
21
#include "mozilla/Preferences.h"
22
23
24

namespace dom = mozilla::dom;

25
26
27
namespace mozilla {
namespace dom {

28
SHEntrySharedParentState::SHEntrySharedParentState(nsISHistory* aSHistory,
29
30
31
                                                   uint64_t aID)
    : SHEntrySharedParentState(nsWeakPtr(do_GetWeakReference(aSHistory)).get(),
                               aID) {}
32

33
34
SHEntrySharedParentState::SHEntrySharedParentState(nsIWeakReference* aSHistory,
                                                   uint64_t aID)
35
    : mDocShellID({0}),
36
      mViewerBounds(0, 0, 0, 0),
37
38
      mCacheKey(0),
      mLastTouched(0),
39
      mID(aID),
40
      mSHistory(aSHistory),
41
42
43
      mIsFrameNavigation(false),
      mSticky(true),
      mDynamicallyCreated(false),
44
45
      mExpired(false),
      mSaveLayoutState(true) {}
46

47
SHEntrySharedParentState::~SHEntrySharedParentState() {}
48

49
void SHEntrySharedParentState::CopyFrom(SHEntrySharedParentState* aEntry) {
50
51
52
53
54
  mDocShellID = aEntry->mDocShellID;
  mTriggeringPrincipal = aEntry->mTriggeringPrincipal;
  mPrincipalToInherit = aEntry->mPrincipalToInherit;
  mStoragePrincipalToInherit = aEntry->mStoragePrincipalToInherit;
  mCsp = aEntry->mCsp;
55
  mSaveLayoutState = aEntry->mSaveLayoutState;
56
57
58
59
60
61
62
63
  mContentType.Assign(aEntry->mContentType);
  mIsFrameNavigation = aEntry->mIsFrameNavigation;
  mSticky = aEntry->mSticky;
  mDynamicallyCreated = aEntry->mDynamicallyCreated;
  mCacheKey = aEntry->mCacheKey;
  mLastTouched = aEntry->mLastTouched;
}

64
65
66
67
68
69
70
void dom::SHEntrySharedParentState::NotifyListenersContentViewerEvicted() {
  if (nsCOMPtr<nsISHistory> shistory = do_QueryReferent(mSHistory)) {
    RefPtr<nsSHistory> nsshistory = static_cast<nsSHistory*>(shistory.get());
    nsshistory->NotifyListenersContentViewerEvicted(1);
  }
}

71
72
73
dom::SHEntrySharedChildState::SHEntrySharedChildState()
    : mSaveLayoutState(true) {}

74
void SHEntrySharedChildState::CopyFrom(SHEntrySharedChildState* aEntry) {
75
76
77
78
  mChildShells.AppendObjects(aEntry->mChildShells);
  mSaveLayoutState = aEntry->mSaveLayoutState;
}

79
80
81
82
}  // namespace dom
}  // namespace mozilla

void nsSHEntryShared::Shutdown() {}
83

84
nsSHEntryShared::~nsSHEntryShared() {
85
86
87
88
  // The destruction can be caused by either the entry is removed from session
  // history and no one holds the reference, or the whole session history is on
  // destruction. We want to ensure that we invoke
  // shistory->RemoveFromExpirationTracker for the former case.
89
  RemoveFromExpirationTracker();
90
91
92
93
94
95
96

  // Calling RemoveDynEntriesForBFCacheEntry on destruction is unnecessary since
  // there couldn't be any SHEntry holding this shared entry, and we noticed
  // that calling RemoveDynEntriesForBFCacheEntry in the middle of
  // nsSHistory::Release can cause a crash, so set mSHistory to null explicitly
  // before RemoveFromBFCacheSync.
  mSHistory = nullptr;
97
98
99
100
101
  if (mContentViewer) {
    RemoveFromBFCacheSync();
  }
}

102
103
104
NS_IMPL_QUERY_INTERFACE(nsSHEntryShared, nsIBFCacheEntry, nsIMutationObserver)
NS_IMPL_ADDREF_INHERITED(nsSHEntryShared, dom::SHEntrySharedParentState)
NS_IMPL_RELEASE_INHERITED(nsSHEntryShared, dom::SHEntrySharedParentState)
105

106
already_AddRefed<nsSHEntryShared> nsSHEntryShared::Duplicate(
107
108
    uint64_t aNewSharedID) {
  RefPtr<nsSHEntryShared> newEntry = new nsSHEntryShared(this, aNewSharedID);
109

110
111
  newEntry->dom::SHEntrySharedParentState::CopyFrom(this);
  newEntry->dom::SHEntrySharedChildState::CopyFrom(this);
112
113
114
115

  return newEntry.forget();
}

116
void nsSHEntryShared::RemoveFromExpirationTracker() {
117
  nsCOMPtr<nsISHistory> shistory = do_QueryReferent(mSHistory);
118
119
  if (shistory && GetExpirationState()->IsTracked()) {
    shistory->RemoveFromExpirationTracker(this);
120
121
122
  }
}

123
void nsSHEntryShared::SyncPresentationState() {
124
125
  if (mContentViewer && mWindowState) {
    // If we have a content viewer and a window state, we should be ok.
126
    return;
127
128
129
130
131
  }

  DropPresentationState();
}

132
void nsSHEntryShared::DropPresentationState() {
133
  RefPtr<nsSHEntryShared> kungFuDeathGrip = this;
134
135

  if (mDocument) {
136
    mDocument->SetBFCacheEntry(nullptr);
137
    mDocument->RemoveMutationObserver(this);
138
    mDocument = nullptr;
139
140
141
142
143
144
  }
  if (mContentViewer) {
    mContentViewer->ClearHistoryEntry();
  }

  RemoveFromExpirationTracker();
145
  mContentViewer = nullptr;
146
  mSticky = true;
147
  mWindowState = nullptr;
148
149
  mViewerBounds.SetRect(0, 0, 0, 0);
  mChildShells.Clear();
150
151
  mRefreshURIList = nullptr;
  mEditorData = nullptr;
152
153
}

154
nsresult nsSHEntryShared::SetContentViewer(nsIContentViewer* aViewer) {
155
156
  MOZ_ASSERT(!aViewer || !mContentViewer,
             "SHEntryShared already contains viewer");
157
158
159
160
161

  if (mContentViewer || !aViewer) {
    DropPresentationState();
  }

162
163
164
  // If we're setting mContentViewer to null, state should already be cleared
  // in the DropPresentationState() call above; If we're setting it to a
  // non-null content viewer, the entry shouldn't have been tracked either.
165
  MOZ_ASSERT(!GetExpirationState()->IsTracked());
166
167
168
  mContentViewer = aViewer;

  if (mContentViewer) {
169
170
171
    // mSHistory is only set for root entries, but in general bfcache only
    // applies to root entries as well. BFCache for subframe navigation has been
    // disabled since 2005 in bug 304860.
172
    if (nsCOMPtr<nsISHistory> shistory = do_QueryReferent(mSHistory)) {
173
174
      shistory->AddToExpirationTracker(this);
    }
175
176
177

    // Store observed document in strong pointer in case it is removed from
    // the contentviewer
178
    mDocument = mContentViewer->GetDocument();
179
180
181
182
183
184
185
186
187
    if (mDocument) {
      mDocument->SetBFCacheEntry(this);
      mDocument->AddMutationObserver(this);
    }
  }

  return NS_OK;
}

188
nsresult nsSHEntryShared::RemoveFromBFCacheSync() {
189
  MOZ_ASSERT(mContentViewer && mDocument, "we're not in the bfcache!");
190

191
192
193
194
195
  // The call to DropPresentationState could drop the last reference, so hold
  // |this| until RemoveDynEntriesForBFCacheEntry finishes.
  RefPtr<nsSHEntryShared> kungFuDeathGrip = this;

  // DropPresentationState would clear mContentViewer.
196
197
198
199
200
201
202
  nsCOMPtr<nsIContentViewer> viewer = mContentViewer;
  DropPresentationState();

  if (viewer) {
    viewer->Destroy();
  }

203
204
  // Now that we've dropped the viewer, we have to clear associated dynamic
  // subframe entries.
205
  nsCOMPtr<nsISHistory> shistory = do_QueryReferent(mSHistory);
206
207
  if (shistory) {
    shistory->RemoveDynEntriesForBFCacheEntry(this);
208
209
  }

210
211
  return NS_OK;
}
212

213
nsresult nsSHEntryShared::RemoveFromBFCacheAsync() {
214
  MOZ_ASSERT(mContentViewer && mDocument, "we're not in the bfcache!");
215

216
  // Check it again to play safe in release builds.
217
218
219
  if (!mDocument) {
    return NS_ERROR_UNEXPECTED;
  }
220
221
222
223
224

  // DropPresentationState would clear mContentViewer & mDocument. Capture and
  // release the references asynchronously so that the document doesn't get
  // nuked mid-mutation.
  nsCOMPtr<nsIContentViewer> viewer = mContentViewer;
225
  RefPtr<dom::Document> document = mDocument;
226
  RefPtr<nsSHEntryShared> self = this;
227
228
229
230
231
232
233
234
235
236
237
238
239
240
  nsresult rv = mDocument->Dispatch(
      mozilla::TaskCategory::Other,
      NS_NewRunnableFunction(
          "nsSHEntryShared::RemoveFromBFCacheAsync",
          [self, viewer, document]() {
            if (viewer) {
              viewer->Destroy();
            }

            nsCOMPtr<nsISHistory> shistory = do_QueryReferent(self->mSHistory);
            if (shistory) {
              shistory->RemoveDynEntriesForBFCacheEntry(self);
            }
          }));
241

242
  if (NS_FAILED(rv)) {
243
    NS_WARNING("Failed to dispatch RemoveFromBFCacheAsync runnable.");
244
245
246
247
248
249
250
251
252
253
  } else {
    // Drop presentation. Only do this if we succeeded in posting the event
    // since otherwise the document could be torn down mid-mutation, causing
    // crashes.
    DropPresentationState();
  }

  return NS_OK;
}

254
255
void nsSHEntryShared::CharacterDataChanged(nsIContent* aContent,
                                           const CharacterDataChangeInfo&) {
256
257
258
  RemoveFromBFCacheAsync();
}

259
260
261
262
void nsSHEntryShared::AttributeChanged(dom::Element* aElement,
                                       int32_t aNameSpaceID, nsAtom* aAttribute,
                                       int32_t aModType,
                                       const nsAttrValue* aOldValue) {
263
264
265
  RemoveFromBFCacheAsync();
}

266
void nsSHEntryShared::ContentAppended(nsIContent* aFirstNewContent) {
267
268
269
  RemoveFromBFCacheAsync();
}

270
void nsSHEntryShared::ContentInserted(nsIContent* aChild) {
271
272
273
  RemoveFromBFCacheAsync();
}

274
275
void nsSHEntryShared::ContentRemoved(nsIContent* aChild,
                                     nsIContent* aPreviousSibling) {
276
277
  RemoveFromBFCacheAsync();
}