Merge "Update deprecated libprivilege-control API functions." into tizen
[platform/framework/native/appfw.git] / src / app / FAppAppResource.cpp
1 //
2 // Copyright (c) 2012 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  * @file        FAppAppResource.cpp
19  * @brief       This is the implementation for the AppResource class.
20  */
21
22 #include <FBaseResult.h>
23 #include <FBaseSysLog.h>
24 #include <FAppAppResource.h>
25
26 #include <FSec_AccessController.h>
27 #include "FApp_AppResourceImpl.h"
28 #include "FApp_AppInfo.h"
29 #ifdef _SINGLETON_CLEANUP
30 #include "FApp_LongevityManager.h"
31 #endif
32
33 using namespace Tizen::Base;
34 using namespace Tizen::Graphics;
35 using namespace Tizen::Security;
36
37 namespace Tizen { namespace App
38 {
39
40 AppResource::AppResource(void)
41         : __pAppResourceImpl(null)
42 {
43 }
44
45
46 AppResource::~AppResource(void)
47 {
48         delete __pAppResourceImpl;
49 }
50
51 result
52 AppResource::GetString(const String& resourceId, String& loadedString) const
53 {
54         SysAssertf(__pAppResourceImpl != null, "Not yet constructed. Construct() should be called before use.");
55         return __pAppResourceImpl->GetString(resourceId, loadedString);
56 }
57
58
59 Bitmap*
60 AppResource::GetBitmapN(const String& imgFilePath, BitmapPixelFormat pixelFormat) const
61 {
62         SysAssertf(__pAppResourceImpl != null, "Not yet constructed. Construct() should be called before use.");
63         return __pAppResourceImpl->GetBitmapN(imgFilePath, pixelFormat);
64 }
65
66
67 Bitmap*
68 AppResource::GetBitmapN(const String& imgFilePath) const
69 {
70         SysAssertf(__pAppResourceImpl != null, "Not yet constructed. Construct() should be called before use.");
71         return __pAppResourceImpl->GetBitmapN(imgFilePath);
72 }
73
74
75 AppResource*
76 AppResource::GetInstance(void)
77 {
78         static AppResource* pSelfAppResource = null;
79         if (pSelfAppResource == null)
80         {
81                 pSelfAppResource = _AppResourceImpl::GetInstanceN(APP_RESOURCE_DEFAULT);
82
83 #ifdef _SINGLETON_CLEANUP
84                 _LongevityManager::GetInstance().RegisterOwnership(*pSelfAppResource);
85 #endif
86         }
87
88 //      ClearLastResult();      // as this method does not register any exception.
89         return pSelfAppResource;
90 }
91
92 AppResource*
93 AppResource::LoadAppResource(const Tizen::Base::String& resourcePath)
94 {
95         SysTryReturn(NID_APP, !resourcePath.IsEmpty(), null, E_INVALID_ARG, "Resource path is empty");
96
97         AppResource* pAppResource = _AppResourceImpl::GetInstanceN(APP_RESOURCE_BY_LIBRARY_NAME, resourcePath);
98         if (GetLastResult() == E_DATA_NOT_FOUND)
99         {
100                 SetLastResult(E_FILE_NOT_FOUND);
101         }
102
103         return pAppResource;
104 }
105
106 AppResource*
107 AppResource::GetInstanceByAppId(const AppId& appId)
108 {
109         result r = E_SUCCESS;
110
111         r = _AccessController::CheckUserPrivilege(_PRV_APPSETTING);
112         SysTryReturn(NID_APP, r == E_SUCCESS, null, E_PRIVILEGE_DENIED, "[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method.");
113
114         SysTryReturn(NID_APP, !appId.IsEmpty(), null, E_APP_NOT_INSTALLED, "Invalid app id");
115
116         String packageId;
117         r = appId.SubString(0, 10, packageId);
118         SysTryReturn(NID_APP, !IsFailed(r), null,
119                         E_APP_NOT_INSTALLED, "[%s] Failed to get the package info", GetErrorMessage(E_APP_NOT_INSTALLED));
120         return _AppResourceImpl::GetInstanceN(APP_RESOURCE_BY_APP_ID, packageId);
121 }
122
123 result
124 AppResource::ReleaseInstanceByAppId(const AppId& appId)
125 {
126         result r = E_SUCCESS;
127
128         r = _AccessController::CheckUserPrivilege(_PRV_APPSETTING);
129         SysTryReturnResult(NID_APP, r == E_SUCCESS, E_PRIVILEGE_DENIED, "[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method.");
130
131         if (appId == _AppInfo::GetApplicationId() || appId == _AppInfo::GetPackageId())
132         {
133                 SysLog(NID_APP, "Ignoring key[%ls] with current instance", appId.GetPointer());
134                 return E_SUCCESS;
135         }
136
137         String packageId;
138         r = appId.SubString(0, 10, packageId);
139         SysTryReturn(NID_APP, !IsFailed(r), null,
140                         E_OBJ_NOT_FOUND, "[%s] Failed to get the package info", GetErrorMessage(E_OBJ_NOT_FOUND));
141         return _AppResourceImpl::ReleaseInstance(packageId);
142 }
143
144 } } // Tizen::App