OSD700 Release 1 – Updating the DOM

First release! Already? Hate to sound not enthused but other courses are bringing down my mood right now 😛 Not this one though!

During these first two and a half weeks I have been able to get three tickets done with two of them landed and the third’s patch review+ and just needed to be run on the try server to finalize if it’s acceptable or not.

Said three bugs are:
Bug 698384
Bug 698385
Bug 718274

You may notice that there is not actual patch for 698385. That is because the changes to that one and 698384 affect the same area of code so in order to properly fix one the other has to be done at the same time. Hence why the patch for both is located in 698384.

The first two were simply updates to the DOM specifications. Both document.createTreeWalker and document.createNodeIterator now had optional arguments according to the DOM Spec and obviously Mozilla wants to adhere to this spec as much as possible. The actual coding changes for these were pretty simple in the end once I knew what to do but getting there was another story. I can’t make the claim that I am an experienced C++ developer so coming in I had no idea how to deal with optional arguments. In the end I figured it out which can be read here.

Bug 698381 is still a work in progress. The types of changes are the same but with this one the update is to a class that has a VERY long and big inheritance chain. It literally is the basis for almost anything web related so finding all the various implementations of that method proved difficult at first but thanks to the help of some DXR searches I should have it finished shortly.

With 718274 I have to admit I thought the fixes needed might actually be more difficult when I first read over it. Not to say it wasn’t interesting but the changes were pretty quick. To sum it up, there was a desire to add a method to nsContentUtils called DispatchUntrustedEvent. The name pretty much tells you what it’s intent is, to send untrusted events. What is an untrusted event? That’s another story entirely.

However at the same time they wanted to introduce another private method called DispatchEvent that both DispatchUntrustedEvent and DispatchTrustedEvent would call as they both would be written the same way other than what they would set a boolean variable aTrusted too. I won’t bother showing the .h files additions but here is the transformation in the .cpp:

From…

static
nsresult GetEventAndTarget(nsIDocument* aDoc, nsISupports* aTarget,
                           const nsAString& aEventName,
                           bool aCanBubble, bool aCancelable,
                           nsIDOMEvent** aEvent,
                           nsIDOMEventTarget** aTargetOut)
{
  nsCOMPtr domDoc = do_QueryInterface(aDoc);
  nsCOMPtr target(do_QueryInterface(aTarget));
  NS_ENSURE_TRUE(domDoc && target, NS_ERROR_INVALID_ARG);

  nsCOMPtr event;
  nsresult rv =
    domDoc->CreateEvent(NS_LITERAL_STRING("Events"), getter_AddRefs(event));
  NS_ENSURE_SUCCESS(rv, rv);

  nsCOMPtr privateEvent(do_QueryInterface(event));
  NS_ENSURE_TRUE(privateEvent, NS_ERROR_FAILURE);

  rv = event->InitEvent(aEventName, aCanBubble, aCancelable);
  NS_ENSURE_SUCCESS(rv, rv);

  rv = privateEvent->SetTrusted(true);
  NS_ENSURE_SUCCESS(rv, rv);

  rv = privateEvent->SetTarget(target);
  NS_ENSURE_SUCCESS(rv, rv);

  event.forget(aEvent);
  target.forget(aTargetOut);
  return NS_OK;
}

// static
nsresult
nsContentUtils::DispatchTrustedEvent(nsIDocument* aDoc, nsISupports* aTarget,
                                     const nsAString& aEventName,
                                     bool aCanBubble, bool aCancelable,
                                     bool *aDefaultAction)
{
  nsCOMPtr event;
  nsCOMPtr target;
  nsresult rv = GetEventAndTarget(aDoc, aTarget, aEventName, aCanBubble,
                                  aCancelable, getter_AddRefs(event),
                                  getter_AddRefs(target));
  NS_ENSURE_SUCCESS(rv, rv);

  bool dummy;
  return target->DispatchEvent(event, aDefaultAction ? aDefaultAction : &dummy);
}

To…

static
nsresult GetEventAndTarget(nsIDocument* aDoc, nsISupports* aTarget,
                           const nsAString& aEventName,
                           bool aCanBubble, bool aCancelable,
                           bool aTrusted, nsIDOMEvent** aEvent,
                           nsIDOMEventTarget** aTargetOut)
{
  nsCOMPtr domDoc = do_QueryInterface(aDoc);
  nsCOMPtr target(do_QueryInterface(aTarget));
  NS_ENSURE_TRUE(domDoc && target, NS_ERROR_INVALID_ARG);

  nsCOMPtr event;
  nsresult rv =
    domDoc->CreateEvent(NS_LITERAL_STRING("Events"), getter_AddRefs(event));
  NS_ENSURE_SUCCESS(rv, rv);

  nsCOMPtr privateEvent(do_QueryInterface(event));
  NS_ENSURE_TRUE(privateEvent, NS_ERROR_FAILURE);

  rv = event->InitEvent(aEventName, aCanBubble, aCancelable);
  NS_ENSURE_SUCCESS(rv, rv);

  rv = privateEvent->SetTrusted(aTrusted);
  NS_ENSURE_SUCCESS(rv, rv);

  rv = privateEvent->SetTarget(target);
  NS_ENSURE_SUCCESS(rv, rv);

  event.forget(aEvent);
  target.forget(aTargetOut);
  return NS_OK;
}

// static
nsresult
nsContentUtils::DispatchTrustedEvent(nsIDocument* aDoc, nsISupports* aTarget,
                                     const nsAString& aEventName,
                                     bool aCanBubble, bool aCancelable,
                                     bool *aDefaultAction)
{
  return DispatchEvent(aDoc, aTarget, aEventName, aCanBubble, aCancelable,
                       true, aDefaultAction);
}

// static
nsresult
nsContentUtils::DispatchUntrustedEvent(nsIDocument* aDoc, nsISupports* aTarget,
                                       const nsAString& aEventName,
                                       bool aCanBubble, bool aCancelable,
                                       bool *aDefaultAction)
{
  return DispatchEvent(aDoc, aTarget, aEventName, aCanBubble, aCancelable,
                       false, aDefaultAction);
}

// static
nsresult
nsContentUtils::DispatchEvent(nsIDocument* aDoc, nsISupports* aTarget,
                              const nsAString& aEventName,
                              bool aCanBubble, bool aCancelable,
                              bool aTrusted, bool *aDefaultAction)
{
  nsCOMPtr event;
  nsCOMPtr target;
  nsresult rv = GetEventAndTarget(aDoc, aTarget, aEventName, aCanBubble,
                                  aCancelable, aTrusted, getter_AddRefs(event),
                                  getter_AddRefs(target));
  NS_ENSURE_SUCCESS(rv, rv);

  bool dummy;
  return target->DispatchEvent(event, aDefaultAction ? aDefaultAction : &dummy);
}

Hoping to finish 698381 soon. Once I’m done with that I hope to find something a little bit more in-depth to help me learn more as a developer!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: