1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
6 #include "xwalk/application/browser/application_tizen.h"
12 #include "content/public/browser/web_contents.h"
13 #include "content/public/browser/render_process_host.h"
14 #include "content/public/browser/screen_orientation_dispatcher_host.h"
15 #include "content/public/browser/screen_orientation_provider.h"
17 #include "xwalk/runtime/browser/runtime_context.h"
18 #include "xwalk/runtime/browser/runtime_url_request_context_getter.h"
19 #include "xwalk/runtime/browser/ui/native_app_window.h"
20 #include "xwalk/runtime/browser/ui/native_app_window_tizen.h"
21 #include "xwalk/runtime/common/xwalk_common_messages.h"
23 #if defined(USE_OZONE)
24 #include "content/public/browser/render_view_host.h"
25 #include "ui/events/event.h"
26 #include "ui/events/event_constants.h"
27 #include "ui/events/keycodes/keyboard_codes_posix.h"
28 #include "ui/events/platform/platform_event_source.h"
31 #include "xwalk/application/common/application_manifest_constants.h"
32 #include "xwalk/application/common/manifest_handlers/tizen_setting_handler.h"
33 #include "xwalk/application/common/manifest_handlers/tizen_splash_screen_handler.h"
37 namespace widget_keys = application_widget_keys;
39 namespace application {
41 blink::WebScreenOrientationLockType GetDefaultOrientation(
42 const base::WeakPtr<Application>& app) {
43 TizenSettingInfo* info = static_cast<TizenSettingInfo*>(
44 app->data()->GetManifestData(widget_keys::kTizenSettingKey));
46 return blink::WebScreenOrientationLockDefault;
47 switch (info->screen_orientation()) {
48 case TizenSettingInfo::PORTRAIT:
49 return blink::WebScreenOrientationLockPortrait;
50 case TizenSettingInfo::LANDSCAPE:
51 return blink::WebScreenOrientationLockLandscape;
52 case TizenSettingInfo::AUTO:
53 return blink::WebScreenOrientationLockAny;
56 return blink::WebScreenOrientationLockDefault;
60 class ScreenOrientationProviderTizen :
61 public content::ScreenOrientationProvider {
63 explicit ScreenOrientationProviderTizen(const base::WeakPtr<Application>& app)
68 virtual void LockOrientation(
70 blink::WebScreenOrientationLockType lock) OVERRIDE {
73 request_id_ = request_id;
74 const std::set<Runtime*>& runtimes = app_->runtimes();
75 DCHECK(!runtimes.empty());
76 // FIXME: Probably need better alignment with
77 // https://w3c.github.io/screen-orientation/#screen-orientation-lock-lifetime
78 std::set<Runtime*>::iterator it = runtimes.begin();
79 for (; it != runtimes.end(); ++it) {
80 NativeAppWindow* window = (*it)->window();
81 if (window && window->IsActive()) {
82 ToNativeAppWindowTizen(window)->LockOrientation(lock);
88 virtual void UnlockOrientation() OVERRIDE {
89 LockOrientation(request_id_, GetDefaultOrientation(app_));
92 virtual void OnOrientationChange() OVERRIDE {}
95 base::WeakPtr<Application> app_;
99 ApplicationTizen::ApplicationTizen(
100 scoped_refptr<ApplicationData> data,
101 RuntimeContext* runtime_context)
102 : Application(data, runtime_context),
103 is_suspended_(false) {
104 #if defined(USE_OZONE)
105 ui::PlatformEventSource::GetInstance()->AddPlatformEventObserver(this);
107 cookie_manager_ = scoped_ptr<CookieManager>(
108 new CookieManager(id(), runtime_context_));
111 ApplicationTizen::~ApplicationTizen() {
112 #if defined(USE_OZONE)
113 ui::PlatformEventSource::GetInstance()->RemovePlatformEventObserver(this);
117 void ApplicationTizen::Hide() {
118 DCHECK(!runtimes_.empty());
119 std::set<Runtime*>::iterator it = runtimes_.begin();
120 for (; it != runtimes_.end(); ++it) {
122 (*it)->window()->Minimize();
126 void ApplicationTizen::Show() {
127 DCHECK(!runtimes_.empty());
128 for (Runtime* runtime : runtimes_) {
129 if (auto window = runtime->window())
134 bool ApplicationTizen::Launch(const LaunchParams& launch_params) {
135 if (Application::Launch(launch_params)) {
136 DCHECK(web_contents_);
137 content::ScreenOrientationProvider *provider =
138 new ScreenOrientationProviderTizen(GetWeakPtr());
139 web_contents_->GetScreenOrientationDispatcherHost()->SetProvider(provider);
141 provider->LockOrientation(0, GetDefaultOrientation(GetWeakPtr()));
147 base::FilePath ApplicationTizen::GetSplashScreenPath() {
148 if (TizenSplashScreenInfo* ss_info = static_cast<TizenSplashScreenInfo*>(
149 data()->GetManifestData(widget_keys::kTizenSplashScreenKey))) {
150 return data()->path().Append(FILE_PATH_LITERAL(ss_info->src()));
152 return base::FilePath();
155 void ApplicationTizen::Suspend() {
159 DCHECK(render_process_host_);
160 render_process_host_->Send(new ViewMsg_SuspendJSEngine(true));
162 DCHECK(!runtimes_.empty());
163 std::set<Runtime*>::iterator it = runtimes_.begin();
164 for (; it != runtimes_.end(); ++it) {
165 if ((*it)->web_contents())
166 (*it)->web_contents()->WasHidden();
168 is_suspended_ = true;
171 void ApplicationTizen::Resume() {
175 DCHECK(render_process_host_);
176 render_process_host_->Send(new ViewMsg_SuspendJSEngine(false));
178 DCHECK(!runtimes_.empty());
179 std::set<Runtime*>::iterator it = runtimes_.begin();
180 for (; it != runtimes_.end(); ++it) {
181 if ((*it)->web_contents())
182 (*it)->web_contents()->WasShown();
184 is_suspended_ = false;
187 #if defined(USE_OZONE)
188 void ApplicationTizen::WillProcessEvent(const ui::PlatformEvent& event) {}
190 void ApplicationTizen::DidProcessEvent(
191 const ui::PlatformEvent& event) {
192 ui::Event* ui_event = static_cast<ui::Event*>(event);
193 if (!ui_event->IsKeyEvent() || ui_event->type() != ui::ET_KEY_PRESSED)
196 ui::KeyEvent* key_event = static_cast<ui::KeyEvent*>(ui_event);
198 // FIXME: Most Wayland devices don't have similar hardware button for 'back'
199 // and 'memu' as Tizen Mobile, even that hardare buttons could be different
200 // across different kinds of Wayland platforms.
201 // Here use external keyboard button 'Backspace' & 'HOME' to emulate 'back'
202 // and 'menu' key. Should change this if there is customized key binding.
203 if (key_event->key_code() != ui::VKEY_BACK &&
204 key_event->key_code() != ui::VKEY_HOME)
207 TizenSettingInfo* info = static_cast<TizenSettingInfo*>(
208 data()->GetManifestData(widget_keys::kTizenSettingKey));
209 if (info && !info->hwkey_enabled())
212 for (std::set<xwalk::Runtime*>::iterator it = runtimes_.begin();
213 it != runtimes_.end(); ++it) {
214 (*it)->web_contents()->GetRenderViewHost()->Send(new ViewMsg_HWKeyPressed(
215 (*it)->web_contents()->GetRoutingID(), key_event->key_code()));
220 void ApplicationTizen::RemoveAllCookies() {
221 cookie_manager_->RemoveAllCookies();
224 void ApplicationTizen::SetUserAgentString(
225 const std::string& user_agent_string) {
226 cookie_manager_->SetUserAgentString(render_process_host_, user_agent_string);
229 } // namespace application