initial import
[vuplus_webkit] / Source / WebCore / page / History.cpp
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 #include "SecurityOrigin.h"
38 #include "SerializedScriptValue.h"
39 #include <wtf/MainThread.h>
40
41 namespace WebCore {
42
43 History::History(Frame* frame)
44     : m_frame(frame)
45 {
46 }
47
48 Frame* History::frame() const
49 {
50     return m_frame;
51 }
52
53 void History::disconnectFrame()
54 {
55     m_frame = 0;
56 }
57
58 unsigned History::length() const
59 {
60     if (!m_frame)
61         return 0;
62     if (!m_frame->page())
63         return 0;
64     return m_frame->page()->backForward()->count();
65 }
66
67 void History::back()
68 {
69     go(-1);
70 }
71
72 void History::back(ScriptExecutionContext* context)
73 {
74     go(context, -1);
75 }
76
77 void History::forward()
78 {
79     go(1);
80 }
81
82 void History::forward(ScriptExecutionContext* context)
83 {
84     go(context, 1);
85 }
86
87 void History::go(int distance)
88 {
89     if (!m_frame)
90         return;
91
92     m_frame->navigationScheduler()->scheduleHistoryNavigation(distance);
93 }
94
95 void History::go(ScriptExecutionContext* context, int distance)
96 {
97     if (!m_frame)
98         return;
99
100     ASSERT(isMainThread());
101     Frame* activeFrame = static_cast<Document*>(context)->frame();
102     if (!activeFrame)
103         return;
104
105     if (!activeFrame->loader()->shouldAllowNavigation(m_frame))
106         return;
107
108     m_frame->navigationScheduler()->scheduleHistoryNavigation(distance);
109 }
110
111 KURL History::urlForState(const String& urlString)
112 {
113     KURL baseURL = m_frame->document()->baseURL();
114     if (urlString.isEmpty())
115         return baseURL;
116
117     return KURL(baseURL, urlString);
118 }
119
120 void History::stateObjectAdded(PassRefPtr<SerializedScriptValue> data, const String& title, const String& urlString, StateObjectType stateObjectType, ExceptionCode& ec)
121 {
122     if (!m_frame || !m_frame->page())
123         return;
124     
125     KURL fullURL = urlForState(urlString);
126     if (!fullURL.isValid() || !m_frame->document()->securityOrigin()->canRequest(fullURL)) {
127         ec = SECURITY_ERR;
128         return;
129     }
130
131     if (stateObjectType == StateObjectPush)
132         m_frame->loader()->history()->pushState(data, title, fullURL.string());
133     else if (stateObjectType == StateObjectReplace)
134         m_frame->loader()->history()->replaceState(data, title, fullURL.string());
135             
136     if (!urlString.isEmpty())
137         m_frame->document()->updateURLForPushOrReplaceState(fullURL);
138
139     if (stateObjectType == StateObjectPush)
140         m_frame->loader()->client()->dispatchDidPushStateWithinPage();
141     else if (stateObjectType == StateObjectReplace)
142         m_frame->loader()->client()->dispatchDidReplaceStateWithinPage();
143 }
144
145 } // namespace WebCore