获取内置 Mozilla Firefox 组件的接口实现

Posted

技术标签:

【中文标题】获取内置 Mozilla Firefox 组件的接口实现【英文标题】:Getting Interface Implementation of built-in Mozilla Firefox Component 【发布时间】:2013-04-30 03:15:09 【问题描述】:

我目前正在尝试用 C++ 开发一个自定义密码管理器。

我已经开发了一个实现 nsILoginManagerStorage 接口的可部署模块,可以将它安装在 firefox 上,并且当密码字段出现时,它会被 firefox 正确调用。

问题是当我尝试实例化要返回的 nsILoginInfo 对象时,do_CreateInstance 函数总是返回 null。 我的方法实现是:

NS_IMETHODIMP FirefoxComponent::FindLogins(uint32_t *count, const nsAString & aHostname, const nsAString & aActionURL, const nsAString & aHttpRealm, nsILoginInfo * **logins)

    nsILoginInfo ** array = static_cast<nsILoginInfo**>(nsMemory::Alloc(sizeof(nsILoginInfo*)));
    nsresult result;
    nsCOMPtr<nsILoginInfo> loginInfo = do_CreateInstance("@mozilla.org/login-manager/loginInfo;1" , &result);
    //nsCOMPtr<nsILoginManager> loginInfo = do_CreateInstance("@mozilla.org/login-manager;1" , &result);


    if (NS_FAILED(result))
        printf("shouldn't be here!!\n");
        return result;
    

我尝试获取 nsILoginManager 实例(只是为了检查它是否有效),但结果相同。 nsILoginInfo 可以通过 java 脚本在 firefox 上使用:

    Components.classes["@mozilla.org/loginmanager/loginInfo;1"].createInstance(Components.interfaces.nsILoginInfo);

我在 Ubuntu x64 上使用 firefox 20.0 和 xul-runner-sdk 20.0(与 20.0.1 的结果相同),并使用 QtCreator(用于 x64)进行构建。

我的代码灵感来自https://github.com/infinity0/mozilla-gnome-keyring

既然我现在 nsILoginInfo 已正确加载到 firefox 中,是否有任何必需的字段/信息让 firefox 允许我访问这些接口?

感谢您的支持。

编辑: 试图通过直接访问组件管理器来加载模块,但我无法加载组件管理器。

    nsIComponentManager * manager;

    result = NS_GetComponentManager(&manager);


    if (NS_FAILED(result))
        printf("failed getting component manager!!\n");
        return result;
    

【问题讨论】:

【参考方案1】:

经过大量试验和错误后,我发现此错误是由于库链接错误造成的。我缺少一个库 (libxpcom.so)。

为了正确编译和运行它,我使用了库 libxpcom.so 和 libxpcomglue_s.a,它们都位于 gecko sdk/xul-runner lib 文件夹中。

有关在每个平台上编译哪些库的更多信息: https://developer.mozilla.org/en-US/docs/XPCOM_Glue

【讨论】:

以上是关于获取内置 Mozilla Firefox 组件的接口实现的主要内容,如果未能解决你的问题,请参考以下文章