Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes
nsMetricsConfig Class Reference

#include <nsMetricsConfig.h>

Collaboration diagram for nsMetricsConfig:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 nsMetricsConfig ()
 ~nsMetricsConfig ()
PRBool Init ()
 This method must be called before using an instance of this class.
void Reset ()
 Restore the default configuration.
nsresult Load (nsIFile *file)
 Load the metrics configuration from disk.
nsresult Save (nsILocalFile *file)
 Writes the current metrics configuration to disk.
PRBool IsEventEnabled (const nsAString &eventNS, const nsAString &eventName) const
 Call this method to determine if the given event type is enabled for collection.
void SetEventEnabled (const nsAString &eventNS, const nsAString &eventName, PRBool enabled)
 Sets a particular event to be enabled or disabled.
void GetEvents (nsTArray< nsString > &events)
 Call this method to get a list of all events that are enabled.
void ClearEvents ()
 Clears the set of events in this config.
PRInt32 EventLimit () const
 Get the limit on the number of events that should be collected.
void SetEventLimit (PRInt32 limit)
 Sets the event limit.
PRInt32 UploadInterval () const
 Get the upload interval (measured in seconds).
void SetUploadInterval (PRInt32 uploadInterval)
 Set the upload interval (measured in seconds).
PRBool HasConfig () const
 Returns true if there was a <config> present in the response.

Private Types

typedef void(nsMetricsConfig::* ForEachChildElementCallback )(nsIDOMElement *)

Private Member Functions

void ForEachChildElement (nsIDOMElement *elem, ForEachChildElementCallback cb)
void ProcessToplevelElement (nsIDOMElement *elem)
void ProcessConfigChild (nsIDOMElement *elem)
void ProcessCollectorElement (nsIDOMElement *elem)

Static Private Member Functions

static PLDHashOperator PR_CALLBACK CopyKey (nsStringHashKey *key, void *userData)

Private Attributes

nsTHashtable< nsStringHashKeymEventSet
nsDataHashtable
< nsStringHashKey, nsString
mNSURIToPrefixMap
PRInt32 mEventLimit
PRInt32 mUploadInterval
PRBool mHasConfig

Detailed Description

Definition at line 50 of file nsMetricsConfig.h.


Member Typedef Documentation

typedef void(nsMetricsConfig::* nsMetricsConfig::ForEachChildElementCallback)(nsIDOMElement *) [private]

Definition at line 146 of file nsMetricsConfig.h.


Constructor & Destructor Documentation

Definition at line 97 of file nsMetricsConfig.cpp.

{
}

Definition at line 54 of file nsMetricsConfig.h.

{}

Member Function Documentation

Clears the set of events in this config.

Definition at line 411 of file nsMetricsConfig.cpp.

{
  NS_ASSERTION(mEventSet.IsInitialized(), "nsMetricsConfig::Init not called");
  mEventSet.Clear();
}

Here is the call graph for this function:

Here is the caller graph for this function:

PLDHashOperator PR_CALLBACK nsMetricsConfig::CopyKey ( nsStringHashKey key,
void userData 
) [static, private]

Definition at line 418 of file nsMetricsConfig.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

Get the limit on the number of events that should be collected.

Definition at line 103 of file nsMetricsConfig.h.

                             {
    NS_ASSERTION(mEventSet.IsInitialized(),
                 "nsMetricsConfig::Init not called");
    return mEventLimit;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 301 of file nsMetricsConfig.cpp.

{
  nsCOMPtr<nsIDOMNode> node, next;
  elem->GetFirstChild(getter_AddRefs(node));
  while (node) {
    nsCOMPtr<nsIDOMElement> childElem = do_QueryInterface(node);
    if (childElem) {
      // Skip elements that are not in our namespace
      nsString namespaceURI;
      childElem->GetNamespaceURI(namespaceURI);
      if (namespaceURI.Equals(NS_LITERAL_STRING(NS_METRICS_NAMESPACE)))
        (this->*callback)(childElem);
    }
    node->GetNextSibling(getter_AddRefs(next));
    node.swap(next);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

Call this method to get a list of all events that are enabled.

The event names are prefixed with the namespace, separated by a colon.

Definition at line 426 of file nsMetricsConfig.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

PRBool nsMetricsConfig::HasConfig ( ) const [inline]

Returns true if there was a <config> present in the response.

Definition at line 139 of file nsMetricsConfig.h.

                           {
    NS_ASSERTION(mEventSet.IsInitialized(),
                 "nsMetricsConfig::Init not called");
    return mHasConfig;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

This method must be called before using an instance of this class.

Definition at line 102 of file nsMetricsConfig.cpp.

{
  if (!mEventSet.Init() || !mNSURIToPrefixMap.Init()) {
    return PR_FALSE;
  }
  Reset();
  return PR_TRUE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PRBool nsMetricsConfig::IsEventEnabled ( const nsAString &  eventNS,
const nsAString &  eventName 
) const

Call this method to determine if the given event type is enabled for collection.

Definition at line 390 of file nsMetricsConfig.cpp.

{
  NS_ASSERTION(mEventSet.IsInitialized(), "nsMetricsConfig::Init not called");
  return mEventSet.GetEntry(MakeKey(eventNS, eventName)) != nsnull;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Load the metrics configuration from disk.

Definition at line 126 of file nsMetricsConfig.cpp.

{
  // The given file references a XML file with the following structure:
  //
  // <response xmlns="http://www.mozilla.org/metrics">
  //   <config xmlns:foo="http://foo.com/metrics">
  //     <collectors>
  //       <collector type="ui"/>
  //       <collector type="document"/>
  //       <collector type="window"/>
  //       <collector type="foo:mystat"/>
  //     </collectors>
  //     <limit events="200"/>
  //     <upload interval="600"/>
  //   </config>
  // </response>

  NS_ASSERTION(mEventSet.IsInitialized(), "nsMetricsConfig::Init not called");

  PRInt64 fileSize;
  nsresult rv = file->GetFileSize(&fileSize);
  NS_ENSURE_SUCCESS(rv, rv);
  NS_ENSURE_STATE(fileSize <= PR_INT32_MAX);

  nsCOMPtr<nsIFileInputStream> stream =
      do_CreateInstance(NS_LOCALFILEINPUTSTREAM_CONTRACTID);
  NS_ENSURE_STATE(stream);
  rv = stream->Init(file, -1, -1, 0);
  NS_ENSURE_SUCCESS(rv, rv);

  nsCOMPtr<nsIDOMParser> parser = do_CreateInstance(NS_DOMPARSER_CONTRACTID);
  NS_ENSURE_STATE(parser);

  nsCOMPtr<nsIDOMDocument> doc;
  parser->ParseFromStream(stream, nsnull, PRInt32(fileSize), "application/xml",
                          getter_AddRefs(doc));
  NS_ENSURE_STATE(doc);

  // Now, walk the DOM.  Most elements are optional, but we check the root
  // element to make sure it's a valid response document.
  nsCOMPtr<nsIDOMElement> elem;
  doc->GetDocumentElement(getter_AddRefs(elem));
  NS_ENSURE_STATE(elem);

  nsString nameSpace;
  elem->GetNamespaceURI(nameSpace);
  if (!nameSpace.Equals(NS_LITERAL_STRING(NS_METRICS_NAMESPACE))) {
    // We have a root element, but it's the wrong namespace
    return NS_ERROR_FAILURE;
  }

  nsString tag;
  elem->GetLocalName(tag);
  if (!tag.Equals(NS_LITERAL_STRING("response"))) {
    // The root tag isn't what we're expecting
    return NS_ERROR_FAILURE;
  }

  // At this point, we can clear our old configuration.
  Reset();

  ForEachChildElement(elem, &nsMetricsConfig::ProcessToplevelElement);
  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 351 of file nsMetricsConfig.cpp.

{
  // Make sure we are dealing with a <collector> element.
  nsString localName;
  elem->GetLocalName(localName);
  if (!localName.Equals(NS_LITERAL_STRING("collector")))
    return;

  nsString type;
  elem->GetAttribute(NS_LITERAL_STRING("type"), type);
  if (type.IsEmpty())
    return;

  // Get the namespace URI specified by any prefix of |type|.
  nsCOMPtr<nsIDOM3Node> node = do_QueryInterface(elem);
  if (!node)
    return;

  // Check to see if this type references a specific namespace.
  PRInt32 colon = FindChar(type, ':');

  nsString namespaceURI;
  if (colon == -1) {
    node->LookupNamespaceURI(EmptyString(), namespaceURI);
    // value is the EventName
  } else {
    // value is NamespacePrefix + ":" + EventName
    nsString prefix(StringHead(type, colon));
    node->LookupNamespaceURI(prefix, namespaceURI);
    type.Cut(0, colon + 1);

    // Add this namespace -> prefix mapping to our lookup table
    mNSURIToPrefixMap.Put(namespaceURI, prefix);
  }

  mEventSet.PutEntry(MakeKey(namespaceURI, type));
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 334 of file nsMetricsConfig.cpp.

{
  // Process a config element child

  nsString name;
  elem->GetLocalName(name);
  if (name.Equals(NS_LITERAL_STRING("collectors"))) {
    // Enumerate <collector> elements
    ForEachChildElement(elem, &nsMetricsConfig::ProcessCollectorElement);
  } else if (name.Equals(NS_LITERAL_STRING("limit"))) {
    ReadIntegerAttr(elem, NS_LITERAL_STRING("events"), &mEventLimit);
  } else if (name.Equals(NS_LITERAL_STRING("upload"))) {
    ReadIntegerAttr(elem, NS_LITERAL_STRING("interval"), &mUploadInterval);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 321 of file nsMetricsConfig.cpp.

{
  // Process a top-level element

  nsString name;
  elem->GetLocalName(name);
  if (name.Equals(NS_LITERAL_STRING("config"))) {
    mHasConfig = PR_TRUE;
    ForEachChildElement(elem, &nsMetricsConfig::ProcessConfigChild);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

Restore the default configuration.

Definition at line 112 of file nsMetricsConfig.cpp.

{
  // By default, we have no event limit, but all collectors are disabled
  // until we're told by the server to enable them.
  NS_ASSERTION(mEventSet.IsInitialized(), "nsMetricsConfig::Init not called");

  mEventSet.Clear();
  mNSURIToPrefixMap.Clear();
  mEventLimit = PR_INT32_MAX;
  mUploadInterval = NS_DEFAULT_UPLOAD_INTERVAL_SEC;
  mHasConfig = PR_FALSE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Writes the current metrics configuration to disk.

Definition at line 192 of file nsMetricsConfig.cpp.

{
  nsCOMPtr<nsIDOMDocument> doc =
    do_CreateInstance("@mozilla.org/xml/xml-document;1");
  NS_ENSURE_STATE(doc);

  nsCOMPtr<nsIDOMElement> response;
  nsMetricsUtils::CreateElement(doc, NS_LITERAL_STRING("response"),
                                getter_AddRefs(response));
  NS_ENSURE_STATE(response);

  nsCOMPtr<nsIDOMElement> config;
  nsMetricsUtils::CreateElement(doc, NS_LITERAL_STRING("config"),
                                getter_AddRefs(config));
  NS_ENSURE_STATE(config);

  nsCOMPtr<nsIDOMElement> collectors;
  nsMetricsUtils::CreateElement(doc, NS_LITERAL_STRING("collectors"),
                                getter_AddRefs(collectors));
  NS_ENSURE_STATE(collectors);

  nsTArray<nsString> events;
  GetEvents(events);

  nsCOMPtr<nsIDOMNode> nodeOut;
  nsresult rv;

  for (PRUint32 i = 0; i < events.Length(); ++i) {
    nsString eventNS, eventName;
    SplitKey(events[i], eventNS, eventName);

    nsString prefix;
    if (!eventNS.Equals(NS_LITERAL_STRING(NS_METRICS_NAMESPACE))) {
      if (!mNSURIToPrefixMap.Get(eventNS, &prefix)) {
        MS_LOG(("uri %s not in prefix map",
                NS_ConvertUTF16toUTF8(eventNS).get()));
        continue;
      }

      // Declare the namespace prefix on the root element
      nsString attrName(NS_LITERAL_STRING("xmlns:"));
      attrName.Append(prefix);
      response->SetAttribute(attrName, eventNS);
    }

    nsCOMPtr<nsIDOMElement> collector;
    nsMetricsUtils::CreateElement(doc, NS_LITERAL_STRING("collector"),
                                  getter_AddRefs(collector));
    NS_ENSURE_STATE(collector);

    nsString shortName;
    if (!prefix.IsEmpty()) {
      shortName = prefix;
      shortName.Append(':');
    }
    shortName.Append(eventName);

    collector->SetAttribute(NS_LITERAL_STRING("type"), eventName);
    collectors->AppendChild(collector, getter_AddRefs(nodeOut));
  }
  config->AppendChild(collectors, getter_AddRefs(nodeOut));

  if (mEventLimit != PR_INT32_MAX) {
    nsCOMPtr<nsIDOMElement> limit;
    nsMetricsUtils::CreateElement(doc, NS_LITERAL_STRING("limit"),
                                  getter_AddRefs(limit));
    NS_ENSURE_STATE(limit);

    nsString limitStr;
    AppendInt(limitStr, mEventLimit);
    limit->SetAttribute(NS_LITERAL_STRING("events"), limitStr);
    config->AppendChild(limit, getter_AddRefs(nodeOut));
  }

  nsCOMPtr<nsIDOMElement> upload;
  nsMetricsUtils::CreateElement(doc, NS_LITERAL_STRING("upload"),
                                getter_AddRefs(upload));
  NS_ENSURE_STATE(upload);

  nsString intervalStr;
  AppendInt(intervalStr, mUploadInterval);
  upload->SetAttribute(NS_LITERAL_STRING("interval"), intervalStr);
  config->AppendChild(upload, getter_AddRefs(nodeOut));

  response->AppendChild(config, getter_AddRefs(nodeOut));

  nsCOMPtr<nsIDOMSerializer> ds =
    do_CreateInstance(NS_XMLSERIALIZER_CONTRACTID);
  NS_ENSURE_STATE(ds);

  nsString docText;
  rv = ds->SerializeToString(response, docText);
  NS_ENSURE_SUCCESS(rv, rv);

  NS_ConvertUTF16toUTF8 utf8Doc(docText);
  PRInt32 num = utf8Doc.Length();

  PRFileDesc *fd;
  rv = file->OpenNSPRFileDesc(
      PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE, 0600, &fd);
  NS_ENSURE_SUCCESS(rv, rv);

  PRBool success = (PR_Write(fd, utf8Doc.get(), num) == num);
  PR_Close(fd);

  return success ? NS_OK : NS_ERROR_FAILURE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void nsMetricsConfig::SetEventEnabled ( const nsAString &  eventNS,
const nsAString &  eventName,
PRBool  enabled 
)

Sets a particular event to be enabled or disabled.

Definition at line 398 of file nsMetricsConfig.cpp.

{
  NS_ASSERTION(mEventSet.IsInitialized(), "nsMetricsConfig::Init not called");
  nsString key = MakeKey(eventNS, eventName);
  if (enabled) {
    mEventSet.PutEntry(key);
  } else {
    mEventSet.RemoveEntry(key);
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

Sets the event limit.

Definition at line 112 of file nsMetricsConfig.h.

                                    {
    NS_ASSERTION(mEventSet.IsInitialized(),
                 "nsMetricsConfig::Init not called");
    mEventLimit = limit;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

void nsMetricsConfig::SetUploadInterval ( PRInt32  uploadInterval) [inline]

Set the upload interval (measured in seconds).

Definition at line 130 of file nsMetricsConfig.h.

                                                 {
    NS_ASSERTION(mEventSet.IsInitialized(),
                 "nsMetricsConfig::Init not called");
    mUploadInterval = uploadInterval;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

Get the upload interval (measured in seconds).

Definition at line 121 of file nsMetricsConfig.h.

                                 {
    NS_ASSERTION(mEventSet.IsInitialized(),
                 "nsMetricsConfig::Init not called");
    return mUploadInterval;
  }

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 160 of file nsMetricsConfig.h.

Definition at line 158 of file nsMetricsConfig.h.

Definition at line 162 of file nsMetricsConfig.h.

Definition at line 159 of file nsMetricsConfig.h.

Definition at line 161 of file nsMetricsConfig.h.


The documentation for this class was generated from the following files: