a7e05878c35f4a1d0280b309f457af928d59a8a8
[platform/framework/web/crosswalk.git] / src / xwalk / application / browser / application_tizen.cc
1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "xwalk/application/browser/application_tizen.h"
6
7 #include <set>
8 #include <string>
9 #include <vector>
10
11 #include "content/public/browser/web_contents.h"
12 #include "content/public/browser/render_process_host.h"
13 #include "content/browser/screen_orientation/screen_orientation_dispatcher_host.h"
14 #include "content/browser/screen_orientation/screen_orientation_provider.h"
15
16 #include "xwalk/runtime/browser/ui/native_app_window.h"
17 #include "xwalk/runtime/browser/ui/native_app_window_tizen.h"
18 #include "xwalk/runtime/common/xwalk_common_messages.h"
19
20 #if defined(USE_OZONE)
21 #include "content/public/browser/render_view_host.h"
22 #include "ui/events/event.h"
23 #include "ui/events/event_constants.h"
24 #include "ui/events/keycodes/keyboard_codes_posix.h"
25 #include "ui/events/platform/platform_event_source.h"
26 #include "xwalk/application/common/manifest_handlers/tizen_setting_handler.h"
27 #endif
28
29 #include "xwalk/application/common/application_manifest_constants.h"
30 #include "xwalk/application/common/manifest_handlers/tizen_splash_screen_handler.h"
31
32 namespace xwalk {
33
34 namespace widget_keys = application_widget_keys;
35
36 namespace application {
37
38 blink::WebScreenOrientationLockType GetDefaultOrientation(
39     const base::WeakPtr<Application>& app) {
40   TizenSettingInfo* info = static_cast<TizenSettingInfo*>(
41     app->data()->GetManifestData(widget_keys::kTizenSettingKey));
42   if (!info)
43     return blink::WebScreenOrientationLockDefault;
44   switch (info->screen_orientation()) {
45     case TizenSettingInfo::PORTRAIT:
46       return blink::WebScreenOrientationLockPortrait;
47     case TizenSettingInfo::LANDSCAPE:
48       return blink::WebScreenOrientationLockLandscape;
49     case TizenSettingInfo::AUTO:
50       return blink::WebScreenOrientationLockAny;
51     default:
52       NOTREACHED();
53       return blink::WebScreenOrientationLockDefault;
54   }
55 }
56
57 class ScreenOrientationProviderTizen :
58     public content::ScreenOrientationProvider {
59  public:
60   explicit ScreenOrientationProviderTizen(const base::WeakPtr<Application>& app)
61       : app_(app),
62         request_id_(0) {
63   }
64
65   virtual void LockOrientation(
66       int request_id,
67       blink::WebScreenOrientationLockType lock) OVERRIDE {
68     if (!app_)
69       return;
70     request_id_ = request_id;
71     const std::set<Runtime*>& runtimes = app_->runtimes();
72     DCHECK(!runtimes.empty());
73     // FIXME: Probably need better alignment with
74     // https://w3c.github.io/screen-orientation/#screen-orientation-lock-lifetime
75     std::set<Runtime*>::iterator it = runtimes.begin();
76     for (; it != runtimes.end(); ++it) {
77       NativeAppWindow* window = (*it)->window();
78       if (window && window->IsActive()) {
79         ToNativeAppWindowTizen(window)->LockOrientation(lock);
80         break;
81       }
82     }
83   }
84
85   virtual void UnlockOrientation() OVERRIDE {
86     LockOrientation(request_id_, GetDefaultOrientation(app_));
87   }
88
89   virtual void OnOrientationChange() OVERRIDE {}
90
91  private:
92   base::WeakPtr<Application> app_;
93   int request_id_;
94 };
95
96 ApplicationTizen::ApplicationTizen(
97     scoped_refptr<ApplicationData> data,
98     RuntimeContext* runtime_context)
99     : Application(data, runtime_context),
100       is_suspended_(false) {
101 #if defined(USE_OZONE)
102   ui::PlatformEventSource::GetInstance()->AddPlatformEventObserver(this);
103 #endif
104 }
105
106 ApplicationTizen::~ApplicationTizen() {
107 #if defined(USE_OZONE)
108   ui::PlatformEventSource::GetInstance()->RemovePlatformEventObserver(this);
109 #endif
110 }
111
112 void ApplicationTizen::Hide() {
113   DCHECK(!runtimes_.empty());
114   std::set<Runtime*>::iterator it = runtimes_.begin();
115   for (; it != runtimes_.end(); ++it) {
116     if ((*it)->window())
117       (*it)->window()->Minimize();
118   }
119 }
120
121 bool ApplicationTizen::Launch(const LaunchParams& launch_params) {
122   if (Application::Launch(launch_params)) {
123     DCHECK(web_contents_);
124     content::ScreenOrientationProvider *provider =
125         new ScreenOrientationProviderTizen(GetWeakPtr());
126     web_contents_->GetScreenOrientationDispatcherHost()->SetProvider(provider);
127
128     provider->LockOrientation(0, GetDefaultOrientation(GetWeakPtr()));
129     return true;
130   }
131   return false;
132 }
133
134 base::FilePath ApplicationTizen::GetSplashScreenPath() {
135   if (TizenSplashScreenInfo* ss_info = static_cast<TizenSplashScreenInfo*>(
136       data()->GetManifestData(widget_keys::kTizenSplashScreenKey))) {
137     return data()->path().Append(FILE_PATH_LITERAL(ss_info->src()));
138   }
139   return base::FilePath();
140 }
141
142 void ApplicationTizen::Suspend() {
143   if (is_suspended_)
144     return;
145
146   DCHECK(render_process_host_);
147   render_process_host_->Send(new ViewMsg_SuspendJSEngine(true));
148
149   DCHECK(!runtimes_.empty());
150   std::set<Runtime*>::iterator it = runtimes_.begin();
151   for (; it != runtimes_.end(); ++it) {
152     if ((*it)->web_contents())
153       (*it)->web_contents()->WasHidden();
154   }
155   is_suspended_ = true;
156 }
157
158 void ApplicationTizen::Resume() {
159   if (!is_suspended_)
160     return;
161
162   DCHECK(render_process_host_);
163   render_process_host_->Send(new ViewMsg_SuspendJSEngine(false));
164
165   DCHECK(!runtimes_.empty());
166   std::set<Runtime*>::iterator it = runtimes_.begin();
167   for (; it != runtimes_.end(); ++it) {
168     if ((*it)->web_contents())
169       (*it)->web_contents()->WasShown();
170   }
171   is_suspended_ = false;
172 }
173
174 #if defined(USE_OZONE)
175 void ApplicationTizen::WillProcessEvent(const ui::PlatformEvent& event) {}
176
177 void ApplicationTizen::DidProcessEvent(
178     const ui::PlatformEvent& event) {
179   ui::Event* ui_event = static_cast<ui::Event*>(event);
180   if (!ui_event->IsKeyEvent() || ui_event->type() != ui::ET_KEY_PRESSED)
181     return;
182
183   ui::KeyEvent* key_event = static_cast<ui::KeyEvent*>(ui_event);
184
185   // FIXME: Most Wayland devices don't have similar hardware button for 'back'
186   // and 'memu' as Tizen Mobile, even that hardare buttons could be different
187   // across different kinds of Wayland platforms.
188   // Here use external keyboard button 'Backspace' & 'HOME' to emulate 'back'
189   // and 'menu' key. Should change this if there is customized key binding.
190   if (key_event->key_code() != ui::VKEY_BACK &&
191       key_event->key_code() != ui::VKEY_HOME)
192     return;
193
194   TizenSettingInfo* info = static_cast<TizenSettingInfo*>(
195       data()->GetManifestData(widget_keys::kTizenSettingKey));
196   if (info && !info->hwkey_enabled())
197     return;
198
199   for (std::set<xwalk::Runtime*>::iterator it = runtimes_.begin();
200       it != runtimes_.end(); ++it) {
201     (*it)->web_contents()->GetRenderViewHost()->Send(new ViewMsg_HWKeyPressed(
202         (*it)->web_contents()->GetRoutingID(), key_event->key_code()));
203   }
204 }
205 #endif
206
207 }  // namespace application
208 }  // namespace xwalk