Merge "fix: use EINA_* booleans instread of TRUE/FALSE" into tizen
[platform/framework/web/wrt.git] / src / view / webkit / view_logic_user_agent_support.cpp
1 /*
2  * Copyright (c) 2013 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_user_agent_support.cpp
18  * @author     Jihoon Chung (jihoon.chung@samsung.com)
19  * @version    1.0
20  */
21
22 #include "view_logic_user_agent_support.h"
23
24 #include <string>
25 #include <cstring>
26 #include <dpl/log/secure_log.h>
27 #include <dpl/string.h>
28 #include <dpl/availability.h>
29 #include <widget_model.h>
30
31 #include <app.h>
32 #include <Elementary.h>
33 #include <EWebKit2.h>
34
35 namespace ViewModule {
36 namespace {
37 void setCustomUA(std::string customUA);
38 bool setAppInfo(WidgetModel* model, Evas_Object* wkView);
39 void printUA(Evas_Object* wkView);
40
41 bool setCustomUA(WidgetModel* model, Evas_Object* wkView)
42 {
43     std::string customUserAgent = model->SettingList.Get().getUserAgent();
44     if (customUserAgent.empty()) {
45         return false;
46     } else {
47         ewk_view_user_agent_set(wkView, customUserAgent.c_str());
48         return true;
49     }
50     return false;
51 }
52
53 bool setAppInfo(WidgetModel* model, Evas_Object* wkView)
54 {
55     DPL_UNUSED_PARAM(model);
56     std::string appInfo; // appname/appversion
57     char* name = NULL;
58     if (app_get_name(&name) == APP_ERROR_NONE) {
59         appInfo = name;
60         free(name);
61     } else {
62         _W("Fail to get app name");
63         if (name) {
64             free(name);
65         }
66         return false;
67     }
68
69     DPL::OptionalString version = model->Version.Get();
70     if (!version.IsNull()) {
71         std::string versionStr = DPL::ToUTF8String(*version);
72         if (versionStr.empty()) {
73             // version is empty
74             // skip to set version field
75         } else {
76             appInfo += "/";
77             appInfo += versionStr;
78         }
79     }
80     if (ewk_view_application_name_for_user_agent_set(wkView, appInfo.c_str())
81         == EINA_TRUE)
82     {
83         // verify
84         const char* info =
85             ewk_view_application_name_for_user_agent_get(wkView);
86         if (!info || !strlen(info) || appInfo != info) {
87             _W("Fail to verify app info in the UA");
88             return false;
89         }
90         return true;
91     } else {
92         _W("Fail to set app info to UA");
93         return false;
94     }
95 }
96
97 void printUA(Evas_Object* wkView)
98 {
99     const char* ua = ewk_view_user_agent_get(wkView);
100     _D("%s", ua);
101 }
102 } // anonymous namespace
103
104 void UserAgentSupport::setUserAgent(WidgetModel* model, Evas_Object* wkView)
105 {
106     Assert(model);
107     Assert(wkView);
108
109     // set custom UA
110     if (setCustomUA(model, wkView)) {
111         printUA(wkView);
112         return;
113     }
114
115     // In case of default UA, add appname/appversion
116     if (setAppInfo(model, wkView)) {
117         printUA(wkView);
118     } else {
119         // default UA
120         printUA(wkView);
121     }
122     return;
123 }
124
125 } // ViewModule