xpcom是firefox跨平台的组建。他可以通过其他语言(比如说python java javascrip)调用c/c++写的组建。 而IDL文件就是一种中间语言。python java javascript只能使用idl中定义的接口。下面代码演示如何通过python使用xpcom组建。
from xpcom import components, Exception, _xpcom
#constant variable
STORE_IN_MEMORY=components.interfaces.nsICache.STORE_IN_MEMORY
STREAM_BASED=components.interfaces.nsICache.STREAM_BASED
ACCESS_WRITE=components.interfaces.nsICache.ACCESS_WRITE
ACCESS_READ=components.interfaces.nsICache.ACCESS_READ
BLOCKING=components.interfaces.nsICache.BLOCKING
offset=0;
cacheservice_instance=components.classes["@mozilla.org/network/cache-service;1"].createInstance()
cacheservice=cacheservice_instance.queryInterface(components.interfaces.nsICacheService)
cacheKey="http://www.mozilla.org/somekey"
data="dajiahaoyahehe"
def write_data():
session=cacheservice.createSession("ucflyimage",STORE_IN_MEMORY,STREAM_BASED)
#write data;
descriptor=session.openCacheEntry(cacheKey,ACCESS_WRITE,BLOCKING)
output=descriptor.openOutputStream(offset)
output.write(data,len(data))
descriptor.bind()
descriptor.close()
write_data()