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"
14 #include "xwalk/runtime/browser/ui/native_app_window.h"
15 #include "xwalk/runtime/common/xwalk_common_messages.h"
17 #if defined(USE_OZONE)
18 #include "content/public/browser/render_view_host.h"
19 #include "ui/events/event.h"
20 #include "ui/events/event_constants.h"
21 #include "ui/events/keycodes/keyboard_codes_posix.h"
22 #include "ui/events/platform/platform_event_source.h"
23 #include "xwalk/application/common/manifest_handlers/tizen_setting_handler.h"
26 #include "xwalk/application/common/application_manifest_constants.h"
27 #include "xwalk/application/common/manifest_handlers/csp_handler.h"
28 #include "xwalk/application/common/manifest_handlers/navigation_handler.h"
32 namespace widget_keys = application_widget_keys;
34 namespace application {
37 const char kAsterisk[] = "*";
39 const char kDirectiveValueSelf[] = "'self'";
40 const char kDirectiveValueNone[] = "'none'";
42 const char kDirectiveNameDefault[] = "default-src";
43 const char kDirectiveNameScript[] = "script-src";
44 const char kDirectiveNameStyle[] = "style-src";
45 const char kDirectiveNameObject[] = "object-src";
47 CSPInfo* GetDefaultCSPInfo() {
48 static CSPInfo default_csp_info;
49 if (default_csp_info.GetDirectives().empty()) {
50 std::vector<std::string> directive_all;
51 std::vector<std::string> directive_self;
52 std::vector<std::string> directive_none;
53 directive_all.push_back(kAsterisk);
54 directive_self.push_back(kDirectiveValueSelf);
55 directive_none.push_back(kDirectiveValueNone);
57 default_csp_info.SetDirective(kDirectiveNameDefault, directive_all);
58 default_csp_info.SetDirective(kDirectiveNameScript, directive_self);
59 default_csp_info.SetDirective(kDirectiveNameStyle, directive_self);
60 default_csp_info.SetDirective(kDirectiveNameObject, directive_none);
63 return (new CSPInfo(default_csp_info));
69 ApplicationTizen::ApplicationTizen(
70 scoped_refptr<ApplicationData> data,
71 RuntimeContext* runtime_context,
72 Application::Observer* observer)
73 : Application(data, runtime_context, observer) {
74 #if defined(USE_OZONE)
75 ui::PlatformEventSource::GetInstance()->AddPlatformEventObserver(this);
79 ApplicationTizen::~ApplicationTizen() {
80 #if defined(USE_OZONE)
81 ui::PlatformEventSource::GetInstance()->RemovePlatformEventObserver(this);
85 void ApplicationTizen::Hide() {
86 DCHECK(runtimes_.size());
87 std::set<Runtime*>::iterator it = runtimes_.begin();
88 for (; it != runtimes_.end(); ++it) {
90 (*it)->window()->Hide();
94 void ApplicationTizen::InitSecurityPolicy() {
95 // On Tizen, CSP mode has higher priority, and WARP will be disabled
96 // if the application is under CSP mode.
97 if (!data_->HasCSPDefined()) {
98 Application::InitSecurityPolicy();
102 if (data_->GetPackageType() != Package::WGT)
106 static_cast<CSPInfo*>(data_->GetManifestData(widget_keys::kCSPKey));
107 if (!csp_info || csp_info->GetDirectives().empty())
108 data_->SetManifestData(widget_keys::kCSPKey, GetDefaultCSPInfo());
110 // Always enable security mode when under CSP mode.
111 security_mode_enabled_ = true;
112 NavigationInfo* info = static_cast<NavigationInfo*>(
113 data_->GetManifestData(widget_keys::kAllowNavigationKey));
115 const std::vector<std::string>& allowed_list = info->GetAllowedDomains();
116 for (std::vector<std::string>::const_iterator it = allowed_list.begin();
117 it != allowed_list.end(); ++it) {
118 // If the policy is "*", it represents that any external link is allowed
120 if ((*it) == kAsterisk) {
121 security_mode_enabled_ = false;
125 // If the policy start with "*.", like this: *.domain,
126 // means that can access to all subdomains for 'domain',
127 // otherwise, the host of request url should exactly the same
129 bool subdomains = ((*it).find("*.") == 0);
130 std::string host = subdomains ? (*it).substr(2) : (*it);
131 AddSecurityPolicy(GURL("http://" + host), subdomains);
132 AddSecurityPolicy(GURL("https://" + host), subdomains);
135 DCHECK(render_process_host_);
136 render_process_host_->Send(
137 new ViewMsg_EnableSecurityMode(
138 ApplicationData::GetBaseURLFromApplicationId(id()),
139 SecurityPolicy::CSP));
142 #if defined(USE_OZONE)
143 void ApplicationTizen::WillProcessEvent(const ui::PlatformEvent& event) {}
145 void ApplicationTizen::DidProcessEvent(
146 const ui::PlatformEvent& event) {
147 ui::Event* ui_event = static_cast<ui::Event*>(event);
148 if (!ui_event->IsKeyEvent() || ui_event->type() != ui::ET_KEY_PRESSED)
151 ui::KeyEvent* key_event = static_cast<ui::KeyEvent*>(ui_event);
153 // FIXME: Most Wayland devices don't have similar hardware button for 'back'
154 // and 'memu' as Tizen Mobile, even that hardare buttons could be different
155 // across different kinds of Wayland platforms.
156 // Here use external keyboard button 'Backspace' & 'HOME' to emulate 'back'
157 // and 'menu' key. Should change this if there is customized key binding.
158 if (key_event->key_code() != ui::VKEY_BACK &&
159 key_event->key_code() != ui::VKEY_HOME)
162 TizenSettingInfo* info = static_cast<TizenSettingInfo*>(
163 data()->GetManifestData(widget_keys::kTizenSettingKey));
164 if (info && !info->hwkey_enabled())
167 for (std::set<xwalk::Runtime*>::iterator it = runtimes_.begin();
168 it != runtimes_.end(); ++it) {
169 (*it)->web_contents()->GetRenderViewHost()->Send(new ViewMsg_HWKeyPressed(
170 (*it)->web_contents()->GetRoutingID(), key_event->key_code()));
175 } // namespace application