2d8aaccf31635cf3950964e8b4a84a657d93c234
[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
15 #include "xwalk/runtime/browser/ui/native_app_window.h"
16 #include "xwalk/runtime/browser/ui/native_app_window_tizen.h"
17 #include "xwalk/runtime/common/xwalk_common_messages.h"
18
19 #if defined(USE_OZONE)
20 #include "content/public/browser/render_view_host.h"
21 #include "ui/events/event.h"
22 #include "ui/events/event_constants.h"
23 #include "ui/events/keycodes/keyboard_codes_posix.h"
24 #include "ui/events/platform/platform_event_source.h"
25 #include "xwalk/application/common/manifest_handlers/tizen_setting_handler.h"
26 #endif
27
28 #include "xwalk/application/common/application_manifest_constants.h"
29
30 namespace xwalk {
31
32 namespace widget_keys = application_widget_keys;
33
34 namespace application {
35
36 ApplicationTizen::ApplicationTizen(
37     scoped_refptr<ApplicationData> data,
38     RuntimeContext* runtime_context,
39     Application::Observer* observer)
40     : Application(data, runtime_context, observer) {
41 #if defined(USE_OZONE)
42   ui::PlatformEventSource::GetInstance()->AddPlatformEventObserver(this);
43 #endif
44 }
45
46 ApplicationTizen::~ApplicationTizen() {
47 #if defined(USE_OZONE)
48   ui::PlatformEventSource::GetInstance()->RemovePlatformEventObserver(this);
49 #endif
50 }
51
52 void ApplicationTizen::Hide() {
53   DCHECK(!runtimes_.empty());
54   std::set<Runtime*>::iterator it = runtimes_.begin();
55   for (; it != runtimes_.end(); ++it) {
56     if ((*it)->window())
57       (*it)->window()->Hide();
58   }
59 }
60
61 bool ApplicationTizen::Launch(const LaunchParams& launch_params) {
62   if (Application::Launch(launch_params)) {
63     DCHECK(render_process_host_);
64     render_process_host_->GetScreenOrientationDispatcherHost()->
65         SetProviderForTests(this);
66     return true;
67   }
68   return false;
69 }
70
71 #if defined(USE_OZONE)
72 void ApplicationTizen::WillProcessEvent(const ui::PlatformEvent& event) {}
73
74 void ApplicationTizen::DidProcessEvent(
75     const ui::PlatformEvent& event) {
76   ui::Event* ui_event = static_cast<ui::Event*>(event);
77   if (!ui_event->IsKeyEvent() || ui_event->type() != ui::ET_KEY_PRESSED)
78     return;
79
80   ui::KeyEvent* key_event = static_cast<ui::KeyEvent*>(ui_event);
81
82   // FIXME: Most Wayland devices don't have similar hardware button for 'back'
83   // and 'memu' as Tizen Mobile, even that hardare buttons could be different
84   // across different kinds of Wayland platforms.
85   // Here use external keyboard button 'Backspace' & 'HOME' to emulate 'back'
86   // and 'menu' key. Should change this if there is customized key binding.
87   if (key_event->key_code() != ui::VKEY_BACK &&
88       key_event->key_code() != ui::VKEY_HOME)
89     return;
90
91   TizenSettingInfo* info = static_cast<TizenSettingInfo*>(
92       data()->GetManifestData(widget_keys::kTizenSettingKey));
93   if (info && !info->hwkey_enabled())
94     return;
95
96   for (std::set<xwalk::Runtime*>::iterator it = runtimes_.begin();
97       it != runtimes_.end(); ++it) {
98     (*it)->web_contents()->GetRenderViewHost()->Send(new ViewMsg_HWKeyPressed(
99         (*it)->web_contents()->GetRoutingID(), key_event->key_code()));
100   }
101 }
102 #endif
103
104 void ApplicationTizen::LockOrientation(
105       blink::WebScreenOrientationLockType lock) {
106   DCHECK(!runtimes_.empty());
107   // FIXME: Probably need better alignment with
108   // https://w3c.github.io/screen-orientation/#screen-orientation-lock-lifetime
109   std::set<Runtime*>::iterator it = runtimes_.begin();
110   for (; it != runtimes_.end(); ++it) {
111     NativeAppWindow* window = (*it)->window();
112     if (window && window->IsActive()) {
113       ToNativeAppWindowTizen(window)->LockOrientation(lock);
114       break;
115     }
116   }
117 }
118
119 void ApplicationTizen::UnlockOrientation() {
120   LockOrientation(blink::WebScreenOrientationLockDefault);
121 }
122
123 }  // namespace application
124 }  // namespace xwalk