Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / browser / application_system.cc
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.
4
5 #include "xwalk/application/browser/application_system.h"
6
7 #include <string>
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"
21
22 #if defined(OS_LINUX)
23 #include "xwalk/application/browser/application_system_linux.h"
24 #endif
25
26 namespace xwalk {
27 namespace application {
28
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(
34         runtime_context,
35         application_storage_.get(),
36         event_manager_.get())) {}
37
38 ApplicationSystem::~ApplicationSystem() {
39 }
40
41 // static
42 scoped_ptr<ApplicationSystem> ApplicationSystem::Create(
43     RuntimeContext* runtime_context) {
44   scoped_ptr<ApplicationSystem> app_system;
45 #if defined(OS_LINUX)
46   app_system.reset(new ApplicationSystemLinux(runtime_context));
47 #else
48   app_system.reset(new ApplicationSystem(runtime_context));
49 #endif
50   return app_system.Pass();
51 }
52
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) << "-----------------------------------------------------";
66     return true;
67   }
68
69   if (cmd_line.HasSwitch(switches::kUninstall)) {
70     const CommandLine::StringVector& args = cmd_line.GetArgs();
71     if (args.empty())
72       return false;
73
74     std::string app_id = std::string(args[0].begin(), args[0].end());
75     if (!ApplicationData::IsIDValid(app_id))
76       return false;
77
78     if (application_service_->Uninstall(app_id)) {
79       LOG(INFO) << "[OK] Application uninstalled successfully: " << app_id;
80     } else {
81       LOG(ERROR) << "[ERR] An error occurred when uninstalling application "
82                  << app_id;
83     }
84     return true;
85   }
86
87   if (cmd_line.HasSwitch(switches::kInstall)) {
88     base::FilePath path;
89     if (!net::FileURLToFilePath(url, &path))
90       return false;
91
92     if (!base::PathExists(path))
93       return false;
94
95     std::string app_id;
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;
105     } else {
106       LOG(ERROR) << "[ERR] Application install/update failure: "
107                  << path.value();
108     }
109     return true;
110   }
111
112   run_default_message_loop = true;
113   return false;
114 }
115
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);
122     return true;
123   }
124   return false;
125 }
126
127 template <>
128 bool ApplicationSystem::LaunchWithCommandLineParam<GURL>(const GURL& url) {
129   return !!application_service_->Launch(url);
130 }
131
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
136   // application ID.
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);
142     return true;
143   }
144 #endif
145
146   // Handles raw app_id passed as first non-switch argument.
147   const CommandLine::StringVector& args = cmd_line.GetArgs();
148   if (!args.empty()) {
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);
152       return true;
153     }
154   }
155
156   if (!url.is_valid())
157     return false;
158
159   base::FilePath path;
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);
166   }
167
168   return true;
169 }
170
171 void ApplicationSystem::CreateExtensions(
172     content::RenderProcessHost* host,
173     extensions::XWalkExtensionVector* extensions) {
174   Application* application =
175     application_service_->GetApplicationByRenderHostID(host->GetID());
176   if (!application)
177     return;  // We might be in browser mode.
178
179   extensions->push_back(new ApplicationRuntimeExtension(application));
180   extensions->push_back(new ApplicationEventExtension(
181               event_manager_.get(), application_storage_.get(), application));
182 }
183
184 }  // namespace application
185 }  // namespace xwalk