OLD | NEW |
(Empty) | |
| 1 #include "PluginStdAfx.h" |
| 2 #include "WebBrowserEventsListener.h" |
| 3 #include "PluginClientBase.h" // for UnescapeUrl |
| 4 |
| 5 WebBrowserEventsListener::WebBrowserEventsListener() |
| 6 : m_isDocumentEvents2Connected(false) |
| 7 , m_state(State::FirstTimeLoading) |
| 8 { |
| 9 } |
| 10 |
| 11 WebBrowserEventsListener::~WebBrowserEventsListener() |
| 12 { |
| 13 if (!!m_onDestroy) |
| 14 { |
| 15 m_onDestroy(); |
| 16 } |
| 17 } |
| 18 |
| 19 HRESULT STDMETHODCALLTYPE WebBrowserEventsListener::OnDocumentComplete(IDispatch
* dispFrameBrowser, VARIANT* /*variantUrl*/) |
| 20 { |
| 21 if (!dispFrameBrowser) |
| 22 { |
| 23 return E_POINTER; |
| 24 } |
| 25 |
| 26 // if it's a signal from another browser (sub-frame for-example) then ignore i
t. |
| 27 if (!m_browser.IsEqualObject(dispFrameBrowser)) |
| 28 { |
| 29 return S_OK; |
| 30 } |
| 31 |
| 32 if (!m_isDocumentEvents2Connected) |
| 33 { |
| 34 ATL::CComPtr<IDispatch> dispDocument; |
| 35 ATL::CComQIPtr<IHTMLDocument2> htmlDocument2; |
| 36 bool isHtmlDocument2 = SUCCEEDED(m_browser->get_Document(&dispDocument)) &&
(htmlDocument2 = dispDocument); |
| 37 isHtmlDocument2 && (m_isDocumentEvents2Connected = SUCCEEDED(HTMLDocumentEve
nts2Listener::DispEventAdvise(htmlDocument2))); |
| 38 } |
| 39 |
| 40 if (m_state == State::FirstTimeLoading) |
| 41 { |
| 42 m_state = State::Loaded; |
| 43 emitReloaded(); |
| 44 } |
| 45 return S_OK; |
| 46 } |
| 47 |
| 48 void STDMETHODCALLTYPE WebBrowserEventsListener::OnReadyStateChange(IHTMLEventOb
j* /*pEvtObj*/) |
| 49 { |
| 50 auto documentReadyState = [this]()->std::wstring |
| 51 { |
| 52 std::wstring notAvailableReadyState; |
| 53 ATL::CComPtr<IDispatch> pDocDispatch; |
| 54 m_browser->get_Document(&pDocDispatch); |
| 55 ATL::CComQIPtr<IHTMLDocument2> htmlDocument2 = pDocDispatch; |
| 56 if (!htmlDocument2) |
| 57 { |
| 58 assert(false && "htmlDocument2 in OnReadyStateChange should not be nullptr
"); |
| 59 return notAvailableReadyState; |
| 60 } |
| 61 ATL::CComBSTR readyState; |
| 62 if (FAILED(htmlDocument2->get_readyState(&readyState)) || !readyState) |
| 63 { |
| 64 assert(false && "cannot obtain document readyState in OnReadyStateChange")
; |
| 65 return notAvailableReadyState; |
| 66 } |
| 67 return std::wstring(readyState, readyState.Length()); |
| 68 }(); |
| 69 if (documentReadyState == L"loading") |
| 70 { |
| 71 m_state = State::Loading; |
| 72 } |
| 73 else if (documentReadyState == L"interactive") |
| 74 { |
| 75 } |
| 76 else if (documentReadyState == L"complete") |
| 77 { |
| 78 if (m_state == State::Loading) |
| 79 { |
| 80 m_state = State::Loaded; |
| 81 emitReloaded(); |
| 82 } |
| 83 else |
| 84 { |
| 85 assert(false); |
| 86 } |
| 87 } |
| 88 else if (documentReadyState == L"uninitialized") |
| 89 { |
| 90 } |
| 91 else |
| 92 { |
| 93 assert(false); |
| 94 } |
| 95 } |
| 96 |
| 97 HRESULT WebBrowserEventsListener::Init(IWebBrowser2* webBrowser, const OnDestroy
& onDestroy) |
| 98 { |
| 99 if (!(m_browser = webBrowser)) |
| 100 { |
| 101 return E_POINTER; |
| 102 } |
| 103 m_onDestroy = onDestroy; |
| 104 if (FAILED(WebBrowserEvents2Listener::DispEventAdvise(m_browser, &DIID_DWebBro
wserEvents2))) |
| 105 { |
| 106 return E_FAIL; |
| 107 } |
| 108 return S_OK; |
| 109 } |
| 110 |
| 111 void WebBrowserEventsListener::emitReloaded() |
| 112 { |
| 113 if (reloaded) |
| 114 { |
| 115 reloaded(); |
| 116 } |
| 117 } |
OLD | NEW |