9366fa1a0d241b11760fe2b65b1a2af4cc69fd30
[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 #include <widget_model.h>
27 #include <common/scheme_action_map.h>
28 #include <EWebKit2.h>
29
30 namespace {
31 char const * const TIZEN_SCHEME = "tizen";
32 char const * const TIZEN_EXIT = "tizen://exit";
33 char const * const TIZEN_HIDE = "tizen://hide";
34 char const * const TIZEN_ORIENTATION_LANDSCAPE =
35     "tizen://orientation.landscape";
36 char const * const TIZEN_ORIENTATION_PORTAIT = "tizen://orientation.portrait";
37 char const * const TIZEN_ORIENTATION_RESET = "tizen://orientation.reset";
38 char const * const TIZEN_CHANGE_USERAGNET = "tizen://changeUA";
39
40 const int ORIENTATION_LANDSCAPE_ANGLE = 270;
41 const int ORIENTATION_PORTAIT_ANGLE = 0;
42
43 static Eina_Bool exitAppIdlerCallback(void* /*data*/)
44 {
45     // webapp termination
46     ewk_shutdown();
47     elm_exit();
48     return ECORE_CALLBACK_CANCEL;}
49
50 } // namespace
51
52 bool SchemeSupport::HandleUri(
53         const char* uri,
54         ViewModule::SchemeActionMap::NavigationContext context)
55 {
56     return ViewModule::SchemeActionMap::HandleUri(uri,
57                                                   context,
58                                                   m_type);
59 }
60
61 bool SchemeSupport::HandleTizenScheme(const char* uri,
62                                       Evas_Object* window,
63                                       Evas_Object* wkView)
64 {
65     LogInfo("HandleTizenScheme called");
66     if (!uri) {
67         LogError("Empty uri");
68         return false;
69     }
70     if (!window) {
71         LogError("Empty window data");
72         return false;
73     }
74
75     if (strncmp(uri, TIZEN_EXIT, strlen(TIZEN_EXIT)) == 0) {
76         LogInfo("Tizen scheme: " << uri << " exit");
77         ecore_idler_add(exitAppIdlerCallback, NULL);
78         return true;
79     } else if (strncmp(uri, TIZEN_HIDE, strlen(TIZEN_HIDE)) == 0) {
80         LogInfo("Tizen scheme: " << uri << " hide");
81         elm_win_lower(window);
82         return true;
83     } else if (strncmp(uri,
84                        TIZEN_ORIENTATION_LANDSCAPE,
85                        strlen(TIZEN_ORIENTATION_LANDSCAPE)) == 0)
86     {
87         LogInfo("Tizen scheme: " << uri << " orientation landscape");
88         elm_win_rotation_with_resize_set(window, ORIENTATION_LANDSCAPE_ANGLE);
89         return true;
90     } else if (strncmp(uri,
91                        TIZEN_ORIENTATION_PORTAIT,
92                        strlen(TIZEN_ORIENTATION_PORTAIT)) == 0)
93     {
94         LogInfo("Tizen scheme: " << uri << " orientation portait");
95         elm_win_rotation_with_resize_set(window, ORIENTATION_PORTAIT_ANGLE);
96         return true;
97     } else if (strncmp(uri,
98                        TIZEN_ORIENTATION_RESET,
99                        strlen(TIZEN_ORIENTATION_RESET)) == 0)
100     {
101         LogInfo("Tizen scheme: " << uri << " reset");
102         elm_win_rotation_with_resize_set(window, ORIENTATION_PORTAIT_ANGLE);
103         return true;
104     } else if (strncmp(uri,
105                        TIZEN_CHANGE_USERAGNET,
106                        strlen(TIZEN_CHANGE_USERAGNET)) == 0)
107     {
108         LogInfo("Tizen scheme: " << uri << " change UA");
109         const char* userAgentString = strstr(uri, "=");
110         if (NULL == userAgentString) {
111             LogDebug("UA string is NULL");
112         } else {
113             userAgentString++;
114             LogDebug("Setting custom user agent as: " << userAgentString);
115             ewk_view_user_agent_set(wkView, userAgentString);
116         }
117         return true;
118     }
119     return false;
120 }
121
122 bool SchemeSupport::filterURIByScheme(Ewk_Policy_Decision* policyDecision,
123                                       bool newWindow,
124                                       WidgetModel* model,
125                                       Evas_Object* window,
126                                       Evas_Object* wkView)
127 {
128     LogDebug("filterURIByScheme called");
129     Assert(policyDecision);
130     Assert(model);
131
132     const char* url = ewk_policy_decision_url_get(policyDecision);
133     if (NULL == url) {
134         // URI is null
135         // There is no reason, security by scheme block null uri
136         return true;
137     }
138
139     bool mainFrame =
140         ewk_frame_is_main_frame(ewk_policy_decision_frame_get(policyDecision));
141
142     using namespace ViewModule::SchemeActionMap;
143     if (HandleTizenScheme(url, window, wkView))
144     {
145         LogDebug("Scheme is tizen scheme");
146         return false;
147     }
148
149     NavigationContext ctx;
150     ctx = (mainFrame ? TOP_LEVEL : FRAME_LEVEL);
151     if (newWindow) {
152         if (model->Type.Get().appType ==
153             WrtDB::APP_TYPE_TIZENWEBAPP)
154         {
155             // In case of Tizen,
156             // policy of new window should be applied,
157             // regardless level of this frame
158             ctx = NEW_WINDOW;
159         } else if (model->Type.Get().appType ==
160                    WrtDB::APP_TYPE_WAC20)
161         {
162             // In case of WAC,
163             // policy of new window should be applied,
164             // only top level frame (this may be changed by WAC spec)
165             if (ctx == TOP_LEVEL) {
166                 ctx = NEW_WINDOW;
167             }
168         }
169     }
170
171     return HandleUri(url, ctx);
172 }
173