Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(654)

Side by Side Diff: ppapi/examples/mojo/mojo.cc

Issue 598183002: Pepper: PPB_Mojo prototype. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ppapi/examples/mojo/README ('k') | ppapi/examples/mojo/mojo.html » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 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 <stdio.h>
6
7 #include "base/bind.h"
8 #include "base/threading/thread.h"
9 #include "mojo/embedder/channel_init.h"
10 #include "mojo/embedder/embedder.h"
11 #include "mojo/embedder/platform_support.h"
12 #include "mojo/examples/echo/echo_service.mojom.h"
13 #include "mojo/public/cpp/utility/run_loop.h"
14 #include "mojo/public/interfaces/application/service_provider.mojom.h"
15 #include "ppapi/c/ppb_mojo.h"
16 #include "ppapi/cpp/instance.h"
17 #include "ppapi/cpp/module.h"
18 #include "ppapi/utility/completion_callback_factory.h"
19
20 class ResponsePrinter {
21 public:
22 void Run(const mojo::String& value) const {
23 fprintf(stderr, "Response: \"%s\"\n", value.get().c_str());
24
25 // TODO(teravest): Remove this.
26 mojo::RunLoop::current()->Quit();
27 }
28 };
29
30 class ServiceRegistryImpl : public mojo::InterfaceImpl<mojo::ServiceProvider> {
31 public:
32 ServiceRegistryImpl(mojo::ScopedMessagePipeHandle handle) {
33 BindRemoteServiceProvider(handle.Pass());
34 }
35
36 void BindRemoteServiceProvider(mojo::ScopedMessagePipeHandle handle) {
37 mojo::WeakBindToPipe(this, handle.Pass());
38 }
39
40 void ConnectToRemoteService(const std::string& service_name,
41 mojo::ScopedMessagePipeHandle handle) {
42 client()->ConnectToService(mojo::String::From(service_name), handle.Pass());
43 }
44
45 virtual void ConnectToService(const mojo::String& service_name,
46 mojo::ScopedMessagePipeHandle handle) {
47 NOTREACHED();
48 }
49
50 void AddService(
51 const std::string& service_name,
52 const base::Callback<
53 void(mojo::ScopedMessagePipeHandle)> service_factory) {
54 NOTREACHED();
55 }
56 };
57
58 class MyInstance : public pp::Instance {
59 public:
60 explicit MyInstance(PP_Instance instance)
61 : pp::Instance(instance),
62 io_thread_("io thread"),
63 callback_factory_(this) {
64 }
65
66 virtual ~MyInstance() {}
67
68 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) {
69 mojo_ = reinterpret_cast<const PPB_Mojo*>(
70 pp::Module::Get()->GetBrowserInterface(PPB_MOJO_INTERFACE));
71
72 mojo::embedder::Init(scoped_ptr<mojo::embedder::PlatformSupport>());
73 if (!mojo_)
74 return false;
75
76 pp::CompletionCallback cc = callback_factory_.NewCallback(
77 &MyInstance::HandleReceived);
78 int32_t rc = mojo_->GetHandle(
79 pp_instance(),
80 &raw_mojo_handle_,
81 cc.pp_completion_callback());
82 return rc == PP_OK_COMPLETIONPENDING;
83 }
84
85 void HandleReceived(int32_t hmm) {
86 base::Thread::Options opts(base::MessageLoop::TYPE_IO, 0);
87 io_thread_.StartWithOptions(opts);
88
89 base::PlatformFile handle = raw_mojo_handle_;
90 mojo::ScopedMessagePipeHandle message_pipe =
91 channel_init_.Init(handle,
92 io_thread_.message_loop()->task_runner());
93 service_registry_.reset(new ServiceRegistryImpl(message_pipe.Pass()));
94
95 mojo::MessagePipe pipe;
96 echo_service_.Bind(pipe.handle0.Pass());
97 service_registry_->ConnectToRemoteService("mojo::examples::EchoService",
98 pipe.handle1.Pass());
99
100 echo_service_->EchoString("test", ResponsePrinter());
101
102 // TODO(teravest): Do we need to construct a RunLoop and call Run() at all?
103 // This doesn't seem to happen for MojoApplication where it's used in the
104 // renderer, but Environment and RunLoop seem to be necessary to make
105 // things work.
106 loop_.Run();
107 }
108
109 private:
110 const PPB_Mojo* mojo_;
111 uint32_t raw_mojo_handle_;
112
113 base::Thread io_thread_;
114
115 scoped_ptr<ServiceRegistryImpl> service_registry_;
116
117 mojo::embedder::ChannelInit channel_init_;
118
119 mojo::examples::EchoServicePtr echo_service_;
120 mojo::Environment environment_;
121 mojo::RunLoop loop_;
122
123 pp::CompletionCallbackFactory<MyInstance> callback_factory_;
124 };
125
126
127 // This object is the global object representing this plugin library as long
128 // as it is loaded.
129 class MyModule : public pp::Module {
130 public:
131 MyModule() : pp::Module() {}
132 virtual ~MyModule() {}
133
134 // Override CreateInstance to create your customized Instance object.
135 virtual pp::Instance* CreateInstance(PP_Instance instance) {
136 return new MyInstance(instance);
137 }
138 };
139
140 namespace pp {
141
142 // Factory function for your specialization of the Module object.
143 Module* CreateModule() {
144 return new MyModule();
145 }
146
147 } // namespace pp
OLDNEW
« no previous file with comments | « ppapi/examples/mojo/README ('k') | ppapi/examples/mojo/mojo.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698