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