123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978 |
- #include "platform/CCPlatformConfig.h"
- #if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
- #ifndef APIENTRY
- #define APIENTRY __stdcall
- #endif // !APIENTRY
- #undef MessageBox
- #define MessageBox MessageBoxW
- //#define NOMB
- #ifndef NTDDI_WIN8
- #define NTDDI_WIN8 0x06020000
- #endif
- #include <atlbase.h>
- #include <atlwin.h>
- #include <ExDispid.h>
- #include <ExDisp.h>
- #include <UrlMon.h>
- #include <comutil.h>
- #pragma comment(lib,"comsuppw")
- #include "UIWebViewImpl-win32.h"
- #include "UIWebView.h"
- #include "base/CCDirector.h"
- #include "platform/CCFileUtils.h"
- #include "platform/CCGLView.h"
- class Win32WebControl : public DWebBrowserEvents2
- {
- public:
- Win32WebControl();
- bool createWebView(
- const std::function<bool (const std::string &)> &shouldStartLoading,
- const std::function<void (const std::string &)> &didFinishLoading,
- const std::function<void (const std::string &)> &didFailLoading,
- const std::function<void (const std::string &)> &onJsCallback);
- void removeWebView();
- void setWebViewRect(const int left, const int top, const int width, const int height);
- void setJavascriptInterfaceScheme(const std::string &scheme) const;
- void loadData(const std::string &data, const std::string &MIMEType, const std::string &encoding, const std::string &baseURL) const;
- void loadHTMLString(const std::string &html, const std::string &baseURL);
- void loadUrl(const std::string &url) const;
- void loadFile(const std::string &filePath) const;
- void postUrl(const std::string &url, const std::string& data, uint32_t len) const;
- void stopLoading() const;
- void reload() const;
- bool canGoBack() const;
- bool canGoForward() const;
- void goBack() const;
- void goForward() const;
- void evaluateJS(const std::string &js) const;
- void setScalesPageToFit(const bool scalesPageToFit) const;
- void setWebViewVisible(const bool visible) const;
- // Implement IUnknown
- virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void **ppvObject);
- virtual ULONG STDMETHODCALLTYPE AddRef(void);
- virtual ULONG STDMETHODCALLTYPE Release(void);
- // Implement IDispatch
- virtual HRESULT STDMETHODCALLTYPE GetTypeInfoCount(UINT *pctinfo);
- virtual HRESULT STDMETHODCALLTYPE GetTypeInfo(UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo);
- virtual HRESULT STDMETHODCALLTYPE GetIDsOfNames(
- REFIID riid,
- LPOLESTR *rgszNames,
- UINT cNames,
- LCID lcid,
- DISPID *rgDispId);
- virtual HRESULT STDMETHODCALLTYPE Invoke(
- DISPID dispIdMember,
- REFIID riid,
- LCID lcid,
- WORD wFlags,
- DISPPARAMS *pDispParams,
- VARIANT *pVarResult,
- EXCEPINFO *pExcepInfo,
- UINT *puArgErr);
- private:
- CAxWindow _winContainer;
- IWebBrowser2 *_webBrowser2;
- IConnectionPoint *_connectionPoint;
- IDispatch *_htmlDoc;
- bool _goBackEnabled;
- bool _goForwardEnabled;
- DWORD _cookie;
- ULONG _reference;
- std::function<bool (const std::string &)> _shouldStartLoading;
- std::function<void (const std::string &)> _didFinishLoading;
- std::function<void (const std::string &)> _didFailLoading;
- std::function<void (const std::string &)> _onJsCallback;
- std::string _htmlWillLoad;
- static bool s_isInitialized;
- static CComModule s_module;
- static void lazyInit();
- void _loadHTMLString(const std::string &html) const;
- void loadUrl(BSTR url) const;
- void loadUrl(const std::wstring &url) const;
- };
- namespace cocos2d {
- namespace experimental {
- namespace ui {
- WebViewImpl::WebViewImpl(WebView *webView) : _createSucceeded(false), _systemWebControl(nullptr), _webView(webView)
- {
- _systemWebControl = new Win32WebControl();
- if (_systemWebControl == nullptr)
- {
- return;
- }
- _createSucceeded = _systemWebControl->createWebView(
- [this](const std::string &url)->bool {
- if (_webView->_onShouldStartLoading != nullptr)
- {
- return _webView->_onShouldStartLoading(_webView, url);
- }
- return true;
- },
- [this](const std::string &url) {
- if (_webView->_onDidFinishLoading != nullptr)
- {
- _webView->_onDidFinishLoading(_webView, url);
- }
- },
- [this](const std::string &url) {
- if (_webView->_onDidFailLoading != nullptr)
- {
- _webView->_onDidFailLoading(_webView, url);
- }
- },
- [this](const std::string &url) {
- if (_webView->_onJSCallback != nullptr)
- {
- _webView->_onJSCallback(_webView, url);
- }
- });
- }
- WebViewImpl::~WebViewImpl()
- {
- if (_systemWebControl != nullptr)
- {
- _systemWebControl->removeWebView();
- delete _systemWebControl;
- _systemWebControl = nullptr;
- }
- }
- void WebViewImpl::loadData(const Data &data, const std::string &MIMEType, const std::string &encoding, const std::string &baseURL)
- {
- if (_createSucceeded)
- {
- std::string dataString(reinterpret_cast<char *>(data.getBytes()), static_cast<unsigned int>(data.getSize()));
- _systemWebControl->loadData(dataString, MIMEType, encoding, baseURL);
- }
- }
- void WebViewImpl::loadHTMLString(const std::string &string, const std::string &baseURL)
- {
- if (_createSucceeded)
- {
- _systemWebControl->loadHTMLString(string, baseURL);
- }
- }
- void WebViewImpl::loadURL(const std::string &url)
- {
- if (_createSucceeded)
- {
- _systemWebControl->loadUrl(url);
- }
- }
-
- void WebViewImpl::loadURL(const std::string &url, bool cleanCachedData)
- {
- if (_createSucceeded)
- {
- _systemWebControl->loadUrl(url);
- }
- }
- void WebViewImpl::loadFile(const std::string &fileName)
- {
- if (_createSucceeded)
- {
- std::string fullPath = FileUtils::getInstance()->fullPathForFilename(fileName);
- _systemWebControl->loadFile(fullPath);
- }
- }
-
- void WebViewImpl::PostURL(const std::string &url, const std::string &data)
- {
- if (_createSucceeded)
- {
- _systemWebControl->postUrl(url, data, data.length());
- }
- }
- void WebViewImpl::stopLoading()
- {
- if (_createSucceeded)
- {
- _systemWebControl->stopLoading();
- }
- }
- void WebViewImpl::reload()
- {
- if (_createSucceeded)
- {
- _systemWebControl->reload();
- }
- }
- bool WebViewImpl::canGoBack()
- {
- if (_createSucceeded)
- {
- return _systemWebControl->canGoBack();
- }
- return false;
- }
- bool WebViewImpl::canGoForward()
- {
- if (_createSucceeded)
- {
- return _systemWebControl->canGoForward();
- }
- return false;
- }
- void WebViewImpl::goBack()
- {
- if (_createSucceeded)
- {
- _systemWebControl->goBack();
- }
- }
- void WebViewImpl::goForward()
- {
- if (_createSucceeded)
- {
- _systemWebControl->goForward();
- }
- }
- void WebViewImpl::setJavascriptInterfaceScheme(const std::string &scheme)
- {
- if (_createSucceeded)
- {
- _systemWebControl->setJavascriptInterfaceScheme(scheme);
- }
- }
- void WebViewImpl::evaluateJS(const std::string &js)
- {
- if (_createSucceeded)
- {
- _systemWebControl->evaluateJS(js);
- }
- }
- void WebViewImpl::setScalesPageToFit(const bool scalesPageToFit)
- {
- if (_createSucceeded)
- {
- _systemWebControl->setScalesPageToFit(scalesPageToFit);
- }
- }
- void WebViewImpl::draw(Renderer *renderer, Mat4 const &transform, uint32_t flags)
- {
- if (_createSucceeded && (flags & Node::FLAGS_TRANSFORM_DIRTY))
- {
- Director *directorInstance = cocos2d::Director::getInstance();
- GLView *glView = directorInstance->getOpenGLView();
- const Size &frameSize = glView->getFrameSize();
- const Size &winSize = directorInstance->getWinSize();
- Vec2 leftBottom = this->_webView->convertToWorldSpace(Point::ZERO);
- Vec2 rightTop = this->_webView->convertToWorldSpace(Point(_webView->getContentSize().width, _webView->getContentSize().height));
- float uiLeft = frameSize.width / 2 + (leftBottom.x - winSize.width / 2) * glView->getScaleX();
- float uiTop = frameSize.height / 2 - (rightTop.y - winSize.height / 2) * glView->getScaleY();
- _systemWebControl->setWebViewRect(uiLeft, uiTop,
- (rightTop.x - leftBottom.x) * glView->getScaleX(),
- (rightTop.y - leftBottom.y) * glView->getScaleY());
- }
- }
- void WebViewImpl::setVisible(bool visible)
- {
- if (_createSucceeded)
- {
- _systemWebControl->setWebViewVisible(visible);
- }
- }
-
- void WebViewImpl::setOpacityWebView(const float opacity){
- // empty function as this was mainly a fix for iOS
- };
-
-
- float WebViewImpl::getOpacityWebView()const{
- // empty function as this was mainly a fix for iOS
- return 1.0;
- };
-
- void WebViewImpl::setBackgroundTransparent(){
- // empty function as this was mainly a fix for iOS
- };
- void WebViewImpl::setBounces(bool bounces) {
- // empty function as this was mainly a fix for iOS
- }
- } // namespace ui
- } // namespace experimental
- } //namespace cocos2d
- //
- // Implement Win32WebControl;
- //
- bool Win32WebControl::s_isInitialized = false;
- CComModule Win32WebControl::s_module;
- void Win32WebControl::lazyInit()
- {
- HWND hwnd = cocos2d::Director::getInstance()->getOpenGLView()->getWin32Window();
- LONG style = GetWindowLong(hwnd, GWL_STYLE);
- SetWindowLong(hwnd, GWL_STYLE, style | WS_CLIPCHILDREN);
- HINSTANCE hInstance = GetModuleHandle(NULL);
- CoInitialize(NULL);
- s_module.Init(NULL, hInstance);
- AtlAxWinInit();
- }
- static HGLOBAL globalAllocWstringFromString(const std::string &str)
- {
- // int len = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);
- // if (len <= 0)
- // {
- // return NULL;
- // }
- // HGLOBAL wstr = GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT, (len + 1) * sizeof(WCHAR));
- // if (wstr == NULL)
- // {
- // return NULL;
- // }
- // MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, (WCHAR *)wstr, len);
- HGLOBAL wstr = GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT,str.length()+1);
- BYTE* pmem = (BYTE*)GlobalLock(wstr);
- memcpy(pmem, str.c_str(), str.length());
- GlobalUnlock(wstr);
- return wstr;
- }
- static std::string bstr2string(BSTR bstr)
- {
- wchar_t *str = OLE2W(bstr);
- std::string ret;
- int len = WideCharToMultiByte(CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL);
- if (len > 0)
- {
- HGLOBAL utf8Str = GlobalAlloc(GMEM_FIXED | GMEM_ZEROINIT, len + 1);
- if (utf8Str != NULL)
- {
- WideCharToMultiByte(CP_UTF8, 0, str, -1, (char *)utf8Str, len, NULL, NULL);
- ret.assign((char *)utf8Str);
- GlobalFree(utf8Str);
- }
- }
- return ret;
- }
- static bool isUrlJs(BSTR url)
- {
- return wcsncmp(OLE2W(url), L"javascript:", 11) == 0;
- }
- Win32WebControl::Win32WebControl()
- : _webBrowser2(NULL)
- , _connectionPoint(NULL)
- , _htmlDoc(NULL)
- , _goBackEnabled(true)
- , _goForwardEnabled(true)
- , _cookie(0)
- , _reference(0)
- , _shouldStartLoading(nullptr)
- , _didFinishLoading(nullptr)
- , _didFailLoading(nullptr)
- {
- if (!s_isInitialized)
- {
- lazyInit();
- }
- }
- bool Win32WebControl::createWebView(
- const std::function<bool (const std::string &)> &shouldStartLoading,
- const std::function<void (const std::string &)> &didFinishLoading,
- const std::function<void (const std::string &)> &didFailLoading,
- const std::function<void (const std::string &)> &onJsCallback)
- {
- bool ret = false;
- IConnectionPointContainer *container = NULL;
- do
- {
- HWND hwnd = cocos2d::Director::getInstance()->getOpenGLView()->getWin32Window();
- _winContainer.Create(hwnd, NULL, NULL, WS_CHILD | WS_VISIBLE);
- HRESULT hr;
- hr = _winContainer.CreateControl(L"shell.Explorer.2");
- CC_BREAK_IF(FAILED(hr));
- hr = _winContainer.QueryControl(__uuidof(IWebBrowser2), (void **)&_webBrowser2);
- CC_BREAK_IF(FAILED(hr) || _webBrowser2 == NULL);
- _webBrowser2->put_Silent(VARIANT_TRUE);
- VARIANT var;
- VariantInit(&var);
- var.vt = VT_BSTR;
- var.bstrVal = SysAllocString(L"about:blank");
- hr = _webBrowser2->Navigate2(&var, NULL, NULL, NULL, NULL);
- SysFreeString(var.bstrVal);
- VariantClear(&var);
- CC_BREAK_IF(FAILED(hr));
- hr = _webBrowser2->QueryInterface(IID_IConnectionPointContainer, (void **)&container);
- CC_BREAK_IF(FAILED(hr));
- hr = container->FindConnectionPoint(DIID_DWebBrowserEvents2, &_connectionPoint);
- CC_BREAK_IF(FAILED(hr));
- hr = _connectionPoint->Advise(this, &_cookie);
- CC_BREAK_IF(FAILED(hr));
- hr = _webBrowser2->get_Document(&_htmlDoc);
- CC_BREAK_IF(FAILED(hr));
- ret = true;
- } while (0);
- if (!ret)
- {
- removeWebView();
- }
- if (container != NULL)
- {
- container->Release();
- container = NULL;
- }
- _shouldStartLoading = shouldStartLoading;
- _didFinishLoading = didFinishLoading;
- _didFailLoading = didFailLoading;
- _onJsCallback = onJsCallback;
- return ret;
- }
- void Win32WebControl::removeWebView()
- {
- if (_connectionPoint != NULL)
- {
- _connectionPoint->Unadvise(_cookie);
- _connectionPoint->Release();
- _connectionPoint = NULL;
- }
- if (_htmlDoc != NULL)
- {
- _htmlDoc->Release();
- _htmlDoc = NULL;
- }
- if (_webBrowser2 != NULL)
- {
- _webBrowser2->Release();
- _webBrowser2 = NULL;
- }
- _winContainer.DestroyWindow();
- }
- void Win32WebControl::setWebViewRect(const int left, const int top, const int width, const int height)
- {
- _winContainer.MoveWindow(left, top, width, height);
- }
- void Win32WebControl::setJavascriptInterfaceScheme(const std::string &scheme) const
- {
- }
- void Win32WebControl::loadData(const std::string &data, const std::string &MIMEType, const std::string &encoding, const std::string &baseURL) const
- {
- }
- void Win32WebControl::loadHTMLString(const std::string &html, const std::string &baseURL)
- {
- //if (baseURL.empty())
- //{
- _loadHTMLString(html);
- //}
- //else
- //{
- // _htmlWillLoad = html;
- // loadUrl(baseURL);
- //}
- }
- void Win32WebControl::_loadHTMLString(const std::string &html) const
- {
- bool flag = false;
- HGLOBAL htmlText = globalAllocWstringFromString(html);
- if (htmlText != NULL)
- {
- IStream *stream = NULL;
- if (SUCCEEDED(CreateStreamOnHGlobal(htmlText, FALSE, &stream)))
- {
- IPersistStreamInit *persistStreamInit = NULL;
- if (SUCCEEDED(_htmlDoc->QueryInterface(IID_IPersistStreamInit, (void **)&persistStreamInit)))
- {
- if (SUCCEEDED(persistStreamInit->InitNew()) && SUCCEEDED(persistStreamInit->Load(stream)))
- {
- flag = true;
- }
- persistStreamInit->Release();
- }
- stream->Release();
- }
- GlobalFree(htmlText);
- }
- if (flag)
- {
- if (_didFinishLoading != nullptr)
- {
- std::string str("data:text/html,");
- str.append(html);
- _didFinishLoading(str);
- }
- }
- else
- {
- if (_didFailLoading != nullptr)
- {
- std::string str("data:text/html,");
- str.append(html);
- _didFailLoading(str);
- }
- }
- }
- void Win32WebControl::loadUrl(BSTR url) const
- {
- VARIANT var;
- VariantInit(&var);
- var.vt = VT_BSTR;
- var.bstrVal = url;
- _webBrowser2->Navigate2(&var, NULL, NULL, NULL, NULL);
- VariantClear(&var);
- }
- void Win32WebControl::loadUrl(const std::wstring &url) const
- {
- BSTR bstr = SysAllocString(url.c_str());
- loadUrl(bstr);
- SysFreeString(bstr);
- }
- void Win32WebControl::loadUrl(const std::string &url) const
- {
- // HGLOBAL unicodeStr = globalAllocWstringFromString(url);
- // if (unicodeStr != NULL)
- // {
- // loadUrl(std::wstring((WCHAR *)unicodeStr));
- // GlobalFree(unicodeStr);
- // }
- _bstr_t bstr_t(url.c_str());
- BSTR bstr = bstr_t.GetBSTR();
- loadUrl(bstr);
- }
- void Win32WebControl::loadFile(const std::string &filePath) const
- {
- // HGLOBAL unicodeStr = globalAllocWstringFromString(filePath);
- // if (unicodeStr != NULL)
- // {
- // loadUrl(std::wstring((WCHAR *)unicodeStr));
- // GlobalFree(unicodeStr);
- // }
- _bstr_t bstr_t(filePath.c_str());
- BSTR bstr = bstr_t.GetBSTR();
- loadUrl(bstr);
- }
- void Win32WebControl::postUrl(const std::string &url, const std::string& data, uint32_t len) const
- {
- _bstr_t bstr_t(url.c_str()), bstr_data(data.c_str());
- BSTR bstr = bstr_t.GetBSTR();
- VARIANT var;
- VARIANT vFlags;
- VARIANT vPostData;
- VARIANT vHeaders;
- VARIANT vNull;
- VariantInit(&var);
- VariantInit(&vFlags);
- VariantInit(&vPostData);
- VariantInit(&vHeaders);
- VariantInit(&vNull);
- var.vt = VT_BSTR;
- var.bstrVal = bstr;
- //vHeaders.vt = VT_BSTR;
- //vHeaders.bstrVal = SysAllocString(L"Content-Type: application/x-www-form-urlencoded\r\n");
- //vFlags.vt = VT_I4;
- //vFlags.lVal = navNoReadFromCache | navNoWriteToCache;
- vPostData.vt = VT_BSTR;
- vPostData.bstrVal = bstr_data.GetBSTR();
- /*SAFEARRAYBOUND bound;
- bound.cElements = len;
- bound.lLbound = 0;
- SAFEARRAY FAR* sfPost = SafeArrayCreate(VT_UI1, 1, &bound);
- const char* pChar = bstr_data;
- for (long lIndex = 0; lIndex < (signed)bound.cElements; lIndex++) {
- SafeArrayPutElement(sfPost, &lIndex, (void*)((pChar++)));
- }
- vPostData.vt = VT_ARRAY | VT_UI1;
- vPostData.parray = sfPost;*/
- _webBrowser2->Navigate2(&var, &vFlags, &vNull, &vPostData, &vHeaders);
- VariantClear(&var);
- }
- void Win32WebControl::stopLoading() const
- {
- _webBrowser2->Stop();
- }
- void Win32WebControl::reload() const
- {
- _webBrowser2->Refresh();
- }
- bool Win32WebControl::canGoBack() const
- {
- return _goBackEnabled;
- }
- bool Win32WebControl::canGoForward() const
- {
- return _goForwardEnabled;
- }
- void Win32WebControl::goBack() const
- {
- _webBrowser2->GoBack();
- }
- void Win32WebControl::goForward() const
- {
- _webBrowser2->GoForward();
- }
- void Win32WebControl::evaluateJS(const std::string &js) const
- {
- std::string url("javascript:");
- url.append(js);
- loadUrl(url);
- }
- void Win32WebControl::setScalesPageToFit(const bool scalesPageToFit) const
- {
- }
- void Win32WebControl::setWebViewVisible(const bool visible) const
- {
- if (visible)
- {
- ::ShowWindow(_winContainer.m_hWnd, SW_SHOW);
- //_winContainer.ShowWindow(SW_SHOW);
- }
- else
- {
- ::ShowWindow(_winContainer.m_hWnd, SW_HIDE);
- //_winContainer.ShowWindow(SW_HIDE);
- }
-
- //_webBrowser2->put_Visible(visible ? VARIANT_TRUE : VARIANT_FALSE);
- }
- // Implement IUnknown
- HRESULT STDMETHODCALLTYPE Win32WebControl::QueryInterface(REFIID riid, void **ppvObject)
- {
- if (IsBadWritePtr(ppvObject, sizeof(void *)))
- {
- return E_POINTER;
- }
- *ppvObject = NULL;
- if (IsEqualIID(riid, IID_IUnknown) || IsEqualIID(riid, IID_IDispatch) || IsEqualIID(riid, DIID_DWebBrowserEvents2))
- {
- AddRef();
- *ppvObject = this;
- return S_OK;
- }
- return E_NOINTERFACE;
- }
- ULONG STDMETHODCALLTYPE Win32WebControl::AddRef(void)
- {
- InterlockedIncrement(&_reference);
- return _reference;
- }
- ULONG STDMETHODCALLTYPE Win32WebControl::Release(void)
- {
- CCASSERT(_reference > 0, "reference count should greater than 0");
- InterlockedDecrement(&_reference);
- // do not delete this if _reference == 0, otherwise, it will crash when call removeWebView
- return _reference;
- }
- // Implement IDispatch
- HRESULT STDMETHODCALLTYPE Win32WebControl::GetTypeInfoCount(UINT *pctinfo)
- {
- *pctinfo = 0;
- return S_OK;
- }
- HRESULT STDMETHODCALLTYPE Win32WebControl::GetTypeInfo(UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
- {
- UNREFERENCED_PARAMETER(iTInfo);
- UNREFERENCED_PARAMETER(lcid);
- *ppTInfo = NULL;
- return E_NOTIMPL;
- }
- HRESULT STDMETHODCALLTYPE Win32WebControl::GetIDsOfNames(
- REFIID riid,
- LPOLESTR *rgszNames,
- UINT cNames,
- LCID lcid,
- DISPID *rgDispId)
- {
- UNREFERENCED_PARAMETER(riid);
- UNREFERENCED_PARAMETER(rgszNames);
- UNREFERENCED_PARAMETER(cNames);
- UNREFERENCED_PARAMETER(lcid);
- UNREFERENCED_PARAMETER(rgDispId);
- return E_NOTIMPL;
- }
- HRESULT STDMETHODCALLTYPE Win32WebControl::Invoke(
- DISPID dispIdMember,
- REFIID riid,
- LCID lcid,
- WORD wFlags,
- DISPPARAMS *pDispParams,
- VARIANT *pVarResult,
- EXCEPINFO *pExcepInfo,
- UINT *puArgErr)
- {
- if (!IsEqualIID(riid, IID_NULL)) // riid should always be IID_NULL
- {
- return DISP_E_UNKNOWNINTERFACE;
- }
- switch (dispIdMember)
- {
- case DISPID_COMMANDSTATECHANGE:
- if (pDispParams != NULL && pDispParams->cArgs == 2)
- {
- const VARIANTARG *rgvarg = pDispParams->rgvarg;
- if (rgvarg[0].vt == VT_BOOL && rgvarg[1].vt == VT_I4)
- {
- switch (rgvarg[1].intVal)
- {
- case CSC_NAVIGATEFORWARD:
- _goForwardEnabled = (rgvarg[0].boolVal != VARIANT_FALSE); // VARIANT_TRUE is -1
- return S_OK;
- case CSC_NAVIGATEBACK:
- _goBackEnabled = (rgvarg[0].boolVal != VARIANT_FALSE); // VARIANT_TRUE is -1
- return S_OK;
- default:
- break;
- }
- }
- }
- break;
- case DISPID_BEFORENAVIGATE2:
- if (pDispParams != NULL && pDispParams->cArgs == 7)
- {
- VARIANTARG *rgvarg = pDispParams->rgvarg;
- if (rgvarg[6].vt == VT_DISPATCH && rgvarg[6].pdispVal == _webBrowser2)
- {
- if (rgvarg[0].vt == (VT_BYREF | VT_BOOL) && rgvarg[5].vt == (VT_BYREF | VT_VARIANT))
- {
- VARIANT_BOOL *cancel = rgvarg[0].pboolVal;
- BSTR url = rgvarg[5].pvarVal->bstrVal;
- *cancel = VARIANT_FALSE;
- if (isUrlJs(url))
- {
- if (_onJsCallback != nullptr)
- {
- _onJsCallback(bstr2string(url));
- }
- }
- else
- {
- if (_shouldStartLoading != nullptr)
- {
- *cancel = _shouldStartLoading(bstr2string(url)) ? VARIANT_FALSE : VARIANT_TRUE; // VARIANT_TRUE is -1
- }
- }
- return S_OK;
- }
- }
- }
- break;
- case DISPID_DOCUMENTCOMPLETE:
- if (pDispParams != NULL && pDispParams->cArgs == 2)
- {
- const VARIANTARG *rgvarg = pDispParams->rgvarg;
- if (rgvarg[1].vt == VT_DISPATCH && rgvarg[1].pdispVal == _webBrowser2 && rgvarg[0].vt == (VT_BYREF | VT_VARIANT))
- {
- READYSTATE state;
- if (SUCCEEDED(_webBrowser2->get_ReadyState(&state)) && state == READYSTATE_COMPLETE)
- {
- BSTR url = rgvarg[0].pvarVal->bstrVal;
- if (_didFinishLoading != nullptr && !isUrlJs(url)) // ignore js
- {
- _didFinishLoading(bstr2string(url));
- }
- if (!_htmlWillLoad.empty())
- {
- _loadHTMLString(_htmlWillLoad);
- _htmlWillLoad.clear();
- }
- return S_OK;
- }
- }
- }
- break;
- case DISPID_NAVIGATECOMPLETE2:
- if (pDispParams != NULL && pDispParams->cArgs == 2)
- {
- const VARIANTARG *rgvarg = pDispParams->rgvarg;
- if (rgvarg[1].vt == VT_DISPATCH && rgvarg[1].pdispVal == _webBrowser2)
- {
- if (rgvarg[0].vt == (VT_BYREF | VT_VARIANT))
- {
- BSTR url = rgvarg[0].pvarVal->bstrVal;
- return S_OK;
- }
- }
- }
- break;
- case DISPID_NAVIGATEERROR:
- if (pDispParams != NULL && pDispParams->cArgs == 5)
- {
- const VARIANTARG *rgvarg = pDispParams->rgvarg;
- if (rgvarg[4].vt == VT_DISPATCH && rgvarg[4].pdispVal == _webBrowser2)
- {
- if (rgvarg[3].vt == (VT_BYREF | VT_VARIANT) && rgvarg[1].vt == (VT_BYREF | VT_VARIANT) && rgvarg[0].vt == (VT_BYREF | VT_BOOL))
- {
- VARIANT_BOOL *cancel = rgvarg[0].pboolVal;
- HRESULT codes = rgvarg[1].pvarVal->lVal;
- BSTR url = rgvarg[3].pvarVal->bstrVal;
- if (_didFailLoading != nullptr && !isUrlJs(url)) // ignore js
- {
- _didFailLoading(bstr2string(url));
- }
- *cancel = VARIANT_FALSE;
- return S_OK;
- }
- }
- }
- break;
- case DISPID_PROGRESSCHANGE:
- if (pDispParams != NULL && pDispParams->cArgs == 2)
- {
- const VARIANTARG *rgvarg = pDispParams->rgvarg;
- if (rgvarg[0].vt == VT_I4 && rgvarg[1].vt == VT_I4)
- {
- LONG maxProgress = rgvarg[0].lVal;
- LONG curProgress = rgvarg[1].lVal;
- return S_OK;
- }
- }
- break;
- case DISPID_NEWWINDOW2:
- if (pDispParams != NULL && pDispParams->cArgs == 2)
- {
- const VARIANTARG *rgvarg = pDispParams->rgvarg;
- if (rgvarg[0].vt == (VT_BYREF | VT_BOOL) && rgvarg[1].vt == (VT_BYREF | VT_DISPATCH))
- {
- VARIANT_BOOL *cancel = rgvarg[0].pboolVal;
- IDispatch **dis = rgvarg[1].ppdispVal;
- *dis = NULL;
- *cancel = VARIANT_TRUE; // forbit to create new window
- return S_OK;
- }
- }
- break;
- case DISPID_NEWWINDOW3:
- if (pDispParams != NULL && pDispParams->cArgs == 5)
- {
- const VARIANTARG *rgvarg = pDispParams->rgvarg;
- if (rgvarg[0].vt == VT_BSTR && rgvarg[1].vt == VT_BSTR && rgvarg[2].vt == VT_I4
- && rgvarg[3].vt == (VT_BYREF | VT_BOOL) && rgvarg[4].vt == (VT_BYREF | VT_DISPATCH))
- {
- BSTR url = rgvarg[0].bstrVal;
- BSTR urlContext = rgvarg[1].bstrVal;
- LONG flags = rgvarg[2].lVal;
- VARIANT_BOOL *cancel = rgvarg[3].pboolVal;
- IDispatch **dis = rgvarg[4].ppdispVal;
- *dis = NULL;
- *cancel = VARIANT_TRUE; // forbit to create new window
- loadUrl(url);
- return S_OK;
- }
- }
- break;
- default:
- break;
- }
- return E_NOTIMPL;
- }
- #endif // CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
|