summaryrefslogtreecommitdiffstats
path: root/toolkit/components/sessionstore/SessionStoreRestoreData.cpp
blob: c7a92cf8ada9df6c22a816602e1402b4f77dc513 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */

#include "mozilla/dom/BrowsingContext.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/SessionStoreUtils.h"
#include "mozilla/dom/sessionstore/SessionStoreTypes.h"
#include "mozilla/dom/WindowContext.h"
#include "nsISessionStoreRestoreData.h"
#include "nsNetUtil.h"

namespace mozilla {
namespace dom {

bool SessionStoreRestoreData::IsEmpty() {
  return (!mURI && mScroll.IsEmpty() && mInnerHTML.IsEmpty() &&
          mEntries.IsEmpty() && mChildren.IsEmpty());
}

SessionStoreRestoreData* SessionStoreRestoreData::FindDataForChild(
    BrowsingContext* aContext) {
  nsTArray<uint32_t> path;
  if (!aContext->GetOffsetPath(path)) {
    return nullptr;
  }

  SessionStoreRestoreData* data = this;

  for (uint32_t offset : Reversed(path)) {
    if (!data || data->mChildren.Length() <= offset ||
        !data->mChildren[offset] || data->mChildren[offset]->IsEmpty()) {
      return nullptr;
    }
    data = data->mChildren[offset];
  }

  return data;
}

bool SessionStoreRestoreData::CanRestoreInto(nsIURI* aDocumentURI) {
  if (!mURI) {
    // This should mean that we don't have form data. It's fine to restore this
    // data into any document — the worst that will happen is that we restore an
    // incorrect scroll position.
    MOZ_ASSERT(mEntries.IsEmpty());
    MOZ_ASSERT(mInnerHTML.IsEmpty());
    return true;
  }
  bool equalsExceptRef = false;
  return (aDocumentURI &&
          NS_SUCCEEDED(mURI->EqualsExceptRef(aDocumentURI, &equalsExceptRef)) &&
          equalsExceptRef);
}

bool SessionStoreRestoreData::RestoreInto(RefPtr<BrowsingContext> aContext) {
  if (!aContext->IsInProcess()) {
    return false;
  }

  if (WindowContext* window = aContext->GetCurrentWindowContext()) {
    if (!mScroll.IsEmpty()) {
      if (nsGlobalWindowInner* inner = window->GetInnerWindow()) {
        SessionStoreUtils::RestoreScrollPosition(*inner, mScroll);
      }
    }
    if (mURI) {
      if (nsCOMPtr<Document> doc = window->GetExtantDoc()) {
        if (!CanRestoreInto(doc->GetDocumentURI())) {
          return false;
        }
        SessionStoreUtils::RestoreFormData(*doc, mInnerHTML, mEntries);
      }
    }
  }

  return true;
}

void SessionStoreRestoreData::AddFormEntry(
    bool aIsXPath, const nsAString& aIdOrXPath,
    sessionstore::FormEntryValue aValue) {
  mEntries.AppendElement(
      Entry{sessionstore::FormEntry{nsString(aIdOrXPath), aValue}, aIsXPath});
}

NS_IMPL_ISUPPORTS(SessionStoreRestoreData, nsISessionStoreRestoreData,
                  SessionStoreRestoreData)

NS_IMETHODIMP
SessionStoreRestoreData::GetUrl(nsACString& aURL) {
  if (mURI) {
    nsresult rv = mURI->GetSpec(aURL);
    NS_ENSURE_SUCCESS(rv, rv);
  }
  return NS_OK;
}

NS_IMETHODIMP
SessionStoreRestoreData::SetUrl(const nsACString& aURL) {
  nsresult rv = NS_NewURI(getter_AddRefs(mURI), aURL);
  NS_ENSURE_SUCCESS(rv, rv);
  return NS_OK;
}

NS_IMETHODIMP
SessionStoreRestoreData::GetInnerHTML(nsAString& aInnerHTML) {
  aInnerHTML = mInnerHTML;
  return NS_OK;
}

NS_IMETHODIMP
SessionStoreRestoreData::SetInnerHTML(const nsAString& aInnerHTML) {
  mInnerHTML = aInnerHTML;
  return NS_OK;
}

NS_IMETHODIMP
SessionStoreRestoreData::GetScroll(nsACString& aScroll) {
  aScroll = mScroll;
  return NS_OK;
}

NS_IMETHODIMP
SessionStoreRestoreData::SetScroll(const nsACString& aScroll) {
  mScroll = aScroll;
  return NS_OK;
}

NS_IMETHODIMP
SessionStoreRestoreData::AddTextField(bool aIsXPath,
                                      const nsAString& aIdOrXPath,
                                      const nsAString& aValue) {
  AddFormEntry(aIsXPath, aIdOrXPath, sessionstore::TextField{nsString(aValue)});
  return NS_OK;
}

NS_IMETHODIMP
SessionStoreRestoreData::AddCheckbox(bool aIsXPath, const nsAString& aIdOrXPath,
                                     const bool aValue) {
  AddFormEntry(aIsXPath, aIdOrXPath, sessionstore::Checkbox{aValue});
  return NS_OK;
}

NS_IMETHODIMP
SessionStoreRestoreData::AddFileList(bool aIsXPath, const nsAString& aIdOrXPath,
                                     const nsAString& aType,
                                     const nsTArray<nsString>& aFileList) {
  AddFormEntry(aIsXPath, aIdOrXPath, sessionstore::FileList{aFileList});
  return NS_OK;
}

NS_IMETHODIMP
SessionStoreRestoreData::AddSingleSelect(bool aIsXPath,
                                         const nsAString& aIdOrXPath,
                                         uint32_t aSelectedIndex,
                                         const nsAString& aValue) {
  AddFormEntry(aIsXPath, aIdOrXPath,
               sessionstore::SingleSelect{aSelectedIndex, nsString(aValue)});
  return NS_OK;
}

NS_IMETHODIMP
SessionStoreRestoreData::AddMultipleSelect(bool aIsXPath,
                                           const nsAString& aIdOrXPath,
                                           const nsTArray<nsString>& aValues) {
  AddFormEntry(aIsXPath, aIdOrXPath, sessionstore::MultipleSelect{aValues});
  return NS_OK;
}

NS_IMETHODIMP
SessionStoreRestoreData::AddChild(nsISessionStoreRestoreData* aChild,
                                  uint32_t aIndex) {
  if (nsCOMPtr<SessionStoreRestoreData> child = do_QueryInterface(aChild)) {
    if (aIndex > mChildren.Length()) {
      mChildren.SetLength(aIndex);
    }
    mChildren.InsertElementAt(aIndex, child);
  }
  return NS_OK;
}

}  // namespace dom
}  // namespace mozilla