942c7027d4f3a10f2bff240d2c9c45761530fa5d
[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 "base/message_loop/message_pump_ozone.h"
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 "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/navigation_handler.h"
28
29 namespace xwalk {
30
31 namespace widget_keys = application_widget_keys;
32
33 namespace application {
34
35 namespace {
36 const char kAsterisk[] = "*";
37 }  // namespace
38
39
40 ApplicationTizen::ApplicationTizen(
41     scoped_refptr<ApplicationData> data,
42     RuntimeContext* runtime_context,
43     Application::Observer* observer)
44     : Application(data, runtime_context, observer) {
45 #if defined(USE_OZONE)
46   base::MessagePumpOzone::Current()->AddObserver(this);
47 #endif
48 }
49
50 ApplicationTizen::~ApplicationTizen() {
51 #if defined(USE_OZONE)
52   base::MessagePumpOzone::Current()->RemoveObserver(this);
53 #endif
54 }
55
56 void ApplicationTizen::Hide() {
57   DCHECK(runtimes_.size());
58   std::set<Runtime*>::iterator it = runtimes_.begin();
59   for (; it != runtimes_.end(); ++it) {
60     if ((*it)->window())
61       (*it)->window()->Hide();
62   }
63 }
64
65 void ApplicationTizen::InitSecurityPolicy() {
66   // On Tizen, CSP mode has higher priority, and WARP will be disabled
67   // if the application is under CSP mode.
68   if (!application_data_->HasCSPDefined()) {
69     Application::InitSecurityPolicy();
70     return;
71   }
72
73   if (application_data_->GetPackageType() != Manifest::TYPE_WGT)
74     return;
75
76   // Always enable security mode when under CSP mode.
77   is_security_mode_ = true;
78   NavigationInfo* info = static_cast<NavigationInfo*>(
79       application_data_->GetManifestData(widget_keys::kAllowNavigationKey));
80   if (info) {
81     const std::vector<std::string>& allowed_list = info->GetAllowedDomains();
82     for (std::vector<std::string>::const_iterator it = allowed_list.begin();
83          it != allowed_list.end(); ++it) {
84       // If the policy is "*", it represents that any external link is allowed
85       // to navigate to.
86       if ((*it) == kAsterisk) {
87         is_security_mode_ = false;
88         return;
89       }
90
91       // If the policy start with "*.", like this: *.domain,
92       // means that can access to all subdomains for 'domain',
93       // otherwise, the host of request url should exactly the same
94       // as policy.
95       bool subdomains = ((*it).find("*.") == 0);
96       std::string host = subdomains ? (*it).substr(2) : (*it);
97       AddSecurityPolicy(GURL("http://" + host), subdomains);
98       AddSecurityPolicy(GURL("https://" + host), subdomains);
99     }
100   }
101   main_runtime_->GetRenderProcessHost()->Send(
102       new ViewMsg_EnableSecurityMode(
103           ApplicationData::GetBaseURLFromApplicationId(id()),
104           SecurityPolicy::CSP));
105 }
106
107 #if defined(USE_OZONE)
108 base::EventStatus ApplicationTizen::WillProcessEvent(
109     const base::NativeEvent& event) {
110   return base::EVENT_CONTINUE;
111 }
112
113 void ApplicationTizen::DidProcessEvent(
114     const base::NativeEvent& event) {
115   ui::Event* ui_event = static_cast<ui::Event*>(event);
116   if (!ui_event->IsKeyEvent() || ui_event->type() != ui::ET_KEY_PRESSED)
117     return;
118
119   ui::KeyEvent* key_event = static_cast<ui::KeyEvent*>(ui_event);
120
121   // FIXME: Most Wayland devices don't have similar hardware button for 'back'
122   // and 'memu' as Tizen Mobile, even that hardare buttons could be different
123   // across different kinds of Wayland platforms.
124   // Here use external keyboard button 'Backspace' & 'HOME' to emulate 'back'
125   // and 'menu' key. Should change this if there is customized key binding.
126   if (key_event->key_code() != ui::VKEY_BACK &&
127       key_event->key_code() != ui::VKEY_HOME)
128     return;
129
130   TizenSettingInfo* info = static_cast<TizenSettingInfo*>(
131       data()->GetManifestData(widget_keys::kTizenSettingKey));
132   if (info && !info->hwkey_enabled())
133     return;
134
135   for (std::set<xwalk::Runtime*>::iterator it = runtimes_.begin();
136       it != runtimes_.end(); ++it) {
137     (*it)->web_contents()->GetRenderViewHost()->Send(new ViewMsg_HWKeyPressed(
138         (*it)->web_contents()->GetRoutingID(), key_event->key_code()));
139   }
140 }
141 #endif
142
143 }  // namespace application
144 }  // namespace xwalk