Upstream version 8.37.180.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 #include "content/browser/screen_orientation/screen_orientation_provider.h"
15
16 #include "xwalk/runtime/browser/ui/native_app_window.h"
17 #include "xwalk/runtime/browser/ui/native_app_window_tizen.h"
18 #include "xwalk/runtime/common/xwalk_common_messages.h"
19
20 #if defined(USE_OZONE)
21 #include "content/public/browser/render_view_host.h"
22 #include "ui/events/event.h"
23 #include "ui/events/event_constants.h"
24 #include "ui/events/keycodes/keyboard_codes_posix.h"
25 #include "ui/events/platform/platform_event_source.h"
26 #include "xwalk/application/common/manifest_handlers/tizen_setting_handler.h"
27 #endif
28
29 #include "xwalk/application/common/application_manifest_constants.h"
30 #include "xwalk/application/common/manifest_handlers/tizen_splash_screen_handler.h"
31
32 namespace xwalk {
33
34 namespace widget_keys = application_widget_keys;
35
36 namespace application {
37
38 class ScreenOrientationProviderTizen :
39     public content::ScreenOrientationProvider {
40  public:
41   explicit ScreenOrientationProviderTizen(const base::WeakPtr<Application>& app)
42       : app_(app) {
43   }
44
45   virtual void LockOrientation(
46       blink::WebScreenOrientationLockType lock) OVERRIDE {
47     if (!app_)
48       return;
49     const std::set<Runtime*>& runtimes = app_->runtimes();
50     DCHECK(!runtimes.empty());
51     // FIXME: Probably need better alignment with
52     // https://w3c.github.io/screen-orientation/#screen-orientation-lock-lifetime
53     std::set<Runtime*>::iterator it = runtimes.begin();
54     for (; it != runtimes.end(); ++it) {
55       NativeAppWindow* window = (*it)->window();
56       if (window && window->IsActive()) {
57         ToNativeAppWindowTizen(window)->LockOrientation(lock);
58         break;
59       }
60     }
61   }
62
63   virtual void UnlockOrientation() OVERRIDE {
64     LockOrientation(blink::WebScreenOrientationLockDefault);
65   }
66
67  private:
68   base::WeakPtr<Application> app_;
69 };
70
71 ApplicationTizen::ApplicationTizen(
72     scoped_refptr<ApplicationData> data,
73     RuntimeContext* runtime_context,
74     Application::Observer* observer)
75     : Application(data, runtime_context, observer) {
76 #if defined(USE_OZONE)
77   ui::PlatformEventSource::GetInstance()->AddPlatformEventObserver(this);
78 #endif
79 }
80
81 ApplicationTizen::~ApplicationTizen() {
82 #if defined(USE_OZONE)
83   ui::PlatformEventSource::GetInstance()->RemovePlatformEventObserver(this);
84 #endif
85 }
86
87 void ApplicationTizen::Hide() {
88   DCHECK(!runtimes_.empty());
89   std::set<Runtime*>::iterator it = runtimes_.begin();
90   for (; it != runtimes_.end(); ++it) {
91     if ((*it)->window())
92       (*it)->window()->Hide();
93   }
94 }
95
96 bool ApplicationTizen::Launch(const LaunchParams& launch_params) {
97   if (Application::Launch(launch_params)) {
98     DCHECK(web_contents_);
99     web_contents_->GetScreenOrientationDispatcherHost()->
100         SetProviderForTests(new ScreenOrientationProviderTizen(GetWeakPtr()));
101     return true;
102   }
103   return false;
104 }
105
106 base::FilePath ApplicationTizen::GetSplashScreenPath() {
107   if (TizenSplashScreenInfo* ss_info = static_cast<TizenSplashScreenInfo*>(
108       data()->GetManifestData(widget_keys::kTizenSplashScreenKey))) {
109     return data()->Path().Append(FILE_PATH_LITERAL(ss_info->src()));
110   }
111   return base::FilePath();
112 }
113
114 #if defined(USE_OZONE)
115 void ApplicationTizen::WillProcessEvent(const ui::PlatformEvent& event) {}
116
117 void ApplicationTizen::DidProcessEvent(
118     const ui::PlatformEvent& event) {
119   ui::Event* ui_event = static_cast<ui::Event*>(event);
120   if (!ui_event->IsKeyEvent() || ui_event->type() != ui::ET_KEY_PRESSED)
121     return;
122
123   ui::KeyEvent* key_event = static_cast<ui::KeyEvent*>(ui_event);
124
125   // FIXME: Most Wayland devices don't have similar hardware button for 'back'
126   // and 'memu' as Tizen Mobile, even that hardare buttons could be different
127   // across different kinds of Wayland platforms.
128   // Here use external keyboard button 'Backspace' & 'HOME' to emulate 'back'
129   // and 'menu' key. Should change this if there is customized key binding.
130   if (key_event->key_code() != ui::VKEY_BACK &&
131       key_event->key_code() != ui::VKEY_HOME)
132     return;
133
134   TizenSettingInfo* info = static_cast<TizenSettingInfo*>(
135       data()->GetManifestData(widget_keys::kTizenSettingKey));
136   if (info && !info->hwkey_enabled())
137     return;
138
139   for (std::set<xwalk::Runtime*>::iterator it = runtimes_.begin();
140       it != runtimes_.end(); ++it) {
141     (*it)->web_contents()->GetRenderViewHost()->Send(new ViewMsg_HWKeyPressed(
142         (*it)->web_contents()->GetRoutingID(), key_event->key_code()));
143   }
144 }
145 #endif
146
147 }  // namespace application
148 }  // namespace xwalk