fd41d65ca5d19a205c59ff506453d935d6e022ad
[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 class ScreenOrientationProviderTizen :
39     public content::ScreenOrientationProvider {
40  public:
41   explicit ScreenOrientationProviderTizen(const base::WeakPtr<Application>& app)
42       : app_(app),
43         request_id_(0) {
44   }
45
46   virtual void LockOrientation(
47       int request_id,
48       blink::WebScreenOrientationLockType lock) OVERRIDE {
49     if (!app_)
50       return;
51     request_id_ = request_id;
52     const std::set<Runtime*>& runtimes = app_->runtimes();
53     DCHECK(!runtimes.empty());
54     // FIXME: Probably need better alignment with
55     // https://w3c.github.io/screen-orientation/#screen-orientation-lock-lifetime
56     std::set<Runtime*>::iterator it = runtimes.begin();
57     for (; it != runtimes.end(); ++it) {
58       NativeAppWindow* window = (*it)->window();
59       if (window && window->IsActive()) {
60         ToNativeAppWindowTizen(window)->LockOrientation(lock);
61         break;
62       }
63     }
64   }
65
66   virtual void UnlockOrientation() OVERRIDE {
67     LockOrientation(request_id_, blink::WebScreenOrientationLockDefault);
68   }
69
70   virtual void OnOrientationChange() OVERRIDE {}
71
72  private:
73   base::WeakPtr<Application> app_;
74   int request_id_;
75 };
76
77 ApplicationTizen::ApplicationTizen(
78     scoped_refptr<ApplicationData> data,
79     RuntimeContext* runtime_context)
80     : Application(data, runtime_context),
81       is_suspended_(false) {
82 #if defined(USE_OZONE)
83   ui::PlatformEventSource::GetInstance()->AddPlatformEventObserver(this);
84 #endif
85 }
86
87 ApplicationTizen::~ApplicationTizen() {
88 #if defined(USE_OZONE)
89   ui::PlatformEventSource::GetInstance()->RemovePlatformEventObserver(this);
90 #endif
91 }
92
93 void ApplicationTizen::Hide() {
94   DCHECK(!runtimes_.empty());
95   std::set<Runtime*>::iterator it = runtimes_.begin();
96   for (; it != runtimes_.end(); ++it) {
97     if ((*it)->window())
98       (*it)->window()->Hide();
99   }
100 }
101
102 bool ApplicationTizen::Launch(const LaunchParams& launch_params) {
103   if (Application::Launch(launch_params)) {
104     DCHECK(web_contents_);
105     web_contents_->GetScreenOrientationDispatcherHost()->
106         SetProvider(new ScreenOrientationProviderTizen(GetWeakPtr()));
107     return true;
108   }
109   return false;
110 }
111
112 base::FilePath ApplicationTizen::GetSplashScreenPath() {
113   if (TizenSplashScreenInfo* ss_info = static_cast<TizenSplashScreenInfo*>(
114       data()->GetManifestData(widget_keys::kTizenSplashScreenKey))) {
115     return data()->Path().Append(FILE_PATH_LITERAL(ss_info->src()));
116   }
117   return base::FilePath();
118 }
119
120 void ApplicationTizen::Suspend() {
121   if (is_suspended_)
122     return;
123
124   DCHECK(render_process_host_);
125   render_process_host_->Send(new ViewMsg_SuspendJSEngine(true));
126
127   DCHECK(!runtimes_.empty());
128   std::set<Runtime*>::iterator it = runtimes_.begin();
129   for (; it != runtimes_.end(); ++it) {
130     if ((*it)->web_contents())
131       (*it)->web_contents()->WasHidden();
132   }
133   is_suspended_ = true;
134 }
135
136 void ApplicationTizen::Resume() {
137   if (!is_suspended_)
138     return;
139
140   DCHECK(render_process_host_);
141   render_process_host_->Send(new ViewMsg_SuspendJSEngine(false));
142
143   DCHECK(!runtimes_.empty());
144   std::set<Runtime*>::iterator it = runtimes_.begin();
145   for (; it != runtimes_.end(); ++it) {
146     if ((*it)->web_contents())
147       (*it)->web_contents()->WasShown();
148   }
149   is_suspended_ = false;
150 }
151
152 #if defined(USE_OZONE)
153 void ApplicationTizen::WillProcessEvent(const ui::PlatformEvent& event) {}
154
155 void ApplicationTizen::DidProcessEvent(
156     const ui::PlatformEvent& event) {
157   ui::Event* ui_event = static_cast<ui::Event*>(event);
158   if (!ui_event->IsKeyEvent() || ui_event->type() != ui::ET_KEY_PRESSED)
159     return;
160
161   ui::KeyEvent* key_event = static_cast<ui::KeyEvent*>(ui_event);
162
163   // FIXME: Most Wayland devices don't have similar hardware button for 'back'
164   // and 'memu' as Tizen Mobile, even that hardare buttons could be different
165   // across different kinds of Wayland platforms.
166   // Here use external keyboard button 'Backspace' & 'HOME' to emulate 'back'
167   // and 'menu' key. Should change this if there is customized key binding.
168   if (key_event->key_code() != ui::VKEY_BACK &&
169       key_event->key_code() != ui::VKEY_HOME)
170     return;
171
172   TizenSettingInfo* info = static_cast<TizenSettingInfo*>(
173       data()->GetManifestData(widget_keys::kTizenSettingKey));
174   if (info && !info->hwkey_enabled())
175     return;
176
177   for (std::set<xwalk::Runtime*>::iterator it = runtimes_.begin();
178       it != runtimes_.end(); ++it) {
179     (*it)->web_contents()->GetRenderViewHost()->Send(new ViewMsg_HWKeyPressed(
180         (*it)->web_contents()->GetRoutingID(), key_event->key_code()));
181   }
182 }
183 #endif
184
185 }  // namespace application
186 }  // namespace xwalk