Merge "fix: use EINA_* booleans instread of TRUE/FALSE" into tizen
[platform/framework/web/wrt.git] / src / view / webkit / injected-bundle / injected_bundle_viewmodes_support.cpp
1 /*
2  * Copyright (c) 2012 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  * @file    injected_bundle_viewmodes_support.cpp
18  * @author  Jihoon Chung (jihoon.chung@samsung.com)
19  * @version 1.0
20  */
21
22 #include "injected_bundle_viewmodes_support.h"
23
24 #include <memory>
25 #include <map>
26 #include <set>
27 #include <string>
28
29 #include <dpl/log/secure_log.h>
30 #include <dpl/assert.h>
31 #include <dpl/string.h>
32 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
33
34 #include <WKString.h>
35 #include <WKBundlePage.h>
36 #include <WKBundlePagePrivate.h>
37
38 namespace InjectedBundle {
39 namespace {
40 const std::string VIEWMODE_TYPE_MAXIMIZED = "maximized";
41 const std::string VIEWMODE_TYPE_FULLSCREEN = "fullscreen";
42 const std::string VIEWMODE_TYPE_WINDOWED = "windowed";
43
44 typedef std::set<std::string> SupportViewmodesSet;
45 SupportViewmodesSet g_supportViewmodes = {VIEWMODE_TYPE_MAXIMIZED,
46                                           VIEWMODE_TYPE_FULLSCREEN,
47                                           VIEWMODE_TYPE_WINDOWED};
48 }
49
50 //Implementation class
51 class ViewmodesSupportImplementation
52 {
53   private:
54     typedef std::map<WKBundlePageRef, std::string> ViewmodesMap;
55     typedef ViewmodesMap::iterator ViewmodesIt;
56
57     bool m_initialized;
58
59     WrtDB::TizenAppId m_appId;
60     WrtDB::WindowModeList m_modeList;
61     ViewmodesMap m_initialViewmodeMap;
62     ViewmodesMap m_currentViewmodeMap;
63
64     bool isExisted(WKBundlePageRef page)
65     {
66         ViewmodesIt viewmodeIt = m_initialViewmodeMap.find(page);
67         if (viewmodeIt == m_initialViewmodeMap.end()) {
68             return false;
69         }
70         return true;
71     }
72
73     std::string getChangedViewmode(void)
74     {
75         if (!m_currentViewmodeMap.empty()) {
76             ViewmodesIt curIt = m_currentViewmodeMap.begin();
77             ViewmodesIt initIt = m_initialViewmodeMap.begin();
78             if (curIt->second != initIt->second) {
79               return curIt->second;
80             }
81         }
82         return std::string();
83     }
84
85     bool isSupportViewmode(const std::string& mode)
86     {
87         if (g_supportViewmodes.find(mode) == g_supportViewmodes.end()) {
88             return false;
89         }
90         return true;
91     }
92
93   public:
94     ViewmodesSupportImplementation(WrtDB::TizenAppId appId) :
95         m_initialized(false),
96         m_appId(appId)
97     {
98         WrtDB::WidgetDAOReadOnly dao(m_appId);
99         m_modeList = dao.getWindowModes();
100
101         m_initialized = true;
102     }
103
104     void initialize(WKBundlePageRef page)
105     {
106         _D("initialize");
107         if (!m_initialized) {
108             Assert(false);
109         }
110
111         if (isExisted(page)) {
112             _W("This page is already initialized");
113             return;
114         }
115
116         // set initial viewmode from manifest
117         std::string initViewmode = VIEWMODE_TYPE_MAXIMIZED;
118         FOREACH(it, m_modeList) {
119             std::string mode = DPL::ToUTF8String(*it);
120             if (g_supportViewmodes.find(mode) != g_supportViewmodes.end()) {
121                 initViewmode = mode;
122             }
123         }
124         m_initialViewmodeMap[page] = initViewmode;
125
126         // In case of current viewmode of chrome is changed,
127         // set to changed viewmode
128         std::string currentViewmode = getChangedViewmode();
129         if (currentViewmode.empty()) {
130             currentViewmode = initViewmode;
131         }
132         m_currentViewmodeMap[page] = currentViewmode;
133
134         WKBundlePageSetViewMode(page,
135                                 WKStringCreateWithUTF8CString(
136                                 currentViewmode.c_str()));
137     }
138
139     void deinitialize(WKBundlePageRef page)
140     {
141         _D("deinitialize");
142         if (!m_initialized) {
143             Assert(false);
144         }
145         m_initialViewmodeMap.erase(page);
146         m_currentViewmodeMap.erase(page);
147     }
148
149     void setViewmodes(WKBundlePageRef page, const std::string& mode)
150     {
151         if (!m_initialized) {
152             Assert(false);
153         }
154
155         m_currentViewmodeMap[page] = mode;
156         WKBundlePageSetViewMode(page,
157                                 WKStringCreateWithUTF8CString(
158                                     mode.c_str()));
159     }
160
161     void enterViewmodesAllPages(const std::string& mode)
162     {
163         _D("setViewmodesAllPages");
164         if (!m_initialized) {
165             Assert(false);
166         }
167         if (!isSupportViewmode(mode)) {
168             _W("Wrong viewmode : %s", mode.c_str());
169             return;
170         }
171
172         FOREACH(it, m_currentViewmodeMap) {
173             setViewmodes(it->first, mode);
174         }
175     }
176
177     void exitViewmodes(WKBundlePageRef page)
178     {
179         if (!m_initialized) {
180             Assert(false);
181         }
182
183         std::string mode = m_initialViewmodeMap[page];
184         m_currentViewmodeMap[page] = mode;
185         WKBundlePageSetViewMode(page,
186                                 WKStringCreateWithUTF8CString(
187                                     mode.c_str()));
188     }
189
190     void exitViewmodesAllPages(void)
191     {
192         _D("exitViewmodesAllPages");
193         if (!m_initialized) {
194             Assert(false);
195         }
196
197         FOREACH(it, m_currentViewmodeMap) {
198             exitViewmodes(it->first);
199         }
200     }
201 };
202
203 ViewmodesSupport::ViewmodesSupport(WrtDB::TizenAppId appId) :
204     m_impl(new ViewmodesSupportImplementation(appId))
205 {
206 }
207
208 ViewmodesSupport::~ViewmodesSupport()
209 {
210 }
211
212 void ViewmodesSupport::initialize(WKBundlePageRef page)
213 {
214     m_impl->initialize(page);
215 }
216
217 void ViewmodesSupport::deinitialize(WKBundlePageRef page)
218 {
219     m_impl->deinitialize(page);
220 }
221
222 void ViewmodesSupport::enterViewmodesAllPages(const std::string& mode)
223 {
224     m_impl->enterViewmodesAllPages(mode);
225 }
226
227 void ViewmodesSupport::exitViewmodesAllPages(void)
228 {
229     m_impl->exitViewmodesAllPages();
230 }
231 }  // namespace InjectedBundle