1/*
2 * Copyright (C) 2007 Apple Inc.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "History.h"
28
29#include "BackForwardController.h"
30#include "Document.h"
31#include "ExceptionCode.h"
32#include "Frame.h"
33#include "FrameLoader.h"
34#include "FrameLoaderClient.h"
35#include "HistoryItem.h"
36#include "Page.h"
37
38namespace WebCore {
39
40History::History(Frame* frame)
41    : m_frame(frame)
42{
43}
44
45Frame* History::frame() const
46{
47    return m_frame;
48}
49
50void History::disconnectFrame()
51{
52    m_frame = 0;
53}
54
55unsigned History::length() const
56{
57    if (!m_frame)
58        return 0;
59    if (!m_frame->page())
60        return 0;
61    return m_frame->page()->backForward()->count();
62}
63
64void History::back()
65{
66    go(-1);
67}
68
69void History::back(ScriptExecutionContext* context)
70{
71    go(context, -1);
72}
73
74void History::forward()
75{
76    go(1);
77}
78
79void History::forward(ScriptExecutionContext* context)
80{
81    go(context, 1);
82}
83
84void History::go(int distance)
85{
86    if (!m_frame)
87        return;
88
89    m_frame->navigationScheduler()->scheduleHistoryNavigation(distance);
90}
91
92void History::go(ScriptExecutionContext* context, int distance)
93{
94    if (!m_frame)
95        return;
96
97    ASSERT(WTF::isMainThread());
98    Frame* activeFrame = static_cast<Document*>(context)->frame();
99    if (!activeFrame)
100        return;
101
102    if (!activeFrame->loader()->shouldAllowNavigation(m_frame))
103        return;
104
105    m_frame->navigationScheduler()->scheduleHistoryNavigation(distance);
106}
107
108KURL History::urlForState(const String& urlString)
109{
110    KURL baseURL = m_frame->loader()->baseURL();
111    if (urlString.isEmpty())
112        return baseURL;
113
114    return KURL(baseURL, urlString);
115}
116
117void History::stateObjectAdded(PassRefPtr<SerializedScriptValue> data, const String& title, const String& urlString, StateObjectType stateObjectType, ExceptionCode& ec)
118{
119    if (!m_frame || !m_frame->page())
120        return;
121
122    KURL fullURL = urlForState(urlString);
123    if (!fullURL.isValid() || !m_frame->document()->securityOrigin()->canRequest(fullURL)) {
124        ec = SECURITY_ERR;
125        return;
126    }
127
128    if (stateObjectType == StateObjectPush)
129        m_frame->loader()->history()->pushState(data, title, fullURL.string());
130    else if (stateObjectType == StateObjectReplace)
131        m_frame->loader()->history()->replaceState(data, title, fullURL.string());
132
133    if (!urlString.isEmpty())
134        m_frame->document()->updateURLForPushOrReplaceState(fullURL);
135
136    if (stateObjectType == StateObjectPush)
137        m_frame->loader()->client()->dispatchDidPushStateWithinPage();
138    else if (stateObjectType == StateObjectReplace)
139        m_frame->loader()->client()->dispatchDidReplaceStateWithinPage();
140}
141
142} // namespace WebCore
143