Clean up constructors of WebApplication and NativeWindow
authorWonYoung Choi <wy80.choi@samsung.com>
Wed, 22 Apr 2015 08:03:46 +0000 (17:03 +0900)
committerWonYoung Choi <wy80.choi@samsung.com>
Wed, 22 Apr 2015 08:19:47 +0000 (17:19 +0900)
- Clean up constructors and initializers
- Move Initialize() function to private scope.
- Change initialized() to launched()
- Remove ExtensionServer dependencies.
- Add UUID to WebApplication

Change-Id: Ic54a5529c12e2860266564ddd33813064391c3f4

src/runtime/native_window.cc
src/runtime/native_window.h
src/runtime/runtime.cc
src/runtime/web_application.cc
src/runtime/web_application.h

index 9afe93f..4bb00ea 100755 (executable)
@@ -25,12 +25,12 @@ namespace {
 
 
 NativeWindow::NativeWindow()
-    : initialized_(false),
-      window_(NULL),
+    : window_(NULL),
       focus_(NULL),
       content_(NULL),
       rotation_(0),
       handler_id_(0) {
+  Initialize();
 }
 
 NativeWindow::~NativeWindow() {
@@ -130,8 +130,6 @@ void NativeWindow::Initialize() {
                                  kWinowRotationEventKey,
                                  rotation_callback,
                                  this);
-
-  initialized_ = true;
 }
 
 void NativeWindow::DidDeleteRequested(void* /*data*/,
index e13f033..00aa7f1 100755 (executable)
@@ -17,9 +17,6 @@ class NativeWindow {
   NativeWindow();
   virtual ~NativeWindow();
 
-  void Initialize();
-
-  bool initialized() const { return initialized_; }
   Evas_Object* evas_object() const;
   void SetContent(Evas_Object* content);
   void SetRotationLock(int degree);
@@ -38,11 +35,12 @@ class NativeWindow {
   static void DidDeleteRequested(void* data, Evas_Object* obj,
                                  void* event_info);
   static void DidProfileChanged(void* data, Evas_Object* obj, void* event_info);
+
+  void Initialize();
+
   void DidRotation(int degree);
   void DidFocusChanged(bool got);
 
-
-  bool initialized_;
   Evas_Object* window_;
   Evas_Object* focus_;
   Evas_Object* content_;
index e17dfa0..0df625c 100755 (executable)
@@ -20,35 +20,32 @@ namespace {
 static NativeWindow* CreateNativeWindow() {
   // TODO(wy80.choi) : consider other type of native window.
   NativeWindow* window = new NativeAppWindow();
-
-  window->Initialize();
   return window;
 }
 
 }  // namespace
 
 Runtime::Runtime()
-    : application_(NULL) {
+    : application_(NULL),
+      native_window_(NULL) {
 }
 
 Runtime::~Runtime() {
   if (application_) {
     delete application_;
   }
+  if (native_window_) {
+    delete native_window_;
+  }
 }
 
 bool Runtime::OnCreate() {
   std::string appid = CommandLine::ForCurrentProcess()->appid();
-  application_ = new WebApplication(appid);
-  if (!application_) {
-    LoggerE("WebApplication couldn't be created.");
-    return false;
-  }
-  application_->set_terminator([](){ ui_app_exit(); });
 
   // Process First Launch
   native_window_ = CreateNativeWindow();
-  application_->Initialize(native_window_);
+  application_ = new WebApplication(native_window_, appid);
+  application_->set_terminator([](){ ui_app_exit(); });
   return true;
 }
 
@@ -56,13 +53,13 @@ void Runtime::OnTerminate() {
 }
 
 void Runtime::OnPause() {
-  if (application_->initialized()) {
+  if (application_->launched()) {
     application_->Suspend();
   }
 }
 
 void Runtime::OnResume() {
-  if (application_->initialized()) {
+  if (application_->launched()) {
     application_->Resume();
   }
 }
@@ -70,7 +67,7 @@ void Runtime::OnResume() {
 void Runtime::OnAppControl(app_control_h app_control) {
   std::unique_ptr<AppControl> appcontrol(new AppControl(app_control));
 
-  if (application_->initialized()) {
+  if (application_->launched()) {
     // Process AppControl
     application_->AppControl(std::move(appcontrol));
   } else {
@@ -173,7 +170,7 @@ int Runtime::Exec(int argc, char* argv[]) {
 }  // namespace wrt
 
 int main(int argc, char* argv[]) {
-  // Initalize CommandLineParser
+  // Initialize CommandLineParser
   wrt::CommandLine::Init(argc, argv);
 
   ewk_init();
index cb87a3b..7ef8207 100755 (executable)
 #include <vector>
 #include <map>
 
-#include "common/message_types.h"
+#include "common/logger.h"
 #include "common/command_line.h"
+#include "common/string_utils.h"
 #include "runtime/native_window.h"
 #include "runtime/web_view.h"
 #include "runtime/vibration_manager.h"
-#include "common/logger.h"
 #include "runtime/app_control.h"
 #include "runtime/locale_manager.h"
 #include "runtime/application_data.h"
@@ -32,6 +32,8 @@ namespace {
   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(){"
@@ -56,52 +58,52 @@ namespace {
 
 namespace wrt {
 
-WebApplication::WebApplication(const std::string& appid)
-    : initialized_(false),
-      appid_(appid),
-      ewk_context_(ewk_context_new()),
-      locale_manager_(new LocaleManager()),
-      app_data_(new ApplicationData(appid)),
+WebApplication::WebApplication(
+    NativeWindow* window, const std::string& appid)
+    : launched_(false),
       debug_mode_(false),
-      terminator_(NULL) {
-  // app_data_path
-  std::unique_ptr<char, decltype(std::free)*>
-    path {app_get_data_path(), std::free};
-  app_data_path_ = path.get();
-
-  // extension_server
-  extension_server_ = new ExtensionServer(ewk_context_);
+      ewk_context_(NULL),
+      window_(NULL) {
+  std::unique_ptr<ApplicationData> appdata_ptr(new ApplicationData(appid));
+  Initialize(window, std::move(appdata_ptr));
 }
 
-WebApplication::WebApplication(std::unique_ptr<ApplicationData> app_data)
-    : initialized_(false),
-      appid_(app_data->app_id()),
-      ewk_context_(ewk_context_new()),
-      locale_manager_(new LocaleManager()),
-      app_data_(std::move(app_data)),
+WebApplication::WebApplication(
+    NativeWindow* window, std::unique_ptr<ApplicationData> app_data)
+    : launched_(false),
       debug_mode_(false),
-      terminator_(NULL) {
-  // app_data_path
-  std::unique_ptr<char, decltype(std::free)*>
-    path {app_get_data_path(), std::free};
-  app_data_path_ = path.get();
-
-  // extension_server
-  extension_server_ = new ExtensionServer(ewk_context_);
+      ewk_context_(NULL),
+      window_(NULL) {
+  Initialize(window, std::move(app_data));
 }
 
 WebApplication::~WebApplication() {
-  ewk_context_delete(ewk_context_);
-  if (extension_server_)
-    delete extension_server_;
+  if (ewk_context_)
+    ewk_context_delete(ewk_context_);
 }
 
-bool WebApplication::Initialize(NativeWindow* window) {
+bool WebApplication::Initialize(
+    NativeWindow* window, std::unique_ptr<ApplicationData> app_data) {
+
+  // NativeWindow
   window_ = window;
 
-  // start extension server
-  if (extension_server_)
-    extension_server_->Start();
+  // Application Id / Data
+  appid_ = app_data->app_id();
+  app_data_ = std::move(app_data);
+  std::unique_ptr<char, decltype(std::free)*>
+    path {app_get_data_path(), std::free};
+  app_data_path_ = path.get();
+
+  // Ewk Context
+  ewk_context_ =
+      ewk_context_new_with_injected_bundle_path(kInjectedBundlePath);
+
+  // Locale Manager
+  locale_manager_ = std::unique_ptr<LocaleManager>(new LocaleManager());
+
+  // UUID
+  uuid_ = utils::GenerateUUID();
 
   // ewk setting
   ewk_context_cache_model_set(ewk_context_, EWK_CACHE_MODEL_DOCUMENT_BROWSER);
@@ -142,7 +144,6 @@ bool WebApplication::Initialize(NativeWindow* window) {
 }
 
 void WebApplication::Launch(std::unique_ptr<wrt::AppControl> appcontrol) {
-  initialized_ = true;
   WebView* view = new WebView(window_, ewk_context_);
   view->SetEventListener(this);
 
@@ -171,6 +172,8 @@ void WebApplication::Launch(std::unique_ptr<wrt::AppControl> appcontrol) {
   // in Wearable, webkit can render contents before show window
   // but Mobile, webkit can't render contents before show window
   window_->Show();
+
+  launched_ = true;
 }
 
 void WebApplication::AppControl(std::unique_ptr<wrt::AppControl> appcontrol) {
@@ -280,24 +283,11 @@ void WebApplication::OnClosedWebView(WebView * view) {
   delete view;
 }
 
-
 void WebApplication::OnReceivedWrtMessage(
     WebView* /*view*/,
     Ewk_IPC_Wrt_Message_Data* message) {
-
-  Eina_Stringshare* msg_type = ewk_ipc_wrt_message_data_type_get(message);
-
-  #define START_WITHS(x, s) (strncmp(x, s, strlen(s)) == 0)
-
-  if (START_WITHS(msg_type, message_types::kExtensionTypePrefix)) {
-    extension_server_->HandleWrtMessage(message);
-  }
-
-  // TODO(wy80.choi): handle other message type?
-  // changeUserAgent, clearAllCookie, GetWindowID, hide, exit, blockedUrl
-
-
-  #undef START_WITHS
+  // TODO(wy80.choi) : Handle messages from injected bundle?
+  // ex. SendRuntimeMessage to hide / exit application.
 }
 
 void WebApplication::OnOrientationLock(WebView* view,
index a3837eb..9e5f304 100755 (executable)
@@ -11,7 +11,6 @@
 #include <functional>
 
 #include "runtime/web_view.h"
-#include "extension/extension_server.h"
 
 class Ewk_Context;
 
@@ -23,8 +22,9 @@ class LocaleManager;
 
 class WebApplication : public WebView::EventListener {
  public:
-  explicit WebApplication(const std::string& appid);
-  explicit WebApplication(std::unique_ptr<ApplicationData> app_data);
+  explicit WebApplication(NativeWindow* window, const std::string& appid);
+  explicit WebApplication(NativeWindow* window,
+                          std::unique_ptr<ApplicationData> app_data);
   virtual ~WebApplication();
 
   void AppControl(std::unique_ptr<wrt::AppControl> appcontrol);
@@ -32,11 +32,10 @@ class WebApplication : public WebView::EventListener {
   void Resume();
   void Suspend();
 
-  bool Initialize(NativeWindow* window);
   std::string data_path() const { return app_data_path_; }
-  bool initialized() const { return initialized_; }
   void set_terminator(std::function<void(void)> terminator)
       { terminator_ = terminator; }
+  bool launched() const { return launched_; }
 
   virtual void OnCreatedNewWebView(WebView* view, WebView* new_view);
   virtual void OnClosedWebView(WebView * view);
@@ -55,21 +54,26 @@ class WebApplication : public WebView::EventListener {
   virtual void OnLowMemory();
   virtual bool OnContextMenuDisabled(WebView* view);
 
+  std::string uuid() const { return uuid_; }
+
  private:
+  bool Initialize(NativeWindow* window,
+                  std::unique_ptr<ApplicationData> app_data);
+
   void ClearViewStack();
   void SendAppControlEvent();
   void LaunchInspector(wrt::AppControl* appcontrol);
 
-  bool initialized_;
-  std::string appid_;
+  bool launched_;
+  bool debug_mode_;
   Ewk_Context* ewk_context_;
   NativeWindow* window_;
-  ExtensionServer* extension_server_;
-  std::list<WebView*> view_stack_;
+  std::string appid_;
   std::string app_data_path_;
+  std::string uuid_;
+  std::list<WebView*> view_stack_;
   std::unique_ptr<LocaleManager> locale_manager_;
   std::unique_ptr<ApplicationData> app_data_;
-  bool debug_mode_;
   std::function<void(void)> terminator_;
 };