Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / mojo / examples / keyboard / keyboard.cc
1 // Copyright 2014 The Chromium Authors. 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 "base/basictypes.h"
6 #include "base/strings/string_util.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "mojo/examples/keyboard/keyboard.mojom.h"
9 #include "mojo/examples/keyboard/keyboard_delegate.h"
10 #include "mojo/examples/keyboard/keyboard_view.h"
11 #include "mojo/public/cpp/application/application_connection.h"
12 #include "mojo/public/cpp/application/application_delegate.h"
13 #include "mojo/public/cpp/application/interface_factory_impl.h"
14 #include "mojo/services/public/cpp/view_manager/view.h"
15 #include "mojo/services/public/cpp/view_manager/view_manager.h"
16 #include "mojo/services/public/cpp/view_manager/view_manager_client_factory.h"
17 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h"
18 #include "mojo/services/public/interfaces/navigation/navigation.mojom.h"
19 #include "mojo/views/native_widget_view_manager.h"
20 #include "mojo/views/views_init.h"
21 #include "ui/events/event.h"
22 #include "ui/views/layout/fill_layout.h"
23 #include "ui/views/widget/widget.h"
24 #include "ui/views/widget/widget_delegate.h"
25 #include "url/gurl.h"
26
27 namespace mojo {
28 namespace examples {
29
30 class Keyboard;
31
32 class KeyboardServiceImpl : public InterfaceImpl<KeyboardService> {
33  public:
34   explicit KeyboardServiceImpl(Keyboard* keyboard);
35   virtual ~KeyboardServiceImpl() {}
36
37   // KeyboardService:
38   virtual void SetTarget(uint32_t view_id) OVERRIDE;
39
40  private:
41   Keyboard* keyboard_;
42
43   DISALLOW_COPY_AND_ASSIGN(KeyboardServiceImpl);
44 };
45
46 class Keyboard : public ApplicationDelegate,
47                  public ViewManagerDelegate,
48                  public KeyboardDelegate {
49  public:
50   Keyboard()
51       : keyboard_service_factory_(this),
52         view_manager_(NULL),
53         view_manager_client_factory_(this),
54         keyboard_service_(NULL),
55         target_(0) {}
56
57   virtual ~Keyboard() {
58   }
59
60   void set_target(Id id) { target_ = id; }
61
62   void set_keyboard_service(KeyboardServiceImpl* keyboard) {
63     keyboard_service_ = keyboard;
64   }
65
66  private:
67   // Overridden from ApplicationDelegate:
68   virtual bool ConfigureIncomingConnection(ApplicationConnection* connection)
69       MOJO_OVERRIDE {
70     views_init_.reset(new ViewsInit);
71     connection->AddService(&view_manager_client_factory_);
72     connection->AddService(&keyboard_service_factory_);
73     return true;
74   }
75
76   void CreateWidget(View* view) {
77     views::WidgetDelegateView* widget_delegate = new views::WidgetDelegateView;
78     widget_delegate->GetContentsView()->AddChildView(new KeyboardView(this));
79     widget_delegate->GetContentsView()->SetLayoutManager(new views::FillLayout);
80
81     views::Widget* widget = new views::Widget;
82     views::Widget::InitParams params(
83         views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
84     params.native_widget = new NativeWidgetViewManager(widget, view);
85     params.delegate = widget_delegate;
86     params.bounds = gfx::Rect(view->bounds().width(), view->bounds().height());
87     widget->Init(params);
88     widget->Show();
89   }
90
91   // ViewManagerDelegate:
92   virtual void OnEmbed(ViewManager* view_manager,
93                        View* root,
94                        ServiceProviderImpl* exported_services,
95                        scoped_ptr<ServiceProvider> imported_services) OVERRIDE {
96     // TODO: deal with OnEmbed() being invoked multiple times.
97     view_manager_ = view_manager;
98     CreateWidget(root);
99   }
100   virtual void OnViewManagerDisconnected(
101       ViewManager* view_manager) OVERRIDE {
102     DCHECK_EQ(view_manager_, view_manager);
103     view_manager_ = NULL;
104     base::MessageLoop::current()->Quit();
105   }
106
107   // KeyboardDelegate:
108   virtual void OnKeyPressed(int key_code, int event_flags) OVERRIDE {
109     if (!target_)
110       return;
111     keyboard_service_->client()->OnKeyboardEvent(target_, key_code,
112                                                  event_flags);
113   }
114
115   InterfaceFactoryImplWithContext<KeyboardServiceImpl, Keyboard>
116       keyboard_service_factory_;
117
118   scoped_ptr<ViewsInit> views_init_;
119
120   ViewManager* view_manager_;
121   ViewManagerClientFactory view_manager_client_factory_;
122
123   KeyboardServiceImpl* keyboard_service_;
124
125   Id target_;
126
127   DISALLOW_COPY_AND_ASSIGN(Keyboard);
128 };
129
130 KeyboardServiceImpl::KeyboardServiceImpl(Keyboard* keyboard)
131     : keyboard_(keyboard) {
132   keyboard_->set_keyboard_service(this);
133 }
134
135 void KeyboardServiceImpl::SetTarget(uint32_t view_id) {
136   keyboard_->set_target(view_id);
137 }
138
139 }  // namespace examples
140
141 // static
142 ApplicationDelegate* ApplicationDelegate::Create() {
143   return new examples::Keyboard;
144 }
145
146 }  // namespace mojo