Bug 382113 make sure to add the request to the new loadgroup before removing it
authorcbiesinger@gmx.at
Wed, 18 Jul 2007 14:37:39 -0700
changeset 3640 8421f2fb4b3726ee005d0b59a12b21970c7ef44e
parent 3639 d9e5caddcf3f722b01e7a8c08c11374087119aa8
child 3641 86e94d827c03e3e29f6d0feb385541a42032f36a
push idunknown
push userunknown
push dateunknown
bugs382113
milestone1.9a7pre
Bug 382113 make sure to add the request to the new loadgroup before removing it from the old one so that onload doesn't fire before it should Also make nsLoadGroup::AddRequest assert that the request isn't in the loadgroup yet Finally, reenable the reftest that tests the <object> onload behaviour r+sr=bz
content/base/test/test_bug382113.html
netwerk/base/src/nsLoadGroup.cpp
uriloader/base/nsURILoader.cpp
--- a/content/base/test/test_bug382113.html
+++ b/content/base/test/test_bug382113.html
@@ -22,18 +22,18 @@ https://bugzilla.mozilla.org/show_bug.cg
 <script class="testbody" type="application/javascript">
 
 SimpleTest.waitForExplicitFinish();
 var childGotOnload = false;
 var objectGotOnload = false;
 
 /** Test for Bug 100533 **/
 function checkEvents() {
-  // todo(childGotOnload, true, "Child got load event");
-  // todo(objectGotOnload, true, "Object got load event");
+  is(childGotOnload, true, "Child got load event");
+  is(objectGotOnload, true, "Object got load event");
   SimpleTest.finish();
 }
 </script>
 
 </pre>
 </body>
 </html>
 
--- a/netwerk/base/src/nsLoadGroup.cpp
+++ b/netwerk/base/src/nsLoadGroup.cpp
@@ -529,16 +529,28 @@ nsLoadGroup::AddRequest(nsIRequest *requ
     {
         nsCAutoString nameStr;
         request->GetName(nameStr);
         LOG(("LOADGROUP [%x]: Adding request %x %s (count=%d).\n",
              this, request, nameStr.get(), mRequests.entryCount));
     }
 #endif /* PR_LOGGING */
 
+#ifdef DEBUG
+    {
+      RequestMapEntry *entry =
+          static_cast<RequestMapEntry *>
+                     (PL_DHashTableOperate(&mRequests, request,
+                                          PL_DHASH_LOOKUP));
+
+      NS_ASSERTION(PL_DHASH_ENTRY_IS_FREE(entry),
+                   "Entry added to loadgroup twice, don't do that");
+    }
+#endif
+
     //
     // Do not add the channel, if the loadgroup is being canceled...
     //
     if (mIsCanceling) {
 
 #if defined(PR_LOGGING)
         LOG(("LOADGROUP [%x]: AddChannel() ABORTED because LoadGroup is"
              " being canceled!!\n", this));
--- a/uriloader/base/nsURILoader.cpp
+++ b/uriloader/base/nsURILoader.cpp
@@ -1,9 +1,10 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode:nil; c-basic-offset: 2 -*- */
+/* vim:set ts=2 sts=2 sw=2 et cin: */
 /* ***** BEGIN LICENSE BLOCK *****
  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  *
  * The contents of this file are subject to the Mozilla Public License Version
  * 1.1 (the "License"); you may not use this file except in compliance with
  * the License. You may obtain a copy of the License at
  * http://www.mozilla.org/MPL/
  *
@@ -970,30 +971,31 @@ nsresult nsURILoader::OpenChannel(nsICha
           return rv;
         listener->SetLoadCookie(nsDocLoader::GetAsSupports(newDocLoader));
       }
     }
   }
 
   // If the channel is pending, then we need to remove it from its current
   // loadgroup
-  if (aChannelIsOpen) {
-    nsCOMPtr<nsILoadGroup> oldGroup;
-    channel->GetLoadGroup(getter_AddRefs(oldGroup));
-    if (oldGroup) {
+  nsCOMPtr<nsILoadGroup> oldGroup;
+  channel->GetLoadGroup(getter_AddRefs(oldGroup));
+  if (aChannelIsOpen && !SameCOMIdentity(oldGroup, loadGroup)) {
+    // It is important to add the channel to the new group before
+    // removing it from the old one, so that the load isn't considered
+    // done as soon as the request is removed.
+    loadGroup->AddRequest(channel, nsnull);
+
+   if (oldGroup) {
       oldGroup->RemoveRequest(channel, nsnull, NS_BINDING_RETARGETED);
     }
   }
 
   channel->SetLoadGroup(loadGroup);
 
-  if (aChannelIsOpen) {
-    loadGroup->AddRequest(channel, nsnull);
-  }
-
   // prepare the loader for receiving data
   nsresult rv = loader->Prepare();
   if (NS_SUCCEEDED(rv))
     NS_ADDREF(*aListener = loader);
   return rv;
 }
 
 NS_IMETHODIMP nsURILoader::OpenChannel(nsIChannel* channel,