9c6d41952c83795a490d5549551af0deca40ece0
[platform/core/uifw/dali-adaptor.git] / dali / internal / imaging / common / file-download.h
1 #ifndef __DALI_TIZEN_PLATFORM_NETWORK_FILE_DOWNLOAD_H__
2 #define __DALI_TIZEN_PLATFORM_NETWORK_FILE_DOWNLOAD_H__
3
4 /*
5  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/common/dali-vector.h>
23 #include <string>
24 #include <mutex> //c++11
25 #include <stdint.h> // uint8
26
27 namespace Dali
28 {
29
30 namespace TizenPlatform
31 {
32
33 namespace Network
34 {
35
36 /**
37  * Set up the cURL environment - this will ensure curl_global_init is called on startup
38  * and curl_global_cleanup is called on shutdown.
39  * Having this environment enables curl to be used in a single or multi-threaded
40  * program safely.
41  */
42 class CurlEnvironment
43 {
44 public:
45   /**
46    * Constructor calls curl_global_init()
47    */
48   CurlEnvironment();
49
50   /**
51    * Destructor calls curl_global_cleanup()
52    */
53   ~CurlEnvironment();
54
55   // Moveable but not copyable
56
57   CurlEnvironment( const CurlEnvironment& ) = delete;
58   CurlEnvironment& operator=( const CurlEnvironment& ) = delete;
59   CurlEnvironment( CurlEnvironment&& ) = default;
60   CurlEnvironment& operator=( CurlEnvironment&& ) = default;
61
62   /**
63    * Locking function for libcurl with openssl
64    */
65   static void OnOpenSSLLocking( int mode, int n, const char* file, int line );
66
67   /**
68    * Gets thread id for libcurl with openssl
69    */
70   static unsigned long GetThreadId();
71
72   static void ConfigureCurlOptions( void* curlHandle, const std::string& url );
73
74   static void InitWriteFunction( void* curlHandle );
75
76 private:
77
78   void SetLockingFunction();
79
80   void UnsetLockingFunction();
81
82   static std::mutex* mMutexs;
83 };
84
85
86 /**
87  * Download a requested file into a memory buffer.
88  *
89  * @note Threading notes: This function can be called from multiple threads, however
90  * we must explicitly call curl_global_init() from a single thread before using curl
91  * as the global function calls are not thread safe.
92  *
93  * @param[in] url The requested file url
94  * @param[out] dataBuffer  A memory buffer object to be written with downloaded file data.
95  * @param[out] dataSize  The size of the memory buffer.
96  * @param[in] maximumAllowedSize The maxmimum allowed file size in bytes to download. E.g. for an Image file this may be 50 MB
97  * @return true on success, false on failure
98  *
99  */
100 bool DownloadRemoteFileIntoMemory( const std::string& url,
101                                    Dali::Vector<uint8_t>& dataBuffer,
102                                    size_t& dataSize,
103                                    size_t maximumAllowedSizeBytes );
104
105 } // namespace Network
106
107 } // namespace TizenPlatform
108
109 } // namespace Dali
110
111 #endif // __DALI_TIZEN_PLATFORM_RESOURCE_THREAD_IMAGE_H__