Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / frame / 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 "core/frame/History.h"
28
29 #include "bindings/core/v8/ExceptionState.h"
30 #include "core/dom/Document.h"
31 #include "core/dom/ExceptionCode.h"
32 #include "core/frame/LocalFrame.h"
33 #include "core/loader/DocumentLoader.h"
34 #include "core/loader/FrameLoader.h"
35 #include "core/loader/FrameLoaderClient.h"
36 #include "core/loader/HistoryItem.h"
37 #include "core/page/BackForwardClient.h"
38 #include "core/page/Page.h"
39 #include "platform/weborigin/KURL.h"
40 #include "platform/weborigin/SecurityOrigin.h"
41 #include "wtf/MainThread.h"
42
43 namespace blink {
44
45 History::History(LocalFrame* frame)
46     : DOMWindowProperty(frame)
47     , m_lastStateObjectRequested(nullptr)
48 {
49     ScriptWrappable::init(this);
50 }
51
52 unsigned History::length() const
53 {
54     if (!m_frame)
55         return 0;
56     if (!m_frame->page())
57         return 0;
58     return m_frame->page()->backForward().backForwardListCount();
59 }
60
61 SerializedScriptValue* History::state()
62 {
63     m_lastStateObjectRequested = stateInternal();
64     return m_lastStateObjectRequested.get();
65 }
66
67 SerializedScriptValue* History::stateInternal() const
68 {
69     if (!m_frame)
70         return 0;
71
72     if (HistoryItem* historyItem = m_frame->loader().currentItem())
73         return historyItem->stateObject();
74
75     return 0;
76 }
77
78 bool History::stateChanged() const
79 {
80     return m_lastStateObjectRequested != stateInternal();
81 }
82
83 bool History::isSameAsCurrentState(SerializedScriptValue* state) const
84 {
85     return state == stateInternal();
86 }
87
88 void History::back(ExecutionContext* context)
89 {
90     go(context, -1);
91 }
92
93 void History::forward(ExecutionContext* context)
94 {
95     go(context, 1);
96 }
97
98 void History::go(ExecutionContext* context, int distance)
99 {
100     if (!m_frame)
101         return;
102
103     ASSERT(isMainThread());
104     Document* activeDocument = toDocument(context);
105     if (!activeDocument)
106         return;
107
108     if (!activeDocument->canNavigate(*m_frame))
109         return;
110
111     m_frame->navigationScheduler().scheduleHistoryNavigation(distance);
112 }
113
114 KURL History::urlForState(const String& urlString)
115 {
116     Document* document = m_frame->document();
117
118     if (urlString.isNull())
119         return document->url();
120     if (urlString.isEmpty())
121         return document->baseURL();
122
123     return KURL(document->baseURL(), urlString);
124 }
125
126 void History::stateObjectAdded(PassRefPtr<SerializedScriptValue> data, const String& /* title */, const String& urlString, FrameLoadType type, ExceptionState& exceptionState)
127 {
128     if (!m_frame || !m_frame->page() || !m_frame->loader().documentLoader())
129         return;
130
131     KURL fullURL = urlForState(urlString);
132     if (!fullURL.isValid() || !m_frame->document()->securityOrigin()->canRequest(fullURL)) {
133         // We can safely expose the URL to JavaScript, as a) no redirection takes place: JavaScript already had this URL, b) JavaScript can only access a same-origin History object.
134         exceptionState.throwSecurityError("A history state object with URL '" + fullURL.elidedString() + "' cannot be created in a document with origin '" + m_frame->document()->securityOrigin()->toString() + "'.");
135         return;
136     }
137     m_frame->loader().updateForSameDocumentNavigation(fullURL, SameDocumentNavigationHistoryApi, data, type);
138 }
139
140 } // namespace blink