Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / browser / application_tizen.cc
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.
5
6 #include "xwalk/application/browser/application_tizen.h"
7
8 #include <set>
9 #include <string>
10 #include <vector>
11
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"
16
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"
22
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"
29 #endif
30
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"
34
35 namespace xwalk {
36
37 namespace widget_keys = application_widget_keys;
38
39 namespace application {
40
41 blink::WebScreenOrientationLockType GetDefaultOrientation(
42     const base::WeakPtr<Application>& app) {
43   TizenSettingInfo* info = static_cast<TizenSettingInfo*>(
44     app->data()->GetManifestData(widget_keys::kTizenSettingKey));
45   if (!info)
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;
54     default:
55       NOTREACHED();
56       return blink::WebScreenOrientationLockDefault;
57   }
58 }
59
60 class ScreenOrientationProviderTizen :
61     public content::ScreenOrientationProvider {
62  public:
63   explicit ScreenOrientationProviderTizen(const base::WeakPtr<Application>& app)
64       : app_(app),
65         request_id_(0) {
66   }
67
68   virtual void LockOrientation(
69       int request_id,
70       blink::WebScreenOrientationLockType lock) OVERRIDE {
71     if (!app_)
72       return;
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);
83         break;
84       }
85     }
86   }
87
88   virtual void UnlockOrientation() OVERRIDE {
89     LockOrientation(request_id_, GetDefaultOrientation(app_));
90   }
91
92   virtual void OnOrientationChange() OVERRIDE {}
93
94  private:
95   base::WeakPtr<Application> app_;
96   int request_id_;
97 };
98
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);
106 #endif
107   cookie_manager_ = scoped_ptr<CookieManager>(
108       new CookieManager(id(), runtime_context_));
109 }
110
111 ApplicationTizen::~ApplicationTizen() {
112 #if defined(USE_OZONE)
113   ui::PlatformEventSource::GetInstance()->RemovePlatformEventObserver(this);
114 #endif
115 }
116
117 void ApplicationTizen::Hide() {
118   DCHECK(!runtimes_.empty());
119   std::set<Runtime*>::iterator it = runtimes_.begin();
120   for (; it != runtimes_.end(); ++it) {
121     if ((*it)->window())
122       (*it)->window()->Minimize();
123   }
124 }
125
126 void ApplicationTizen::Show() {
127   DCHECK(!runtimes_.empty());
128   for (Runtime* runtime : runtimes_) {
129     if (auto window = runtime->window())
130       window->Restore();
131   }
132 }
133
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);
140
141     provider->LockOrientation(0, GetDefaultOrientation(GetWeakPtr()));
142     return true;
143   }
144   return false;
145 }
146
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()));
151   }
152   return base::FilePath();
153 }
154
155 void ApplicationTizen::Suspend() {
156   if (is_suspended_)
157     return;
158
159   DCHECK(render_process_host_);
160   render_process_host_->Send(new ViewMsg_SuspendJSEngine(true));
161
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();
167   }
168   is_suspended_ = true;
169 }
170
171 void ApplicationTizen::Resume() {
172   if (!is_suspended_)
173     return;
174
175   DCHECK(render_process_host_);
176   render_process_host_->Send(new ViewMsg_SuspendJSEngine(false));
177
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();
183   }
184   is_suspended_ = false;
185 }
186
187 #if defined(USE_OZONE)
188 void ApplicationTizen::WillProcessEvent(const ui::PlatformEvent& event) {}
189
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)
194     return;
195
196   ui::KeyEvent* key_event = static_cast<ui::KeyEvent*>(ui_event);
197
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)
205     return;
206
207   TizenSettingInfo* info = static_cast<TizenSettingInfo*>(
208       data()->GetManifestData(widget_keys::kTizenSettingKey));
209   if (info && !info->hwkey_enabled())
210     return;
211
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()));
216   }
217 }
218 #endif
219
220 void ApplicationTizen::RemoveAllCookies() {
221   cookie_manager_->RemoveAllCookies();
222 }
223
224 void ApplicationTizen::SetUserAgentString(
225     const std::string& user_agent_string) {
226   cookie_manager_->SetUserAgentString(render_process_host_, user_agent_string);
227 }
228
229 }  // namespace application
230 }  // namespace xwalk