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
194 lines (160 loc) · 5.51 KB
/
HTMLMoniker.cpp
File metadata and controls
194 lines (160 loc) · 5.51 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* Copyright 2006-2009, 2017, 2020 United States Government, as represented by the
* Administrator of the National Aeronautics and Space Administration.
* All rights reserved.
*
* The NASA World Wind Java (WWJ) platform is licensed under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* NASA World Wind Java (WWJ) also contains the following 3rd party Open Source
* software:
*
* Jackson Parser – Licensed under Apache 2.0
* GDAL – Licensed under MIT
* JOGL – Licensed under Berkeley Software Distribution (BSD)
* Gluegen – Licensed under Berkeley Software Distribution (BSD)
*
* A complete listing of 3rd Party software notices and licenses included in
* NASA World Wind Java (WWJ) can be found in the WorldWindJava-v2.2 3rd-party
* notices and licenses PDF found in code directory.
*/
/**
* 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;
}