Add none exec_label in xwalk_runtime
[platform/framework/web/crosswalk-tizen.git] / runtime / browser / watch_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 #include <ewk_chromium.h>
18 #include <watch_app.h>
19 #include <appcore-watch.h>
20
21 #include <memory>
22 #include <string>
23
24 #include "common/application_data.h"
25 #include "common/app_control.h"
26 #include "common/app_db.h"
27 #include "common/command_line.h"
28 #include "common/logger.h"
29 #include "common/profiler.h"
30 #include "runtime/common/constants.h"
31 #include "runtime/browser/native_watch_window.h"
32 #include "runtime/browser/preload_manager.h"
33 #include "runtime/browser/runtime.h"
34 #include "runtime/browser/watch_runtime.h"
35
36 namespace runtime {
37
38 namespace {
39
40 static NativeWindow* CreateNativeWindow() {
41   SCOPE_PROFILE();
42   NativeWindow* window = NULL;
43   auto cached = PreloadManager::GetInstance()->GetCachedNativeWindow();
44   if (cached != nullptr) {
45     delete cached;
46   }
47   window = new NativeWatchWindow();
48   window->Initialize();
49
50   return window;
51 }
52
53 }  // namespace
54
55 WatchRuntime::WatchRuntime(common::ApplicationData* app_data)
56     : application_(NULL),
57       native_window_(NULL),
58       app_data_(app_data) {
59 }
60
61 WatchRuntime::~WatchRuntime() {
62   if (application_) {
63     delete application_;
64   }
65   if (native_window_) {
66     delete native_window_;
67   }
68 }
69
70 bool WatchRuntime::OnCreate() {
71   STEP_PROFILE_END("watch_app_main -> OnCreate");
72   STEP_PROFILE_END("Start -> OnCreate");
73   STEP_PROFILE_START("OnCreate -> URL Set");
74
75   common::CommandLine* cmd = common::CommandLine::ForCurrentProcess();
76   std::string appid = cmd->GetAppIdFromCommandLine(kRuntimeExecName);
77
78   // Init AppDB for Runtime
79   common::AppDB* appdb = common::AppDB::GetInstance();
80   appdb->Set(kAppDBRuntimeSection, kAppDBRuntimeName, "xwalk-tizen");
81   appdb->Set(kAppDBRuntimeSection, kAppDBRuntimeAppID, appid);
82   if (app_data_->setting_info()->background_support_enabled()) {
83     appdb->Set(kAppDBRuntimeSection, kAppDBRuntimeBackgroundSupport, "true");
84   } else {
85     appdb->Set(kAppDBRuntimeSection, kAppDBRuntimeBackgroundSupport, "false");
86   }
87   appdb->Remove(kAppDBRuntimeSection, kAppDBRuntimeBundle);
88
89   // Init WebApplication
90   native_window_ = CreateNativeWindow();
91   STEP_PROFILE_START("WebApplication Create");
92   application_ = new WebApplication(native_window_, app_data_);
93   STEP_PROFILE_END("WebApplication Create");
94   application_->set_terminator([](){ watch_app_exit(); });
95
96   setlocale(LC_ALL, "");
97   bindtextdomain(kTextDomainRuntime, kTextLocalePath);
98
99   return true;
100 }
101
102 void WatchRuntime::OnTerminate() {
103   if (application_) {
104     delete application_;
105     application_ = nullptr;
106   }
107   if (native_window_) {
108     delete native_window_;
109     native_window_ = nullptr;
110   }
111 }
112
113 void WatchRuntime::OnPause() {
114   if (application_->launched()) {
115     application_->Suspend();
116   }
117 }
118
119 void WatchRuntime::OnResume() {
120   if (application_->launched()) {
121     application_->Resume();
122   }
123 }
124
125 void WatchRuntime::OnAppControl(app_control_h app_control) {
126   SCOPE_PROFILE();
127   std::unique_ptr<common::AppControl>
128       appcontrol(new common::AppControl(app_control));
129   common::AppDB* appdb = common::AppDB::GetInstance();
130   appdb->Set(kAppDBRuntimeSection, kAppDBRuntimeBundle,
131              appcontrol->encoded_bundle());
132   if (application_->launched()) {
133     application_->AppControl(std::move(appcontrol));
134   } else {
135     application_->Launch(std::move(appcontrol));
136   }
137 }
138
139 void WatchRuntime::OnLanguageChanged(const std::string& language) {
140   if (application_) {
141     application_->OnLanguageChanged();
142     elm_language_set(language.c_str());
143   }
144 }
145
146 void WatchRuntime::OnLowMemory() {
147   if (application_) {
148     application_->OnLowMemory();
149   }
150 }
151
152 void WatchRuntime::OnTimeTick(watch_time_h watch_time) {
153   //do not fire tick event for normal clock for web app
154 #if 0
155   time_t time;
156   int ret = watch_time_get_utc_timestamp(watch_time, &time);
157   if (!ret) {
158     LOGGER(DEBUG) << "time : " << time;
159     application_->OnTimeTick(time);
160   } else {
161     LOGGER(DEBUG) << "Fail to get utc time. skip send time tick event";
162   }
163 #endif
164 }
165
166 void WatchRuntime::OnAmbientTick(watch_time_h watch_time) {
167   time_t time;
168   int ret = watch_time_get_utc_timestamp(watch_time, &time);
169   if (!ret) {
170     LOGGER(DEBUG) << "time : " << time;
171     application_->OnAmbientTick(time);
172   } else {
173     LOGGER(ERROR) << "Fail to get utc time. skip send ambient tick event";
174   }
175 }
176
177 void WatchRuntime::OnAmbientChanged(bool ambient_mode) {
178   application_->OnAmbientChanged(ambient_mode);
179 }
180
181 int WatchRuntime::Exec(int argc, char* argv[]) {
182   watch_app_lifecycle_callback_s ops =
183     {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
184
185   // onCreate
186   ops.create = [](int width, int height, void *data) -> bool {
187     WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
188     if (!runtime) {
189       LOGGER(ERROR) << "Runtime has not been created.";
190       return false;
191     }
192     return runtime->OnCreate();
193   };
194
195   // onTerminate
196   ops.terminate = [](void* data) -> void {
197     WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
198     if (!runtime) {
199       LOGGER(ERROR) << "Runtime has not been created.";
200       return;
201     }
202     runtime->OnTerminate();
203   };
204
205   // onPause
206   ops.pause = [](void* data) -> void {
207     WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
208     if (!runtime) {
209       LOGGER(ERROR) << "Runtime has not been created.";
210       return;
211     }
212     runtime->OnPause();
213   };
214
215   // onResume
216   ops.resume = [](void* data) -> void {
217     WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
218     if (!runtime) {
219       LOGGER(ERROR) << "Runtime has not been created.";
220       return;
221     }
222     runtime->OnResume();
223   };
224
225   // onAppControl
226   ops.app_control = [](app_control_h app_control, void* data) -> void {
227     WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
228     if (!runtime) {
229       LOGGER(ERROR) << "Runtime has not been created.";
230       return;
231     }
232     runtime->OnAppControl(app_control);
233   };
234
235   // onTimeTick
236   ops.time_tick = [](watch_time_h watch_time, void* data) -> void {
237     WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
238     if (!runtime) {
239       LOGGER(ERROR) << "Runtime has not been created.";
240       return;
241     }
242     runtime->OnTimeTick(watch_time);
243   };
244
245   // onAmbientTick
246   ops.ambient_tick = [](watch_time_h watch_time, void* data) -> void {
247     WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
248     if (!runtime) {
249       LOGGER(ERROR) << "Runtime has not been created.";
250       return;
251     }
252     runtime->OnAmbientTick(watch_time);
253   };
254
255   // onAmbientChanged
256   ops.ambient_changed = [](bool ambient_mode, void* data) -> void {
257     WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
258     if (!runtime) {
259       LOGGER(ERROR) << "Runtime has not been created.";
260       return;
261     }
262     // To render web application on ambient mode
263     if (ambient_mode) {
264       runtime->OnResume();
265     } else {
266       runtime->OnPause();
267     }
268     runtime->OnAmbientChanged(ambient_mode);
269   };
270
271   // language changed callback
272   auto language_changed = [](app_event_info_h event_info, void* user_data) {
273     char* str;
274     if (app_event_get_language(event_info, &str) == 0 && str != NULL) {
275       std::string language = std::string(str);
276       std::free(str);
277       WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(user_data);
278       runtime->OnLanguageChanged(language);
279     }
280   };
281
282   auto low_memory = [](app_event_info_h /*event_info*/, void* user_data) {
283     WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(user_data);
284     runtime->OnLowMemory();
285   };
286
287   app_event_handler_h ev_handle;
288   watch_app_add_event_handler(&ev_handle,
289                            APP_EVENT_LANGUAGE_CHANGED,
290                            language_changed,
291                            this);
292   watch_app_add_event_handler(&ev_handle,
293                            APP_EVENT_LOW_MEMORY,
294                            low_memory,
295                            this);
296   STEP_PROFILE_START("watch_app_main -> OnCreate");
297
298   return watch_app_main(argc, argv, &ops, this);
299 }
300
301 }  // namespace runtime