Fixed authentication popup of circle
[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   appdb->Remove(kAppDBRuntimeSection, kAppDBRuntimeBundle);
83
84   // Init WebApplication
85   native_window_ = CreateNativeWindow();
86   STEP_PROFILE_START("WebApplication Create");
87   application_ = new WebApplication(native_window_, app_data_);
88   STEP_PROFILE_END("WebApplication Create");
89   application_->set_terminator([](){ watch_app_exit(); });
90
91   setlocale(LC_ALL, "");
92   bindtextdomain(kTextDomainRuntime, kTextLocalePath);
93
94   return true;
95 }
96
97 void WatchRuntime::OnTerminate() {
98   if (application_) {
99     delete application_;
100     application_ = nullptr;
101   }
102   if (native_window_) {
103     delete native_window_;
104     native_window_ = nullptr;
105   }
106 }
107
108 void WatchRuntime::OnPause() {
109   if (application_->launched()) {
110     application_->Suspend();
111   }
112 }
113
114 void WatchRuntime::OnResume() {
115   if (application_->launched()) {
116     application_->Resume();
117   }
118 }
119
120 void WatchRuntime::OnAppControl(app_control_h app_control) {
121   SCOPE_PROFILE();
122   std::unique_ptr<common::AppControl>
123       appcontrol(new common::AppControl(app_control));
124   common::AppDB* appdb = common::AppDB::GetInstance();
125   appdb->Set(kAppDBRuntimeSection, kAppDBRuntimeBundle,
126              appcontrol->encoded_bundle());
127   if (application_->launched()) {
128     application_->AppControl(std::move(appcontrol));
129   } else {
130     application_->Launch(std::move(appcontrol));
131   }
132 }
133
134 void WatchRuntime::OnLanguageChanged(const std::string& language) {
135   if (application_) {
136     application_->OnLanguageChanged();
137     elm_language_set(language.c_str());
138   }
139 }
140
141 void WatchRuntime::OnLowMemory() {
142   if (application_) {
143     application_->OnLowMemory();
144   }
145 }
146
147 void WatchRuntime::OnTimeTick(watch_time_h watch_time) {
148   //do not fire tick event for normal clock for web app
149 #if 0
150   time_t time;
151   int ret = watch_time_get_utc_timestamp(watch_time, &time);
152   if (!ret) {
153     LOGGER(DEBUG) << "time : " << time;
154     application_->OnTimeTick(time);
155   } else {
156     LOGGER(DEBUG) << "Fail to get utc time. skip send time tick event";
157   }
158 #endif
159 }
160
161 void WatchRuntime::OnAmbientTick(watch_time_h watch_time) {
162   time_t time;
163   int ret = watch_time_get_utc_timestamp(watch_time, &time);
164   if (!ret) {
165     LOGGER(DEBUG) << "time : " << time;
166     application_->OnAmbientTick(time);
167   } else {
168     LOGGER(ERROR) << "Fail to get utc time. skip send ambient tick event";
169   }
170 }
171
172 void WatchRuntime::OnAmbientChanged(bool ambient_mode) {
173   application_->OnAmbientChanged(ambient_mode);
174 }
175
176 int WatchRuntime::Exec(int argc, char* argv[]) {
177   watch_app_lifecycle_callback_s ops =
178     {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
179
180   // onCreate
181   ops.create = [](int width, int height, void *data) -> bool {
182     WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
183     if (!runtime) {
184       LOGGER(ERROR) << "Runtime has not been created.";
185       return false;
186     }
187     return runtime->OnCreate();
188   };
189
190   // onTerminate
191   ops.terminate = [](void* data) -> void {
192     WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
193     if (!runtime) {
194       LOGGER(ERROR) << "Runtime has not been created.";
195       return;
196     }
197     runtime->OnTerminate();
198   };
199
200   // onPause
201   ops.pause = [](void* data) -> void {
202     WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
203     if (!runtime) {
204       LOGGER(ERROR) << "Runtime has not been created.";
205       return;
206     }
207     runtime->OnPause();
208   };
209
210   // onResume
211   ops.resume = [](void* data) -> void {
212     WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
213     if (!runtime) {
214       LOGGER(ERROR) << "Runtime has not been created.";
215       return;
216     }
217     runtime->OnResume();
218   };
219
220   // onAppControl
221   ops.app_control = [](app_control_h app_control, void* data) -> void {
222     WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
223     if (!runtime) {
224       LOGGER(ERROR) << "Runtime has not been created.";
225       return;
226     }
227     runtime->OnAppControl(app_control);
228   };
229
230   // onTimeTick
231   ops.time_tick = [](watch_time_h watch_time, void* data) -> void {
232     WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
233     if (!runtime) {
234       LOGGER(ERROR) << "Runtime has not been created.";
235       return;
236     }
237     runtime->OnTimeTick(watch_time);
238   };
239
240   // onAmbientTick
241   ops.ambient_tick = [](watch_time_h watch_time, void* data) -> void {
242     WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
243     if (!runtime) {
244       LOGGER(ERROR) << "Runtime has not been created.";
245       return;
246     }
247     runtime->OnAmbientTick(watch_time);
248   };
249
250   // onAmbientChanged
251   ops.ambient_changed = [](bool ambient_mode, void* data) -> void {
252     WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(data);
253     if (!runtime) {
254       LOGGER(ERROR) << "Runtime has not been created.";
255       return;
256     }
257     // To render web application on ambient mode
258     if (ambient_mode) {
259       runtime->OnResume();
260     } else {
261       runtime->OnPause();
262     }
263     runtime->OnAmbientChanged(ambient_mode);
264   };
265
266   // language changed callback
267   auto language_changed = [](app_event_info_h event_info, void* user_data) {
268     char* str;
269     if (app_event_get_language(event_info, &str) == 0 && str != NULL) {
270       std::string language = std::string(str);
271       std::free(str);
272       WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(user_data);
273       runtime->OnLanguageChanged(language);
274     }
275   };
276
277   auto low_memory = [](app_event_info_h /*event_info*/, void* user_data) {
278     WatchRuntime* runtime = reinterpret_cast<WatchRuntime*>(user_data);
279     runtime->OnLowMemory();
280   };
281
282   app_event_handler_h ev_handle;
283   watch_app_add_event_handler(&ev_handle,
284                            APP_EVENT_LANGUAGE_CHANGED,
285                            language_changed,
286                            this);
287   watch_app_add_event_handler(&ev_handle,
288                            APP_EVENT_LOW_MEMORY,
289                            low_memory,
290                            this);
291   STEP_PROFILE_START("watch_app_main -> OnCreate");
292
293   return watch_app_main(argc, argv, &ops, this);
294 }
295
296 }  // namespace runtime