Run Tizen Webapps in single process mode
[platform/framework/web/crosswalk-tizen.git] / atom / browser / api / atom_api_app.cc
index 7358960..1471c86 100644 (file)
@@ -30,6 +30,7 @@
 #include "base/path_service.h"
 #include "base/strings/string_util.h"
 #include "brightray/browser/brightray_paths.h"
+#include "chrome/browser/browser_process.h"
 #include "chrome/browser/icon_manager.h"
 #include "chrome/common/chrome_paths.h"
 #include "content/public/browser/browser_accessibility_state.h"
@@ -41,6 +42,7 @@
 #include "native_mate/dictionary.h"
 #include "native_mate/object_template_builder.h"
 #include "net/ssl/ssl_cert_request_info.h"
+#include "tizen/common/env_variables.h"
 #include "ui/base/l10n/l10n_util.h"
 #include "ui/gfx/image/image.h"
 
@@ -426,7 +428,7 @@ void OnClientCertificateSelected(
 
   auto certs = net::X509Certificate::CreateCertificateListFromBytes(
       data.c_str(), data.length(), net::X509Certificate::FORMAT_AUTO);
-  if (certs.size() > 0)
+  if (!certs.empty())
     delegate->ContinueWithCertificate(certs[0].get());
 }
 
@@ -472,12 +474,14 @@ int ImportIntoCertStore(
 }
 #endif
 
-void OnIconDataAvailable(const App::FileIconCallback& callback,
+void OnIconDataAvailable(v8::Isolate* isolate,
+                         const App::FileIconCallback& callback,
                          gfx::Image* icon) {
-  v8::Isolate* isolate = v8::Isolate::GetCurrent();
-  if (icon && !icon->IsEmpty()) {
+  if (!::tizen::is_single_process)
     v8::Locker locker(isolate);
-    v8::HandleScope handle_scope(isolate);
+  v8::HandleScope handle_scope(isolate);
+
+  if (icon && !icon->IsEmpty()) {
     callback.Run(v8::Null(isolate), *icon);
   } else {
     v8::Local<v8::String> error_message =
@@ -489,7 +493,8 @@ void OnIconDataAvailable(const App::FileIconCallback& callback,
 }  // namespace
 
 App::App(v8::Isolate* isolate) {
-  static_cast<AtomBrowserClient*>(AtomBrowserClient::Get())->set_delegate(this);
+  if (AtomBrowserClient::Get())
+    static_cast<AtomBrowserClient*>(AtomBrowserClient::Get())->set_delegate(this);
   Browser::Get()->AddObserver(this);
   content::GpuDataManager::GetInstance()->AddObserver(this);
   Init(isolate);
@@ -518,7 +523,7 @@ void App::OnQuit() {
   int exitCode = AtomBrowserMainParts::Get()->GetExitCode();
   Emit("quit", exitCode);
 
-  if (process_singleton_.get()) {
+  if (process_singleton_) {
     process_singleton_->Cleanup();
     process_singleton_.reset();
   }
@@ -564,7 +569,8 @@ void App::OnContinueUserActivity(
 
 void App::OnLogin(LoginHandler* login_handler,
                   const base::DictionaryValue& request_details) {
-  v8::Locker locker(isolate());
+  if (!::tizen::is_single_process)
+    v8::Locker locker(isolate());
   v8::HandleScope handle_scope(isolate());
   bool prevent_default = Emit(
       "login",
@@ -586,7 +592,8 @@ void App::OnCreateWindow(
     const scoped_refptr<content::ResourceRequestBodyImpl>& body,
     int render_process_id,
     int render_frame_id) {
-  v8::Locker locker(isolate());
+  if (!::tizen::is_single_process)
+    v8::Locker locker(isolate());
   v8::HandleScope handle_scope(isolate());
   content::RenderFrameHost* rfh =
       content::RenderFrameHost::FromID(render_process_id, render_frame_id);
@@ -613,7 +620,8 @@ void App::AllowCertificateError(
     bool expired_previous_decision,
     const base::Callback<void(content::CertificateRequestResultType)>&
         callback) {
-  v8::Locker locker(isolate());
+  if (!::tizen::is_single_process)
+    v8::Locker locker(isolate());
   v8::HandleScope handle_scope(isolate());
   bool prevent_default = Emit("certificate-error",
                               WebContents::CreateFrom(isolate(), web_contents),
@@ -653,6 +661,14 @@ void App::OnGpuProcessCrashed(base::TerminationStatus status) {
     status == base::TERMINATION_STATUS_PROCESS_WAS_KILLED);
 }
 
+base::FilePath App::GetAppPath() const {
+  return app_path_;
+}
+
+void App::SetAppPath(const base::FilePath& app_path) {
+  app_path_ = app_path;
+}
+
 base::FilePath App::GetPath(mate::Arguments* args, const std::string& name) {
   bool succeed = false;
   base::FilePath path;
@@ -693,7 +709,7 @@ std::string App::GetLocale() {
 
 bool App::MakeSingleInstance(
     const ProcessSingleton::NotificationCallback& callback) {
-  if (process_singleton_.get())
+  if (process_singleton_)
     return false;
 
   base::FilePath user_dir;
@@ -714,7 +730,7 @@ bool App::MakeSingleInstance(
 }
 
 void App::ReleaseSingleInstance() {
-  if (process_singleton_.get()) {
+  if (process_singleton_) {
     process_singleton_->Cleanup();
     process_singleton_.reset();
   }
@@ -871,6 +887,12 @@ void App::GetFileIcon(const base::FilePath& path,
   IconLoader::IconSize icon_size;
   FileIconCallback callback;
 
+  if (!::tizen::is_single_process)
+    v8::Locker locker(isolate());
+  v8::HandleScope handle_scope(isolate());
+
+  base::FilePath normalized_path = path.NormalizePathSeparators();
+
   if (!args->GetNext(&options)) {
     icon_size = IconLoader::IconSize::NORMAL;
   } else {
@@ -880,20 +902,20 @@ void App::GetFileIcon(const base::FilePath& path,
   }
 
   if (!args->GetNext(&callback)) {
-    args->ThrowError();
+    args->ThrowError("Missing required callback function");
     return;
   }
 
-  IconManager* icon_manager = IconManager::GetInstance();
-  gfx::Image* icon = icon_manager->LookupIconFromFilepath(path, icon_size);
+  auto icon_manager = g_browser_process->GetIconManager();
+  gfx::Image* icon =
+      icon_manager->LookupIconFromFilepath(normalized_path, icon_size);
   if (icon) {
-    v8::Isolate* isolate = v8::Isolate::GetCurrent();
-    v8::Locker locker(isolate);
-    v8::HandleScope handle_scope(isolate);
-    callback.Run(v8::Null(isolate), *icon);
+    callback.Run(v8::Null(isolate()), *icon);
   } else {
-    icon_manager->LoadIcon(path, icon_size,
-                           base::Bind(&OnIconDataAvailable, callback));
+    icon_manager->LoadIcon(
+        normalized_path, icon_size,
+        base::Bind(&OnIconDataAvailable, isolate(), callback),
+        &cancelable_task_tracker_);
   }
 }
 
@@ -952,6 +974,8 @@ void App::BuildPrototype(
       .SetMethod("isUnityRunning",
                  base::Bind(&Browser::IsUnityRunning, browser))
 #endif
+      .SetMethod("setAppPath", &App::SetAppPath)
+      .SetMethod("getAppPath", &App::GetAppPath)
       .SetMethod("setPath", &App::SetPath)
       .SetMethod("getPath", &App::GetPath)
       .SetMethod("setDesktopName", &App::SetDesktopName)