Use wxCOMPtr throughout wxWebViewIE to simplify the code and reduce the chance of memory leaks. Also mark PPV_ARGS_CHECK as inline so it can be used from multiple libraries.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@71639 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Steve Lamerton
2012-06-03 17:41:32 +00:00
parent 41087dc90a
commit f40f8e1722
3 changed files with 34 additions and 67 deletions

View File

@@ -113,7 +113,7 @@ private:
// Define a helper for the macro below: we just need a function taking a // Define a helper for the macro below: we just need a function taking a
// pointer and not returning anything to avoid warnings about unused return // pointer and not returning anything to avoid warnings about unused return
// value of the cast in the macro itself. // value of the cast in the macro itself.
namespace wxPrivate { void PPV_ARGS_CHECK(void*) { } } namespace wxPrivate { inline void PPV_ARGS_CHECK(void*) { } }
// Takes the interface name and a pointer to a pointer of the interface type // Takes the interface name and a pointer to a pointer of the interface type
// and expands into the IID of this interface and the same pointer but after a // and expands into the IID of this interface and the same pointer but after a

View File

@@ -19,6 +19,7 @@
#include "wx/msw/ole/automtn.h" #include "wx/msw/ole/automtn.h"
#include "wx/msw/ole/activex.h" #include "wx/msw/ole/activex.h"
#include "wx/msw/ole/oleutils.h" #include "wx/msw/ole/oleutils.h"
#include "wx/msw/private/comptr.h"
#include "wx/msw/wrapwin.h" #include "wx/msw/wrapwin.h"
#include "wx/msw/missing.h" #include "wx/msw/missing.h"
#include "wx/sharedptr.h" #include "wx/sharedptr.h"
@@ -360,7 +361,7 @@ private:
wxAutomationObject m_ie; wxAutomationObject m_ie;
IWebBrowser2* m_webBrowser; IWebBrowser2* m_webBrowser;
DWORD m_dwCookie; DWORD m_dwCookie;
DocHostUIHandler* m_uiHandler; wxCOMPtr<DocHostUIHandler> m_uiHandler;
//We store the current zoom type; //We store the current zoom type;
wxWebViewZoomType m_zoomType; wxWebViewZoomType m_zoomType;
@@ -383,7 +384,7 @@ private:
//Generic helper functions for IHtmlDocument commands //Generic helper functions for IHtmlDocument commands
bool CanExecCommand(wxString command) const; bool CanExecCommand(wxString command) const;
void ExecCommand(wxString command); void ExecCommand(wxString command);
IHTMLDocument2* GetDocument() const; wxCOMPtr<IHTMLDocument2> GetDocument() const;
//Toggles control features see INTERNETFEATURELIST for values. //Toggles control features see INTERNETFEATURELIST for values.
bool EnableControlFeature(long flag, bool enable = true); bool EnableControlFeature(long flag, bool enable = true);

View File

@@ -83,7 +83,6 @@ bool wxWebViewIE::Create(wxWindow* parent,
m_webBrowser->put_RegisterAsDropTarget(VARIANT_TRUE); m_webBrowser->put_RegisterAsDropTarget(VARIANT_TRUE);
m_uiHandler = new DocHostUIHandler; m_uiHandler = new DocHostUIHandler;
m_uiHandler->AddRef();
m_container = new wxIEContainer(this, IID_IWebBrowser2, m_webBrowser, m_uiHandler); m_container = new wxIEContainer(this, IID_IWebBrowser2, m_webBrowser, m_uiHandler);
@@ -99,8 +98,6 @@ wxWebViewIE::~wxWebViewIE()
{ {
m_factories[i]->Release(); m_factories[i]->Release();
} }
m_uiHandler->Release();
} }
void wxWebViewIE::LoadURL(const wxString& url) void wxWebViewIE::LoadURL(const wxString& url)
@@ -121,14 +118,13 @@ void wxWebViewIE::SetPage(const wxString& html, const wxString& baseUrl)
hr = SafeArrayUnaccessData(psaStrings); hr = SafeArrayUnaccessData(psaStrings);
IHTMLDocument2* document = GetDocument(); wxCOMPtr<IHTMLDocument2> document(GetDocument());
if(!document) if(!document)
return; return;
document->write(psaStrings); document->write(psaStrings);
document->close(); document->close();
document->Release();
SafeArrayDestroy(psaStrings); SafeArrayDestroy(psaStrings);
@@ -149,7 +145,6 @@ void wxWebViewIE::SetPage(const wxString& html, const wxString& baseUrl)
return; return;
document->write(psaStrings); document->write(psaStrings);
document->Release();
// SafeArrayDestroy calls SysFreeString for each BSTR // SafeArrayDestroy calls SysFreeString for each BSTR
SafeArrayDestroy(psaStrings); SafeArrayDestroy(psaStrings);
@@ -179,12 +174,12 @@ void wxWebViewIE::SetPage(const wxString& html, const wxString& baseUrl)
wxString wxWebViewIE::GetPageSource() const wxString wxWebViewIE::GetPageSource() const
{ {
IHTMLDocument2* document = GetDocument(); wxCOMPtr<IHTMLDocument2> document(GetDocument());
if(document) if(document)
{ {
IHTMLElement *bodyTag = NULL; wxCOMPtr<IHTMLElement> bodyTag;
IHTMLElement *htmlTag = NULL; wxCOMPtr<IHTMLElement> htmlTag;
wxString source; wxString source;
HRESULT hr = document->get_body(&bodyTag); HRESULT hr = document->get_body(&bodyTag);
if(SUCCEEDED(hr)) if(SUCCEEDED(hr))
@@ -195,12 +190,8 @@ wxString wxWebViewIE::GetPageSource() const
BSTR bstr; BSTR bstr;
htmlTag->get_outerHTML(&bstr); htmlTag->get_outerHTML(&bstr);
source = wxString(bstr); source = wxString(bstr);
htmlTag->Release();
} }
bodyTag->Release();
} }
document->Release();
return source; return source;
} }
else else
@@ -536,13 +527,12 @@ wxString wxWebViewIE::GetCurrentURL() const
wxString wxWebViewIE::GetCurrentTitle() const wxString wxWebViewIE::GetCurrentTitle() const
{ {
IHTMLDocument2* document = GetDocument(); wxCOMPtr<IHTMLDocument2> document(GetDocument());
if(document) if(document)
{ {
BSTR title; BSTR title;
document->get_nameProp(&title); document->get_nameProp(&title);
document->Release();
return wxString(title); return wxString(title);
} }
else else
@@ -603,7 +593,7 @@ void wxWebViewIE::Redo()
void wxWebViewIE::SetEditable(bool enable) void wxWebViewIE::SetEditable(bool enable)
{ {
IHTMLDocument2* document = GetDocument(); wxCOMPtr<IHTMLDocument2> document(GetDocument());
if(document) if(document)
{ {
@@ -612,19 +602,17 @@ void wxWebViewIE::SetEditable(bool enable)
else else
document->put_designMode(SysAllocString(L"Off")); document->put_designMode(SysAllocString(L"Off"));
document->Release();
} }
} }
bool wxWebViewIE::IsEditable() const bool wxWebViewIE::IsEditable() const
{ {
IHTMLDocument2* document = GetDocument(); wxCOMPtr<IHTMLDocument2> document(GetDocument());
if(document) if(document)
{ {
BSTR mode; BSTR mode;
document->get_designMode(&mode); document->get_designMode(&mode);
document->Release();
if(wxString(mode) == "On") if(wxString(mode) == "On")
return true; return true;
else else
@@ -643,11 +631,11 @@ void wxWebViewIE::SelectAll()
bool wxWebViewIE::HasSelection() const bool wxWebViewIE::HasSelection() const
{ {
IHTMLDocument2* document = GetDocument(); wxCOMPtr<IHTMLDocument2> document(GetDocument());
if(document) if(document)
{ {
IHTMLSelectionObject* selection; wxCOMPtr<IHTMLSelectionObject> selection;
wxString sel; wxString sel;
HRESULT hr = document->get_selection(&selection); HRESULT hr = document->get_selection(&selection);
if(SUCCEEDED(hr)) if(SUCCEEDED(hr))
@@ -655,9 +643,7 @@ bool wxWebViewIE::HasSelection() const
BSTR type; BSTR type;
selection->get_type(&type); selection->get_type(&type);
sel = wxString(type); sel = wxString(type);
selection->Release();
} }
document->Release();
return sel != "None"; return sel != "None";
} }
else else
@@ -673,33 +659,29 @@ void wxWebViewIE::DeleteSelection()
wxString wxWebViewIE::GetSelectedText() const wxString wxWebViewIE::GetSelectedText() const
{ {
IHTMLDocument2* document = GetDocument(); wxCOMPtr<IHTMLDocument2> document(GetDocument());
if(document) if(document)
{ {
IHTMLSelectionObject* selection; wxCOMPtr<IHTMLSelectionObject> selection;
wxString selected; wxString selected;
HRESULT hr = document->get_selection(&selection); HRESULT hr = document->get_selection(&selection);
if(SUCCEEDED(hr)) if(SUCCEEDED(hr))
{ {
IDispatch* disrange; wxCOMPtr<IDispatch> disrange;
hr = selection->createRange(&disrange); hr = selection->createRange(&disrange);
if(SUCCEEDED(hr)) if(SUCCEEDED(hr))
{ {
IHTMLTxtRange* range; wxCOMPtr<IHTMLTxtRange> range;
hr = disrange->QueryInterface(IID_IHTMLTxtRange, (void**)&range); hr = disrange->QueryInterface(IID_IHTMLTxtRange, (void**)&range);
if(SUCCEEDED(hr)) if(SUCCEEDED(hr))
{ {
BSTR text; BSTR text;
range->get_text(&text); range->get_text(&text);
selected = wxString(text); selected = wxString(text);
range->Release();
} }
disrange->Release();
} }
selection->Release();
} }
document->Release();
return selected; return selected;
} }
else else
@@ -710,33 +692,29 @@ wxString wxWebViewIE::GetSelectedText() const
wxString wxWebViewIE::GetSelectedSource() const wxString wxWebViewIE::GetSelectedSource() const
{ {
IHTMLDocument2* document = GetDocument(); wxCOMPtr<IHTMLDocument2> document(GetDocument());
if(document) if(document)
{ {
IHTMLSelectionObject* selection; wxCOMPtr<IHTMLSelectionObject> selection;
wxString selected; wxString selected;
HRESULT hr = document->get_selection(&selection); HRESULT hr = document->get_selection(&selection);
if(SUCCEEDED(hr)) if(SUCCEEDED(hr))
{ {
IDispatch* disrange; wxCOMPtr<IDispatch> disrange;
hr = selection->createRange(&disrange); hr = selection->createRange(&disrange);
if(SUCCEEDED(hr)) if(SUCCEEDED(hr))
{ {
IHTMLTxtRange* range; wxCOMPtr<IHTMLTxtRange> range;
hr = disrange->QueryInterface(IID_IHTMLTxtRange, (void**)&range); hr = disrange->QueryInterface(IID_IHTMLTxtRange, (void**)&range);
if(SUCCEEDED(hr)) if(SUCCEEDED(hr))
{ {
BSTR text; BSTR text;
range->get_htmlText(&text); range->get_htmlText(&text);
selected = wxString(text); selected = wxString(text);
range->Release();
} }
disrange->Release();
} }
selection->Release();
} }
document->Release();
return selected; return selected;
} }
else else
@@ -747,39 +725,35 @@ wxString wxWebViewIE::GetSelectedSource() const
void wxWebViewIE::ClearSelection() void wxWebViewIE::ClearSelection()
{ {
IHTMLDocument2* document = GetDocument(); wxCOMPtr<IHTMLDocument2> document(GetDocument());
if(document) if(document)
{ {
IHTMLSelectionObject* selection; wxCOMPtr<IHTMLSelectionObject> selection;
wxString selected; wxString selected;
HRESULT hr = document->get_selection(&selection); HRESULT hr = document->get_selection(&selection);
if(SUCCEEDED(hr)) if(SUCCEEDED(hr))
{ {
selection->empty(); selection->empty();
selection->Release();
} }
document->Release();
} }
} }
wxString wxWebViewIE::GetPageText() const wxString wxWebViewIE::GetPageText() const
{ {
IHTMLDocument2* document = GetDocument(); wxCOMPtr<IHTMLDocument2> document(GetDocument());
if(document) if(document)
{ {
wxString text; wxString text;
IHTMLElement* body; wxCOMPtr<IHTMLElement> body;
HRESULT hr = document->get_body(&body); HRESULT hr = document->get_body(&body);
if(SUCCEEDED(hr)) if(SUCCEEDED(hr))
{ {
BSTR out; BSTR out;
body->get_innerText(&out); body->get_innerText(&out);
text = wxString(out); text = wxString(out);
body->Release();
} }
document->Release();
return text; return text;
} }
else else
@@ -790,11 +764,11 @@ wxString wxWebViewIE::GetPageText() const
void wxWebViewIE::RunScript(const wxString& javascript) void wxWebViewIE::RunScript(const wxString& javascript)
{ {
IHTMLDocument2* document = GetDocument(); wxCOMPtr<IHTMLDocument2> document(GetDocument());
if(document) if(document)
{ {
IHTMLWindow2* window; wxCOMPtr<IHTMLWindow2> window;
wxString language = "javascript"; wxString language = "javascript";
HRESULT hr = document->get_parentWindow(&window); HRESULT hr = document->get_parentWindow(&window);
if(SUCCEEDED(hr)) if(SUCCEEDED(hr))
@@ -806,7 +780,6 @@ void wxWebViewIE::RunScript(const wxString& javascript)
SysAllocString(language.wc_str()), SysAllocString(language.wc_str()),
&level); &level);
} }
document->Release();
} }
} }
@@ -843,14 +816,13 @@ void wxWebViewIE::RegisterHandler(wxSharedPtr<wxWebViewHandler> handler)
bool wxWebViewIE::CanExecCommand(wxString command) const bool wxWebViewIE::CanExecCommand(wxString command) const
{ {
IHTMLDocument2* document = GetDocument(); wxCOMPtr<IHTMLDocument2> document(GetDocument());
if(document) if(document)
{ {
VARIANT_BOOL enabled; VARIANT_BOOL enabled;
document->queryCommandEnabled(SysAllocString(command.wc_str()), &enabled); document->queryCommandEnabled(SysAllocString(command.wc_str()), &enabled);
document->Release();
return (enabled == VARIANT_TRUE); return (enabled == VARIANT_TRUE);
} }
@@ -863,32 +835,26 @@ bool wxWebViewIE::CanExecCommand(wxString command) const
void wxWebViewIE::ExecCommand(wxString command) void wxWebViewIE::ExecCommand(wxString command)
{ {
IHTMLDocument2* document = GetDocument(); wxCOMPtr<IHTMLDocument2> document(GetDocument());
if(document) if(document)
{ {
document->execCommand(SysAllocString(command.wc_str()), VARIANT_FALSE, VARIANT(), NULL); document->execCommand(SysAllocString(command.wc_str()), VARIANT_FALSE, VARIANT(), NULL);
document->Release();
} }
} }
IHTMLDocument2* wxWebViewIE::GetDocument() const wxCOMPtr<IHTMLDocument2> wxWebViewIE::GetDocument() const
{ {
IDispatch* dispatch = NULL; wxCOMPtr<IDispatch> dispatch;
wxCOMPtr<IHTMLDocument2> document;
HRESULT result = m_webBrowser->get_Document(&dispatch); HRESULT result = m_webBrowser->get_Document(&dispatch);
if(dispatch && SUCCEEDED(result)) if(dispatch && SUCCEEDED(result))
{ {
IHTMLDocument2* document;
dispatch->QueryInterface(IID_IHTMLDocument2, (void**)&document);
dispatch->Release();
//document is set to null automatically if the interface isn't supported //document is set to null automatically if the interface isn't supported
dispatch->QueryInterface(IID_IHTMLDocument2, (void**)&document);
}
return document; return document;
} }
else
{
return NULL;
}
}
bool wxWebViewIE::EnableControlFeature(long flag, bool enable) bool wxWebViewIE::EnableControlFeature(long flag, bool enable)
{ {