Upstream version 7.36.152.0
[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_tizen.h"
16 #include "xwalk/runtime/common/xwalk_common_messages.h"
17
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"
25 #endif
26
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"
30
31 namespace xwalk {
32
33 namespace widget_keys = application_widget_keys;
34
35 namespace application {
36
37 namespace {
38 const char kAsterisk[] = "*";
39
40 const char kDirectiveValueSelf[] = "'self'";
41 const char kDirectiveValueNone[] = "'none'";
42
43 const char kDirectiveNameDefault[] = "default-src";
44 const char kDirectiveNameScript[] = "script-src";
45 const char kDirectiveNameStyle[] = "style-src";
46 const char kDirectiveNameObject[] = "object-src";
47
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);
57
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);
62   }
63
64   return (new CSPInfo(default_csp_info));
65 }
66
67 }  // namespace
68
69
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);
77 #endif
78 }
79
80 ApplicationTizen::~ApplicationTizen() {
81 #if defined(USE_OZONE)
82   ui::PlatformEventSource::GetInstance()->RemovePlatformEventObserver(this);
83 #endif
84 }
85
86 void ApplicationTizen::Hide() {
87   DCHECK(!runtimes_.empty());
88   std::set<Runtime*>::iterator it = runtimes_.begin();
89   for (; it != runtimes_.end(); ++it) {
90     if ((*it)->window())
91       (*it)->window()->Hide();
92   }
93 }
94
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);
100     return true;
101   }
102   return false;
103 }
104
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();
110     return;
111   }
112
113   if (data_->GetPackageType() != Package::WGT)
114     return;
115
116   CSPInfo* csp_info =
117       static_cast<CSPInfo*>(data_->GetManifestData(widget_keys::kCSPKey));
118   if (!csp_info || csp_info->GetDirectives().empty())
119     data_->SetManifestData(widget_keys::kCSPKey, GetDefaultCSPInfo());
120
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));
125   if (info) {
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
130       // to navigate to.
131       if ((*it) == kAsterisk) {
132         security_mode_enabled_ = false;
133         return;
134       }
135
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
139       // as policy.
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);
144     }
145   }
146   DCHECK(render_process_host_);
147   render_process_host_->Send(
148       new ViewMsg_EnableSecurityMode(
149           ApplicationData::GetBaseURLFromApplicationId(id()),
150           SecurityPolicy::CSP));
151 }
152
153 #if defined(USE_OZONE)
154 void ApplicationTizen::WillProcessEvent(const ui::PlatformEvent& event) {}
155
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)
160     return;
161
162   ui::KeyEvent* key_event = static_cast<ui::KeyEvent*>(ui_event);
163
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)
171     return;
172
173   TizenSettingInfo* info = static_cast<TizenSettingInfo*>(
174       data()->GetManifestData(widget_keys::kTizenSettingKey));
175   if (info && !info->hwkey_enabled())
176     return;
177
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()));
182   }
183 }
184 #endif
185
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);
196       break;
197     }
198   }
199 }
200
201 void ApplicationTizen::UnlockOrientation() {
202   LockOrientation(blink::WebScreenOrientationLockDefault);
203 }
204
205 }  // namespace application
206 }  // namespace xwalk