Memory optimization of web app
[platform/framework/web/crosswalk-tizen.git] / runtime / browser / native_window.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 "runtime/browser/native_window.h"
18
19 #include <Ecore_Wayland.h>
20 #include <ewk_chromium.h>
21 #include <cstdint>
22
23 #include "common/logger.h"
24
25 namespace runtime {
26
27 namespace {
28   const char* kEdjePath = "/usr/share/edje/xwalk/xwalk_tizen.edj";
29   const char* kWinowRotationEventKey = "wm,rotation,changed";
30   const char* kWinowFocusedEventKey = "focused";
31   const char* kWinowUnfocusedEventKey = "unfocused";
32 }  // namespace
33
34
35 NativeWindow::NativeWindow()
36     : window_(NULL),
37       window_type_(Type::NORMAL),
38       initialized_(false),
39       focus_(NULL),
40       content_(NULL),
41       rotation_(0),
42       handler_id_(0) {
43 }
44
45 NativeWindow::~NativeWindow() {
46   if (window_)
47     evas_object_del(window_);
48 }
49
50 void NativeWindow::Initialize() {
51   if (initialized_) {
52     LOGGER(DEBUG) << "already initialized";
53     return;
54   }
55   // window
56   window_ = CreateWindowInternal();
57   elm_win_conformant_set(window_, EINA_TRUE);
58   int w, h;
59   ecore_wl_screen_size_get(&w, &h);
60   evas_object_resize(window_, w, h);
61   elm_win_autodel_set(window_, EINA_TRUE);
62   evas_object_smart_callback_add(window_, "delete,request",
63                                  DidDeleteRequested, this);
64   evas_object_smart_callback_add(window_, "profile,changed",
65                                  DidProfileChanged, this);
66
67   #define EVAS_SIZE_EXPAND_FILL(obj) \
68     evas_object_size_hint_weight_set(obj, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); \
69     evas_object_size_hint_align_set(obj, EVAS_HINT_FILL, EVAS_HINT_FILL);
70
71   // background
72   Evas_Object* bg = evas_object_rectangle_add(evas_object_evas_get(window_));
73   evas_object_color_set(bg, 0, 0, 0, 255);
74   EVAS_SIZE_EXPAND_FILL(bg);
75   elm_win_resize_object_add(window_, bg);
76   evas_object_render_op_set(bg, EVAS_RENDER_BLEND);
77   evas_object_show(bg);
78
79   // conformant
80   Evas_Object* conformant = elm_conformant_add(window_);
81   EVAS_SIZE_EXPAND_FILL(conformant);
82   elm_win_resize_object_add(window_, conformant);
83   evas_object_show(conformant);
84
85   // top layout
86   Evas_Object* top_layout = elm_layout_add(conformant);
87   elm_layout_file_set(top_layout, kEdjePath, "web-application");
88   EVAS_SIZE_EXPAND_FILL(top_layout);
89   elm_object_content_set(conformant, top_layout);
90   evas_object_show(top_layout);
91
92   // focus
93   Evas_Object* focus = elm_bg_add(top_layout);
94   evas_object_size_hint_align_set(focus, EVAS_HINT_FILL, EVAS_HINT_FILL);
95   evas_object_size_hint_weight_set(focus, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
96   elm_object_focus_allow_set(focus, EINA_TRUE);
97   elm_object_part_content_set(top_layout, "elm.swallow.content", focus);
98   EVAS_SIZE_EXPAND_FILL(focus);
99   elm_access_object_unregister(focus);
100   evas_object_show(focus);
101   focus_ = focus;
102
103   // focus callback
104   auto focus_callback = [](void* user_data,
105                            Evas_Object*,
106                            void*) -> void {
107     NativeWindow* window = static_cast<NativeWindow*>(user_data);
108     window->DidFocusChanged(true);
109   };
110   auto unfocus_callback = [](void* user_data,
111                              Evas_Object*,
112                              void*) -> void {
113     NativeWindow* window = static_cast<NativeWindow*>(user_data);
114     window->DidFocusChanged(false);
115   };
116
117   evas_object_smart_callback_add(focus,
118                                  kWinowFocusedEventKey,
119                                  focus_callback,
120                                  this);
121   evas_object_smart_callback_add(focus,
122                                  kWinowUnfocusedEventKey,
123                                  unfocus_callback,
124                                  this);
125
126   // Rotation
127   auto rotation_callback = [](void* user_data,
128                               Evas_Object* obj,
129                               void*) -> void {
130       NativeWindow* window = static_cast<NativeWindow*>(user_data);
131       int degree = elm_win_rotation_get(obj);
132       window->DidRotation(degree);
133   };
134   evas_object_smart_callback_add(window_,
135                                  kWinowRotationEventKey,
136                                  rotation_callback,
137                                  this);
138
139   if (w > h) {
140     natural_orientation_ = ScreenOrientation::LANDSCAPE_PRIMARY;
141   } else {
142     natural_orientation_ = ScreenOrientation::PORTRAIT_PRIMARY;
143   }
144
145   initialized_ = true;
146 }
147
148 void NativeWindow::DidDeleteRequested(void* /*data*/,
149     Evas_Object* /*obj*/, void* /*event_info*/) {
150   LOGGER(DEBUG) << "didDeleteRequested";
151   elm_exit();
152 }
153
154 void NativeWindow::DidProfileChanged(void* /*data*/,
155     Evas_Object* /*obj*/, void* /*event_info*/) {
156   LOGGER(DEBUG) << "didProfileChanged";
157 }
158
159 Evas_Object* NativeWindow::evas_object() const {
160   return window_;
161 }
162
163 void NativeWindow::SetContent(Evas_Object* content) {
164   // Remarks
165   // If any object was already set as a content object in the same part,
166   // the previous object will be deleted automatically with this call.
167   // If the content is NULL, this call will just delete the previous object.
168   // If the If you wish to preserve it,
169   // issue elm_object_part_content_unset() on it first.
170
171   evas_object_show(content);
172   evas_object_hide(
173     elm_object_part_content_unset(focus_, "elm.swallow.content"));
174   elm_object_part_content_set(focus_, "elm.swallow.content", content);
175   elm_object_focus_set(focus_, EINA_TRUE);
176   content_ = content;
177
178   // attached webview was resized by evas_norender API
179   evas_norender(evas_object_evas_get(window_));
180 }
181
182 void NativeWindow::DidRotation(int degree) {
183   rotation_ = degree;
184   auto it = handler_table_.begin();
185   for ( ; it != handler_table_.end(); ++it) {
186     it->second(degree);
187   }
188 }
189
190 void NativeWindow::DidFocusChanged(bool got) {
191   if (content_ != NULL) {
192     ewk_view_focus_set(content_, got ? EINA_TRUE : EINA_FALSE);
193   }
194 }
195
196 int NativeWindow::AddRotationHandler(RotationHandler handler) {
197   int id = handler_id_++;
198   handler_table_[id] = handler;
199   return id;
200 }
201
202 void NativeWindow::RemoveRotationHandler(int id) {
203   handler_table_.erase(id);
204 }
205
206 void NativeWindow::SetRotationLock(int degree) {
207   if (degree != -1)
208     rotation_ = degree % 360;
209   elm_win_wm_rotation_preferred_rotation_set(window_, rotation_);
210 }
211
212 void NativeWindow::SetRotationLock(ScreenOrientation orientation) {
213   int portrait_natural_angle[] = {
214     0,  // PORTRAIT_PRIMARY
215     180,  // PORTRAIT_SECONDARY
216     270,  // LANDSCAPE_PRIMARY
217     90,  // LANDSCAPE_SECONDARY
218     0,  // NATURAL
219     -1  // ANY
220   };
221   int landscape_natural_angle[] = {
222     270,  // PORTRAIT_PRIMARY
223     90,  // PORTRAIT_SECONDARY
224     0,  // LANDSCAPE_PRIMARY
225     180,  // LANDSCAPE_SECONDARY
226     0,  // NATURAL
227     -1,  // ANY
228   };
229   auto& convert_table =
230       natural_orientation_ == ScreenOrientation::PORTRAIT_PRIMARY ?
231           portrait_natural_angle :
232           landscape_natural_angle;
233   SetRotationLock(convert_table[static_cast<int>(orientation)]);
234 }
235
236
237 void NativeWindow::SetAutoRotation() {
238   elm_win_wm_rotation_preferred_rotation_set(window_, -1);
239   if (elm_win_wm_rotation_supported_get(window_)) {
240     const int rotation[4] = {0, 90, 180, 270};
241     elm_win_wm_rotation_available_rotations_set(window_, rotation, 4);
242   }
243   rotation_ = elm_win_rotation_get(window_);
244 }
245
246 void NativeWindow::Show() {
247   evas_object_show(window_);
248 }
249
250 void NativeWindow::Active() {
251   elm_win_activate(window_);
252 }
253
254 void NativeWindow::InActive() {
255   elm_win_lower(window_);
256 }
257
258 void NativeWindow::FullScreen(bool enable) {
259   elm_win_indicator_opacity_set(window_,
260       enable ? ELM_WIN_INDICATOR_TRANSPARENT : ELM_WIN_INDICATOR_OPAQUE);
261 }
262
263 }  // namespace runtime