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.
5 #include "xwalk/application/browser/application_tizen.h"
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"
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"
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"
29 #include "xwalk/application/common/application_manifest_constants.h"
30 #include "xwalk/application/common/manifest_handlers/tizen_splash_screen_handler.h"
34 namespace widget_keys = application_widget_keys;
36 namespace application {
38 blink::WebScreenOrientationLockType GetDefaultOrientation(
39 const base::WeakPtr<Application>& app) {
40 TizenSettingInfo* info = static_cast<TizenSettingInfo*>(
41 app->data()->GetManifestData(widget_keys::kTizenSettingKey));
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;
53 return blink::WebScreenOrientationLockDefault;
57 class ScreenOrientationProviderTizen :
58 public content::ScreenOrientationProvider {
60 explicit ScreenOrientationProviderTizen(const base::WeakPtr<Application>& app)
65 virtual void LockOrientation(
67 blink::WebScreenOrientationLockType lock) OVERRIDE {
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);
85 virtual void UnlockOrientation() OVERRIDE {
86 LockOrientation(request_id_, GetDefaultOrientation(app_));
89 virtual void OnOrientationChange() OVERRIDE {}
92 base::WeakPtr<Application> app_;
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);
106 ApplicationTizen::~ApplicationTizen() {
107 #if defined(USE_OZONE)
108 ui::PlatformEventSource::GetInstance()->RemovePlatformEventObserver(this);
112 void ApplicationTizen::Hide() {
113 DCHECK(!runtimes_.empty());
114 std::set<Runtime*>::iterator it = runtimes_.begin();
115 for (; it != runtimes_.end(); ++it) {
117 (*it)->window()->Minimize();
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);
128 provider->LockOrientation(0, GetDefaultOrientation(GetWeakPtr()));
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()));
139 return base::FilePath();
142 void ApplicationTizen::Suspend() {
146 DCHECK(render_process_host_);
147 render_process_host_->Send(new ViewMsg_SuspendJSEngine(true));
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();
155 is_suspended_ = true;
158 void ApplicationTizen::Resume() {
162 DCHECK(render_process_host_);
163 render_process_host_->Send(new ViewMsg_SuspendJSEngine(false));
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();
171 is_suspended_ = false;
174 #if defined(USE_OZONE)
175 void ApplicationTizen::WillProcessEvent(const ui::PlatformEvent& event) {}
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)
183 ui::KeyEvent* key_event = static_cast<ui::KeyEvent*>(ui_event);
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)
194 TizenSettingInfo* info = static_cast<TizenSettingInfo*>(
195 data()->GetManifestData(widget_keys::kTizenSettingKey));
196 if (info && !info->hwkey_enabled())
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()));
207 } // namespace application