sonicbb
小狐狸
小狐狸
  • UID30529
  • 注册日期2009-09-29
  • 最后登录2009-10-06
  • 发帖数2
  • 经验10枚
  • 威望0点
  • 贡献值0点
  • 好评度0点
阅读:5086回复:2

[求助]嵌入gecko的简单例程,在调用nsIWebNavigation接口的LoadURI时返回错误

楼主#
更多 发布于:2009-10-06 15:02
环境:
- xulrunner-1.9.1.3(用mozilla源码重新编译的)
- redhat RHEL5.2
- gtk+-2.0
- gcc-4.1.2

例程很简单,目标是开一个GTK的window,把gecko往里面一包,打开一个html页面。例程源代码见下面。

问题:webNavigation->LoadURI()返回错误码0x805e000a(NS_ERROR_CONTENT_BLOCKED)。这个帖子http://www.nabble.com/nsIWebNavigation::LoadURI-fails-with-NS_ERROR_CONTENT_BLOCKED-td23851286.html也提到相同的问题,但是没有解决方案。不知道这里是否有人遇到过这样的问题?

源代码:
#include <iostream>
using namespace std;
#include <stdio.h>
#include <stdlib.h>
/* We can get limits of the operating system by including this header
 */
#include <limits.h>
#include <gtk/gtk.h>

/* XPCOM Glue is a static library which component developers and embedders
 * can link against. It allows developers to link only against the frozen
 * XPCOM method symbols and maintain compatibility with multiple versions 
 * XPCOM.
 */
#define XPCOM_GLUE
#include "mozilla-config.h"
// #include "xpcom-config.h"
#include "nsXPCOMGlue.h"
#include "nsDebug.h"
#include "nsCOMPtr.h"
#include "widget/nsIBaseWindow.h"
#include "nsILocalFile.h"
#include "nsIWebBrowser.h"
#include "docshell/nsIWebNavigation.h"
#include "nsEmbedCID.h"
#include "nsEmbedString.h"
#include "xulapp/nsXULAppAPI.h"
#include "nsComponentManagerUtils.h"

XRE_InitEmbeddingType XRE_InitEmbedding;
XRE_TermEmbeddingType XRE_TermEmbedding;

static gboolean delete_event(GtkWidget *widget,
                             GdkEvent *event,
                             gpointer data){
  gtk_main_quit();
  return FALSE;
}

int main(int argc, char *argv[])
{
  GtkWidget *window;
//  nsCOMPtr<nsIBaseWindow> baseWindow;
  nsCOMPtr<nsIWebBrowser> webBrowser;
  nsCOMPtr<nsILocalFile> libxul;
  nsCOMPtr<nsIWebNavigation> webNavigation;
  nsDynamicFunctionLoad nsFuncs[] = {
    {"XRE_InitEmbedding", (NSFuncPtr*)&XRE_InitEmbedding},
    {"XRE_TermEmbedding", (NSFuncPtr*)&XRE_TermEmbedding},
    {0,0}
  };

  nsresult rv;
  gtk_init(&argc,&argv);
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_widget_show(window);
  rv = XPCOMGlueStartup("/home/sonic/ws/mozilla-1.9.1/dist/xulrunner/libxpcom.so");
  if(NS_FAILED(rv)){
    printf("XPCOMGlueStartup\n");
  }
  rv = XPCOMGlueLoadXULFunctions(nsFuncs);
  if(NS_FAILED(rv)){
    printf("XPCOMGlueLoadXULFunctions\n");
  }
  rv = NS_NewNativeLocalFile(nsEmbedCString("/home/sonic/ws/mozilla-1.9.1/dist/xulrunner"), PR_FALSE, getter_AddRefs(libxul));
  if(NS_FAILED(rv)){
    printf("NS_NewNativeLocalFile\n");
  }
  rv = XRE_InitEmbedding(libxul,nsnull,nsnull,nsnull,0);
  if(NS_FAILED(rv)){
    printf("XRE_InitEmbedding\n");
  }
  webBrowser = do_CreateInstance(NS_WEBBROWSER_CONTRACTID, &rv);
  if(NS_FAILED(rv)){
    printf("do_CreateInstance webBrowser\n");
  }
//  baseWindow = do_QueryInterface(webBrowser,&rv);
  nsCOMPtr<nsIBaseWindow> baseWindow(do_QueryInterface(webBrowser, &rv));
  if(NS_FAILED(rv)){
    printf("do_QueryInterface baseWindow\n");
  }
//  rv = baseWindow->InitWindow(window,0,0,0,300,400);
  rv = baseWindow->InitWindow(window, nsnull, 0,0,
                              GTK_WIDGET(window)->allocation.width,
                              GTK_WIDGET(window)->allocation.height);
  if(NS_FAILED(rv)){
    printf("InitWindow\n");
  }
  rv = baseWindow->Create();
  if(NS_FAILED(rv)){
    printf("Create errcode=%x\n",rv);
  }
  rv = baseWindow->SetVisibility(PR_TRUE);
  if(NS_FAILED(rv)){
    printf("SetVisibility\n");
  }

  webNavigation = do_QueryInterface(webBrowser);

  rv = webNavigation->LoadURI(
                 NS_ConvertASCIItoUTF16("file:///home/sonic/workspace/tmp/test.html").get(),
                 nsIWebNavigation::LOAD_FLAGS_NONE,nsnull,nsnull,nsnull);
  if(NS_FAILED(rv)){
    printf("LoadURI errcode=%x;\n", rv);
  }
  g_signal_connect(G_OBJECT(window),
                   "delete_event",
                   G_CALLBACK(delete_event),0);
  gtk_main();
  printf("exit...\n");
  XRE_TermEmbedding();
  XPCOMGlueShutdown();
  return 0;
}
marffin
火狐狸
火狐狸
  • UID26524
  • 注册日期2008-10-04
  • 最后登录2012-10-17
  • 发帖数188
  • 经验10枚
  • 威望0点
  • 贡献值0点
  • 好评度0点
1楼#
发布于:2009-10-06 15:02
问一句没关系的,为啥不用xul构造界面?用xul一切都是原生的
sonicbb
小狐狸
小狐狸
  • UID30529
  • 注册日期2009-09-29
  • 最后登录2009-10-06
  • 发帖数2
  • 经验10枚
  • 威望0点
  • 贡献值0点
  • 好评度0点
2楼#
发布于:2009-10-06 15:02
因为最后我根本不需要界面。功能需求是:给gecko一个URL,让它把页面下载下来(如果有需要执行的javascript,执行之),然后生成dom tree;不必做显示、排版。对性能也有需求,越快越好。

我的想法是用C++直接调用Gecko里面的docshell,获得dom tree。不知道路子是否正确。
游客

返回顶部