Add features to download images over http protocol.
[platform/core/uifw/dali-adaptor.git] / platform-abstractions / slp / resource-loader / resource-thread-base.h
1 #ifndef __DALI_SLP_PLATFORM_RESOURCE_THREAD_BASE_H__
2 #define __DALI_SLP_PLATFORM_RESOURCE_THREAD_BASE_H__
3
4 /*
5  * Copyright (c) 2014 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 // INTERNAL INCLUDES
22 #include "resource-loader.h"
23 #include "resource-loading-client.h"
24 #include <dali/integration-api/platform-abstraction.h>
25 #include <dali/integration-api/resource-cache.h>
26
27 // EXTERNAL INCLUDES
28 #include <deque>
29 #include <boost/thread.hpp>
30
31 namespace Dali
32 {
33
34 namespace SlpPlatform
35 {
36
37 /**
38  * Resource loader worker thread
39  */
40 class ResourceThreadBase : public ResourceLoadingClient
41 {
42 public:
43   // typedefs and enums
44
45   /** Client threads send work to resource threads through Requests, for which
46    *  this type identifies the action to be taken on the resource thread. */
47   enum RequestType
48   {
49     /** Pull a resource out of the platform's file system. */
50     RequestLoad,
51     /** Pull a resource over http protocol. */
52     RequestDownload,
53     /** Pull a resource out of a memory buffer. */
54     RequestDecode,
55     /** Push a resource's data out to the file system. */
56     RequestSave
57   };
58
59   typedef std::pair<Integration::ResourceRequest, RequestType>  RequestInfo;
60   typedef std::deque<RequestInfo>                               RequestQueue;
61   typedef RequestQueue::iterator                                RequestQueueIter;
62
63 public:
64   // Constructor
65   ResourceThreadBase(ResourceLoader& resourceLoader);
66
67   // Destructor
68   virtual ~ResourceThreadBase();
69
70 protected:
71   void TerminateThread();
72
73 public:
74   /**
75    * Add a resource request to the back of the queue
76    * @param[in] request The requested resource/file url and attributes
77    * @param[in] type    Load or save flag
78    */
79   void AddRequest(const Integration::ResourceRequest& request, const RequestType type);
80
81   /**
82    * Cancel a resource request. Removes the request from the queue.
83    * @param[in] resourceId ID of the resource to be canceled
84    */
85   void CancelRequest(Integration::ResourceId  resourceId);
86
87   /**
88    * Pause starting new work in the background, but keep that work queued.
89    */
90   void Pause();
91
92   /**
93    * Resume processing tasks on background thread.
94    */
95   void Resume();
96
97
98 protected:
99   /**
100    * Main control loop for the thread.
101    * The thread is terminated when this function exits
102    */
103   void ThreadLoop();
104
105   /**
106    * Wait for an incoming resource request or termination
107    */
108   void WaitForRequests();
109
110   /**
111    * Process the resource request at the head of the queue
112    */
113   void ProcessNextRequest();
114
115   /**
116    * Install a logging function in to core for this thread.
117    */
118   void InstallLogging();
119
120   /**
121    * Uninstall a logging function.
122    */
123   void UninstallLogging();
124
125   /**
126    * Load a resource
127    * @param[in] request  The requested resource/file url and attributes
128    */
129   virtual void Load(const Integration::ResourceRequest& request) = 0;
130
131   /**
132    * Download a resource
133    * @param[in] request  The requested resource/file url and attributes
134    */
135   virtual void Download(const Integration::ResourceRequest& request);
136
137   /**
138    * Decode a resource exactly as if it were being loaded but source its data
139    * from a memory buffer attached directly to the request object.
140    * @param[in] request  The requested resource data and attributes
141    */
142   virtual void Decode(const Integration::ResourceRequest& request);
143
144   /**
145    * Save a resource
146    * @param[in] request  The requested resource/file url and attributes
147    */
148   virtual void Save(const Integration::ResourceRequest& request) = 0;
149
150   /**
151    * @brief Cancels current resource request if it matches the one latched to be cancelled.
152    *
153    * @copydoc ResourceLoadingClient::InterruptionPoint
154    */
155   virtual void InterruptionPoint() const;
156
157 protected:
158   ResourceLoader& mResourceLoader;
159   boost::thread* mThread;                       ///< thread instance
160   boost::condition_variable mCondition;         ///< condition variable
161   boost::mutex mMutex;                          ///< used to protect mQueue
162   RequestQueue mQueue;                          ///< Request queue
163 private:
164   Integration::ResourceId mCurrentRequestId;    ///< Current request, set by worker thread
165   volatile Integration::ResourceId mCancelRequestId; ///< Request to be cancelled on thread: written by external thread and read by worker.
166   bool mPaused;                                ///< Whether to process work in mQueue
167
168 #if defined(DEBUG_ENABLED)
169 public:
170   Integration::Log::Filter* mLogFilter;
171 #endif
172 }; // class ResourceThreadBase
173
174 } // namespace SlpPlatform
175
176 } // namespace Dali
177
178 #endif // __DALI_SLPPLATFORM_RESOURCE_THREAD_BASE_H__