Create string tightly when retrive string from cbhm callback event
[framework/web/webkit-efl.git] / Source / WebKit2 / UIProcess / API / efl / ewk_geolocation.cpp
1 /*
2    Copyright (C) 2012 Samsung Electronics
3
4     This library is free software; you can redistribute it and/or
5     modify it under the terms of the GNU Library General Public
6     License as published by the Free Software Foundation; either
7     version 2 of the License, or (at your option) any later version.
8
9     This library is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12     Library General Public License for more details.
13
14     You should have received a copy of the GNU Library General Public License
15     along with this library; see the file COPYING.LIB.  If not, write to
16     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17     Boston, MA 02110-1301, USA.
18 */
19
20 #include "config.h"
21 #include "ewk_geolocation.h"
22
23 #if ENABLE(TIZEN_GEOLOCATION)
24 #include "WKAPICast.h"
25 #include "WKGeolocationManager.h"
26 #include "WKGeolocationPermissionRequest.h"
27 #include "WKGeolocationPosition.h"
28 #include "WKRetainPtr.h"
29 #include "ewk_context_private.h"
30 #include "ewk_geolocation_private.h"
31 #include "ewk_view_private.h"
32
33 #include <Evas.h>
34 #include <location/locations.h>
35 #include <wtf/text/CString.h>
36
37 using namespace WebKit;
38
39 struct _Ewk_Geolocation {
40     WKRetainPtr<WKGeolocationManagerRef> geolocationManager;
41     location_manager_h locationManager;
42     bool started;
43 };
44
45 struct _Ewk_Geolocation_Permission_Request {
46     WKRetainPtr<WKGeolocationPermissionRequestRef> requestRef;
47     Evas_Object* ewkView;
48     Ewk_Security_Origin* origin;
49     bool isDecided;
50     bool isSuspended;
51
52     _Ewk_Geolocation_Permission_Request(Evas_Object* ewkView, WKRetainPtr<WKGeolocationPermissionRequestRef> geolocationPermissionRequestRef, WKSecurityOriginRef originRef)
53         : requestRef(geolocationPermissionRequestRef)
54         , ewkView(ewkView)
55         , isDecided(false)
56         , isSuspended(false)
57     {
58         origin = createSecurityOrigin(originRef);
59     }
60
61     ~_Ewk_Geolocation_Permission_Request()
62     {
63         deleteSecurityOrigin(origin);
64     }
65 };
66
67 static void ewkGeolocationPositionChangedCallback(double latitude, double longitude, double altitude, time_t timestamp, void* user_data)
68 {
69     TIZEN_LOGI("");
70     Ewk_Geolocation* ewkGeolocation = static_cast<Ewk_Geolocation*>(user_data);
71     if(!timestamp || !ewkGeolocation)
72         return;
73
74     location_accuracy_level_e level;
75     double horizontal;
76     double vertical;
77
78     location_manager_get_last_accuracy(ewkGeolocation->locationManager, &level, &horizontal, &vertical);
79
80     WKRetainPtr<WKGeolocationPositionRef> position(AdoptWK, WKGeolocationPositionCreate(timestamp, latitude, longitude, horizontal));
81     WKGeolocationManagerProviderDidChangePosition(ewkGeolocation->geolocationManager.get(), position.get());
82 }
83
84 Ewk_Geolocation* ewkGeolocationCreateGeolocation(WKGeolocationManagerRef geolocationManager)
85 {
86     Ewk_Geolocation* ewkGeolocation = new Ewk_Geolocation();
87     ewkGeolocation->geolocationManager = geolocationManager;
88     ewkGeolocation->started = false;
89
90     return ewkGeolocation;
91 }
92
93 void ewkGeolocationDeleteGeolocation(Ewk_Geolocation* ewkGeolocation)
94 {
95     if (ewkGeolocation->started)
96         ewkGeolocationStopLocationManager(ewkGeolocation);
97
98     delete ewkGeolocation;
99 }
100
101 Ewk_Geolocation_Permission_Request* ewkGeolocationCreatePermissionRequest(Evas_Object* ewkView, WKGeolocationPermissionRequestRef geolocationPermissionRequestRef, WKSecurityOriginRef originRef)
102 {
103     return new Ewk_Geolocation_Permission_Request(ewkView, geolocationPermissionRequestRef, originRef);
104 }
105
106 void ewkGeolocationDeletePermissionRequestList(Eina_List* requestList)
107 {
108     void* data = 0;
109     EINA_LIST_FREE(requestList, data)
110         delete (static_cast<Ewk_Geolocation_Permission_Request*>(data));
111 }
112
113 bool ewkGeolocationIsPermissionRequestSuspended(const Ewk_Geolocation_Permission_Request* permissionRequest)
114 {
115     return permissionRequest->isSuspended;
116 }
117
118 bool ewkGeolocationIsPermissionRequestDecided(const Ewk_Geolocation_Permission_Request* permissionRequest)
119 {
120     return permissionRequest->isDecided;
121 }
122
123 bool ewkGeolocationStartLocationManager(Ewk_Geolocation* ewkGeolocation)
124 {
125     EINA_SAFETY_ON_NULL_RETURN_VAL(ewkGeolocation, false);
126
127     location_error_e ret = static_cast<location_error_e>(location_manager_create(LOCATIONS_METHOD_HYBRID, &ewkGeolocation->locationManager));
128     if (ret != LOCATIONS_ERROR_NONE) {
129         TIZEN_LOGE("location_manager_create error (%d)", ret);
130         return false;
131     }
132
133     ret = static_cast<location_error_e>(location_manager_set_position_updated_cb(ewkGeolocation->locationManager, ewkGeolocationPositionChangedCallback, 1, const_cast<Ewk_Geolocation*>(ewkGeolocation)));
134     if (ret != LOCATIONS_ERROR_NONE){
135         TIZEN_LOGE("location_manager_set_position_updated_cb error(%d)", ret);
136         location_manager_destroy(ewkGeolocation->locationManager);
137         return false;
138     }
139
140     ret = static_cast<location_error_e>(location_manager_start(ewkGeolocation->locationManager));
141     if (ret != LOCATIONS_ERROR_NONE) {
142         TIZEN_LOGE("location_manager_start error(%d)", ret);
143         location_manager_unset_position_updated_cb(ewkGeolocation->locationManager);
144         location_manager_destroy(ewkGeolocation->locationManager);
145         return false;
146     }
147
148     ewkGeolocation->started = true;
149
150     return true;
151 }
152
153 void ewkGeolocationStopLocationManager(Ewk_Geolocation* ewkGeolocation)
154 {
155     EINA_SAFETY_ON_NULL_RETURN(ewkGeolocation);
156
157     if (!ewkGeolocation->started) {
158         TIZEN_LOGE("started error");
159         return;
160     }
161
162     location_error_e ret = static_cast<location_error_e>(location_manager_stop(ewkGeolocation->locationManager));
163     if (ret != LOCATIONS_ERROR_NONE) {
164         TIZEN_LOGE("location_manager_stop error(%d)", ret);
165         location_manager_destroy(ewkGeolocation->locationManager);
166     }
167
168     ret = static_cast<location_error_e>(location_manager_unset_position_updated_cb(ewkGeolocation->locationManager));
169     if (ret != LOCATIONS_ERROR_NONE) {
170         TIZEN_LOGE("location_manager_unset_position_updated_cb error(%d)", ret);
171         return;
172     }
173
174     ret = static_cast<location_error_e>(location_manager_destroy(ewkGeolocation->locationManager));
175     if (ret != LOCATIONS_ERROR_NONE) {
176         TIZEN_LOGE("location_manager_destroy error(%d)", ret);
177         return;
178     }
179
180     ewkGeolocation->started = false;
181 }
182 #endif
183
184 const Ewk_Security_Origin* ewk_geolocation_permission_request_origin_get(const Ewk_Geolocation_Permission_Request* permissionRequest)
185 {
186 #if ENABLE(TIZEN_GEOLOCATION)
187     EINA_SAFETY_ON_NULL_RETURN_VAL(permissionRequest, 0);
188
189     return permissionRequest->origin;
190 #else
191     UNUSED_PARAM(permissionRequest);
192     return 0;
193 #endif
194 }
195
196 Eina_Bool ewk_geolocation_permission_request_set(Ewk_Geolocation_Permission_Request* permissionRequest, Eina_Bool allow)
197 {
198 #if ENABLE(TIZEN_GEOLOCATION)
199     EINA_SAFETY_ON_NULL_RETURN_VAL(permissionRequest, false);
200     TIZEN_LOGI("allow(%d)", allow);
201
202     permissionRequest->isDecided = true;
203
204     allow ? WKGeolocationPermissionRequestAllow(permissionRequest->requestRef.get()) : WKGeolocationPermissionRequestDeny(permissionRequest->requestRef.get());
205
206     ewkViewDeleteGeolocationPermission(permissionRequest->ewkView, permissionRequest);
207     delete permissionRequest;
208
209     return true;
210 #else
211     UNUSED_PARAM(permissionRequest);
212     UNUSED_PARAM(allow);
213     return false;
214 #endif
215 }
216
217 void ewk_geolocation_permission_request_suspend(Ewk_Geolocation_Permission_Request* permissionRequest)
218 {
219 #if ENABLE(TIZEN_GEOLOCATION)
220     EINA_SAFETY_ON_NULL_RETURN(permissionRequest);
221     TIZEN_LOGI("request(%d) is suspended", permissionRequest);
222
223     permissionRequest->isSuspended = true;
224 #else
225     UNUSED_PARAM(permissionRequest);
226 #endif
227 }