Upstream version 11.39.244.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / browser / application.cc
index deee238..8b7acc4 100644 (file)
 
 #include <string>
 
+#include "base/files/file_enumerator.h"
+#include "base/json/json_reader.h"
+#include "base/macros.h"
 #include "base/message_loop/message_loop.h"
 #include "base/stl_util.h"
+#include "base/strings/string_split.h"
+#include "base/threading/thread_restrictions.h"
+#include "base/values.h"
 #include "content/public/browser/web_contents.h"
 #include "content/public/browser/render_process_host.h"
+#include "content/public/browser/site_instance.h"
 #include "net/base/net_util.h"
-#include "xwalk/application/browser/application_event_manager.h"
-#include "xwalk/application/browser/application_service.h"
-#include "xwalk/application/browser/application_storage.h"
-#include "xwalk/application/browser/application_system.h"
 #include "xwalk/application/common/application_manifest_constants.h"
 #include "xwalk/application/common/constants.h"
-#include "xwalk/application/common/manifest_handlers/main_document_handler.h"
-#include "xwalk/application/common/event_names.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_defered_ui_strategy.h"
 #include "xwalk/runtime/browser/xwalk_runner.h"
 
+#if defined(OS_TIZEN)
+#include "xwalk/application/browser/application_tizen.h"
+#endif
+
+using content::RenderProcessHost;
+
 namespace xwalk {
 
 namespace keys = application_manifest_keys;
+namespace widget_keys = application_widget_keys;
+
+namespace {
+const char* kDefaultWidgetEntryPage[] = {
+"index.html",
+"index.htm",
+"index.svg",
+"index.xhtml",
+"index.xht"};
+
+GURL GetDefaultWidgetEntryPage(
+    scoped_refptr<xwalk::application::ApplicationData> data) {
+  base::ThreadRestrictions::SetIOAllowed(true);
+  base::FileEnumerator iter(
+      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) {
+        source = kDefaultWidgetEntryPage[i];
+        priority = i;
+      }
+    }
+  }
 
-namespace application {
+  return source.empty() ? GURL() : data->GetResourceURL(source);
+}
 
-class FinishEventObserver : public EventObserver {
- public:
-  FinishEventObserver(
-      ApplicationEventManager* event_manager,
-      Application* application)
-      : EventObserver(event_manager),
-        application_(application) {
-  }
+}  // namespace
 
-  virtual void Observe(const std::string& app_id,
-                       scoped_refptr<Event> event) OVERRIDE {
-    DCHECK(xwalk::application::kOnJavaScriptEventAck == event->name());
-    std::string ack_event_name;
-    event->args()->GetString(0, &ack_event_name);
-    if (ack_event_name == xwalk::application::kOnSuspend)
-      application_->CloseMainDocument();
-  }
+namespace application {
 
- private:
-  Application* application_;
-};
+scoped_ptr<Application> Application::Create(
+    scoped_refptr<ApplicationData> data,
+    RuntimeContext* context) {
+#if defined(OS_TIZEN)
+  return make_scoped_ptr<Application>(new ApplicationTizen(data, context));
+#else
+  return make_scoped_ptr(new Application(data, context));
+#endif
+}
 
 Application::Application(
     scoped_refptr<ApplicationData> data,
-    RuntimeContext* runtime_context,
-    Observer* observer)
-    : runtime_context_(runtime_context),
-      application_data_(data),
-      main_runtime_(NULL),
-      observer_(observer) {
+    RuntimeContext* runtime_context)
+    : data_(data),
+      render_process_host_(NULL),
+      web_contents_(NULL),
+      security_mode_enabled_(false),
+      runtime_context_(runtime_context),
+      observer_(NULL),
+      ui_strategy_(new RuntimeDeferedUIStrategy),
+      remote_debugging_enabled_(false),
+      weak_factory_(this) {
   DCHECK(runtime_context_);
-  DCHECK(application_data_);
-  DCHECK(observer_);
+  DCHECK(data_.get());
 }
 
 Application::~Application() {
+  Terminate();
+  if (render_process_host_)
+    render_process_host_->RemoveObserver(this);
 }
 
 template<>
-bool Application::TryLaunchAt<Application::AppMainKey>() {
-  const MainDocumentInfo* main_info =
-      ToMainDocumentInfo(application_data_->GetManifestData(keys::kAppMainKey));
-  if (!main_info || !main_info->GetMainURL().is_valid()) {
-    LOG(WARNING) << "Can't find a valid main document URL for app.";
-    return false;
+GURL Application::GetStartURL<Manifest::TYPE_WIDGET>() {
+#if defined(OS_TIZEN)
+  if (data_->IsHostedApp()) {
+    std::string source;
+    data_->GetManifest()->GetString(widget_keys::kLaunchLocalPathKey, &source);
+    GURL url = GURL(source);
+
+    if (url.is_valid() && url.SchemeIsHTTPOrHTTPS())
+      return url;
   }
+#endif
 
-  DCHECK(HasMainDocument());
-  main_runtime_ = Runtime::Create(runtime_context_, this);
-  main_runtime_->LoadURL(main_info->GetMainURL());
-  return true;
+  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<>
-bool Application::TryLaunchAt<Application::LaunchLocalPathKey>() {
-  const Manifest* manifest = application_data_->GetManifest();
-  std::string entry_page;
-  if (manifest->GetString(application_manifest_keys::kLaunchLocalPathKey,
-        &entry_page) && !entry_page.empty()) {
-    GURL url = application_data_->GetResourceURL(entry_page);
-    if (url.is_empty()) {
-      LOG(WARNING) << "Can't find a valid local path URL for app.";
-      return false;
-    }
+GURL Application::GetStartURL<Manifest::TYPE_MANIFEST>() {
+  if (data_->IsHostedApp()) {
+    std::string source;
+    data_->GetManifest()->GetString(keys::kStartURLKey, &source);
+    // Not trying to get a relative path for the "fake" application.
+    return GURL(source);
+  }
 
-    // main_runtime_ should be initialized before 'LoadURL' call,
-    // so that it is in place already when application extensions
-    // are created.
-    main_runtime_= Runtime::Create(runtime_context_, this);
-    main_runtime_->LoadURL(url);
-    main_runtime_->AttachDefaultWindow();
-    return true;
+  GURL url = GetAbsoluteURLFromKey(keys::kStartURLKey);
+  if (url.is_valid())
+    return url;
+
+  url = GetAbsoluteURLFromKey(keys::kLaunchLocalPathKey);
+  if (url.is_valid())
+    return url;
+
+  url = GetAbsoluteURLFromKey(keys::kDeprecatedURLKey);
+  if (url.is_valid()) {
+    LOG(WARNING) << "Deprecated key '" << keys::kDeprecatedURLKey
+        << "' found. Please migrate to using '" << keys::kStartURLKey
+        << "' instead.";
+    return url;
   }
 
-  return false;
+  LOG(WARNING) << "Failed to find a valid start URL in the manifest.";
+  return GURL();
 }
 
+
 template<>
-bool Application::TryLaunchAt<Application::URLKey>() {
-  const Manifest* manifest = application_data_->GetManifest();
-  std::string url_string;
-  if (manifest->GetString(application_manifest_keys::kURLKey,
-      &url_string)) {
-    GURL url(url_string);
-    if (!url.is_valid()) {
-      LOG(WARNING) << "Can't find a valid URL for app.";
-      return false;
-    }
+ui::WindowShowState Application::GetWindowShowState<Manifest::TYPE_WIDGET>(
+    const LaunchParams& params) {
+  if (params.force_fullscreen)
+    return ui::SHOW_STATE_FULLSCREEN;
+
+  const Manifest* manifest = data_->GetManifest();
+  std::string view_modes_string;
+  if (manifest->GetString(widget_keys::kViewModesKey, &view_modes_string)) {
+    // FIXME: ATM only 'fullscreen' and 'windowed' values are supported.
+    // If the first user prefererence is 'fullscreen', set window show state
+    // FULLSCREEN, otherwise set the default window show state.
+    std::vector<std::string> modes;
+    base::SplitString(view_modes_string, ' ', &modes);
+    if (!modes.empty() && modes[0] == "fullscreen")
+      return ui::SHOW_STATE_FULLSCREEN;
+  }
 
-    // main_runtime_ should be initialized before 'LoadURL' call,
-    // so that it is in place already when application extensions
-    // are created.
-    main_runtime_= Runtime::Create(runtime_context_, this);
-    main_runtime_->LoadURL(url);
-    main_runtime_->AttachDefaultWindow();
-    return true;
+  return ui::SHOW_STATE_DEFAULT;
+}
+
+template<>
+ui::WindowShowState Application::GetWindowShowState<Manifest::TYPE_MANIFEST>(
+    const LaunchParams& params) {
+  if (params.force_fullscreen)
+    return ui::SHOW_STATE_FULLSCREEN;
+
+  const Manifest* manifest = data_->GetManifest();
+  std::string display_string;
+  if (manifest->GetString(keys::kDisplay, &display_string)) {
+    // FIXME: ATM only 'fullscreen' and 'standalone' (which is fallback value)
+    // values are supported.
+    if (display_string.find("fullscreen") != std::string::npos)
+      return ui::SHOW_STATE_FULLSCREEN;
   }
 
-  return false;
+  return ui::SHOW_STATE_DEFAULT;
 }
 
 bool Application::Launch(const LaunchParams& launch_params) {
   if (!runtimes_.empty()) {
-    LOG(ERROR) << "Attempt to launch app: " << id()
-               << " that was already launched.";
+    LOG(ERROR) << "Attempt to launch app with id " << id()
+               << ", but it is already running.";
     return false;
   }
 
-  if ((launch_params.entry_points & AppMainKey) && TryLaunchAt<AppMainKey>())
-    return true;
-  if ((launch_params.entry_points & LaunchLocalPathKey)
-      && TryLaunchAt<LaunchLocalPathKey>())
-    return true;
-  if ((launch_params.entry_points & URLKey) && TryLaunchAt<URLKey>())
-    return true;
+  CHECK(!render_process_host_);
+  bool is_wgt = data_->manifest_type() == Manifest::TYPE_WIDGET;
 
-  return false;
+  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));
+  render_process_host_ = runtime->GetRenderProcessHost();
+  render_process_host_->AddObserver(this);
+  web_contents_ = runtime->web_contents();
+  InitSecurityPolicy();
+  runtime->LoadURL(url);
+
+  NativeAppWindow::CreateParams params;
+  params.net_wm_pid = launch_params.launcher_pid;
+  params.state = is_wgt ?
+      GetWindowShowState<Manifest::TYPE_WIDGET>(launch_params):
+      GetWindowShowState<Manifest::TYPE_MANIFEST>(launch_params);
+  window_show_params_.state = params.state;
+
+  params.splash_screen_path = GetSplashScreenPath();
+
+  ui_strategy_->Show(runtime, params);
+
+  return true;
+}
+
+GURL Application::GetAbsoluteURLFromKey(const std::string& key) {
+  const Manifest* manifest = data_->GetManifest();
+  std::string source;
+
+  if (!manifest->GetString(key, &source) || source.empty())
+    return GURL();
+
+  return data_->GetResourceURL(source);
 }
 
 void Application::Terminate() {
   std::set<Runtime*> to_be_closed(runtimes_);
-  if (HasMainDocument() && to_be_closed.size() > 1) {
-    // The main document runtime is closed separately
-    // (needs some extra logic) in Application::OnRuntimeRemoved.
-    to_be_closed.erase(main_runtime_);
-  }
+  std::for_each(to_be_closed.begin(), to_be_closed.end(),
+                std::mem_fun(&Runtime::Close));
+}
 
-  std::set<Runtime*>::iterator it = to_be_closed.begin();
-  for (; it!= to_be_closed.end(); ++it)
-    (*it)->Close();
+int Application::GetRenderProcessHostID() const {
+  DCHECK(render_process_host_);
+  return render_process_host_->GetID();
 }
 
 void Application::OnRuntimeAdded(Runtime* runtime) {
   DCHECK(runtime);
+  runtime->set_remote_debugging_enabled(remote_debugging_enabled_);
+  if (!runtimes_.empty())
+    ui_strategy_->Show(runtime, window_show_params_);
   runtimes_.insert(runtime);
 }
 
@@ -170,62 +273,143 @@ void Application::OnRuntimeRemoved(Runtime* runtime) {
   DCHECK(runtime);
   runtimes_.erase(runtime);
 
-  if (runtimes_.empty()) {
+  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);
+  VLOG(1) << "RenderProcess id: " << host->GetID() << " is gone!";
+  XWalkRunner::GetInstance()->OnRenderProcessHostGone(host);
+}
+
+void Application::RenderProcessHostDestroyed(RenderProcessHost* host) {
+  DCHECK(render_process_host_ == host);
+  render_process_host_ = NULL;
+  web_contents_ = NULL;
+}
+
+void Application::NotifyTermination() {
+  CHECK(!render_process_host_);
+  if (observer_)
     observer_->OnApplicationTerminated(this);
-    return;
-  }
+}
+
+bool Application::UseExtension(const std::string& extension_name) const {
+  // TODO(Bai): Tells whether the application contains the specified extension
+  return true;
+}
 
-  if (runtimes_.size() == 1 && HasMainDocument() &&
-      ContainsKey(runtimes_, main_runtime_)) {
-    ApplicationSystem* system = XWalkRunner::GetInstance()->app_system();
-    ApplicationEventManager* event_manager = system->event_manager();
+bool Application::RegisterPermissions(const std::string& extension_name,
+                                      const std::string& perm_table) {
+  // TODO(Bai): Parse the permission table and fill in the name_perm_map_
+  // The perm_table format is a simple JSON string, like
+  // [{"permission_name":"echo","apis":["add","remove","get"]}]
+  scoped_ptr<base::Value> root;
+  root.reset(base::JSONReader().ReadToValue(perm_table));
+  if (root.get() == NULL || !root->IsType(base::Value::TYPE_LIST))
+    return false;
 
-    // If onSuspend is not registered in main document,
-    // we close the main document immediately.
-    if (!IsOnSuspendHandlerRegistered()) {
-      CloseMainDocument();
-      return;
-    }
+  base::ListValue* permission_list = static_cast<base::ListValue*>(root.get());
+  if (permission_list->GetSize() == 0)
+    return false;
 
-    DCHECK(!finish_observer_);
-    finish_observer_.reset(
-        new FinishEventObserver(event_manager, this));
-    event_manager->AttachObserver(
-        application_data_->ID(), kOnJavaScriptEventAck,
-        finish_observer_.get());
-
-    scoped_ptr<base::ListValue> event_args(new base::ListValue);
-    scoped_refptr<Event> event = Event::CreateEvent(
-        xwalk::application::kOnSuspend, event_args.Pass());
-    event_manager->SendEvent(application_data_->ID(), event);
+  for (base::ListValue::const_iterator iter = permission_list->begin();
+      iter != permission_list->end(); ++iter) {
+    if (!(*iter)->IsType(base::Value::TYPE_DICTIONARY))
+        return false;
+
+    base::DictionaryValue* dict_val =
+        static_cast<base::DictionaryValue*>(*iter);
+    std::string permission_name;
+    if (!dict_val->GetString("permission_name", &permission_name))
+      return false;
+
+    base::ListValue* api_list = NULL;
+    if (!dict_val->GetList("apis", &api_list))
+      return false;
+
+    for (base::ListValue::const_iterator api_iter = api_list->begin();
+        api_iter != api_list->end(); ++api_iter) {
+      std::string api;
+      if (!((*api_iter)->IsType(base::Value::TYPE_STRING)
+          && (*api_iter)->GetAsString(&api)))
+        return false;
+      // register the permission and api
+      name_perm_map_[api] = permission_name;
+      DLOG(INFO) << "Permission Registered [PERM] " << permission_name
+                 << " [API] " << api;
+    }
   }
-}
 
-void Application::CloseMainDocument() {
-  DCHECK(main_runtime_);
+  return true;
+}
 
-  finish_observer_.reset();
-  main_runtime_->Close();
-  main_runtime_ = NULL;
+std::string Application::GetRegisteredPermissionName(
+    const std::string& extension_name,
+    const std::string& api_name) const {
+  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 iter->second;
 }
 
-Runtime* Application::GetMainDocumentRuntime() const {
-  return HasMainDocument() ? main_runtime_ : NULL;
+StoredPermission Application::GetPermission(PermissionType type,
+                               const std::string& permission_name) const {
+  if (type == SESSION_PERMISSION) {
+    StoredPermissionMap::const_iterator iter =
+        permission_map_.find(permission_name);
+    if (iter == permission_map_.end())
+      return UNDEFINED_STORED_PERM;
+    return iter->second;
+  }
+  if (type == PERSISTENT_PERMISSION) {
+    return data_->GetPermission(permission_name);
+  }
+  NOTREACHED();
+  return UNDEFINED_STORED_PERM;
 }
 
-int Application::GetRenderProcessHostID() const {
-  DCHECK(main_runtime_);
-  return main_runtime_->web_contents()->
-          GetRenderProcessHost()->GetID();
+bool Application::SetPermission(PermissionType type,
+                                const std::string& permission_name,
+                                StoredPermission perm) {
+  if (type == SESSION_PERMISSION) {
+    permission_map_[permission_name] = perm;
+    return true;
+  }
+  if (type == PERSISTENT_PERMISSION)
+    return data_->SetPermission(permission_name, perm);
+
+  NOTREACHED();
+  return false;
 }
 
-bool Application::IsOnSuspendHandlerRegistered() const {
-  const std::set<std::string>& events = data()->GetEvents();
-  if (events.find(kOnSuspend) == events.end())
-    return false;
+void Application::InitSecurityPolicy() {
+  // CSP policy takes precedence over WARP.
+  if (data_->HasCSPDefined())
+    security_policy_.reset(new SecurityPolicyCSP(this));
+  else if (data_->manifest_type() == Manifest::TYPE_WIDGET)
+    security_policy_.reset(new SecurityPolicyWARP(this));
+
+  if (security_policy_)
+    security_policy_->Enforce();
+}
 
+bool Application::CanRequestURL(const GURL& url) const {
+  if (security_policy_)
+    return security_policy_->IsAccessAllowed(url);
   return true;
 }
 
+base::FilePath Application::GetSplashScreenPath() {
+  return base::FilePath();
+}
+
 }  // namespace application
 }  // namespace xwalk