[Release] wrt_0.8.121
[platform/framework/web/wrt.git] / src / popup-process / PopupInvoker.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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 #include "PopupInvoker.h"
18 #include <sstream>
19 #include <unistd.h>
20 #include <stdio.h>
21 #include <dpl/log/log.h>
22 #include <dpl/waitable_handle.h>
23 #include <dpl/binary_queue.h>
24 #include <dpl/serialization.h>
25 #include <unistd.h>
26 #include <stdlib.h>
27 #include "PopupEnum.h"
28 #include "PopupSerializer.h"
29
30 namespace {
31 const char *POPUP_EXEC = "/usr/bin/wrt-popup";
32 }
33
34 namespace Wrt {
35 PopupInvoker::PopupInvoker() :
36     m_inputName(tmpnam(NULL)),
37     m_outputName(tmpnam(NULL))
38 {
39     Try
40     {
41         m_input.Create(m_inputName);
42         m_output.Create(m_outputName);
43         LogDebug("Pipes created");
44     }
45     Catch (DPL::Exception)
46     {
47         LogError("Cannot create pipes");
48     }
49 }
50
51 PopupInvoker::~PopupInvoker()
52 {
53     Try
54     {
55         m_input.Destroy(m_inputName);
56         m_output.Destroy(m_outputName);
57         LogDebug("Pipes destroyed");
58     }
59     Catch (DPL::Exception)
60     {
61         LogError("Cannot destroy pipes");
62     }
63 }
64
65 bool PopupInvoker::askYesNo(const std::string& title, const std::string& message)
66 {
67     Try
68     {
69         DPL::BinaryQueue data;
70         PopupSerializer::appendArg(YES_NO_PROMPT, data);
71         PopupSerializer::appendArg(title, data);
72         PopupSerializer::appendArg(message, data);
73         DPL::NamedInputPipe tmp;
74         tmp.Open(m_outputName);
75         m_output.Open(m_outputName);
76         m_input.Open(m_inputName);
77         m_output.Write(data, data.Size());
78
79         executePopup();
80
81         //Result from popup application is available. Read it.
82         DPL::BinaryQueueAutoPtr resultData =
83             m_input.Read(std::numeric_limits<std::size_t>::max());
84         const int result = PopupSerializer::getIntArg(*resultData);
85
86         LogDebug("Popup result is: " << result);
87
88         Assert(resultData->Empty());
89
90         tmp.Close();
91         m_input.Close();
92         m_output.Close();
93
94         return (!!result);
95     }
96     Catch(DPL::Exception)
97     {
98         LogError("error occured");
99     }
100
101     return false;
102 }
103
104 void PopupInvoker::executePopup()
105 {
106     pid_t pid = fork();
107     if (pid == -1)
108     {
109         //error occured
110         LogError("Cannot display popup!");
111         Assert(false);
112     }
113     if (pid == 0)
114     {
115         //child process
116         int ret = execl(POPUP_EXEC,
117                         POPUP_EXEC,
118                         m_outputName.c_str(),
119                         m_inputName.c_str(),
120                         NULL);
121         if (ret == -1) {
122             //execl returns -1 on error
123             LogError("Cannot display popup!");
124             Assert(false);
125         }
126     }
127
128     DPL::WaitableHandle handle = m_input.WaitableReadHandle();
129     DPL::WaitForSingleHandle(handle);
130 }
131 } // Wrt