Fix invalid licenses
[platform/framework/web/crosswalk-tizen.git] / src / runtime / runtime.cc
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16
17
18 #include "runtime/runtime.h"
19
20 #include <ewk_chromium.h>
21 #include <string>
22 #include <memory>
23
24 #include "common/logger.h"
25 #include "common/command_line.h"
26 #include "common/app_control.h"
27 #include "common/application_data.h"
28 #include "runtime/native_app_window.h"
29
30 namespace wrt {
31
32 namespace {
33
34 const char* kTextLocalePath = "/usr/share/locale";
35 const char* kTextDomainWrt = "wrt";
36
37 static NativeWindow* CreateNativeWindow() {
38   // TODO(wy80.choi) : consider other type of native window.
39   NativeWindow* window = new NativeAppWindow();
40   window->Initialize();
41   return window;
42 }
43
44 }  // namespace
45
46 Runtime::Runtime()
47     : application_(NULL),
48       native_window_(NULL) {
49 }
50
51 Runtime::~Runtime() {
52   if (application_) {
53     delete application_;
54   }
55   if (native_window_) {
56     delete native_window_;
57   }
58 }
59
60 bool Runtime::OnCreate() {
61   std::string appid = CommandLine::ForCurrentProcess()->appid();
62
63   // Process First Launch
64   std::unique_ptr<ApplicationData> appdata(new ApplicationData(appid));
65   if (!appdata->LoadManifestData()) {
66     return false;
67   }
68
69   native_window_ = CreateNativeWindow();
70   application_ = new WebApplication(native_window_, std::move(appdata));
71   application_->set_terminator([](){ ui_app_exit(); });
72
73   setlocale(LC_ALL, "");
74   bindtextdomain(kTextDomainWrt, kTextLocalePath);
75
76   return true;
77 }
78
79 void Runtime::OnTerminate() {
80 }
81
82 void Runtime::OnPause() {
83   if (application_->launched()) {
84     application_->Suspend();
85   }
86 }
87
88 void Runtime::OnResume() {
89   if (application_->launched()) {
90     application_->Resume();
91   }
92 }
93
94 void Runtime::OnAppControl(app_control_h app_control) {
95   std::unique_ptr<AppControl> appcontrol(new AppControl(app_control));
96   if (application_->launched()) {
97     application_->AppControl(std::move(appcontrol));
98   } else {
99     application_->Launch(std::move(appcontrol));
100   }
101 }
102
103 void Runtime::OnLanguageChanged(const std::string& language) {
104   if (application_) {
105     application_->OnLanguageChanged();
106     elm_language_set(language.c_str());
107   }
108 }
109
110 void Runtime::OnLowMemory() {
111   if (application_) {
112     application_->OnLowMemory();
113   }
114 }
115
116 int Runtime::Exec(int argc, char* argv[]) {
117   ui_app_lifecycle_callback_s ops = {NULL, NULL, NULL, NULL, NULL};
118
119   // onCreate
120   ops.create = [](void* data) -> bool {
121     Runtime* runtime = reinterpret_cast<Runtime*>(data);
122     if (!runtime) {
123       LOGGER(ERROR) << "Runtime has not been created.";
124       return false;
125     }
126     return runtime->OnCreate();
127   };
128
129   // onTerminate
130   ops.terminate = [](void* data) -> void {
131     Runtime* runtime = reinterpret_cast<Runtime*>(data);
132     if (!runtime) {
133       LOGGER(ERROR) << "Runtime has not been created.";
134       return;
135     }
136     runtime->OnTerminate();
137   };
138
139   // onPause
140   ops.pause = [](void* data) -> void {
141     Runtime* runtime = reinterpret_cast<Runtime*>(data);
142     if (!runtime) {
143       LOGGER(ERROR) << "Runtime has not been created.";
144       return;
145     }
146     runtime->OnPause();
147   };
148
149   // onResume
150   ops.resume = [](void* data) -> void {
151     Runtime* runtime = reinterpret_cast<Runtime*>(data);
152     if (!runtime) {
153       LOGGER(ERROR) << "Runtime has not been created.";
154       return;
155     }
156     runtime->OnResume();
157   };
158
159   // onAppControl
160   ops.app_control = [](app_control_h app_control, void* data) -> void {
161     Runtime* runtime = reinterpret_cast<Runtime*>(data);
162     if (!runtime) {
163       LOGGER(ERROR) << "Runtime has not been created.";
164       return;
165     }
166     runtime->OnAppControl(app_control);
167   };
168
169   // language changed callback
170   auto language_changed = [](app_event_info_h event_info, void* user_data) {
171     char* str;
172     if (app_event_get_language(event_info, &str) == 0 && str != NULL) {
173       std::string language = std::string(str);
174       std::free(str);
175       Runtime* runtime = reinterpret_cast<Runtime*>(user_data);
176       runtime->OnLanguageChanged(language);
177     }
178   };
179   auto low_memory = [](app_event_info_h /*event_info*/, void* user_data) {
180     Runtime* runtime = reinterpret_cast<Runtime*>(user_data);
181     runtime->OnLowMemory();
182   };
183   app_event_handler_h ev_handle;
184   ui_app_add_event_handler(&ev_handle,
185                            APP_EVENT_LANGUAGE_CHANGED,
186                            language_changed,
187                            this);
188   ui_app_add_event_handler(&ev_handle,
189                            APP_EVENT_LOW_MEMORY,
190                            low_memory,
191                            this);
192
193   return ui_app_main(argc, argv, &ops, this);
194 }
195
196 }  // namespace wrt