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/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" 28 #include "url/gurl.h" 29 30 namespace mojo { 31 namespace examples { 32 33 class Keyboard; 34 35 class KeyboardServiceImpl : public InterfaceImpl<KeyboardService> { 36 public: 37 explicit KeyboardServiceImpl(Keyboard* keyboard); 38 virtual ~KeyboardServiceImpl() {} 39 40 // KeyboardService: 41 virtual void SetTarget(uint32_t view_id) OVERRIDE; 42 43 private: 44 Keyboard* keyboard_; 45 46 DISALLOW_COPY_AND_ASSIGN(KeyboardServiceImpl); 47 }; 48 49 class Keyboard : public ApplicationDelegate, 50 public ViewManagerDelegate, 51 public KeyboardDelegate { 52 public: 53 Keyboard() 54 : keyboard_service_factory_(this), 55 view_manager_(NULL), 56 keyboard_service_(NULL), 57 target_(0) {} 58 59 virtual ~Keyboard() { 60 } 61 62 void set_target(Id id) { target_ = id; } 63 64 void set_keyboard_service(KeyboardServiceImpl* keyboard) { 65 keyboard_service_ = keyboard; 66 } 67 68 private: 69 // Overridden from ApplicationDelegate: 70 virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE { 71 view_manager_client_factory_.reset( 72 new ViewManagerClientFactory(app->shell(), this)); 73 } 74 75 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) 76 MOJO_OVERRIDE { 77 views_init_.reset(new ViewsInit); 78 connection->AddService(view_manager_client_factory_.get()); 79 connection->AddService(&keyboard_service_factory_); 80 return true; 81 } 82 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); 87 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()); 94 widget->Init(params); 95 widget->Show(); 96 } 97 98 // ViewManagerDelegate: 99 virtual void OnEmbed(ViewManager* view_manager, 100 View* root, 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; 105 CreateWidget(root); 106 } 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(); 112 } 113 114 // KeyboardDelegate: 115 virtual void OnKeyPressed(int key_code, int event_flags) OVERRIDE { 116 if (!target_) 117 return; 118 keyboard_service_->client()->OnKeyboardEvent(target_, key_code, 119 event_flags); 120 } 121 122 InterfaceFactoryImplWithContext<KeyboardServiceImpl, Keyboard> 123 keyboard_service_factory_; 124 125 scoped_ptr<ViewsInit> views_init_; 126 127 ViewManager* view_manager_; 128 scoped_ptr<ViewManagerClientFactory> view_manager_client_factory_; 129 130 KeyboardServiceImpl* keyboard_service_; 131 132 Id target_; 133 134 DISALLOW_COPY_AND_ASSIGN(Keyboard); 135 }; 136 137 KeyboardServiceImpl::KeyboardServiceImpl(Keyboard* keyboard) 138 : keyboard_(keyboard) { 139 keyboard_->set_keyboard_service(this); 140 } 141 142 void KeyboardServiceImpl::SetTarget(uint32_t view_id) { 143 keyboard_->set_target(view_id); 144 } 145 146 } // namespace examples 147 } // namespace mojo 148 149 MojoResult MojoMain(MojoHandle shell_handle) { 150 mojo::ApplicationRunnerChromium runner(new mojo::examples::Keyboard); 151 return runner.Run(shell_handle); 152 } 153