1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "xwalk/application/browser/application_system.h"
8 #include "base/command_line.h"
9 #include "base/file_util.h"
10 #include "content/public/browser/render_process_host.h"
11 #include "content/public/common/content_switches.h"
12 #include "net/base/filename_util.h"
13 #include "xwalk/application/browser/application.h"
14 #include "xwalk/application/browser/application_service.h"
15 #include "xwalk/application/common/application_storage.h"
16 #include "xwalk/application/common/application_manifest_constants.h"
17 #include "xwalk/application/common/id_util.h"
18 #include "xwalk/application/extension/application_runtime_extension.h"
19 #include "xwalk/application/extension/application_widget_extension.h"
20 #include "xwalk/runtime/browser/runtime_context.h"
21 #include "xwalk/runtime/common/xwalk_switches.h"
24 #include "xwalk/application/browser/application_system_linux.h"
28 namespace application {
30 ApplicationSystem::ApplicationSystem(RuntimeContext* runtime_context)
31 : runtime_context_(runtime_context),
32 application_storage_(new ApplicationStorage(runtime_context->GetPath())),
33 application_service_(new ApplicationService(
35 application_storage_.get())) {}
37 ApplicationSystem::~ApplicationSystem() {
41 scoped_ptr<ApplicationSystem> ApplicationSystem::Create(
42 RuntimeContext* runtime_context) {
43 scoped_ptr<ApplicationSystem> app_system;
45 app_system.reset(new ApplicationSystemLinux(runtime_context));
47 app_system.reset(new ApplicationSystem(runtime_context));
49 return app_system.Pass();
53 bool ApplicationSystem::LaunchWithCommandLineParam(
54 const T& param, const base::CommandLine& cmd_line) {
55 Application::LaunchParams launch_params;
56 launch_params.force_fullscreen = cmd_line.HasSwitch(switches::kFullscreen);
57 launch_params.remote_debugging =
58 cmd_line.HasSwitch(switches::kRemoteDebuggingPort);
60 return application_service_->Launch(param, launch_params);
63 // Launch an application created from arbitrary url.
64 // FIXME: This application should have the same strict permissions
65 // as common browser apps.
67 bool ApplicationSystem::LaunchWithCommandLineParam<GURL>(
68 const GURL& url, const base::CommandLine& cmd_line) {
70 scoped_refptr<ApplicationData> application_data =
71 ApplicationData::Create(url, &error);
72 if (!application_data) {
73 LOG(ERROR) << "Error occurred while trying to launch application: "
78 Application::LaunchParams launch_params;
79 launch_params.force_fullscreen = cmd_line.HasSwitch(switches::kFullscreen);
80 launch_params.entry_points = Application::StartURLKey;
81 launch_params.remote_debugging =
82 cmd_line.HasSwitch(switches::kRemoteDebuggingPort);
84 return !!application_service_->Launch(application_data, launch_params);
87 bool ApplicationSystem::LaunchFromCommandLine(
88 const base::CommandLine& cmd_line, const GURL& url,
89 bool& run_default_message_loop) { // NOLINT
91 // Handles raw app_id passed as first non-switch argument.
92 const base::CommandLine::StringVector& args = cmd_line.GetArgs();
94 std::string app_id = std::string(args[0].begin(), args[0].end());
95 if (IsValidApplicationID(app_id)) {
96 run_default_message_loop = LaunchWithCommandLineParam(app_id, cmd_line);
105 if (url.SchemeIsFile() &&
106 net::FileURLToFilePath(url, &path) &&
107 base::DirectoryExists(path)) { // Handles local directory.
108 run_default_message_loop = LaunchWithCommandLineParam(path, cmd_line);
109 } else { // Handles external URL.
110 run_default_message_loop = LaunchWithCommandLineParam(url, cmd_line);
116 void ApplicationSystem::CreateExtensions(
117 content::RenderProcessHost* host,
118 extensions::XWalkExtensionVector* extensions) {
119 Application* application =
120 application_service_->GetApplicationByRenderHostID(host->GetID());
122 return; // We might be in browser mode.
124 extensions->push_back(new ApplicationRuntimeExtension(application));
125 extensions->push_back(new ApplicationWidgetExtension(application));
128 } // namespace application