Commit a6dd2209 authored by Jeff Walden's avatar Jeff Walden
Browse files

Bug 903332 - Make watch/unwatch into proxy hooks and such, and make...

Bug 903332 - Make watch/unwatch into proxy hooks and such, and make watching/unwatching work on DOM proxies and windows (or at least work as much as it ever did, which is to say kinda-sorta-ish).  r=bhackett, r=efaust, a=bajaj
parent 8e80c21c
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ MOCHITEST_FILES = test_bug1682.html \
		test_viewport.html \
		test_documentAll.html \
		test_document-element-inserted.html \
		test_document.watch.html \
		$(filter disabled-temporarily--bug-559932, test_bug445004.html) \
		bug445004-inner.js \
		bug445004-outer-rel.html \
+129 −0
Original line number Diff line number Diff line
<!DOCTYPE html>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=903332
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug 903332</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  <script type="application/javascript">

  /** Test for Bug 903332 **/

  var watch1Called;
  function watch1(prop, oldValue, newValue)
  {
    is(watch1Called, false, "watch1Called not reset properly?");
    watch1Called = true;

    is(prop, "cookie", "wrong property name passed to watch1");
    return newValue;
  }

  var watch2Called;
  function watch2(prop, oldValue, newValue)
  {
    is(watch2Called, false, "watch2Called not reset properly?");
    watch2Called = true;

    is(prop, "cookie", "wrong property name passed to watch2");
    return newValue;
  }

  // Just in case subsequent tests depend on a particular value...
  var originalValue = document.cookie;
  ok(true, "originalValue: " + originalValue);

  var originalPrefix = originalValue.length > 0 ? originalValue + "; " : "";

  try
  {
    // trial set (no watch) to verify things work
    document.cookie = "first=set";
    is(document.cookie, originalPrefix + "first=set",
       "first value correct");

    // add a watch
    document.watch("cookie", watch1);

    // set, check for watch invoked
    watch1Called = false;
    document.cookie = "second=set";
    is(watch1Called, true, "watch1 function should be called");
    is(document.cookie, originalPrefix + "first=set; second=set",
       "second value correct");

    // and a second time, just in case
    watch1Called = false;
    document.cookie = "third=set";
    is(watch1Called, true, "watch1 function should be called");
    is(document.cookie, originalPrefix + "first=set; second=set; third=set",
       "third value correct");

    // overwrite the current watch with a new one
    document.watch("cookie", watch2);

    // set, check for watch invoked
    watch1Called = false;
    watch2Called = false;
    document.cookie = "fourth=set";
    is(watch1Called, false, "watch1 invoked erroneously");
    is(watch2Called, true, "watch2 function should be called");
    is(document.cookie, originalPrefix + "first=set; second=set; third=set; fourth=set",
       "fourth value correct");

    // and a second time, just in case
    watch1Called = false;
    watch2Called = false;
    document.cookie = "fifth=set";
    is(watch1Called, false, "watch1 invoked erroneously");
    is(watch2Called, true, "watch2 function should be called");
    is(document.cookie, originalPrefix + "first=set; second=set; third=set; fourth=set; fifth=set",
       "fifth value correct");

    // remove the watch
    document.unwatch("cookie");

    // check for non-invocation now
    watch1Called = false;
    watch2Called = false;
    document.cookie = "sixth=set";
    is(watch1Called, false, "watch1 shouldn't be called");
    is(watch2Called, false, "watch2 shouldn't be called");
    is(document.cookie, originalPrefix + "first=set; second=set; third=set; fourth=set; fifth=set; sixth=set",
       "sixth value correct");
  }
  finally
  {
    // reset
    document.unwatch("cookie"); // harmless, should be no-op except if bugs

    var d = new Date();
    d.setTime(0);
    var suffix = "=; expires=" + d.toGMTString();

    document.cookie = "first" + suffix;
    document.cookie = "second" + suffix;
    document.cookie = "third" + suffix;
    document.cookie = "fourth" + suffix;
    document.cookie = "fifth" + suffix;
    document.cookie = "sixth" + suffix;
  }

  is(document.cookie, originalValue,
     "document.cookie isn't what it was initially!  expect bustage further " +
     "down the line");
  </script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=903332">Mozilla Bug 903332</a>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test">
</pre>
</body>
</html>
+19 −0
Original line number Diff line number Diff line
@@ -614,6 +614,11 @@ public:
  virtual bool enumerate(JSContext *cx, JS::Handle<JSObject*> proxy,
                         JS::AutoIdVector &props) MOZ_OVERRIDE;

  virtual bool watch(JSContext *cx, JS::Handle<JSObject*> proxy,
                     JS::Handle<jsid> id, JS::Handle<JSObject*> callable) MOZ_OVERRIDE;
  virtual bool unwatch(JSContext *cx, JS::Handle<JSObject*> proxy,
                       JS::Handle<jsid> id) MOZ_OVERRIDE;

  // Derived traps
  virtual bool has(JSContext *cx, JS::Handle<JSObject*> proxy,
                   JS::Handle<jsid> id, bool *bp) MOZ_OVERRIDE;
@@ -963,6 +968,20 @@ nsOuterWindowProxy::AppendIndexedPropertyNames(JSContext *cx, JSObject *proxy,
  return true;
}

bool
nsOuterWindowProxy::watch(JSContext *cx, JS::Handle<JSObject*> proxy,
                          JS::Handle<jsid> id, JS::Handle<JSObject*> callable)
{
  return js::WatchGuts(cx, proxy, id, callable);
}

bool
nsOuterWindowProxy::unwatch(JSContext *cx, JS::Handle<JSObject*> proxy,
                            JS::Handle<jsid> id)
{
  return js::UnwatchGuts(cx, proxy, id);
}

nsOuterWindowProxy
nsOuterWindowProxy::singleton;

+13 −0
Original line number Diff line number Diff line
@@ -234,6 +234,19 @@ BaseDOMProxyHandler::enumerate(JSContext* cx, JS::Handle<JSObject*> proxy,
         (!proto || js::GetPropertyNames(cx, proto, 0, &props));
}

bool
BaseDOMProxyHandler::watch(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id,
                           JS::Handle<JSObject*> callable)
{
  return js::WatchGuts(cx, proxy, id, callable);
}

bool
BaseDOMProxyHandler::unwatch(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id)
{
  return js::UnwatchGuts(cx, proxy, id);
}

bool
DOMProxyHandler::has(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id, bool* bp)
{
+5 −0
Original line number Diff line number Diff line
@@ -58,6 +58,11 @@ public:
                             JS::Handle<jsid> id,
                             JS::MutableHandle<JSPropertyDescriptor> desc,
                             unsigned flags) MOZ_OVERRIDE;

  bool watch(JSContext* cx, JS::Handle<JSObject*> proxy, JS::Handle<jsid> id,
             JS::Handle<JSObject*> callable) MOZ_OVERRIDE;
  bool unwatch(JSContext* cx, JS::Handle<JSObject*> proxy,
               JS::Handle<jsid> id) MOZ_OVERRIDE;
};

class DOMProxyHandler : public BaseDOMProxyHandler
Loading