[Tizen] Add codes for Dali Windows Backend
[platform/core/uifw/dali-adaptor.git] / dali / internal / imaging / windows / file-download-win.cpp
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // HEADER
19 #include <dali/internal/imaging/common/file-download.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <pthread.h>
24 #include <openssl/crypto.h>
25 #include <cstring>
26 #include <curl/curl.h>
27
28 // INTERNAL INCLUDES
29 #include <dali/internal/system/common/file-writer.h>
30
31 #ifdef TPK_CURL_ENABLED
32 #include <tpkp_curl.h>
33 #endif // TPK_CURL_ENABLED
34
35 using namespace Dali::Integration;
36
37 namespace Dali
38 {
39
40 namespace TizenPlatform
41 {
42
43 namespace // unnamed namespace
44 {
45 const long EXCLUDE_HEADER = 0L;
46 const long INCLUDE_BODY = 0L;
47
48 /**
49  * Curl library environment. Direct initialize ensures it's constructed before adaptor
50  * or application creates any threads.
51  */
52 static Dali::TizenPlatform::Network::CurlEnvironment gCurlEnvironment;
53
54 // Without a write function or a buffer (file descriptor) to write to, curl will pump out
55 // header/body contents to stdout
56 size_t __cdecl DummyWrite(char *ptr, size_t size, size_t nmemb, void *userdata)
57 {
58   return size * nmemb;
59 }
60
61 struct ChunkData
62 {
63   std::vector< uint8_t > data;
64 };
65
66 size_t __cdecl ChunkLoader(char *ptr, size_t size, size_t nmemb, void *userdata)
67 {
68   std::vector<ChunkData>* chunks = static_cast<std::vector<ChunkData>*>( userdata );
69   int numBytes = size*nmemb;
70   chunks->push_back( ChunkData() );
71   ChunkData& chunkData = (*chunks)[chunks->size()-1];
72   chunkData.data.reserve( numBytes );
73   memcpy( chunkData.data.data(), ptr, numBytes );
74   return numBytes;
75 }
76
77
78 CURLcode DownloadFileDataWithSize( CURL* curlHandle, Dali::Vector<uint8_t>& dataBuffer, size_t dataSize )
79 {
80   CURLcode result( CURLE_OK );
81
82   // create
83   Dali::Internal::Platform::FileWriter fileWriter( dataBuffer, dataSize );
84   FILE* dataBufferFilePointer = fileWriter.GetFile();
85   if( NULL != dataBufferFilePointer )
86   {
87     // we only want the body which contains the file data
88     curl_easy_setopt( curlHandle, CURLOPT_HEADER, EXCLUDE_HEADER );
89     curl_easy_setopt( curlHandle, CURLOPT_NOBODY, INCLUDE_BODY );
90
91     // disable the write callback, and get curl to write directly into our data buffer
92     Network::CurlEnvironment::InitWriteFunction( curlHandle );
93
94     curl_easy_setopt( curlHandle, CURLOPT_WRITEDATA, dataBufferFilePointer );
95
96     // synchronous request of the body data
97     result = curl_easy_perform( curlHandle );
98   }
99   return result;
100 }
101
102 CURLcode DownloadFileDataByChunk( CURL* curlHandle, Dali::Vector<uint8_t>& dataBuffer, size_t& dataSize )
103 {
104   // create
105   std::vector< ChunkData > chunks;
106
107   // we only want the body which contains the file data
108   curl_easy_setopt( curlHandle, CURLOPT_HEADER, EXCLUDE_HEADER );
109   curl_easy_setopt( curlHandle, CURLOPT_NOBODY, INCLUDE_BODY );
110
111   // Enable the write callback.
112   curl_easy_setopt( curlHandle, CURLOPT_WRITEFUNCTION, ChunkLoader );
113   curl_easy_setopt( curlHandle, CURLOPT_WRITEDATA, &chunks );
114
115   // synchronous request of the body data
116   CURLcode result = curl_easy_perform( curlHandle );
117
118   // chunks should now contain all of the chunked data. Reassemble into a single vector
119   dataSize = 0;
120   for( size_t i=0; i<chunks.size() ; ++i )
121   {
122     dataSize += chunks[i].data.capacity();
123   }
124   dataBuffer.Resize(dataSize);
125
126   size_t offset = 0;
127   for( size_t i=0; i<chunks.size() ; ++i )
128   {
129     memcpy( &dataBuffer[offset], chunks[i].data.data(), chunks[i].data.capacity() ); 
130     offset += chunks[i].data.capacity();
131   }
132
133   return result;
134 }
135
136 bool DownloadFile( CURL* curlHandle,
137                    const std::string& url,
138                    Dali::Vector<uint8_t>& dataBuffer,
139                    size_t& dataSize,
140                    size_t maximumAllowedSizeBytes )
141 {
142   CURLcode result( CURLE_OK );
143   double size(0);
144
145   // setup curl to download just the header so we can extract the content length
146   Network::CurlEnvironment::ConfigureCurlOptions( curlHandle, url );
147
148   curl_easy_setopt( curlHandle, CURLOPT_WRITEFUNCTION, DummyWrite);
149
150   // perform the request to get the header
151   result = curl_easy_perform( curlHandle );
152
153   if( result != CURLE_OK)
154   {
155     DALI_LOG_ERROR( "Failed to download http header for \"%s\" with error code %d\n", url.c_str(), result );
156     return false;
157   }
158
159   // get the content length, -1 == size is not known
160   curl_easy_getinfo( curlHandle,CURLINFO_CONTENT_LENGTH_DOWNLOAD , &size );
161
162
163   if( size >= maximumAllowedSizeBytes )
164   {
165     DALI_LOG_ERROR( "File content length %f > max allowed %zu \"%s\" \n", size, maximumAllowedSizeBytes, url.c_str() );
166     return false;
167   }
168   else if( size > 0 )
169   {
170     // If we know the size up front, allocate once and avoid chunk copies.
171     dataSize = static_cast<size_t>( size );
172     result = DownloadFileDataWithSize( curlHandle, dataBuffer, dataSize );
173   }
174   else
175   {
176     result = DownloadFileDataByChunk( curlHandle, dataBuffer, dataSize );
177   }
178
179   if( result != CURLE_OK )
180   {
181     DALI_LOG_ERROR( "Failed to download image file \"%s\" with error code %d\n", url.c_str(), result );
182     return false;
183   }
184   return true;
185 }
186
187
188 } // unnamed namespace
189
190
191 namespace Network
192 {
193
194 std::mutex* CurlEnvironment::mMutexs = NULL;
195
196 CurlEnvironment::CurlEnvironment()
197 {
198   // Must be called before we attempt any loads. e.g. by using curl_easy_init()
199   // and before we start any threads.
200   curl_global_init(CURL_GLOBAL_ALL);
201
202  // libcurl with openssl needs locking_function and thread id for threadsafe
203  // https://curl.haxx.se/libcurl/c/threadsafe.html
204  // https://www.openssl.org/docs/man1.0.2/crypto/threads.html#DESCRIPTION
205  // SetLockingFunction sets locking_function and get thread id by the guide.
206   SetLockingFunction();
207 }
208
209 CurlEnvironment::~CurlEnvironment()
210 {
211   UnsetLockingFunction();
212
213   curl_global_cleanup();
214 }
215
216 // libcurl with openssl needs locking_function and thread id for threadsafe
217 // https://curl.haxx.se/libcurl/c/threadsafe.html
218 // https://www.openssl.org/docs/man1.0.2/crypto/threads.html#DESCRIPTION
219 void CurlEnvironment::OnOpenSSLLocking( int mode, int n, const char* file, int line )
220 {
221   if( mode & CRYPTO_LOCK )
222   {
223     mMutexs[n].lock();
224   }
225   else
226   {
227     mMutexs[n].unlock();
228   }
229 }
230
231 void CurlEnvironment::SetLockingFunction()
232 {
233   if( mMutexs != NULL )
234   {
235     return;
236   }
237
238   mMutexs = new std::mutex[ CRYPTO_num_locks() ];
239
240   CRYPTO_set_id_callback( &CurlEnvironment::GetThreadId );
241   CRYPTO_set_locking_callback( &CurlEnvironment::OnOpenSSLLocking );
242 }
243
244 void CurlEnvironment::UnsetLockingFunction()
245 {
246   if( mMutexs == NULL )
247   {
248     return;
249   }
250
251   CRYPTO_set_id_callback( NULL );
252   CRYPTO_set_locking_callback( NULL );
253
254   delete [] mMutexs;
255   mMutexs = NULL;
256 }
257
258 bool DownloadRemoteFileIntoMemory( const std::string& url,
259                                    Dali::Vector<uint8_t>& dataBuffer,
260                                    size_t& dataSize,
261                                    size_t maximumAllowedSizeBytes )
262 {
263   if( url.empty() )
264   {
265     DALI_LOG_WARNING("empty url requested \n");
266     return false;
267   }
268
269   // start a libcurl easy session, this internally calls curl_global_init, if we ever have more than one download
270   // thread we need to explicity call curl_global_init() on startup from a single thread.
271
272   CURL* curlHandle = curl_easy_init();
273
274   bool result = DownloadFile( curlHandle, url, dataBuffer,  dataSize, maximumAllowedSizeBytes);
275
276   // clean up session
277   curl_easy_cleanup( curlHandle );
278
279 #ifdef TPK_CURL_ENABLED
280   // Clean up tpkp(the module for certificate pinning) resources on Tizen
281   tpkp_curl_cleanup();
282 #endif // TPK_CURL_ENABLED
283
284   return result;
285 }
286
287 } // namespace Network
288
289 } // namespace TizenPlatform
290
291 } // namespace Dali