On Apr 21, 5:17 pm, mlutken <[EMAIL PROTECTED]> wrote:
> Hi
>
> I've been trying to get the pagesource via the cache, but that only
> seems to work with some pages. Mainly simpler/strait ones likewww.w3c.org.
> The strange thing is that "about:cache?device=file" does seem to
> contain the currently loaded page.
>
> My code looks like this (using gtkmozembed): I fail with pages
> likewww.google.comon the ERROR 4,
> OpenCacheEntry returns error.
>
> Any help would really be appreciated
>
> -Martin L
>
> static void ex_nsCache1()
> {
> nsresult rv;
> nsCOMPtr<nsIServiceManager> servMan;
> rv = NS_GetServiceManager(getter_AddRefs(servMan));
> if (NS_FAILED(rv)) {
> printf ( "ERROR 1\n" ); return;
> }
>
> nsCOMPtr<nsICacheService> cacheService;
> rv = servMan->GetServiceByContractID( "@mozilla.org/network/cache-
> service;1" , NS_GET_IID(nsICacheService),
> getter_AddRefs(cacheService));
> if (NS_FAILED(rv)) {
> printf ( "ERROR 2\n" ); return;
> }
> nsCOMPtr<nsICacheSession> cacheSession;
>
> nsCacheStoragePolicy storagePolicy = nsICache::STORE_ON_DISK;
>
> const char* sessionName = "HTTP";
> switch (storagePolicy) {
> case nsICache::STORE_IN_MEMORY:
> sessionName = "HTTP-memory-only";
> break;
> case nsICache::STORE_OFFLINE:
> sessionName = "HTTP-offline";
> break;
> default:
> break;
> }
>
> rv = cacheService->CreateSession( sessionName , storagePolicy,
> nsICache::STREAM_BASED , getter_AddRefs(cacheSession) );
> if (NS_FAILED(rv)) {
> printf ( "ERROR 3\n" ); return;
> }
>
> nsCAutoString cacheKey;
> const char* szUrl = "http://www.doxys.dk/doxys_homepage/
> index.html";
> cacheKey.Append( szUrl );
> nsCOMPtr<nsICacheEntryDescriptor> cacheEntry;
>
> rv = cacheSession->OpenCacheEntry(cacheKey, nsICache::ACCESS_READ,
> PR_TRUE, getter_AddRefs(cacheEntry));
> // FAILS HERE with pages likewww.google.com
> if (NS_FAILED(rv)) {
> printf ( "ERROR 4: %d\n", rv ); return;
> }
>
> nsCOMPtr<nsIInputStream> cacheEntryStream;
> rv = cacheEntry->OpenInputStream( 0,
> getter_AddRefs(cacheEntryStream) );
> if (NS_FAILED(rv)) {
> printf ( "ERROR 5: %d\n", rv ); return;
> }
>
> PRUint32 iStreamLen;
> PRUint32 iBytesRead;
> PRBool bNonBlocking;
>
> rv = cacheEntryStream->Available( &iStreamLen );
> if (NS_FAILED(rv)) {
> printf ( "ERROR 6: %d\n", rv ); return;
> }
> rv = cacheEntryStream->IsNonBlocking( &bNonBlocking );
>
> char szBuf[iStreamLen+1];
> rv = cacheEntryStream->Read( szBuf, iStreamLen, &iBytesRead);
> if (NS_FAILED(rv)) {
> printf ( "ERROR 7: %d\n", rv ); return;
> }
> printf("iBytesRead: %d\n", iBytesRead );
> printf("----------------------------\n");
> printf("%s\n", szBuf );
> printf("----------------------------\n");
>
> }
Hi,
I have some working code (in java), I hope it will help you (it loads
from cache page, that is currently displayed):
if (!loadingCompleted){
/* it can hang the caller */
LOG.warn("Page loading is not completed");
return null;
}
/* get nsIHistory */
org.mozilla.interfaces.nsIWebNavigation wn =
(org.mozilla.interfaces.nsIWebNavigation)
webBrowser.queryInterface(nsIWebNavigation.NS_IWEBNAVIGATION_IID_STR);
/* some magic code to get correct POST (??? I haven't
seen it
worked yet... maybe it varies with gecko version)*/
nsISHEntry she = null;
try {
nsIWebPageDescriptor wpd = (nsIWebPageDescriptor)
wn.queryInterface(nsIWebPageDescriptor.NS_IWEBPAGEDESCRIPTOR_IID);
she = (nsISHEntry) wpd.getCurrentDescriptor().
queryInterface(nsISHEntry.NS_ISHENTRY_IID);
} catch (XPCOMException e) {
LOG.warn("no pagedescriptor!");
}
/* create nsIChannel */
nsIURI uri = wn.getCurrentURI();
nsIIOService ios = (nsIIOService)
Mozilla.getInstance().getServiceManager().
getServiceByContractID("@mozilla.org/network/io-service;1",
nsIIOService.NS_IIOSERVICE_IID);
if (uri == null){
/* we are at about:blank or something similar, get out
of here */
return "<html></html>";
}
nsIChannel channel = ios.newChannelFromURI(uri);
channel.setLoadFlags(channel.getLoadFlags() |
nsIRequest.VALIDATE_NEVER |
nsIRequest.LOAD_FROM_CACHE |
nsICachingChannel.LOAD_ONLY_FROM_CACHE);
/* set cache Key... (?) */
nsICachingChannel cc = (nsICachingChannel) channel.
queryInterface(nsICachingChannel.NS_ICACHINGCHANNEL_IID);
if (she != null)
cc.setCacheKey(she.getCacheKey());
/* load */
nsIInputStream stream = channel.open();
/* get binary data - in JavaXPCOM simple nsIInputStream is not
binary readable */
nsIComponentManager cm =
Mozilla.getInstance().getComponentManager();
nsIBinaryInputStream bis = (nsIBinaryInputStream) cm.
createInstanceByContractID("@mozilla.org/binaryinputstream;1",
null,
nsIBinaryInputStream.NS_IBINARYINPUTSTREAM_IID);
bis.setInputStream(stream);
int count = (int) bis.available();
LOG.info ("page contents: " + count + " bytes");
byte [] ret = new byte [count];
short [] a = bis.readByteArray(count);
for (int i = 0; i< count; i++)
ret[i] = (byte) a[i];
bis.close();
/* now the page is stored in ret */
bart
PS. code is rewritten from javascript from Validator plugin for
Firefox, I don't understand every line... but it seems to work on all
pages.
_______________________________________________
dev-embedding mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-embedding