admin
2020-06-10 a610f2ab6e543d2cb78c1ef212ac6a74ddc067d9
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Copyright © 2015 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
 
#pragma once
 
#include "Stdafx.h"
 
#include "include/internal/cef_ptr.h"
#include "include\cef_download_item.h"
#include "include\cef_response.h"
#include "include\cef_web_plugin.h"
 
#include "Serialization\ObjectsSerialization.h"
#include "Serialization\V8Serialization.h"
 
using namespace System::Collections::Generic;
using namespace System::Collections::Specialized;
using namespace System::Security::Cryptography::X509Certificates;
using namespace CefSharp::Internals::Serialization;
 
namespace CefSharp
{
    namespace Internals
    {
        //Static class for converting to/from Cef classes
        private ref class TypeConversion abstract sealed
        {
        public:
            //Convert from NameValueCollection to HeaderMap
            static CefResponse::HeaderMap ToNative(NameValueCollection^ headers)
            {
                CefResponse::HeaderMap result;
 
                if (headers == nullptr)
                {
                    return result;
                }
 
                for each (String^ key in headers)
                {
                    for each(String^ value in headers->GetValues(key))
                    {
                        result.insert(std::pair<CefString, CefString>(StringUtils::ToNative(key), StringUtils::ToNative(value)));
                    }
                }
 
                return result;
            }
 
            //ConvertFrom CefDownload to DownloadItem
            static DownloadItem^ FromNative(CefRefPtr<CefDownloadItem> downloadItem)
            {
                auto item = gcnew CefSharp::DownloadItem();
                item->IsValid = downloadItem->IsValid();
                //NOTE: Description for IsValid says `Do not call any other methods if this function returns false.` so only load if IsValid = true
                if (item->IsValid)
                {
                    item->IsInProgress = downloadItem->IsInProgress();
                    item->IsComplete = downloadItem->IsComplete();
                    item->IsCancelled = downloadItem->IsCanceled();
                    item->CurrentSpeed = downloadItem->GetCurrentSpeed();
                    item->PercentComplete = downloadItem->GetPercentComplete();
                    item->TotalBytes = downloadItem->GetTotalBytes();
                    item->ReceivedBytes = downloadItem->GetReceivedBytes();
                    item->StartTime = FromNative(downloadItem->GetStartTime());
                    item->EndTime = FromNative(downloadItem->GetEndTime());
                    item->FullPath = StringUtils::ToClr(downloadItem->GetFullPath());
                    item->Id = downloadItem->GetId();
                    item->Url = StringUtils::ToClr(downloadItem->GetURL());
                    item->OriginalUrl = StringUtils::ToClr(downloadItem->GetOriginalUrl());
                    item->SuggestedFileName = StringUtils::ToClr(downloadItem->GetSuggestedFileName());
                    item->ContentDisposition = StringUtils::ToClr(downloadItem->GetContentDisposition());
                    item->MimeType = StringUtils::ToClr(downloadItem->GetMimeType());
                }
 
                return item;
            }
 
            //Convert from CefTime to Nullable<DateTime>
            static Nullable<DateTime> FromNative(CefTime time)
            {
                auto epoch = time.GetDoubleT();
                if (epoch == 0)
                {
                    return Nullable<DateTime>();
                }
                return Nullable<DateTime>(DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(epoch).ToLocalTime());
            }
 
            static WebPluginInfo^ FromNative(CefRefPtr<CefWebPluginInfo> webPluginInfo)
            {
                return gcnew WebPluginInfo(StringUtils::ToClr(webPluginInfo->GetName()),
                    StringUtils::ToClr(webPluginInfo->GetDescription()),
                    StringUtils::ToClr(webPluginInfo->GetPath()),
                    StringUtils::ToClr(webPluginInfo->GetVersion()));
            }
 
            static IList<DraggableRegion>^ FromNative(const std::vector<CefDraggableRegion>& regions)
            {
                if (regions.size() == 0)
                {
                    return nullptr;
                }
 
                auto list = gcnew List<DraggableRegion>();
 
                for each (CefDraggableRegion region in regions)
                {
                    list->Add(DraggableRegion(region.bounds.width, region.bounds.height, region.bounds.x, region.bounds.y, region.draggable == 1));
                }
 
                return list;
            }
 
            static CefRefPtr<CefValue> ToNative(Object^ value)
            {
                auto cefValue = CefValue::Create();
 
                if (value == nullptr)
                {
                    cefValue->SetNull();
 
                    return cefValue;
                }
 
                auto type = value->GetType();
                Type^ underlyingType = Nullable::GetUnderlyingType(type);
                if (underlyingType != nullptr)
                {
                    type = underlyingType;
                }
 
                if (type == Boolean::typeid)
                {
                    cefValue->SetBool(safe_cast<bool>(value));
                }
                else if (type == Int32::typeid)
                {
                    cefValue->SetInt(safe_cast<int>(value));
                }
                else if (type == String::typeid)
                {
                    cefValue->SetString(StringUtils::ToNative(safe_cast<String^>(value)));
                }
                else if (type == Double::typeid)
                {
                    cefValue->SetDouble(safe_cast<double>(value));
                }
                else if (type == Decimal::typeid)
                {
                    cefValue->SetDouble(Convert::ToDouble(value));
                }
                else if (System::Collections::IDictionary::typeid->IsAssignableFrom(type))
                {
                    auto dictionary = (System::Collections::IDictionary^) value;
                    auto cefDictionary = CefDictionaryValue::Create();
 
                    for each (System::Collections::DictionaryEntry entry in dictionary)
                    {
                        auto key = StringUtils::ToNative(Convert::ToString(entry.Key));
                        auto entryValue = entry.Value;
                        SerializeV8Object(cefDictionary, key, entryValue);
                    }
 
                    cefValue->SetDictionary(cefDictionary);
                }
                else if (System::Collections::IEnumerable::typeid->IsAssignableFrom(type))
                {
                    auto enumerable = (System::Collections::IEnumerable^) value;
                    auto cefList = CefListValue::Create();
 
                    int i = 0;
                    for each (Object^ arrObj in enumerable)
                    {
                        SerializeV8Object(cefList, i, arrObj);
 
                        i++;
                    }
                    cefValue->SetList(cefList);
                }
 
                return cefValue;
            }
 
            static Object^ FromNative(const CefRefPtr<CefValue>& value)
            {
                if (!value.get())
                {
                    return nullptr;
                }
 
                auto type = value->GetType();
 
                if (type == CefValueType::VTYPE_BOOL)
                {
                    return value->GetBool();
                }
 
                if (type == CefValueType::VTYPE_DOUBLE)
                {
                    return value->GetDouble();
                }
 
                if (type == CefValueType::VTYPE_INT)
                {
                    return value->GetInt();
                }
 
                if (type == CefValueType::VTYPE_STRING)
                {
                    return StringUtils::ToClr(value->GetString());
                }
 
                if (type == CefValueType::VTYPE_DICTIONARY)
                {
                    return FromNative(value->GetDictionary());
                }
 
                if (type == CefValueType::VTYPE_LIST)
                {
                    return FromNative(value->GetList());
                }
 
                return nullptr;
            }
 
            static IDictionary<String^, Object^>^ FromNative(const CefRefPtr<CefDictionaryValue>& dictionary)
            {
                if (!dictionary.get() || dictionary->GetSize() == 0)
                {
                    return nullptr;
                }
 
                auto dict = gcnew Dictionary<String^, Object^>();
 
                CefDictionaryValue::KeyList keys;
                dictionary->GetKeys(keys);
 
                for (auto i = 0; i < keys.size(); i++)
                {
                    auto key = StringUtils::ToClr(keys[i]);
                    auto value = DeserializeObject(dictionary, keys[i], nullptr);
 
                    dict->Add(key, value);
                }
 
                return dict;
            }
 
            static List<Object^>^ FromNative(const CefRefPtr<CefListValue>& list)
            {
                auto result = gcnew List<Object^>(list->GetSize());
                for (auto i = 0; i < list->GetSize(); i++)
                {
                    result->Add(DeserializeObject(list, i, nullptr));
                }
 
                return result;
            }
 
            // Copied from CefSharp.BrowserSubprocess.Core\TypeUtils.h since it can't be included
            static DateTime ConvertCefTimeToDateTime(CefTime time)
            {
                return DateTimeUtils::FromCefTime(time.year,
                    time.month,
                    time.day_of_month,
                    time.hour,
                    time.minute,
                    time.second,
                    time.millisecond);
            }
 
            static Cookie^ FromNative(const CefCookie& cefCookie)
            {
                auto cookie = gcnew Cookie();
                auto cookieName = StringUtils::ToClr(cefCookie.name);
 
                if (!String::IsNullOrEmpty(cookieName))
                {
                    cookie->Name = cookieName;
                    cookie->Value = StringUtils::ToClr(cefCookie.value);
                    cookie->Domain = StringUtils::ToClr(cefCookie.domain);
                    cookie->Path = StringUtils::ToClr(cefCookie.path);
                    cookie->Secure = cefCookie.secure == 1;
                    cookie->HttpOnly = cefCookie.httponly == 1;
                    cookie->Creation = ConvertCefTimeToDateTime(cefCookie.creation);
                    cookie->LastAccess = ConvertCefTimeToDateTime(cefCookie.last_access);
 
                    if (cefCookie.has_expires)
                    {
                        cookie->Expires = ConvertCefTimeToDateTime(cefCookie.expires);
                    }
                }
 
                return cookie;
            }
 
            static NavigationEntry^ FromNative(const CefRefPtr<CefNavigationEntry> entry, bool current)
            {
                SslStatus^ sslStatus;
              
                if (!entry.get())
                {
                    return nullptr;
                }
 
                if (!entry->IsValid())
                {
                    return gcnew NavigationEntry(current, DateTime::MinValue, nullptr, -1, nullptr, nullptr, (TransitionType)-1, nullptr, false, false, sslStatus);
                }
 
                auto time = entry->GetCompletionTime();
                DateTime completionTime = CefTimeUtils::ConvertCefTimeToDateTime(time.GetDoubleT());
                auto ssl = entry->GetSSLStatus();
                X509Certificate2^ sslCertificate;
 
                if (ssl.get())
                {
                    auto certificate = ssl->GetX509Certificate();
                    if (certificate.get())
                    {
                        auto derEncodedCertificate = certificate->GetDEREncoded();
                        auto byteCount = derEncodedCertificate->GetSize();
                        if (byteCount > 0)
                        {
                            auto bytes = gcnew cli::array<Byte>(byteCount);
                            pin_ptr<Byte> src = &bytes[0]; // pin pointer to first element in arr
 
                            derEncodedCertificate->GetData(static_cast<void*>(src), byteCount, 0);
 
                            sslCertificate = gcnew X509Certificate2(bytes);
                        }
                    }
 
                    sslStatus = gcnew SslStatus(ssl->IsSecureConnection(), (CertStatus)ssl->GetCertStatus(), (SslVersion)ssl->GetSSLVersion(), (SslContentStatus)ssl->GetContentStatus(), sslCertificate);
                }
 
                return gcnew NavigationEntry(current, completionTime, StringUtils::ToClr(entry->GetDisplayURL()), entry->GetHttpStatusCode(),
                    StringUtils::ToClr(entry->GetOriginalURL()), StringUtils::ToClr(entry->GetTitle()), (TransitionType)entry->GetTransitionType(),
                    StringUtils::ToClr(entry->GetURL()), entry->HasPostData(), true, sslStatus);
            }
        };
    }
}