Add state attribute to history's dom interface.
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 8 Feb 2012 10:39:13 +0000 (10:39 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 8 Feb 2012 10:39:13 +0000 (10:39 +0000)
https://bugs.webkit.org/show_bug.cgi?id=76035

Patch by Pablo Flouret <pablof@motorola.com> on 2012-02-08
Reviewed by Kentaro Hara.

Source/WebCore:

Tests: fast/loader/stateobjects/state-attribute-object-types.html
       fast/loader/stateobjects/state-attribute-only-one-deserialization.html

* bindings/js/JSHistoryCustom.cpp:
(WebCore::JSHistory::state):
(WebCore):
(WebCore::JSHistory::pushState):
(WebCore::JSHistory::replaceState):
* bindings/v8/custom/V8HistoryCustom.cpp:
(WebCore::V8History::stateAccessorGetter):
(WebCore):
(WebCore::V8History::pushStateCallback):
(WebCore::V8History::replaceStateCallback):
* page/History.cpp:
(WebCore::History::History):
(WebCore::History::state):
(WebCore):
(WebCore::History::stateInternal):
(WebCore::History::stateChanged):
* page/History.h:
(History):
* page/History.idl:

LayoutTests:

* fast/dom/Window/window-appendages-cleared-expected.txt:
* fast/loader/stateobjects/state-attribute-object-types-expected.txt: Added.
* fast/loader/stateobjects/state-attribute-object-types.html: Added.
* fast/loader/stateobjects/state-attribute-only-one-deserialization-expected.txt: Added.
* fast/loader/stateobjects/state-attribute-only-one-deserialization.html: Added.

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@107058 268f45cc-cd09-0410-ab3c-d52691b4dbfc

12 files changed:
LayoutTests/ChangeLog
LayoutTests/fast/dom/Window/window-appendages-cleared-expected.txt
LayoutTests/fast/loader/stateobjects/state-attribute-object-types-expected.txt [new file with mode: 0644]
LayoutTests/fast/loader/stateobjects/state-attribute-object-types.html [new file with mode: 0644]
LayoutTests/fast/loader/stateobjects/state-attribute-only-one-deserialization-expected.txt [new file with mode: 0644]
LayoutTests/fast/loader/stateobjects/state-attribute-only-one-deserialization.html [new file with mode: 0644]
Source/WebCore/ChangeLog
Source/WebCore/bindings/js/JSHistoryCustom.cpp
Source/WebCore/bindings/v8/custom/V8HistoryCustom.cpp
Source/WebCore/page/History.cpp
Source/WebCore/page/History.h
Source/WebCore/page/History.idl

index bce47e1..be350cf 100644 (file)
@@ -1,3 +1,16 @@
+2012-02-08  Pablo Flouret  <pablof@motorola.com>
+
+        Add state attribute to history's dom interface.
+        https://bugs.webkit.org/show_bug.cgi?id=76035
+
+        Reviewed by Kentaro Hara.
+
+        * fast/dom/Window/window-appendages-cleared-expected.txt:
+        * fast/loader/stateobjects/state-attribute-object-types-expected.txt: Added.
+        * fast/loader/stateobjects/state-attribute-object-types.html: Added.
+        * fast/loader/stateobjects/state-attribute-only-one-deserialization-expected.txt: Added.
+        * fast/loader/stateobjects/state-attribute-only-one-deserialization.html: Added.
+
 2012-02-08  Nikolas Zimmermann  <nzimmermann@rim.com>
 
         SVGLoad event fires too early
index 8506efa..7b7614c 100644 (file)
@@ -4,6 +4,7 @@ PASS history.go == "LEFTOVER" is false
 PASS history.length == "LEFTOVER" is false
 PASS history.pushState == "LEFTOVER" is false
 PASS history.replaceState == "LEFTOVER" is false
+PASS history.state == "LEFTOVER" is false
 PASS location.assign == "LEFTOVER" is false
 PASS location.hash == "LEFTOVER" is false
 PASS location.host == "LEFTOVER" is false
diff --git a/LayoutTests/fast/loader/stateobjects/state-attribute-object-types-expected.txt b/LayoutTests/fast/loader/stateobjects/state-attribute-object-types-expected.txt
new file mode 100644 (file)
index 0000000..225306b
--- /dev/null
@@ -0,0 +1,25 @@
+This test calls pushState with state objects of all the different object types supported by the HTML5 "internal structured cloning algorithm" and makes sure the state attribute returns the expected objects.
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+PASS history.state is defined.
+
+history.state should initially be null.
+PASS history.state is null
+
+PASS history.state is undefined.
+PASS history.state is null
+PASS history.state is false
+PASS history.state is true
+PASS history.state is 42
+PASS history.state is "String"
+PASS +history.state is +(new Date(0))
+PASS history.state == '/foo/gi' is true
+PASS history.state == '' is true
+PASS history.state == '[object Object]' is true
+PASS history.state == '[object ImageData]' is true
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
diff --git a/LayoutTests/fast/loader/stateobjects/state-attribute-object-types.html b/LayoutTests/fast/loader/stateobjects/state-attribute-object-types.html
new file mode 100644 (file)
index 0000000..b089b22
--- /dev/null
@@ -0,0 +1,44 @@
+<!DOCTYPE html>
+<html>
+<head>
+    <script src="../../js/resources/js-test-pre.js"></script>
+</head/>
+<body>
+<script>
+    description('This test calls pushState with state objects of all the different object types supported by the HTML5 "internal structured cloning algorithm" and makes sure the state attribute returns the expected objects.');
+
+    if (window.layoutTestController)
+        layoutTestController.clearBackForwardList();
+
+    shouldBeDefined("history.state");
+    debug("\nhistory.state should initially be null.");
+    shouldBeNull("history.state");
+    debug("");
+
+    history.pushState(undefined, "undefined entry");
+    shouldBeUndefined("history.state");
+    history.pushState(null, "null entry");
+    shouldBeNull("history.state");
+    history.pushState(false, "false entry");
+    shouldBeFalse("history.state");
+    history.pushState(true, "true entry");
+    shouldBeTrue("history.state");
+    history.pushState(42, "Number entry");
+    shouldBe("history.state", "42");
+    history.pushState("String", "String entry");
+    shouldBeEqualToString("history.state", "String");
+    history.pushState(new Date(0), "Date entry");
+    shouldBe("+history.state", "+(new Date(0))");
+    history.pushState(new RegExp("foo", "gi"), "RegExp entry");
+    shouldEvaluateTo("history.state", new RegExp("foo", "gi"));
+    history.pushState(new Array, "Array entry");
+    shouldEvaluateTo("history.state", new Array);
+    history.pushState(new Object, "Object entry");
+    shouldEvaluateTo("history.state", new Object);
+    history.pushState(document.createElement("canvas").getContext("2d").createImageData(10,10), "ImageData entry");
+    shouldEvaluateTo("history.state", document.createElement("canvas").getContext("2d").createImageData(10,10));
+</script>
+<script src="../../js/resources/js-test-post.js"></script>
+</body>
+</html>
+
diff --git a/LayoutTests/fast/loader/stateobjects/state-attribute-only-one-deserialization-expected.txt b/LayoutTests/fast/loader/stateobjects/state-attribute-only-one-deserialization-expected.txt
new file mode 100644 (file)
index 0000000..e3d0117
--- /dev/null
@@ -0,0 +1,19 @@
+Make sure the same deserialization of the state object is used every time (both in the history object and popstate events).
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+PASS history.state is defined.
+PASS history.state === history.state is true
+PASS history.state !== stateObject is true
+PASS history.state === stateObject is true
+
+Inside popstate event
+
+PASS history.state !== stateObject is true
+PASS popStateEvent.state !== stateObject is true
+FAIL popStateEvent.state === history.state should be true. Was false.
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
diff --git a/LayoutTests/fast/loader/stateobjects/state-attribute-only-one-deserialization.html b/LayoutTests/fast/loader/stateobjects/state-attribute-only-one-deserialization.html
new file mode 100644 (file)
index 0000000..083721d
--- /dev/null
@@ -0,0 +1,61 @@
+<!DOCTYPE html>
+<html>
+<head>
+    <script src="../../js/resources/js-test-pre.js"></script>
+</head/>
+<body>
+<script>
+    description("Make sure the same deserialization of the state object is used every time (both in the history object and popstate events).");
+
+    window.jsTestIsAsync = true;
+    if (window.layoutTestController) {
+        layoutTestController.clearBackForwardList();
+        layoutTestController.waitUntilDone();
+    }
+
+    shouldBeDefined("history.state");
+
+    // Create a new object.
+    var stateObject = ["a"];
+
+    // Use it as the state object in a replaceState.  This clones the object.
+    history.replaceState(stateObject, null, null);
+
+    shouldBeTrue("history.state === history.state");
+
+    // Since the actual history.state is a structured clone, it should not match our original object.
+    shouldBeTrue("history.state !== stateObject");
+
+    // Now let's refetch a copy of history.state to store;
+    stateObject = history.state;
+
+    // Our reference and the history.state itself should be the same.  This is now Adam's original assertion.
+    shouldBeTrue("history.state === stateObject");
+
+    // Now let's do a pushstate to add a history entry.
+    history.pushState(null, null, null);
+
+    // Now add a handler for the popstate event.
+    var popStateEvent;
+    window.onpopstate = function(e) {
+        debug("\nInside popstate event\n");
+        popStateEvent = e;
+        // Our stored reference to stateObject will not match the current state object, as it is a structured clone of the history item's state object.
+        shouldBeTrue("history.state !== stateObject");
+        // Our stored reference to stateObject will not match the state object in this pop state event, as it is the same as history.state which is a structured clone of the history item's state object.
+        shouldBeTrue("popStateEvent.state !== stateObject");
+        // The event's state object and the current state object should match.
+        shouldBeTrue("popStateEvent.state === history.state"); // This fails for now, needs to be fixed.
+
+        var s = document.createElement("script");
+        s.src = "../../js/resources/js-test-post.js";
+        document.body.appendChild(s);
+        setTimeout(finishJSTest, 0);
+    }
+
+    // Now let's go back to our original history entry which has a state object that we've stored a reference to already.
+    // This will fire our popstate event handler above.
+    history.back();
+</script>
+</body>
+</html>
index 0d04131..df34d92 100644 (file)
@@ -1,3 +1,33 @@
+2012-02-08  Pablo Flouret  <pablof@motorola.com>
+
+        Add state attribute to history's dom interface.
+        https://bugs.webkit.org/show_bug.cgi?id=76035
+
+        Reviewed by Kentaro Hara.
+
+        Tests: fast/loader/stateobjects/state-attribute-object-types.html
+               fast/loader/stateobjects/state-attribute-only-one-deserialization.html
+
+        * bindings/js/JSHistoryCustom.cpp:
+        (WebCore::JSHistory::state):
+        (WebCore):
+        (WebCore::JSHistory::pushState):
+        (WebCore::JSHistory::replaceState):
+        * bindings/v8/custom/V8HistoryCustom.cpp:
+        (WebCore::V8History::stateAccessorGetter):
+        (WebCore):
+        (WebCore::V8History::pushStateCallback):
+        (WebCore::V8History::replaceStateCallback):
+        * page/History.cpp:
+        (WebCore::History::History):
+        (WebCore::History::state):
+        (WebCore):
+        (WebCore::History::stateInternal):
+        (WebCore::History::stateChanged):
+        * page/History.h:
+        (History):
+        * page/History.idl:
+
 2012-02-08  Nikolas Zimmermann  <nzimmermann@rim.com>
 
         SVGLoad event fires too early
index 1aedb86..480658b 100644 (file)
@@ -164,6 +164,20 @@ void JSHistory::getOwnPropertyNames(JSObject* object, ExecState* exec, PropertyN
     Base::getOwnPropertyNames(thisObject, exec, propertyNames, mode);
 }
 
+JSValue JSHistory::state(ExecState *exec) const
+{
+    History* history = static_cast<History*>(impl());
+
+    JSValue cachedValue = m_state.get();
+    if (!cachedValue.isEmpty() && !history->stateChanged())
+        return cachedValue;
+
+    SerializedScriptValue* serialized = history->state();
+    JSValue result = serialized ? serialized->deserialize(exec, globalObject(), 0) : jsNull();
+    const_cast<JSHistory*>(this)->m_state.set(exec->globalData(), this, result);
+    return result;
+}
+
 JSValue JSHistory::pushState(ExecState* exec)
 {
     RefPtr<SerializedScriptValue> historyState = SerializedScriptValue::create(exec, exec->argument(0), 0);
@@ -185,6 +199,8 @@ JSValue JSHistory::pushState(ExecState* exec)
     impl()->stateObjectAdded(historyState.release(), title, url, History::StateObjectPush, ec);
     setDOMException(exec, ec);
 
+    m_state.clear();
+
     return jsUndefined();
 }
 
@@ -209,6 +225,8 @@ JSValue JSHistory::replaceState(ExecState* exec)
     impl()->stateObjectAdded(historyState.release(), title, url, History::StateObjectReplace, ec);
     setDOMException(exec, ec);
 
+    m_state.clear();
+
     return jsUndefined();
 }
 
index 8956109..a1bd76b 100644 (file)
 
 namespace WebCore {
 
+v8::Handle<v8::Value> V8History::stateAccessorGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
+{
+    INC_STATS("DOM.History.state");
+    History* history = V8History::toNative(info.Holder());
+
+    v8::Handle<v8::String> propertyName = v8::String::NewSymbol("state");
+    v8::Handle<v8::Value> value = info.Holder()->GetHiddenValue(propertyName);
+
+    if (!value.IsEmpty() && !history->stateChanged())
+        return value;
+
+    SerializedScriptValue* serialized = history->state();
+    value = serialized ? serialized->deserialize() : v8::Handle<v8::Value>(v8::Null());
+    info.Holder()->SetHiddenValue(propertyName, value);
+
+    return value;
+}
+
 v8::Handle<v8::Value> V8History::pushStateCallback(const v8::Arguments& args)
 {
     bool didThrow = false;
@@ -62,6 +80,7 @@ v8::Handle<v8::Value> V8History::pushStateCallback(const v8::Arguments& args)
     ExceptionCode ec = 0;
     History* history = V8History::toNative(args.Holder());
     history->stateObjectAdded(historyState.release(), title, url, History::StateObjectPush, ec);
+    args.Holder()->DeleteHiddenValue(v8::String::NewSymbol("state"));
     return throwError(ec);
 }
 
@@ -86,6 +105,7 @@ v8::Handle<v8::Value> V8History::replaceStateCallback(const v8::Arguments& args)
     ExceptionCode ec = 0;
     History* history = V8History::toNative(args.Holder());
     history->stateObjectAdded(historyState.release(), title, url, History::StateObjectReplace, ec);
+    args.Holder()->DeleteHiddenValue(v8::String::NewSymbol("state"));
     return throwError(ec);
 }
 
index 1f5a39f..a1f9a05 100644 (file)
@@ -42,6 +42,7 @@ namespace WebCore {
 
 History::History(Frame* frame)
     : DOMWindowProperty(frame)
+    , m_lastStateObjectRequested(0)
 {
 }
 
@@ -54,6 +55,28 @@ unsigned History::length() const
     return m_frame->page()->backForward()->count();
 }
 
+SerializedScriptValue* History::state()
+{
+    m_lastStateObjectRequested = stateInternal();
+    return m_lastStateObjectRequested;
+}
+
+SerializedScriptValue* History::stateInternal() const
+{
+    if (!m_frame)
+        return 0;
+
+    if (HistoryItem* historyItem = m_frame->loader()->history()->currentItem())
+        return historyItem->stateObject();
+
+    return 0;
+}
+
+bool History::stateChanged() const
+{
+    return m_lastStateObjectRequested != stateInternal();
+}
+
 void History::back()
 {
     go(-1);
index bd50fcc..bd6f6aa 100644 (file)
@@ -44,6 +44,8 @@ public:
     static PassRefPtr<History> create(Frame* frame) { return adoptRef(new History(frame)); }
 
     unsigned length() const;
+    SerializedScriptValue* state();
+    bool stateChanged() const;
     void back();
     void forward();
     void go(int distance);
@@ -62,6 +64,10 @@ private:
     explicit History(Frame*);
 
     KURL urlForState(const String& url);
+
+    SerializedScriptValue* stateInternal() const;
+
+    SerializedScriptValue* m_lastStateObjectRequested;
 };
 
 } // namespace WebCore
index 9fc9bf0..f703640 100644 (file)
@@ -37,6 +37,7 @@ module window {
         OmitConstructor
     ] History {
         readonly attribute unsigned long length;
+        readonly attribute [CachedAttribute, Custom] SerializedScriptValue state;
 
         [DoNotCheckDomainSecurity, CallWith=ScriptExecutionContext] void back();
         [DoNotCheckDomainSecurity, CallWith=ScriptExecutionContext] void forward();