Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

收集 In-Memory 数据源

 

Introduction

You can use XPCOM aggregation1 with the in-memory datasource. Why would you want to do this? Say you were writing a datasource2, and the way you chose to implement it was to "wrap" the in-memory datasource; i.e.,

MyClass : public nsIMyInterface, public nsIRDFDataSource {
private:
    nsCOMPtr<nsIRDFDataSource> mInner;

public:
    // nsIRDFDataSource methods
    NS_IMETHOD Init(const char* aURI) {
        return mInner->Init(aURI);
    }

    NS_IMETHOD GetURI(char* *aURI) {
        return mInner->GetURI(aURI);
    }

    // etc., for each method in nsIRDFDataSource!
};

Very painful, prone to errors, and fragile as the interfaces are still in flux (a wee bit). Aggregation to the rescue! Here are the gory details on how.

When It Won't Work

Although this magic is terribly convenient to use, it won't work in the case that you want to "override" some of the in-memory datasource's methods. For example, while writing the bookmarks datasource, I wanted to be able to trap Assert() to enforce the bookmarks datasource would only accept "bookmarks related" assertions. If I'd just delegated to the in-memory datasource, Assert() would've taken any old random garbage. Similarly, I wanted to trap Flush() so that I could write the bookmarks.html file back to disk.

In short, the only case where this technique is useful is when you're implementing a datasource to get "read-only reflection". That is, you want to reflect the contents of something as an RDF graph (presumably so that it can be aggregated with other information or displayed as styled content).

Technical Details

As before, have an nsCOMPtr as your delegate, but this time around, don't derive from nsIRDFDataSource. Also, instead of keeping an nsCOMPtr<nsIRDFDataSource>, you'll just want an nsCOMPtr<nsISupports>:

class MyClass : public nsIMyInterface {
    ...
private:
    nsCOMPtr<nsISupports> mInner;
};

Construct the datasource delegate when your object is constructed (or, at worst, when somebody QI's for it):

rv = nsComponentManager::CreateInstance(
        kRDFInMemoryDataSourceCID,
        this, /* the "outer" */
        nsCOMTypeInfo<nsISupports>::GetIID(),
        getter_AddRefs(mInner));

Note passing this as the "outer" parameter.

Now, if the in-memory datasource's implementation of QueryInterface() fails because it doesn't support the requested interface, it will forward the query interface to its "outer" (which is "us"). This preserves the symmetrical property of QueryInterface().

For us to preserve symmetry, our QueryInterface() implementation needs to forward nsIRDFDataSource to the delegate3:

NS_IMETHODIMP
MyClass::QueryInterface(REFNSIID aIID, void** aResult)
{
  NS_PRECONDITION(aResult != nsnull, "null ptr");
  if (! aResult)
    return NS_ERROR_NULL_POINTER;

  if (aIID.Equals(nsCOMTypeInfo<nsIMyInterface>::GetIID()) ||
      aIID.Equals(nsCOMTypeInfo<nsISupports>::GetIID())) {
    *aResult = NS_STATIC_CAST(nsIGlobalHistory*, this);
  }
  else if (aIID.Equals(nsCOMTypeInfo<nsIRDFDataSource>::GetIID())) {
    return mInner->QueryInterface(aIID, aResult);
  }
  else {
    *aResult = nsnull;
    return NS_NOINTERFACE;
  }

  NS_ADDREF(NS_STATIC_CAST(nsISupports*, aResult));
  return NS_OK;
}

The only other thing that you'll need to be aware of is that you'll need to QueryInterface() from nsISupports to nsIRDFDataSource before you can actually do anything useful with the datasource from within your object. For example:

NS_IMETHODIMP
MyClass::DoSomething()
{
  nsCOMPtr<nsIRDFDataSopurce> ds = do_QueryInterface(mInner);

  rv = ds->Assert(/* something useful here */);

  // etc...

  return NS_OK;
}

It may be tempting to keep a pointer to the aggregate's nsIRDFDataSource in a member variable, but you can't do that . Why? Because if you did, you'd hold a circular reference that would never unwind.

Notes

  1. Describing all of the vagaries of XPCOM aggregation is beyond the scope of this document. The basic idea is to overload QueryInterface(), allowing it to return a delegate object that supports the interface. There is some trickery involved on the delegate's part to ensure that reference counting is done sanely, and that the reflexive, symmetric, and transitive properties of QueryInterface() are preserved. If you're really interested, I'd recommend reading about it in a COM book.
  2. For more information on writing a datasource, see the RDF Datasource How-To document.
  3. You could also forward other interfaces to the mInner that you know it can support; however, this is extremely risky. It's risky because another implementation of the same object might not support those interfaces. Then the QueryInterface() will be forwarded back to you, and we'll recurse off to infinity (and beyond!...)

Interwiki Language Links

文档标签和贡献者

标签: 
 此页面的贡献者: ziyunfei, Freeopen
 最后编辑者: ziyunfei,