[Release] wrt_0.8.260
[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 <widget_model.h>
29
30 #include <app.h>
31 #include <Elementary.h>
32 #include <EWebKit2.h>
33
34 namespace ViewModule {
35 namespace {
36 void setCustomUA(std::string customUA);
37 bool setAppInfo(WidgetModel* model, Evas_Object* wkView);
38 void printUA(Evas_Object* wkView);
39
40 bool setCustomUA(WidgetModel* model, Evas_Object* wkView)
41 {
42     std::string customUserAgent = model->SettingList.Get().getUserAgent();
43     if (customUserAgent.empty()) {
44         return false;
45     } else {
46         ewk_view_user_agent_set(wkView, customUserAgent.c_str());
47         return true;
48     }
49     return false;
50 }
51
52 bool setAppInfo(WidgetModel* model, Evas_Object* wkView)
53 {
54     std::string appInfo; // appname/appversion
55     char* name = NULL;
56     if (app_get_name(&name) == APP_ERROR_NONE) {
57         appInfo = name;
58         free(name);
59     } else {
60         _W("Fail to get app name");
61         if (name) {
62             free(name);
63         }
64         return false;
65     }
66
67     DPL::OptionalString version = model->Version.Get();
68     if (!version.IsNull()) {
69         std::string versionStr = DPL::ToUTF8String(*version);
70         if (versionStr.empty()) {
71             // version is empty
72             // skip to set version field
73         } else {
74             appInfo += "/";
75             appInfo += versionStr;
76         }
77     }
78     if (ewk_view_application_name_for_user_agent_set(wkView, appInfo.c_str())
79         == EINA_TRUE)
80     {
81         // verify
82         const char* info =
83             ewk_view_application_name_for_user_agent_get(wkView);
84         if (!info || !strlen(info) || appInfo != info) {
85             _W("Fail to verify app info in the UA");
86             return false;
87         }
88         return true;
89     } else {
90         _W("Fail to set app info to UA");
91         return false;
92     }
93 }
94
95 void printUA(Evas_Object* wkView)
96 {
97     const char* ua = ewk_view_user_agent_get(wkView);
98     _D("%s", ua);
99 }
100 } // anonymous namespace
101
102 void UserAgentSupport::setUserAgent(WidgetModel* model, Evas_Object* wkView)
103 {
104     Assert(model);
105     Assert(wkView);
106
107     // set custom UA
108     if (setCustomUA(model, wkView)) {
109         printUA(wkView);
110         return;
111     }
112
113     // In case of default UA, add appname/appversion
114     if (setAppInfo(model, wkView)) {
115         printUA(wkView);
116     } else {
117         // default UA
118         printUA(wkView);
119     }
120     return;
121 }
122
123 } // ViewModule