Add features to download images over http protocol.
[platform/core/uifw/dali-adaptor.git] / platform-abstractions / slp / resource-loader / resource-bitmap-requester.cpp
1 /*
2  * Copyright (c) 2014 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 #include "resource-bitmap-requester.h"
19 #include <dali/integration-api/resource-cache.h>
20
21 using namespace Dali::Integration;
22
23 namespace Dali
24 {
25 namespace SlpPlatform
26 {
27
28 ResourceBitmapRequester::ResourceBitmapRequester( ResourceLoader& resourceLoader )
29 : ResourceRequesterBase( resourceLoader )
30 {
31   mThreadImageLocal  = new ResourceThreadImage( resourceLoader, false );
32   mThreadImageRemote  = new ResourceThreadImage( resourceLoader, true );
33   mThreadDistanceField = new ResourceThreadDistanceField( resourceLoader );
34 }
35
36 ResourceBitmapRequester::~ResourceBitmapRequester()
37 {
38   delete mThreadImageLocal;
39   delete mThreadImageRemote;
40   delete mThreadDistanceField;
41 }
42
43 void ResourceBitmapRequester::Pause()
44 {
45   mThreadImageLocal->Pause();
46   mThreadImageRemote->Pause();
47   mThreadDistanceField->Pause();
48 }
49
50 void ResourceBitmapRequester::Resume()
51 {
52   mThreadImageLocal->Resume();
53   mThreadImageRemote->Resume();
54   mThreadDistanceField->Resume();
55 }
56
57 void ResourceBitmapRequester::LoadResource( Integration::ResourceRequest& request )
58 {
59   DALI_ASSERT_DEBUG( (0 != dynamic_cast<BitmapResourceType*>(request.GetType())) && "Only requsts for bitmap resources can ever be routed to ResourceBitmapRequester.\n");
60   BitmapResourceType* resType = static_cast<BitmapResourceType*>(request.GetType());
61   if( resType )
62   {
63     // Work out what thread to decode / load the image on:
64     ResourceThreadBase* const localImageThread = mThreadImageLocal;
65     ResourceThreadBase* const remoteImageThread = mThreadImageRemote;
66     ResourceThreadBase* const distanceFieldThread = mThreadDistanceField ;
67     ResourceThreadBase* workerThread;
68
69     // Work out if the resource is in memory, a file, or in a remote server:
70     ResourceThreadBase::RequestType requestType;
71     if( request.GetResource().Get() )
72     {
73       requestType = ResourceThreadBase::RequestDecode;
74       workerThread = localImageThread;
75     }
76     else
77     {
78       const std::string& resourcePath = request.GetPath();
79       if( resourcePath.length() > 7 && strncasecmp( resourcePath.c_str(), "http://", 7 ) == 0 )
80       {
81         requestType = ResourceThreadBase::RequestDownload;
82         workerThread = remoteImageThread;
83       }
84       else
85       {
86         requestType = ResourceThreadBase::RequestLoad;
87         workerThread = localImageThread;
88       }
89     }
90
91     if( resType->imageAttributes.IsDistanceField() )
92     {
93       workerThread = distanceFieldThread;
94     }
95
96     // Dispatch the job to the right thread:
97     workerThread->AddRequest( request, requestType );
98   }
99 }
100
101 Integration::LoadStatus ResourceBitmapRequester::LoadFurtherResources( Integration::ResourceRequest& request, LoadedResource partialResource )
102 {
103   // Nothing to do
104   return RESOURCE_COMPLETELY_LOADED;
105 }
106
107 void ResourceBitmapRequester::SaveResource(const Integration::ResourceRequest& request )
108 {
109   // Nothing to do
110 }
111
112 void ResourceBitmapRequester::CancelLoad(Integration::ResourceId id, Integration::ResourceTypeId typeId)
113 {
114   mThreadImageLocal->CancelRequest(id);
115   mThreadImageRemote->CancelRequest(id);
116   mThreadDistanceField->CancelRequest(id);
117 }
118
119 } // SlpPlatform
120 } // Dali