2 * Copyright (c) 2011 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 * @file popup-runner.cpp
18 * @author Janusz Kozerski (j.kozerski@samsung.com)
23 * This is just a example pop-up that shows how to use a new C-API in
25 * This pop-up should be re-implemented by members of NGWAP.
33 #include <sys/types.h>
36 #include <dpl/log/log.h>
38 #include "popup-runner.h"
40 namespace { // anonymous
41 const char *POPUP_EXEC = "/usr/bin/wrt-popup-ace-runtime";
43 void _ace_params_serializer (const ace_param_list_t* ace_param_list,
44 Wrt::Popup::BinaryStream *stream)
46 LogDebug("_ace_params_serializer");
48 if (NULL == ace_param_list || NULL == stream) {
49 LogError("*ace_param_list or *stream is NULL; return");
52 LogDebug("Param count : " << ace_param_list->count);
54 // serialize ace_param_list->count;
55 size_t count = static_cast <size_t>(ace_param_list->count);
56 DPL::Serialization::Serialize(*stream, count);
58 for (size_t i = 0; i < count; ++i) {
59 std::string name(ace_param_list->items[i].name);
60 DPL::Serialization::Serialize(*stream, name);
62 std::string value(ace_param_list->items[i].value);
63 DPL::Serialization::Serialize(*stream, value);
66 } // anonymous namespace
70 // BinaryStream class implementation
71 void BinaryStream::Read(size_t num, void * bytes)
73 size_t max_size = m_data.size();
74 for (size_t i = 0; i < num; ++i) {
75 if (i + m_readPosition >= max_size) {
78 static_cast <unsigned char*>(bytes)[i] = m_data[i + m_readPosition];
80 m_readPosition += num;
83 void BinaryStream::Write(size_t num, const void * bytes)
85 for (size_t i = 0; i < num; ++i) {
86 m_data.push_back(static_cast <const unsigned char*>(bytes)[i]);
90 BinaryStream::BinaryStream()
95 BinaryStream::~BinaryStream() {}
97 const unsigned char* BinaryStream::char_pointer() const
102 size_t BinaryStream::size() const
104 return m_data.size();
108 ace_return_t run_popup(
109 ace_popup_t popup_type,
110 const ace_resource_t resource_name,
111 const ace_session_id_t session_id,
112 const ace_param_list_t* ace_param_list,
113 ace_widget_handle_t handle,
114 ace_bool_t* validation_result)
116 if (NULL == resource_name ||
117 NULL == session_id ||
118 NULL == ace_param_list ||
119 NULL == validation_result)
121 LogError("run_popup : ACE_INVALID_ARGUMENTS");
122 return ACE_INVALID_ARGUMENTS;
124 LogDebug("popup_type : " << popup_type);
125 LogDebug("resource_name : " << resource_name);
126 LogDebug("session_id : " << session_id);
127 LogDebug("widget handle : " << handle);
129 int popup_type_int = static_cast <int>(popup_type);
130 char *resource_name_char = static_cast <char *>(resource_name);
131 char *session_id_char = static_cast <char *>(session_id);
132 int handle_int = static_cast <int>(handle);
135 Wrt::Popup::BinaryStream stream;
136 DPL::Serialization::Serialize(stream, popup_type_int);
138 std::string resource_name_str(resource_name_char);
139 DPL::Serialization::Serialize(stream, resource_name_str);
141 std::string session_id_str(session_id_char);
142 DPL::Serialization::Serialize(stream, session_id_str);
144 DPL::Serialization::Serialize(stream, handle_int);
146 _ace_params_serializer(ace_param_list, &stream);
148 int fd_send_to_child[2];
149 int fd_send_to_parent[2];
152 if (0 != pipe(fd_send_to_child)) {
153 LogError("Cannot create pipes!");
154 return ACE_INTERNAL_ERROR;
156 if (0 != pipe(fd_send_to_parent)) {
157 LogError("Cannot create pipes!");
158 return ACE_INTERNAL_ERROR;
161 if ((childpid = fork()) == -1) {
162 LogError("Fork() ERROR");
163 return ACE_ACE_UNKNOWN_ERROR;
166 if (childpid == 0) { // Child process
169 // read data from parent
170 close(fd_send_to_child[1]);
172 // send data to parent
173 close(fd_send_to_parent[0]);
175 std::stringstream pipe_in_buff;
176 std::stringstream pipe_out_buff;
177 pipe_in_buff << fd_send_to_parent[1];
178 pipe_out_buff << fd_send_to_child[0];
179 std::string pipe_in = pipe_in_buff.str();
180 std::string pipe_out = pipe_out_buff.str();
183 "Passed file descriptors: " << fd_send_to_child[0] << ", " <<
184 fd_send_to_parent[1]);
186 if (execl(POPUP_EXEC, POPUP_EXEC, pipe_out.c_str(), pipe_in.c_str(),
189 LogError("execlp FAILED");
192 LogError("This should not happened!!!");
193 } // end of child process - from now you can use DEBUG LOGS
194 else { // Parent process
197 int buff_size = 1024;
198 char result[buff_size];
199 // send data to child
200 close(fd_send_to_child[0]);
203 LogDebug("Sending message to popup-bin process");
205 TEMP_FAILURE_RETRY(write(fd_send_to_child[1], stream.char_pointer(),
208 LogError("Write to pipe failed!");
209 return ACE_INTERNAL_ERROR;
211 close(fd_send_to_child[1]); // cleanup
212 LogDebug("Message has been sent");
214 // read data from child
215 close(fd_send_to_parent[1]);
219 LogDebug("STATUS EXIT ON POPUP (CHILD): " << status);
225 case ACE_INVALID_ARGUMENTS:
226 LogDebug("ACE_INVALID_ARGUMENTS");
227 close(fd_send_to_parent[0]);
228 return static_cast <ace_return_t>(status);
231 case ACE_INTERNAL_ERROR:
232 LogDebug("ACE_INTERNAL_ERROR");
233 close(fd_send_to_parent[0]);
234 return static_cast <ace_return_t>(status);
237 case ACE_ACE_UNKNOWN_ERROR:
238 LogDebug("ACE_ACE_UNKNOWN_ERROR");
239 close(fd_send_to_parent[0]);
240 return static_cast <ace_return_t>(status);
244 LogDebug("UNKNOWN_ERROR");
245 close(fd_send_to_parent[0]);
246 status = (int) ACE_ACE_UNKNOWN_ERROR;
247 return static_cast <ace_return_t>(status);
252 count = TEMP_FAILURE_RETRY(read(fd_send_to_parent[0], result, buff_size));
253 close(fd_send_to_parent[0]); // cleanup
257 BinaryStream stream_in;
258 int validation_result_int;
259 stream_in.Write(count, result);
260 LogDebug("RESULT FROM POPUP (CHILD) : [ " << count << " ]");
261 DPL::Deserialization::Deserialize(stream_in, validation_result_int);
262 *validation_result = static_cast <ace_bool_t>(validation_result_int);
264 LogDebug("validation_result :");
265 switch (*validation_result) {
267 LogDebug("ACE_FALSE");
270 LogDebug("ACE_TRUE");
273 LogDebug("UNKNOWN - DEFAULT");
277 LogDebug("count = " << count);
278 LogDebug("UNKNOWN_ERROR");
279 return ACE_ACE_UNKNOWN_ERROR;
282 LogDebug("popup-runner: EXIT");
283 return (ace_return_t) status;
286 LogError("This should not happend!!!");
287 return ACE_ACE_UNKNOWN_ERROR;