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"
26 #include "wx/msw/registry.h"
27 #include "wx/msw/missing.h"
28 #include "wx/filesys.h"
30 wxIMPLEMENT_DYNAMIC_CLASS(wxWebViewIE
, wxWebView
);
32 //We link to urlmon as it is required for CoInternetGetSession
33 #pragma comment(lib, "urlmon")
35 BEGIN_EVENT_TABLE(wxWebViewIE
, wxControl
)
36 EVT_ACTIVEX(wxID_ANY
, wxWebViewIE::onActiveXEvent
)
37 EVT_ERASE_BACKGROUND(wxWebViewIE::onEraseBg
)
40 bool wxWebViewIE::Create(wxWindow
* parent
,
48 if (!wxControl::Create(parent
, id
, pos
, size
, style
,
49 wxDefaultValidator
, name
))
56 m_historyLoadingFromList
= false;
57 m_historyEnabled
= true;
58 m_historyPosition
= -1;
59 m_zoomType
= wxWEB_VIEW_ZOOM_TYPE_TEXT
;
61 if (::CoCreateInstance(CLSID_WebBrowser
, NULL
,
62 CLSCTX_INPROC_SERVER
, // CLSCTX_INPROC,
63 IID_IWebBrowser2
, (void**)&m_webBrowser
) != 0)
65 wxLogError("Failed to initialize IE, CoCreateInstance returned an error");
69 m_ie
.SetDispatchPtr(m_webBrowser
); // wxAutomationObject will release itself
71 m_webBrowser
->put_RegisterAsBrowser(VARIANT_TRUE
);
72 m_webBrowser
->put_RegisterAsDropTarget(VARIANT_TRUE
);
74 m_container
= new wxActiveXContainer(this, IID_IWebBrowser2
, m_webBrowser
);
76 SetBackgroundStyle(wxBG_STYLE_PAINT
);
77 SetDoubleBuffered(true);
83 void wxWebViewIE::LoadUrl(const wxString
& url
)
85 m_ie
.CallMethod("Navigate", (BSTR
) url
.wc_str(), NULL
, NULL
, NULL
, NULL
);
88 void wxWebViewIE::SetPage(const wxString
& html
, const wxString
& baseUrl
)
90 BSTR bstr
= SysAllocString(html
.wc_str());
92 // Creates a new one-dimensional array
93 SAFEARRAY
*psaStrings
= SafeArrayCreateVector(VT_VARIANT
, 0, 1);
94 if (psaStrings
!= NULL
)
98 HRESULT hr
= SafeArrayAccessData(psaStrings
, (LPVOID
*)¶m
);
100 param
->bstrVal
= bstr
;
101 hr
= SafeArrayUnaccessData(psaStrings
);
103 IHTMLDocument2
* document
= GetDocument();
104 document
->write(psaStrings
);
107 // SafeArrayDestroy calls SysFreeString for each BSTR
108 SafeArrayDestroy(psaStrings
);
110 //We send the events when we are done to mimic webkit
112 wxWebNavigationEvent
event(wxEVT_COMMAND_WEB_VIEW_NAVIGATED
,
113 GetId(), baseUrl
, "", false);
114 event
.SetEventObject(this);
115 HandleWindowEvent(event
);
117 //Document complete event
118 event
.SetEventType(wxEVT_COMMAND_WEB_VIEW_LOADED
);
119 event
.SetEventObject(this);
120 HandleWindowEvent(event
);
124 wxLogError("wxWebViewIE::SetPage() : psaStrings is NULL");
129 wxString
wxWebViewIE::GetPageSource()
131 IHTMLDocument2
* document
= GetDocument();
132 IHTMLElement
*bodyTag
= NULL
;
133 IHTMLElement
*htmlTag
= NULL
;
135 HRESULT hr
= document
->get_body(&bodyTag
);
138 hr
= bodyTag
->get_parentElement(&htmlTag
);
142 htmlTag
->get_outerHTML(&bstr
);
143 source
= wxString(bstr
);
153 wxWebViewZoom
wxWebViewIE::GetZoom()
155 if(m_zoomType
== wxWEB_VIEW_ZOOM_TYPE_LAYOUT
)
156 return GetIEOpticalZoom();
157 else if(m_zoomType
== wxWEB_VIEW_ZOOM_TYPE_TEXT
)
158 return GetIETextZoom();
162 //Dummy return to stop compiler warnings
163 return wxWEB_VIEW_ZOOM_MEDIUM
;
167 void wxWebViewIE::SetZoom(wxWebViewZoom zoom
)
169 if(m_zoomType
== wxWEB_VIEW_ZOOM_TYPE_LAYOUT
)
170 SetIEOpticalZoom(zoom
);
171 else if(m_zoomType
== wxWEB_VIEW_ZOOM_TYPE_TEXT
)
177 void wxWebViewIE::SetIETextZoom(wxWebViewZoom level
)
179 //We do not use OLECMDID_OPTICAL_GETZOOMRANGE as the docs say the range
180 //is 0 to 4 so the check is unnecessary, these match exactly with the
183 VariantInit (&zoomVariant
);
184 V_VT(&zoomVariant
) = VT_I4
;
185 V_I4(&zoomVariant
) = level
;
187 HRESULT result
= m_webBrowser
->ExecWB(OLECMDID_ZOOM
,
188 OLECMDEXECOPT_DONTPROMPTUSER
,
190 wxASSERT(result
== S_OK
);
193 wxWebViewZoom
wxWebViewIE::GetIETextZoom()
196 VariantInit (&zoomVariant
);
197 V_VT(&zoomVariant
) = VT_I4
;
199 HRESULT result
= m_webBrowser
->ExecWB(OLECMDID_ZOOM
,
200 OLECMDEXECOPT_DONTPROMPTUSER
,
202 wxASSERT(result
== S_OK
);
204 //We can safely cast here as we know that the range matches our enum
205 return static_cast<wxWebViewZoom
>(V_I4(&zoomVariant
));
208 void wxWebViewIE::SetIEOpticalZoom(wxWebViewZoom level
)
210 //We do not use OLECMDID_OPTICAL_GETZOOMRANGE as the docs say the range
211 //is 10 to 1000 so the check is unnecessary
213 VariantInit (&zoomVariant
);
214 V_VT(&zoomVariant
) = VT_I4
;
216 //We make a somewhat arbitray map here, taken from values used by webkit
219 case wxWEB_VIEW_ZOOM_TINY
:
220 V_I4(&zoomVariant
) = 60;
222 case wxWEB_VIEW_ZOOM_SMALL
:
223 V_I4(&zoomVariant
) = 80;
225 case wxWEB_VIEW_ZOOM_MEDIUM
:
226 V_I4(&zoomVariant
) = 100;
228 case wxWEB_VIEW_ZOOM_LARGE
:
229 V_I4(&zoomVariant
) = 130;
231 case wxWEB_VIEW_ZOOM_LARGEST
:
232 V_I4(&zoomVariant
) = 160;
238 HRESULT result
= m_webBrowser
->ExecWB((OLECMDID
)OLECMDID_OPTICAL_ZOOM
,
239 OLECMDEXECOPT_DODEFAULT
,
242 wxASSERT(result
== S_OK
);
245 wxWebViewZoom
wxWebViewIE::GetIEOpticalZoom()
248 VariantInit (&zoomVariant
);
249 V_VT(&zoomVariant
) = VT_I4
;
251 HRESULT result
= m_webBrowser
->ExecWB((OLECMDID
)OLECMDID_OPTICAL_ZOOM
,
252 OLECMDEXECOPT_DODEFAULT
, NULL
,
254 wxASSERT(result
== S_OK
);
256 const int zoom
= V_I4(&zoomVariant
);
258 //We make a somewhat arbitray map here, taken from values used by webkit
261 return wxWEB_VIEW_ZOOM_TINY
;
263 else if (zoom
> 65 && zoom
<= 90)
265 return wxWEB_VIEW_ZOOM_SMALL
;
267 else if (zoom
> 90 && zoom
<= 115)
269 return wxWEB_VIEW_ZOOM_MEDIUM
;
271 else if (zoom
> 115 && zoom
<= 145)
273 return wxWEB_VIEW_ZOOM_LARGE
;
275 else /*if (zoom > 145) */ //Using else removes a compiler warning
277 return wxWEB_VIEW_ZOOM_LARGEST
;
281 void wxWebViewIE::SetZoomType(wxWebViewZoomType type
)
286 wxWebViewZoomType
wxWebViewIE::GetZoomType() const
291 bool wxWebViewIE::CanSetZoomType(wxWebViewZoomType type
) const
293 //IE 6 and below only support text zoom, so check the registry to see what
294 //version we actually have
295 wxRegKey
key(wxRegKey::HKLM
, "Software\\Microsoft\\Internet Explorer");
297 key
.QueryValue("Version", value
);
299 long version
= wxAtoi(value
.Left(1));
300 if(version
<= 6 && type
== wxWEB_VIEW_ZOOM_TYPE_LAYOUT
)
306 void wxWebViewIE::Print()
308 m_webBrowser
->ExecWB(OLECMDID_PRINTPREVIEW
,
309 OLECMDEXECOPT_DODEFAULT
, NULL
, NULL
);
312 bool wxWebViewIE::CanGoBack()
315 return m_historyPosition
> 0;
320 bool wxWebViewIE::CanGoForward()
323 return m_historyPosition
!= static_cast<int>(m_historyList
.size()) - 1;
328 void wxWebViewIE::LoadHistoryItem(wxSharedPtr
<wxWebHistoryItem
> item
)
331 for(unsigned int i
= 0; i
< m_historyList
.size(); i
++)
333 //We compare the actual pointers to find the correct item
334 if(m_historyList
[i
].get() == item
.get())
337 wxASSERT_MSG(pos
!= static_cast<int>(m_historyList
.size()),
338 "invalid history item");
339 m_historyLoadingFromList
= true;
340 LoadUrl(item
->GetUrl());
341 m_historyPosition
= pos
;
344 wxVector
<wxSharedPtr
<wxWebHistoryItem
> > wxWebViewIE::GetBackwardHistory()
346 wxVector
<wxSharedPtr
<wxWebHistoryItem
> > backhist
;
347 //As we don't have std::copy or an iterator constructor in the wxwidgets
348 //native vector we construct it by hand
349 for(int i
= 0; i
< m_historyPosition
; i
++)
351 backhist
.push_back(m_historyList
[i
]);
356 wxVector
<wxSharedPtr
<wxWebHistoryItem
> > wxWebViewIE::GetForwardHistory()
358 wxVector
<wxSharedPtr
<wxWebHistoryItem
> > forwardhist
;
359 //As we don't have std::copy or an iterator constructor in the wxwidgets
360 //native vector we construct it by hand
361 for(int i
= m_historyPosition
+ 1; i
< static_cast<int>(m_historyList
.size()); i
++)
363 forwardhist
.push_back(m_historyList
[i
]);
368 void wxWebViewIE::GoBack()
370 LoadHistoryItem(m_historyList
[m_historyPosition
- 1]);
373 void wxWebViewIE::GoForward()
375 LoadHistoryItem(m_historyList
[m_historyPosition
+ 1]);
378 void wxWebViewIE::Stop()
380 m_ie
.CallMethod("Stop");
383 void wxWebViewIE::ClearHistory()
385 m_historyList
.clear();
386 m_historyPosition
= -1;
389 void wxWebViewIE::EnableHistory(bool enable
)
391 m_historyEnabled
= enable
;
392 m_historyList
.clear();
393 m_historyPosition
= -1;
396 void wxWebViewIE::Reload(wxWebViewReloadFlags flags
)
400 V_VT(&level
) = VT_I2
;
404 case wxWEB_VIEW_RELOAD_DEFAULT
:
405 V_I2(&level
) = REFRESH_NORMAL
;
407 case wxWEB_VIEW_RELOAD_NO_CACHE
:
408 V_I2(&level
) = REFRESH_COMPLETELY
;
411 wxFAIL_MSG("Unexpected reload type");
414 m_webBrowser
->Refresh2(&level
);
417 bool wxWebViewIE::IsOfflineMode()
419 wxVariant out
= m_ie
.GetProperty("Offline");
421 wxASSERT(out
.GetType() == "bool");
423 return out
.GetBool();
426 void wxWebViewIE::SetOfflineMode(bool offline
)
428 // FIXME: the wxWidgets docs do not really document what the return
429 // parameter of PutProperty is
430 const bool success
= m_ie
.PutProperty("Offline", (offline
?
436 bool wxWebViewIE::IsBusy()
438 if (m_isBusy
) return true;
440 wxVariant out
= m_ie
.GetProperty("Busy");
442 wxASSERT(out
.GetType() == "bool");
444 return out
.GetBool();
447 wxString
wxWebViewIE::GetCurrentURL()
449 wxVariant out
= m_ie
.GetProperty("LocationURL");
451 wxASSERT(out
.GetType() == "string");
452 return out
.GetString();
455 wxString
wxWebViewIE::GetCurrentTitle()
457 IHTMLDocument2
* document
= GetDocument();
460 document
->get_nameProp(&title
);
462 return wxString(title
);
465 bool wxWebViewIE::CanCut()
467 return CanExecCommand("Cut");
470 bool wxWebViewIE::CanCopy()
472 return CanExecCommand("Copy");
474 bool wxWebViewIE::CanPaste()
476 return CanExecCommand("Paste");
479 void wxWebViewIE::Cut()
484 void wxWebViewIE::Copy()
489 void wxWebViewIE::Paste()
491 ExecCommand("Paste");
494 bool wxWebViewIE::CanUndo()
496 return CanExecCommand("Undo");
498 bool wxWebViewIE::CanRedo()
500 return CanExecCommand("Redo");
503 void wxWebViewIE::Undo()
508 void wxWebViewIE::Redo()
513 void wxWebViewIE::SetEditable(bool enable
)
515 IHTMLDocument2
* document
= GetDocument();
517 document
->put_designMode(SysAllocString(L
"On"));
519 document
->put_designMode(SysAllocString(L
"Off"));
524 bool wxWebViewIE::IsEditable()
526 IHTMLDocument2
* document
= GetDocument();
528 document
->get_designMode(&mode
);
530 if(wxString(mode
) == "On")
536 void wxWebViewIE::SelectAll()
538 ExecCommand("SelectAll");
541 bool wxWebViewIE::HasSelection()
543 IHTMLDocument2
* document
= GetDocument();
544 IHTMLSelectionObject
* selection
;
546 HRESULT hr
= document
->get_selection(&selection
);
550 selection
->get_type(&type
);
551 sel
= wxString(type
);
552 selection
->Release();
555 return sel
!= "None";
558 void wxWebViewIE::DeleteSelection()
560 ExecCommand("Delete");
563 wxString
wxWebViewIE::GetSelectedText()
565 IHTMLDocument2
* document
= GetDocument();
566 IHTMLSelectionObject
* selection
;
568 HRESULT hr
= document
->get_selection(&selection
);
572 hr
= selection
->createRange(&disrange
);
575 IHTMLTxtRange
* range
;
576 hr
= disrange
->QueryInterface(IID_IHTMLTxtRange
, (void**)&range
);
580 range
->get_text(&text
);
581 selected
= wxString(text
);
586 selection
->Release();
592 wxString
wxWebViewIE::GetSelectedSource()
594 IHTMLDocument2
* document
= GetDocument();
595 IHTMLSelectionObject
* selection
;
597 HRESULT hr
= document
->get_selection(&selection
);
601 hr
= selection
->createRange(&disrange
);
604 IHTMLTxtRange
* range
;
605 hr
= disrange
->QueryInterface(IID_IHTMLTxtRange
, (void**)&range
);
609 range
->get_htmlText(&text
);
610 selected
= wxString(text
);
615 selection
->Release();
621 void wxWebViewIE::ClearSelection()
623 IHTMLDocument2
* document
= GetDocument();
624 IHTMLSelectionObject
* selection
;
626 HRESULT hr
= document
->get_selection(&selection
);
630 selection
->Release();
635 wxString
wxWebViewIE::GetPageText()
637 IHTMLDocument2
* document
= GetDocument();
640 HRESULT hr
= document
->get_body(&body
);
644 body
->get_innerText(&out
);
645 text
= wxString(out
);
652 void wxWebViewIE::RunScript(const wxString
& javascript
)
654 IHTMLDocument2
* document
= GetDocument();
655 IHTMLWindow2
* window
;
656 wxString language
= "javascript";
657 HRESULT hr
= document
->get_parentWindow(&window
);
662 V_VT(&level
) = VT_EMPTY
;
663 window
->execScript(SysAllocString(javascript
), SysAllocString(language
), &level
);
668 void wxWebViewIE::RegisterHandler(wxWebHandler
* handler
)
670 ClassFactory
* cf
= new ClassFactory(handler
);
671 IInternetSession
* session
;
672 if(FAILED(CoInternetGetSession(0, &session
, 0)))
674 wxFAIL_MSG("Could not retrive internet session");
677 HRESULT hr
= session
->RegisterNameSpace(cf
, CLSID_FileProtocol
, handler
->GetName(), 0, NULL
, 0);
680 wxFAIL_MSG("Could not register protocol");
684 bool wxWebViewIE::CanExecCommand(wxString command
)
686 IHTMLDocument2
* document
= GetDocument();
687 VARIANT_BOOL enabled
;
689 document
->queryCommandEnabled(SysAllocString(command
.wc_str()), &enabled
);
692 return (enabled
== VARIANT_TRUE
);
695 void wxWebViewIE::ExecCommand(wxString command
)
697 IHTMLDocument2
* document
= GetDocument();
698 document
->execCommand(SysAllocString(command
.wc_str()), VARIANT_FALSE
, VARIANT(), NULL
);
702 IHTMLDocument2
* wxWebViewIE::GetDocument()
704 wxVariant variant
= m_ie
.GetProperty("Document");
705 IHTMLDocument2
* document
= (IHTMLDocument2
*)variant
.GetVoidPtr();
712 void wxWebViewIE::onActiveXEvent(wxActiveXEvent
& evt
)
714 if (m_webBrowser
== NULL
) return;
716 switch (evt
.GetDispatchId())
718 case DISPID_BEFORENAVIGATE2
:
722 wxString url
= evt
[1].GetString();
723 wxString target
= evt
[3].GetString();
725 wxWebNavigationEvent
event(wxEVT_COMMAND_WEB_VIEW_NAVIGATING
,
726 GetId(), url
, target
, true);
727 event
.SetEventObject(this);
728 HandleWindowEvent(event
);
730 if (event
.IsVetoed())
732 wxActiveXEventNativeMSW
* nativeParams
=
733 evt
.GetNativeParameters();
734 *V_BOOLREF(&nativeParams
->pDispParams
->rgvarg
[0]) = VARIANT_TRUE
;
737 // at this point, either the navigation event has been cancelled
738 // and we're not busy, either it was accepted and IWebBrowser2's
739 // Busy property will be true; so we don't need our override
746 case DISPID_NAVIGATECOMPLETE2
:
748 wxString url
= evt
[1].GetString();
749 // TODO: set target parameter if possible
750 wxString target
= wxEmptyString
;
751 wxWebNavigationEvent
event(wxEVT_COMMAND_WEB_VIEW_NAVIGATED
,
752 GetId(), url
, target
, false);
753 event
.SetEventObject(this);
754 HandleWindowEvent(event
);
758 case DISPID_PROGRESSCHANGE
:
764 case DISPID_DOCUMENTCOMPLETE
:
766 //Only send a complete even if we are actually finished, this brings
767 //the event in to line with webkit
769 m_webBrowser
->get_ReadyState( &rs
);
770 if(rs
!= READYSTATE_COMPLETE
)
773 wxString url
= evt
[1].GetString();
775 //As we are complete we also add to the history list, but not if the
776 //page is not the main page, ie it is a subframe
777 if(m_historyEnabled
&& !m_historyLoadingFromList
&& url
== GetCurrentURL())
779 //If we are not at the end of the list, then erase everything
780 //between us and the end before adding the new page
781 if(m_historyPosition
!= static_cast<int>(m_historyList
.size()) - 1)
783 m_historyList
.erase(m_historyList
.begin() + m_historyPosition
+ 1,
784 m_historyList
.end());
786 wxSharedPtr
<wxWebHistoryItem
> item(new wxWebHistoryItem(url
, GetCurrentTitle()));
787 m_historyList
.push_back(item
);
790 //Reset as we are done now
791 m_historyLoadingFromList
= false;
792 // TODO: set target parameter if possible
793 wxString target
= wxEmptyString
;
794 wxWebNavigationEvent
event(wxEVT_COMMAND_WEB_VIEW_LOADED
, GetId(),
796 event
.SetEventObject(this);
797 HandleWindowEvent(event
);
801 case DISPID_STATUSTEXTCHANGE
:
806 case DISPID_TITLECHANGE
:
808 wxString title
= evt
[0].GetString();
810 wxWebNavigationEvent
event(wxEVT_COMMAND_WEB_VIEW_TITLE_CHANGED
,
811 GetId(), GetCurrentURL(), wxEmptyString
, true);
812 event
.SetString(title
);
813 event
.SetEventObject(this);
814 HandleWindowEvent(event
);
818 case DISPID_NAVIGATEERROR
:
820 wxWebNavigationError errorType
= wxWEB_NAV_ERR_OTHER
;
821 wxString errorCode
= "?";
822 switch (evt
[3].GetLong())
824 case INET_E_INVALID_URL
: // (0x800C0002L or -2146697214)
825 errorCode
= "INET_E_INVALID_URL";
826 errorType
= wxWEB_NAV_ERR_REQUEST
;
828 case INET_E_NO_SESSION
: // (0x800C0003L or -2146697213)
829 errorCode
= "INET_E_NO_SESSION";
830 errorType
= wxWEB_NAV_ERR_CONNECTION
;
832 case INET_E_CANNOT_CONNECT
: // (0x800C0004L or -2146697212)
833 errorCode
= "INET_E_CANNOT_CONNECT";
834 errorType
= wxWEB_NAV_ERR_CONNECTION
;
836 case INET_E_RESOURCE_NOT_FOUND
: // (0x800C0005L or -2146697211)
837 errorCode
= "INET_E_RESOURCE_NOT_FOUND";
838 errorType
= wxWEB_NAV_ERR_NOT_FOUND
;
840 case INET_E_OBJECT_NOT_FOUND
: // (0x800C0006L or -2146697210)
841 errorCode
= "INET_E_OBJECT_NOT_FOUND";
842 errorType
= wxWEB_NAV_ERR_NOT_FOUND
;
844 case INET_E_DATA_NOT_AVAILABLE
: // (0x800C0007L or -2146697209)
845 errorCode
= "INET_E_DATA_NOT_AVAILABLE";
846 errorType
= wxWEB_NAV_ERR_NOT_FOUND
;
848 case INET_E_DOWNLOAD_FAILURE
: // (0x800C0008L or -2146697208)
849 errorCode
= "INET_E_DOWNLOAD_FAILURE";
850 errorType
= wxWEB_NAV_ERR_CONNECTION
;
852 case INET_E_AUTHENTICATION_REQUIRED
: // (0x800C0009L or -2146697207)
853 errorCode
= "INET_E_AUTHENTICATION_REQUIRED";
854 errorType
= wxWEB_NAV_ERR_AUTH
;
856 case INET_E_NO_VALID_MEDIA
: // (0x800C000AL or -2146697206)
857 errorCode
= "INET_E_NO_VALID_MEDIA";
858 errorType
= wxWEB_NAV_ERR_REQUEST
;
860 case INET_E_CONNECTION_TIMEOUT
: // (0x800C000BL or -2146697205)
861 errorCode
= "INET_E_CONNECTION_TIMEOUT";
862 errorType
= wxWEB_NAV_ERR_CONNECTION
;
864 case INET_E_INVALID_REQUEST
: // (0x800C000CL or -2146697204)
865 errorCode
= "INET_E_INVALID_REQUEST";
866 errorType
= wxWEB_NAV_ERR_REQUEST
;
868 case INET_E_UNKNOWN_PROTOCOL
: // (0x800C000DL or -2146697203)
869 errorCode
= "INET_E_UNKNOWN_PROTOCOL";
870 errorType
= wxWEB_NAV_ERR_REQUEST
;
872 case INET_E_SECURITY_PROBLEM
: // (0x800C000EL or -2146697202)
873 errorCode
= "INET_E_SECURITY_PROBLEM";
874 errorType
= wxWEB_NAV_ERR_SECURITY
;
876 case INET_E_CANNOT_LOAD_DATA
: // (0x800C000FL or -2146697201)
877 errorCode
= "INET_E_CANNOT_LOAD_DATA";
878 errorType
= wxWEB_NAV_ERR_OTHER
;
880 case INET_E_CANNOT_INSTANTIATE_OBJECT
:
881 // CoCreateInstance will return an error code if this happens,
882 // we'll handle this above.
885 case INET_E_REDIRECT_FAILED
: // (0x800C0014L or -2146697196)
886 errorCode
= "INET_E_REDIRECT_FAILED";
887 errorType
= wxWEB_NAV_ERR_OTHER
;
889 case INET_E_REDIRECT_TO_DIR
: // (0x800C0015L or -2146697195)
890 errorCode
= "INET_E_REDIRECT_TO_DIR";
891 errorType
= wxWEB_NAV_ERR_REQUEST
;
893 case INET_E_CANNOT_LOCK_REQUEST
: // (0x800C0016L or -2146697194)
894 errorCode
= "INET_E_CANNOT_LOCK_REQUEST";
895 errorType
= wxWEB_NAV_ERR_OTHER
;
897 case INET_E_USE_EXTEND_BINDING
: // (0x800C0017L or -2146697193)
898 errorCode
= "INET_E_USE_EXTEND_BINDING";
899 errorType
= wxWEB_NAV_ERR_OTHER
;
901 case INET_E_TERMINATED_BIND
: // (0x800C0018L or -2146697192)
902 errorCode
= "INET_E_TERMINATED_BIND";
903 errorType
= wxWEB_NAV_ERR_OTHER
;
905 case INET_E_INVALID_CERTIFICATE
: // (0x800C0019L or -2146697191)
906 errorCode
= "INET_E_INVALID_CERTIFICATE";
907 errorType
= wxWEB_NAV_ERR_CERTIFICATE
;
909 case INET_E_CODE_DOWNLOAD_DECLINED
: // (0x800C0100L or -2146696960)
910 errorCode
= "INET_E_CODE_DOWNLOAD_DECLINED";
911 errorType
= wxWEB_NAV_ERR_USER_CANCELLED
;
913 case INET_E_RESULT_DISPATCHED
: // (0x800C0200L or -2146696704)
914 // cancel request cancelled...
915 errorCode
= "INET_E_RESULT_DISPATCHED";
916 errorType
= wxWEB_NAV_ERR_OTHER
;
918 case INET_E_CANNOT_REPLACE_SFP_FILE
: // (0x800C0300L or -2146696448)
919 errorCode
= "INET_E_CANNOT_REPLACE_SFP_FILE";
920 errorType
= wxWEB_NAV_ERR_SECURITY
;
922 case INET_E_CODE_INSTALL_BLOCKED_BY_HASH_POLICY
:
923 errorCode
= "INET_E_CODE_INSTALL_BLOCKED_BY_HASH_POLICY";
924 errorType
= wxWEB_NAV_ERR_SECURITY
;
926 case INET_E_CODE_INSTALL_SUPPRESSED
:
927 errorCode
= "INET_E_CODE_INSTALL_SUPPRESSED";
928 errorType
= wxWEB_NAV_ERR_SECURITY
;
932 wxString url
= evt
[1].GetString();
933 wxString target
= evt
[2].GetString();
934 wxWebNavigationEvent
event(wxEVT_COMMAND_WEB_VIEW_ERROR
, GetId(),
936 event
.SetEventObject(this);
937 event
.SetInt(errorType
);
938 event
.SetString(errorCode
);
939 HandleWindowEvent(event
);
942 case DISPID_NEWWINDOW3
:
944 wxString url
= evt
[4].GetString();
946 wxWebNavigationEvent
event(wxEVT_COMMAND_WEB_VIEW_NEWWINDOW
,
947 GetId(), url
, wxEmptyString
, true);
948 event
.SetEventObject(this);
949 HandleWindowEvent(event
);
951 //We always cancel this event otherwise an Internet Exporer window
952 //is opened for the url
953 wxActiveXEventNativeMSW
* nativeParams
= evt
.GetNativeParameters();
954 *V_BOOLREF(&nativeParams
->pDispParams
->rgvarg
[3]) = VARIANT_TRUE
;
962 VirtualProtocol::VirtualProtocol(wxWebHandler
*handler
)
969 VirtualProtocol::~VirtualProtocol()
973 ULONG
VirtualProtocol::AddRef()
979 HRESULT
VirtualProtocol::QueryInterface(REFIID riid
, void **ppvObject
)
981 if(riid
== IID_IUnknown
|| riid
== IID_IInternetProtocolRoot
||
982 riid
== IID_IInternetProtocol
)
984 *ppvObject
= (IInternetProtocol
*)this;
988 else if(riid
== IID_IInternetProtocolInfo
)
990 *ppvObject
= (IInternetProtocolInfo
*)this;
1001 ULONG
VirtualProtocol::Release()
1015 HRESULT
VirtualProtocol::Start(LPCWSTR szUrl
, IInternetProtocolSink
*pOIProtSink
,
1016 IInternetBindInfo
*pOIBindInfo
, DWORD grfPI
,
1017 HANDLE_PTR dwReserved
)
1020 wxUnusedVar(pOIBindInfo
);
1022 wxUnusedVar(dwReserved
);
1023 m_protocolSink
= pOIProtSink
;
1025 //We get the file itself from the protocol handler
1026 m_file
= m_handler
->GetFile(szUrl
);
1030 return INET_E_RESOURCE_NOT_FOUND
;
1032 //We return the stream length for current and total size as we can always
1033 //read the whole file from the stream
1034 wxFileOffset length
= m_file
->GetStream()->GetLength();
1035 m_protocolSink
->ReportData(BSCF_FIRSTDATANOTIFICATION
|
1036 BSCF_DATAFULLYAVAILABLE
|
1037 BSCF_LASTDATANOTIFICATION
,
1042 HRESULT
VirtualProtocol::Read(void *pv
, ULONG cb
, ULONG
*pcbRead
)
1044 //If the file is null we return false to indicte it is finished
1048 wxStreamError err
= m_file
->GetStream()->Read(pv
, cb
).GetLastError();
1049 *pcbRead
= m_file
->GetStream()->LastRead();
1051 if(err
== wxSTREAM_NO_ERROR
)
1056 m_protocolSink
->ReportResult(S_OK
, 0, NULL
);
1058 //As we are not eof there is more data
1061 else if(err
== wxSTREAM_EOF
)
1064 m_protocolSink
->ReportResult(S_OK
, 0, NULL
);
1065 //We are eof and so finished
1068 else if(err
== wxSTREAM_READ_ERROR
)
1071 return INET_E_DOWNLOAD_FAILURE
;
1075 //Dummy return to surpress a compiler warning
1077 return INET_E_DOWNLOAD_FAILURE
;
1081 HRESULT
VirtualProtocol::CombineUrl(LPCWSTR pwzBaseUrl
, LPCWSTR pwzRelativeUrl
,
1082 DWORD
WXUNUSED(dwCombineFlags
),
1084 DWORD cchResult
, DWORD
*pcchResult
,
1085 DWORD
WXUNUSED(dwReserved
))
1088 wxString newuri
= m_handler
->CombineURIs(pwzBaseUrl
, pwzRelativeUrl
);
1089 //Check the buffer we are given can hold the new url
1090 if(wxStrlen(newuri
) > cchResult
)
1093 wxStrcpy(pwzResult
, newuri
.c_str());
1094 *pcchResult
= wxStrlen(newuri
);
1098 HRESULT
VirtualProtocol::ParseUrl(LPCWSTR pwzUrl
,
1099 PARSEACTION
WXUNUSED(ParseAction
),
1100 DWORD
WXUNUSED(dwParseFlags
),
1102 DWORD cchResult
, DWORD
*pcchResult
,
1103 DWORD
WXUNUSED(dwReserved
))
1105 //Check the buffer we are given can hold the new url
1106 if(wxStrlen(pwzUrl
) > cchResult
)
1109 wxStrcpy(pwzResult
, pwzUrl
);
1110 *pcchResult
= wxStrlen(pwzResult
);
1114 HRESULT
VirtualProtocol::QueryInfo(LPCWSTR
WXUNUSED(pwzUrl
),
1115 QUERYOPTION
WXUNUSED(OueryOption
),
1116 DWORD
WXUNUSED(dwQueryFlags
),
1117 LPVOID
WXUNUSED(pBuffer
),
1118 DWORD
WXUNUSED(cbBuffer
),
1119 DWORD
* WXUNUSED(pcbBuf
),
1120 DWORD
WXUNUSED(dwReserved
))
1122 return INET_E_DEFAULT_ACTION
;
1125 HRESULT
ClassFactory::CreateInstance(IUnknown
* pUnkOuter
, REFIID riid
,
1129 return CLASS_E_NOAGGREGATION
;
1130 VirtualProtocol
* vp
= new VirtualProtocol(m_handler
);
1132 HRESULT hr
= vp
->QueryInterface(riid
, ppvObject
);
1138 STDMETHODIMP
ClassFactory::LockServer(BOOL fLock
)
1144 ULONG
ClassFactory::AddRef(void)
1150 HRESULT
ClassFactory::QueryInterface(REFIID riid
, void **ppvObject
)
1152 if ((riid
== IID_IUnknown
) || (riid
== IID_IClassFactory
))
1166 ULONG
ClassFactory::Release(void)