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