bool BrowserWrapper::GetDocumentFromWindow(IHTMLWindow2 *window, IHTMLDocument2 **doc) { HRESULT hr = window->get_document(doc); if (SUCCEEDED(hr)) { return true; } if (hr == E_ACCESSDENIED) { // Cross-domain documents may throw Access Denied. If so, // get the document through the IWebBrowser2 interface. CComPtr<IWebBrowser2> window_browser; CComQIPtr<IServiceProvider> service_provider(window); hr = service_provider->QueryService(IID_IWebBrowserApp, &window_browser); if (FAILED(hr)) { return false; } CComQIPtr<IDispatch> document_dispatch; hr = window_browser->get_Document(&document_dispatch); if (FAILED(hr)) { return false; } hr = document_dispatch->QueryInterface(doc); if (FAILED(hr)) { return false; } return true; } return false; }
bool Browser::GetDocumentFromWindow(IHTMLWindow2* window, IHTMLDocument2** doc) { LOG(TRACE) << "Entering Browser::GetDocumentFromWindow"; HRESULT hr = window->get_document(doc); if (SUCCEEDED(hr)) { return true; } if (hr == E_ACCESSDENIED) { // Cross-domain documents may throw Access Denied. If so, // get the document through the IWebBrowser2 interface. CComPtr<IWebBrowser2> window_browser; CComQIPtr<IServiceProvider> service_provider(window); hr = service_provider->QueryService(IID_IWebBrowserApp, &window_browser); if (FAILED(hr)) { LOGHR(WARN, hr) << "Unable to get browser, call to IServiceProvider::QueryService failed for IID_IWebBrowserApp"; return false; } CComQIPtr<IDispatch> document_dispatch; hr = window_browser->get_Document(&document_dispatch); if (FAILED(hr) || hr == S_FALSE) { LOGHR(WARN, hr) << "Unable to get document, call to IWebBrowser2::get_Document failed"; return false; } hr = document_dispatch->QueryInterface(doc); if (FAILED(hr)) { LOGHR(WARN, hr) << "Unable to query document, call to IDispatch::QueryInterface failed."; return false; } return true; } else { LOGHR(WARN, hr) << "Unable to get main document, IHTMLWindow2::get_document returned other than E_ACCESSDENIED"; } return false; }
int Element::GetParentDocument(IHTMLWindow2* parent_window, IHTMLDocument2** parent_doc) { LOG(TRACE) << "Entering Element::GetParentDocument"; HRESULT hr = parent_window->get_document(parent_doc); if (FAILED(hr)) { if (hr == E_ACCESSDENIED) { // Cross-domain documents may throw Access Denied. If so, // get the document through the IWebBrowser2 interface. CComPtr<IWebBrowser2> window_browser; CComQIPtr<IServiceProvider> service_provider(parent_window); hr = service_provider->QueryService(IID_IWebBrowserApp, &window_browser); if (FAILED(hr)) { LOGHR(WARN, hr) << "Unable to get browser, call to IServiceProvider::QueryService failed for IID_IWebBrowserApp"; return ENOSUCHDOCUMENT; } CComQIPtr<IDispatch> parent_doc_dispatch; hr = window_browser->get_Document(&parent_doc_dispatch); if (FAILED(hr)) { LOGHR(WARN, hr) << "Unable to get document, call to IWebBrowser2::get_Document failed"; return ENOSUCHDOCUMENT; } try { hr = parent_doc_dispatch->QueryInterface<IHTMLDocument2>(parent_doc); if (FAILED(hr)) { LOGHR(WARN, hr) << "Unable to get document, QueryInterface for IHTMLDocument2 failed"; return ENOSUCHDOCUMENT; } } catch(...) { LOG(WARN) << "Unable to get document, exception thrown attempting to QueryInterface for IHTMLDocument2"; return ENOSUCHDOCUMENT; } } else { LOGHR(WARN, hr) << "Unable to get document, IHTMLWindow2::get_document failed with error code other than E_ACCESSDENIED"; return ENOSUCHDOCUMENT; } } return SUCCESS; }