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()