Upstream version 7.36.149.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
14 #include "xwalk/runtime/browser/ui/native_app_window.h"
15 #include "xwalk/runtime/common/xwalk_common_messages.h"
16
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"
24 #endif
25
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"
29
30 namespace xwalk {
31
32 namespace widget_keys = application_widget_keys;
33
34 namespace application {
35
36 namespace {
37 const char kAsterisk[] = "*";
38
39 const char kDirectiveValueSelf[] = "'self'";
40 const char kDirectiveValueNone[] = "'none'";
41
42 const char kDirectiveNameDefault[] = "default-src";
43 const char kDirectiveNameScript[] = "script-src";
44 const char kDirectiveNameStyle[] = "style-src";
45 const char kDirectiveNameObject[] = "object-src";
46
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);
56
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);
61   }
62
63   return (new CSPInfo(default_csp_info));
64 }
65
66 }  // namespace
67
68
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);
76 #endif
77 }
78
79 ApplicationTizen::~ApplicationTizen() {
80 #if defined(USE_OZONE)
81   ui::PlatformEventSource::GetInstance()->RemovePlatformEventObserver(this);
82 #endif
83 }
84
85 void ApplicationTizen::Hide() {
86   DCHECK(runtimes_.size());
87   std::set<Runtime*>::iterator it = runtimes_.begin();
88   for (; it != runtimes_.end(); ++it) {
89     if ((*it)->window())
90       (*it)->window()->Hide();
91   }
92 }
93
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();
99     return;
100   }
101
102   if (data_->GetPackageType() != Package::WGT)
103     return;
104
105   CSPInfo* csp_info =
106       static_cast<CSPInfo*>(data_->GetManifestData(widget_keys::kCSPKey));
107   if (!csp_info || csp_info->GetDirectives().empty())
108     data_->SetManifestData(widget_keys::kCSPKey, GetDefaultCSPInfo());
109
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));
114   if (info) {
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
119       // to navigate to.
120       if ((*it) == kAsterisk) {
121         security_mode_enabled_ = false;
122         return;
123       }
124
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
128       // as policy.
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);
133     }
134   }
135   DCHECK(render_process_host_);
136   render_process_host_->Send(
137       new ViewMsg_EnableSecurityMode(
138           ApplicationData::GetBaseURLFromApplicationId(id()),
139           SecurityPolicy::CSP));
140 }
141
142 #if defined(USE_OZONE)
143 void ApplicationTizen::WillProcessEvent(const ui::PlatformEvent& event) {}
144
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)
149     return;
150
151   ui::KeyEvent* key_event = static_cast<ui::KeyEvent*>(ui_event);
152
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)
160     return;
161
162   TizenSettingInfo* info = static_cast<TizenSettingInfo*>(
163       data()->GetManifestData(widget_keys::kTizenSettingKey));
164   if (info && !info->hwkey_enabled())
165     return;
166
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()));
171   }
172 }
173 #endif
174
175 }  // namespace application
176 }  // namespace xwalk