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"
15 #include "xwalk/runtime/browser/ui/native_app_window_tizen.h"
16 #include "xwalk/runtime/common/xwalk_common_messages.h"
18 #if defined(USE_OZONE)
19 #include "content/public/browser/render_view_host.h"
20 #include "ui/events/event.h"
21 #include "ui/events/event_constants.h"
22 #include "ui/events/keycodes/keyboard_codes_posix.h"
23 #include "ui/events/platform/platform_event_source.h"
24 #include "xwalk/application/common/manifest_handlers/tizen_setting_handler.h"
27 #include "xwalk/application/common/application_manifest_constants.h"
28 #include "xwalk/application/common/manifest_handlers/csp_handler.h"
29 #include "xwalk/application/common/manifest_handlers/navigation_handler.h"
33 namespace widget_keys = application_widget_keys;
35 namespace application {
38 const char kAsterisk[] = "*";
40 const char kDirectiveValueSelf[] = "'self'";
41 const char kDirectiveValueNone[] = "'none'";
43 const char kDirectiveNameDefault[] = "default-src";
44 const char kDirectiveNameScript[] = "script-src";
45 const char kDirectiveNameStyle[] = "style-src";
46 const char kDirectiveNameObject[] = "object-src";
48 CSPInfo* GetDefaultCSPInfo() {
49 static CSPInfo default_csp_info;
50 if (default_csp_info.GetDirectives().empty()) {
51 std::vector<std::string> directive_all;
52 std::vector<std::string> directive_self;
53 std::vector<std::string> directive_none;
54 directive_all.push_back(kAsterisk);
55 directive_self.push_back(kDirectiveValueSelf);
56 directive_none.push_back(kDirectiveValueNone);
58 default_csp_info.SetDirective(kDirectiveNameDefault, directive_all);
59 default_csp_info.SetDirective(kDirectiveNameScript, directive_self);
60 default_csp_info.SetDirective(kDirectiveNameStyle, directive_self);
61 default_csp_info.SetDirective(kDirectiveNameObject, directive_none);
64 return (new CSPInfo(default_csp_info));
70 ApplicationTizen::ApplicationTizen(
71 scoped_refptr<ApplicationData> data,
72 RuntimeContext* runtime_context,
73 Application::Observer* observer)
74 : Application(data, runtime_context, observer) {
75 #if defined(USE_OZONE)
76 ui::PlatformEventSource::GetInstance()->AddPlatformEventObserver(this);
80 ApplicationTizen::~ApplicationTizen() {
81 #if defined(USE_OZONE)
82 ui::PlatformEventSource::GetInstance()->RemovePlatformEventObserver(this);
86 void ApplicationTizen::Hide() {
87 DCHECK(!runtimes_.empty());
88 std::set<Runtime*>::iterator it = runtimes_.begin();
89 for (; it != runtimes_.end(); ++it) {
91 (*it)->window()->Hide();
95 bool ApplicationTizen::Launch(const LaunchParams& launch_params) {
96 if (Application::Launch(launch_params)) {
97 DCHECK(render_process_host_);
98 render_process_host_->GetScreenOrientationDispatcherHost()->
99 SetProviderForTests(this);
105 void ApplicationTizen::InitSecurityPolicy() {
106 // On Tizen, CSP mode has higher priority, and WARP will be disabled
107 // if the application is under CSP mode.
108 if (!data_->HasCSPDefined()) {
109 Application::InitSecurityPolicy();
113 if (data_->GetPackageType() != Package::WGT)
117 static_cast<CSPInfo*>(data_->GetManifestData(widget_keys::kCSPKey));
118 if (!csp_info || csp_info->GetDirectives().empty())
119 data_->SetManifestData(widget_keys::kCSPKey, GetDefaultCSPInfo());
121 // Always enable security mode when under CSP mode.
122 security_mode_enabled_ = true;
123 NavigationInfo* info = static_cast<NavigationInfo*>(
124 data_->GetManifestData(widget_keys::kAllowNavigationKey));
126 const std::vector<std::string>& allowed_list = info->GetAllowedDomains();
127 for (std::vector<std::string>::const_iterator it = allowed_list.begin();
128 it != allowed_list.end(); ++it) {
129 // If the policy is "*", it represents that any external link is allowed
131 if ((*it) == kAsterisk) {
132 security_mode_enabled_ = false;
136 // If the policy start with "*.", like this: *.domain,
137 // means that can access to all subdomains for 'domain',
138 // otherwise, the host of request url should exactly the same
140 bool subdomains = ((*it).find("*.") == 0);
141 std::string host = subdomains ? (*it).substr(2) : (*it);
142 AddSecurityPolicy(GURL("http://" + host), subdomains);
143 AddSecurityPolicy(GURL("https://" + host), subdomains);
146 DCHECK(render_process_host_);
147 render_process_host_->Send(
148 new ViewMsg_EnableSecurityMode(
149 ApplicationData::GetBaseURLFromApplicationId(id()),
150 SecurityPolicy::CSP));
153 #if defined(USE_OZONE)
154 void ApplicationTizen::WillProcessEvent(const ui::PlatformEvent& event) {}
156 void ApplicationTizen::DidProcessEvent(
157 const ui::PlatformEvent& event) {
158 ui::Event* ui_event = static_cast<ui::Event*>(event);
159 if (!ui_event->IsKeyEvent() || ui_event->type() != ui::ET_KEY_PRESSED)
162 ui::KeyEvent* key_event = static_cast<ui::KeyEvent*>(ui_event);
164 // FIXME: Most Wayland devices don't have similar hardware button for 'back'
165 // and 'memu' as Tizen Mobile, even that hardare buttons could be different
166 // across different kinds of Wayland platforms.
167 // Here use external keyboard button 'Backspace' & 'HOME' to emulate 'back'
168 // and 'menu' key. Should change this if there is customized key binding.
169 if (key_event->key_code() != ui::VKEY_BACK &&
170 key_event->key_code() != ui::VKEY_HOME)
173 TizenSettingInfo* info = static_cast<TizenSettingInfo*>(
174 data()->GetManifestData(widget_keys::kTizenSettingKey));
175 if (info && !info->hwkey_enabled())
178 for (std::set<xwalk::Runtime*>::iterator it = runtimes_.begin();
179 it != runtimes_.end(); ++it) {
180 (*it)->web_contents()->GetRenderViewHost()->Send(new ViewMsg_HWKeyPressed(
181 (*it)->web_contents()->GetRoutingID(), key_event->key_code()));
186 void ApplicationTizen::LockOrientation(
187 blink::WebScreenOrientationLockType lock) {
188 DCHECK(!runtimes_.empty());
189 // FIXME: Probably need better alignment with
190 // https://w3c.github.io/screen-orientation/#screen-orientation-lock-lifetime
191 std::set<Runtime*>::iterator it = runtimes_.begin();
192 for (; it != runtimes_.end(); ++it) {
193 NativeAppWindow* window = (*it)->window();
194 if (window && window->IsActive()) {
195 ToNativeAppWindowTizen(window)->LockOrientation(lock);
201 void ApplicationTizen::UnlockOrientation() {
202 LockOrientation(blink::WebScreenOrientationLockDefault);
205 } // namespace application