Search code examples
winapiiwebbrowser2

Make IWebBrowser2 Control safe for scripting


I'm using IWebBrowser2 control in my application to display external web pages. The problem is that the object is not safe for scripting and calls to get_Document fails (with S_FALSE as return value).

I've implemented a new class, IScriptableWebBrowser2 that inherits both from IWebBrowser2 & IObjectSafety, and tried to use it instead of IWebBrowser2 but that didn't do the trick.

How do I make my IWebBrowser2 control safe for scripting ?

class IScriptableWebBrowser2 : 
   public CComObjectRootEx<CComSingleThreadModel>,
   public IWebBrowser2,
   public IObjectSafety
{
BEGIN_COM_MAP(IScriptableWebBrowser2)
   COM_INTERFACE_ENTRY(IObjectSafety)
END_COM_MAP()

    // IObjectSafety implementation
    STDMETHODIMP GetInterfaceSafetyOptions(REFIID riid, 
                                           DWORD *pdwSupportedOptions, 
                                           DWORD *pdwEnabledOptions )
    {
        *pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER | 
                               INTERFACESAFE_FOR_UNTRUSTED_DATA;
        *pdwEnabledOptions = *pdwSupportedOptions;
        return S_OK;
    }
    STDMETHODIMP SetInterfaceSafetyOptions(REFIID riid, DWORD dwOptionSetMask, DWORD     dwEnabledOptions)
    {
        return S_OK;
    }
};

Solution

  • Well, I finally had some time to come back to this one..

    It turns out that get_Document fails if you call it BEFORE the page completely loaded but the return value (S_FALSE) indicates a completely different error ("not safe for scripting")

    btw, Loading local pages will give you the desirable behavior.

    Therefore, calling get_Document after the page was loaded (DISPID_NAVIGATECOMPLETE2, DISPID_DOWNLOADCOMPLETE, DISPID_DOCUMENTCOMPLETE) will do the trick.

    Hope this helps.