WebView and WebApplication basic behavior and struct implements
authorSeungkeun Lee <sngn.lee@samsung.com>
Fri, 3 Apr 2015 03:45:50 +0000 (12:45 +0900)
committerSeungkeun Lee <sngn.lee@samsung.com>
Fri, 3 Apr 2015 09:38:12 +0000 (18:38 +0900)
Change-Id: Iac21b2613e13a4dfc02b10ba4de1181da8c4d6be

packaging/wrt.spec [changed mode: 0644->0755]
src/runtime/CMakeLists.txt [changed mode: 0644->0755]
src/runtime/native_app_window.cc [changed mode: 0644->0755]
src/runtime/native_window.cc [changed mode: 0644->0755]
src/runtime/native_window.h [changed mode: 0644->0755]
src/runtime/runtime.cc [changed mode: 0644->0755]
src/runtime/runtime.h [changed mode: 0644->0755]
src/runtime/web_application.cc [changed mode: 0644->0755]
src/runtime/web_application.h [changed mode: 0644->0755]
src/runtime/web_view.cc [changed mode: 0644->0755]
src/runtime/web_view.h [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index 4a24aab..3dfacd3
@@ -21,6 +21,9 @@ BuildRequires: pkgconfig(ecore-x)
 BuildRequires: pkgconfig(ecore-wayland)
 %endif
 
+#web-engine
+BuildRequires:  pkgconfig(chromium-efl)
+
 %description
 Runtime for Web Application
 
old mode 100644 (file)
new mode 100755 (executable)
index e674f8e..2c653a4
@@ -15,6 +15,7 @@ PKG_CHECK_MODULES(TARGET_RUNTIME_DEPS
   ${WIN_PKG}
   elementary
   capi-appfw-application
+  chromium-efl
   REQUIRED
 )
 
@@ -36,6 +37,7 @@ SET(TARGET_RUNTIME_SRCS
   ${PROJECT_SOURCE_DIR}/src/runtime/native_app_window.cc
   ${PROJECT_SOURCE_DIR}/src/runtime/web_application.cc
   ${PROJECT_SOURCE_DIR}/src/runtime/runtime.cc
+  ${PROJECT_SOURCE_DIR}/src/runtime/web_view.cc
 )
 
 # Compiler Flags
old mode 100644 (file)
new mode 100755 (executable)
index e78f824..d1f4c7d
@@ -4,6 +4,8 @@
 
 #include "native_app_window.h"
 
+#include <Elementary.h>
+
 namespace wrt {
 
 NativeAppWindow::NativeAppWindow() {
old mode 100644 (file)
new mode 100755 (executable)
index 02a0dbe..fde9218
@@ -114,6 +114,14 @@ void NativeWindow::didProfileChanged(void* data,
   LoggerD("didProfileChanged");
 }
 
+Evas_Object* NativeWindow::evas_object() const {
+  return window_;
+}
+
+void NativeWindow::SetContent(Evas_Object* content) {
+  //TODO. swallow content into focus
+}
+
 
 
 } // namespace wrt
old mode 100644 (file)
new mode 100755 (executable)
index dae4491..e9e6dec
@@ -4,7 +4,6 @@
 
 #ifndef WRT_RUNTIME_NATIVE_WINDOW_H_
 #define WRT_RUNTIME_NATIVE_WINDOW_H_
-
 #include <Elementary.h>
 
 namespace wrt {
@@ -17,6 +16,8 @@ class NativeWindow {
   void Initialize();
 
   bool initialized() const { return initialized_; }
+  Evas_Object* evas_object() const;
+  void SetContent(Evas_Object* content);
 
  protected:
   virtual Evas_Object* createWindowInternal() = 0;
old mode 100644 (file)
new mode 100755 (executable)
index 3c10ae1..8d22677
@@ -20,47 +20,85 @@ Runtime::~Runtime() {
   }
 }
 
+bool Runtime::OnCreate(){
+  std::string appid = CommandLine::ForCurrentProcess()->appid();
+  application_ = new WebApplication(appid);
+  if (!application_) {
+    LoggerE("WebApplication couldn't be created.");
+    return false;
+  }
+
+  // Process First Launch
+  createNativeWindow();
+  application_->Initialize(native_window_);
+  return true;
+}
+
 bool Runtime::onCreate(void* data) {
   Runtime* runtime = reinterpret_cast<Runtime*>(data);
   if (!runtime) {
     LoggerE("Runtime has not been created.");
     return false;
   }
-  std::string appid = CommandLine::ForCurrentProcess()->appid();
-  runtime->application_ = new WebApplication(appid);
-  if (!runtime->application_) {
-    LoggerE("WebApplication couldn't be created.");
-    return false;
-  }
-  return true;
+  return runtime->OnCreate();
 }
 
+void Runtime::OnTerminate(){
+}
 void Runtime::onTerminate(void* data) {
+  Runtime* runtime = reinterpret_cast<Runtime*>(data);
+  if (!runtime) {
+    LoggerE("Runtime has not been created.");
+    return;
+  }
+  runtime->OnTerminate();
+}
+
+void Runtime::OnPause(){
+  if (application_->initialized()) {
+    application_->Suspend();
+  }
 }
 
 void Runtime::onPause(void* data) {
   Runtime* runtime = reinterpret_cast<Runtime*>(data);
-  if (runtime->application_->initialized()) {
-    runtime->application_->Suspend();
+  if (!runtime) {
+    LoggerE("Runtime has not been created.");
+    return;
   }
+  runtime->OnPause();
 }
 
+void Runtime::OnResume(){
+  if (application_->initialized()) {
+    application_->Resume();
+  }
+}
 void Runtime::onResume(void* data) {
   Runtime* runtime = reinterpret_cast<Runtime*>(data);
-  if (runtime->application_->initialized()) {
-    runtime->application_->Resume();
+  if (!runtime) {
+    LoggerE("Runtime has not been created.");
+    return;
   }
+  runtime->OnResume();
 }
 
-void Runtime::onAppControl(app_control_h app_control, void* data) {
-  Runtime* runtime = reinterpret_cast<Runtime*>(data);
-  if (runtime->application_->initialized()) {
+void Runtime::OnAppControl(app_control_h app_control){
+  if (application_->initialized()) {
     // Process AppControl
+    application_->AppControl();
   } else {
-    // Process First Launch
-    runtime->createNativeWindow();
-    runtime->application_->Launch();
+    application_->Launch();
+  }
+
+}
+void Runtime::onAppControl(app_control_h app_control, void* data) {
+  Runtime* runtime = reinterpret_cast<Runtime*>(data);
+  if (!runtime) {
+    LoggerE("Runtime has not been created.");
+    return;
   }
+  runtime->OnAppControl(app_control);
 }
 
 int Runtime::Exec(int argc, char* argv[]) {
old mode 100644 (file)
new mode 100755 (executable)
index 6c02b4d..a7207e5
@@ -7,8 +7,8 @@
 
 #include <app.h>
 
-#include "web_application.h"
 #include "native_window.h"
+#include "web_application.h"
 
 namespace wrt {
 
@@ -17,7 +17,15 @@ class Runtime {
   Runtime();
   virtual ~Runtime();
 
-  int Exec(int argc, char* argv[]);
+  virtual int Exec(int argc, char* argv[]);
+
+ protected:
+  virtual bool OnCreate();
+  virtual void OnTerminate();
+  virtual void OnPause();
+  virtual void OnResume();
+  virtual void OnAppControl(app_control_h app_control);
+
  private:
   static bool onCreate(void* data);
   static void onTerminate(void* data);
old mode 100644 (file)
new mode 100755 (executable)
index ada737a..e65eb86
 
 #include "web_application.h"
 
+#include <algorithm>
+#include <ewk_chromium.h>
+
+#include "native_window.h"
+#include "command_line.h"
+#include "web_view.h"
+
+
+namespace {
+  const char* kAppControlEventScript = \
+        "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); }";
+}
+
 namespace wrt {
 
 WebApplication::WebApplication(const std::string& appid)
     : initialized_(false),
-      appid_(appid) {
+      appid_(appid), ewk_context_(ewk_context_new()) {
 }
 
 WebApplication::~WebApplication() {
+  ewk_context_delete(ewk_context_);
 }
 
-void WebApplication::Launch() {
+bool WebApplication::Initialize(NativeWindow* window) {
+  window_ = window;
+
+  char* chromium_arg_options[] = {
+    CommandLine::ForCurrentProcess()->argv()[0],
+    const_cast<char*>("--enable-file-cookies"),
+    const_cast<char*>("--allow-file-access-from-files"),
+    const_cast<char*>("--allow-universal-access-from-files")
+  };
+  const int chromium_arg_cnt = sizeof(chromium_arg_options) / sizeof(chromium_arg_options[0]);
+  ewk_set_arguments(chromium_arg_cnt, chromium_arg_options);
+
+  //ewk setting
+  ewk_context_cache_model_set(ewk_context_, EWK_CACHE_MODEL_DOCUMENT_BROWSER);
+
+  //cookie
+  auto cookie_manager = ewk_context_cookie_manager_get(ewk_context_);
+  ewk_cookie_manager_accept_policy_set(cookie_manager, EWK_COOKIE_ACCEPT_POLICY_ALWAYS);
 
+  //set persistent storage path
+  std::string cookie_path = GetDataPath() + ".cookie";
+  ewk_cookie_manager_persistent_storage_set(cookie_manager, cookie_path.c_str(), EWK_COOKIE_PERSISTENT_STORAGE_SQLITE);
+
+  //TODO. Find the path of certificate file
+  //ewk_context_certificate_file_set(ewk_context_, .... );
+
+  //TODO. find the proxy url
+  //ewk_context_proxy_uri_set(ewk_context_, ... );
+  return true;
+}
+
+void WebApplication::Launch() {
   initialized_ = true;
+  WebView* view = new WebView(window_, ewk_context_);
+
+  //TODO. Get the start file
+  view->LoadUrl("index.html");
+  view_stack_.push_front(view);
+  window_->SetContent(view->evas_object());
+}
+
+void WebApplication::AppControl() {
+
+  //TODO. find the app control url and the reset options
+
+  //TODO. Set the injected bundle into extension process
+
+
+  if (true) {
+    //Reset to context
+    ClearViewStack();
+    WebView* view = new WebView(window_, ewk_context_);
+    view->LoadUrl("index.html");
+    view_stack_.push_front(view);
+    window_->SetContent(view->evas_object());
+  } else {
+    //Send Event
+    SendAppControlEvent();
+  }
+}
+
+void WebApplication::SendAppControlEvent() {
+  auto it = view_stack_.begin();
+  while (it != view_stack_.end()) {
+    (*it)->EvalJavascript(kAppControlEventScript);
+  }
+}
+
+void WebApplication::ClearViewStack() {
+  auto it = view_stack_.begin();
+  while (it != view_stack_.end()) {
+    (*it)->Suspend();
+    delete *it;
+  }
+  view_stack_.clear();
 }
 
 void WebApplication::Resume() {
+  if (view_stack_.size() > 0 && view_stack_.front() != NULL)
+    view_stack_.front()->Resume();
 
 }
 
 void WebApplication::Suspend() {
+  if (view_stack_.size() > 0 && view_stack_.front() != NULL)
+    view_stack_.front()->Suspend();
+}
+
 
+void WebApplication::OnCreatedNewWebView(WebView* view, WebView* new_view) {
+  view->Suspend();
+  if (view_stack_.size() > 0 && view_stack_.front() != NULL)
+    view_stack_.front()->Suspend();
+
+  //TODO. check the background support option
+  //new_view.AlwaysRun(false);
+  view_stack_.push_front(new_view);
+  window_->SetContent(new_view->evas_object());
 }
 
+void WebApplication::OnClosedWebView(WebView * view) {
+  if (view_stack_.size() == 0)
+    return;
+
+  WebView* current = view_stack_.front();
+  if (current == view) {
+    view_stack_.pop_front();
+  } else {
+    auto found = std::find(view_stack_.begin(), view_stack_.end(), view);
+    if(found != view_stack_.end()) {
+      view_stack_.erase(found);
+    }
+  }
+
+  delete view;
+
+  if (view_stack_.size() == 0) {
+    //TODO. terminate the webapp
+  } else if (current != view_stack_.front()){
+    window_->SetContent(view_stack_.front()->evas_object());
+  }
+}
+
+
+
+std::string WebApplication::GetDataPath() const {
+  //TODO. To be Implements
+  return std::string("./");
+}
 
 } // namespace wrt
old mode 100644 (file)
new mode 100755 (executable)
index 5a9ad71..a972a8b
@@ -6,23 +6,40 @@
 #define WRT_RUNTIME_WEB_APPLICATION_H_
 
 #include <string>
+#include <list>
+#include "web_view.h"
+
+class Ewk_Context;
 
 namespace wrt {
+class NativeWindow;
 
-class WebApplication {
+class WebApplication : public WebView::EventListener {
  public:
   WebApplication(const std::string& appid);
   virtual ~WebApplication();
 
+  void AppControl();
   void Launch();
   void Resume();
   void Suspend();
 
+  bool Initialize(NativeWindow* window);
+  std::string GetDataPath() const;
   bool initialized() const { return initialized_; }
 
+  virtual void OnCreatedNewWebView(WebView* view, WebView* new_view);
+  virtual void OnClosedWebView(WebView * view);
+
  private:
+  void ClearViewStack();
+  void SendAppControlEvent();
+
   bool initialized_;
   std::string appid_;
+  Ewk_Context* ewk_context_;
+  NativeWindow* window_;
+  std::list<WebView*> view_stack_;
 
 };
 
old mode 100644 (file)
new mode 100755 (executable)
index 09a75a1..26ed655
@@ -1,3 +1,56 @@
 // 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.
\ No newline at end of file
+// found in the LICENSE file.
+
+#include "web_view.h"
+
+#include "native_window.h"
+
+using namespace std;
+namespace wrt {
+
+WebView::WebView(NativeWindow * window,Ewk_Context * context): window_(window), context_(context), always_run_(false) {
+}
+
+void WebView::LoadUrl(const std::string& url) {
+  //TODO. To be implemented
+}
+
+void WebView::Suspend() {
+  if (!always_run_) {
+    //suspend webview
+  }
+  // change the visibility
+}
+
+void WebView::Resume() {
+  if (!always_run_) {
+    // resume webview
+  }
+  // change the visiblity
+}
+
+void WebView::Reload() {
+}
+
+void WebView::AlwaysRun(bool run) {
+  always_run_ = run;
+}
+
+bool WebView::EvalJavascript(const std::string& script) {
+  return false;
+}
+
+void WebView::Initialize() {
+}
+
+std::string WebView::GetUrl() {
+  return std::string();
+}
+
+Evas_Object* WebView::evas_object() const {
+  //TODO
+  return NULL;
+}
+
+}
\ No newline at end of file
old mode 100644 (file)
new mode 100755 (executable)
index 09a75a1..fab406d
@@ -1,3 +1,53 @@
 // 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.
\ No newline at end of file
+// found in the LICENSE file.
+
+#ifndef WRT_RUNTIME_WEB_VIEW_H_
+#define WRT_RUNTIME_WEB_VIEW_H_
+#include <string>
+#include <Elementary.h>
+
+class Ewk_Context;
+
+namespace wrt {
+class NativeWindow;
+
+class WebView {
+ public:
+  class EventListener {
+   public:
+    virtual void OnLoadStart(WebView* view) {};
+    virtual void OnLoadProgress(WebView* view, double persent ) {};
+    virtual void OnLoadFinished(WebView* view) {};
+    virtual void OnRendered(WebView* view) {};
+    virtual void OnCreatedNewWebView(WebView* view, WebView* new_view) {};
+    virtual void OnClosedWebView(WebView* view) {};
+    virtual void OnCrashed(WebView* view) {};
+    virtual bool OnDidOpenWindow(WebView* view) { return true; };
+  };
+
+  WebView(wrt::NativeWindow* window, Ewk_Context* context);
+
+  void LoadUrl(const std::string& url);
+  std::string GetUrl();
+
+  void Suspend();
+  void Resume();
+  void Reload();
+  void AlwaysRun(bool run);
+  bool EvalJavascript(const std::string& script);
+
+  void SetEventListener(EventListener* listener);
+  Evas_Object* evas_object() const;
+
+ private:
+  void Initialize();
+  NativeWindow* window_;
+  Ewk_Context* context_;
+  bool always_run_;
+
+};
+
+} // namespace wrt
+
+#endif // WRT_RUNTIME_WEB_VIEW_H_