summaryrefslogtreecommitdiffstats
path: root/WebKitTools/DumpRenderTree/chromium/TestNavigationController.cpp
blob: 9653c078f11daad4476155b6e5faceefa465e46f (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
 * Copyright (C) 2010 Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "TestNavigationController.h"

#include "TestShell.h"
#include <wtf/Assertions.h>

using namespace WebKit;
using namespace std;

// ----------------------------------------------------------------------------
// TestNavigationEntry

PassRefPtr<TestNavigationEntry> TestNavigationEntry::create()
{
    return adoptRef(new TestNavigationEntry);
}

PassRefPtr<TestNavigationEntry> TestNavigationEntry::create(
    int pageID, const WebURL& url, const WebString& title, const WebString& targetFrame)
{
    return adoptRef(new TestNavigationEntry(pageID, url, title, targetFrame));
}

TestNavigationEntry::TestNavigationEntry()
    : m_pageID(-1) {}

TestNavigationEntry::TestNavigationEntry(
    int pageID, const WebURL& url, const WebString& title, const WebString& targetFrame)
    : m_pageID(pageID)
    , m_url(url)
    , m_title(title)
    , m_targetFrame(targetFrame) {}

TestNavigationEntry::~TestNavigationEntry() {}

void TestNavigationEntry::setContentState(const WebHistoryItem& state)
{
    m_state = state;
}

// ----------------------------------------------------------------------------
// TestNavigationController

TestNavigationController::TestNavigationController(NavigationHost* host)
    : m_pendingEntry(0)
    , m_lastCommittedEntryIndex(-1)
    , m_pendingEntryIndex(-1)
    , m_host(host)
    , m_maxPageID(-1) {}

TestNavigationController::~TestNavigationController()
{
    discardPendingEntry();
}

void TestNavigationController::reset()
{
    m_entries.clear();
    discardPendingEntry();

    m_lastCommittedEntryIndex = -1;
}

void TestNavigationController::reload()
{
    // Base the navigation on where we are now...
    int currentIndex = currentEntryIndex();

    // If we are no where, then we can't reload.  TODO(darin): We should add a
    // CanReload method.
    if (currentIndex == -1)
        return;

    discardPendingEntry();

    m_pendingEntryIndex = currentIndex;
    navigateToPendingEntry(true);
}

void TestNavigationController::goToOffset(int offset)
{
    int index = m_lastCommittedEntryIndex + offset;
    if (index < 0 || index >= entryCount())
        return;

    goToIndex(index);
}

void TestNavigationController::goToIndex(int index)
{
    ASSERT(index >= 0);
    ASSERT(index < static_cast<int>(m_entries.size()));

    discardPendingEntry();

    m_pendingEntryIndex = index;
    navigateToPendingEntry(false);
}

void TestNavigationController::loadEntry(TestNavigationEntry* entry)
{
    // When navigating to a new page, we don't know for sure if we will actually
    // end up leaving the current page.  The new page load could for example
    // result in a download or a 'no content' response (e.g., a mailto: URL).
    discardPendingEntry();
    m_pendingEntry = entry;
    navigateToPendingEntry(false);
}


TestNavigationEntry* TestNavigationController::lastCommittedEntry() const
{
    if (m_lastCommittedEntryIndex == -1)
        return 0;
    return m_entries[m_lastCommittedEntryIndex].get();
}

TestNavigationEntry* TestNavigationController::activeEntry() const
{
    TestNavigationEntry* entry = m_pendingEntry.get();
    if (!entry)
        entry = lastCommittedEntry();
    return entry;
}

int TestNavigationController::currentEntryIndex() const
{
    if (m_pendingEntryIndex != -1)
        return m_pendingEntryIndex;
    return m_lastCommittedEntryIndex;
}


TestNavigationEntry* TestNavigationController::entryAtIndex(int index) const
{
    if (index < 0 || index >= entryCount())
        return 0;
    return m_entries[index].get();
}

TestNavigationEntry* TestNavigationController::entryWithPageID(int32_t pageID) const
{
    int index = entryIndexWithPageID(pageID);
    return (index != -1) ? m_entries[index].get() : 0;
}

void TestNavigationController::didNavigateToEntry(TestNavigationEntry* entry)
{
    // If the entry is that of a page with PageID larger than any this Tab has
    // seen before, then consider it a new navigation.
    if (entry->pageID() > maxPageID()) {
        insertEntry(entry);
        return;
    }

    // Otherwise, we just need to update an existing entry with matching PageID.
    // If the existing entry corresponds to the entry which is pending, then we
    // must update the current entry index accordingly.  When navigating to the
    // same URL, a new PageID is not created.

    int existingEntryIndex = entryIndexWithPageID(entry->pageID());
    TestNavigationEntry* existingEntry = (existingEntryIndex != -1) ?
        m_entries[existingEntryIndex].get() : 0;
    if (!existingEntry) {
        // No existing entry, then simply ignore this navigation!
    } else if (existingEntry == m_pendingEntry.get()) {
        // The given entry might provide a new URL... e.g., navigating back to a
        // page in session history could have resulted in a new client redirect.
        existingEntry->setURL(entry->URL());
        existingEntry->setContentState(entry->contentState());
        m_lastCommittedEntryIndex = m_pendingEntryIndex;
        m_pendingEntryIndex = -1;
        m_pendingEntry.clear();
    } else if (m_pendingEntry && m_pendingEntry->pageID() == -1
               && GURL(m_pendingEntry->URL()) == GURL(existingEntry->URL().spec())) {
        // Not a new navigation
        discardPendingEntry();
    } else {
        // The given entry might provide a new URL... e.g., navigating to a page
        // might result in a client redirect, which should override the URL of the
        // existing entry.
        existingEntry->setURL(entry->URL());
        existingEntry->setContentState(entry->contentState());

        // The navigation could have been issued by the renderer, so be sure that
        // we update our current index.
        m_lastCommittedEntryIndex = existingEntryIndex;
    }

    updateMaxPageID();
}

void TestNavigationController::discardPendingEntry()
{
    m_pendingEntry.clear();
    m_pendingEntryIndex = -1;
}

void TestNavigationController::insertEntry(TestNavigationEntry* entry)
{
    discardPendingEntry();

    // Prune any entry which are in front of the current entry
    int currentSize = static_cast<int>(m_entries.size());
    if (currentSize > 0) {
        while (m_lastCommittedEntryIndex < (currentSize - 1)) {
            m_entries.removeLast();
            currentSize--;
        }
    }

    m_entries.append(RefPtr<TestNavigationEntry>(entry));
    m_lastCommittedEntryIndex = static_cast<int>(m_entries.size()) - 1;
    updateMaxPageID();
}

int TestNavigationController::entryIndexWithPageID(int32 pageID) const
{
    for (int i = static_cast<int>(m_entries.size()) - 1; i >= 0; --i) {
        if (m_entries[i]->pageID() == pageID)
            return i;
    }
    return -1;
}

void TestNavigationController::navigateToPendingEntry(bool reload)
{
    // For session history navigations only the pending_entry_index_ is set.
    if (!m_pendingEntry) {
        ASSERT(m_pendingEntryIndex != -1);
        m_pendingEntry = m_entries[m_pendingEntryIndex];
    }

    if (m_host->navigate(*m_pendingEntry.get(), reload)) {
        // Note: this is redundant if navigation completed synchronously because
        // DidNavigateToEntry call this as well.
        updateMaxPageID();
    } else
        discardPendingEntry();
}

void TestNavigationController::updateMaxPageID()
{
    TestNavigationEntry* entry = activeEntry();
    if (entry)
        m_maxPageID = max(m_maxPageID, entry->pageID());
}