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.
5 #include "base/basictypes.h"
6 #include "base/strings/string_util.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "mojo/application/application_runner_chromium.h"
9 #include "mojo/examples/keyboard/keyboard.mojom.h"
10 #include "mojo/examples/keyboard/keyboard_delegate.h"
11 #include "mojo/examples/keyboard/keyboard_view.h"
12 #include "mojo/public/c/system/main.h"
13 #include "mojo/public/cpp/application/application_connection.h"
14 #include "mojo/public/cpp/application/application_delegate.h"
15 #include "mojo/public/cpp/application/application_impl.h"
16 #include "mojo/public/cpp/application/interface_factory_impl.h"
17 #include "mojo/services/public/cpp/view_manager/view.h"
18 #include "mojo/services/public/cpp/view_manager/view_manager.h"
19 #include "mojo/services/public/cpp/view_manager/view_manager_client_factory.h"
20 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h"
21 #include "mojo/services/public/interfaces/navigation/navigation.mojom.h"
22 #include "mojo/views/native_widget_view_manager.h"
23 #include "mojo/views/views_init.h"
24 #include "ui/events/event.h"
25 #include "ui/views/layout/fill_layout.h"
26 #include "ui/views/widget/widget.h"
27 #include "ui/views/widget/widget_delegate.h"
35 class KeyboardServiceImpl : public InterfaceImpl<KeyboardService> {
37 explicit KeyboardServiceImpl(Keyboard* keyboard);
38 virtual ~KeyboardServiceImpl() {}
41 virtual void SetTarget(uint32_t view_id) OVERRIDE;
46 DISALLOW_COPY_AND_ASSIGN(KeyboardServiceImpl);
49 class Keyboard : public ApplicationDelegate,
50 public ViewManagerDelegate,
51 public KeyboardDelegate {
54 : keyboard_service_factory_(this),
56 keyboard_service_(NULL),
62 void set_target(Id id) { target_ = id; }
64 void set_keyboard_service(KeyboardServiceImpl* keyboard) {
65 keyboard_service_ = keyboard;
69 // Overridden from ApplicationDelegate:
70 virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE {
71 view_manager_client_factory_.reset(
72 new ViewManagerClientFactory(app->shell(), this));
75 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection)
77 views_init_.reset(new ViewsInit);
78 connection->AddService(view_manager_client_factory_.get());
79 connection->AddService(&keyboard_service_factory_);
83 void CreateWidget(View* view) {
84 views::WidgetDelegateView* widget_delegate = new views::WidgetDelegateView;
85 widget_delegate->GetContentsView()->AddChildView(new KeyboardView(this));
86 widget_delegate->GetContentsView()->SetLayoutManager(new views::FillLayout);
88 views::Widget* widget = new views::Widget;
89 views::Widget::InitParams params(
90 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
91 params.native_widget = new NativeWidgetViewManager(widget, view);
92 params.delegate = widget_delegate;
93 params.bounds = gfx::Rect(view->bounds().width(), view->bounds().height());
98 // ViewManagerDelegate:
99 virtual void OnEmbed(ViewManager* view_manager,
101 ServiceProviderImpl* exported_services,
102 scoped_ptr<ServiceProvider> imported_services) OVERRIDE {
103 // TODO: deal with OnEmbed() being invoked multiple times.
104 view_manager_ = view_manager;
107 virtual void OnViewManagerDisconnected(
108 ViewManager* view_manager) OVERRIDE {
109 DCHECK_EQ(view_manager_, view_manager);
110 view_manager_ = NULL;
111 base::MessageLoop::current()->Quit();
115 virtual void OnKeyPressed(int key_code, int event_flags) OVERRIDE {
118 keyboard_service_->client()->OnKeyboardEvent(target_, key_code,
122 InterfaceFactoryImplWithContext<KeyboardServiceImpl, Keyboard>
123 keyboard_service_factory_;
125 scoped_ptr<ViewsInit> views_init_;
127 ViewManager* view_manager_;
128 scoped_ptr<ViewManagerClientFactory> view_manager_client_factory_;
130 KeyboardServiceImpl* keyboard_service_;
134 DISALLOW_COPY_AND_ASSIGN(Keyboard);
137 KeyboardServiceImpl::KeyboardServiceImpl(Keyboard* keyboard)
138 : keyboard_(keyboard) {
139 keyboard_->set_keyboard_service(this);
142 void KeyboardServiceImpl::SetTarget(uint32_t view_id) {
143 keyboard_->set_target(view_id);
146 } // namespace examples
149 MojoResult MojoMain(MojoHandle shell_handle) {
150 mojo::ApplicationRunnerChromium runner(new mojo::examples::Keyboard);
151 return runner.Run(shell_handle);