Index: ppapi/examples/mojo/mojo.cc |
diff --git a/ppapi/examples/mojo/mojo.cc b/ppapi/examples/mojo/mojo.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..2e8bca8d8bd177642e9898d56d810aada87aba0e |
--- /dev/null |
+++ b/ppapi/examples/mojo/mojo.cc |
@@ -0,0 +1,147 @@ |
+// Copyright (c) 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <stdio.h> |
+ |
+#include "base/bind.h" |
+#include "base/threading/thread.h" |
+#include "mojo/embedder/channel_init.h" |
+#include "mojo/embedder/embedder.h" |
+#include "mojo/embedder/platform_support.h" |
+#include "mojo/examples/echo/echo_service.mojom.h" |
+#include "mojo/public/cpp/utility/run_loop.h" |
+#include "mojo/public/interfaces/application/service_provider.mojom.h" |
+#include "ppapi/c/ppb_mojo.h" |
+#include "ppapi/cpp/instance.h" |
+#include "ppapi/cpp/module.h" |
+#include "ppapi/utility/completion_callback_factory.h" |
+ |
+class ResponsePrinter { |
+ public: |
+ void Run(const mojo::String& value) const { |
+ fprintf(stderr, "Response: \"%s\"\n", value.get().c_str()); |
+ |
+ // TODO(teravest): Remove this. |
+ mojo::RunLoop::current()->Quit(); |
+ } |
+}; |
+ |
+class ServiceRegistryImpl : public mojo::InterfaceImpl<mojo::ServiceProvider> { |
+ public: |
+ ServiceRegistryImpl(mojo::ScopedMessagePipeHandle handle) { |
+ BindRemoteServiceProvider(handle.Pass()); |
+ } |
+ |
+ void BindRemoteServiceProvider(mojo::ScopedMessagePipeHandle handle) { |
+ mojo::WeakBindToPipe(this, handle.Pass()); |
+ } |
+ |
+ void ConnectToRemoteService(const std::string& service_name, |
+ mojo::ScopedMessagePipeHandle handle) { |
+ client()->ConnectToService(mojo::String::From(service_name), handle.Pass()); |
+ } |
+ |
+ virtual void ConnectToService(const mojo::String& service_name, |
+ mojo::ScopedMessagePipeHandle handle) { |
+ NOTREACHED(); |
+ } |
+ |
+ void AddService( |
+ const std::string& service_name, |
+ const base::Callback< |
+ void(mojo::ScopedMessagePipeHandle)> service_factory) { |
+ NOTREACHED(); |
+ } |
+}; |
+ |
+class MyInstance : public pp::Instance { |
+ public: |
+ explicit MyInstance(PP_Instance instance) |
+ : pp::Instance(instance), |
+ io_thread_("io thread"), |
+ callback_factory_(this) { |
+ } |
+ |
+ virtual ~MyInstance() {} |
+ |
+ virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) { |
+ mojo_ = reinterpret_cast<const PPB_Mojo*>( |
+ pp::Module::Get()->GetBrowserInterface(PPB_MOJO_INTERFACE)); |
+ |
+ mojo::embedder::Init(scoped_ptr<mojo::embedder::PlatformSupport>()); |
+ if (!mojo_) |
+ return false; |
+ |
+ pp::CompletionCallback cc = callback_factory_.NewCallback( |
+ &MyInstance::HandleReceived); |
+ int32_t rc = mojo_->GetHandle( |
+ pp_instance(), |
+ &raw_mojo_handle_, |
+ cc.pp_completion_callback()); |
+ return rc == PP_OK_COMPLETIONPENDING; |
+ } |
+ |
+ void HandleReceived(int32_t hmm) { |
+ base::Thread::Options opts(base::MessageLoop::TYPE_IO, 0); |
+ io_thread_.StartWithOptions(opts); |
+ |
+ base::PlatformFile handle = raw_mojo_handle_; |
+ mojo::ScopedMessagePipeHandle message_pipe = |
+ channel_init_.Init(handle, |
+ io_thread_.message_loop()->task_runner()); |
+ service_registry_.reset(new ServiceRegistryImpl(message_pipe.Pass())); |
+ |
+ mojo::MessagePipe pipe; |
+ echo_service_.Bind(pipe.handle0.Pass()); |
+ service_registry_->ConnectToRemoteService("mojo::examples::EchoService", |
+ pipe.handle1.Pass()); |
+ |
+ echo_service_->EchoString("test", ResponsePrinter()); |
+ |
+ // TODO(teravest): Do we need to construct a RunLoop and call Run() at all? |
+ // This doesn't seem to happen for MojoApplication where it's used in the |
+ // renderer, but Environment and RunLoop seem to be necessary to make |
+ // things work. |
+ loop_.Run(); |
+ } |
+ |
+ private: |
+ const PPB_Mojo* mojo_; |
+ uint32_t raw_mojo_handle_; |
+ |
+ base::Thread io_thread_; |
+ |
+ scoped_ptr<ServiceRegistryImpl> service_registry_; |
+ |
+ mojo::embedder::ChannelInit channel_init_; |
+ |
+ mojo::examples::EchoServicePtr echo_service_; |
+ mojo::Environment environment_; |
+ mojo::RunLoop loop_; |
+ |
+ pp::CompletionCallbackFactory<MyInstance> callback_factory_; |
+}; |
+ |
+ |
+// This object is the global object representing this plugin library as long |
+// as it is loaded. |
+class MyModule : public pp::Module { |
+ public: |
+ MyModule() : pp::Module() {} |
+ virtual ~MyModule() {} |
+ |
+ // Override CreateInstance to create your customized Instance object. |
+ virtual pp::Instance* CreateInstance(PP_Instance instance) { |
+ return new MyInstance(instance); |
+ } |
+}; |
+ |
+namespace pp { |
+ |
+// Factory function for your specialization of the Module object. |
+Module* CreateModule() { |
+ return new MyModule(); |
+} |
+ |
+} // namespace pp |