Implement geolocation permission callback
authorSeungkeun Lee <sngn.lee@samsung.com>
Wed, 29 Apr 2015 07:40:43 +0000 (16:40 +0900)
committerSeungkeun Lee <sngn.lee@samsung.com>
Thu, 30 Apr 2015 04:24:53 +0000 (13:24 +0900)
Change-Id: Ia5af9042c3c6e8c8bdb64ef74cdd2d2eaa282af2

src/runtime/web_application.cc
src/runtime/web_application.h
src/runtime/web_view.h
src/runtime/web_view_impl.cc
src/runtime/web_view_impl.h

index 4b3e252..72bd68d 100755 (executable)
@@ -60,6 +60,8 @@ namespace {
   const char* kFullscreenFeature = "fullscreen";
   const char* kNotificationPrivilege =
       "http://tizen.org/privilege/notification";
+  const char* kLocationPrivilege =
+      "http://tizen.org/privilege/location";
 
 bool FindPrivilege(wrt::ApplicationData* app_data,
                    const std::string& privilege) {
@@ -457,6 +459,30 @@ void WebApplication::OnNotificationPermissionRequest(
   // TODO(sngn.lee): create popup and show
 }
 
+void WebApplication::OnGeolocationPermissionRequest(
+    WebView* view,
+    const std::string& url,
+    std::function<void(bool)> result_handler) {
+  // TODO(sngn.lee): check from DB url was already allowed
+  // if(check already allow or denied) {
+  //   result_handler(true);
+  //   return;
+  // }
+  // Local Domain: Grant permission if defined, otherwise block execution.
+  // Remote Domain: Popup user prompt if defined, otherwise block execution.
+  if (!FindPrivilege(app_data_.get(), kLocationPrivilege)) {
+    result_handler(false);
+    return;
+  }
+
+  if (utils::StartsWith(url, "file://")) {
+    result_handler(true);
+    return;
+  }
+
+  // TODO(sngn.lee): create popup and show
+}
+
 
 
 }  // namespace wrt
index 89a458e..c194823 100755 (executable)
@@ -59,6 +59,10 @@ class WebApplication : public WebView::EventListener {
       WebView* view,
       const std::string& url,
       std::function<void(bool)> result_handler);
+  virtual void OnGeolocationPermissionRequest(
+      WebView* view,
+      const std::string& url,
+      std::function<void(bool)> result_handler);
 
 
   std::string uuid() const { return uuid_; }
index 2bad6c5..c79e379 100755 (executable)
@@ -47,6 +47,10 @@ class WebView {
         WebView* /*view*/,
         const std::string& /*url*/,
         std::function<void(bool)> /*result_handler*/) {}
+    virtual void OnGeolocationPermissionRequest(
+        WebView* /*view*/,
+        const std::string& /*url*/,
+        std::function<void(bool)> /*result_handler*/) {}
   };
 
   WebView(wrt::NativeWindow* window, Ewk_Context* context);
index d51e0b7..f976080 100755 (executable)
@@ -99,13 +99,13 @@ void WebViewImpl::Initialize() {
   InitWindowCreateCallback();
   InitFullscreenCallback();
   InitNotificationPermissionCallback();
+  InitGeolocationPermissionCallback();
 
   // TODO(sngn.lee): "request,certificate,confirm" certification popup
   // TODO(sngn.lee): "notification,show"
   // TODO(sngn.lee): "notification,cancel"
   // TODO(sngn.lee): "protocolhandler,registration,requested"
   //                  custom protocol handler
-  // TODO(sngn.lee): ewk_view_geolocation_permission_callback_set
   // TODO(sngn.lee): ewk_view_user_media_permission_callback_set
 
   // Show webview
@@ -143,6 +143,10 @@ void WebViewImpl::Deinitialize() {
       ewk_view_,
       NULL,
       NULL);
+  ewk_view_geolocation_permission_callback_set(
+      ewk_view_,
+      NULL,
+      NULL);
   window_->RemoveRotationHandler(rotation_handler_id_);
 }
 
@@ -549,6 +553,43 @@ void WebViewImpl::InitNotificationPermissionCallback() {
                                                 this);
 }
 
+void WebViewImpl::InitGeolocationPermissionCallback() {
+  auto permission_callback = [](
+      Evas_Object*,
+      Ewk_Geolocation_Permission_Request* request,
+      void* user_data) {
+    LoggerD("Geolocation Permission Request");
+    WebViewImpl* self = static_cast<WebViewImpl*>(user_data);
+    if (self == NULL || self->listener_ == NULL) {
+      ewk_geolocation_permission_reply(request, EINA_FALSE);
+      return EINA_TRUE;
+    }
+    ewk_geolocation_permission_request_suspend(request);
+
+    const Ewk_Security_Origin* ewk_origin =
+        ewk_geolocation_permission_request_origin_get(request);
+    auto result_handler = [request](bool result) {
+      LoggerD("Geolocation Permission Result : %d", result);
+      ewk_geolocation_permission_reply(request, result);
+    };
+
+    std::stringstream url;
+    url << ewk_security_origin_protocol_get(ewk_origin)
+        << "://"
+        << ewk_security_origin_host_get(ewk_origin)
+        << ":"
+        << ewk_security_origin_port_get(ewk_origin);
+
+    self->listener_->OnGeolocationPermissionRequest(
+        self->view_,
+        url.str(),
+        result_handler);
+    return EINA_TRUE;
+  };
+  ewk_view_geolocation_permission_callback_set(ewk_view_,
+                                               permission_callback,
+                                               this);
+}
 
 std::string WebViewImpl::GetUrl() {
   return std::string(ewk_view_url_get(ewk_view_));
index 43893ca..3debadd 100755 (executable)
@@ -54,6 +54,7 @@ class WebViewImpl {
   void InitWindowCreateCallback();
   void InitFullscreenCallback();
   void InitNotificationPermissionCallback();
+  void InitGeolocationPermissionCallback();
 
   NativeWindow* window_;
   Ewk_Context* context_;