1/*
2 * Copyright (C) 2011 Google 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 are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "config.h"
32#include "JSPopStateEvent.h"
33
34#include "JSHistory.h"
35#include <runtime/JSCJSValueInlines.h>
36
37using namespace JSC;
38
39namespace WebCore {
40
41// Save the state value to the m_state member of a JSPopStateEvent, and return it, for convenience.
42static const JSValue& cacheState(ExecState* exec, JSPopStateEvent* event, const JSValue& state)
43{
44    event->m_state.set(exec->vm(), event, state);
45    return state;
46}
47
48JSValue JSPopStateEvent::state(ExecState* exec) const
49{
50    JSValue cachedValue = m_state.get();
51    if (!cachedValue.isEmpty())
52        return cachedValue;
53
54    PopStateEvent& event = impl();
55
56    if (!event.state().hasNoValue())
57        return cacheState(exec, const_cast<JSPopStateEvent*>(this), event.state().jsValue());
58
59    History* history = event.history();
60    if (!history || !event.serializedState())
61        return cacheState(exec, const_cast<JSPopStateEvent*>(this), jsNull());
62
63    // There's no cached value from a previous invocation, nor a state value was provided by the
64    // event, but there is a history object, so first we need to see if the state object has been
65    // deserialized through the history object already.
66    // The current history state object might've changed in the meantime, so we need to take care
67    // of using the correct one, and always share the same deserialization with history.state.
68
69    bool isSameState = history->isSameAsCurrentState(event.serializedState().get());
70    JSValue result;
71
72    if (isSameState) {
73        JSHistory* jsHistory = jsCast<JSHistory*>(toJS(exec, globalObject(), history).asCell());
74        result = jsHistory->state(exec);
75    } else
76        result = event.serializedState()->deserialize(exec, globalObject(), 0);
77
78    return cacheState(exec, const_cast<JSPopStateEvent*>(this), result);
79}
80
81} // namespace WebCore
82