603f938ad4e144b35e47278e225f067bf273c042
[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 "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"
22
23 #if defined(OS_LINUX)
24 #include "xwalk/application/browser/application_system_linux.h"
25 #endif
26
27 namespace xwalk {
28 namespace application {
29
30 ApplicationSystem::ApplicationSystem(RuntimeContext* runtime_context)
31   : runtime_context_(runtime_context),
32     application_storage_(new ApplicationStorage(runtime_context->GetPath())),
33     application_service_(new ApplicationService(
34         runtime_context,
35         application_storage_.get())) {}
36
37 ApplicationSystem::~ApplicationSystem() {
38 }
39
40 // static
41 scoped_ptr<ApplicationSystem> ApplicationSystem::Create(
42     RuntimeContext* runtime_context) {
43   scoped_ptr<ApplicationSystem> app_system;
44 #if defined(OS_LINUX)
45   app_system.reset(new ApplicationSystemLinux(runtime_context));
46 #else
47   app_system.reset(new ApplicationSystem(runtime_context));
48 #endif
49   return app_system.Pass();
50 }
51
52 template <typename T>
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);
59
60   return application_service_->Launch(param, launch_params);
61 }
62
63 // Launch an application created from arbitrary url.
64 // FIXME: This application should have the same strict permissions
65 // as common browser apps.
66 template <>
67 bool ApplicationSystem::LaunchWithCommandLineParam<GURL>(
68     const GURL& url, const base::CommandLine& cmd_line) {
69   std::string error;
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: "
74                << error;
75     return false;
76   }
77
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);
83
84   return !!application_service_->Launch(application_data, launch_params);
85 }
86
87 bool ApplicationSystem::LaunchFromCommandLine(
88     const base::CommandLine& cmd_line, const GURL& url,
89     bool& run_default_message_loop) { // NOLINT
90
91   // Handles raw app_id passed as first non-switch argument.
92   const base::CommandLine::StringVector& args = cmd_line.GetArgs();
93   if (!args.empty()) {
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);
97       return true;
98     }
99   }
100
101   if (!url.is_valid())
102     return false;
103
104   base::FilePath path;
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);
111   }
112
113   return true;
114 }
115
116 void ApplicationSystem::CreateExtensions(
117     content::RenderProcessHost* host,
118     extensions::XWalkExtensionVector* extensions) {
119   Application* application =
120     application_service_->GetApplicationByRenderHostID(host->GetID());
121   if (!application)
122     return;  // We might be in browser mode.
123
124   extensions->push_back(new ApplicationRuntimeExtension(application));
125   extensions->push_back(new ApplicationWidgetExtension(application));
126 }
127
128 }  // namespace application
129 }  // namespace xwalk