1

I am doing some development on the firefox both with javascript and C++ for some XPCOM components.
I am trying to monitor the http activity with nsIHttpActivityDistributor.

The problem now is , is there any flag or id that belong to nsIHttpChannel that I can use to identify a unique nsHttpChannel object?

I want to save some nsIHttpChannel referred objects in C++ and then process later in Javascript or C++. The thing is that currently I cannot find a elegent way to identify a channel object that can used both in js and C++, which is used to log it clearly into a log file.

Any idea?

winterTTr
  • 1,739
  • 1
  • 10
  • 30

1 Answers1

2

You can easily add your own data to HTTP channels, they always implement nsIPropertyBag2 and nsIWritablePropertyBag2 interfaces. Something along these lines (untested code, merely to illustrate the principle):

static PRInt64 maxChannelID = -1;

...

nsCOMPtr<nsIWritablePropertyBag2> bag = do_QueryInterface(channel);
if (!bag)
  ...

nsAutoString prop(NS_LITERAL_STRING("myChannelID"));
PRInt64 channelID;

rv = bag->GetPropertyAsInt64(prop, &channelID);
if (NS_FAILED(rv))
{
  // First time that we see that channel, assign it an ID
  channelID = ++maxChannelID;
  rv = bag->SetPropertyAsInt64(prop, channelID)
  if (NS_FAILED(rv))
    ...
}

printf("Channel ID: %i\n", channelID);

You might want to check what happens on HTTP redirect however. I think that channel properties are copied over to the new channel in that case, not sure whether this is desirable for you.

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
  • Cool job! I know nsIWritableBag is inherited by nsIHTTPChannel, but I never try to use it before! Thanks your so much! – winterTTr Oct 14 '11 at 06:28