148155bc0c6a8fe488f44bff9edf4fbb47f094df
[platform/framework/web/crosswalk.git] / src / xwalk / application / tools / tizen / xwalk_backend.cc
1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5
6 #include <stdbool.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9
10 #include <gio/gio.h>
11 #include <glib.h>
12
13 #include "base/at_exit.h"
14 #include "base/files/file_util.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/path_service.h"
17 #include "xwalk/application/common/id_util.h"
18 #include "xwalk/application/common/tizen/application_storage.h"
19 #include "xwalk/application/tools/tizen/xwalk_package_installer.h"
20 #include "xwalk/application/tools/tizen/xwalk_tizen_user.h"
21 #include "xwalk/runtime/common/xwalk_paths.h"
22
23 using xwalk::application::ApplicationData;
24 using xwalk::application::ApplicationStorage;
25
26 namespace {
27
28 char* install_path = NULL;
29 char* uninstall_id = NULL;
30 char* reinstall_id = NULL;
31 char* operation_key = NULL;
32 int quiet = 0;
33
34 gboolean continue_tasks = FALSE;
35
36 GOptionEntry entries[] = {
37   { "install", 'i', 0, G_OPTION_ARG_STRING, &install_path,
38     "Path of the application to be installed/updated", "PATH" },
39   { "uninstall", 'd', 0, G_OPTION_ARG_STRING, &uninstall_id,
40     "Uninstall the application with this pkgid", "ID" },
41   { "continue", 'c' , 0, G_OPTION_ARG_NONE, &continue_tasks,
42     "Continue the previous unfinished tasks", NULL},
43   { "reinstall", 'r', 0, G_OPTION_ARG_STRING, &reinstall_id,
44     "Reinstall the application with this pkgid "
45     "(This option is ONLY for SDK to support RDS mode"
46     " (Rapid Development Support)", "ID" },
47   { "key", 'k', 0, G_OPTION_ARG_STRING, &operation_key,
48     "Unique operation key", "KEY" },
49   { "quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet,
50     "Quiet mode", NULL },
51   { NULL }
52 };
53
54 }  // namespace
55
56 int main(int argc, char* argv[]) {
57   GError* error = NULL;
58   GOptionContext* context;
59   bool success = false;
60
61 #if !GLIB_CHECK_VERSION(2, 36, 0)
62   // g_type_init() is deprecated on GLib since 2.36, Tizen has 2.32.
63   g_type_init();
64 #endif
65
66   if (xwalk_tizen_check_user_for_xwalk_backend())
67     return 1;
68
69   context = g_option_context_new("- Crosswalk Application Management");
70   if (!context) {
71     g_print("g_option_context_new failed\n");
72     return 1;
73   }
74   g_option_context_add_main_entries(context, entries, NULL);
75   if (!g_option_context_parse(context, &argc, &argv, &error)) {
76     g_print("option parsing failed: %s\n", error->message);
77     g_option_context_free(context);
78     return 1;
79   }
80
81   g_option_context_free(context);
82
83   base::AtExitManager at_exit;
84   base::FilePath data_path;
85   xwalk::RegisterPathProvider();
86   PathService::Get(xwalk::DIR_DATA_PATH, &data_path);
87   scoped_ptr<ApplicationStorage> storage(new ApplicationStorage(data_path));
88   scoped_ptr<PackageInstaller> installer =
89       PackageInstaller::Create(storage.get());
90
91   installer->SetQuiet(static_cast<bool>(quiet));
92   if (operation_key)
93     installer->SetInstallationKey(operation_key);
94
95   if (continue_tasks) {
96     g_print("trying to continue previous unfinished tasks.\n");
97     installer->ContinueUnfinishedTasks();
98     success = true;
99     g_print("Previous tasks have been finished.\n");
100   }
101
102   if (install_path) {
103     std::string app_id;
104     const base::FilePath& path =
105         base::MakeAbsoluteFilePath(base::FilePath(install_path));
106     success = installer->Install(path, &app_id);
107     if (!success && !app_id.empty() && storage->Contains(app_id)) {
108       g_print("trying to update %s\n", app_id.c_str());
109       success = installer->Update(app_id, path);
110     }
111   } else if (uninstall_id) {
112     success = installer->Uninstall(uninstall_id);
113   } else if (reinstall_id) {
114     success = installer->Reinstall(reinstall_id);
115   }
116   return success ? 0 : 1;
117 }