78ee6bb70d36052a5e69ed165e89f26ade9de2fd
[platform/framework/web/crosswalk-tizen.git] / src / runtime / runtime.cc
1 // Copyright 2015 Samsung Electronics Co, Ltd. 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 "runtime/runtime.h"
6
7 #include <ewk_chromium.h>
8 #include <string>
9 #include <memory>
10
11 #include "common/logger.h"
12 #include "common/command_line.h"
13 #include "common/app_control.h"
14 #include "common/application_data.h"
15 #include "runtime/native_app_window.h"
16
17 namespace wrt {
18
19 namespace {
20
21 const char* kTextLocalePath = "/usr/share/locale";
22 const char* kTextDomainWrt = "wrt";
23
24 static NativeWindow* CreateNativeWindow() {
25   // TODO(wy80.choi) : consider other type of native window.
26   NativeWindow* window = new NativeAppWindow();
27   window->Initialize();
28   return window;
29 }
30
31 }  // namespace
32
33 Runtime::Runtime()
34     : application_(NULL),
35       native_window_(NULL) {
36 }
37
38 Runtime::~Runtime() {
39   if (application_) {
40     delete application_;
41   }
42   if (native_window_) {
43     delete native_window_;
44   }
45 }
46
47 bool Runtime::OnCreate() {
48   std::string appid = CommandLine::ForCurrentProcess()->appid();
49
50   // Process First Launch
51   std::unique_ptr<ApplicationData> appdata(new ApplicationData(appid));
52   if (!appdata->LoadManifestData()) {
53     return false;
54   }
55
56   native_window_ = CreateNativeWindow();
57   application_ = new WebApplication(native_window_, std::move(appdata));
58   application_->set_terminator([](){ ui_app_exit(); });
59
60   setlocale(LC_ALL, "");
61   bindtextdomain(kTextDomainWrt, kTextLocalePath);
62
63   return true;
64 }
65
66 void Runtime::OnTerminate() {
67 }
68
69 void Runtime::OnPause() {
70   if (application_->launched()) {
71     application_->Suspend();
72   }
73 }
74
75 void Runtime::OnResume() {
76   if (application_->launched()) {
77     application_->Resume();
78   }
79 }
80
81 void Runtime::OnAppControl(app_control_h app_control) {
82   std::unique_ptr<AppControl> appcontrol(new AppControl(app_control));
83   if (application_->launched()) {
84     application_->AppControl(std::move(appcontrol));
85   } else {
86     application_->Launch(std::move(appcontrol));
87   }
88 }
89
90 void Runtime::OnLanguageChanged(const std::string& language) {
91   if (application_) {
92     application_->OnLanguageChanged();
93     elm_language_set(language.c_str());
94   }
95 }
96
97 void Runtime::OnLowMemory() {
98   if (application_) {
99     application_->OnLowMemory();
100   }
101 }
102
103 int Runtime::Exec(int argc, char* argv[]) {
104   ui_app_lifecycle_callback_s ops = {NULL, NULL, NULL, NULL, NULL};
105
106   // onCreate
107   ops.create = [](void* data) -> bool {
108     Runtime* runtime = reinterpret_cast<Runtime*>(data);
109     if (!runtime) {
110       LOGGER(ERROR) << "Runtime has not been created.";
111       return false;
112     }
113     return runtime->OnCreate();
114   };
115
116   // onTerminate
117   ops.terminate = [](void* data) -> void {
118     Runtime* runtime = reinterpret_cast<Runtime*>(data);
119     if (!runtime) {
120       LOGGER(ERROR) << "Runtime has not been created.";
121       return;
122     }
123     runtime->OnTerminate();
124   };
125
126   // onPause
127   ops.pause = [](void* data) -> void {
128     Runtime* runtime = reinterpret_cast<Runtime*>(data);
129     if (!runtime) {
130       LOGGER(ERROR) << "Runtime has not been created.";
131       return;
132     }
133     runtime->OnPause();
134   };
135
136   // onResume
137   ops.resume = [](void* data) -> void {
138     Runtime* runtime = reinterpret_cast<Runtime*>(data);
139     if (!runtime) {
140       LOGGER(ERROR) << "Runtime has not been created.";
141       return;
142     }
143     runtime->OnResume();
144   };
145
146   // onAppControl
147   ops.app_control = [](app_control_h app_control, void* data) -> void {
148     Runtime* runtime = reinterpret_cast<Runtime*>(data);
149     if (!runtime) {
150       LOGGER(ERROR) << "Runtime has not been created.";
151       return;
152     }
153     runtime->OnAppControl(app_control);
154   };
155
156   // language changed callback
157   auto language_changed = [](app_event_info_h event_info, void* user_data) {
158     char* str;
159     if (app_event_get_language(event_info, &str) == 0 && str != NULL) {
160       std::string language = std::string(str);
161       std::free(str);
162       Runtime* runtime = reinterpret_cast<Runtime*>(user_data);
163       runtime->OnLanguageChanged(language);
164     }
165   };
166   auto low_memory = [](app_event_info_h /*event_info*/, void* user_data) {
167     Runtime* runtime = reinterpret_cast<Runtime*>(user_data);
168     runtime->OnLowMemory();
169   };
170   app_event_handler_h ev_handle;
171   ui_app_add_event_handler(&ev_handle,
172                            APP_EVENT_LANGUAGE_CHANGED,
173                            language_changed,
174                            this);
175   ui_app_add_event_handler(&ev_handle,
176                            APP_EVENT_LOW_MEMORY,
177                            low_memory,
178                            this);
179
180   return ui_app_main(argc, argv, &ops, this);
181 }
182
183 }  // namespace wrt