Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "mojo/examples/sample_app/hello_world_client_impl.h" | |
| 7 #include "mojo/public/system/core.h" | 9 #include "mojo/public/system/core.h" |
| 8 #include "mojo/public/system/macros.h" | 10 #include "mojo/public/system/macros.h" |
| 11 #include "mojo/public/bindings/common/bindings_support_impl.h" | |
| 12 #include "mojo/public/bindings/lib/bindings_support.h" | |
| 9 | 13 |
| 10 #if defined(WIN32) | 14 #if defined(WIN32) |
| 11 #if !defined(CDECL) | 15 #if !defined(CDECL) |
| 12 #define CDECL __cdecl | 16 #define CDECL __cdecl |
| 13 #endif | 17 #endif |
| 14 #define SAMPLE_APP_EXPORT __declspec(dllexport) | 18 #define SAMPLE_APP_EXPORT __declspec(dllexport) |
| 15 #else | 19 #else |
| 16 #define CDECL | 20 #define CDECL |
| 17 #define SAMPLE_APP_EXPORT __attribute__((visibility("default"))) | 21 #define SAMPLE_APP_EXPORT __attribute__((visibility("default"))) |
| 18 #endif | 22 #endif |
| 19 | 23 |
| 20 char* ReadStringFromPipe(mojo::Handle pipe) { | 24 namespace mojo { |
| 21 uint32_t len = 0; | 25 namespace examples { |
| 22 char* buf = NULL; | 26 |
| 23 MojoResult result = mojo::ReadMessage(pipe, buf, &len, NULL, NULL, | 27 static HelloWorldClientImpl* client = 0; |
| 24 MOJO_READ_MESSAGE_FLAG_NONE); | 28 |
| 25 if (result == MOJO_RESULT_RESOURCE_EXHAUSTED) { | 29 void SayHello(mojo::Handle pipe) { |
| 26 buf = new char[len]; | 30 client = new HelloWorldClientImpl(pipe); |
| 27 result = mojo::ReadMessage(pipe, buf, &len, NULL, NULL, | 31 |
| 28 MOJO_READ_MESSAGE_FLAG_NONE); | 32 mojo::ScratchBuffer buf; |
| 29 } | 33 const std::string kGreeting("hello, world!"); |
| 30 if (result < MOJO_RESULT_OK) { | 34 mojo::String* greeting = mojo::String::NewCopyOf(&buf, kGreeting); |
| 31 // Failure.. | 35 |
| 32 if (buf) | 36 client->service()->Greeting(greeting); |
| 33 delete[] buf; | |
| 34 return NULL; | |
| 35 } | |
| 36 return buf; | |
| 37 } | 37 } |
| 38 | 38 |
| 39 class SampleMessageWaiter { | 39 } // examples |
| 40 public: | 40 } // 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 | 41 |
| 71 extern "C" SAMPLE_APP_EXPORT MojoResult CDECL MojoMain( | 42 extern "C" SAMPLE_APP_EXPORT MojoResult CDECL MojoMain( |
| 72 mojo::Handle pipe) { | 43 mojo::Handle pipe) { |
| 73 SampleMessageWaiter(pipe).WaitAndRead(); | 44 common::BindingsSupportImpl bindings_support; |
|
darin (slow to review)
2013/11/13 22:57:49
note: using common::BindingsSupportImpl implies ha
| |
| 45 mojo::BindingsSupport::Set(&bindings_support); | |
| 46 | |
| 47 mojo::examples::SayHello(pipe); | |
| 48 | |
| 49 | |
| 50 BindingsSupport::Set(NULL); | |
| 74 return MOJO_RESULT_OK; | 51 return MOJO_RESULT_OK; |
| 75 } | 52 } |
| OLD | NEW |