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 "net/base/net_util.h"
12 #include "xwalk/application/browser/application.h"
13 #include "xwalk/application/browser/application_event_manager.h"
14 #include "xwalk/application/browser/application_service.h"
15 #include "xwalk/application/browser/application_storage.h"
16 #include "xwalk/application/common/event_names.h"
17 #include "xwalk/application/extension/application_event_extension.h"
18 #include "xwalk/application/extension/application_runtime_extension.h"
19 #include "xwalk/runtime/browser/runtime_context.h"
20 #include "xwalk/runtime/common/xwalk_switches.h"
23 #include "xwalk/application/browser/application_system_linux.h"
27 namespace application {
29 ApplicationSystem::ApplicationSystem(RuntimeContext* runtime_context)
30 : runtime_context_(runtime_context),
31 application_storage_(new ApplicationStorage(runtime_context->GetPath())),
32 event_manager_(new ApplicationEventManager()),
33 application_service_(new ApplicationService(
35 application_storage_.get(),
36 event_manager_.get())) {}
38 ApplicationSystem::~ApplicationSystem() {
42 scoped_ptr<ApplicationSystem> ApplicationSystem::Create(
43 RuntimeContext* runtime_context) {
44 scoped_ptr<ApplicationSystem> app_system;
46 app_system.reset(new ApplicationSystemLinux(runtime_context));
48 app_system.reset(new ApplicationSystem(runtime_context));
50 return app_system.Pass();
53 bool ApplicationSystem::HandleApplicationManagementCommands(
54 const CommandLine& cmd_line, const GURL& url,
55 bool& run_default_message_loop) {
56 run_default_message_loop = false;
57 if (cmd_line.HasSwitch(switches::kListApplications)) {
58 const ApplicationData::ApplicationDataMap& apps =
59 application_storage_->GetInstalledApplications();
60 LOG(INFO) << "Application ID Application Name";
61 LOG(INFO) << "-----------------------------------------------------";
62 ApplicationData::ApplicationDataMap::const_iterator it;
63 for (it = apps.begin(); it != apps.end(); ++it)
64 LOG(INFO) << it->first << " " << it->second->Name();
65 LOG(INFO) << "-----------------------------------------------------";
69 if (cmd_line.HasSwitch(switches::kUninstall)) {
70 const CommandLine::StringVector& args = cmd_line.GetArgs();
74 std::string app_id = std::string(args[0].begin(), args[0].end());
75 if (!ApplicationData::IsIDValid(app_id))
78 if (application_service_->Uninstall(app_id)) {
79 LOG(INFO) << "[OK] Application uninstalled successfully: " << app_id;
81 LOG(ERROR) << "[ERR] An error occurred when uninstalling application "
87 if (cmd_line.HasSwitch(switches::kInstall)) {
89 if (!net::FileURLToFilePath(url, &path))
92 if (!base::PathExists(path))
96 if (application_service_->Install(path, &app_id)) {
97 LOG(INFO) << "[OK] Application installed: " << app_id;
98 if (application_storage_->GetApplicationData(app_id)->HasMainDocument())
99 run_default_message_loop = true;
100 } else if (!app_id.empty() &&
101 application_service_->Update(app_id, path)) {
102 LOG(INFO) << "[OK] Application updated: " << app_id;
103 if (application_storage_->GetApplicationData(app_id)->HasMainDocument())
104 run_default_message_loop = true;
106 LOG(ERROR) << "[ERR] Application install/update failure: "
112 run_default_message_loop = true;
116 template <typename T>
117 bool ApplicationSystem::LaunchWithCommandLineParam(const T& param) {
118 scoped_refptr<Event> event = Event::CreateEvent(
119 kOnLaunched, scoped_ptr<base::ListValue>(new base::ListValue));
120 if (Application* application = application_service_->Launch(param)) {
121 event_manager_->SendEvent(application->id(), event);
128 bool ApplicationSystem::LaunchWithCommandLineParam<GURL>(const GURL& url) {
129 return !!application_service_->Launch(url);
132 bool ApplicationSystem::LaunchFromCommandLine(
133 const CommandLine& cmd_line, const GURL& url,
134 bool& run_default_message_loop) {
135 // On Tizen, applications are launched by a symbolic link named like the
137 // FIXME(cmarcelo): Remove when we move to a separate launcher on Tizen.
138 #if defined(OS_TIZEN)
139 std::string command_name = cmd_line.GetProgram().BaseName().MaybeAsASCII();
140 if (ApplicationData::IsIDValid(command_name)) {
141 run_default_message_loop = LaunchWithCommandLineParam(command_name);
146 // Handles raw app_id passed as first non-switch argument.
147 const CommandLine::StringVector& args = cmd_line.GetArgs();
149 std::string app_id = std::string(args[0].begin(), args[0].end());
150 if (ApplicationData::IsIDValid(app_id)) {
151 run_default_message_loop = LaunchWithCommandLineParam(app_id);
160 if (url.SchemeIsFile() &&
161 net::FileURLToFilePath(url, &path) &&
162 base::DirectoryExists(path)) { // Handles local directory.
163 run_default_message_loop = LaunchWithCommandLineParam(path);
164 } else { // Handles external URL.
165 run_default_message_loop = LaunchWithCommandLineParam(url);
171 void ApplicationSystem::CreateExtensions(
172 content::RenderProcessHost* host,
173 extensions::XWalkExtensionVector* extensions) {
174 Application* application =
175 application_service_->GetApplicationByRenderHostID(host->GetID());
177 return; // We might be in browser mode.
179 extensions->push_back(new ApplicationRuntimeExtension(application));
180 extensions->push_back(new ApplicationEventExtension(
181 event_manager_.get(), application_storage_.get(), application));
184 } // namespace application