Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / browser / application.cc
index 81fa29e..4167231 100644 (file)
@@ -22,7 +22,8 @@
 #include "xwalk/application/common/constants.h"
 #include "xwalk/application/common/manifest_handlers/warp_handler.h"
 #include "xwalk/runtime/browser/runtime.h"
-#include "xwalk/runtime/browser/runtime_context.h"
+#include "xwalk/runtime/browser/runtime_ui_delegate.h"
+#include "xwalk/runtime/browser/xwalk_browser_context.h"
 #include "xwalk/runtime/browser/xwalk_runner.h"
 
 #if defined(OS_TIZEN)
@@ -48,20 +49,22 @@ GURL GetDefaultWidgetEntryPage(
     scoped_refptr<xwalk::application::ApplicationData> data) {
   base::ThreadRestrictions::SetIOAllowed(true);
   base::FileEnumerator iter(
-      data->Path(), true,
+      data->path(), true,
       base::FileEnumerator::FILES,
       FILE_PATH_LITERAL("index.*"));
   size_t priority = arraysize(kDefaultWidgetEntryPage);
   std::string source;
 
   for (base::FilePath file = iter.Next(); !file.empty(); file = iter.Next()) {
-    for (size_t i = 0; i < arraysize(kDefaultWidgetEntryPage); ++i) {
-      if (file.BaseName().MaybeAsASCII() == kDefaultWidgetEntryPage[i] &&
-          i < priority) {
+    for (size_t i = 0; i < priority; ++i) {
+      if (file.BaseName().MaybeAsASCII() == kDefaultWidgetEntryPage[i]) {
         source = kDefaultWidgetEntryPage[i];
         priority = i;
+        break;
       }
     }
+    if (!priority)
+      break;
   }
 
   return source.empty() ? GURL() : data->GetResourceURL(source);
@@ -73,7 +76,7 @@ namespace application {
 
 scoped_ptr<Application> Application::Create(
     scoped_refptr<ApplicationData> data,
-    RuntimeContext* context) {
+    XWalkBrowserContext* context) {
 #if defined(OS_TIZEN)
   return make_scoped_ptr<Application>(new ApplicationTizen(data, context));
 #else
@@ -83,17 +86,17 @@ scoped_ptr<Application> Application::Create(
 
 Application::Application(
     scoped_refptr<ApplicationData> data,
-    RuntimeContext* runtime_context)
+    XWalkBrowserContext* browser_context)
     : data_(data),
       render_process_host_(NULL),
       web_contents_(NULL),
       security_mode_enabled_(false),
-      runtime_context_(runtime_context),
+      browser_context_(browser_context),
       observer_(NULL),
       remote_debugging_enabled_(false),
       weak_factory_(this) {
-  DCHECK(runtime_context_);
-  DCHECK(data_);
+  DCHECK(browser_context_);
+  DCHECK(data_.get());
 }
 
 Application::~Application() {
@@ -103,31 +106,45 @@ Application::~Application() {
 }
 
 template<>
-GURL Application::GetStartURL<Package::WGT>() {
-  GURL url = GetAbsoluteURLFromKey(widget_keys::kLaunchLocalPathKey);
-  if (!url.is_valid()) {
-    LOG(WARNING) << "Failed to find start URL from the 'config.xml'"
-                 << "trying to find default entry page.";
-    url = GetDefaultWidgetEntryPage(data_);
-  }
-
-  if (url.is_valid()) {
+GURL Application::GetStartURL<Manifest::TYPE_WIDGET>() {
 #if defined(OS_TIZEN)
-    if (data_->IsHostedApp() && !url.SchemeIsHTTPOrHTTPS()) {
-      LOG(ERROR) << "Hosted apps are only supported with"
-                    "http:// or https:// scheme.";
-      return GURL();
+  if (data_->IsHostedApp()) {
+    std::string source;
+    GURL url;
+    if (data_->GetManifest()->GetString(
+        widget_keys::kLaunchLocalPathKey, &source)) {
+      url = GURL(source);
     }
+
+    if (url.is_valid() && url.SchemeIsHTTPOrHTTPS())
+      return url;
+  }
 #endif
+
+  GURL url = GetAbsoluteURLFromKey(widget_keys::kLaunchLocalPathKey);
+  if (url.is_valid())
+    return url;
+
+  LOG(WARNING) << "Failed to find start URL from the 'config.xml'"
+               << "trying to find default entry page.";
+  url = GetDefaultWidgetEntryPage(data_);
+  if (url.is_valid())
     return url;
-  }
 
   LOG(WARNING) << "Failed to find a valid start URL in the manifest.";
   return GURL();
 }
 
 template<>
-GURL Application::GetStartURL<Package::XPK>() {
+GURL Application::GetStartURL<Manifest::TYPE_MANIFEST>() {
+  if (data_->IsHostedApp()) {
+    std::string source;
+    // Not trying to get a relative path for the "fake" application.
+    if (data_->GetManifest()->GetString(keys::kStartURLKey, &source))
+      return GURL(source);
+    return GURL();
+  }
+
   GURL url = GetAbsoluteURLFromKey(keys::kStartURLKey);
   if (url.is_valid())
     return url;
@@ -150,7 +167,7 @@ GURL Application::GetStartURL<Package::XPK>() {
 
 
 template<>
-ui::WindowShowState Application::GetWindowShowState<Package::WGT>(
+ui::WindowShowState Application::GetWindowShowState<Manifest::TYPE_WIDGET>(
     const LaunchParams& params) {
   if (params.force_fullscreen)
     return ui::SHOW_STATE_FULLSCREEN;
@@ -171,7 +188,7 @@ ui::WindowShowState Application::GetWindowShowState<Package::WGT>(
 }
 
 template<>
-ui::WindowShowState Application::GetWindowShowState<Package::XPK>(
+ui::WindowShowState Application::GetWindowShowState<Manifest::TYPE_MANIFEST>(
     const LaunchParams& params) {
   if (params.force_fullscreen)
     return ui::SHOW_STATE_FULLSCREEN;
@@ -196,18 +213,19 @@ bool Application::Launch(const LaunchParams& launch_params) {
   }
 
   CHECK(!render_process_host_);
-  bool is_wgt = data_->GetPackageType() == Package::WGT;
+  bool is_wgt = data_->manifest_type() == Manifest::TYPE_WIDGET;
 
-  GURL url = is_wgt ? GetStartURL<Package::WGT>():
-                      GetStartURL<Package::XPK>();
+  GURL url = is_wgt ? GetStartURL<Manifest::TYPE_WIDGET>() :
+                      GetStartURL<Manifest::TYPE_MANIFEST>();
   if (!url.is_valid())
     return false;
 
   remote_debugging_enabled_ = launch_params.remote_debugging;
-
-  Runtime* runtime = Runtime::Create(
-      runtime_context_,
-      this, content::SiteInstance::CreateForURL(runtime_context_, url));
+  auto site = content::SiteInstance::CreateForURL(browser_context_, url);
+  Runtime* runtime = Runtime::Create(browser_context_, site);
+  runtime->set_observer(this);
+  runtime->set_remote_debugging_enabled(remote_debugging_enabled_);
+  runtimes_.push_back(runtime);
   render_process_host_ = runtime->GetRenderProcessHost();
   render_process_host_->AddObserver(this);
   web_contents_ = runtime->web_contents();
@@ -216,12 +234,16 @@ bool Application::Launch(const LaunchParams& launch_params) {
 
   NativeAppWindow::CreateParams params;
   params.net_wm_pid = launch_params.launcher_pid;
-  params.state = is_wgt ? GetWindowShowState<Package::WGT>(launch_params):
-                          GetWindowShowState<Package::XPK>(launch_params);
+  params.state = is_wgt ?
+      GetWindowShowState<Manifest::TYPE_WIDGET>(launch_params) :
+      GetWindowShowState<Manifest::TYPE_MANIFEST>(launch_params);
 
+  window_show_params_ = params;
+  // Only the first runtime can have a launch screen.
   params.splash_screen_path = GetSplashScreenPath();
-
-  runtime->AttachWindow(params);
+  runtime->set_ui_delegate(DefaultRuntimeUIDelegate::Create(runtime, params));
+  // We call "Show" after RP is initialized to reduce
+  // the application start up time.
 
   return true;
 }
@@ -233,16 +255,13 @@ GURL Application::GetAbsoluteURLFromKey(const std::string& key) {
   if (!manifest->GetString(key, &source) || source.empty())
     return GURL();
 
-  std::size_t found = source.find("://");
-  if (found == std::string::npos)
-    return data_->GetResourceURL(source);
-  return GURL(source);
+  return data_->GetResourceURL(source);
 }
 
 void Application::Terminate() {
-  std::set<Runtime*> to_be_closed(runtimes_);
-  std::for_each(to_be_closed.begin(), to_be_closed.end(),
-                std::mem_fun(&Runtime::Close));
+  std::vector<Runtime*> to_be_closed(runtimes_.get());
+  for (Runtime* runtime : to_be_closed)
+    runtime->Close();
 }
 
 int Application::GetRenderProcessHostID() const {
@@ -250,28 +269,28 @@ int Application::GetRenderProcessHostID() const {
   return render_process_host_->GetID();
 }
 
-void Application::OnRuntimeAdded(Runtime* runtime) {
-  DCHECK(runtime);
+void Application::OnNewRuntimeAdded(Runtime* runtime) {
   runtime->set_remote_debugging_enabled(remote_debugging_enabled_);
-  runtimes_.insert(runtime);
+  runtime->set_observer(this);
+  runtime->set_ui_delegate(
+      DefaultRuntimeUIDelegate::Create(runtime, window_show_params_));
+  runtime->Show();
+  runtimes_.push_back(runtime);
 }
 
-void Application::OnRuntimeRemoved(Runtime* runtime) {
-  DCHECK(runtime);
-  runtimes_.erase(runtime);
+void Application::OnRuntimeClosed(Runtime* runtime) {
+  auto found = std::find(runtimes_.begin(), runtimes_.end(), runtime);
+  CHECK(found != runtimes_.end());
+  LOG(INFO) << "Application::OnRuntimeClosed " << runtime;
+  runtimes_.erase(found);
 
-  if (runtimes_.empty()) {
-#if defined(OS_TIZEN_MOBILE)
-    runtime->CloseRootWindow();
-#endif
+  if (runtimes_.empty())
     base::MessageLoop::current()->PostTask(FROM_HERE,
         base::Bind(&Application::NotifyTermination,
                    weak_factory_.GetWeakPtr()));
-  }
 }
 
 void Application::RenderProcessExited(RenderProcessHost* host,
-                                      base::ProcessHandle,
                                       base::TerminationStatus,
                                       int) {
   DCHECK(render_process_host_ == host);
@@ -291,6 +310,11 @@ void Application::NotifyTermination() {
     observer_->OnApplicationTerminated(this);
 }
 
+void Application::RenderChannelCreated() {
+  CHECK(!runtimes_.empty());
+  runtimes_.front()->Show();
+}
+
 bool Application::UseExtension(const std::string& extension_name) const {
   // TODO(Bai): Tells whether the application contains the specified extension
   return true;
@@ -313,7 +337,7 @@ bool Application::RegisterPermissions(const std::string& extension_name,
   for (base::ListValue::const_iterator iter = permission_list->begin();
       iter != permission_list->end(); ++iter) {
     if (!(*iter)->IsType(base::Value::TYPE_DICTIONARY))
-        return false;
+      return false;
 
     base::DictionaryValue* dict_val =
         static_cast<base::DictionaryValue*>(*iter);
@@ -347,12 +371,12 @@ std::string Application::GetRegisteredPermissionName(
   std::map<std::string, std::string>::const_iterator iter =
       name_perm_map_.find(api_name);
   if (iter == name_perm_map_.end())
-    return std::string("");
+    return std::string();
   return iter->second;
 }
 
 StoredPermission Application::GetPermission(PermissionType type,
-                               const std::string& permission_name) const {
+    const std::string& permission_name) const {
   if (type == SESSION_PERMISSION) {
     StoredPermissionMap::const_iterator iter =
         permission_map_.find(permission_name);
@@ -384,9 +408,9 @@ bool Application::SetPermission(PermissionType type,
 void Application::InitSecurityPolicy() {
   // CSP policy takes precedence over WARP.
   if (data_->HasCSPDefined())
-    security_policy_.reset(new SecurityPolicyCSP(this));
-  else if (data_->GetPackageType() == Package::WGT)
-    security_policy_.reset(new SecurityPolicyWARP(this));
+    security_policy_.reset(new ApplicationSecurityPolicyCSP(this));
+  else if (data_->manifest_type() == Manifest::TYPE_WIDGET)
+    security_policy_.reset(new ApplicationSecurityPolicyWARP(this));
 
   if (security_policy_)
     security_policy_->Enforce();