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

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

Issue 72123002: Work in progress for end-to-end bindings (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add base dep for sample_app Created 7 years, 1 month 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 #include <string>
6 7
8 #include "base/message_loop/message_loop.h"
9 #include "mojo/common/bindings_support_impl.h"
10 #include "mojo/examples/sample_app/hello_world_client_impl.h"
11 #include "mojo/public/bindings/lib/bindings_support.h"
7 #include "mojo/public/system/core.h" 12 #include "mojo/public/system/core.h"
8 #include "mojo/public/system/macros.h" 13 #include "mojo/public/system/macros.h"
9 14
10 #if defined(WIN32) 15 #if defined(WIN32)
11 #if !defined(CDECL) 16 #if !defined(CDECL)
12 #define CDECL __cdecl 17 #define CDECL __cdecl
13 #endif 18 #endif
14 #define SAMPLE_APP_EXPORT __declspec(dllexport) 19 #define SAMPLE_APP_EXPORT __declspec(dllexport)
15 #else 20 #else
16 #define CDECL 21 #define CDECL
17 #define SAMPLE_APP_EXPORT __attribute__((visibility("default"))) 22 #define SAMPLE_APP_EXPORT __attribute__((visibility("default")))
18 #endif 23 #endif
19 24
20 char* ReadStringFromPipe(mojo::Handle pipe) { 25 namespace mojo {
21 uint32_t len = 0; 26 namespace examples {
22 char* buf = NULL; 27
23 MojoResult result = mojo::ReadMessage(pipe, buf, &len, NULL, NULL, 28 void SayHello(mojo::Handle pipe) {
24 MOJO_READ_MESSAGE_FLAG_NONE); 29 // Send message out.
25 if (result == MOJO_RESULT_RESOURCE_EXHAUSTED) { 30 HelloWorldClientImpl client(pipe);
26 buf = new char[len]; 31 mojo::ScratchBuffer buf;
27 result = mojo::ReadMessage(pipe, buf, &len, NULL, NULL, 32 const std::string kGreeting("hello, world!");
28 MOJO_READ_MESSAGE_FLAG_NONE); 33 mojo::String* greeting = mojo::String::NewCopyOf(&buf, kGreeting);
29 } 34 client.service()->Greeting(greeting);
30 if (result < MOJO_RESULT_OK) { 35
31 // Failure.. 36 // Run loop to receieve Ack. The client will quit the loop.
32 if (buf) 37 base::MessageLoop::current()->Run();
33 delete[] buf;
34 return NULL;
35 }
36 return buf;
37 } 38 }
38 39
39 class SampleMessageWaiter { 40 } // examples
40 public: 41 } // mojo
41 explicit SampleMessageWaiter(mojo::Handle pipe) : pipe_(pipe) {}
42 ~SampleMessageWaiter() {}
43
44 void Read() {
45 char* string = ReadStringFromPipe(pipe_);
46 if (string) {
47 printf("Read string from pipe: %s\n", string);
48 delete[] string;
49 string = NULL;
50 }
51 }
52
53 void WaitAndRead() {
54 for (int i = 0; i < 100;) {
55 MojoResult result = mojo::Wait(pipe_, MOJO_WAIT_FLAG_READABLE, 100);
56 if (result < MOJO_RESULT_OK) {
57 // Failure...
58 continue;
59 }
60 ++i;
61 Read();
62 }
63 }
64
65 private:
66 mojo::Handle pipe_;
67
68 MOJO_DISALLOW_COPY_AND_ASSIGN(SampleMessageWaiter);
69 };
70 42
71 extern "C" SAMPLE_APP_EXPORT MojoResult CDECL MojoMain( 43 extern "C" SAMPLE_APP_EXPORT MojoResult CDECL MojoMain(
72 mojo::Handle pipe) { 44 mojo::Handle pipe) {
73 SampleMessageWaiter(pipe).WaitAndRead(); 45 base::MessageLoop loop;
46 // Set the global bindings support.
47 mojo::common::BindingsSupportImpl bindings_support;
48 mojo::BindingsSupport::Set(&bindings_support);
49
50 mojo::examples::SayHello(pipe);
51
52 mojo::BindingsSupport::Set(NULL);
74 return MOJO_RESULT_OK; 53 return MOJO_RESULT_OK;
75 } 54 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698