2 Copyright (C) 2012 Samsung Electronics
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.
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.
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.
21 #include "ewk_geolocation.h"
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"
34 #include <location/locations.h>
35 #include <wtf/text/CString.h>
37 using namespace WebKit;
39 struct _Ewk_Geolocation {
40 WKRetainPtr<WKGeolocationManagerRef> geolocationManager;
41 location_manager_h locationManager;
45 struct _Ewk_Geolocation_Permission_Request {
46 WKRetainPtr<WKGeolocationPermissionRequestRef> requestRef;
48 Ewk_Security_Origin* origin;
52 _Ewk_Geolocation_Permission_Request(Evas_Object* ewkView, WKRetainPtr<WKGeolocationPermissionRequestRef> geolocationPermissionRequestRef, WKSecurityOriginRef originRef)
53 : requestRef(geolocationPermissionRequestRef)
58 origin = createSecurityOrigin(originRef);
61 ~_Ewk_Geolocation_Permission_Request()
63 deleteSecurityOrigin(origin);
67 static void ewkGeolocationPositionChangedCallback(double latitude, double longitude, double altitude, time_t timestamp, void* user_data)
70 Ewk_Geolocation* ewkGeolocation = static_cast<Ewk_Geolocation*>(user_data);
71 if(!timestamp || !ewkGeolocation)
74 location_accuracy_level_e level;
78 location_manager_get_last_accuracy(ewkGeolocation->locationManager, &level, &horizontal, &vertical);
80 WKRetainPtr<WKGeolocationPositionRef> position(AdoptWK, WKGeolocationPositionCreate(timestamp, latitude, longitude, horizontal));
81 WKGeolocationManagerProviderDidChangePosition(ewkGeolocation->geolocationManager.get(), position.get());
84 Ewk_Geolocation* ewkGeolocationCreateGeolocation(WKGeolocationManagerRef geolocationManager)
86 Ewk_Geolocation* ewkGeolocation = new Ewk_Geolocation();
87 ewkGeolocation->geolocationManager = geolocationManager;
88 ewkGeolocation->started = false;
90 return ewkGeolocation;
93 void ewkGeolocationDeleteGeolocation(Ewk_Geolocation* ewkGeolocation)
95 if (ewkGeolocation->started)
96 ewkGeolocationStopLocationManager(ewkGeolocation);
98 delete ewkGeolocation;
101 Ewk_Geolocation_Permission_Request* ewkGeolocationCreatePermissionRequest(Evas_Object* ewkView, WKGeolocationPermissionRequestRef geolocationPermissionRequestRef, WKSecurityOriginRef originRef)
103 return new Ewk_Geolocation_Permission_Request(ewkView, geolocationPermissionRequestRef, originRef);
106 void ewkGeolocationDeletePermissionRequestList(Eina_List* requestList)
109 EINA_LIST_FREE(requestList, data)
110 delete (static_cast<Ewk_Geolocation_Permission_Request*>(data));
113 bool ewkGeolocationIsPermissionRequestSuspended(const Ewk_Geolocation_Permission_Request* permissionRequest)
115 return permissionRequest->isSuspended;
118 bool ewkGeolocationIsPermissionRequestDecided(const Ewk_Geolocation_Permission_Request* permissionRequest)
120 return permissionRequest->isDecided;
123 bool ewkGeolocationStartLocationManager(Ewk_Geolocation* ewkGeolocation)
125 EINA_SAFETY_ON_NULL_RETURN_VAL(ewkGeolocation, false);
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);
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);
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);
148 ewkGeolocation->started = true;
153 void ewkGeolocationStopLocationManager(Ewk_Geolocation* ewkGeolocation)
155 EINA_SAFETY_ON_NULL_RETURN(ewkGeolocation);
157 if (!ewkGeolocation->started) {
158 TIZEN_LOGE("started error");
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);
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);
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);
180 ewkGeolocation->started = false;
184 const Ewk_Security_Origin* ewk_geolocation_permission_request_origin_get(const Ewk_Geolocation_Permission_Request* permissionRequest)
186 #if ENABLE(TIZEN_GEOLOCATION)
187 EINA_SAFETY_ON_NULL_RETURN_VAL(permissionRequest, 0);
189 return permissionRequest->origin;
191 UNUSED_PARAM(permissionRequest);
196 Eina_Bool ewk_geolocation_permission_request_set(Ewk_Geolocation_Permission_Request* permissionRequest, Eina_Bool allow)
198 #if ENABLE(TIZEN_GEOLOCATION)
199 EINA_SAFETY_ON_NULL_RETURN_VAL(permissionRequest, false);
200 TIZEN_LOGI("allow(%d)", allow);
202 permissionRequest->isDecided = true;
204 allow ? WKGeolocationPermissionRequestAllow(permissionRequest->requestRef.get()) : WKGeolocationPermissionRequestDeny(permissionRequest->requestRef.get());
206 ewkViewDeleteGeolocationPermission(permissionRequest->ewkView, permissionRequest);
207 delete permissionRequest;
211 UNUSED_PARAM(permissionRequest);
217 void ewk_geolocation_permission_request_suspend(Ewk_Geolocation_Permission_Request* permissionRequest)
219 #if ENABLE(TIZEN_GEOLOCATION)
220 EINA_SAFETY_ON_NULL_RETURN(permissionRequest);
221 TIZEN_LOGI("request(%d) is suspended", permissionRequest);
223 permissionRequest->isSuspended = true;
225 UNUSED_PARAM(permissionRequest);