d122dbc0f2ebcc73279a29f2b0dedfed762b5256
[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/files/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_manifest_constants.h"
16 #include "xwalk/application/common/id_util.h"
17 #include "xwalk/application/extension/application_runtime_extension.h"
18 #include "xwalk/application/extension/application_widget_extension.h"
19 #include "xwalk/runtime/browser/xwalk_browser_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 #if defined(OS_TIZEN)
27 #include "xwalk/application/browser/application_service_tizen.h"
28 #endif
29
30 namespace xwalk {
31 namespace application {
32
33 ApplicationSystem::ApplicationSystem(XWalkBrowserContext* browser_context)
34   : browser_context_(browser_context),
35     application_service_(ApplicationService::Create(
36         browser_context)) {}
37
38 ApplicationSystem::~ApplicationSystem() {
39 }
40
41 // static
42 scoped_ptr<ApplicationSystem> ApplicationSystem::Create(
43     XWalkBrowserContext* browser_context) {
44   scoped_ptr<ApplicationSystem> app_system;
45 #if defined(OS_LINUX)
46   app_system.reset(new ApplicationSystemLinux(browser_context));
47 #else
48   app_system.reset(new ApplicationSystem(browser_context));
49 #endif
50   return app_system.Pass();
51 }
52
53 namespace {
54
55 Application::LaunchParams launch_params(
56     const base::CommandLine& cmd_line) {
57   Application::LaunchParams params = {
58       0,
59       cmd_line.HasSwitch(switches::kFullscreen),
60       cmd_line.HasSwitch(switches::kRemoteDebuggingPort)};
61   return params;
62 }
63
64 }  // namespace
65
66 bool ApplicationSystem::LaunchFromCommandLine(
67     const base::CommandLine& cmd_line, const GURL& url,
68     bool& run_default_message_loop) { // NOLINT
69
70 #if defined(OS_TIZEN)
71   // Handles raw app_id passed as first non-switch argument.
72   const base::CommandLine::StringVector& args = cmd_line.GetArgs();
73   if (!args.empty()) {
74     std::string app_id = std::string(args[0].begin(), args[0].end());
75     if (IsValidApplicationID(app_id)) {
76       ApplicationServiceTizen* app_service_tizen =
77           ToApplicationServiceTizen(application_service_.get());
78       run_default_message_loop = app_service_tizen->LaunchFromAppID(
79           app_id, launch_params(cmd_line));
80       return true;
81     }
82   }
83 #endif
84   if (!url.is_valid())
85     return false;
86
87   base::FilePath path;
88   bool is_local = url.SchemeIsFile() && net::FileURLToFilePath(url, &path);
89   if (!is_local) {  // Handles external URL.
90     run_default_message_loop = application_service_->LaunchHostedURL(
91         url, launch_params(cmd_line));
92     return true;
93   }
94
95   if (!base::PathExists(path))
96     return false;
97
98   if (path.MatchesExtension(FILE_PATH_LITERAL(".xpk")) ||
99       path.MatchesExtension(FILE_PATH_LITERAL(".wgt"))) {
100     run_default_message_loop = application_service_->LaunchFromPackagePath(
101         path, launch_params(cmd_line));
102     return true;
103   }
104
105   if (path.MatchesExtension(FILE_PATH_LITERAL(".json"))) {
106     run_default_message_loop = application_service_->LaunchFromManifestPath(
107         path, Manifest::TYPE_MANIFEST, launch_params(cmd_line));
108     return true;
109   }
110
111   if (path.MatchesExtension(FILE_PATH_LITERAL(".xml"))) {
112     run_default_message_loop = application_service_->LaunchFromManifestPath(
113         path, Manifest::TYPE_WIDGET, launch_params(cmd_line));
114     return true;
115   }
116
117   return false;
118 }
119
120 void ApplicationSystem::CreateExtensions(
121     content::RenderProcessHost* host,
122     extensions::XWalkExtensionVector* extensions) {
123   Application* application =
124     application_service_->GetApplicationByRenderHostID(host->GetID());
125   if (!application)
126     return;  // We might be in browser mode.
127
128   extensions->push_back(new ApplicationRuntimeExtension(application));
129   extensions->push_back(new ApplicationWidgetExtension(application));
130 }
131
132 }  // namespace application
133 }  // namespace xwalk