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, 2011 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"
19 #if wxUSE_WEBVIEW && wxUSE_WEBVIEW_IE
26 #include "wx/msw/registry.h"
27 #include "wx/msw/missing.h"
28 #include "wx/filesys.h"
29 #include "wx/dynlib.h"
32 /* These GUID definitions are our own implementation to support interfaces
33 * normally in urlmon.h. See include/wx/msw/webview_ie.h
38 DEFINE_GUID(wxIID_IInternetProtocolRoot
,0x79eac9e3,0xbaf9,0x11ce,0x8c,0x82,0,0xaa,0,0x4b,0xa9,0xb);
39 DEFINE_GUID(wxIID_IInternetProtocol
,0x79eac9e4,0xbaf9,0x11ce,0x8c,0x82,0,0xaa,0,0x4b,0xa9,0xb);
43 wxIMPLEMENT_DYNAMIC_CLASS(wxWebViewIE
, wxWebView
);
45 BEGIN_EVENT_TABLE(wxWebViewIE
, wxControl
)
46 EVT_ACTIVEX(wxID_ANY
, wxWebViewIE::onActiveXEvent
)
47 EVT_ERASE_BACKGROUND(wxWebViewIE::onEraseBg
)
50 bool wxWebViewIE::Create(wxWindow
* parent
,
58 if (!wxControl::Create(parent
, id
, pos
, size
, style
,
59 wxDefaultValidator
, name
))
66 m_historyLoadingFromList
= false;
67 m_historyEnabled
= true;
68 m_historyPosition
= -1;
69 m_zoomType
= wxWEB_VIEW_ZOOM_TYPE_TEXT
;
71 if (::CoCreateInstance(CLSID_WebBrowser
, NULL
,
72 CLSCTX_INPROC_SERVER
, // CLSCTX_INPROC,
73 IID_IWebBrowser2
, (void**)&m_webBrowser
) != 0)
75 wxLogError("Failed to initialize IE, CoCreateInstance returned an error");
79 m_ie
.SetDispatchPtr(m_webBrowser
); // wxAutomationObject will release itself
81 m_webBrowser
->put_RegisterAsBrowser(VARIANT_TRUE
);
82 m_webBrowser
->put_RegisterAsDropTarget(VARIANT_TRUE
);
84 m_container
= new wxActiveXContainer(this, IID_IWebBrowser2
, m_webBrowser
);
90 wxWebViewIE::~wxWebViewIE()
92 for(unsigned int i
= 0; i
< m_factories
.size(); i
++)
94 m_factories
[i
]->Release();
98 void wxWebViewIE::LoadURL(const wxString
& url
)
100 m_ie
.CallMethod("Navigate", wxConvertStringToOle(url
));
103 void wxWebViewIE::SetPage(const wxString
& html
, const wxString
& baseUrl
)
105 BSTR bstr
= SysAllocString(html
.wc_str());
107 // Creates a new one-dimensional array
108 SAFEARRAY
*psaStrings
= SafeArrayCreateVector(VT_VARIANT
, 0, 1);
109 if (psaStrings
!= NULL
)
113 HRESULT hr
= SafeArrayAccessData(psaStrings
, (LPVOID
*)¶m
);
115 param
->bstrVal
= bstr
;
116 hr
= SafeArrayUnaccessData(psaStrings
);
118 IHTMLDocument2
* document
= GetDocument();
119 document
->write(psaStrings
);
122 // SafeArrayDestroy calls SysFreeString for each BSTR
123 SafeArrayDestroy(psaStrings
);
125 //We send the events when we are done to mimic webkit
127 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_NAVIGATED
,
128 GetId(), baseUrl
, "");
129 event
.SetEventObject(this);
130 HandleWindowEvent(event
);
132 //Document complete event
133 event
.SetEventType(wxEVT_COMMAND_WEB_VIEW_LOADED
);
134 event
.SetEventObject(this);
135 HandleWindowEvent(event
);
139 wxLogError("wxWebViewIE::SetPage() : psaStrings is NULL");
144 wxString
wxWebViewIE::GetPageSource() const
146 IHTMLDocument2
* document
= GetDocument();
147 IHTMLElement
*bodyTag
= NULL
;
148 IHTMLElement
*htmlTag
= NULL
;
150 HRESULT hr
= document
->get_body(&bodyTag
);
153 hr
= bodyTag
->get_parentElement(&htmlTag
);
157 htmlTag
->get_outerHTML(&bstr
);
158 source
= wxString(bstr
);
168 wxWebViewZoom
wxWebViewIE::GetZoom() const
172 case wxWEB_VIEW_ZOOM_TYPE_LAYOUT
:
173 return GetIEOpticalZoom();
174 case wxWEB_VIEW_ZOOM_TYPE_TEXT
:
175 return GetIETextZoom();
180 //Dummy return to stop compiler warnings
181 return wxWEB_VIEW_ZOOM_MEDIUM
;
185 void wxWebViewIE::SetZoom(wxWebViewZoom zoom
)
189 case wxWEB_VIEW_ZOOM_TYPE_LAYOUT
:
190 SetIEOpticalZoom(zoom
);
192 case wxWEB_VIEW_ZOOM_TYPE_TEXT
:
200 void wxWebViewIE::SetIETextZoom(wxWebViewZoom level
)
202 //We do not use OLECMDID_OPTICAL_GETZOOMRANGE as the docs say the range
203 //is 0 to 4 so the check is unnecessary, these match exactly with the
206 VariantInit (&zoomVariant
);
207 V_VT(&zoomVariant
) = VT_I4
;
208 V_I4(&zoomVariant
) = level
;
213 m_webBrowser
->ExecWB(OLECMDID_ZOOM
,
214 OLECMDEXECOPT_DONTPROMPTUSER
,
216 wxASSERT(result
== S_OK
);
219 wxWebViewZoom
wxWebViewIE::GetIETextZoom() const
222 VariantInit (&zoomVariant
);
223 V_VT(&zoomVariant
) = VT_I4
;
228 m_webBrowser
->ExecWB(OLECMDID_ZOOM
,
229 OLECMDEXECOPT_DONTPROMPTUSER
,
231 wxASSERT(result
== S_OK
);
233 //We can safely cast here as we know that the range matches our enum
234 return static_cast<wxWebViewZoom
>(V_I4(&zoomVariant
));
237 void wxWebViewIE::SetIEOpticalZoom(wxWebViewZoom level
)
239 //We do not use OLECMDID_OPTICAL_GETZOOMRANGE as the docs say the range
240 //is 10 to 1000 so the check is unnecessary
242 VariantInit (&zoomVariant
);
243 V_VT(&zoomVariant
) = VT_I4
;
245 //We make a somewhat arbitray map here, taken from values used by webkit
248 case wxWEB_VIEW_ZOOM_TINY
:
249 V_I4(&zoomVariant
) = 60;
251 case wxWEB_VIEW_ZOOM_SMALL
:
252 V_I4(&zoomVariant
) = 80;
254 case wxWEB_VIEW_ZOOM_MEDIUM
:
255 V_I4(&zoomVariant
) = 100;
257 case wxWEB_VIEW_ZOOM_LARGE
:
258 V_I4(&zoomVariant
) = 130;
260 case wxWEB_VIEW_ZOOM_LARGEST
:
261 V_I4(&zoomVariant
) = 160;
270 m_webBrowser
->ExecWB((OLECMDID
)63 /*OLECMDID_OPTICAL_ZOOM*/,
271 OLECMDEXECOPT_DODEFAULT
,
274 wxASSERT(result
== S_OK
);
277 wxWebViewZoom
wxWebViewIE::GetIEOpticalZoom() const
280 VariantInit (&zoomVariant
);
281 V_VT(&zoomVariant
) = VT_I4
;
286 m_webBrowser
->ExecWB((OLECMDID
)63 /*OLECMDID_OPTICAL_ZOOM*/,
287 OLECMDEXECOPT_DODEFAULT
, NULL
,
289 wxASSERT(result
== S_OK
);
291 const int zoom
= V_I4(&zoomVariant
);
293 //We make a somewhat arbitray map here, taken from values used by webkit
296 return wxWEB_VIEW_ZOOM_TINY
;
298 else if (zoom
> 65 && zoom
<= 90)
300 return wxWEB_VIEW_ZOOM_SMALL
;
302 else if (zoom
> 90 && zoom
<= 115)
304 return wxWEB_VIEW_ZOOM_MEDIUM
;
306 else if (zoom
> 115 && zoom
<= 145)
308 return wxWEB_VIEW_ZOOM_LARGE
;
310 else /*if (zoom > 145) */ //Using else removes a compiler warning
312 return wxWEB_VIEW_ZOOM_LARGEST
;
316 void wxWebViewIE::SetZoomType(wxWebViewZoomType type
)
321 wxWebViewZoomType
wxWebViewIE::GetZoomType() const
326 bool wxWebViewIE::CanSetZoomType(wxWebViewZoomType type
) const
328 //IE 6 and below only support text zoom, so check the registry to see what
329 //version we actually have
330 wxRegKey
key(wxRegKey::HKLM
, "Software\\Microsoft\\Internet Explorer");
332 key
.QueryValue("Version", value
);
334 long version
= wxAtoi(value
.Left(1));
335 if(version
<= 6 && type
== wxWEB_VIEW_ZOOM_TYPE_LAYOUT
)
341 void wxWebViewIE::Print()
343 m_webBrowser
->ExecWB(OLECMDID_PRINTPREVIEW
,
344 OLECMDEXECOPT_DODEFAULT
, NULL
, NULL
);
347 bool wxWebViewIE::CanGoBack() const
350 return m_historyPosition
> 0;
355 bool wxWebViewIE::CanGoForward() const
358 return m_historyPosition
!= static_cast<int>(m_historyList
.size()) - 1;
363 void wxWebViewIE::LoadHistoryItem(wxSharedPtr
<wxWebViewHistoryItem
> item
)
366 for(unsigned int i
= 0; i
< m_historyList
.size(); i
++)
368 //We compare the actual pointers to find the correct item
369 if(m_historyList
[i
].get() == item
.get())
372 wxASSERT_MSG(pos
!= static_cast<int>(m_historyList
.size()),
373 "invalid history item");
374 m_historyLoadingFromList
= true;
375 LoadURL(item
->GetUrl());
376 m_historyPosition
= pos
;
379 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > wxWebViewIE::GetBackwardHistory()
381 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > backhist
;
382 //As we don't have std::copy or an iterator constructor in the wxwidgets
383 //native vector we construct it by hand
384 for(int i
= 0; i
< m_historyPosition
; i
++)
386 backhist
.push_back(m_historyList
[i
]);
391 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > wxWebViewIE::GetForwardHistory()
393 wxVector
<wxSharedPtr
<wxWebViewHistoryItem
> > forwardhist
;
394 //As we don't have std::copy or an iterator constructor in the wxwidgets
395 //native vector we construct it by hand
396 for(int i
= m_historyPosition
+ 1; i
< static_cast<int>(m_historyList
.size()); i
++)
398 forwardhist
.push_back(m_historyList
[i
]);
403 void wxWebViewIE::GoBack()
405 LoadHistoryItem(m_historyList
[m_historyPosition
- 1]);
408 void wxWebViewIE::GoForward()
410 LoadHistoryItem(m_historyList
[m_historyPosition
+ 1]);
413 void wxWebViewIE::Stop()
415 m_ie
.CallMethod("Stop");
418 void wxWebViewIE::ClearHistory()
420 m_historyList
.clear();
421 m_historyPosition
= -1;
424 void wxWebViewIE::EnableHistory(bool enable
)
426 m_historyEnabled
= enable
;
427 m_historyList
.clear();
428 m_historyPosition
= -1;
431 void wxWebViewIE::Reload(wxWebViewReloadFlags flags
)
435 V_VT(&level
) = VT_I2
;
439 case wxWEB_VIEW_RELOAD_DEFAULT
:
440 V_I2(&level
) = REFRESH_NORMAL
;
442 case wxWEB_VIEW_RELOAD_NO_CACHE
:
443 V_I2(&level
) = REFRESH_COMPLETELY
;
446 wxFAIL_MSG("Unexpected reload type");
449 m_webBrowser
->Refresh2(&level
);
452 bool wxWebViewIE::IsOfflineMode()
454 wxVariant out
= m_ie
.GetProperty("Offline");
456 wxASSERT(out
.GetType() == "bool");
458 return out
.GetBool();
461 void wxWebViewIE::SetOfflineMode(bool offline
)
463 // FIXME: the wxWidgets docs do not really document what the return
464 // parameter of PutProperty is
468 m_ie
.PutProperty("Offline", (offline
?
474 bool wxWebViewIE::IsBusy() const
476 if (m_isBusy
) return true;
478 wxVariant out
= m_ie
.GetProperty("Busy");
480 wxASSERT(out
.GetType() == "bool");
482 return out
.GetBool();
485 wxString
wxWebViewIE::GetCurrentURL() const
487 wxVariant out
= m_ie
.GetProperty("LocationURL");
489 wxASSERT(out
.GetType() == "string");
490 return out
.GetString();
493 wxString
wxWebViewIE::GetCurrentTitle() const
495 IHTMLDocument2
* document
= GetDocument();
498 document
->get_nameProp(&title
);
500 return wxString(title
);
503 bool wxWebViewIE::CanCut() const
505 return CanExecCommand("Cut");
508 bool wxWebViewIE::CanCopy() const
510 return CanExecCommand("Copy");
512 bool wxWebViewIE::CanPaste() const
514 return CanExecCommand("Paste");
517 void wxWebViewIE::Cut()
522 void wxWebViewIE::Copy()
527 void wxWebViewIE::Paste()
529 ExecCommand("Paste");
532 bool wxWebViewIE::CanUndo() const
534 return CanExecCommand("Undo");
536 bool wxWebViewIE::CanRedo() const
538 return CanExecCommand("Redo");
541 void wxWebViewIE::Undo()
546 void wxWebViewIE::Redo()
551 void wxWebViewIE::SetEditable(bool enable
)
553 IHTMLDocument2
* document
= GetDocument();
555 document
->put_designMode(SysAllocString(L
"On"));
557 document
->put_designMode(SysAllocString(L
"Off"));
562 bool wxWebViewIE::IsEditable() const
564 IHTMLDocument2
* document
= GetDocument();
566 document
->get_designMode(&mode
);
568 if(wxString(mode
) == "On")
574 void wxWebViewIE::SelectAll()
576 ExecCommand("SelectAll");
579 bool wxWebViewIE::HasSelection() const
581 IHTMLDocument2
* document
= GetDocument();
582 IHTMLSelectionObject
* selection
;
584 HRESULT hr
= document
->get_selection(&selection
);
588 selection
->get_type(&type
);
589 sel
= wxString(type
);
590 selection
->Release();
593 return sel
!= "None";
596 void wxWebViewIE::DeleteSelection()
598 ExecCommand("Delete");
601 wxString
wxWebViewIE::GetSelectedText() const
603 IHTMLDocument2
* document
= GetDocument();
604 IHTMLSelectionObject
* selection
;
606 HRESULT hr
= document
->get_selection(&selection
);
610 hr
= selection
->createRange(&disrange
);
613 IHTMLTxtRange
* range
;
614 hr
= disrange
->QueryInterface(IID_IHTMLTxtRange
, (void**)&range
);
618 range
->get_text(&text
);
619 selected
= wxString(text
);
624 selection
->Release();
630 wxString
wxWebViewIE::GetSelectedSource() const
632 IHTMLDocument2
* document
= GetDocument();
633 IHTMLSelectionObject
* selection
;
635 HRESULT hr
= document
->get_selection(&selection
);
639 hr
= selection
->createRange(&disrange
);
642 IHTMLTxtRange
* range
;
643 hr
= disrange
->QueryInterface(IID_IHTMLTxtRange
, (void**)&range
);
647 range
->get_htmlText(&text
);
648 selected
= wxString(text
);
653 selection
->Release();
659 void wxWebViewIE::ClearSelection()
661 IHTMLDocument2
* document
= GetDocument();
662 IHTMLSelectionObject
* selection
;
664 HRESULT hr
= document
->get_selection(&selection
);
668 selection
->Release();
673 wxString
wxWebViewIE::GetPageText() const
675 IHTMLDocument2
* document
= GetDocument();
678 HRESULT hr
= document
->get_body(&body
);
682 body
->get_innerText(&out
);
683 text
= wxString(out
);
690 void wxWebViewIE::RunScript(const wxString
& javascript
)
692 IHTMLDocument2
* document
= GetDocument();
693 IHTMLWindow2
* window
;
694 wxString language
= "javascript";
695 HRESULT hr
= document
->get_parentWindow(&window
);
700 V_VT(&level
) = VT_EMPTY
;
701 window
->execScript(SysAllocString(javascript
), SysAllocString(language
), &level
);
706 void wxWebViewIE::RegisterHandler(wxSharedPtr
<wxWebViewHandler
> handler
)
708 wxDynamicLibrary
urlMon(wxT("urlmon.dll"));
709 if(urlMon
.HasSymbol(wxT("CoInternetGetSession")))
711 typedef HRESULT (WINAPI
*CoInternetGetSession_t
)(DWORD
, wxIInternetSession
**, DWORD
);
712 wxDYNLIB_FUNCTION(CoInternetGetSession_t
, CoInternetGetSession
, urlMon
);
714 ClassFactory
* cf
= new ClassFactory(handler
);
715 wxIInternetSession
* session
;
716 HRESULT res
= (*pfnCoInternetGetSession
)(0, &session
, 0);
719 wxFAIL_MSG("Could not retrive internet session");
722 HRESULT hr
= session
->RegisterNameSpace(cf
, CLSID_FileProtocol
, handler
->GetName(), 0, NULL
, 0);
725 wxFAIL_MSG("Could not register protocol");
727 m_factories
.push_back(cf
);
731 wxFAIL_MSG("urlmon does not contain CoInternetGetSession");
735 bool wxWebViewIE::CanExecCommand(wxString command
) const
737 IHTMLDocument2
* document
= GetDocument();
738 VARIANT_BOOL enabled
;
740 document
->queryCommandEnabled(SysAllocString(command
.wc_str()), &enabled
);
743 return (enabled
== VARIANT_TRUE
);
746 void wxWebViewIE::ExecCommand(wxString command
)
748 IHTMLDocument2
* document
= GetDocument();
749 document
->execCommand(SysAllocString(command
.wc_str()), VARIANT_FALSE
, VARIANT(), NULL
);
753 IHTMLDocument2
* wxWebViewIE::GetDocument() const
755 wxVariant variant
= m_ie
.GetProperty("Document");
756 IHTMLDocument2
* document
= (IHTMLDocument2
*)variant
.GetVoidPtr();
763 void wxWebViewIE::onActiveXEvent(wxActiveXEvent
& evt
)
765 if (m_webBrowser
== NULL
) return;
767 switch (evt
.GetDispatchId())
769 case DISPID_BEFORENAVIGATE2
:
773 wxString url
= evt
[1].GetString();
774 wxString target
= evt
[3].GetString();
776 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_NAVIGATING
,
777 GetId(), url
, target
);
779 //skip empty javascript events.
780 if(url
== "javascript:\"\"" && target
.IsEmpty())
786 event
.SetEventObject(this);
787 HandleWindowEvent(event
);
790 if (!event
.IsAllowed())
792 wxActiveXEventNativeMSW
* nativeParams
=
793 evt
.GetNativeParameters();
794 *V_BOOLREF(&nativeParams
->pDispParams
->rgvarg
[0]) = VARIANT_TRUE
;
797 // at this point, either the navigation event has been cancelled
798 // and we're not busy, either it was accepted and IWebBrowser2's
799 // Busy property will be true; so we don't need our override
806 case DISPID_NAVIGATECOMPLETE2
:
808 wxString url
= evt
[1].GetString();
809 // TODO: set target parameter if possible
810 wxString target
= wxEmptyString
;
811 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_NAVIGATED
,
812 GetId(), url
, target
);
813 event
.SetEventObject(this);
814 HandleWindowEvent(event
);
818 case DISPID_PROGRESSCHANGE
:
824 case DISPID_DOCUMENTCOMPLETE
:
826 //Only send a complete even if we are actually finished, this brings
827 //the event in to line with webkit
829 m_webBrowser
->get_ReadyState( &rs
);
830 if(rs
!= READYSTATE_COMPLETE
)
833 wxString url
= evt
[1].GetString();
835 //As we are complete we also add to the history list, but not if the
836 //page is not the main page, ie it is a subframe
837 //We also have to check if we are loading a file:// url, if so we
838 //need to change the comparison as ie passes back a different style
840 if(m_historyEnabled
&& !m_historyLoadingFromList
&&
841 (url
== GetCurrentURL() ||
842 (GetCurrentURL().substr(0, 4) == "file" &&
843 wxFileSystem::URLToFileName(GetCurrentURL()).GetFullPath() == url
)))
845 //If we are not at the end of the list, then erase everything
846 //between us and the end before adding the new page
847 if(m_historyPosition
!= static_cast<int>(m_historyList
.size()) - 1)
849 m_historyList
.erase(m_historyList
.begin() + m_historyPosition
+ 1,
850 m_historyList
.end());
852 wxSharedPtr
<wxWebViewHistoryItem
> item(new wxWebViewHistoryItem(url
, GetCurrentTitle()));
853 m_historyList
.push_back(item
);
856 //Reset as we are done now
857 m_historyLoadingFromList
= false;
858 // TODO: set target parameter if possible
859 wxString target
= wxEmptyString
;
860 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_LOADED
, GetId(),
862 event
.SetEventObject(this);
863 HandleWindowEvent(event
);
867 case DISPID_STATUSTEXTCHANGE
:
872 case DISPID_TITLECHANGE
:
874 wxString title
= evt
[0].GetString();
876 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED
,
877 GetId(), GetCurrentURL(), "");
878 event
.SetString(title
);
879 event
.SetEventObject(this);
880 HandleWindowEvent(event
);
884 case DISPID_NAVIGATEERROR
:
886 wxWebViewNavigationError errorType
= wxWEB_NAV_ERR_OTHER
;
887 wxString errorCode
= "?";
888 switch (evt
[3].GetLong())
890 case INET_E_INVALID_URL
: // (0x800C0002L or -2146697214)
891 errorCode
= "INET_E_INVALID_URL";
892 errorType
= wxWEB_NAV_ERR_REQUEST
;
894 case INET_E_NO_SESSION
: // (0x800C0003L or -2146697213)
895 errorCode
= "INET_E_NO_SESSION";
896 errorType
= wxWEB_NAV_ERR_CONNECTION
;
898 case INET_E_CANNOT_CONNECT
: // (0x800C0004L or -2146697212)
899 errorCode
= "INET_E_CANNOT_CONNECT";
900 errorType
= wxWEB_NAV_ERR_CONNECTION
;
902 case INET_E_RESOURCE_NOT_FOUND
: // (0x800C0005L or -2146697211)
903 errorCode
= "INET_E_RESOURCE_NOT_FOUND";
904 errorType
= wxWEB_NAV_ERR_NOT_FOUND
;
906 case INET_E_OBJECT_NOT_FOUND
: // (0x800C0006L or -2146697210)
907 errorCode
= "INET_E_OBJECT_NOT_FOUND";
908 errorType
= wxWEB_NAV_ERR_NOT_FOUND
;
910 case INET_E_DATA_NOT_AVAILABLE
: // (0x800C0007L or -2146697209)
911 errorCode
= "INET_E_DATA_NOT_AVAILABLE";
912 errorType
= wxWEB_NAV_ERR_NOT_FOUND
;
914 case INET_E_DOWNLOAD_FAILURE
: // (0x800C0008L or -2146697208)
915 errorCode
= "INET_E_DOWNLOAD_FAILURE";
916 errorType
= wxWEB_NAV_ERR_CONNECTION
;
918 case INET_E_AUTHENTICATION_REQUIRED
: // (0x800C0009L or -2146697207)
919 errorCode
= "INET_E_AUTHENTICATION_REQUIRED";
920 errorType
= wxWEB_NAV_ERR_AUTH
;
922 case INET_E_NO_VALID_MEDIA
: // (0x800C000AL or -2146697206)
923 errorCode
= "INET_E_NO_VALID_MEDIA";
924 errorType
= wxWEB_NAV_ERR_REQUEST
;
926 case INET_E_CONNECTION_TIMEOUT
: // (0x800C000BL or -2146697205)
927 errorCode
= "INET_E_CONNECTION_TIMEOUT";
928 errorType
= wxWEB_NAV_ERR_CONNECTION
;
930 case INET_E_INVALID_REQUEST
: // (0x800C000CL or -2146697204)
931 errorCode
= "INET_E_INVALID_REQUEST";
932 errorType
= wxWEB_NAV_ERR_REQUEST
;
934 case INET_E_UNKNOWN_PROTOCOL
: // (0x800C000DL or -2146697203)
935 errorCode
= "INET_E_UNKNOWN_PROTOCOL";
936 errorType
= wxWEB_NAV_ERR_REQUEST
;
938 case INET_E_SECURITY_PROBLEM
: // (0x800C000EL or -2146697202)
939 errorCode
= "INET_E_SECURITY_PROBLEM";
940 errorType
= wxWEB_NAV_ERR_SECURITY
;
942 case INET_E_CANNOT_LOAD_DATA
: // (0x800C000FL or -2146697201)
943 errorCode
= "INET_E_CANNOT_LOAD_DATA";
944 errorType
= wxWEB_NAV_ERR_OTHER
;
946 case INET_E_CANNOT_INSTANTIATE_OBJECT
:
947 // CoCreateInstance will return an error code if this happens,
948 // we'll handle this above.
951 case INET_E_REDIRECT_FAILED
: // (0x800C0014L or -2146697196)
952 errorCode
= "INET_E_REDIRECT_FAILED";
953 errorType
= wxWEB_NAV_ERR_OTHER
;
955 case INET_E_REDIRECT_TO_DIR
: // (0x800C0015L or -2146697195)
956 errorCode
= "INET_E_REDIRECT_TO_DIR";
957 errorType
= wxWEB_NAV_ERR_REQUEST
;
959 case INET_E_CANNOT_LOCK_REQUEST
: // (0x800C0016L or -2146697194)
960 errorCode
= "INET_E_CANNOT_LOCK_REQUEST";
961 errorType
= wxWEB_NAV_ERR_OTHER
;
963 case INET_E_USE_EXTEND_BINDING
: // (0x800C0017L or -2146697193)
964 errorCode
= "INET_E_USE_EXTEND_BINDING";
965 errorType
= wxWEB_NAV_ERR_OTHER
;
967 case INET_E_TERMINATED_BIND
: // (0x800C0018L or -2146697192)
968 errorCode
= "INET_E_TERMINATED_BIND";
969 errorType
= wxWEB_NAV_ERR_OTHER
;
971 case INET_E_INVALID_CERTIFICATE
: // (0x800C0019L or -2146697191)
972 errorCode
= "INET_E_INVALID_CERTIFICATE";
973 errorType
= wxWEB_NAV_ERR_CERTIFICATE
;
975 case INET_E_CODE_DOWNLOAD_DECLINED
: // (0x800C0100L or -2146696960)
976 errorCode
= "INET_E_CODE_DOWNLOAD_DECLINED";
977 errorType
= wxWEB_NAV_ERR_USER_CANCELLED
;
979 case INET_E_RESULT_DISPATCHED
: // (0x800C0200L or -2146696704)
980 // cancel request cancelled...
981 errorCode
= "INET_E_RESULT_DISPATCHED";
982 errorType
= wxWEB_NAV_ERR_OTHER
;
984 case INET_E_CANNOT_REPLACE_SFP_FILE
: // (0x800C0300L or -2146696448)
985 errorCode
= "INET_E_CANNOT_REPLACE_SFP_FILE";
986 errorType
= wxWEB_NAV_ERR_SECURITY
;
988 case INET_E_CODE_INSTALL_BLOCKED_BY_HASH_POLICY
:
989 errorCode
= "INET_E_CODE_INSTALL_BLOCKED_BY_HASH_POLICY";
990 errorType
= wxWEB_NAV_ERR_SECURITY
;
992 case INET_E_CODE_INSTALL_SUPPRESSED
:
993 errorCode
= "INET_E_CODE_INSTALL_SUPPRESSED";
994 errorType
= wxWEB_NAV_ERR_SECURITY
;
998 wxString url
= evt
[1].GetString();
999 wxString target
= evt
[2].GetString();
1000 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_ERROR
, GetId(),
1002 event
.SetEventObject(this);
1003 event
.SetInt(errorType
);
1004 event
.SetString(errorCode
);
1005 HandleWindowEvent(event
);
1008 case DISPID_NEWWINDOW3
:
1010 wxString url
= evt
[4].GetString();
1012 wxWebViewEvent
event(wxEVT_COMMAND_WEB_VIEW_NEWWINDOW
,
1013 GetId(), url
, wxEmptyString
);
1014 event
.SetEventObject(this);
1015 HandleWindowEvent(event
);
1017 //We always cancel this event otherwise an Internet Exporer window
1018 //is opened for the url
1019 wxActiveXEventNativeMSW
* nativeParams
= evt
.GetNativeParameters();
1020 *V_BOOLREF(&nativeParams
->pDispParams
->rgvarg
[3]) = VARIANT_TRUE
;
1028 VirtualProtocol::VirtualProtocol(wxSharedPtr
<wxWebViewHandler
> handler
)
1032 m_handler
= handler
;
1035 VirtualProtocol::~VirtualProtocol()
1039 ULONG
VirtualProtocol::AddRef()
1045 HRESULT
VirtualProtocol::QueryInterface(REFIID riid
, void **ppvObject
)
1047 if(riid
== IID_IUnknown
|| riid
== wxIID_IInternetProtocolRoot
||
1048 riid
== wxIID_IInternetProtocol
)
1050 *ppvObject
= (wxIInternetProtocol
*)this;
1061 ULONG
VirtualProtocol::Release()
1075 HRESULT
VirtualProtocol::Start(LPCWSTR szUrl
, wxIInternetProtocolSink
*pOIProtSink
,
1076 wxIInternetBindInfo
*pOIBindInfo
, DWORD grfPI
,
1077 HANDLE_PTR dwReserved
)
1080 wxUnusedVar(pOIBindInfo
);
1082 wxUnusedVar(dwReserved
);
1083 m_protocolSink
= pOIProtSink
;
1085 //We get the file itself from the protocol handler
1086 m_file
= m_handler
->GetFile(szUrl
);
1090 return INET_E_RESOURCE_NOT_FOUND
;
1092 //We return the stream length for current and total size as we can always
1093 //read the whole file from the stream
1094 wxFileOffset length
= m_file
->GetStream()->GetLength();
1095 m_protocolSink
->ReportData(wxBSCF_FIRSTDATANOTIFICATION
|
1096 wxBSCF_DATAFULLYAVAILABLE
|
1097 wxBSCF_LASTDATANOTIFICATION
,
1102 HRESULT
VirtualProtocol::Read(void *pv
, ULONG cb
, ULONG
*pcbRead
)
1104 //If the file is null we return false to indicte it is finished
1108 wxStreamError err
= m_file
->GetStream()->Read(pv
, cb
).GetLastError();
1109 *pcbRead
= m_file
->GetStream()->LastRead();
1111 if(err
== wxSTREAM_NO_ERROR
)
1116 m_protocolSink
->ReportResult(S_OK
, 0, NULL
);
1118 //As we are not eof there is more data
1121 else if(err
== wxSTREAM_EOF
)
1124 m_protocolSink
->ReportResult(S_OK
, 0, NULL
);
1125 //We are eof and so finished
1128 else if(err
== wxSTREAM_READ_ERROR
)
1131 return INET_E_DOWNLOAD_FAILURE
;
1135 //Dummy return to surpress a compiler warning
1137 return INET_E_DOWNLOAD_FAILURE
;
1141 HRESULT
ClassFactory::CreateInstance(IUnknown
* pUnkOuter
, REFIID riid
,
1145 return CLASS_E_NOAGGREGATION
;
1146 VirtualProtocol
* vp
= new VirtualProtocol(m_handler
);
1148 HRESULT hr
= vp
->QueryInterface(riid
, ppvObject
);
1154 STDMETHODIMP
ClassFactory::LockServer(BOOL fLock
)
1160 ULONG
ClassFactory::AddRef(void)
1166 HRESULT
ClassFactory::QueryInterface(REFIID riid
, void **ppvObject
)
1168 if ((riid
== IID_IUnknown
) || (riid
== IID_IClassFactory
))
1182 ULONG
ClassFactory::Release(void)
1197 #endif // wxUSE_WEBVIEW && wxUSE_WEBVIEW_IE