#ifndef BROWSER_H #define BROWSER_H #include #include #include class WebEventHandler; class BrowserObserver; /** * Internal browser widget */ class CWebBrowser { public: CWebBrowser() : _DoNotSaveUrl(false), _ForceHidden(false), _IsActive(true), _IWeb(NULL), _WidgetWnd(NULL), _ParentWnd(NULL), _HDLL(NULL), _ConnectionPoint(NULL), _Cookie(0), _WebEventHandler(NULL), _X(0), _Y(0), _W(512), _H(384), _Initialized(false) {} ~CWebBrowser() { release(); } /// Initialize bool init(HWND hParentWnd, DWORD windowStyle); /// Is the object initialized? bool initialized() const { return _Initialized; } /// De-initialize void release(); /// Put this in the default case of the WndProc of your parent window void processWindowsMessage(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, void* userParam = NULL); /// Open web page bool navigate(char *url); /// Open last page visited if it existed bool navigateBack(); // Write some code in the page //void setHTMLContent(const char *html, bool append=true); bool getHtmlContent(std::string * html); /// Toggle visibility void setVisible(bool visible); /// Get visibility status bool isVisible() const; /// If true, setVisible(bool) will not work, use it after setVisible void setVisibilityForced(bool visible) { _ForceHidden = visible; } /// Move/resize void setPosAndSize(int x, int y, int w, int h); // Set on top of other windows (if initialized) //bool setOnTop(); /// get cookies for currently domain with syntax : "name1=value1; name2=value2" void grabAllCookies(); /// get specific value cookie for currently domain std::string getCookieValue(const std::string& name); bool getAllCookies(std::string * cookies); /// Attach a new observer void attachObserver(BrowserObserver* obs); /// Remove an observer void detachObserver(BrowserObserver* obs); /// return if URL is equal to _LastUrlVisited bool checkLastURLVisited(char * url); /// return if we have lauch this browser for the first time bool isFirstLoading() const; /// return if the browser can receive Event bool isActive() const { return _IsActive; } void setActive(bool active) { _IsActive = active; } std::string GetUrl (); /// erase the last url visited void clearHistory(); /// get memory usage #ifndef USER_MODE unsigned int getMemoryUsage() const; #endif bool hasFocus() const; /// callback when the windows event "WM_DESTROY" is called void killed(); HWND GetOwnWindow() const; HWND GetActiveXWindow() const; HWND GetIEWindow() const; IWebBrowser2 *GetIWeb() const; protected: /// Notify his observers void notifyObserver(UINT message, WPARAM wParam, void* userParam); private: IWebBrowser2 *_IWeb; // MS Web Browser interface HWND _WidgetWnd; // Container window HWND _ParentWnd; // Parent window HINSTANCE _HDLL; // ATL.DLL IConnectionPoint *_ConnectionPoint; // event handler connection point DWORD _Cookie; // cookie for connection point WebEventHandler *_WebEventHandler; int _X, _Y, _W, _H; bool _Initialized; bool _IsActive; bool _ForceHidden; bool _DoNotSaveUrl; std::string _LastUrlVisited; std::string _CurrentUrl; typedef std::map CookiesList; CookiesList _CookiesList; typedef std::list > ObserverList; // ObserverList _ObserverList; }; #include // Adapted from http://www.cppfrance.com/codes/NAVIGATEUR-WEB-API_29247.aspx #define BEFORENAVIGATE2 WM_USER #define DOWNLOADBEGIN WM_USER+1 #define DOWNLOADCOMPLETE WM_USER+2 #define NAVIGATECOMPLETE2 WM_USER+3 #define DOCUMENTCOMPLETE WM_USER+4 #define COMMANDSTATECHANGE WM_USER+5 #define NEWWINDOW3 WM_USER+6 #define DOCUMENTERROR WM_USER+7 /* * */ class WebEventHandler : public IDispatch { private: long _Ref; BSTR _URL; public: struct NewWindowParam { BSTR URL; VARIANT_BOOL * CancelFlagPtr; } ; struct DocumentError { BSTR URL; int StatusCode; } ; HWND _HWnd; CWebBrowser *_Browser; WebEventHandler(HWND hWnd, CWebBrowser *browser) : _HWnd(hWnd), _Browser(browser), _URL(NULL) {} ~WebEventHandler() { //SysFreeString(_URL); } void GetUrl (std::string *url); STDMETHODIMP QueryInterface(REFIID iid, void ** ppvObject) { if (iid==IID_IUnknown || iid==IID_IDispatch || iid==DIID_DWebBrowserEvents2) { *ppvObject=this; AddRef(); return S_OK; } else return E_NOINTERFACE; } ULONG STDMETHODCALLTYPE AddRef() { return InterlockedIncrement(&_Ref); } ULONG STDMETHODCALLTYPE Release() { int tmp = InterlockedDecrement(&_Ref); if (tmp==0) delete this; return tmp; } HRESULT STDMETHODCALLTYPE GetTypeInfoCount(unsigned int FAR* pctinfo) { return E_NOTIMPL; } HRESULT STDMETHODCALLTYPE GetTypeInfo(unsigned int iTInfo, LCID lcid, ITypeInfo FAR* FAR* ppTInfo) { return E_NOTIMPL; } HRESULT STDMETHODCALLTYPE GetIDsOfNames(REFIID riid, OLECHAR FAR* FAR* rgszNames, unsigned int cNames, LCID lcid, DISPID FAR* rgDispId) { return E_NOTIMPL; } HRESULT STDMETHODCALLTYPE Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS FAR* pDispParams, VARIANT FAR* parResult, EXCEPINFO FAR* pExcepInfo, unsigned int FAR* puArgErr); }; class BrowserObserver { public: BrowserObserver () {} virtual void updateEvent(CWebBrowser* webbrowser, UINT message, WPARAM wParam, void* userParam) = 0; private: }; #endif // BROWSER_H