I embedded three CEF3s(branch 1916) into my application for displaying web pages.
I use a C++ class to create/destroy a set of browsers. When closing browsers, some Usage dialogs will be popupped out and then disappear soon. Dialog's content is "-wWIDTH\n-hHEIGHT\n-p \"Relative path or absolute path which contains folder[package] or file of [package.zip]\"\n\nWIDTH and HEIGHT MUST be more than zero!".
Could anyone tell me how to avoid displaying/popupping those dialogs?
Sorry for my poor English and Thanks for your help!
By the way, I also try CEF branch 1750, 2171, and 2271. However, situation still exists.
My Environment:
OS: Win7 SP1 X64
Compiler: MS Visual Studio 2012
CEF3: Branch 1916 Win32 binary downloaded from [url]cefbuilds.com[/url]
Source code:
JAppHtmApp.h
- Code: Select all
#pragma once
#if defined(EN_HTM_FEATURE) && EN_HTM_FEATURE
#include "JAppObject.h"
#include "include/cef_app.h"
#include <TlHelp32.h>
#ifdef _DEBUG
#include <assert.h>
#endif
#pragma comment(lib, "libCef.lib")
#pragma comment(lib, "libCef_dll_wrapper.lib")
NS_JApp_BEGIN
#ifndef JApp_ASSERT
#ifdef _DEBUG
#define JApp_ASSERT assert
#else
#define JApp_ASSERT
#endif
#endif
#define BLANK_WEB "about:blank"
// CefApp does all of the work, but it is an abstract base class that needs reference counting implemented;
// thus, we create a dummy class that inherits off of CefApp but does nothing
class JAppHtmApp : public CefApp
{
public:
JAppHtmApp(){ ; }
virtual ~JAppHtmApp(){ ; }
static JAppHtmApp *getInstance()
{
CefRefPtr<JAppHtmApp> ptApp = _getInstance();
return ptApp.get();
}
bool init(CefRefPtr<JAppHtmApp> ptApp)
{
//CefRefPtr<JAppHtmApp> ptApp = _getInstance();
HINSTANCE hInst = GetModuleHandle(NULL);
CefMainArgs main_args(hInst);
int exitCode = CefExecuteProcess(main_args, ptApp.get(), NULL);
if ( exitCode >= 0 )
{
return false;
}
CefSettings webSets;
webSets.no_sandbox = true;
webSets.single_process = false;
webSets.multi_threaded_message_loop = true;
if ( !CefInitialize(main_args, webSets, ptApp.get(), NULL) )
{
return false;
}
return true;
}
// KillCEFProcess
// Desc. : Kill processes created by CEF. All sub processes of current
// process will be killed.
// Param : None
// Return: None
// Note : It'a a workaround to avoid CEF process show dialog when
// main program is closing.
static void KillCEFProcess(void)
{
DWORD dwPrtProcId = GetCurrentProcessId();
HANDLE hProc = NULL;
DWORD dwExitCode = 0;
BOOL bFound = FALSE, bRtn = FALSE;
HANDLE hndl = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
DWORD dwsma = GetLastError();
// search process name in process list
PROCESSENTRY32 procEntry={0};
procEntry.dwSize = sizeof(PROCESSENTRY32);
Process32First(hndl,&procEntry);
do
{
if ( dwPrtProcId == procEntry.th32ParentProcessID )
{
hProc = OpenProcess(PROCESS_ALL_AJBaseESS, 0, procEntry.th32ProcessID);
GetExitCodeProcess(hProc, &dwExitCode);
if ( hProc )
{
bRtn = TerminateProcess(hProc, dwExitCode);
}
}
} while ( Process32Next(hndl, &procEntry) );
}
protected:
static CefRefPtr<JAppHtmApp> _getInstance()
{
static CefRefPtr<JAppHtmApp> ptApp;
if ( ptApp.get() == NULL )
ptApp = new JAppHtmApp;
return ptApp;
}
private:
IMPLEMENT_REFCOUNTING (JAppHtmApp);
};
NS_JApp_END
#endif // #if defined(EN_HTM_FEATURE) && EN_HTM_FEATURE
JAppHtmHandler.h
- Code: Select all
#pragma once
#if defined(EN_HTM_FEATURE) && EN_HTM_FEATURE
#include <include/cef_client.h>
#include "util.h"
NS_JApp_BEGIN
class JAppHtmBrowser;
class JAppHtmHandler : public CefClient,
public CefContextMenuHandler,
public CefDisplayHandler,
public CefDownloadHandler,
public CefDragHandler,
public CefGeolocationHandler,
public CefKeyboardHandler,
public CefLifeSpanHandler,
public CefLoadHandler,
public CefRequestHandler
{
public:
JAppHtmHandler();
virtual ~JAppHtmHandler();
CefRefPtr<CefBrowser> GetBrowser();
void CloseAllBrowsers(bool force_close);
void SetBrowserPtr(JAppHtmBrowser* pBrowser){ m_pBrowser = pBrowser; }
#pragma region CefClient
// since we are letting the base implementations handle all of the heavy lifting,
// these functions just return the this pointer
virtual CefRefPtr<CefContextMenuHandler> GetContextMenuHandler () OVERRIDE;
virtual CefRefPtr<CefDisplayHandler> GetDisplayHandler () OVERRIDE;
virtual CefRefPtr<CefDownloadHandler> GetDownloadHandler () OVERRIDE;
virtual CefRefPtr<CefDragHandler> GetDragHandler () OVERRIDE;
virtual CefRefPtr<CefGeolocationHandler> GetGeolocationHandler () OVERRIDE;
virtual CefRefPtr<CefKeyboardHandler> GetKeyboardHandler () OVERRIDE;
virtual CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler () OVERRIDE;
virtual CefRefPtr<CefLoadHandler> GetLoadHandler () OVERRIDE;
virtual CefRefPtr<CefRequestHandler> GetRequestHandler () OVERRIDE;
#pragma endregion // CefClient
#pragma region CefDownloadHandler
// this function is virtual and must be implemented; we do nothing in it, so downloading files won't work as the callback function isn't invoked
virtual void OnBeforeDownload (CefRefPtr<CefBrowser> browser, CefRefPtr<CefDownloadItem> download_item, const CefString& suggested_name, CefRefPtr<CefBeforeDownloadCallback> callback);
#pragma endregion // CefDownloadHandler
#pragma region CefLifeSpanHandler
// cache a reference to the browser
virtual void OnAfterCreated (CefRefPtr<CefBrowser> browser) OVERRIDE;
// release the browser reference
virtual void OnBeforeClose (CefRefPtr<CefBrowser> browser) OVERRIDE;
virtual bool DoClose(CefRefPtr<CefBrowser> browser) OVERRIDE;
virtual bool OnBeforePopup(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, const CefString& target_url, const CefString& target_frame_name, const CefPopupFeatures& popupFeatures, CefWindowInfo& windowInfo, CefRefPtr<CefClient>& client, CefBrowserSettings& settings, bool* no_javascript_aJBaseess) OVERRIDE;
#pragma endregion // CefLifeSpanHandler
#pragma region CefContextMenuHandler
// CefContextMenuHandler methods
// Do something before displaying right-click menu
virtual void OnBeforeContextMenu(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefContextMenuParams> params, CefRefPtr<CefMenuModel> model) OVERRIDE;
virtual void OnLoadError(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, ErrorCode errorCode, const CefString& errorText, const CefString& failedUrl) OVERRIDE;
#pragma endregion // CefContextMenuHandler
#pragma region CefLoadHandler
virtual void OnLoadingStateChange(CefRefPtr<CefBrowser> browser, bool isLoading, bool canGoBack, bool canGoForward) OVERRIDE;
#pragma endregion // CefLoadHandler
protected:
// the browser reference
CefRefPtr<CefBrowser> browser;
int m_s32BrowserId;
JAppHtmBrowser* m_pBrowser;
// Include the default reference counting implementation.
IMPLEMENT_REFCOUNTING (JAppHtmHandler);
// Include the default locking implementation.
};
NS_JApp_END
#endif // #if defined(EN_HTM_FEATURE) && EN_HTM_FEATURE
JAppHtmHandler.cpp
- Code: Select all
#include "JAppHtmHandler.h"
#include "JAppHtmBrowser.h"
#include "include/cef_runnable.h"
#include <string>
#include "JAppLog.h"
using namespace std;
NS_JApp_BEGIN
JApp_CREATE_LOG_FUNC(L, "JAppHtmHandler", JAppLOG_LEVEL_ALL);
JAppHtmHandler::JAppHtmHandler ():
m_s32BrowserId(0), m_pBrowser(NULL)
{
DLOG(L, "JAppHtmHandler(0x%X) Constructor", this);
}
JAppHtmHandler::~JAppHtmHandler ()
{
DLOG(L, "JAppHtmHandler(0x%X) Destructor", this);
}
CefRefPtr<CefBrowser> JAppHtmHandler::GetBrowser ()
{
return browser;
}
CefRefPtr<CefContextMenuHandler> JAppHtmHandler::GetContextMenuHandler ()
{
return this;
}
CefRefPtr<CefDisplayHandler> JAppHtmHandler::GetDisplayHandler ()
{
return this;
}
CefRefPtr<CefDownloadHandler> JAppHtmHandler::GetDownloadHandler ()
{
return this;
}
CefRefPtr<CefDragHandler> JAppHtmHandler::GetDragHandler ()
{
return this;
}
CefRefPtr<CefGeolocationHandler> JAppHtmHandler::GetGeolocationHandler ()
{
return this;
}
CefRefPtr<CefKeyboardHandler> JAppHtmHandler::GetKeyboardHandler ()
{
return this;
}
CefRefPtr<CefLifeSpanHandler> JAppHtmHandler::GetLifeSpanHandler ()
{
return this;
}
CefRefPtr<CefLoadHandler> JAppHtmHandler::GetLoadHandler ()
{
return this;
}
CefRefPtr<CefRequestHandler> JAppHtmHandler::GetRequestHandler ()
{
return this;
}
void JAppHtmHandler::OnBeforeDownload (CefRefPtr<CefBrowser> browser,
CefRefPtr<CefDownloadItem> download_item,
const CefString& suggested_name,
CefRefPtr<CefBeforeDownloadCallback> callback)
{
UNREFERENCED_PARAMETER (browser);
UNREFERENCED_PARAMETER (download_item);
callback->Continue (suggested_name, true);
}
void JAppHtmHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser)
{
REQUIRE_UI_THREAD();
this->browser = browser;
m_s32BrowserId = browser->GetIdentifier();
CefLifeSpanHandler::OnAfterCreated (browser);
DLOG(L, "OnAfterCreated of browser(%d, 0x%X, 0x%X) is called",
browser->GetIdentifier(), browser, browser->GetHost()->GetWindowHandle());
}
void JAppHtmHandler::OnBeforeClose(CefRefPtr<CefBrowser> browser)
{
REQUIRE_UI_THREAD();
DLOG(L, "OnBeforeClose of Browser(%d, 0x%X, 0x%X) is called",
browser->GetIdentifier(), browser, browser->GetHost()->GetWindowHandle());
browser = NULL;
CefLifeSpanHandler::OnBeforeClose (browser);
}
void JAppHtmHandler::CloseAllBrowsers(bool force_close)
{
if ( !CefCurrentlyOn(TID_UI) )
{
// Execute on the UI thread.
CefPostTask(TID_UI,
NewCefRunnableMethod(this, &JAppHtmHandler::CloseAllBrowsers,
force_close));
return;
}
if ( browser.get() )
{
DLOG(L, "CloseAllBrowsers is called, Browser(%d, 0x%X, 0x%X)",
browser->GetIdentifier(), browser, browser->GetHost()->GetWindowHandle());
// Request that the main browser close.
browser->GetHost()->CloseBrowser(force_close);
}
FLOG_L(L);
}
bool JAppHtmHandler::DoClose(CefRefPtr<CefBrowser> browser)
{
REQUIRE_UI_THREAD();
// Closing the main window requires special handling. See the DoClose()
// documentation in the CEF header for a detailed description of this
// process.
if ( m_s32BrowserId == browser->GetIdentifier() )
{
// Set a flag to indicate that the window close should be allowed.
//m_bIsClosing = true;
HWND hnd = GetParent(browser->GetHost()->GetWindowHandle());
//SetParent(hnd, NULL);
//SetWindowLong(hnd, GWL_USERDATA, 0L);
//DestroyWindow(hnd);
DLOG(L, "DoClose is called, Brower(%d, 0x%X, 0x%X)",
browser->GetIdentifier(), browser, browser->GetHost()->GetWindowHandle());
}
// Allow the close. For windowed browsers this will result in the OS close
// event being sent.
return false;
}
bool JAppHtmHandler::OnBeforePopup(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, const CefString& target_url, const CefString& target_frame_name, const CefPopupFeatures& popupFeatures, CefWindowInfo& windowInfo, CefRefPtr<CefClient>& client, CefBrowserSettings& settings, bool* no_javascript_aJBaseess)
{
// Allow or block popup windows, customize popup window creation...
return true; // true means no popup window is allowed
}
void JAppHtmHandler::OnBeforeContextMenu(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefContextMenuParams> params,
CefRefPtr<CefMenuModel> model)
{
model->Clear(); // Disable Right click
}
void JAppHtmHandler::OnLoadingStateChange(CefRefPtr<CefBrowser> browser,
bool isLoading, bool canGoBack,
bool canGoForward)
{
string strURL = browser->GetMainFrame()->GetURL();
if ( isLoading )
{
DLOG(L, "browser(%d, 0x%X, 0x%X) is loading URL: %s",
browser->GetIdentifier(), browser, browser->GetHost()->GetWindowHandle(), strURL.c_str());
}
else
{
DLOG(L, "Browser(%d, 0x%X, 0x%X): Loading is completed. URL is %s",
browser->GetIdentifier(), browser, browser->GetHost()->GetWindowHandle(), strURL.c_str());
if ( m_s32BrowserId == browser->GetIdentifier() )
{
if ( m_pBrowser ) m_pBrowser->cbBrowserLoaded(strURL);
}
}
}
void JAppHtmHandler::OnLoadError(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
ErrorCode errorCode,
const CefString& errorText,
const CefString& failedUrl)
{
string strURL = failedUrl;
DLOG(L, "Browser(0x%X, 0x%X): Load Error, Error is %d, URL is %s",
browser, browser->GetHost()->GetWindowHandle(),
errorCode, strURL.c_str());
}
NS_JApp_END
#endif // #if defined(EN_HTM_FEATURE) && EN_HTM_FEATURE
JAppHtmBrowser.h
- Code: Select all
#pragma once
#if defined(EN_HTM_FEATURE) && EN_HTM_FEATURE
#include "JAppHtmApp.h"
#include "JAppHtmHandler.h"
#include <string>
NS_JApp_BEGIN
#define HTM_SET_URL (WM_USER + 0x50)
using namespace std;
class JAppHtmSprite;
class JAppHtmBrowser
{
// Member functions
public:
JAppHtmBrowser(void* pSprite);
virtual ~JAppHtmBrowser();
void cbBrowserLoaded(string strCurrURL = "");
bool Create(HWND hParent, RECT &rt, string strUrl = "");
bool SetUrl(string strUrl);
void CloseAll();
HWND getHwnd() { return m_hWnd; }
string getURL();
void setVisible(bool var)
{
if ( NULL == m_hWnd ) return;
ShowWindow(m_hWnd, var ? SW_SHOW : SW_HIDE);
}
void setPosition(int x, int y) {
SetWindowPos(m_hWnd, HWND_TOPMOST, x, y , 0, 0, SWP_NOSIZE | SWP_NOZORDER);
}
LRESULT SendMsg(unsigned int msg, WPARAM wParam, LPARAM lParam)
{ return ( NULL == m_hWnd ) ? 0 : SendMessage(m_hWnd, msg, wParam, lParam); }
protected:
LRESULT _WndProc(HWND hWnd, UINT msg, WPARAM paramW, LPARAM paramL);
private:
JAppHtmBrowser(){};
public:
static LRESULT CALLBACK _bWndProc(HWND hWnd, UINT msg, WPARAM paramW, LPARAM paramL);
// Member data
public:
protected:
string m_strLastUrl;
private:
CefRefPtr<JAppHtmHandler> m_Client;
JAppHtmSprite* m_ptSprite;
HWND m_hWnd;
bool m_bReady;
bool m_bNext;
};
NS_JApp_END
#endif // #if defined(EN_HTM_FEATURE) && EN_HTM_FEATURE
JAppHtmBrowser.cpp
- Code: Select all
#if defined(EN_HTM_FEATURE) && EN_HTM_FEATURE
#include "JAppHtmSpriteX86.h" // It must be front of JAppHtmBrowser.h, or it will get lots of errors.
#include "JAppHtmBrowser.h"
#include "JAppLog.h"
NS_JApp_BEGIN
JApp_CREATE_LOG_FUNC(L, "JAppHtmBrowser", JAppLOG_LEVEL_ALL);
#define BROWSER_WINDOW_CLASS TEXT("BrowserWindowClass")
class JAppWaitBaseSprite;
JAppHtmBrowser::JAppHtmBrowser(void* pSprite):
m_hWnd(NULL), m_strLastUrl(""), m_bReady(false), m_bNext(false)
{
DLOG(L, "JAppHtmBrowser[0x%X]::JAppHtmBrowser() Constructor 0x%X", this, pSprite);
m_ptSprite = (JAppHtmSprite*)pSprite;
}
JAppHtmBrowser::~JAppHtmBrowser()
{
DLOG(L, ">>>Entering : JAppHtmBrowser[0x%X]::~JAppHtmBrowser() Destructor", this);
m_Client = NULL;
DLOG(L, "<<<Leaving : JAppHtmBrowser[0x%X]::~JAppHtmBrowser() Destructor", this);
}
LRESULT CALLBACK JAppHtmBrowser::_bWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if ( WM_CREATE == msg ) // Save class pointer if it's provided
{
CREATESTRUCT* pcsCreate = (CREATESTRUCT*)lParam;
SetWindowLong(hWnd, GWL_USERDATA, (int)pcsCreate->lpCreateParams);
}
JAppHtmBrowser *pWin = (JAppHtmBrowser*)GetWindowLong(hWnd, GWL_USERDATA);
return pWin ? pWin->_WndProc(hWnd, msg, wParam, lParam)
: DefWindowProc(hWnd, msg, wParam, lParam);
}
LRESULT JAppHtmBrowser::_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
LRESULT result = 0;
switch ( msg )
{
case WM_CREATE:
{
if ( NULL == m_Client )
m_Client = new JAppHtmHandler();
m_Client->SetBrowserPtr(this);
RECT rt = {0};
GetClientRect(hWnd, &rt);
CefWindowInfo info;
info.SetAsChild(hWnd, rt);
CefBrowserSettings bwSets;
CefString cstrUrl = BLANK_WEB;
if ( m_strLastUrl.length() ) cstrUrl = m_strLastUrl;
CefBrowserHost::CreateBrowser(info, m_Client.get(),
CefString(BLANK_WEB),
bwSets, NULL);
break;
} // End of WM_CREATE case
case HTM_SET_URL:
{
const char* newUrl = (const char*)wParam;
JApp_ASSERT(NULL != newUrl);
if ( NULL != newUrl )
{
this->SetUrl(string(newUrl));
}
break;
} // End of HTM_SET_URL case
case WM_SIZE:
{
if ( SIZE_MINIMIZED != wParam
&& NULL != m_Client.get()
&& NULL != m_Client->GetBrowser() )
{
CefWindowHandle hwnd = m_Client->GetBrowser()->GetHost()->GetWindowHandle();
if ( hwnd )
{
RECT rt = {0};
GetClientRect(hWnd, &rt);
HDWP hdwp = BeginDeferWindowPos (1);
hdwp = DeferWindowPos (hdwp, hwnd, NULL,
rt.left, rt.top, rt.right - rt.left, rt.bottom - rt.top,
SWP_NOZORDER);
EndDeferWindowPos (hdwp);
}
}
break;
} // End of WM_SIZE case
case WM_ERASEBKGND:
{
if ( (m_Client.get())
&& (m_Client->GetBrowser()) )
{
CefWindowHandle hwnd = m_Client->GetBrowser()->GetHost()->GetWindowHandle();
// from the cefclient example, don't erase the background
// if the browser window has been loaded to avoid flashing
result = hwnd ? 1 : DefWindowProc(hWnd, msg, wParam, lParam);
}
break;
} // End of WM_ERASEBKGND case
case WM_ENTERMENULOOP:
{
if ( 0 == wParam ){ CefSetOSModalLoop(true); }
result = DefWindowProc(hWnd, msg, wParam, lParam);
break;
} // End of WM_ENTERMENULOOP case
case WM_EXITMENULOOP:
{
if ( 0 != wParam ){ CefSetOSModalLoop(false); }
result = DefWindowProc(hWnd, msg, wParam, lParam);
break;
} // End of WM_EXITMENULOOP case
case WM_DESTROY:
DLOG(L, "Browser(0x%X, 0x%X)'s WM_DESTROY is called!", this, m_hWnd);
break;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
return result;
}
void JAppHtmBrowser::CloseAll()
{
if ( NULL == m_Client.get() ) return;
// Use Destroy parent window instead of using Clent/Handler's closing.
// It seems a solution to decrease the opportunity CEF popup out dialog while closing.
//m_Client->CloseAllBrowsers(true);
if ( m_hWnd ) DestroyWindow(m_hWnd);
DLOG(L, "Browser(0x%X, 0x%X)'s CloseAll() is called!", this, m_hWnd);
}
bool JAppHtmBrowser::SetUrl(string strUrl)
{
if ( m_bReady )
{
do
{
CefRefPtr<CefBrowser> pBrowser = NULL;
CefRefPtr<CefFrame> pFrame = NULL;
if ( NULL == m_hWnd ) break;
if ( NULL == m_Client.get() ) break;
if ( NULL == (pBrowser = m_Client->GetBrowser()) ) break;
if ( NULL == (pFrame = pBrowser->GetMainFrame()) ) break;
if ( 0 == strUrl.length() ) strUrl = BLANK_WEB;
pFrame->LoadURL(CefString(strUrl));
return true;
} while ( 0 );
}
else
{
m_strLastUrl = strUrl;
return true;
}
return false;
}
string JAppHtmBrowser::getURL()
{
string strRtn = m_strLastUrl;
if ( m_bReady )
{
do
{
CefRefPtr<CefBrowser> pBrowser = NULL;
CefRefPtr<CefFrame> pFrame = NULL;
if ( NULL == m_hWnd ) break;
if ( NULL == m_Client.get() ) break;
if ( NULL == (pBrowser = m_Client->GetBrowser()) ) break;
if ( NULL == (pFrame = pBrowser->GetMainFrame()) ) break;
strRtn = pFrame->GetURL();
} while ( 0 );
}
else
{
strRtn = m_strLastUrl;
}
return strRtn;
}
bool JAppHtmBrowser::Create(HWND hParent, RECT &rt, string strUrl)
{
JApp_ASSERT(NULL != hParent);
if ( NULL == hParent ) return false;
HINSTANCE hInst = GetModuleHandle(NULL);
WNDCLASSEX wcex = {0};
wcex.cbSize = sizeof (wcex);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = JAppHtmBrowser::_bWndProc;
wcex.hInstance = hInst;
wcex.hCursor = NULL;
wcex.hbrBackground = WHITE_BRUSH;
wcex.lpszClassName = BROWSER_WINDOW_CLASS;
RegisterClassEx (&wcex);
m_strLastUrl = strUrl;
m_hWnd = CreateWindow(BROWSER_WINDOW_CLASS, BROWSER_WINDOW_CLASS,
WS_CHILD | WS_CLIPCHILDREN,
rt.left, rt.top, rt.right - rt.left, rt.bottom - rt.top,
hParent, NULL, hInst, this);
if ( NULL == m_hWnd ) return false;
DLOG(L, "Browser parent/Host window is 0x%X", m_hWnd);
ShowWindow(m_hWnd, SW_HIDE);
UpdateWindow(m_hWnd);
return true;
}
//////////////////////////////////////////////////////////////////////////
// cbBrowserLoaded
// Desc. : Callback function for Browser to send URL loading completed
// information
// Param : strCurrURL - current loaded URL
// Return: None
// Note : None
//////////////////////////////////////////////////////////////////////////
void JAppHtmBrowser::cbBrowserLoaded(string strCurrURL)
{
if ( !m_bReady )
{
m_bReady = true;
if ( 0 != m_strLastUrl.length()
&& 0 != _strcmpi(m_strLastUrl.c_str(), BLANK_WEB) )
{
SetUrl(m_strLastUrl);
}
}
else
{
m_strLastUrl = strCurrURL;
if ( m_bNext && NULL != m_ptSprite )
{
((JAppHtmSprite*)m_ptSprite)->ResponseSceneTmpDuration();
}
m_bNext = true;
}
}
NS_JApp_END
#endif // #if defined(EN_HTM_FEATURE) && EN_HTM_FEATURE
JAppHtmSprite.h
- Code: Select all
#pragma once
#if defined(EN_HTM_FEATURE) && EN_HTM_FEATURE
#include "JAppWaitBaseSprite.h"
#include "JAppHtmBrowser.h"
USING_NS_JBase;
NS_JApp_BEGIN
class JAppHtmSprite: public JAppWaitBaseSprite
{
public:
virtual ~JAppHtmSprite();
static JAppHtmSprite* create(const JBaseLayer* pPrt, const char* szUrl, const JBaseSize &dimensions, void* pScnPtr, wstring &wstrMdaId, wstring &wstrHtmId, string &wstrHtmSrc);
void draw(void){ JBaseSprite::draw(); }
void setUrl(const char *newString);
void setPosition(const JBasePoint& pos);
void setContentSize(const JBaseSize & size);
void setVisible(bool var);
void play(void);
void stop(void);
virtual void ForceClose(void);
virtual HWND getHwnd();
private:
bool init(int w, int h, void* pScnPtr, string strUrl = "");
JAppHtmSprite();
int m_s32ReqW;
int m_s32ReqH;
JAppUserScene* m_pUsrScene;
JAppHtmBrowser *m_pBrowser;
wstring m_wstrMdaId, m_wstrHtmId;
string m_strHtmSrc;
string m_strURL;
bool m_bReg2Parent;
bool m_bStopped;
};
NS_JApp_END
#endif // #if defined(EN_HTM_FEATURE) && EN_HTM_FEATURE
JAppHtmSprite.cpp
- Code: Select all
#if defined(EN_HTM_FEATURE) && EN_HTM_FEATURE
#include "JAppHtmSprite.h"
#include <exception>
#include "JAppLog.h"
#include "JAppUtils.h"
#include "JAppScene.h"
#include "JBaseEGLView.h"
#include "JAppBaseLayer.h"
#include "JAppTaskController.h"
using namespace std;
NS_JApp_BEGIN
JApp_CREATE_LOG_FUNC(TAG, "JAppHtmSprite", JAppLOG_LEVEL_ALL);
JAppHtmSprite::JAppHtmSprite():
m_s32ReqW(0), m_s32ReqH(0), m_pBrowser(NULL), m_strURL(""),
m_pUsrScene(NULL), m_wstrMdaId(L""), m_wstrHtmId(L""), m_strHtmSrc(""),
m_bReg2Parent(false), m_bStopped(false)
{
DLOG(TAG, "JAppHtmSprite() construct, this: 0x%X", this);
}
JAppHtmSprite::~JAppHtmSprite()
{
DLOG(TAG, ">>>Entering: JAppHtmSprite[0x%X]::~JAppHtmSprite() Destructor, JAppBrowser: 0x%X", this, this->m_pBrowser);
stop();
// Here, I pass HtmBrowser's pointer to another thread for delay
// killing, and then set it to NULL
if ( NULL != m_pBrowser )
{
JAppMessage msg = JAppMessage(JAppDelTask::MSG_DEL_HTM_SPRITE, (JAppParam)m_pBrowser);
JAppUtils::postMessageToUI(msg);
}
m_pBrowser = NULL;
DLOG(TAG, "<<<Leaving: JAppHtmSprite[0x%X]::~JAppHtmSprite() Destructor", this);
}
JAppHtmSprite* JAppHtmSprite::create(const JBaseLayer* pPrt, const char* szUrl,
const JBaseSize &dimensions, void* pScnPtr,
wstring &wstrMdaId, wstring &wstrHtmId, string &strHtmSrc)
{
JAppHtmSprite *pSprite = new JAppHtmSprite();
if ( NULL == pSprite ) return NULL;
if ( !pSprite->init((int)dimensions.width, (int)dimensions.height, pScnPtr, string(szUrl)) )
{
delete pSprite;
return NULL;
}
pSprite->m_wstrMdaId = wstrMdaId;
pSprite->m_wstrHtmId = wstrHtmId;
pSprite->m_strHtmSrc = strHtmSrc;
pSprite->autorelease();
return pSprite;
}
bool JAppHtmSprite::init(int w, int h, void* pScnPtr, string strUrl)
{
if ( 0 == w || 0 == h )
{
JBaseSize size = JBase::getWinSize();
m_s32ReqW = (unsigned int)size.width;
m_s32ReqH = (unsigned int)size.height;
}
else
{
m_s32ReqW = w;
m_s32ReqH = h;
}
m_pUsrScene = (JAppUserScene*)pScnPtr;
JBaseSprite::init();
HWND hPrt = JBase::getHWnd();
RECT rtArea = {0, 0, w, h};
if ( 0 == strUrl.length() ) strUrl = BLANK_WEB;
m_strURL = strUrl;
m_pBrowser = new JAppHtmBrowser(this);
m_pBrowser->Create(hPrt, rtArea, strUrl.c_str());
return true;
}
void JAppHtmSprite::setUrl(const char *newUrl)
{
if ( NULL == newUrl ) return;
if ( NULL != m_pBrowser ) m_pBrowser->SendMsg(HTM_SET_URL, (WPARAM)newUrl, 0);
}
void JAppHtmSprite::setPosition(const JBasePoint& pos)
{
JBaseSprite::setPosition(pos);
JBasePoint wdsPos = getParent()->getPosition(pos);
JBaseSize winSiz = JBase::getWinSize();
JBasePoint winPos(wdsPos.x - pos.x,
winSiz.height - wdsPos.y - pos.y);
if ( NULL != m_pBrowser )
m_pBrowser->setPosition((int)winPos.x, (int)winPos.y);
}
void JAppHtmSprite::setContentSize(const JBaseSize & size)
{
JBaseNode::setContentSize(size);
if ( NULL != m_pBrowser )
SetWindowPos(m_pBrowser->getHwnd(), NULL, 0, 0, (int)size.width, (int)size.height, SWP_NOMOVE | SWP_NOZORDER);
}
void JAppHtmSprite::setVisible(bool var)
{
JBaseNode::setVisible(var);
if ( NULL != m_pBrowser )
m_pBrowser->setVisible(var);
}
void JAppHtmSprite::play()
{
if ( NULL != m_pBrowser )
{
DLOG(TAG, "Play Html");
if ( !m_bReg2Parent )
{
m_bReg2Parent = true;
m_pUsrScene->mu32SpriteCnt++;
}
}
}
void JAppHtmSprite::stop()
{
if ( NULL != m_pBrowser )
{
if ( !m_bStopped )
{
DLOG(TAG, "Stop Html");
this->setVisible(false);
if ( m_bReg2Parent )
{
m_bReg2Parent = false;
m_pUsrScene->maintainSpritePtr(this);
m_pUsrScene->mu32SpriteCnt--;
}
m_pBrowser->setVisible(false);
m_pBrowser->CloseAll();
m_bStopped = true;
}
}
}
void JAppHtmSprite::ForceClose(void)
{
stop();
}
//////////////////////////////////////////////////////////////////////////
// getHwnd
// Desc. : Return window handle that sprite use
// Param : None
// Return: Window handle
// Note : None
//////////////////////////////////////////////////////////////////////////
HWND JAppHtmSprite::getHwnd()
{
if ( NULL == m_pBrowser ) return NULL;
return m_pBrowser->getHwnd();
}
NS_JApp_END
#endif // #if defined(EN_HTM_FEATURE) && EN_HTM_FEATURE