Move DBusServer to WebApplication from Runtime
authorWonYoung Choi <wy80.choi@samsung.com>
Wed, 29 Apr 2015 01:51:15 +0000 (10:51 +0900)
committerWonYoung Choi <wy80.choi@samsung.com>
Wed, 6 May 2015 00:14:53 +0000 (09:14 +0900)
Change-Id: I5079f64ca52cfb2921077507f7c80bd2b6b3f4aa

src/bundle/extension_client.cc
src/common/constants.cc
src/common/constants.h
src/extension/extension_server.cc
src/extension/extension_server.h
src/runtime/runtime.cc
src/runtime/runtime.h
src/runtime/web_application.cc
src/runtime/web_application.h

index c0d0744..1222d19 100755 (executable)
@@ -46,6 +46,7 @@ std::string ExtensionClient::CreateInstance(
   std::string ret(instance_id);
   handlers_[ret] = handler;
 
+  g_variant_unref(value);
   return ret;
 }
 
@@ -64,6 +65,8 @@ void ExtensionClient::DestroyInstance(const std::string& instance_id) {
   if (it != handlers_.end()) {
     handlers_.erase(it);
   }
+
+  g_variant_unref(value);
 }
 
 void ExtensionClient::PostMessageToNative(
@@ -89,7 +92,10 @@ std::string ExtensionClient::SendSyncMessageToNative(
   gchar* reply;
   g_variant_get(value, "(&s)", &reply);
 
-  return std::string(reply);
+  std::string ret(reply);
+  g_variant_unref(value);
+
+  return ret;
 }
 
 void ExtensionClient::Initialize(const std::string& uuid) {
@@ -132,6 +138,8 @@ void ExtensionClient::Initialize(const std::string& uuid) {
     }
     extension_apis_.insert(std::make_pair(std::string(name), code));
   }
+
+  g_variant_unref(value);
 }
 
 void ExtensionClient::HandleSignal(
index 90402fb..2547076 100644 (file)
@@ -10,8 +10,8 @@ const char kSystemExtensionPath[] = "/usr/lib/tizen-extensions-crosswalk";
 const char kExtensionPrefix[] = "lib";
 const char kExtensionSuffix[] = ".so";
 
-const char kDBusNameForRuntime[] = "Runtime";
-const char kDBusInterfaceNameForRuntime[] = "org.tizen.wrt.Runtime";
+const char kDBusNameForApplication[] = "Application";
+const char kDBusInterfaceNameForApplication[] = "org.tizen.wrt.Application";
 const char kMethodNotifyEPCreated[] = "NotifyEPCreated";
 const char kMethodGetRuntimeVariable[] = "GetRuntimeVariable";
 
index c7856b2..9eaa205 100644 (file)
@@ -11,8 +11,8 @@ extern const char kSystemExtensionPath[];
 extern const char kExtensionPrefix[];
 extern const char kExtensionSuffix[];
 
-extern const char kDBusNameForRuntime[];
-extern const char kDBusInterfaceNameForRuntime[];
+extern const char kDBusNameForApplication[];
+extern const char kDBusInterfaceNameForApplication[];
 extern const char kMethodNotifyEPCreated[];
 extern const char kMethodGetRuntimeVariable[];
 
index c725ade..1a3a249 100644 (file)
@@ -72,10 +72,10 @@ bool ExtensionServer::Start(const StringVector& paths) {
     }
   }
 
-  // Connect to DBusServer for Runtime
-  if (!dbus_runtime_client_.ConnectByName(
-          app_uuid_ + "." + std::string(kDBusNameForRuntime))) {
-    LoggerE("Failed to connect to the dbus server for Runtime.");
+  // Connect to DBusServer for Application of Runtime
+  if (!dbus_application_client_.ConnectByName(
+          app_uuid_ + "." + std::string(kDBusNameForApplication))) {
+    LoggerE("Failed to connect to the dbus server for Application.");
     return false;
   }
 
@@ -91,7 +91,7 @@ bool ExtensionServer::Start(const StringVector& paths) {
   dbus_server_.Start(app_uuid_ + "." + std::string(kDBusNameForExtension));
 
   // Send 'ready' signal to Injected Bundle.
-  NotifyEPCreatedToRuntime();
+  NotifyEPCreatedToApplication();
 
   return true;
 }
@@ -146,14 +146,27 @@ bool ExtensionServer::RegisterSymbols(Extension* extension) {
   return true;
 }
 
-void ExtensionServer::GetRuntimeVariable(const char* /*key*/, char* /*value*/,
-    size_t /*value_len*/) {
-  // TODO(wy80.choi): DBus Call to Runtime to get runtime variable
+void ExtensionServer::GetRuntimeVariable(const char* key, char* value,
+    size_t value_len) {
+  GVariant* ret = dbus_application_client_.Call(
+      kDBusInterfaceNameForApplication, kMethodGetRuntimeVariable,
+      g_variant_new("(s)", key), G_VARIANT_TYPE("(s)"));
+
+  if (!ret) {
+    LoggerE("Failed to get runtime variable from Application. (%s)", key);
+    return;
+  }
+
+  gchar* v;
+  g_variant_get(ret, "(&s)", &v);
+  strncpy(value, v, value_len);
+
+  g_variant_unref(ret);
 }
 
-void ExtensionServer::NotifyEPCreatedToRuntime() {
-  dbus_runtime_client_.Call(
-      kDBusInterfaceNameForRuntime, kMethodNotifyEPCreated,
+void ExtensionServer::NotifyEPCreatedToApplication() {
+  dbus_application_client_.Call(
+      kDBusInterfaceNameForApplication, kMethodNotifyEPCreated,
       g_variant_new("(s)", dbus_server_.GetClientAddress().c_str()),
       NULL);
 }
index a2edb83..cf5d4bb 100644 (file)
@@ -35,7 +35,7 @@ class ExtensionServer : public Extension::ExtensionDelegate {
 
   void GetRuntimeVariable(const char* key, char* value, size_t value_len);
 
-  void NotifyEPCreatedToRuntime();
+  void NotifyEPCreatedToApplication();
 
   void HandleDBusMethod(GDBusConnection* connection,
                         const std::string& method_name,
@@ -63,7 +63,7 @@ class ExtensionServer : public Extension::ExtensionDelegate {
 
   std::string app_uuid_;
   DBusServer dbus_server_;
-  DBusClient dbus_runtime_client_;
+  DBusClient dbus_application_client_;
 
   typedef std::set<std::string> StringSet;
   StringSet extension_symbols_;
index e12a97b..a7fab2f 100755 (executable)
@@ -19,19 +19,6 @@ namespace wrt {
 
 namespace {
 
-static const char* kDBusIntrospectionXML =
-  "<node>"
-  "  <interface name='org.tizen.wrt.Runtime'>"
-  "    <method name='NotifyEPCreated'>"
-  "      <arg name='status' type='s' direction='in'/>"
-  "    </method>"
-  "    <method name='GetRuntimeVariable'>"
-  "      <arg name='key' type='s' direction='in' />"
-  "      <arg name='value' type='s' direction='out' />"
-  "    </method>"
-  "  </interface>"
-  "</node>";
-
 static NativeWindow* CreateNativeWindow() {
   // TODO(wy80.choi) : consider other type of native window.
   NativeWindow* window = new NativeAppWindow();
@@ -39,20 +26,6 @@ static NativeWindow* CreateNativeWindow() {
   return window;
 }
 
-static void ExecExtensionProcess(const std::string& uuid) {
-  pid_t pid = -1;
-  if ((pid = fork()) < 0) {
-    LoggerE("Failed to fork child process for extension process.");
-  }
-
-  if (pid == 0) {
-    // TODO(wy80.choi): wrt-extension should be merged to this runtime exec.
-    // It should be changed to "/usr/bin/wrt --extension-process"
-    execl("/usr/bin/wrt-extension",
-          "/usr/bin/wrt-extension", uuid.c_str(), NULL);
-  }
-}
-
 }  // namespace
 
 Runtime::Runtime()
@@ -82,21 +55,6 @@ bool Runtime::OnCreate() {
   application_ = new WebApplication(native_window_, std::move(appdata));
   application_->set_terminator([](){ ui_app_exit(); });
 
-  // Start DBus Server for Runtime
-  // TODO(wy80.choi): Should I add PeerCredentials checker?
-  using std::placeholders::_1;
-  using std::placeholders::_2;
-  using std::placeholders::_3;
-  using std::placeholders::_4;
-  dbus_server_.SetIntrospectionXML(kDBusIntrospectionXML);
-  dbus_server_.SetMethodCallback(kDBusInterfaceNameForRuntime,
-    std::bind(&Runtime::HandleDBusMethod, this, _1, _2, _3, _4));
-  dbus_server_.Start(application_->uuid() +
-                     "." + std::string(kDBusNameForRuntime));
-
-  // Launch Extension Process
-  ExecExtensionProcess(application_->uuid());
-
   return true;
 }
 
@@ -117,9 +75,7 @@ void Runtime::OnResume() {
 
 void Runtime::OnAppControl(app_control_h app_control) {
   std::unique_ptr<AppControl> appcontrol(new AppControl(app_control));
-
   if (application_->launched()) {
-    // Process AppControl
     application_->AppControl(std::move(appcontrol));
   } else {
     application_->Launch(std::move(appcontrol));
@@ -138,19 +94,6 @@ void Runtime::OnLowMemory() {
   }
 }
 
-void Runtime::HandleDBusMethod(GDBusConnection* /*connection*/,
-                               const std::string& method_name,
-                               GVariant* /*parameters*/,
-                               GDBusMethodInvocation* /*invocation*/) {
-  if (method_name == kMethodNotifyEPCreated) {
-    // TODO(wy80.choi): send signal to injected bundle to make connection
-    // between injected bundle and extension process
-    LoggerD("Call!!!! NotifyEPCreated!");
-  } else if (method_name == kMethodGetRuntimeVariable) {
-    // TODO(wy80.choi): return runtime variable
-  }
-}
-
 int Runtime::Exec(int argc, char* argv[]) {
   ui_app_lifecycle_callback_s ops = {NULL, NULL, NULL, NULL, NULL};
 
index c8ead3d..7f12d15 100755 (executable)
@@ -8,7 +8,6 @@
 #include <app.h>
 #include <string>
 
-#include "common/dbus_server.h"
 #include "runtime/native_window.h"
 #include "runtime/web_application.h"
 
@@ -31,14 +30,8 @@ class Runtime {
   virtual void OnLowMemory();
 
  private:
-  void HandleDBusMethod(GDBusConnection* connection,
-                        const std::string& method_name,
-                        GVariant* parameters,
-                        GDBusMethodInvocation* invocation);
-
   WebApplication* application_;
   NativeWindow* native_window_;
-  DBusServer dbus_server_;
 };
 
 }  // namespace wrt
index f66458f..fe3e109 100755 (executable)
@@ -14,6 +14,7 @@
 #include <map>
 
 #include "common/logger.h"
+#include "common/constants.h"
 #include "common/command_line.h"
 #include "common/string_utils.h"
 #include "runtime/native_window.h"
 #include "runtime/resource_manager.h"
 
 namespace {
-  // TODO(sngn.lee) : It should be declare in common header
-  const char* kKeyNameBack = "back";
-
-  const char* kConsoleLogEnableKey = "WRT_CONSOLE_LOG_ENABLE";
-  const char* kConsoleMessageLogTag = "ConsoleMessage";
-
-  const char* kDebugKey = "debug";
-  const char* kPortKey = "port";
-
-  // TODO(wy80.choi): consider 64bits system.
-  const char* kInjectedBundlePath = "/usr/lib/libwrt-injected-bundle.so";
-
-  const char* kAppControlEventScript = \
-        "(function(){"
-        "var __event = document.createEvent(\"CustomEvent\");\n"
-        "__event.initCustomEvent(\"appcontrol\", true, true);\n"
-        "document.dispatchEvent(__event);\n"
-        "\n"
-        "for (var i=0; i < window.frames.length; i++)\n"
-        "{ window.frames[i].document.dispatchEvent(__event); }"
-        "})()";
-  const char* kBackKeyEventScript = \
-        "(function(){"
-        "var __event = document.createEvent(\"CustomEvent\");\n"
-        "__event.initCustomEvent(\"tizenhwkey\", true, true);\n"
-        "__event.keyName = \"back\";\n"
-        "document.dispatchEvent(__event);\n"
-        "\n"
-        "for (var i=0; i < window.frames.length; i++)\n"
-        "{ window.frames[i].document.dispatchEvent(__event); }"
-        "})()";
-  const char* kFullscreenPrivilege = "http://tizen.org/privilege/fullscreen";
-  const char* kFullscreenFeature = "fullscreen";
-  const char* kNotificationPrivilege =
-      "http://tizen.org/privilege/notification";
-  const char* kLocationPrivilege =
-      "http://tizen.org/privilege/location";
-  const char* kStoragePrivilege =
-      "http://tizen.org/privilege/unlimitedstorage";
-
-  const char* kVisibilitySuspendFeature = "visibility,suspend";
-  const char* kMediastreamRecordFeature = "mediastream,record";
-  const char* kEncryptedDatabaseFeature = "encrypted,database";
-  const char* kRotationLockFeature = "rotation,lock";
-  const char* kBackgroundMusicFeature = "background,music";
-  const char* kSoundModeFeature = "sound,mode";
-  const char* kBackgroundVibrationFeature = "background,vibration";
+// TODO(sngn.lee) : It should be declare in common header
+const char* kKeyNameBack = "back";
+
+const char* kConsoleLogEnableKey = "WRT_CONSOLE_LOG_ENABLE";
+const char* kConsoleMessageLogTag = "ConsoleMessage";
+
+const char* kDebugKey = "debug";
+const char* kPortKey = "port";
+
+// TODO(wy80.choi): consider 64bits system.
+const char* kInjectedBundlePath = "/usr/lib/libwrt-injected-bundle.so";
+const char* kDBusIntrospectionXML =
+    "<node>"
+    "  <interface name='org.tizen.wrt.Application'>"
+    "    <method name='NotifyEPCreated'>"
+    "      <arg name='status' type='s' direction='in'/>"
+    "    </method>"
+    "    <method name='GetRuntimeVariable'>"
+    "      <arg name='key' type='s' direction='in' />"
+    "      <arg name='value' type='s' direction='out' />"
+    "    </method>"
+    "  </interface>"
+    "</node>";
+const char* kAppControlEventScript = \
+    "(function(){"
+    "var __event = document.createEvent(\"CustomEvent\");\n"
+    "__event.initCustomEvent(\"appcontrol\", true, true);\n"
+    "document.dispatchEvent(__event);\n"
+    "\n"
+    "for (var i=0; i < window.frames.length; i++)\n"
+    "{ window.frames[i].document.dispatchEvent(__event); }"
+    "})()";
+const char* kBackKeyEventScript = \
+    "(function(){"
+    "var __event = document.createEvent(\"CustomEvent\");\n"
+    "__event.initCustomEvent(\"tizenhwkey\", true, true);\n"
+    "__event.keyName = \"back\";\n"
+    "document.dispatchEvent(__event);\n"
+    "\n"
+    "for (var i=0; i < window.frames.length; i++)\n"
+    "{ window.frames[i].document.dispatchEvent(__event); }"
+    "})()";
+const char* kFullscreenPrivilege = "http://tizen.org/privilege/fullscreen";
+const char* kFullscreenFeature = "fullscreen";
+const char* kNotificationPrivilege =
+    "http://tizen.org/privilege/notification";
+const char* kLocationPrivilege =
+    "http://tizen.org/privilege/location";
+const char* kStoragePrivilege =
+    "http://tizen.org/privilege/unlimitedstorage";
+
+const char* kVisibilitySuspendFeature = "visibility,suspend";
+const char* kMediastreamRecordFeature = "mediastream,record";
+const char* kEncryptedDatabaseFeature = "encrypted,database";
+const char* kRotationLockFeature = "rotation,lock";
+const char* kBackgroundMusicFeature = "background,music";
+const char* kSoundModeFeature = "sound,mode";
+const char* kBackgroundVibrationFeature = "background,vibration";
 
 bool FindPrivilege(wrt::ApplicationData* app_data,
                    const std::string& privilege) {
@@ -85,6 +97,20 @@ bool FindPrivilege(wrt::ApplicationData* app_data,
   }
   return false;
 }
+
+void ExecExtensionProcess(const std::string& uuid) {
+  pid_t pid = -1;
+  if ((pid = fork()) < 0) {
+    LoggerE("Failed to fork child process for extension process.");
+  }
+  if (pid == 0) {
+    // TODO(wy80.choi): wrt-extension should be merged to this runtime exec.
+    // It should be changed to "/usr/bin/wrt --extension-process"
+    execl("/usr/bin/wrt-extension",
+          "/usr/bin/wrt-extension", uuid.c_str(), NULL);
+  }
+}
+
 }  // namespace
 
 namespace wrt {
@@ -97,10 +123,10 @@ WebApplication::WebApplication(
           kInjectedBundlePath)),
       window_(window),
       appid_(app_data->app_id()),
+      app_uuid_(utils::GenerateUUID()),
       locale_manager_(new LocaleManager()),
       app_data_(std::move(app_data)),
-      terminator_(NULL),
-      uuid_(utils::GenerateUUID()) {
+      terminator_(NULL) {
   std::unique_ptr<char, decltype(std::free)*>
     path {app_get_data_path(), std::free};
   app_data_path_ = path.get();
@@ -208,6 +234,22 @@ bool WebApplication::Initialize() {
 }
 
 void WebApplication::Launch(std::unique_ptr<wrt::AppControl> appcontrol) {
+  // Start DBus Server for Runtime
+  // TODO(wy80.choi): Should I add PeerCredentials checker?
+  using std::placeholders::_1;
+  using std::placeholders::_2;
+  using std::placeholders::_3;
+  using std::placeholders::_4;
+  dbus_server_.SetIntrospectionXML(kDBusIntrospectionXML);
+  dbus_server_.SetMethodCallback(kDBusInterfaceNameForApplication,
+    std::bind(&WebApplication::HandleDBusMethod, this, _1, _2, _3, _4));
+  dbus_server_.Start(app_uuid_ +
+                     "." + std::string(kDBusNameForApplication));
+
+  // Execute ExtensionProcess
+  ExecExtensionProcess(app_uuid_);
+
+  // Setup View
   WebView* view = new WebView(window_, ewk_context_);
   SetupWebView(view);
 
@@ -220,10 +262,11 @@ void WebApplication::Launch(std::unique_ptr<wrt::AppControl> appcontrol) {
   //                     elm_theme_get(NULL),
   //                    uuid_.c_str());
 
-  // view->LoadUrl("file:///home/owner/apps_rw/33CFo0eFJe/"
-  //               "33CFo0eFJe.annex/index.html");
   std::unique_ptr<ResourceManager::Resource> res =
     resource_manager_->GetStartResource(appcontrol.get());
+  // TODO(wy80.choi): temporary comment for test, remove it later.
+  // view->LoadUrl("file:///home/owner/apps_rw/33CFo0eFJe/"
+  //               "33CFo0eFJe.annex/index.html");
   view->LoadUrl(res->uri());
   view_stack_.push_front(view);
   window_->SetContent(view->evas_object());
@@ -363,7 +406,7 @@ void WebApplication::OnClosedWebView(WebView * view) {
   // Delete after the callback context(for ewk view) was not used
   ecore_idler_add([](void* view) {
       WebView* obj = static_cast<WebView*>(view);
-      delete view;
+      delete obj;
       return EINA_FALSE;
     }, view);
 }
@@ -545,6 +588,28 @@ void WebApplication::OnQuotaExceed(
   // TODO(sngn.lee): create popup and show
 }
 
-
+void WebApplication::HandleDBusMethod(GDBusConnection* /*connection*/,
+                                      const std::string& method_name,
+                                      GVariant* parameters,
+                                      GDBusMethodInvocation* invocation) {
+  if (method_name == kMethodNotifyEPCreated) {
+    // TODO(wy80.choi): send signal to injected bundle to make connection
+    // between injected bundle and extension process
+    LoggerD("Call!!!! NotifyEPCreated!");
+  } else if (method_name == kMethodGetRuntimeVariable) {
+    gchar* key;
+    std::string value;
+    g_variant_get(parameters, "(&s)", &key);
+    if (g_strcmp0(key, "runtime_name") == 0) {
+      value = std::string("wrt");
+    } else if (g_strcmp0(key, "app_id") == 0) {
+      value = appid_;
+    } else if (g_strcmp0(key, "encoded_bundle") == 0) {
+      value = received_appcontrol_->encoded_bundle();
+    }
+    g_dbus_method_invocation_return_value(
+          invocation, g_variant_new("(s)", value.c_str()));
+  }
+}
 
 }  // namespace wrt
index aaf529f..6b6f069 100755 (executable)
@@ -10,6 +10,7 @@
 #include <memory>
 #include <functional>
 
+#include "common/dbus_server.h"
 #include "runtime/web_view.h"
 
 class Ewk_Context;
@@ -54,7 +55,6 @@ class WebApplication : public WebView::EventListener {
   virtual void OnLowMemory();
   virtual bool OnContextMenuDisabled(WebView* view);
   virtual bool OnDidNavigation(WebView* view, const std::string& url);
-
   virtual void OnNotificationPermissionRequest(
       WebView* view,
       const std::string& url,
@@ -68,9 +68,6 @@ class WebApplication : public WebView::EventListener {
       const std::string& url,
       std::function<void(bool)> result_handler);
 
-
-  std::string uuid() const { return uuid_; }
-
  private:
   bool Initialize();
 
@@ -79,19 +76,25 @@ class WebApplication : public WebView::EventListener {
   void LaunchInspector(wrt::AppControl* appcontrol);
   void SetupWebView(WebView* view);
 
+  void HandleDBusMethod(GDBusConnection* connection,
+                        const std::string& method_name,
+                        GVariant* parameters,
+                        GDBusMethodInvocation* invocation);
+
   bool launched_;
   bool debug_mode_;
   Ewk_Context* ewk_context_;
   NativeWindow* window_;
+  DBusServer dbus_server_;
   std::string appid_;
   std::string app_data_path_;
+  std::string app_uuid_;
   std::list<WebView*> view_stack_;
   std::unique_ptr<LocaleManager> locale_manager_;
   std::unique_ptr<ApplicationData> app_data_;
   std::unique_ptr<ResourceManager> resource_manager_;
   std::unique_ptr<wrt::AppControl> received_appcontrol_;
   std::function<void(void)> terminator_;
-  std::string uuid_;
 };
 
 }  // namespace wrt