forked from NASAWorldWind/WorldWindJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTMLMoniker.cpp
More file actions
172 lines (138 loc) · 4.39 KB
/
HTMLMoniker.cpp
File metadata and controls
172 lines (138 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* Copyright (C) 2012 United States Government as represented by the Administrator of the
* National Aeronautics and Space Administration.
* All Rights Reserved.
*/
/**
* Version: $Id: HTMLMoniker.cpp 1171 2013-02-11 21:45:02Z dcollins $
*/
#include "stdafx.h"
#include "HTMLMoniker.h"
#include "util/Logging.h"
extern const wchar_t *DEFAULT_BASE_URL;
const wchar_t *SHLWAPI_DLL = L"shlwapi.dll";
// Ordinal of the SHCreateMemStream function in ShlWapi.dll
#define INDEX_OF_SHCreateMemStream 12
HTMLMoniker::HTMLMoniker()
: refCount(0),
baseUrl(NULL),
htmlBuffer(NULL),
htmlStream(NULL),
libShlWapi(NULL),
SHCreateMemStream(NULL)
{
// Load the SHCreateMemStream function from ShWapi.dll.
libShlWapi = LoadLibrary(SHLWAPI_DLL);
if (this->libShlWapi != NULL)
{
this->SHCreateMemStream = (fnSHCreateMemStream) GetProcAddress(this->libShlWapi, (LPCSTR) INDEX_OF_SHCreateMemStream);
if (this->SHCreateMemStream == NULL)
{
Logging::logger()->severe(L"NativeLib.LibraryNotAvailable", L"shlwapi.dll:SHCreateMemStream");
assert(FALSE && "Failed to load SHCreateMemStream from shawapi.dll");
}
}
else
{
Logging::logger()->severe(L"NativeLib.LibraryNotAvailable", SHLWAPI_DLL);
assert(FALSE && "Failed to load shawapi.dll");
}
}
HTMLMoniker::~HTMLMoniker()
{
if (htmlStream)
htmlStream->Release();
if (htmlBuffer)
free(htmlBuffer);
if (baseUrl)
free(baseUrl);
// Release our reference to ShWapi.dll. FreeLibrary decrements the reference count on the loaded library,
// so this will not interfere with any other code in our process that is using the library.
FreeLibrary(libShlWapi);
}
HRESULT HTMLMoniker::SetHTML(BYTE *buffer, UINT bufferSize)
{
if (htmlBuffer)
free(htmlBuffer);
this->htmlBuffer = buffer;
if (this->htmlStream)
this->htmlStream->Release();
if (this->SHCreateMemStream)
this->htmlStream = SHCreateMemStream(buffer, bufferSize);
else
Logging::logger()->severe(L"NativeLib.LibraryNotAvailable", L"shlwapi.dll:SHCreateMemStream");
return S_OK;
}
HRESULT HTMLMoniker::SetBaseURL(const wchar_t *baseUrl, size_t baseUrlLen)
{
if (this->baseUrl)
free(this->baseUrl);
this->baseUrl = (wchar_t*)calloc(baseUrlLen + 1, sizeof(wchar_t));
wcsncat_s(this->baseUrl, baseUrlLen + 1, reinterpret_cast<const wchar_t*>(baseUrl), baseUrlLen);
return S_OK;
}
BOOL HTMLMoniker::IsDefaultBaseUrl() const
{
return (wcscmp(this->baseUrl, DEFAULT_BASE_URL) == 0);
}
////////////////////////////////////////
// IMoniker
////////////////////////////////////////
/**
* Get a stream of the HTML content.
*/
STDMETHODIMP HTMLMoniker::BindToStorage(IBindCtx *pbc, IMoniker *pmkToLeft, REFIID riid, void **ppvObj)
{
// Reset the stream cursor to the beginning of the stream
LARGE_INTEGER seek = {0};
this->htmlStream->Seek(seek, STREAM_SEEK_SET, NULL);
return this->htmlStream->QueryInterface(riid, ppvObj);
}
/**
* Get the moniker display name. We return our base URL. MSHTML will use this as this value as the base URL
* for resolving relative links.
*/
STDMETHODIMP HTMLMoniker::GetDisplayName(IBindCtx *pbc, IMoniker *pmkToLeft, LPOLESTR *ppszDisplayName)
{
if (!ppszDisplayName)
return E_POINTER;
*ppszDisplayName = NULL;
size_t baseUrlLen = wcslen(baseUrl);
LPOLESTR displayName = (LPOLESTR) CoTaskMemAlloc((baseUrlLen + 1) * sizeof(wchar_t));
*displayName = L'\0';
if (baseUrlLen > 0)
wcscpy_s(displayName, baseUrlLen + 1, baseUrl);
*ppszDisplayName = displayName;
return S_OK;
}
////////////////////////////////////////
// IUnknown
////////////////////////////////////////
STDMETHODIMP HTMLMoniker::QueryInterface(REFIID riid, void **ppvObject)
{
*ppvObject = NULL;
if (riid == IID_IUnknown)
*ppvObject = static_cast<IUnknown*>(this);
else if (riid == IID_IMoniker)
*ppvObject = static_cast<IMoniker*>(this);
if (*ppvObject)
{
((IUnknown*)*ppvObject)->AddRef();
return S_OK;
}
else return E_NOINTERFACE;
}
ULONG STDMETHODCALLTYPE HTMLMoniker::AddRef()
{
return this->refCount++;
}
ULONG STDMETHODCALLTYPE HTMLMoniker::Release()
{
this->refCount--;
if (this->refCount == 0)
{
delete this;
return 0;
}
return this->refCount;
}