f1bf7d2ae23ec3076ed512a4bee9012dfd261702
[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
33 namespace ViewModule {
34
35 using namespace SecurityOriginDB;
36 using namespace ViewModule::SecurityOriginSupportUtil;
37
38 namespace {
39 const char* const WEB_STORAGE_CREATE_ASK_BODY_PREFIX = "Do you want to allow ";
40 const char* const WEB_STORAGE_CREATE_ASK_BODY =
41     " to use up to ";
42 const char* const WEB_STORAGE_CREATE_ASK_BODY_POSTFIX =
43     "MB of storage?";
44 const char* const WEB_STORAGE_CREATE_ASK_CHECK = "Don't ask again";
45
46 // function declare
47 void askUserForWebStorageCreatePermission(
48     Evas_Object* window,
49     PermissionData* data);
50 static void popupCallback(void* data, Evas_Object* obj, void* eventInfo);
51
52 void askUserForWebStorageCreatePermission(
53     Evas_Object* window,
54     PermissionData* data)
55 {
56     LogDebug("askUserForWebStorageCreatePermission called");
57     PermissionData* permData = static_cast<PermissionData*>(data);
58     Ewk_Context_Exceeded_Quota* exceededQuota =
59             static_cast<Ewk_Context_Exceeded_Quota*>(permData->m_data);
60
61     std::ostringstream size;
62     size << ewk_context_web_database_exceeded_quota_expected_usage_get(exceededQuota) /
63         (1024 * 1024);
64
65     std::string body =
66         WEB_STORAGE_CREATE_ASK_BODY_PREFIX +
67         DPL::ToUTF8String(data->m_originData.origin.host) +
68         WEB_STORAGE_CREATE_ASK_BODY +
69         size.str() +
70         WEB_STORAGE_CREATE_ASK_BODY_POSTFIX;
71
72     Evas_Object* popup = createPopup(window,
73                                      body.c_str(),
74                                      WEB_STORAGE_CREATE_ASK_CHECK,
75                                      popupCallback,
76                                      data);
77
78     if (popup == NULL) {
79         LogError("Fail to create popup object");
80         delete data;
81         return;
82     } else {
83         evas_object_show(popup);
84     }
85 }
86
87 void popupCallback(void* data, Evas_Object* obj, void* /*eventInfo*/)
88 {
89     LogDebug("popupCallback");
90     Assert(data);
91     PermissionData* permData = static_cast<PermissionData*>(data);
92     Ewk_Context_Exceeded_Quota* exceededQuota =
93             static_cast<Ewk_Context_Exceeded_Quota*>(permData->m_data);
94
95     Evas_Object* popup = getPopup(obj);
96     Result result = getResult(obj);
97
98     if (result != RESULT_UNKNOWN) {
99         permData->m_originDao->setSecurityOriginData(permData->m_originData, result);
100     }
101     if  (result == RESULT_ALLOW_ALWAYS || result == RESULT_ALLOW_ONCE) {
102         ewk_context_web_database_exceeded_quota_new_quota_set(
103             exceededQuota,
104             ewk_context_web_database_exceeded_quota_expected_usage_get(
105                 exceededQuota) +
106             10*(1024*1024)); //10MB
107     }
108
109     delete permData;
110     evas_object_hide(popup);
111     evas_object_del(popup);
112 }
113 } // namespace
114
115 void WebStorageSupport::webStorageCreatePermissionRequest(
116     Evas_Object* window,
117     SecurityOriginDAO* securityOriginDAO,
118     void* data)
119 {
120     LogDebug("webStorageCreatePermissionRequest called");
121     Assert(securityOriginDAO);
122     Assert(data);
123     Ewk_Context_Exceeded_Quota* exceededQuota =
124         static_cast<Ewk_Context_Exceeded_Quota*>(data);
125     Ewk_Security_Origin* ewkOrigin =
126         ewk_context_web_database_exceeded_quota_security_origin_get(
127             exceededQuota);
128     Assert(ewkOrigin);
129
130     SecurityOriginData securityOriginData(
131         FEATURE_WEB_DATABASE,
132         Origin(
133             DPL::FromUTF8String(ewk_security_origin_protocol_get(ewkOrigin)),
134             DPL::FromUTF8String(ewk_security_origin_host_get(ewkOrigin)),
135             ewk_security_origin_port_get(ewkOrigin)));
136
137     // check cache database
138     Result result = securityOriginDAO->getResult(securityOriginData);
139     if (RESULT_ALLOW_ONCE == result || RESULT_ALLOW_ALWAYS == result) {
140         LogDebug("allow");
141         ewk_context_web_database_exceeded_quota_new_quota_set(
142                 exceededQuota,
143                 ewk_context_web_database_exceeded_quota_expected_usage_get(
144                     exceededQuota) +
145                 10*(1024*1024)); //10MB
146         return;
147     } else if (RESULT_DENY_ONCE == result || RESULT_DENY_ALWAYS == result) {
148         LogDebug("deny");
149         return;
150     }
151
152     // ask to user
153     PermissionData* permissionData =
154         new PermissionData(securityOriginDAO,
155                            securityOriginData,
156                            exceededQuota);
157     askUserForWebStorageCreatePermission(window, permissionData);
158     return;
159 }
160 } // namespace ViewModule