2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
17 #include <sys/types.h>
18 #include <sys/inotify.h>
24 #include <tzplatform_config.h>
25 #include <klay/auth/user.h>
27 #include <dpm/pil/policy-context.h>
28 #include <dpm/pil/policy-model.h>
29 #include <dpm/pil/policy-storage.h>
31 #ifndef KRATE_SUPPORTED
32 #include <dpm/pil/app-bundle.h>
33 #include <dpm/pil/launchpad.h>
35 #include <krate/krate.h>
37 #define NAME_PATTERN "^[A-Za-z_][A-Za-z0-9_.-]*"
41 std::regex krateNamePattern(NAME_PATTERN);
43 bool foreachKrateCallback(const char* name, void* user_data)
45 auto pList = (std::vector<std::string>*)user_data;
46 pList->push_back(name);
50 bool isAllowedName(const std::string& name)
52 if (!std::regex_match(name, krateNamePattern)) {
58 runtime::User user(name);
60 } catch (runtime::Exception& e) {
70 class Zone : public AbstractPolicyProvider {
72 int create(const std::string& name, const std::string& setupWizAppid);
73 int remove(const std::string& name);
74 int getState(const std::string& name);
75 std::vector<std::string> enumerate(int state);
78 int Zone::create(const std::string& name, const std::string& setupWizAppid)
80 #ifndef KRATE_SUPPORTED
81 if (!isAllowedName(name)) {
82 return TIZEN_ERROR_NOT_PERMITTED;
87 bundle.add("id", "krate-create");
88 bundle.add("user-data", name);
90 Launchpad launchpad(rmi::Service::getPeerUid());
91 launchpad.launch("org.tizen.dpm-syspopup", bundle);
92 } catch (runtime::Exception& e) {
99 return TIZEN_ERROR_NOT_PERMITTED;
103 int Zone::remove(const std::string& name)
105 if (getState(name) == 0) {
106 return TIZEN_ERROR_NOT_PERMITTED;
109 #ifndef KRATE_SUPPORTED
112 bundle.add("id", "krate-remove");
113 bundle.add("user-data", name);
115 Launchpad launchpad(rmi::Service::getPeerUid());
116 launchpad.launch("org.tizen.dpm-syspopup", bundle);
117 } catch (runtime::Exception& e) {
124 return TIZEN_ERROR_NOT_PERMITTED;
128 int Zone::getState(const std::string& name)
130 #ifndef KRATE_SUPPORTED
131 krate_state_e state = (krate_state_e)0;
132 krate_manager_h krate_manager;
134 krate_manager_create(&krate_manager);
135 krate_manager_get_krate_state(krate_manager, name.c_str(), &state);
136 krate_manager_destroy(krate_manager);
144 std::vector<std::string> Zone::enumerate(int state)
146 std::vector<std::string> list;
147 #ifndef KRATE_SUPPORTED
148 krate_manager_h krate_manager;
150 krate_manager_create(&krate_manager);
151 krate_manager_foreach_name(krate_manager, (krate_state_e)state, foreachKrateCallback, &list);
152 krate_manager_destroy(krate_manager);
160 #define PRIVILEGE "http://tizen.org/privilege/dpm.zone"
162 AbstractPolicyProvider *PolicyFactory(PolicyControlContext& context)
164 Zone *policy = new Zone();
166 context.expose(policy, PRIVILEGE, (int)(Zone::create)(std::string, std::string));
167 context.expose(policy, PRIVILEGE, (int)(Zone::remove)(std::string));
168 context.expose(policy, "", (int)(Zone::getState)(std::string));
169 context.expose(policy, "", (std::vector<std::string>)(Zone::enumerate)(int));