2191704fdde1b28890c31278c628045ad6e5c078
[platform/framework/web/wrt.git] / src / view / webkit / view_logic_web_storage_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_web_storage_support.cpp
18  * @author  Jihoon Chung (jihoon.chung@samsung.com)
19  */
20
21 #include "view_logic_web_storage_support.h"
22
23 #include <string>
24 #include <sstream>
25 #include <dpl/log/log.h>
26 #include <dpl/assert.h>
27 #include <wrt-commons/security-origin-dao/security_origin_dao_types.h>
28 #include <wrt-commons/security-origin-dao/security_origin_dao.h>
29 #include <EWebKit2.h>
30 #include <common/view_logic_security_origin_support.h>
31 #include <Elementary.h>
32 #include <widget_string.h>
33
34 namespace ViewModule {
35 using namespace SecurityOriginDB;
36 using namespace ViewModule::SecurityOriginSupportUtil;
37
38 namespace {
39 struct WebStoragePermissionData {
40     WebStorageSupport::ewkQuotaReply m_replyEAPI;
41     Evas_Object* m_ewkView;
42     WebStoragePermissionData(WebStorageSupport::ewkQuotaReply replyEAPI,
43                              Evas_Object* ewkView) :
44         m_replyEAPI(replyEAPI),
45         m_ewkView(ewkView)
46     {}
47 };
48
49 const char* const WEB_STORAGE_CREATE_ASK_BODY_PREFIX = "Do you want to allow ";
50 const char* const WEB_STORAGE_CREATE_ASK_BODY =
51     " to use up to ";
52 const char* const WEB_STORAGE_CREATE_ASK_BODY_POSTFIX =
53     "MB of storage?";
54
55 // function declare
56 void askUserForWebStorageCreatePermission(
57     Evas_Object* window,
58     unsigned long long expectedQuota,
59     PermissionData* data);
60 static void popupCallback(void* data, Evas_Object* obj, void* eventInfo);
61
62 void askUserForWebStorageCreatePermission(
63     Evas_Object* window,
64     unsigned long long expectedQuota,
65     PermissionData* data)
66 {
67     LogDebug("askUserForWebStorageCreatePermission called");
68     std::ostringstream size;
69     size << expectedQuota / (1024 * 1024);
70
71     std::string body =
72         WEB_STORAGE_CREATE_ASK_BODY_PREFIX +
73         DPL::ToUTF8String(data->m_originData.origin.host) +
74         WEB_STORAGE_CREATE_ASK_BODY +
75         size.str() +
76         WEB_STORAGE_CREATE_ASK_BODY_POSTFIX;
77
78     Evas_Object* popup = createPopup(window,
79                                      body.c_str(),
80                                      WRT_BODY_REMEMBER_PREFERENCE,
81                                      popupCallback,
82                                      data);
83
84     if (popup == NULL) {
85         LogError("Fail to create popup object");
86         delete data;
87         return;
88     } else {
89         evas_object_show(popup);
90     }
91 }
92
93 void popupCallback(void* data, Evas_Object* obj, void* /*eventInfo*/)
94 {
95     LogDebug("popupCallback");
96     Assert(data);
97     PermissionData* permData = static_cast<PermissionData*>(data);
98     WebStoragePermissionData* webStoragePermissionData =
99         static_cast<WebStoragePermissionData*>(permData->m_data);
100     Evas_Object* ewkView =
101         static_cast<Evas_Object*>(webStoragePermissionData->m_ewkView);
102
103     Evas_Object* popup = getPopup(obj);
104     Result result = getResult(obj);
105
106     if (result != RESULT_UNKNOWN) {
107         permData->m_originDao->setSecurityOriginData(permData->m_originData,
108                                                      result);
109     }
110     Eina_Bool ret =
111         (result == RESULT_ALLOW_ALWAYS || result == RESULT_ALLOW_ONCE) ?
112             EINA_TRUE : EINA_FALSE;
113     webStoragePermissionData->m_replyEAPI(ewkView, ret);
114
115     delete webStoragePermissionData;
116     delete permData;
117     evas_object_hide(popup);
118     evas_object_del(popup);
119 }
120 } // namespace
121
122 void WebStorageSupport::createPermissionRequest(
123     Evas_Object* window,
124     SecurityOriginDB::SecurityOriginDAO* securityOriginDAO,
125     Evas_Object* ewkView,
126     Ewk_Security_Origin* ewkOrigin,
127     unsigned long long expectedQuota,
128     ewkQuotaReply replyEAPI)
129 {
130     LogDebug("createPermissionRequest called");
131     Assert(securityOriginDAO);
132     Assert(ewkOrigin);
133     Assert(ewkView);
134     SecurityOriginData securityOriginData(
135         WrtDB::FEATURE_WEB_DATABASE,
136         Origin(
137             DPL::FromUTF8String(ewk_security_origin_protocol_get(ewkOrigin)),
138             DPL::FromUTF8String(ewk_security_origin_host_get(ewkOrigin)),
139             ewk_security_origin_port_get(ewkOrigin)));
140
141     // check cache database
142     Result result = securityOriginDAO->getResult(securityOriginData);
143     if (result != RESULT_UNKNOWN) {
144         Eina_Bool ret =
145             (result == RESULT_ALLOW_ALWAYS || result == RESULT_ALLOW_ONCE) ?
146                 EINA_TRUE : EINA_FALSE;
147         replyEAPI(ewkView, ret);
148         return;
149     }
150
151     // ask to user
152     WebStoragePermissionData* webStoragePermissionData =
153         new WebStoragePermissionData(replyEAPI, ewkView);
154     PermissionData* permissionData =
155         new PermissionData(securityOriginDAO,
156                            securityOriginData,
157                            webStoragePermissionData);
158     askUserForWebStorageCreatePermission(window,
159                                          expectedQuota,
160                                          permissionData);
161     return;
162 }
163 } // namespace ViewModule