15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/*
25c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Copyright (C) 2010 Google Inc. All rights reserved.
35c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without
55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * modification, are permitted provided that the following conditions are
65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * met:
75c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *     * Redistributions of source code must retain the above copyright
95c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * notice, this list of conditions and the following disclaimer.
105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *     * Redistributions in binary form must reproduce the above
115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * copyright notice, this list of conditions and the following disclaimer
125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * in the documentation and/or other materials provided with the
135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * distribution.
145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *     * Neither the name of Google Inc. nor the names of its
155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * contributors may be used to endorse or promote products derived from
165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * this software without specific prior written permission.
175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
295c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) */
305c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
315c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "config.h"
32d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)#include "bindings/core/v8/V8PopStateEvent.h"
335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
34197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch#include "bindings/core/v8/SerializedScriptValue.h"
35197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch#include "bindings/core/v8/V8HiddenValue.h"
36d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)#include "bindings/core/v8/V8History.h"
3706f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)#include "core/events/PopStateEvent.h"
381e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)#include "core/frame/History.h"
395c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
40c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink {
415c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
425c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// Save the state value to a hidden attribute in the V8PopStateEvent, and return it, for convenience.
4306f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)static v8::Handle<v8::Value> cacheState(v8::Handle<v8::Object> popStateEvent, v8::Handle<v8::Value> state, v8::Isolate* isolate)
445c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
45d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    V8HiddenValue::setHiddenValue(isolate, popStateEvent, V8HiddenValue::state(isolate), state);
465c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    return state;
475c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
485c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
4919cde67944066db31e633d9e386f2aa9bf9fadb3Torne (Richard Coles)void V8PopStateEvent::stateAttributeGetterCustom(const v8::PropertyCallbackInfo<v8::Value>& info)
505c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
51d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    v8::Handle<v8::Value> result = V8HiddenValue::getHiddenValue(info.GetIsolate(), info.Holder(), V8HiddenValue::state(info.GetIsolate()));
525c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
535267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    if (!result.IsEmpty()) {
545267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        v8SetReturnValue(info, result);
555267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        return;
565267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    }
575c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
587242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    PopStateEvent* event = V8PopStateEvent::toImpl(info.Holder());
595c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    History* history = event->history();
605267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    if (!history || !event->serializedState()) {
617757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch        if (!event->serializedState()) {
627757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch            // If we're in an isolated world and the event was created in the main world,
637757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch            // we need to find the 'state' property on the main world wrapper and clone it.
64d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)            v8::Local<v8::Value> mainWorldState = V8HiddenValue::getHiddenValueFromMainWorldWrapper(info.GetIsolate(), event, V8HiddenValue::state(info.GetIsolate()));
657757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch            if (!mainWorldState.IsEmpty())
667757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch                event->setSerializedState(SerializedScriptValue::createAndSwallowExceptions(mainWorldState, info.GetIsolate()));
677757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch        }
687757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch        if (event->serializedState())
697757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch            result = event->serializedState()->deserialize();
707757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch        else
717757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch            result = v8::Null(info.GetIsolate());
7206f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)        v8SetReturnValue(info, cacheState(info.Holder(), result, info.GetIsolate()));
735267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        return;
745267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    }
755c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
765c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // There's no cached value from a previous invocation, nor a state value was provided by the
775c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // event, but there is a history object, so first we need to see if the state object has been
785c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // deserialized through the history object already.
795c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // The current history state object might've changed in the meantime, so we need to take care
805c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    // of using the correct one, and always share the same deserialization with history.state.
815c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
82e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch    bool isSameState = history->isSameAsCurrentState(event->serializedState());
835c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
845c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    if (isSameState) {
85e1f1df5f01594c0e62e751e4b46e779b85c2faa5Torne (Richard Coles)        v8::Handle<v8::Object> v8History = toV8(history, info.Holder(), info.GetIsolate()).As<v8::Object>();
865c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        if (!history->stateChanged()) {
87d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)            result = V8HiddenValue::getHiddenValue(info.GetIsolate(), v8History, V8HiddenValue::state(info.GetIsolate()));
885267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)            if (!result.IsEmpty()) {
8906f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)                v8SetReturnValue(info, cacheState(info.Holder(), result, info.GetIsolate()));
905267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)                return;
915267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)            }
925c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        }
93926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        result = event->serializedState()->deserialize(info.GetIsolate());
94d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        V8HiddenValue::setHiddenValue(info.GetIsolate(), v8History, V8HiddenValue::state(info.GetIsolate()), result);
957757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch    } else {
96926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        result = event->serializedState()->deserialize(info.GetIsolate());
977757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch    }
985c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
9906f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)    v8SetReturnValue(info, cacheState(info.Holder(), result, info.GetIsolate()));
1005c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
1015c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
102c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)} // namespace blink
103