| 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   // We can get here when readyStateChanged("complete") is already received, | 
|  | 41   // don't emit reloaded, because it's already emitted from OnReadyStateChange. | 
|  | 42   if (m_state == State::FirstTimeLoading) | 
|  | 43   { | 
|  | 44     m_state = State::Loaded; | 
|  | 45     emitReloaded(); | 
|  | 46   } | 
|  | 47   return S_OK; | 
|  | 48 } | 
|  | 49 | 
|  | 50 void STDMETHODCALLTYPE WebBrowserEventsListener::OnReadyStateChange(IHTMLEventOb
     j* /*pEvtObj*/) | 
|  | 51 { | 
|  | 52   auto documentReadyState = [this]()->std::wstring | 
|  | 53   { | 
|  | 54     std::wstring notAvailableReadyState; | 
|  | 55     ATL::CComPtr<IDispatch> pDocDispatch; | 
|  | 56     m_browser->get_Document(&pDocDispatch); | 
|  | 57     ATL::CComQIPtr<IHTMLDocument2> htmlDocument2 = pDocDispatch; | 
|  | 58     if (!htmlDocument2) | 
|  | 59     { | 
|  | 60       assert(false && "htmlDocument2 in OnReadyStateChange should not be nullptr
     "); | 
|  | 61       return notAvailableReadyState; | 
|  | 62     } | 
|  | 63     ATL::CComBSTR readyState; | 
|  | 64     if (FAILED(htmlDocument2->get_readyState(&readyState)) || !readyState) | 
|  | 65     { | 
|  | 66       assert(false && "cannot obtain document readyState in OnReadyStateChange")
     ; | 
|  | 67       return notAvailableReadyState; | 
|  | 68     } | 
|  | 69     return std::wstring(readyState, readyState.Length()); | 
|  | 70   }(); | 
|  | 71   if (documentReadyState == L"loading") | 
|  | 72   { | 
|  | 73     m_state = State::Loading; | 
|  | 74   } | 
|  | 75   else if (documentReadyState == L"interactive") | 
|  | 76   { | 
|  | 77   } | 
|  | 78   else if (documentReadyState == L"complete") | 
|  | 79   { | 
|  | 80     if (m_state == State::Loading) | 
|  | 81     { | 
|  | 82       m_state = State::Loaded; | 
|  | 83       emitReloaded(); | 
|  | 84     } | 
|  | 85     else if (m_state == State::Loaded) | 
|  | 86     { | 
|  | 87       // It happens but very rearely, most often it appears on gmail. | 
|  | 88       // It seems IE prepares the 'browser' and then immediately says | 
|  | 89       // "complete" with the new URL. However all cases are related to | 
|  | 90       // some redirection technique and I could not reproduce it with local | 
|  | 91       // server which redirects, so let's wait for the user response on another | 
|  | 92       // web site when an advertisement is not blocked to better investigate | 
|  | 93       // when it happens. | 
|  | 94     } | 
|  | 95     else | 
|  | 96     { | 
|  | 97       assert(false); | 
|  | 98     } | 
|  | 99   } | 
|  | 100   else if (documentReadyState == L"uninitialized") | 
|  | 101   { | 
|  | 102   } | 
|  | 103   else | 
|  | 104   { | 
|  | 105     assert(false); | 
|  | 106   } | 
|  | 107 } | 
|  | 108 | 
|  | 109 HRESULT WebBrowserEventsListener::Init(IWebBrowser2* webBrowser, const OnDestroy
     & onDestroy) | 
|  | 110 { | 
|  | 111   if (!(m_browser = webBrowser)) | 
|  | 112   { | 
|  | 113     return E_POINTER; | 
|  | 114   } | 
|  | 115   m_onDestroy = onDestroy; | 
|  | 116   if (FAILED(WebBrowserEvents2Listener::DispEventAdvise(m_browser, &DIID_DWebBro
     wserEvents2))) | 
|  | 117   { | 
|  | 118     return E_FAIL; | 
|  | 119   } | 
|  | 120   return S_OK; | 
|  | 121 } | 
|  | 122 | 
|  | 123 void WebBrowserEventsListener::emitReloaded() | 
|  | 124 { | 
|  | 125   if (reloaded) | 
|  | 126   { | 
|  | 127     reloaded(); | 
|  | 128   } | 
|  | 129 } | 
| OLD | NEW | 
|---|