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"
30 //We link to urlmon as it is required for CoInternetGetSession
31 #pragma comment(lib, "urlmon")
33 BEGIN_EVENT_TABLE(wxWebViewIE
, wxControl
)
34 EVT_ACTIVEX(wxID_ANY
, wxWebViewIE::onActiveXEvent
)
35 EVT_ERASE_BACKGROUND(wxWebViewIE::onEraseBg
)
38 bool wxWebViewIE::Create(wxWindow
* parent
,
46 if (!wxControl::Create(parent
, id
, pos
, size
, style
,
47 wxDefaultValidator
, name
))
54 m_historyLoadingFromList
= false;
55 m_historyEnabled
= true;
56 m_historyPosition
= -1;
57 m_zoomType
= wxWEB_VIEW_ZOOM_TYPE_TEXT
;
59 if (::CoCreateInstance(CLSID_WebBrowser
, NULL
,
60 CLSCTX_INPROC_SERVER
, // CLSCTX_INPROC,
61 IID_IWebBrowser2
, (void**)&m_webBrowser
) != 0)
63 wxLogError("Failed to initialize IE, CoCreateInstance returned an error");
67 m_ie
.SetDispatchPtr(m_webBrowser
); // wxAutomationObject will release itself
69 m_webBrowser
->put_RegisterAsBrowser(VARIANT_TRUE
);
70 m_webBrowser
->put_RegisterAsDropTarget(VARIANT_TRUE
);
72 m_container
= new wxActiveXContainer(this, IID_IWebBrowser2
, m_webBrowser
);
74 SetBackgroundStyle(wxBG_STYLE_PAINT
);
75 SetDoubleBuffered(true);
81 void wxWebViewIE::LoadUrl(const wxString
& url
)
83 m_ie
.CallMethod("Navigate", (BSTR
) url
.wc_str(), NULL
, NULL
, NULL
, NULL
);
86 void wxWebViewIE::SetPage(const wxString
& html
, const wxString
& baseUrl
)
88 BSTR bstr
= SysAllocString(html
.wc_str());
90 // Creates a new one-dimensional array
91 SAFEARRAY
*psaStrings
= SafeArrayCreateVector(VT_VARIANT
, 0, 1);
92 if (psaStrings
!= NULL
)
96 HRESULT hr
= SafeArrayAccessData(psaStrings
, (LPVOID
*)¶m
);
98 param
->bstrVal
= bstr
;
99 hr
= SafeArrayUnaccessData(psaStrings
);
101 IHTMLDocument2
* document
= GetDocument();
102 document
->write(psaStrings
);
105 // SafeArrayDestroy calls SysFreeString for each BSTR
106 SafeArrayDestroy(psaStrings
);
108 //We send the events when we are done to mimic webkit
110 wxWebNavigationEvent
event(wxEVT_COMMAND_WEB_VIEW_NAVIGATED
,
111 GetId(), baseUrl
, "", false);
112 event
.SetEventObject(this);
113 HandleWindowEvent(event
);
115 //Document complete event
116 event
.SetEventType(wxEVT_COMMAND_WEB_VIEW_LOADED
);
117 event
.SetEventObject(this);
118 HandleWindowEvent(event
);
122 wxLogError("wxWebViewIE::SetPage() : psaStrings is NULL");
127 wxString
wxWebViewIE::GetPageSource()
129 IHTMLDocument2
* document
= GetDocument();
130 IHTMLElement
*bodyTag
= NULL
;
131 IHTMLElement
*htmlTag
= NULL
;
133 HRESULT hr
= document
->get_body(&bodyTag
);
136 hr
= bodyTag
->get_parentElement(&htmlTag
);
140 htmlTag
->get_outerHTML(&bstr
);
141 source
= wxString(bstr
);
151 wxWebViewZoom
wxWebViewIE::GetZoom()
153 if(m_zoomType
== wxWEB_VIEW_ZOOM_TYPE_LAYOUT
)
154 return GetIEOpticalZoom();
155 else if(m_zoomType
== wxWEB_VIEW_ZOOM_TYPE_TEXT
)
156 return GetIETextZoom();
160 //Dummy return to stop compiler warnings
161 return wxWEB_VIEW_ZOOM_MEDIUM
;
165 void wxWebViewIE::SetZoom(wxWebViewZoom zoom
)
167 if(m_zoomType
== wxWEB_VIEW_ZOOM_TYPE_LAYOUT
)
168 SetIEOpticalZoom(zoom
);
169 else if(m_zoomType
== wxWEB_VIEW_ZOOM_TYPE_TEXT
)
175 void wxWebViewIE::SetIETextZoom(wxWebViewZoom level
)
177 //We do not use OLECMDID_OPTICAL_GETZOOMRANGE as the docs say the range
178 //is 0 to 4 so the check is unnecessary, these match exactly with the
181 VariantInit (&zoomVariant
);
182 V_VT(&zoomVariant
) = VT_I4
;
183 V_I4(&zoomVariant
) = level
;
185 HRESULT result
= m_webBrowser
->ExecWB(OLECMDID_ZOOM
,
186 OLECMDEXECOPT_DONTPROMPTUSER
,
188 wxASSERT(result
== S_OK
);
191 wxWebViewZoom
wxWebViewIE::GetIETextZoom()
194 VariantInit (&zoomVariant
);
195 V_VT(&zoomVariant
) = VT_I4
;
197 HRESULT result
= m_webBrowser
->ExecWB(OLECMDID_ZOOM
,
198 OLECMDEXECOPT_DONTPROMPTUSER
,
200 wxASSERT(result
== S_OK
);
202 //We can safely cast here as we know that the range matches our enum
203 return static_cast<wxWebViewZoom
>(V_I4(&zoomVariant
));
206 void wxWebViewIE::SetIEOpticalZoom(wxWebViewZoom level
)
208 //We do not use OLECMDID_OPTICAL_GETZOOMRANGE as the docs say the range
209 //is 10 to 1000 so the check is unnecessary
211 VariantInit (&zoomVariant
);
212 V_VT(&zoomVariant
) = VT_I4
;
214 //We make a somewhat arbitray map here, taken from values used by webkit
217 case wxWEB_VIEW_ZOOM_TINY
:
218 V_I4(&zoomVariant
) = 60;
220 case wxWEB_VIEW_ZOOM_SMALL
:
221 V_I4(&zoomVariant
) = 80;
223 case wxWEB_VIEW_ZOOM_MEDIUM
:
224 V_I4(&zoomVariant
) = 100;
226 case wxWEB_VIEW_ZOOM_LARGE
:
227 V_I4(&zoomVariant
) = 130;
229 case wxWEB_VIEW_ZOOM_LARGEST
:
230 V_I4(&zoomVariant
) = 160;
236 HRESULT result
= m_webBrowser
->ExecWB((OLECMDID
)OLECMDID_OPTICAL_ZOOM
,
237 OLECMDEXECOPT_DODEFAULT
,
240 wxASSERT(result
== S_OK
);
243 wxWebViewZoom
wxWebViewIE::GetIEOpticalZoom()
246 VariantInit (&zoomVariant
);
247 V_VT(&zoomVariant
) = VT_I4
;
249 HRESULT result
= m_webBrowser
->ExecWB((OLECMDID
)OLECMDID_OPTICAL_ZOOM
,
250 OLECMDEXECOPT_DODEFAULT
, NULL
,
252 wxASSERT(result
== S_OK
);
254 const int zoom
= V_I4(&zoomVariant
);
256 //We make a somewhat arbitray map here, taken from values used by webkit
259 return wxWEB_VIEW_ZOOM_TINY
;
261 else if (zoom
> 65 && zoom
<= 90)
263 return wxWEB_VIEW_ZOOM_SMALL
;
265 else if (zoom
> 90 && zoom
<= 115)
267 return wxWEB_VIEW_ZOOM_MEDIUM
;
269 else if (zoom
> 115 && zoom
<= 145)
271 return wxWEB_VIEW_ZOOM_LARGE
;
273 else /*if (zoom > 145) */ //Using else removes a compiler warning
275 return wxWEB_VIEW_ZOOM_LARGEST
;
279 void wxWebViewIE::SetZoomType(wxWebViewZoomType type
)
284 wxWebViewZoomType
wxWebViewIE::GetZoomType() const
289 bool wxWebViewIE::CanSetZoomType(wxWebViewZoomType type
) const
291 //IE 6 and below only support text zoom, so check the registry to see what
292 //version we actually have
293 wxRegKey
key(wxRegKey::HKLM
, "Software\\Microsoft\\Internet Explorer");
295 key
.QueryValue("Version", value
);
297 long version
= wxAtoi(value
.Left(1));
298 if(version
<= 6 && type
== wxWEB_VIEW_ZOOM_TYPE_LAYOUT
)
304 void wxWebViewIE::Print()
306 m_webBrowser
->ExecWB(OLECMDID_PRINTPREVIEW
,
307 OLECMDEXECOPT_DODEFAULT
, NULL
, NULL
);
310 bool wxWebViewIE::CanGoBack()
313 return m_historyPosition
> 0;
318 bool wxWebViewIE::CanGoForward()
321 return m_historyPosition
!= static_cast<int>(m_historyList
.size()) - 1;
326 void wxWebViewIE::LoadHistoryItem(wxSharedPtr
<wxWebHistoryItem
> item
)
329 for(unsigned int i
= 0; i
< m_historyList
.size(); i
++)
331 //We compare the actual pointers to find the correct item
332 if(m_historyList
[i
].get() == item
.get())
335 wxASSERT_MSG(pos
!= static_cast<int>(m_historyList
.size()),
336 "invalid history item");
337 m_historyLoadingFromList
= true;
338 LoadUrl(item
->GetUrl());
339 m_historyPosition
= pos
;
342 wxVector
<wxSharedPtr
<wxWebHistoryItem
> > wxWebViewIE::GetBackwardHistory()
344 wxVector
<wxSharedPtr
<wxWebHistoryItem
> > backhist
;
345 //As we don't have std::copy or an iterator constructor in the wxwidgets
346 //native vector we construct it by hand
347 for(int i
= 0; i
< m_historyPosition
; i
++)
349 backhist
.push_back(m_historyList
[i
]);
354 wxVector
<wxSharedPtr
<wxWebHistoryItem
> > wxWebViewIE::GetForwardHistory()
356 wxVector
<wxSharedPtr
<wxWebHistoryItem
> > forwardhist
;
357 //As we don't have std::copy or an iterator constructor in the wxwidgets
358 //native vector we construct it by hand
359 for(int i
= m_historyPosition
+ 1; i
< static_cast<int>(m_historyList
.size()); i
++)
361 forwardhist
.push_back(m_historyList
[i
]);
366 void wxWebViewIE::GoBack()
368 LoadHistoryItem(m_historyList
[m_historyPosition
- 1]);
371 void wxWebViewIE::GoForward()
373 LoadHistoryItem(m_historyList
[m_historyPosition
+ 1]);
376 void wxWebViewIE::Stop()
378 m_ie
.CallMethod("Stop");
381 void wxWebViewIE::ClearHistory()
383 m_historyList
.clear();
384 m_historyPosition
= -1;
387 void wxWebViewIE::EnableHistory(bool enable
)
389 m_historyEnabled
= enable
;
390 m_historyList
.clear();
391 m_historyPosition
= -1;
394 void wxWebViewIE::Reload(wxWebViewReloadFlags flags
)
398 V_VT(&level
) = VT_I2
;
402 case wxWEB_VIEW_RELOAD_DEFAULT
:
403 V_I2(&level
) = REFRESH_NORMAL
;
405 case wxWEB_VIEW_RELOAD_NO_CACHE
:
406 V_I2(&level
) = REFRESH_COMPLETELY
;
409 wxFAIL_MSG("Unexpected reload type");
412 m_webBrowser
->Refresh2(&level
);
415 bool wxWebViewIE::IsOfflineMode()
417 wxVariant out
= m_ie
.GetProperty("Offline");
419 wxASSERT(out
.GetType() == "bool");
421 return out
.GetBool();
424 void wxWebViewIE::SetOfflineMode(bool offline
)
426 // FIXME: the wxWidgets docs do not really document what the return
427 // parameter of PutProperty is
428 const bool success
= m_ie
.PutProperty("Offline", (offline
?
434 bool wxWebViewIE::IsBusy()
436 if (m_isBusy
) return true;
438 wxVariant out
= m_ie
.GetProperty("Busy");
440 wxASSERT(out
.GetType() == "bool");
442 return out
.GetBool();
445 wxString
wxWebViewIE::GetCurrentURL()
447 wxVariant out
= m_ie
.GetProperty("LocationURL");
449 wxASSERT(out
.GetType() == "string");
450 return out
.GetString();
453 wxString
wxWebViewIE::GetCurrentTitle()
455 IHTMLDocument2
* document
= GetDocument();
458 document
->get_nameProp(&title
);
460 return wxString(title
);
463 bool wxWebViewIE::CanCut()
465 return CanExecCommand("Cut");
468 bool wxWebViewIE::CanCopy()
470 return CanExecCommand("Copy");
472 bool wxWebViewIE::CanPaste()
474 return CanExecCommand("Paste");
477 void wxWebViewIE::Cut()
482 void wxWebViewIE::Copy()
487 void wxWebViewIE::Paste()
489 ExecCommand("Paste");
492 bool wxWebViewIE::CanUndo()
494 return CanExecCommand("Undo");
496 bool wxWebViewIE::CanRedo()
498 return CanExecCommand("Redo");
501 void wxWebViewIE::Undo()
506 void wxWebViewIE::Redo()
511 void wxWebViewIE::SetEditable(bool enable
)
513 IHTMLDocument2
* document
= GetDocument();
515 document
->put_designMode(SysAllocString(L
"On"));
517 document
->put_designMode(SysAllocString(L
"Off"));
522 bool wxWebViewIE::IsEditable()
524 IHTMLDocument2
* document
= GetDocument();
526 document
->get_designMode(&mode
);
528 if(wxString(mode
) == "On")
534 void wxWebViewIE::SelectAll()
536 ExecCommand("SelectAll");
539 bool wxWebViewIE::HasSelection()
541 IHTMLDocument2
* document
= GetDocument();
542 IHTMLSelectionObject
* selection
;
544 HRESULT hr
= document
->get_selection(&selection
);
548 selection
->get_type(&type
);
549 sel
= wxString(type
);
550 selection
->Release();
553 return sel
!= "None";
556 void wxWebViewIE::DeleteSelection()
558 ExecCommand("Delete");
561 wxString
wxWebViewIE::GetSelectedText()
563 IHTMLDocument2
* document
= GetDocument();
564 IHTMLSelectionObject
* selection
;
566 HRESULT hr
= document
->get_selection(&selection
);
570 hr
= selection
->createRange(&disrange
);
573 IHTMLTxtRange
* range
;
574 hr
= disrange
->QueryInterface(IID_IHTMLTxtRange
, (void**)&range
);
578 range
->get_text(&text
);
579 selected
= wxString(text
);
584 selection
->Release();
590 wxString
wxWebViewIE::GetSelectedSource()
592 IHTMLDocument2
* document
= GetDocument();
593 IHTMLSelectionObject
* selection
;
595 HRESULT hr
= document
->get_selection(&selection
);
599 hr
= selection
->createRange(&disrange
);
602 IHTMLTxtRange
* range
;
603 hr
= disrange
->QueryInterface(IID_IHTMLTxtRange
, (void**)&range
);
607 range
->get_htmlText(&text
);
608 selected
= wxString(text
);
613 selection
->Release();
619 void wxWebViewIE::ClearSelection()
621 IHTMLDocument2
* document
= GetDocument();
622 IHTMLSelectionObject
* selection
;
624 HRESULT hr
= document
->get_selection(&selection
);
628 selection
->Release();
633 wxString
wxWebViewIE::GetPageText()
635 IHTMLDocument2
* document
= GetDocument();
638 HRESULT hr
= document
->get_body(&body
);
642 body
->get_innerText(&out
);
643 text
= wxString(out
);
650 void wxWebViewIE::RunScript(const wxString
& javascript
)
652 IHTMLDocument2
* document
= GetDocument();
653 IHTMLWindow2
* window
;
654 wxString language
= "javascript";
655 HRESULT hr
= document
->get_parentWindow(&window
);
660 V_VT(&level
) = VT_EMPTY
;
661 window
->execScript(SysAllocString(javascript
), SysAllocString(language
), &level
);
666 void wxWebViewIE::RegisterProtocol(wxWebProtocolHandler
* handler
)
668 ClassFactory
* cf
= new ClassFactory(handler
);
669 IInternetSession
* session
;
670 if(FAILED(CoInternetGetSession(0, &session
, 0)))
672 wxFAIL_MSG("Could not retrive internet session");
675 HRESULT hr
= session
->RegisterNameSpace(cf
, CLSID_FileProtocol
, handler
->GetProtocol(), 0, NULL
, 0);
678 wxFAIL_MSG("Could not register protocol");
682 bool wxWebViewIE::CanExecCommand(wxString command
)
684 IHTMLDocument2
* document
= GetDocument();
685 VARIANT_BOOL enabled
;
687 document
->queryCommandEnabled(SysAllocString(command
.wc_str()), &enabled
);
690 return (enabled
== VARIANT_TRUE
);
693 void wxWebViewIE::ExecCommand(wxString command
)
695 IHTMLDocument2
* document
= GetDocument();
696 document
->execCommand(SysAllocString(command
.wc_str()), VARIANT_FALSE
, VARIANT(), NULL
);
700 IHTMLDocument2
* wxWebViewIE::GetDocument()
702 wxVariant variant
= m_ie
.GetProperty("Document");
703 IHTMLDocument2
* document
= (IHTMLDocument2
*)variant
.GetVoidPtr();
710 void wxWebViewIE::onActiveXEvent(wxActiveXEvent
& evt
)
712 if (m_webBrowser
== NULL
) return;
714 switch (evt
.GetDispatchId())
716 case DISPID_BEFORENAVIGATE2
:
720 wxString url
= evt
[1].GetString();
721 wxString target
= evt
[3].GetString();
723 wxWebNavigationEvent
event(wxEVT_COMMAND_WEB_VIEW_NAVIGATING
,
724 GetId(), url
, target
, true);
725 event
.SetEventObject(this);
726 HandleWindowEvent(event
);
728 if (event
.IsVetoed())
730 wxActiveXEventNativeMSW
* nativeParams
=
731 evt
.GetNativeParameters();
732 *V_BOOLREF(&nativeParams
->pDispParams
->rgvarg
[0]) = VARIANT_TRUE
;
735 // at this point, either the navigation event has been cancelled
736 // and we're not busy, either it was accepted and IWebBrowser2's
737 // Busy property will be true; so we don't need our override
744 case DISPID_NAVIGATECOMPLETE2
:
746 wxString url
= evt
[1].GetString();
747 // TODO: set target parameter if possible
748 wxString target
= wxEmptyString
;
749 wxWebNavigationEvent
event(wxEVT_COMMAND_WEB_VIEW_NAVIGATED
,
750 GetId(), url
, target
, false);
751 event
.SetEventObject(this);
752 HandleWindowEvent(event
);
756 case DISPID_PROGRESSCHANGE
:
762 case DISPID_DOCUMENTCOMPLETE
:
764 //Only send a complete even if we are actually finished, this brings
765 //the event in to line with webkit
767 m_webBrowser
->get_ReadyState( &rs
);
768 if(rs
!= READYSTATE_COMPLETE
)
771 wxString url
= evt
[1].GetString();
773 //As we are complete we also add to the history list, but not if the
774 //page is not the main page, ie it is a subframe
775 if(m_historyEnabled
&& !m_historyLoadingFromList
&& url
== GetCurrentURL())
777 //If we are not at the end of the list, then erase everything
778 //between us and the end before adding the new page
779 if(m_historyPosition
!= static_cast<int>(m_historyList
.size()) - 1)
781 m_historyList
.erase(m_historyList
.begin() + m_historyPosition
+ 1,
782 m_historyList
.end());
784 wxSharedPtr
<wxWebHistoryItem
> item(new wxWebHistoryItem(url
, GetCurrentTitle()));
785 m_historyList
.push_back(item
);
788 //Reset as we are done now
789 m_historyLoadingFromList
= false;
790 // TODO: set target parameter if possible
791 wxString target
= wxEmptyString
;
792 wxWebNavigationEvent
event(wxEVT_COMMAND_WEB_VIEW_LOADED
, GetId(),
794 event
.SetEventObject(this);
795 HandleWindowEvent(event
);
799 case DISPID_STATUSTEXTCHANGE
:
804 case DISPID_TITLECHANGE
:
809 case DISPID_NAVIGATEERROR
:
811 wxWebNavigationError errorType
= wxWEB_NAV_ERR_OTHER
;
812 wxString errorCode
= "?";
813 switch (evt
[3].GetLong())
815 case INET_E_INVALID_URL
: // (0x800C0002L or -2146697214)
816 errorCode
= "INET_E_INVALID_URL";
817 errorType
= wxWEB_NAV_ERR_REQUEST
;
819 case INET_E_NO_SESSION
: // (0x800C0003L or -2146697213)
820 errorCode
= "INET_E_NO_SESSION";
821 errorType
= wxWEB_NAV_ERR_CONNECTION
;
823 case INET_E_CANNOT_CONNECT
: // (0x800C0004L or -2146697212)
824 errorCode
= "INET_E_CANNOT_CONNECT";
825 errorType
= wxWEB_NAV_ERR_CONNECTION
;
827 case INET_E_RESOURCE_NOT_FOUND
: // (0x800C0005L or -2146697211)
828 errorCode
= "INET_E_RESOURCE_NOT_FOUND";
829 errorType
= wxWEB_NAV_ERR_NOT_FOUND
;
831 case INET_E_OBJECT_NOT_FOUND
: // (0x800C0006L or -2146697210)
832 errorCode
= "INET_E_OBJECT_NOT_FOUND";
833 errorType
= wxWEB_NAV_ERR_NOT_FOUND
;
835 case INET_E_DATA_NOT_AVAILABLE
: // (0x800C0007L or -2146697209)
836 errorCode
= "INET_E_DATA_NOT_AVAILABLE";
837 errorType
= wxWEB_NAV_ERR_NOT_FOUND
;
839 case INET_E_DOWNLOAD_FAILURE
: // (0x800C0008L or -2146697208)
840 errorCode
= "INET_E_DOWNLOAD_FAILURE";
841 errorType
= wxWEB_NAV_ERR_CONNECTION
;
843 case INET_E_AUTHENTICATION_REQUIRED
: // (0x800C0009L or -2146697207)
844 errorCode
= "INET_E_AUTHENTICATION_REQUIRED";
845 errorType
= wxWEB_NAV_ERR_AUTH
;
847 case INET_E_NO_VALID_MEDIA
: // (0x800C000AL or -2146697206)
848 errorCode
= "INET_E_NO_VALID_MEDIA";
849 errorType
= wxWEB_NAV_ERR_REQUEST
;
851 case INET_E_CONNECTION_TIMEOUT
: // (0x800C000BL or -2146697205)
852 errorCode
= "INET_E_CONNECTION_TIMEOUT";
853 errorType
= wxWEB_NAV_ERR_CONNECTION
;
855 case INET_E_INVALID_REQUEST
: // (0x800C000CL or -2146697204)
856 errorCode
= "INET_E_INVALID_REQUEST";
857 errorType
= wxWEB_NAV_ERR_REQUEST
;
859 case INET_E_UNKNOWN_PROTOCOL
: // (0x800C000DL or -2146697203)
860 errorCode
= "INET_E_UNKNOWN_PROTOCOL";
861 errorType
= wxWEB_NAV_ERR_REQUEST
;
863 case INET_E_SECURITY_PROBLEM
: // (0x800C000EL or -2146697202)
864 errorCode
= "INET_E_SECURITY_PROBLEM";
865 errorType
= wxWEB_NAV_ERR_SECURITY
;
867 case INET_E_CANNOT_LOAD_DATA
: // (0x800C000FL or -2146697201)
868 errorCode
= "INET_E_CANNOT_LOAD_DATA";
869 errorType
= wxWEB_NAV_ERR_OTHER
;
871 case INET_E_CANNOT_INSTANTIATE_OBJECT
:
872 // CoCreateInstance will return an error code if this happens,
873 // we'll handle this above.
876 case INET_E_REDIRECT_FAILED
: // (0x800C0014L or -2146697196)
877 errorCode
= "INET_E_REDIRECT_FAILED";
878 errorType
= wxWEB_NAV_ERR_OTHER
;
880 case INET_E_REDIRECT_TO_DIR
: // (0x800C0015L or -2146697195)
881 errorCode
= "INET_E_REDIRECT_TO_DIR";
882 errorType
= wxWEB_NAV_ERR_REQUEST
;
884 case INET_E_CANNOT_LOCK_REQUEST
: // (0x800C0016L or -2146697194)
885 errorCode
= "INET_E_CANNOT_LOCK_REQUEST";
886 errorType
= wxWEB_NAV_ERR_OTHER
;
888 case INET_E_USE_EXTEND_BINDING
: // (0x800C0017L or -2146697193)
889 errorCode
= "INET_E_USE_EXTEND_BINDING";
890 errorType
= wxWEB_NAV_ERR_OTHER
;
892 case INET_E_TERMINATED_BIND
: // (0x800C0018L or -2146697192)
893 errorCode
= "INET_E_TERMINATED_BIND";
894 errorType
= wxWEB_NAV_ERR_OTHER
;
896 case INET_E_INVALID_CERTIFICATE
: // (0x800C0019L or -2146697191)
897 errorCode
= "INET_E_INVALID_CERTIFICATE";
898 errorType
= wxWEB_NAV_ERR_CERTIFICATE
;
900 case INET_E_CODE_DOWNLOAD_DECLINED
: // (0x800C0100L or -2146696960)
901 errorCode
= "INET_E_CODE_DOWNLOAD_DECLINED";
902 errorType
= wxWEB_NAV_ERR_USER_CANCELLED
;
904 case INET_E_RESULT_DISPATCHED
: // (0x800C0200L or -2146696704)
905 // cancel request cancelled...
906 errorCode
= "INET_E_RESULT_DISPATCHED";
907 errorType
= wxWEB_NAV_ERR_OTHER
;
909 case INET_E_CANNOT_REPLACE_SFP_FILE
: // (0x800C0300L or -2146696448)
910 errorCode
= "INET_E_CANNOT_REPLACE_SFP_FILE";
911 errorType
= wxWEB_NAV_ERR_SECURITY
;
913 case INET_E_CODE_INSTALL_BLOCKED_BY_HASH_POLICY
:
914 errorCode
= "INET_E_CODE_INSTALL_BLOCKED_BY_HASH_POLICY";
915 errorType
= wxWEB_NAV_ERR_SECURITY
;
917 case INET_E_CODE_INSTALL_SUPPRESSED
:
918 errorCode
= "INET_E_CODE_INSTALL_SUPPRESSED";
919 errorType
= wxWEB_NAV_ERR_SECURITY
;
923 wxString url
= evt
[1].GetString();
924 wxString target
= evt
[2].GetString();
925 wxWebNavigationEvent
event(wxEVT_COMMAND_WEB_VIEW_ERROR
, GetId(),
927 event
.SetEventObject(this);
928 event
.SetInt(errorType
);
929 event
.SetString(errorCode
);
930 HandleWindowEvent(event
);
933 case DISPID_NEWWINDOW3
:
935 wxString url
= evt
[4].GetString();
937 wxWebNavigationEvent
event(wxEVT_COMMAND_WEB_VIEW_NEWWINDOW
,
938 GetId(), url
, wxEmptyString
, true);
939 event
.SetEventObject(this);
940 HandleWindowEvent(event
);
942 //We always cancel this event otherwise an Internet Exporer window
943 //is opened for the url
944 wxActiveXEventNativeMSW
* nativeParams
= evt
.GetNativeParameters();
945 *V_BOOLREF(&nativeParams
->pDispParams
->rgvarg
[3]) = VARIANT_TRUE
;
953 VirtualProtocol::VirtualProtocol(wxWebProtocolHandler
*handler
)
960 VirtualProtocol::~VirtualProtocol()
964 ULONG
VirtualProtocol::AddRef()
970 HRESULT
VirtualProtocol::QueryInterface(REFIID riid
, void **ppvObject
)
972 if(riid
== IID_IUnknown
|| riid
== IID_IInternetProtocolRoot
||
973 riid
== IID_IInternetProtocol
)
975 *ppvObject
= (IInternetProtocol
*)this;
979 else if(riid
== IID_IInternetProtocolInfo
)
981 *ppvObject
= (IInternetProtocolInfo
*)this;
992 ULONG
VirtualProtocol::Release()
1006 HRESULT
VirtualProtocol::Start(LPCWSTR szUrl
, IInternetProtocolSink
*pOIProtSink
,
1007 IInternetBindInfo
*pOIBindInfo
, DWORD grfPI
,
1008 HANDLE_PTR dwReserved
)
1011 wxUnusedVar(pOIBindInfo
);
1013 wxUnusedVar(dwReserved
);
1014 m_protocolSink
= pOIProtSink
;
1016 //We get the file itself from the protocol handler
1017 m_file
= m_handler
->GetFile(szUrl
);
1021 return INET_E_RESOURCE_NOT_FOUND
;
1023 //We return the stream length for current and total size as we can always
1024 //read the whole file from the stream
1025 wxFileOffset length
= m_file
->GetStream()->GetLength();
1026 m_protocolSink
->ReportData(BSCF_FIRSTDATANOTIFICATION
|
1027 BSCF_DATAFULLYAVAILABLE
|
1028 BSCF_LASTDATANOTIFICATION
,
1033 HRESULT
VirtualProtocol::Read(void *pv
, ULONG cb
, ULONG
*pcbRead
)
1035 //If the file is null we return false to indicte it is finished
1039 wxStreamError err
= m_file
->GetStream()->Read(pv
, cb
).GetLastError();
1040 *pcbRead
= m_file
->GetStream()->LastRead();
1042 if(err
== wxSTREAM_NO_ERROR
)
1047 m_protocolSink
->ReportResult(S_OK
, 0, NULL
);
1049 //As we are not eof there is more data
1052 else if(err
== wxSTREAM_EOF
)
1055 m_protocolSink
->ReportResult(S_OK
, 0, NULL
);
1056 //We are eof and so finished
1059 else if(err
== wxSTREAM_READ_ERROR
)
1062 return INET_E_DOWNLOAD_FAILURE
;
1066 //Dummy return to surpress a compiler warning
1068 return INET_E_DOWNLOAD_FAILURE
;
1072 HRESULT
VirtualProtocol::CombineUrl(LPCWSTR pwzBaseUrl
, LPCWSTR pwzRelativeUrl
,
1073 DWORD dwCombineFlags
, LPWSTR pwzResult
,
1074 DWORD cchResult
, DWORD
*pcchResult
,
1077 wxString newuri
= m_handler
->CombineURIs(pwzBaseUrl
, pwzRelativeUrl
);
1078 //Check the buffer we are given can hold the new urll
1079 if(wxStrlen(newuri
) > cchResult
)
1082 wxStrcpy(pwzResult
, newuri
.c_str());
1083 *pcchResult
= wxStrlen(newuri
);
1087 HRESULT
VirtualProtocol::ParseUrl(LPCWSTR pwzUrl
, PARSEACTION ParseAction
,
1088 DWORD dwParseFlags
, LPWSTR pwzResult
,
1089 DWORD cchResult
, DWORD
*pcchResult
,
1092 //return INET_E_DEFAULT_ACTION;
1093 wxStrcpy(pwzResult
, pwzUrl
);
1094 *pcchResult
= wxStrlen(pwzResult
);
1098 HRESULT
VirtualProtocol::QueryInfo(LPCWSTR pwzUrl
, QUERYOPTION OueryOption
,
1099 DWORD dwQueryFlags
, LPVOID pBuffer
,
1100 DWORD cbBuffer
, DWORD
*pcbBuf
,
1103 return INET_E_DEFAULT_ACTION
;
1106 HRESULT
ClassFactory::CreateInstance(IUnknown
* pUnkOuter
, REFIID riid
,
1110 return CLASS_E_NOAGGREGATION
;
1111 VirtualProtocol
* vp
= new VirtualProtocol(m_handler
);
1113 HRESULT hr
= vp
->QueryInterface(riid
, ppvObject
);
1119 STDMETHODIMP
ClassFactory::LockServer(BOOL fLock
)
1125 ULONG
ClassFactory::AddRef(void)
1131 HRESULT
ClassFactory::QueryInterface(REFIID riid
, void **ppvObject
)
1133 if ((riid
== IID_IUnknown
) || (riid
== IID_IClassFactory
))
1147 ULONG
ClassFactory::Release(void)