[Websetting] Added throwing exception when runtime
authorKonrad Zdunczyk <k.zdunczyk@samsung.com>
Thu, 15 Jan 2015 15:11:01 +0000 (16:11 +0100)
committerPawel Sikorski <p.sikorski@samsung.com>
Mon, 19 Jan 2015 10:19:49 +0000 (19:19 +0900)
is wrt-service

Change-Id: Iff7474d9a03d2c0506c36c6ec59bbe3a24b8ae23
Signed-off-by: Konrad Zdunczyk <k.zdunczyk@samsung.com>
19 files changed:
src/tizen-wrt.gyp
src/websetting/web_setting.cc [deleted file]
src/websetting/web_setting.gyp [deleted file]
src/websetting/web_setting.h [deleted file]
src/websetting/web_setting_api.js [deleted file]
src/websetting/web_setting_extension.cc [deleted file]
src/websetting/web_setting_extension.h [deleted file]
src/websetting/web_setting_extension_utils.h [deleted file]
src/websetting/web_setting_instance.cc [deleted file]
src/websetting/web_setting_instance.h [deleted file]
src/websetting/websetting.cc [new file with mode: 0644]
src/websetting/websetting.gyp [new file with mode: 0644]
src/websetting/websetting.h [new file with mode: 0644]
src/websetting/websetting_api.js [new file with mode: 0644]
src/websetting/websetting_extension.cc [new file with mode: 0644]
src/websetting/websetting_extension.h [new file with mode: 0644]
src/websetting/websetting_extension_utils.h [new file with mode: 0644]
src/websetting/websetting_instance.cc [new file with mode: 0644]
src/websetting/websetting_instance.h [new file with mode: 0644]

index 138ab77dfc23de86b56c93321abae3b44accc1a0..837896e121beeedbf6453d290ea7897301e3331b 100644 (file)
@@ -17,6 +17,7 @@
         #'datasync/datasync.gyp:*',
         'archive/archive.gyp:*',
         'exif/exif.gyp:*',
+        'websetting/websetting.gyp:*',
       ],
       'conditions': [
         [
diff --git a/src/websetting/web_setting.cc b/src/websetting/web_setting.cc
deleted file mode 100644 (file)
index 41df642..0000000
+++ /dev/null
@@ -1,100 +0,0 @@
-// Copyright (c) 2014 Samsung Electronics Co., Ltd. All Rights Reserved
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "web_setting/web_setting.h"
-
-#include <sys/types.h>
-#include <unistd.h>
-#include <utility>
-
-#include "web_setting/web_setting_extension_utils.h"
-
-namespace {
-
-const char kRuntimeServiceName[] =  "org.crosswalkproject.Runtime1";
-const char kRuntimeRunningManagerPath[] = "/running1";
-const char kRuntimeRunningAppInterface[] =
-    "org.crosswalkproject.Running.Application1";
-
-// The runtime process exports object for each running app on the session bus.
-GDBusProxy* CreateRunningAppProxy(const std::string& app_id) {
-  GError* error = NULL;
-  GDBusConnection* connection =
-      g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
-  if (!connection) {
-    std::cerr << "Couldn't get the session bus connection: "
-              << error->message << std::endl;
-    g_error_free(error);
-    return NULL;
-  }
-
-  std::string path =
-      std::string(kRuntimeRunningManagerPath) + "/" + app_id;
-  // Every application id contains '.' character and since object path
-  // is created from application id it also contains '.' character.
-  // The d-bus proxy doesn't accept '.' character in object path
-  // And that is why the substantiation is needed here.
-  std::replace(path.begin(), path.end(), '.', '_');
-  GDBusProxy* proxy = g_dbus_proxy_new_sync(
-      connection, G_DBUS_PROXY_FLAGS_NONE, NULL, kRuntimeServiceName,
-      path.c_str(), kRuntimeRunningAppInterface, NULL, &error);
-  if (!proxy) {
-    std::cerr << "Couldn't create proxy for " << kRuntimeRunningAppInterface
-              << ": " << error->message << std::endl;
-    g_error_free(error);
-    return NULL;
-  }
-
-  return proxy;
-}
-
-}  // namespace
-
-WebSetting::WebSetting(const std::string& app_id)
-    : app_id_(app_id),
-      running_app_proxy_(NULL) {
-}
-
-WebSetting::~WebSetting() {
-  if (running_app_proxy_)
-    g_object_unref(running_app_proxy_);
-}
-
-std::unique_ptr<picojson::value> WebSetting::RemoveAllCookies() {
-  if (!running_app_proxy_) {
-    if (!(running_app_proxy_ = CreateRunningAppProxy(app_id_)))
-      return CreateResultMessage(WebApiAPIErrors::UNKNOWN_ERR);
-  }
-  GError* error = NULL;
-  GVariant* result = g_dbus_proxy_call_sync(
-      running_app_proxy_, "RemoveAllCookies", NULL,
-      G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
-  if (!result) {
-    std::cerr << "Fail to call 'RemoveuserAgentAllCookies':"
-              << error->message << std::endl;
-    g_error_free(error);
-    return CreateResultMessage(WebApiAPIErrors::UNKNOWN_ERR);
-  }
-  return CreateResultMessage();
-}
-
-std::unique_ptr<picojson::value> WebSetting::SetUserAgentString(
-    const std::string& user_agent) {
-  if (!running_app_proxy_) {
-    if (!(running_app_proxy_ = CreateRunningAppProxy(app_id_)))
-      return CreateResultMessage(WebApiAPIErrors::UNKNOWN_ERR);
-  }
-  GError* error = NULL;
-  GVariant* result = g_dbus_proxy_call_sync(
-      running_app_proxy_, "SetUserAgentString",
-      g_variant_new("(s)", user_agent.c_str()),
-      G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
-  if (!result) {
-    std::cerr << "Fail to call 'SetUserAgentString':"
-              << error->message << std::endl;
-    g_error_free(error);
-    return CreateResultMessage(WebApiAPIErrors::UNKNOWN_ERR);
-  }
-  return CreateResultMessage();
-}
diff --git a/src/websetting/web_setting.gyp b/src/websetting/web_setting.gyp
deleted file mode 100644 (file)
index fe4bc14..0000000
+++ /dev/null
@@ -1,29 +0,0 @@
-{
-  'includes':[
-    '../common/common.gypi',
-  ],
-  'targets': [
-    {
-      'target_name': 'tizen_websetting',
-      'type': 'loadable_module',
-      'sources': [
-        'web_setting.cc',
-        'web_setting.h',
-        'web_setting_api.js',
-        'web_setting_extension.cc',
-        'web_setting_extension.h',
-        'web_setting_extension_utils.h',
-        'web_setting_instance.cc',
-        'web_setting_instance.h',
-      ],
-      'includes': [
-        '../common/pkg-config.gypi',
-      ],
-      'variables': {
-        'packages': [
-          'glib-2.0',
-        ]
-      },
-    },
-  ],
-}
diff --git a/src/websetting/web_setting.h b/src/websetting/web_setting.h
deleted file mode 100644 (file)
index 55a2e41..0000000
+++ /dev/null
@@ -1,28 +0,0 @@
-// Copyright (c) 2014 Samsung Electronics Co., Ltd. All Rights Reserved
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef WEB_SETTING_WEB_SETTING_H_
-#define WEB_SETTING_WEB_SETTING_H_
-
-#include <gio/gio.h>
-#include <memory>
-#include <string>
-
-#include "common/picojson.h"
-
-class WebSetting {
- public:
-  explicit WebSetting(const std::string& app_id);
-  ~WebSetting();
-
-  std::unique_ptr<picojson::value> RemoveAllCookies();
-  std::unique_ptr<picojson::value> SetUserAgentString(
-      const std::string& user_agent);
-
- private:
-  std::string app_id_;
-  GDBusProxy* running_app_proxy_;
-};
-
-#endif  // WEB_SETTING_WEB_SETTING_H_
diff --git a/src/websetting/web_setting_api.js b/src/websetting/web_setting_api.js
deleted file mode 100644 (file)
index cdcba26..0000000
+++ /dev/null
@@ -1,96 +0,0 @@
-var asyncCallbacks = {
-  _next_id: 0,
-  _callbacks: {},
-  key: '_callback',
-
-  // Return a callback ID number which will be contained by the native message.
-  setup: function(callback) {
-    var id = ++this._next_id;
-    this._callbacks[id] = callback;
-    return id;
-  },
-
-  dispatch: function(m) {
-    var id = m[this.key];
-    var callback = this._callbacks[id];
-    callback.call(null, m);
-    delete this._callbacks[id];
-  }
-};
-
-extension.setMessageListener(function(msg) {
-  var m = JSON.parse(msg);
-  if (typeof m[asyncCallbacks.key] === 'number') {
-    asyncCallbacks.dispatch(m);
-  } else {
-    if (m[asyncCallbacks.key] === appInfoEventCallbacks.key)
-      appInfoEventCallbacks.dispatch(m);
-    else
-      console.error('unexpected message received' + msg);
-  }
-});
-
-// Post async message to extension with callbackId saved. The extension will return
-// a message with the same callbackId to the callback set in setMessageListener.
-function postMessage(msg, callbackId) {
-  msg[asyncCallbacks.key] = callbackId;
-  extension.postMessage(JSON.stringify(msg));
-}
-
-function defineReadOnlyProperty(object, key, value) {
-  Object.defineProperty(object, key, {
-    enumerable: true,
-    writable: false,
-    value: value
-  });
-}
-
-exports.setUserAgentString = function(userAgent,
-                                      successCallback, errorCallback) {
-  if (arguments.length > 0 && typeof userAgent !== 'string' ||
-      arguments.length > 1 && (successCallback !== null &&
-      typeof successCallback !== 'function') || arguments.length > 2 &&
-      (errorCallback !== null && typeof errorCallback !== 'function')) {
-    throw new tizen.WebAPIException(tizen.WebAPIException.TYPE_MISMATCH_ERR);
-  }
-
-  var callbackId = asyncCallbacks.setup(function(result) {
-    if (result.error !== null) {
-      if (!errorCallback) {
-        return;
-      }
-      return errorCallback(new tizen.WebAPIError(result.error));
-    }
-    if (successCallback !== null) {
-      return successCallback(result.data);
-    }
-    return;
-  });
-
-  var msg = { cmd: 'SetUserAgentString', userAgentStr: userAgent };
-  postMessage(msg, callbackId);
-};
-
-exports.removeAllCookies = function(successCallback, errorCallback) {
-  if (arguments.length > 0 && (successCallback !== null &&
-      typeof successCallback !== 'function') || arguments.length > 1 &&
-      (errorCallback !== null && typeof errorCallback !== 'function')) {
-    throw new tizen.WebAPIException(tizen.WebAPIException.TYPE_MISMATCH_ERR);
-  }
-
-  var callbackId = asyncCallbacks.setup(function(result) {
-    if (result.error !== null) {
-      if (!errorCallback) {
-        return;
-      }
-      return errorCallback(new tizen.WebAPIError(result.error));
-    }
-    if (successCallback !== null) {
-      return successCallback(result.data);
-    }
-    return;
-  });
-
-  var msg = { cmd: 'RemoveAllCookies' };
-  postMessage(msg, callbackId);
-};
diff --git a/src/websetting/web_setting_extension.cc b/src/websetting/web_setting_extension.cc
deleted file mode 100644 (file)
index 108c769..0000000
+++ /dev/null
@@ -1,34 +0,0 @@
-// Copyright (c) 2014 Samsung Electronics Co., Ltd. All Rights Reserved
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "web_setting/web_setting_extension.h"
-
-#include <string>
-
-#include "web_setting/web_setting.h"
-#include "web_setting/web_setting_instance.h"
-
-extern const char kSource_web_setting_api[];
-
-common::Extension* CreateExtension() {
-  std::string env_app_id = common::Extension::GetRuntimeVariable("app_id", 64);
-  std::string app_id = env_app_id.substr(1, env_app_id.rfind('"') - 1);
-  if (app_id.empty()) {
-    std::cerr << "Got invalid application ID." << std::endl;
-    return nullptr;
-  }
-  return new WebSettingExtension(app_id);
-}
-
-WebSettingExtension::WebSettingExtension(const std::string& app_id) {
-  current_app_.reset(new WebSetting(app_id));
-  SetExtensionName("tizen.websetting");
-  SetJavaScriptAPI(kSource_web_setting_api);
-}
-
-WebSettingExtension::~WebSettingExtension() {}
-
-common::Instance* WebSettingExtension::CreateInstance() {
-  return new WebSettingInstance(this);
-}
diff --git a/src/websetting/web_setting_extension.h b/src/websetting/web_setting_extension.h
deleted file mode 100644 (file)
index f7e3aaf..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-// Copyright (c) 2014 Samsung Electronics Co., Ltd. All Rights Reserved
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef WEB_SETTING_WEB_SETTING_EXTENSION_H_
-#define WEB_SETTING_WEB_SETTING_EXTENSION_H_
-
-#include <memory>
-#include <string>
-#include "common/extension.h"
-#include "web_setting/web_setting.h"
-
-class WebSettingExtension : public common::Extension {
- public:
-  explicit WebSettingExtension(const std::string& app_id);
-  virtual ~WebSettingExtension();
-
-  WebSetting* current_app() { return current_app_.get(); }
- private:
-  virtual common::Instance* CreateInstance();
-
-  std::unique_ptr<WebSetting> current_app_;
-};
-
-#endif  // WEB_SETTING_WEB_SETTING_EXTENSION_H_
diff --git a/src/websetting/web_setting_extension_utils.h b/src/websetting/web_setting_extension_utils.h
deleted file mode 100644 (file)
index 03a92a0..0000000
+++ /dev/null
@@ -1,47 +0,0 @@
-// Copyright (c) 2014 Samsung Electronics Co., Ltd. All Rights Reserved
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef WEB_SETTING_WEB_SETTING_EXTENSION_UTILS_H_
-#define WEB_SETTING_WEB_SETTING_EXTENSION_UTILS_H_
-
-#include <memory>
-
-#include "common/picojson.h"
-#include "tizen/tizen.h"
-
-std::unique_ptr<picojson::value> CreateResultMessage() {
-  picojson::object obj;
-  obj["error"] = picojson::value();
-  return std::unique_ptr<picojson::value>(new picojson::value(obj));
-}
-
-std::unique_ptr<picojson::value> CreateResultMessage(
-    WebApiAPIErrors error) {
-  picojson::object obj;
-  obj["error"] = picojson::value(static_cast<double>(error));
-  return std::unique_ptr<picojson::value>(new picojson::value(obj));
-}
-
-std::unique_ptr<picojson::value> CreateResultMessage(
-    const picojson::object& data) {
-  picojson::object obj;
-  obj["data"] = picojson::value(data);
-  return std::unique_ptr<picojson::value>(new picojson::value(obj));
-}
-
-std::unique_ptr<picojson::value> CreateResultMessage(
-    const picojson::array& data) {
-  picojson::object obj;
-  obj["data"] = picojson::value(data);
-  return std::unique_ptr<picojson::value>(new picojson::value(obj));
-}
-
-std::unique_ptr<picojson::value> CreateResultMessage(
-    const picojson::value& data) {
-  picojson::object obj;
-  obj["data"] = data;
-  return std::unique_ptr<picojson::value>(new picojson::value(obj));
-}
-
-#endif  // WEB_SETTING_WEB_SETTING_EXTENSION_UTILS_H_
diff --git a/src/websetting/web_setting_instance.cc b/src/websetting/web_setting_instance.cc
deleted file mode 100644 (file)
index 08f3070..0000000
+++ /dev/null
@@ -1,72 +0,0 @@
-// Copyright (c) 2014 Samsung Electronics Co., Ltd. All Rights Reserved
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "web_setting/web_setting_instance.h"
-
-#include <string>
-#include <memory>
-#include "common/picojson.h"
-
-namespace {
-
-const char kJSCallbackKey[] = "_callback";
-
-double GetJSCallbackId(const picojson::value& msg) {
-  assert(msg.contains(kJSCallbackKey));
-  const picojson::value& id_value = msg.get(kJSCallbackKey);
-  return id_value.get<double>();
-}
-
-void SetJSCallbackId(picojson::value& msg, double id) {
-  assert(msg.is<picojson::object>());
-  msg.get<picojson::object>()[kJSCallbackKey] = picojson::value(id);
-}
-
-}  // namespace
-
-WebSettingInstance::WebSettingInstance(WebSettingExtension* extension)
-    : extension_(extension) {
-}
-
-WebSettingInstance::~WebSettingInstance() {}
-
-void WebSettingInstance::HandleMessage(const char* message) {
-  picojson::value v;
-
-  std::string err;
-  picojson::parse(v, message, message + strlen(message), &err);
-  if (!err.empty()) {
-    std::cerr << "Error during parsing message: " << err.c_str();
-    return;
-  }
-
-  std::string cmd = v.get("cmd").to_str();
-  if (cmd == "SetUserAgentString")
-    HandleSetUserAgentString(v);
-  else if (cmd == "RemoveAllCookies")
-    HandleRemoveAllCookies(v);
-  else
-    std::cerr << "ASSERT NOT REACHED. \n";
-}
-
-void WebSettingInstance::HandleSetUserAgentString(const picojson::value &msg) {
-  std::string userAgent = msg.get("userAgentStr").to_str();
-  picojson::value *result = extension_->current_app()->
-                            SetUserAgentString(userAgent).release();
-  ReturnMessageAsync(GetJSCallbackId(msg), *result);
-  delete result;
-}
-
-void WebSettingInstance::HandleRemoveAllCookies(const picojson::value& msg) {
-  picojson::value *result = extension_->current_app()->
-                            RemoveAllCookies().release();
-  ReturnMessageAsync(GetJSCallbackId(msg), *result);
-  delete result;
-}
-
-void WebSettingInstance::ReturnMessageAsync(double callback_id,
-    picojson::value& value) {
-  SetJSCallbackId(value, callback_id);
-  PostMessage(value.serialize().c_str());
-}
diff --git a/src/websetting/web_setting_instance.h b/src/websetting/web_setting_instance.h
deleted file mode 100644 (file)
index e887541..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-// Copyright (c) 2014 Samsung Electronics Co., Ltd. All Rights Reserved
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef WEB_SETTING_WEB_SETTING_INSTANCE_H_
-#define WEB_SETTING_WEB_SETTING_INSTANCE_H_
-
-#include "common/extension.h"
-#include "common/picojson.h"
-#include "tizen/tizen.h"
-
-#include "web_setting/web_setting_extension.h"
-
-class WebSettingInstance : public common::Instance {
- public:
-  explicit WebSettingInstance(WebSettingExtension* extension);
-  virtual ~WebSettingInstance();
-
- private:
-  void HandleMessage(const char* message);
-
-  void HandleSetUserAgentString(const picojson::value& msg);
-  void HandleRemoveAllCookies(const picojson::value& msg);
-
-  void ReturnMessageAsync(double callback_id, picojson::value& value);
-
-  WebSettingExtension* extension_;
-};
-
-#endif  // WEB_SETTING_WEB_SETTING_INSTANCE_H_
diff --git a/src/websetting/websetting.cc b/src/websetting/websetting.cc
new file mode 100644 (file)
index 0000000..7940bae
--- /dev/null
@@ -0,0 +1,100 @@
+// Copyright (c) 2014 Samsung Electronics Co., Ltd. All Rights Reserved
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "websetting/websetting.h"
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <utility>
+
+#include "websetting/websetting_extension_utils.h"
+
+namespace {
+
+const char kRuntimeServiceName[] =  "org.crosswalkproject.Runtime1";
+const char kRuntimeRunningManagerPath[] = "/running1";
+const char kRuntimeRunningAppInterface[] =
+    "org.crosswalkproject.Running.Application1";
+
+// The runtime process exports object for each running app on the session bus.
+GDBusProxy* CreateRunningAppProxy(const std::string& app_id) {
+  GError* error = NULL;
+  GDBusConnection* connection =
+      g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
+  if (!connection) {
+    std::cerr << "Couldn't get the session bus connection: "
+              << error->message << std::endl;
+    g_error_free(error);
+    return NULL;
+  }
+
+  std::string path =
+      std::string(kRuntimeRunningManagerPath) + "/" + app_id;
+  // Every application id contains '.' character and since object path
+  // is created from application id it also contains '.' character.
+  // The d-bus proxy doesn't accept '.' character in object path
+  // And that is why the substantiation is needed here.
+  std::replace(path.begin(), path.end(), '.', '_');
+  GDBusProxy* proxy = g_dbus_proxy_new_sync(
+      connection, G_DBUS_PROXY_FLAGS_NONE, NULL, kRuntimeServiceName,
+      path.c_str(), kRuntimeRunningAppInterface, NULL, &error);
+  if (!proxy) {
+    std::cerr << "Couldn't create proxy for " << kRuntimeRunningAppInterface
+              << ": " << error->message << std::endl;
+    g_error_free(error);
+    return NULL;
+  }
+
+  return proxy;
+}
+
+}  // namespace
+
+WebSetting::WebSetting(const std::string& app_id)
+    : app_id_(app_id),
+      running_app_proxy_(NULL) {
+}
+
+WebSetting::~WebSetting() {
+  if (running_app_proxy_)
+    g_object_unref(running_app_proxy_);
+}
+
+std::unique_ptr<picojson::value> WebSetting::RemoveAllCookies() {
+  if (!running_app_proxy_) {
+    if (!(running_app_proxy_ = CreateRunningAppProxy(app_id_)))
+      return CreateResultMessage(WebApiAPIErrors::UNKNOWN_ERR);
+  }
+  GError* error = NULL;
+  GVariant* result = g_dbus_proxy_call_sync(
+      running_app_proxy_, "RemoveAllCookies", NULL,
+      G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
+  if (!result) {
+    std::cerr << "Fail to call 'RemoveuserAgentAllCookies':"
+              << error->message << std::endl;
+    g_error_free(error);
+    return CreateResultMessage(WebApiAPIErrors::UNKNOWN_ERR);
+  }
+  return CreateResultMessage();
+}
+
+std::unique_ptr<picojson::value> WebSetting::SetUserAgentString(
+    const std::string& user_agent) {
+  if (!running_app_proxy_) {
+    if (!(running_app_proxy_ = CreateRunningAppProxy(app_id_)))
+      return CreateResultMessage(WebApiAPIErrors::UNKNOWN_ERR);
+  }
+  GError* error = NULL;
+  GVariant* result = g_dbus_proxy_call_sync(
+      running_app_proxy_, "SetUserAgentString",
+      g_variant_new("(s)", user_agent.c_str()),
+      G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
+  if (!result) {
+    std::cerr << "Fail to call 'SetUserAgentString':"
+              << error->message << std::endl;
+    g_error_free(error);
+    return CreateResultMessage(WebApiAPIErrors::UNKNOWN_ERR);
+  }
+  return CreateResultMessage();
+}
diff --git a/src/websetting/websetting.gyp b/src/websetting/websetting.gyp
new file mode 100644 (file)
index 0000000..20e4d18
--- /dev/null
@@ -0,0 +1,29 @@
+{
+  'includes':[
+    '../common/common.gypi',
+  ],
+  'targets': [
+    {
+      'target_name': 'tizen_websetting',
+      'type': 'loadable_module',
+      'sources': [
+        'websetting.cc',
+        'websetting.h',
+        'websetting_api.js',
+        'websetting_extension.cc',
+        'websetting_extension.h',
+        'websetting_extension_utils.h',
+        'websetting_instance.cc',
+        'websetting_instance.h',
+      ],
+      'includes': [
+        '../common/pkg-config.gypi',
+      ],
+      'variables': {
+        'packages': [
+          'glib-2.0',
+        ]
+      },
+    },
+  ],
+}
diff --git a/src/websetting/websetting.h b/src/websetting/websetting.h
new file mode 100644 (file)
index 0000000..8bb5674
--- /dev/null
@@ -0,0 +1,28 @@
+// Copyright (c) 2014 Samsung Electronics Co., Ltd. All Rights Reserved
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef WEBSETTING_WEBSETTING_H_
+#define WEBSETTING_WEBSETTING_H_
+
+#include <gio/gio.h>
+#include <memory>
+#include <string>
+
+#include "common/picojson.h"
+
+class WebSetting {
+ public:
+  explicit WebSetting(const std::string& app_id);
+  ~WebSetting();
+
+  std::unique_ptr<picojson::value> RemoveAllCookies();
+  std::unique_ptr<picojson::value> SetUserAgentString(
+      const std::string& user_agent);
+
+ private:
+  std::string app_id_;
+  GDBusProxy* running_app_proxy_;
+};
+
+#endif  // websetting_websetting_H_
diff --git a/src/websetting/websetting_api.js b/src/websetting/websetting_api.js
new file mode 100644 (file)
index 0000000..56aaa03
--- /dev/null
@@ -0,0 +1,74 @@
+// Copyright 2015 Samsung Electronics Co, Ltd. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+var validator_ = xwalk.utils.validator;
+var type_ = xwalk.utils.type;
+var native_ = new xwalk.utils.NativeManager(extension);
+var converter_ = xwalk.utils.converter;
+
+exports.setUserAgentString = function() {
+  var args = validator_.validateArgs(arguments, [
+    {
+      name: 'uri',
+      type: validator_.Types.STRING,
+      optional: false,
+      nullable: false
+    },
+    {
+      name: 'successCallback',
+      type: validator_.Types.FUNCTION,
+      optional: true,
+      nullable: true
+    },
+    {
+      name: 'errorCallback',
+      type: validator_.Types.FUNCTION,
+      optional: true,
+      nullable: true
+    }
+  ]);
+
+  var callback = function(result) {
+    if (native_.isFailure(result)) {
+      native_.callIfPossible(args.errorCallback,
+          native_.getErrorObject(result));
+    } else {
+      var exifInfo = native_.getResultObject(result);
+      args.successCallback(exifInfo);
+    }
+  };
+
+  native_.call('Websetting_setUserAgentString', {'userAgent': args.userAgent},
+      callback);
+};
+
+exports.removeAllCookies = function() {
+  var args = validator_.validateArgs(arguments, [
+    {
+      name: 'successCallback',
+      type: validator_.Types.FUNCTION,
+      optional: true,
+      nullable: true
+    },
+    {
+      name: 'errorCallback',
+      type: validator_.Types.FUNCTION,
+      optional: true,
+      nullable: true
+    }
+  ]);
+
+  var callback = function(result) {
+    if (native_.isFailure(result)) {
+      native_.callIfPossible(args.errorCallback,
+          native_.getErrorObject(result));
+    } else {
+      var exifInfo = native_.getResultObject(result);
+      args.successCallback(exifInfo);
+    }
+  };
+
+  native_.call('Websetting_removeAllCookies', {},
+      callback);
+};
diff --git a/src/websetting/websetting_extension.cc b/src/websetting/websetting_extension.cc
new file mode 100644 (file)
index 0000000..f09e4c3
--- /dev/null
@@ -0,0 +1,34 @@
+// Copyright (c) 2014 Samsung Electronics Co., Ltd. All Rights Reserved
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "websetting/websetting_extension.h"
+
+#include <string>
+
+#include "websetting/websetting.h"
+#include "websetting/websetting_instance.h"
+
+extern const char kSource_websetting_api[];
+
+common::Extension* CreateExtension() {
+  std::string env_app_id = common::Extension::GetRuntimeVariable("app_id", 64);
+  std::string app_id = env_app_id.substr(1, env_app_id.rfind('"') - 1);
+  if (app_id.empty()) {
+    std::cerr << "Got invalid application ID." << std::endl;
+    return nullptr;
+  }
+  return new WebSettingExtension(app_id);
+}
+
+WebSettingExtension::WebSettingExtension(const std::string& app_id) {
+  current_app_.reset(new WebSetting(app_id));
+  SetExtensionName("tizen.websetting");
+  SetJavaScriptAPI(kSource_websetting_api);
+}
+
+WebSettingExtension::~WebSettingExtension() {}
+
+common::Instance* WebSettingExtension::CreateInstance() {
+  return new extension::websetting::WebSettingInstance(this);
+}
diff --git a/src/websetting/websetting_extension.h b/src/websetting/websetting_extension.h
new file mode 100644 (file)
index 0000000..940a6d8
--- /dev/null
@@ -0,0 +1,25 @@
+// Copyright (c) 2014 Samsung Electronics Co., Ltd. All Rights Reserved
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef WEBSETTING_WEBSETTING_EXTENSION_H_
+#define WEBSETTING_WEBSETTING_EXTENSION_H_
+
+#include <memory>
+#include <string>
+#include "common/extension.h"
+#include "websetting/websetting.h"
+
+class WebSettingExtension : public common::Extension {
+ public:
+  explicit WebSettingExtension(const std::string& app_id);
+  virtual ~WebSettingExtension();
+
+  WebSetting* current_app() { return current_app_.get(); }
+ private:
+  virtual common::Instance* CreateInstance();
+
+  std::unique_ptr<WebSetting> current_app_;
+};
+
+#endif  // WEBSETTING_WEBSETTING_EXTENSION_H_
diff --git a/src/websetting/websetting_extension_utils.h b/src/websetting/websetting_extension_utils.h
new file mode 100644 (file)
index 0000000..bf31bb5
--- /dev/null
@@ -0,0 +1,47 @@
+// Copyright (c) 2014 Samsung Electronics Co., Ltd. All Rights Reserved
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef WEBSETTING_WEBSETTING_EXTENSION_UTILS_H_
+#define WEBSETTING_WEBSETTING_EXTENSION_UTILS_H_
+
+#include <memory>
+
+#include "common/picojson.h"
+#include "tizen/tizen.h"
+
+std::unique_ptr<picojson::value> CreateResultMessage() {
+  picojson::object obj;
+  obj["error"] = picojson::value();
+  return std::unique_ptr<picojson::value>(new picojson::value(obj));
+}
+
+std::unique_ptr<picojson::value> CreateResultMessage(
+    WebApiAPIErrors error) {
+  picojson::object obj;
+  obj["error"] = picojson::value(static_cast<double>(error));
+  return std::unique_ptr<picojson::value>(new picojson::value(obj));
+}
+
+std::unique_ptr<picojson::value> CreateResultMessage(
+    const picojson::object& data) {
+  picojson::object obj;
+  obj["data"] = picojson::value(data);
+  return std::unique_ptr<picojson::value>(new picojson::value(obj));
+}
+
+std::unique_ptr<picojson::value> CreateResultMessage(
+    const picojson::array& data) {
+  picojson::object obj;
+  obj["data"] = picojson::value(data);
+  return std::unique_ptr<picojson::value>(new picojson::value(obj));
+}
+
+std::unique_ptr<picojson::value> CreateResultMessage(
+    const picojson::value& data) {
+  picojson::object obj;
+  obj["data"] = data;
+  return std::unique_ptr<picojson::value>(new picojson::value(obj));
+}
+
+#endif  // WEBSETTING_WEBSETTING_EXTENSION_UTILS_H_
diff --git a/src/websetting/websetting_instance.cc b/src/websetting/websetting_instance.cc
new file mode 100644 (file)
index 0000000..ff9caee
--- /dev/null
@@ -0,0 +1,110 @@
+// Copyright (c) 2014 Samsung Electronics Co., Ltd. All Rights Reserved
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "websetting/websetting_instance.h"
+
+#include <string>
+#include <memory>
+
+#include "common/logger.h"
+#include "common/picojson.h"
+#include "common/task-queue.h"
+#include "common/platform_exception.h"
+
+namespace {
+const char kWrtServiceName[] = "wrt-service";
+const char kSetUserAgentString[] = "Websetting_setUserAgentString";
+const char kRemoveAllCookies[] = "Websetting_removeAllCookies";
+}  // namespace
+
+namespace extension {
+namespace websetting {
+
+typedef picojson::value JsonValue;
+typedef picojson::object JsonObject;
+typedef picojson::array JsonArray;
+typedef std::string JsonString;
+
+WebSettingInstance::WebSettingInstance(WebSettingExtension* extension)
+    : extension_(extension) {
+  using namespace std::placeholders;
+
+#define REGISTER_ASYNC(c, x) \
+  RegisterHandler(c, std::bind(&WebSettingInstance::x, this, _1, _2));
+  REGISTER_ASYNC(kSetUserAgentString, setUserAgentString);
+  REGISTER_ASYNC(kRemoveAllCookies, removeAllCookies);
+#undef REGISTER_ASYNC
+}
+
+WebSettingInstance::~WebSettingInstance() {}
+
+void WebSettingInstance::setUserAgentString(const picojson::value& args,
+                                            picojson::object& out) {
+  const double callback_id = args.get("callbackId").get<double>();
+  auto get = [=](const std::shared_ptr<JsonValue>& response) -> void {
+    try {
+      const char *runtime_name =
+          common::Extension::GetRuntimeVariable("runtime_name", 64).c_str();
+      LoggerD("runtime_name: %s", runtime_name);
+      if (strcmp(runtime_name, kWrtServiceName) == 0) {
+        throw common::NotSupportedException("Not Implemented");
+      }
+
+      std::string userAgent = args.get("userAgentStr").to_str();
+      extension_->current_app()->
+          SetUserAgentString(userAgent).release();
+      ReportSuccess(response->get<picojson::object>());
+    } catch (const common::PlatformException& e) {
+      ReportError(e, response->get<picojson::object>());
+    }
+  };
+
+  auto get_response =
+    [callback_id, this](const std::shared_ptr<JsonValue>& response) -> void {
+      picojson::object& obj = response->get<picojson::object>();
+      obj.insert(std::make_pair("callbackId", callback_id));
+      LoggerD("callback is %s", response->serialize().c_str());
+      PostMessage(response->serialize().c_str());
+    };
+
+  common::TaskQueue::GetInstance().Queue<JsonValue>(
+      get, get_response,
+      std::shared_ptr<JsonValue>(new JsonValue(JsonObject())));
+}
+
+void WebSettingInstance::removeAllCookies(const picojson::value& args,
+                                          picojson::object& out) {
+  const double callback_id = args.get("callbackId").get<double>();
+  auto get = [=](const std::shared_ptr<JsonValue>& response) -> void {
+    try {
+      const char *runtime_name =
+          common::Extension::GetRuntimeVariable("runtime_name", 64).c_str();
+      LoggerD("runtime_name: %s", runtime_name);
+      if (strcmp(runtime_name, kWrtServiceName) == 0) {
+        throw common::NotSupportedException("Not Implemented");
+      }
+
+      extension_->current_app()->
+          RemoveAllCookies().release();
+      ReportSuccess(response->get<picojson::object>());
+    } catch (const common::PlatformException& e) {
+      ReportError(e, response->get<picojson::object>());
+    }
+  };
+
+  auto get_response =
+      [callback_id, this](const std::shared_ptr<JsonValue>& response) -> void {
+        picojson::object& obj = response->get<picojson::object>();
+        obj.insert(std::make_pair("callbackId", callback_id));
+        LoggerD("callback is %s", response->serialize().c_str());
+        PostMessage(response->serialize().c_str());
+      };
+
+  common::TaskQueue::GetInstance().Queue<JsonValue>(
+      get, get_response,
+      std::shared_ptr<JsonValue>(new JsonValue(JsonObject())));
+}
+
+}  // namespace websetting
+}  // namespace extension
diff --git a/src/websetting/websetting_instance.h b/src/websetting/websetting_instance.h
new file mode 100644 (file)
index 0000000..82b9424
--- /dev/null
@@ -0,0 +1,32 @@
+// Copyright (c) 2014 Samsung Electronics Co., Ltd. All Rights Reserved
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef WEBSETTING_WEBSETTING_INSTANCE_H_
+#define WEBSETTING_WEBSETTING_INSTANCE_H_
+
+#include "common/extension.h"
+#include "common/picojson.h"
+#include "tizen/tizen.h"
+
+#include "websetting/websetting_extension.h"
+
+namespace extension {
+namespace websetting {
+
+class WebSettingInstance : public common::ParsedInstance {
+ public:
+  explicit WebSettingInstance(WebSettingExtension* extension);
+  virtual ~WebSettingInstance();
+
+ private:
+  void setUserAgentString(const picojson::value& args, picojson::object& out);
+  void removeAllCookies(const picojson::value& args, picojson::object& out);
+
+  WebSettingExtension* extension_;
+};
+
+}  // namespace websetting
+}  // namespace extension
+
+#endif  // WEBSETTING_WEBSETTING_INSTANCE_H_