Search code examples
javascriptc++firefoxmozillaxpcom

How can we identify a unique nsHTTPChannel?


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?


Solution

  • 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.