| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stdio.h> | 5 #include <stdio.h> |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/scoped_vector.h" | 8 #include "base/memory/scoped_vector.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "mojo/public/bindings/allocation_scope.h" | 10 #include "mojo/public/bindings/allocation_scope.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 #endif | 26 #endif |
| 27 #define VIEW_MANAGER_EXPORT __declspec(dllexport) | 27 #define VIEW_MANAGER_EXPORT __declspec(dllexport) |
| 28 #else | 28 #else |
| 29 #define CDECL | 29 #define CDECL |
| 30 #define VIEW_MANAGER_EXPORT __attribute__((visibility("default"))) | 30 #define VIEW_MANAGER_EXPORT __attribute__((visibility("default"))) |
| 31 #endif | 31 #endif |
| 32 | 32 |
| 33 namespace mojo { | 33 namespace mojo { |
| 34 namespace examples { | 34 namespace examples { |
| 35 | 35 |
| 36 // Convenience interface to connect to a service. Necessary because all code for | 36 class ViewImpl : public View { |
| 37 // this app lives in one .cc file. | |
| 38 class Connector { | |
| 39 public: | 37 public: |
| 40 virtual void Connect(const String& url, | 38 explicit ViewImpl(ScopedMessagePipeHandle handle) |
| 41 ScopedMessagePipeHandle client_handle) = 0; | 39 : id_(-1), |
| 40 client_(handle.Pass()) {} |
| 41 virtual ~ViewImpl() {} |
| 42 | 42 |
| 43 protected: | 43 private: |
| 44 virtual ~Connector() {} | 44 // Overridden from View: |
| 45 virtual void SetId(int view_id) MOJO_OVERRIDE { |
| 46 id_ = view_id; |
| 47 } |
| 48 virtual void GetId() MOJO_OVERRIDE { |
| 49 client_->OnGotId(id_); |
| 50 } |
| 51 |
| 52 int id_; |
| 53 RemotePtr<ViewClient> client_; |
| 54 |
| 55 DISALLOW_COPY_AND_ASSIGN(ViewImpl); |
| 45 }; | 56 }; |
| 46 | 57 |
| 47 class NativeViewportClientImpl : public NativeViewportClient, | 58 class ViewManagerImpl : public ViewManager, |
| 48 public LauncherClient { | 59 public ShellClient, |
| 60 public NativeViewportClient, |
| 61 public LauncherClient { |
| 49 public: | 62 public: |
| 50 explicit NativeViewportClientImpl(Connector* connector) | 63 explicit ViewManagerImpl(ScopedMessagePipeHandle shell_handle) |
| 51 : connector_(connector) { | 64 : shell_(shell_handle.Pass(), this) { |
| 52 AllocationScope scope; | 65 InitNativeViewport(); |
| 53 ScopedMessagePipeHandle client_handle, native_viewport_handle; | |
| 54 CreateMessagePipe(&client_handle, &native_viewport_handle); | |
| 55 connector_->Connect("mojo:mojo_native_viewport_service", | |
| 56 client_handle.Pass()); | |
| 57 native_viewport_.reset(native_viewport_handle.Pass(), this); | |
| 58 | |
| 59 native_viewport_->Create(gfx::Rect(50, 50, 800, 600)); | |
| 60 native_viewport_->Show(); | |
| 61 } | 66 } |
| 62 virtual ~NativeViewportClientImpl() {} | |
| 63 | 67 |
| 64 private: | 68 private: |
| 69 // Overridden from ViewManager: |
| 70 virtual void CreateView() MOJO_OVERRIDE { |
| 71 ScopedMessagePipeHandle server_handle, client_handle; |
| 72 CreateMessagePipe(&server_handle, &client_handle); |
| 73 views_.push_back(new ViewImpl(server_handle.Pass())); |
| 74 client_->OnViewCreated(client_handle.Pass()); |
| 75 } |
| 76 |
| 77 // Overridden from ShellClient: |
| 78 virtual void AcceptConnection(ScopedMessagePipeHandle handle) MOJO_OVERRIDE { |
| 79 client_.reset(handle.Pass(), this); |
| 80 } |
| 81 |
| 65 // Overridden from NativeViewportClient: | 82 // Overridden from NativeViewportClient: |
| 66 virtual void OnCreated() OVERRIDE { | 83 virtual void OnCreated() OVERRIDE { |
| 67 } | 84 } |
| 68 virtual void OnDestroyed() OVERRIDE { | 85 virtual void OnDestroyed() OVERRIDE { |
| 69 base::MessageLoop::current()->Quit(); | 86 base::MessageLoop::current()->Quit(); |
| 70 } | 87 } |
| 71 virtual void OnBoundsChanged(const Rect& bounds) OVERRIDE { | 88 virtual void OnBoundsChanged(const Rect& bounds) OVERRIDE { |
| 72 // TODO(beng): | 89 // TODO(beng): |
| 73 } | 90 } |
| 74 virtual void OnEvent(const Event& event) OVERRIDE { | 91 virtual void OnEvent(const Event& event) OVERRIDE { |
| 75 if (!event.location().is_null()) | 92 if (!event.location().is_null()) |
| 76 native_viewport_->AckEvent(event); | 93 native_viewport_->AckEvent(event); |
| 77 if (event.action() == ui::ET_KEY_RELEASED) { | 94 if (event.action() == ui::ET_KEY_RELEASED) { |
| 78 if (event.key_data().key_code() == ui::VKEY_L && | 95 if (event.key_data().key_code() == ui::VKEY_L && |
| 79 (event.flags() & ui::EF_CONTROL_DOWN)) { | 96 (event.flags() & ui::EF_CONTROL_DOWN)) { |
| 80 InitLauncher(); | 97 InitLauncher(); |
| 81 launcher_->Show(); | 98 launcher_->Show(); |
| 82 } | 99 } |
| 83 } | 100 } |
| 84 } | 101 } |
| 85 | 102 |
| 86 // Overridden from LauncherClient: | 103 // Overridden from LauncherClient: |
| 87 virtual void OnURLEntered(const mojo::String& url) OVERRIDE { | 104 virtual void OnURLEntered(const mojo::String& url) OVERRIDE { |
| 88 std::string url_spec = url.To<std::string>(); | 105 std::string url_spec = url.To<std::string>(); |
| 89 printf("Received URL from launcher app: %s\n", url_spec.c_str()); | 106 printf("Received URL from launcher app: %s\n", url_spec.c_str()); |
| 90 launcher_->Hide(); | 107 launcher_->Hide(); |
| 91 } | 108 } |
| 92 | 109 |
| 110 void InitNativeViewport() { |
| 111 AllocationScope scope; |
| 112 ScopedMessagePipeHandle client_handle, native_viewport_handle; |
| 113 CreateMessagePipe(&client_handle, &native_viewport_handle); |
| 114 shell_->Connect("mojo:mojo_native_viewport_service", |
| 115 client_handle.Pass()); |
| 116 native_viewport_.reset(native_viewport_handle.Pass(), this); |
| 117 |
| 118 native_viewport_->Create(gfx::Rect(50, 50, 800, 600)); |
| 119 native_viewport_->Show(); |
| 120 } |
| 121 |
| 93 void InitLauncher() { | 122 void InitLauncher() { |
| 94 if (!launcher_.is_null()) | 123 if (!launcher_.is_null()) |
| 95 return; | 124 return; |
| 96 AllocationScope scope; | 125 AllocationScope scope; |
| 97 ScopedMessagePipeHandle client_handle, native_viewport_handle; | 126 ScopedMessagePipeHandle client_handle, native_viewport_handle; |
| 98 CreateMessagePipe(&client_handle, &native_viewport_handle); | 127 CreateMessagePipe(&client_handle, &native_viewport_handle); |
| 99 connector_->Connect("mojo:mojo_launcher", client_handle.Pass()); | 128 shell_->Connect("mojo:mojo_launcher", client_handle.Pass()); |
| 100 launcher_.reset(native_viewport_handle.Pass(), this); | 129 launcher_.reset(native_viewport_handle.Pass(), this); |
| 101 } | 130 } |
| 102 | 131 |
| 103 Connector* connector_; | 132 void DidCreateContext() { |
| 133 } |
| 134 |
| 135 RemotePtr<Shell> shell_; |
| 136 RemotePtr<ViewManagerClient> client_; |
| 137 ScopedVector<ViewImpl> views_; |
| 104 RemotePtr<NativeViewport> native_viewport_; | 138 RemotePtr<NativeViewport> native_viewport_; |
| 105 RemotePtr<Launcher> launcher_; | 139 RemotePtr<Launcher> launcher_; |
| 106 | 140 |
| 107 DISALLOW_COPY_AND_ASSIGN(NativeViewportClientImpl); | |
| 108 }; | |
| 109 | |
| 110 class ViewImpl : public View { | |
| 111 public: | |
| 112 explicit ViewImpl(ScopedMessagePipeHandle handle) | |
| 113 : id_(-1), | |
| 114 client_(handle.Pass()) {} | |
| 115 virtual ~ViewImpl() {} | |
| 116 | |
| 117 private: | |
| 118 // Overridden from View: | |
| 119 virtual void SetId(int view_id) MOJO_OVERRIDE { | |
| 120 id_ = view_id; | |
| 121 } | |
| 122 virtual void GetId() MOJO_OVERRIDE { | |
| 123 client_->OnGotId(id_); | |
| 124 } | |
| 125 | |
| 126 int id_; | |
| 127 RemotePtr<ViewClient> client_; | |
| 128 | |
| 129 DISALLOW_COPY_AND_ASSIGN(ViewImpl); | |
| 130 }; | |
| 131 | |
| 132 class ViewManagerImpl : public ViewManager { | |
| 133 public: | |
| 134 explicit ViewManagerImpl(ScopedMessagePipeHandle handle) | |
| 135 : client_(handle.Pass()) { | |
| 136 } | |
| 137 virtual ~ViewManagerImpl() {} | |
| 138 | |
| 139 private: | |
| 140 // Overridden from ViewManager: | |
| 141 virtual void CreateView() MOJO_OVERRIDE { | |
| 142 ScopedMessagePipeHandle server_handle, client_handle; | |
| 143 CreateMessagePipe(&server_handle, &client_handle); | |
| 144 views_.push_back(new ViewImpl(server_handle.Pass())); | |
| 145 client_->OnViewCreated(client_handle.Pass()); | |
| 146 } | |
| 147 | |
| 148 RemotePtr<ViewManagerClient> client_; | |
| 149 ScopedVector<ViewImpl> views_; | |
| 150 | |
| 151 DISALLOW_COPY_AND_ASSIGN(ViewManagerImpl); | 141 DISALLOW_COPY_AND_ASSIGN(ViewManagerImpl); |
| 152 }; | 142 }; |
| 153 | 143 |
| 154 class ViewManager : public ShellClient, | |
| 155 public Connector { | |
| 156 public: | |
| 157 explicit ViewManager(ScopedMessagePipeHandle shell_handle) | |
| 158 : shell_(shell_handle.Pass(), this) { | |
| 159 nvc_.reset(new NativeViewportClientImpl(this)); | |
| 160 } | |
| 161 | |
| 162 private: | |
| 163 // Overridden from ShellClient: | |
| 164 virtual void AcceptConnection(ScopedMessagePipeHandle handle) MOJO_OVERRIDE { | |
| 165 view_managers_.push_back(new ViewManagerImpl(handle.Pass())); | |
| 166 } | |
| 167 | |
| 168 // Overridden from Connector: | |
| 169 virtual void Connect(const String& url, | |
| 170 ScopedMessagePipeHandle client_handle) OVERRIDE { | |
| 171 shell_->Connect(url, client_handle.Pass()); | |
| 172 } | |
| 173 | |
| 174 RemotePtr<Shell> shell_; | |
| 175 scoped_ptr<NativeViewportClientImpl> nvc_; | |
| 176 ScopedVector<ViewManagerImpl> view_managers_; | |
| 177 | |
| 178 DISALLOW_COPY_AND_ASSIGN(ViewManager); | |
| 179 }; | |
| 180 | |
| 181 } // namespace examples | 144 } // namespace examples |
| 182 } // namespace mojo | 145 } // namespace mojo |
| 183 | 146 |
| 184 extern "C" VIEW_MANAGER_EXPORT MojoResult CDECL MojoMain( | 147 extern "C" VIEW_MANAGER_EXPORT MojoResult CDECL MojoMain( |
| 185 MojoHandle shell_handle) { | 148 MojoHandle shell_handle) { |
| 186 base::MessageLoop loop; | 149 base::MessageLoop loop; |
| 187 mojo::examples::ViewManager view_manager( | 150 mojo::examples::ViewManagerImpl view_manager( |
| 188 mojo::MakeScopedHandle(mojo::MessagePipeHandle(shell_handle)).Pass()); | 151 mojo::MakeScopedHandle(mojo::MessagePipeHandle(shell_handle)).Pass()); |
| 189 loop.Run(); | 152 loop.Run(); |
| 190 | 153 |
| 191 return MOJO_RESULT_OK; | 154 return MOJO_RESULT_OK; |
| 192 } | 155 } |
| OLD | NEW |