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
// 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\cef_request.h"
 
#include "PostDataElement.h"
#include "Internals\TypeConversion.h"
#include "Internals\CefWrapper.h"
 
using namespace System::Collections::Generic;
using namespace System::Collections::ObjectModel;
 
namespace CefSharp
{
    /// <summary>
    /// Form Post Data
    /// </summary>
    /// <seealso cref="T:IPostData"/>
    public ref class PostData : public IPostData, public CefWrapper
    {
        MCefRefPtr<CefPostData> _postData;
        List<IPostDataElement^>^ _postDataElements;
 
    internal:
        PostData(CefRefPtr<CefPostData> &postData) :
            _postData(postData)
        {
 
        }
 
        /// <summary>
        /// Finalizer.
        /// </summary>
        !PostData()
        {
            _postData = nullptr;
        }
 
        /// <summary>
        /// Destructor.
        /// </summary>
        ~PostData()
        {
            this->!PostData();
 
            if (_postDataElements != nullptr)
            {
                //Make sure the unmanaged resources are handled
                for each (IPostDataElement^ element in _postDataElements)
                {
                    delete element;
                }
 
                _postDataElements = nullptr;
            }
 
            _disposed = true;
        }
 
        /// <summary>
        /// Throw exception if Readonly
        /// </summary>
        /// <exception cref="Exception">Thrown when an exception error condition occurs.</exception>
        void ThrowIfReadOnly()
        {
            if (IsReadOnly)
            {
                throw gcnew Exception(gcnew String(L"This IPostDataWrapper is readonly and cannot be modified."));
            }
        }
 
    public:
        /// <summary>
        /// Default constructor.
        /// </summary>
        PostData()
        {
            _postData = CefPostData::Create();
        }
 
        /// <summary>
        /// Returns true if this object is read-only.
        /// </summary>
        virtual property bool IsReadOnly
        {
            bool get()
            {
                ThrowIfDisposed();
 
                return _postData->IsReadOnly();
            }
        }
 
        /// <summary>
        /// Retrieve the post data elements.
        /// </summary>
        virtual property IList<IPostDataElement^>^ Elements
        {
            IList<IPostDataElement^>^ get()
            {
                ThrowIfDisposed();
 
                if (_postDataElements == nullptr)
                {
                    _postDataElements = gcnew List<IPostDataElement^>();
 
                    auto elementCount = _postData.get() ? _postData->GetElementCount() : 0;
                    if (elementCount == 0)
                    {
                        return gcnew ReadOnlyCollection<IPostDataElement^>(_postDataElements);
                    }
                    CefPostData::ElementVector ev;
 
                    _postData->GetElements(ev);
 
                    for (CefPostData::ElementVector::iterator it = ev.begin(); it != ev.end(); ++it)
                    {
                        CefPostDataElement *el = it->get();
 
                        _postDataElements->Add(gcnew PostDataElement(el));
                    }
                }
 
                return gcnew ReadOnlyCollection<IPostDataElement^>(_postDataElements);
            }
        }
 
        /// <summary>
        /// Add the specified <see cref="IPostDataElement"/>.
        /// </summary>
        /// <param name="element">element to be added.</param>
        /// <returns>Returns true if the add succeeds.</returns>
        virtual bool AddElement(IPostDataElement^ element)
        {
            ThrowIfDisposed();
 
            ThrowIfReadOnly();
 
            //Access the Elements collection to initialize the underlying _postDataElements collection
            auto elements = Elements;
 
            //If the element has already been added then don't add it again
            if (elements->Contains(element))
            {
                return false;
            }
 
            _postDataElements->Add(element);
 
            auto elementWrapper = (PostDataElement^)element;
 
            return _postData->AddElement(elementWrapper);
        }
 
        /// <summary>
        /// Remove  the specified <see cref="IPostDataElement"/>.
        /// </summary>
        /// <param name="element">element to be removed.</param>
        /// <returns> Returns true if the add succeeds.</returns>
        virtual bool RemoveElement(IPostDataElement^ element)
        {
            ThrowIfDisposed();
 
            ThrowIfReadOnly();
 
            //Access the Elements collection to initialize the underlying _postDataElements collection
            auto elements = Elements;
 
            if (!elements->Contains(element))
            {
                return false;
            }
 
            _postDataElements->Remove(element);
 
            auto elementWrapper = (PostDataElement^)element;
 
            return _postData->RemoveElement(elementWrapper);
        }
 
        /// <summary>
        /// Remove all existing post data elements.
        /// </summary>
        virtual void RemoveElements()
        {
            ThrowIfDisposed();
 
            ThrowIfReadOnly();
 
            _postData->RemoveElements();
        }
 
        /// <summary>
        /// Create a new <see cref="IPostDataElement"/> instance
        /// </summary>
        /// <returns>PostDataElement</returns>
        virtual IPostDataElement^ CreatePostDataElement()
        {
            auto element = CefPostDataElement::Create();
 
            return gcnew PostDataElement(element);
        }
 
        /// <summary>
        /// Returns true if the underlying POST data includes elements that are not
        /// represented by this IPostData object (for example, multi-part file upload
        /// data). Modifying IPostData objects with excluded elements may result in
        /// the request failing.
        /// </summary>
        virtual property bool HasExcludedElements
        {
            bool get()
            {
                ThrowIfDisposed();
 
                return _postData->HasExcludedElements();
            }
        }
 
        operator CefRefPtr<CefPostData>()
        {
            if (this == nullptr)
            {
                return NULL;
            }
            return _postData.get();
        }
    };
}