1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/webview_ie.cpp
3 // Purpose: wxMSW wxWebViewIE class implementation for web view component
4 // Author: Marianne Gagnon
6 // Copyright: (c) 2010 Marianne Gagnon, Steven Lamerton
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
13 #if defined(__BORLANDC__)
17 #include "wx/msw/webview_ie.h"
26 #include "wx/msw/registry.h"
27 #include "wx/msw/missing.h"
28 #include "wx/filesys.h"
29 #include "wx/tokenzr.h"
31 //We link to urlmon as it is required for CoInternetGetSession
32 #pragma comment(lib, "urlmon")
34 //Taken from wx/filesys.cpp
35 static wxString
EscapeFileNameCharsInURL(const char *in
)
39 for ( const unsigned char *p
= (const unsigned char*)in
; *p
; ++p
)
41 const unsigned char c
= *p
;
43 if ( c
== '/' || c
== '-' || c
== '.' || c
== '_' || c
== '~' ||
44 (c
>= '0' && c
<= '9') ||
45 (c
>= 'a' && c
<= 'z') ||
46 (c
>= 'A' && c
<= 'Z') )
52 s
<< wxString::Format("%%%02x", c
);
59 wxWebFileProtocolHandler::wxWebFileProtocolHandler()
62 m_fileSystem
= new wxFileSystem();
65 wxFSFile
* wxWebFileProtocolHandler::GetFile(const wxString
&uri
)
67 size_t pos
= uri
.find('?');
68 //There is no query string so we can load the file directly
69 if(pos
== wxString::npos
)
71 size_t doubleslash
= uri
.find("//");
72 //The path is incorrectly formed without // after the first protocol
73 if(doubleslash
== wxString::npos
)
76 wxString fspath
= "file:" +
77 EscapeFileNameCharsInURL(uri
.substr(doubleslash
+ 2));
78 return m_fileSystem
->OpenFile(fspath
);
80 //Otherwise we have a query string of some kind that we need to extract
82 //First we extract the query string, this should have two parameters,
83 //protocol=type and path=path
84 wxString query
= uri
.substr(pos
+ 1), protocol
, path
;
85 //We also trim the query off the end as we handle it alone
86 wxString lefturi
= uri
.substr(0, pos
);
87 wxStringTokenizer
tokenizer(query
, ";");
88 while(tokenizer
.HasMoreTokens() && (protocol
== "" || path
== ""))
90 wxString token
= tokenizer
.GetNextToken();
91 if(token
.substr(0, 9) == "protocol=")
93 protocol
= token
.substr(9);
95 else if(token
.substr(0, 5) == "path=")
97 path
= token
.substr(5);
100 if(protocol
== "" || path
== "")
103 //We now have the path and the protocol and so can format a correct uri
104 //to pass to wxFileSystem to get a wxFSFile
105 size_t doubleslash
= uri
.find("//");
106 //The path is incorrectly formed without // after the first protocol
107 if(doubleslash
== wxString::npos
)
110 wxString fspath
= "file:" +
111 EscapeFileNameCharsInURL(lefturi
.substr(doubleslash
+ 2))
112 + "#" + protocol
+":" + path
;
113 return m_fileSystem
->OpenFile(fspath
);
117 wxString
wxWebFileProtocolHandler::CombineURIs(const wxString
&baseuri
,
118 const wxString
&newuri
)
120 //Still need to be implemented correctly
124 BEGIN_EVENT_TABLE(wxWebViewIE
, wxControl
)
125 EVT_ACTIVEX(wxID_ANY
, wxWebViewIE::onActiveXEvent
)
126 EVT_ERASE_BACKGROUND(wxWebViewIE::onEraseBg
)
129 bool wxWebViewIE::Create(wxWindow
* parent
,
135 const wxString
& name
)
137 if (!wxControl::Create(parent
, id
, pos
, size
, style
,
138 wxDefaultValidator
, name
))
145 m_historyLoadingFromList
= false;
146 m_historyEnabled
= true;
147 m_historyPosition
= -1;
148 m_zoomType
= wxWEB_VIEW_ZOOM_TYPE_TEXT
;
150 if (::CoCreateInstance(CLSID_WebBrowser
, NULL
,
151 CLSCTX_INPROC_SERVER
, // CLSCTX_INPROC,
152 IID_IWebBrowser2
, (void**)&m_webBrowser
) != 0)
154 wxLogError("Failed to initialize IE, CoCreateInstance returned an error");
158 m_ie
.SetDispatchPtr(m_webBrowser
); // wxAutomationObject will release itself
160 m_webBrowser
->put_RegisterAsBrowser(VARIANT_TRUE
);
161 m_webBrowser
->put_RegisterAsDropTarget(VARIANT_TRUE
);
163 //For testing purposes
164 RegisterProtocol(new wxWebFileProtocolHandler());
166 m_container
= new wxActiveXContainer(this, IID_IWebBrowser2
, m_webBrowser
);
168 SetBackgroundStyle(wxBG_STYLE_PAINT
);
169 SetDoubleBuffered(true);
175 void wxWebViewIE::LoadUrl(const wxString
& url
)
177 m_ie
.CallMethod("Navigate", (BSTR
) url
.wc_str(), NULL
, NULL
, NULL
, NULL
);
180 void wxWebViewIE::SetPage(const wxString
& html
, const wxString
& baseUrl
)
182 BSTR bstr
= SysAllocString(html
.wc_str());
184 // Creates a new one-dimensional array
185 SAFEARRAY
*psaStrings
= SafeArrayCreateVector(VT_VARIANT
, 0, 1);
186 if (psaStrings
!= NULL
)
190 HRESULT hr
= SafeArrayAccessData(psaStrings
, (LPVOID
*)¶m
);
192 param
->bstrVal
= bstr
;
193 hr
= SafeArrayUnaccessData(psaStrings
);
195 IHTMLDocument2
* document
= GetDocument();
196 document
->write(psaStrings
);
199 // SafeArrayDestroy calls SysFreeString for each BSTR
200 SafeArrayDestroy(psaStrings
);
202 //We send the events when we are done to mimic webkit
204 wxWebNavigationEvent
event(wxEVT_COMMAND_WEB_VIEW_NAVIGATED
,
205 GetId(), baseUrl
, "", false);
206 event
.SetEventObject(this);
207 HandleWindowEvent(event
);
209 //Document complete event
210 event
.SetEventType(wxEVT_COMMAND_WEB_VIEW_LOADED
);
211 event
.SetEventObject(this);
212 HandleWindowEvent(event
);
216 wxLogError("wxWebViewIE::SetPage() : psaStrings is NULL");
221 wxString
wxWebViewIE::GetPageSource()
223 IHTMLDocument2
* document
= GetDocument();
224 IHTMLElement
*bodyTag
= NULL
;
225 IHTMLElement
*htmlTag
= NULL
;
227 HRESULT hr
= document
->get_body(&bodyTag
);
230 hr
= bodyTag
->get_parentElement(&htmlTag
);
234 htmlTag
->get_outerHTML(&bstr
);
235 source
= wxString(bstr
);
245 wxWebViewZoom
wxWebViewIE::GetZoom()
247 if(m_zoomType
== wxWEB_VIEW_ZOOM_TYPE_LAYOUT
)
248 return GetIEOpticalZoom();
249 else if(m_zoomType
== wxWEB_VIEW_ZOOM_TYPE_TEXT
)
250 return GetIETextZoom();
254 //Dummy return to stop compiler warnings
255 return wxWEB_VIEW_ZOOM_MEDIUM
;
259 void wxWebViewIE::SetZoom(wxWebViewZoom zoom
)
261 if(m_zoomType
== wxWEB_VIEW_ZOOM_TYPE_LAYOUT
)
262 SetIEOpticalZoom(zoom
);
263 else if(m_zoomType
== wxWEB_VIEW_ZOOM_TYPE_TEXT
)
269 void wxWebViewIE::SetIETextZoom(wxWebViewZoom level
)
271 //We do not use OLECMDID_OPTICAL_GETZOOMRANGE as the docs say the range
272 //is 0 to 4 so the check is unnecessary, these match exactly with the
275 VariantInit (&zoomVariant
);
276 V_VT(&zoomVariant
) = VT_I4
;
277 V_I4(&zoomVariant
) = level
;
279 HRESULT result
= m_webBrowser
->ExecWB(OLECMDID_ZOOM
,
280 OLECMDEXECOPT_DONTPROMPTUSER
,
282 wxASSERT(result
== S_OK
);
285 wxWebViewZoom
wxWebViewIE::GetIETextZoom()
288 VariantInit (&zoomVariant
);
289 V_VT(&zoomVariant
) = VT_I4
;
291 HRESULT result
= m_webBrowser
->ExecWB(OLECMDID_ZOOM
,
292 OLECMDEXECOPT_DONTPROMPTUSER
,
294 wxASSERT(result
== S_OK
);
296 //We can safely cast here as we know that the range matches our enum
297 return static_cast<wxWebViewZoom
>(V_I4(&zoomVariant
));
300 void wxWebViewIE::SetIEOpticalZoom(wxWebViewZoom level
)
302 //We do not use OLECMDID_OPTICAL_GETZOOMRANGE as the docs say the range
303 //is 10 to 1000 so the check is unnecessary
305 VariantInit (&zoomVariant
);
306 V_VT(&zoomVariant
) = VT_I4
;
308 //We make a somewhat arbitray map here, taken from values used by webkit
311 case wxWEB_VIEW_ZOOM_TINY
:
312 V_I4(&zoomVariant
) = 60;
314 case wxWEB_VIEW_ZOOM_SMALL
:
315 V_I4(&zoomVariant
) = 80;
317 case wxWEB_VIEW_ZOOM_MEDIUM
:
318 V_I4(&zoomVariant
) = 100;
320 case wxWEB_VIEW_ZOOM_LARGE
:
321 V_I4(&zoomVariant
) = 130;
323 case wxWEB_VIEW_ZOOM_LARGEST
:
324 V_I4(&zoomVariant
) = 160;
330 HRESULT result
= m_webBrowser
->ExecWB((OLECMDID
)OLECMDID_OPTICAL_ZOOM
,
331 OLECMDEXECOPT_DODEFAULT
,
334 wxASSERT(result
== S_OK
);
337 wxWebViewZoom
wxWebViewIE::GetIEOpticalZoom()
340 VariantInit (&zoomVariant
);
341 V_VT(&zoomVariant
) = VT_I4
;
343 HRESULT result
= m_webBrowser
->ExecWB((OLECMDID
)OLECMDID_OPTICAL_ZOOM
,
344 OLECMDEXECOPT_DODEFAULT
, NULL
,
346 wxASSERT(result
== S_OK
);
348 const int zoom
= V_I4(&zoomVariant
);
350 //We make a somewhat arbitray map here, taken from values used by webkit
353 return wxWEB_VIEW_ZOOM_TINY
;
355 else if (zoom
> 65 && zoom
<= 90)
357 return wxWEB_VIEW_ZOOM_SMALL
;
359 else if (zoom
> 90 && zoom
<= 115)
361 return wxWEB_VIEW_ZOOM_MEDIUM
;
363 else if (zoom
> 115 && zoom
<= 145)
365 return wxWEB_VIEW_ZOOM_LARGE
;
367 else /*if (zoom > 145) */ //Using else removes a compiler warning
369 return wxWEB_VIEW_ZOOM_LARGEST
;
373 void wxWebViewIE::SetZoomType(wxWebViewZoomType type
)
378 wxWebViewZoomType
wxWebViewIE::GetZoomType() const
383 bool wxWebViewIE::CanSetZoomType(wxWebViewZoomType type
) const
385 //IE 6 and below only support text zoom, so check the registry to see what
386 //version we actually have
387 wxRegKey
key(wxRegKey::HKLM
, "Software\\Microsoft\\Internet Explorer");
389 key
.QueryValue("Version", value
);
391 long version
= wxAtoi(value
.Left(1));
392 if(version
<= 6 && type
== wxWEB_VIEW_ZOOM_TYPE_LAYOUT
)
398 void wxWebViewIE::Print()
400 m_webBrowser
->ExecWB(OLECMDID_PRINTPREVIEW
,
401 OLECMDEXECOPT_DODEFAULT
, NULL
, NULL
);
404 bool wxWebViewIE::CanGoBack()
407 return m_historyPosition
> 0;
412 bool wxWebViewIE::CanGoForward()
415 return m_historyPosition
!= static_cast<int>(m_historyList
.size()) - 1;
420 void wxWebViewIE::LoadHistoryItem(wxSharedPtr
<wxWebHistoryItem
> item
)
423 for(unsigned int i
= 0; i
< m_historyList
.size(); i
++)
425 //We compare the actual pointers to find the correct item
426 if(m_historyList
[i
].get() == item
.get())
429 wxASSERT_MSG(pos
!= static_cast<int>(m_historyList
.size()),
430 "invalid history item");
431 m_historyLoadingFromList
= true;
432 LoadUrl(item
->GetUrl());
433 m_historyPosition
= pos
;
436 wxVector
<wxSharedPtr
<wxWebHistoryItem
> > wxWebViewIE::GetBackwardHistory()
438 wxVector
<wxSharedPtr
<wxWebHistoryItem
> > backhist
;
439 //As we don't have std::copy or an iterator constructor in the wxwidgets
440 //native vector we construct it by hand
441 for(int i
= 0; i
< m_historyPosition
; i
++)
443 backhist
.push_back(m_historyList
[i
]);
448 wxVector
<wxSharedPtr
<wxWebHistoryItem
> > wxWebViewIE::GetForwardHistory()
450 wxVector
<wxSharedPtr
<wxWebHistoryItem
> > forwardhist
;
451 //As we don't have std::copy or an iterator constructor in the wxwidgets
452 //native vector we construct it by hand
453 for(int i
= m_historyPosition
+ 1; i
< static_cast<int>(m_historyList
.size()); i
++)
455 forwardhist
.push_back(m_historyList
[i
]);
460 void wxWebViewIE::GoBack()
462 LoadHistoryItem(m_historyList
[m_historyPosition
- 1]);
465 void wxWebViewIE::GoForward()
467 LoadHistoryItem(m_historyList
[m_historyPosition
+ 1]);
470 void wxWebViewIE::Stop()
472 m_ie
.CallMethod("Stop");
475 void wxWebViewIE::ClearHistory()
477 m_historyList
.clear();
478 m_historyPosition
= -1;
481 void wxWebViewIE::EnableHistory(bool enable
)
483 m_historyEnabled
= enable
;
484 m_historyList
.clear();
485 m_historyPosition
= -1;
488 void wxWebViewIE::Reload(wxWebViewReloadFlags flags
)
492 V_VT(&level
) = VT_I2
;
496 case wxWEB_VIEW_RELOAD_DEFAULT
:
497 V_I2(&level
) = REFRESH_NORMAL
;
499 case wxWEB_VIEW_RELOAD_NO_CACHE
:
500 V_I2(&level
) = REFRESH_COMPLETELY
;
503 wxFAIL_MSG("Unexpected reload type");
506 m_webBrowser
->Refresh2(&level
);
509 bool wxWebViewIE::IsOfflineMode()
511 wxVariant out
= m_ie
.GetProperty("Offline");
513 wxASSERT(out
.GetType() == "bool");
515 return out
.GetBool();
518 void wxWebViewIE::SetOfflineMode(bool offline
)
520 // FIXME: the wxWidgets docs do not really document what the return
521 // parameter of PutProperty is
522 const bool success
= m_ie
.PutProperty("Offline", (offline
?
528 bool wxWebViewIE::IsBusy()
530 if (m_isBusy
) return true;
532 wxVariant out
= m_ie
.GetProperty("Busy");
534 wxASSERT(out
.GetType() == "bool");
536 return out
.GetBool();
539 wxString
wxWebViewIE::GetCurrentURL()
541 wxVariant out
= m_ie
.GetProperty("LocationURL");
543 wxASSERT(out
.GetType() == "string");
544 return out
.GetString();
547 wxString
wxWebViewIE::GetCurrentTitle()
549 IHTMLDocument2
* document
= GetDocument();
552 document
->get_nameProp(&title
);
554 return wxString(title
);
557 bool wxWebViewIE::CanCut()
559 return CanExecCommand("Cut");
562 bool wxWebViewIE::CanCopy()
564 return CanExecCommand("Copy");
566 bool wxWebViewIE::CanPaste()
568 return CanExecCommand("Paste");
571 void wxWebViewIE::Cut()
576 void wxWebViewIE::Copy()
581 void wxWebViewIE::Paste()
583 ExecCommand("Paste");
586 bool wxWebViewIE::CanUndo()
588 return CanExecCommand("Undo");
590 bool wxWebViewIE::CanRedo()
592 return CanExecCommand("Redo");
595 void wxWebViewIE::Undo()
600 void wxWebViewIE::Redo()
605 void wxWebViewIE::SetEditable(bool enable
)
607 IHTMLDocument2
* document
= GetDocument();
609 document
->put_designMode(SysAllocString(L
"On"));
611 document
->put_designMode(SysAllocString(L
"Off"));
616 bool wxWebViewIE::IsEditable()
618 IHTMLDocument2
* document
= GetDocument();
620 document
->get_designMode(&mode
);
622 if(wxString(mode
) == "On")
628 void wxWebViewIE::SelectAll()
630 ExecCommand("SelectAll");
633 bool wxWebViewIE::HasSelection()
635 IHTMLDocument2
* document
= GetDocument();
636 IHTMLSelectionObject
* selection
;
638 HRESULT hr
= document
->get_selection(&selection
);
642 selection
->get_type(&type
);
643 sel
= wxString(type
);
644 selection
->Release();
647 return sel
!= "None";
650 void wxWebViewIE::DeleteSelection()
652 ExecCommand("Delete");
655 wxString
wxWebViewIE::GetSelectedText()
657 IHTMLDocument2
* document
= GetDocument();
658 IHTMLSelectionObject
* selection
;
660 HRESULT hr
= document
->get_selection(&selection
);
664 hr
= selection
->createRange(&disrange
);
667 IHTMLTxtRange
* range
;
668 hr
= disrange
->QueryInterface(IID_IHTMLTxtRange
, (void**)&range
);
672 range
->get_text(&text
);
673 selected
= wxString(text
);
678 selection
->Release();
684 wxString
wxWebViewIE::GetSelectedSource()
686 IHTMLDocument2
* document
= GetDocument();
687 IHTMLSelectionObject
* selection
;
689 HRESULT hr
= document
->get_selection(&selection
);
693 hr
= selection
->createRange(&disrange
);
696 IHTMLTxtRange
* range
;
697 hr
= disrange
->QueryInterface(IID_IHTMLTxtRange
, (void**)&range
);
701 range
->get_htmlText(&text
);
702 selected
= wxString(text
);
707 selection
->Release();
713 void wxWebViewIE::ClearSelection()
715 IHTMLDocument2
* document
= GetDocument();
716 IHTMLSelectionObject
* selection
;
718 HRESULT hr
= document
->get_selection(&selection
);
722 selection
->Release();
727 wxString
wxWebViewIE::GetPageText()
729 IHTMLDocument2
* document
= GetDocument();
732 HRESULT hr
= document
->get_body(&body
);
736 body
->get_innerText(&out
);
737 text
= wxString(out
);
744 void wxWebViewIE::RunScript(const wxString
& javascript
)
746 IHTMLDocument2
* document
= GetDocument();
747 IHTMLWindow2
* window
;
748 wxString language
= "javascript";
749 HRESULT hr
= document
->get_parentWindow(&window
);
754 V_VT(&level
) = VT_EMPTY
;
755 window
->execScript(SysAllocString(javascript
), SysAllocString(language
), &level
);
760 void wxWebViewIE::RegisterProtocol(wxWebProtocolHandler
* handler
)
762 ClassFactory
* cf
= new ClassFactory(handler
);
763 IInternetSession
* session
;
764 if(FAILED(CoInternetGetSession(0, &session
, 0)))
766 wxFAIL_MSG("Could not retrive internet session");
769 HRESULT hr
= session
->RegisterNameSpace(cf
, CLSID_FileProtocol
, handler
->GetProtocol(), 0, NULL
, 0);
772 wxFAIL_MSG("Could not register protocol");
776 bool wxWebViewIE::CanExecCommand(wxString command
)
778 IHTMLDocument2
* document
= GetDocument();
779 VARIANT_BOOL enabled
;
781 document
->queryCommandEnabled(SysAllocString(command
.wc_str()), &enabled
);
784 return (enabled
== VARIANT_TRUE
);
787 void wxWebViewIE::ExecCommand(wxString command
)
789 IHTMLDocument2
* document
= GetDocument();
790 document
->execCommand(SysAllocString(command
.wc_str()), VARIANT_FALSE
, VARIANT(), NULL
);
794 IHTMLDocument2
* wxWebViewIE::GetDocument()
796 wxVariant variant
= m_ie
.GetProperty("Document");
797 IHTMLDocument2
* document
= (IHTMLDocument2
*)variant
.GetVoidPtr();
804 void wxWebViewIE::onActiveXEvent(wxActiveXEvent
& evt
)
806 if (m_webBrowser
== NULL
) return;
808 switch (evt
.GetDispatchId())
810 case DISPID_BEFORENAVIGATE2
:
814 wxString url
= evt
[1].GetString();
815 wxString target
= evt
[3].GetString();
817 wxWebNavigationEvent
event(wxEVT_COMMAND_WEB_VIEW_NAVIGATING
,
818 GetId(), url
, target
, true);
819 event
.SetEventObject(this);
820 HandleWindowEvent(event
);
822 if (event
.IsVetoed())
824 wxActiveXEventNativeMSW
* nativeParams
=
825 evt
.GetNativeParameters();
826 *V_BOOLREF(&nativeParams
->pDispParams
->rgvarg
[0]) = VARIANT_TRUE
;
829 // at this point, either the navigation event has been cancelled
830 // and we're not busy, either it was accepted and IWebBrowser2's
831 // Busy property will be true; so we don't need our override
838 case DISPID_NAVIGATECOMPLETE2
:
840 wxString url
= evt
[1].GetString();
841 // TODO: set target parameter if possible
842 wxString target
= wxEmptyString
;
843 wxWebNavigationEvent
event(wxEVT_COMMAND_WEB_VIEW_NAVIGATED
,
844 GetId(), url
, target
, false);
845 event
.SetEventObject(this);
846 HandleWindowEvent(event
);
850 case DISPID_PROGRESSCHANGE
:
856 case DISPID_DOCUMENTCOMPLETE
:
858 //Only send a complete even if we are actually finished, this brings
859 //the event in to line with webkit
861 m_webBrowser
->get_ReadyState( &rs
);
862 if(rs
!= READYSTATE_COMPLETE
)
865 wxString url
= evt
[1].GetString();
867 //As we are complete we also add to the history list, but not if the
868 //page is not the main page, ie it is a subframe
869 if(m_historyEnabled
&& !m_historyLoadingFromList
&& url
== GetCurrentURL())
871 //If we are not at the end of the list, then erase everything
872 //between us and the end before adding the new page
873 if(m_historyPosition
!= static_cast<int>(m_historyList
.size()) - 1)
875 m_historyList
.erase(m_historyList
.begin() + m_historyPosition
+ 1,
876 m_historyList
.end());
878 wxSharedPtr
<wxWebHistoryItem
> item(new wxWebHistoryItem(url
, GetCurrentTitle()));
879 m_historyList
.push_back(item
);
882 //Reset as we are done now
883 m_historyLoadingFromList
= false;
884 // TODO: set target parameter if possible
885 wxString target
= wxEmptyString
;
886 wxWebNavigationEvent
event(wxEVT_COMMAND_WEB_VIEW_LOADED
, GetId(),
888 event
.SetEventObject(this);
889 HandleWindowEvent(event
);
893 case DISPID_STATUSTEXTCHANGE
:
898 case DISPID_TITLECHANGE
:
903 case DISPID_NAVIGATEERROR
:
905 wxWebNavigationError errorType
= wxWEB_NAV_ERR_OTHER
;
906 wxString errorCode
= "?";
907 switch (evt
[3].GetLong())
909 case INET_E_INVALID_URL
: // (0x800C0002L or -2146697214)
910 errorCode
= "INET_E_INVALID_URL";
911 errorType
= wxWEB_NAV_ERR_REQUEST
;
913 case INET_E_NO_SESSION
: // (0x800C0003L or -2146697213)
914 errorCode
= "INET_E_NO_SESSION";
915 errorType
= wxWEB_NAV_ERR_CONNECTION
;
917 case INET_E_CANNOT_CONNECT
: // (0x800C0004L or -2146697212)
918 errorCode
= "INET_E_CANNOT_CONNECT";
919 errorType
= wxWEB_NAV_ERR_CONNECTION
;
921 case INET_E_RESOURCE_NOT_FOUND
: // (0x800C0005L or -2146697211)
922 errorCode
= "INET_E_RESOURCE_NOT_FOUND";
923 errorType
= wxWEB_NAV_ERR_NOT_FOUND
;
925 case INET_E_OBJECT_NOT_FOUND
: // (0x800C0006L or -2146697210)
926 errorCode
= "INET_E_OBJECT_NOT_FOUND";
927 errorType
= wxWEB_NAV_ERR_NOT_FOUND
;
929 case INET_E_DATA_NOT_AVAILABLE
: // (0x800C0007L or -2146697209)
930 errorCode
= "INET_E_DATA_NOT_AVAILABLE";
931 errorType
= wxWEB_NAV_ERR_NOT_FOUND
;
933 case INET_E_DOWNLOAD_FAILURE
: // (0x800C0008L or -2146697208)
934 errorCode
= "INET_E_DOWNLOAD_FAILURE";
935 errorType
= wxWEB_NAV_ERR_CONNECTION
;
937 case INET_E_AUTHENTICATION_REQUIRED
: // (0x800C0009L or -2146697207)
938 errorCode
= "INET_E_AUTHENTICATION_REQUIRED";
939 errorType
= wxWEB_NAV_ERR_AUTH
;
941 case INET_E_NO_VALID_MEDIA
: // (0x800C000AL or -2146697206)
942 errorCode
= "INET_E_NO_VALID_MEDIA";
943 errorType
= wxWEB_NAV_ERR_REQUEST
;
945 case INET_E_CONNECTION_TIMEOUT
: // (0x800C000BL or -2146697205)
946 errorCode
= "INET_E_CONNECTION_TIMEOUT";
947 errorType
= wxWEB_NAV_ERR_CONNECTION
;
949 case INET_E_INVALID_REQUEST
: // (0x800C000CL or -2146697204)
950 errorCode
= "INET_E_INVALID_REQUEST";
951 errorType
= wxWEB_NAV_ERR_REQUEST
;
953 case INET_E_UNKNOWN_PROTOCOL
: // (0x800C000DL or -2146697203)
954 errorCode
= "INET_E_UNKNOWN_PROTOCOL";
955 errorType
= wxWEB_NAV_ERR_REQUEST
;
957 case INET_E_SECURITY_PROBLEM
: // (0x800C000EL or -2146697202)
958 errorCode
= "INET_E_SECURITY_PROBLEM";
959 errorType
= wxWEB_NAV_ERR_SECURITY
;
961 case INET_E_CANNOT_LOAD_DATA
: // (0x800C000FL or -2146697201)
962 errorCode
= "INET_E_CANNOT_LOAD_DATA";
963 errorType
= wxWEB_NAV_ERR_OTHER
;
965 case INET_E_CANNOT_INSTANTIATE_OBJECT
:
966 // CoCreateInstance will return an error code if this happens,
967 // we'll handle this above.
970 case INET_E_REDIRECT_FAILED
: // (0x800C0014L or -2146697196)
971 errorCode
= "INET_E_REDIRECT_FAILED";
972 errorType
= wxWEB_NAV_ERR_OTHER
;
974 case INET_E_REDIRECT_TO_DIR
: // (0x800C0015L or -2146697195)
975 errorCode
= "INET_E_REDIRECT_TO_DIR";
976 errorType
= wxWEB_NAV_ERR_REQUEST
;
978 case INET_E_CANNOT_LOCK_REQUEST
: // (0x800C0016L or -2146697194)
979 errorCode
= "INET_E_CANNOT_LOCK_REQUEST";
980 errorType
= wxWEB_NAV_ERR_OTHER
;
982 case INET_E_USE_EXTEND_BINDING
: // (0x800C0017L or -2146697193)
983 errorCode
= "INET_E_USE_EXTEND_BINDING";
984 errorType
= wxWEB_NAV_ERR_OTHER
;
986 case INET_E_TERMINATED_BIND
: // (0x800C0018L or -2146697192)
987 errorCode
= "INET_E_TERMINATED_BIND";
988 errorType
= wxWEB_NAV_ERR_OTHER
;
990 case INET_E_INVALID_CERTIFICATE
: // (0x800C0019L or -2146697191)
991 errorCode
= "INET_E_INVALID_CERTIFICATE";
992 errorType
= wxWEB_NAV_ERR_CERTIFICATE
;
994 case INET_E_CODE_DOWNLOAD_DECLINED
: // (0x800C0100L or -2146696960)
995 errorCode
= "INET_E_CODE_DOWNLOAD_DECLINED";
996 errorType
= wxWEB_NAV_ERR_USER_CANCELLED
;
998 case INET_E_RESULT_DISPATCHED
: // (0x800C0200L or -2146696704)
999 // cancel request cancelled...
1000 errorCode
= "INET_E_RESULT_DISPATCHED";
1001 errorType
= wxWEB_NAV_ERR_OTHER
;
1003 case INET_E_CANNOT_REPLACE_SFP_FILE
: // (0x800C0300L or -2146696448)
1004 errorCode
= "INET_E_CANNOT_REPLACE_SFP_FILE";
1005 errorType
= wxWEB_NAV_ERR_SECURITY
;
1007 case INET_E_CODE_INSTALL_BLOCKED_BY_HASH_POLICY
:
1008 errorCode
= "INET_E_CODE_INSTALL_BLOCKED_BY_HASH_POLICY";
1009 errorType
= wxWEB_NAV_ERR_SECURITY
;
1011 case INET_E_CODE_INSTALL_SUPPRESSED
:
1012 errorCode
= "INET_E_CODE_INSTALL_SUPPRESSED";
1013 errorType
= wxWEB_NAV_ERR_SECURITY
;
1017 wxString url
= evt
[1].GetString();
1018 wxString target
= evt
[2].GetString();
1019 wxWebNavigationEvent
event(wxEVT_COMMAND_WEB_VIEW_ERROR
, GetId(),
1020 url
, target
, false);
1021 event
.SetEventObject(this);
1022 event
.SetInt(errorType
);
1023 event
.SetString(errorCode
);
1024 HandleWindowEvent(event
);
1027 case DISPID_NEWWINDOW3
:
1029 wxString url
= evt
[4].GetString();
1031 wxWebNavigationEvent
event(wxEVT_COMMAND_WEB_VIEW_NEWWINDOW
,
1032 GetId(), url
, wxEmptyString
, true);
1033 event
.SetEventObject(this);
1034 HandleWindowEvent(event
);
1036 //We always cancel this event otherwise an Internet Exporer window
1037 //is opened for the url
1038 wxActiveXEventNativeMSW
* nativeParams
= evt
.GetNativeParameters();
1039 *V_BOOLREF(&nativeParams
->pDispParams
->rgvarg
[3]) = VARIANT_TRUE
;
1047 VirtualProtocol::VirtualProtocol(wxWebProtocolHandler
*handler
)
1051 m_handler
= handler
;
1054 VirtualProtocol::~VirtualProtocol()
1058 ULONG
VirtualProtocol::AddRef()
1064 HRESULT
VirtualProtocol::QueryInterface(REFIID riid
, void **ppvObject
)
1066 if(riid
== IID_IUnknown
|| riid
== IID_IInternetProtocolRoot
||
1067 riid
== IID_IInternetProtocol
)
1069 *ppvObject
= (IInternetProtocol
*)this;
1073 else if(riid
== IID_IInternetProtocolInfo
)
1075 *ppvObject
= (IInternetProtocolInfo
*)this;
1086 ULONG
VirtualProtocol::Release()
1100 HRESULT
VirtualProtocol::Start(LPCWSTR szUrl
, IInternetProtocolSink
*pOIProtSink
,
1101 IInternetBindInfo
*pOIBindInfo
, DWORD grfPI
,
1102 HANDLE_PTR dwReserved
)
1105 wxUnusedVar(pOIBindInfo
);
1107 wxUnusedVar(dwReserved
);
1108 m_protocolSink
= pOIProtSink
;
1110 //We get the file itself from the protocol handler
1111 m_file
= m_handler
->GetFile(szUrl
);
1115 return INET_E_RESOURCE_NOT_FOUND
;
1117 //We return the stream length for current and total size as we can always
1118 //read the whole file from the stream
1119 wxFileOffset length
= m_file
->GetStream()->GetLength();
1120 m_protocolSink
->ReportData(BSCF_FIRSTDATANOTIFICATION
|
1121 BSCF_DATAFULLYAVAILABLE
|
1122 BSCF_LASTDATANOTIFICATION
,
1127 HRESULT
VirtualProtocol::Read(void *pv
, ULONG cb
, ULONG
*pcbRead
)
1129 //If the file is null we return false to indicte it is finished
1133 wxStreamError err
= m_file
->GetStream()->Read(pv
, cb
).GetLastError();
1134 *pcbRead
= m_file
->GetStream()->LastRead();
1136 if(err
== wxSTREAM_NO_ERROR
)
1141 m_protocolSink
->ReportResult(S_OK
, 0, NULL
);
1143 //As we are not eof there is more data
1146 else if(err
== wxSTREAM_EOF
)
1149 m_protocolSink
->ReportResult(S_OK
, 0, NULL
);
1150 //We are eof and so finished
1153 else if(err
== wxSTREAM_READ_ERROR
)
1156 return INET_E_DOWNLOAD_FAILURE
;
1160 //Dummy return to surpress a compiler warning
1162 return INET_E_DOWNLOAD_FAILURE
;
1166 HRESULT
VirtualProtocol::CombineUrl(LPCWSTR pwzBaseUrl
, LPCWSTR pwzRelativeUrl
,
1167 DWORD dwCombineFlags
, LPWSTR pwzResult
,
1168 DWORD cchResult
, DWORD
*pcchResult
,
1171 return INET_E_DEFAULT_ACTION
;
1174 HRESULT
VirtualProtocol::ParseUrl(LPCWSTR pwzUrl
, PARSEACTION ParseAction
,
1175 DWORD dwParseFlags
, LPWSTR pwzResult
,
1176 DWORD cchResult
, DWORD
*pcchResult
,
1179 //return INET_E_DEFAULT_ACTION;
1180 wcscpy(pwzResult
, pwzUrl
);
1181 *pcchResult
= wcslen(pwzResult
);
1185 HRESULT
VirtualProtocol::QueryInfo(LPCWSTR pwzUrl
, QUERYOPTION OueryOption
,
1186 DWORD dwQueryFlags
, LPVOID pBuffer
,
1187 DWORD cbBuffer
, DWORD
*pcbBuf
,
1190 return INET_E_DEFAULT_ACTION
;
1193 HRESULT
ClassFactory::CreateInstance(IUnknown
* pUnkOuter
, REFIID riid
,
1197 return CLASS_E_NOAGGREGATION
;
1198 VirtualProtocol
* vp
= new VirtualProtocol(m_handler
);
1200 HRESULT hr
= vp
->QueryInterface(riid
, ppvObject
);
1206 STDMETHODIMP
ClassFactory::LockServer(BOOL fLock
)
1212 ULONG
ClassFactory::AddRef(void)
1218 HRESULT
ClassFactory::QueryInterface(REFIID riid
, void **ppvObject
)
1220 if ((riid
== IID_IUnknown
) || (riid
== IID_IClassFactory
))
1234 ULONG
ClassFactory::Release(void)