Update wrt_0.8.89
[platform/framework/web/wrt.git] / src / view / webkit / view_logic_scheme_support.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  * @file       view_logic_scheme_support.cpp
18  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version    1.0
20  */
21
22 #include "view_logic_scheme_support.h"
23
24 #include <appcore-common.h>
25 #include <dpl/log/log.h>
26
27 #include <widget_model.h>
28
29 #include <common/scheme_action_map.h>
30
31 #include <ewk_view.h>
32 #include <WKString.h>
33 #include <WKType.h>
34 #include "view_logic_utils.h"
35
36 namespace {
37 char const * const TIZEN_SCHEME = "tizen";
38 char const * const TIZEN_EXIT = "tizen://exit";
39 char const * const TIZEN_HIDE = "tizen://hide";
40 char const * const TIZEN_ORIENTATION_LANDSCAPE =
41     "tizen://orientation.landscape";
42 char const * const TIZEN_ORIENTATION_PORTAIT = "tizen://orientation.portrait";
43 char const * const TIZEN_ORIENTATION_RESET = "tizen://orientation.reset";
44 char const * const TIZEN_CHANGE_USERAGNET = "tizen://changeUA";
45
46 const int ORIENTATION_LANDSCAPE_ANGLE = 270;
47 const int ORIENTATION_PORTAIT_ANGLE = 0;
48
49 static Eina_Bool exitAppIdlerCallback(void* /*data*/)
50 {
51     // webapp termination
52     elm_exit();
53     return ECORE_CALLBACK_CANCEL;
54 }
55
56 } // namespace
57
58 bool SchemeSupport::HandleUri(
59         const char* uri,
60         ViewModule::SchemeActionMap::NavigationContext context)
61 {
62     return ViewModule::SchemeActionMap::HandleUri(uri,
63                                                   context,
64                                                   m_type);
65 }
66
67 bool SchemeSupport::HandleTizenScheme(const char* uri,
68                                       Evas_Object* window,
69                                       WKPageRef page)
70 {
71     LogInfo("HandleTizenScheme called");
72     if (!uri) {
73         LogError("Empty uri");
74         return false;
75     }
76     if (!window) {
77         LogError("Empty window data");
78         return false;
79     }
80
81     if (strncmp(uri, TIZEN_EXIT, strlen(TIZEN_EXIT)) == 0) {
82         LogInfo("Tizen scheme: " << uri << " exit");
83         ecore_idler_add(exitAppIdlerCallback, NULL);
84         return true;
85     } else if (strncmp(uri, TIZEN_HIDE, strlen(TIZEN_HIDE)) == 0) {
86         LogInfo("Tizen scheme: " << uri << " hide");
87         elm_win_lower(window);
88         return true;
89     } else if (strncmp(uri,
90                        TIZEN_ORIENTATION_LANDSCAPE,
91                        strlen(TIZEN_ORIENTATION_LANDSCAPE)) == 0)
92     {
93         LogInfo("Tizen scheme: " << uri << " orientation landscape");
94         elm_win_rotation_with_resize_set(window, ORIENTATION_LANDSCAPE_ANGLE);
95         return true;
96     } else if (strncmp(uri,
97                        TIZEN_ORIENTATION_PORTAIT,
98                        strlen(TIZEN_ORIENTATION_PORTAIT)) == 0)
99     {
100         LogInfo("Tizen scheme: " << uri << " orientation portait");
101         elm_win_rotation_with_resize_set(window, ORIENTATION_PORTAIT_ANGLE);
102         return true;
103     } else if (strncmp(uri,
104                        TIZEN_ORIENTATION_RESET,
105                        strlen(TIZEN_ORIENTATION_RESET)) == 0)
106     {
107         LogInfo("Tizen scheme: " << uri << " reset");
108         elm_win_rotation_with_resize_set(window, ORIENTATION_PORTAIT_ANGLE);
109         return true;
110     } else if (strncmp(uri,
111                        TIZEN_CHANGE_USERAGNET,
112                        strlen(TIZEN_CHANGE_USERAGNET)) == 0)
113     {
114         LogInfo("Tizen scheme: " << uri << " change UA");
115         const char* userAgentString = strstr(uri, "=") + 1;
116         if (NULL == userAgentString) {
117             LogDebug("UA string is NULL");
118         } else {
119             LogDebug("Setting custom user agent as: " << userAgentString);
120             WKStringRef userAgentStringRef =
121                 WKStringCreateWithUTF8CString(userAgentString);
122             WKPageSetCustomUserAgent(page, userAgentStringRef);
123             WKRelease(userAgentStringRef);
124         }
125         return true;
126     }
127     return false;
128 }
129
130 bool SchemeSupport::filterURIByScheme(WKPageRef page,
131                                       WKFrameRef frame,
132                                       WKURLRequestRef request,
133                                       bool newWindow,
134                                       WidgetModel *model,
135                                       Evas_Object* window)
136 {
137     LogDebug("filterURIByScheme called");
138
139     DPL::OptionalString requestOptionalString =
140             ViewModule::Utils::toString(request);
141     if (requestOptionalString.IsNull()) {
142         // URI is null
143         // There is no reason, security by scheme block null uri
144         return true;
145     }
146
147     std::string url = DPL::ToUTF8String(*requestOptionalString);
148     bool mainFrame = WKFrameIsMainFrame(frame);
149
150     using namespace ViewModule::SchemeActionMap;
151     if (HandleTizenScheme(url.c_str(), window, page))
152     {
153         LogDebug("Scheme is tizen scheme");
154         return false;
155     }
156
157     NavigationContext ctx;
158     ctx = (mainFrame ? TOP_LEVEL : FRAME_LEVEL);
159     if (newWindow) {
160         if (model->Type.Get().appType ==
161             WrtDB::APP_TYPE_TIZENWEBAPP)
162         {
163             // In case of Tizen,
164             // policy of new window should be applied,
165             // regardless level of this frame
166             ctx = NEW_WINDOW;
167         } else if (model->Type.Get().appType ==
168                    WrtDB::APP_TYPE_WAC20)
169         {
170             // In case of WAC,
171             // policy of new window should be applied,
172             // only top level frame (this may be changed by WAC spec)
173             if (ctx == TOP_LEVEL) {
174                 ctx = NEW_WINDOW;
175             }
176         }
177     }
178
179     return HandleUri(url.c_str(), ctx);
180 }
181