| 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> | |
| 7 | 6 |
| 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" | |
| 12 #include "mojo/public/system/core.h" | 7 #include "mojo/public/system/core.h" |
| 13 #include "mojo/public/system/macros.h" | 8 #include "mojo/public/system/macros.h" |
| 14 | 9 |
| 15 #if defined(WIN32) | 10 #if defined(WIN32) |
| 16 #if !defined(CDECL) | 11 #if !defined(CDECL) |
| 17 #define CDECL __cdecl | 12 #define CDECL __cdecl |
| 18 #endif | 13 #endif |
| 19 #define SAMPLE_APP_EXPORT __declspec(dllexport) | 14 #define SAMPLE_APP_EXPORT __declspec(dllexport) |
| 20 #else | 15 #else |
| 21 #define CDECL | 16 #define CDECL |
| 22 #define SAMPLE_APP_EXPORT __attribute__((visibility("default"))) | 17 #define SAMPLE_APP_EXPORT __attribute__((visibility("default"))) |
| 23 #endif | 18 #endif |
| 24 | 19 |
| 25 namespace mojo { | 20 char* ReadStringFromPipe(mojo::Handle pipe) { |
| 26 namespace examples { | 21 uint32_t len = 0; |
| 27 | 22 char* buf = NULL; |
| 28 static HelloWorldClientImpl* g_client = 0; | 23 MojoResult result = mojo::ReadMessage(pipe, buf, &len, NULL, NULL, |
| 29 | 24 MOJO_READ_MESSAGE_FLAG_NONE); |
| 30 void SayHello(mojo::Handle pipe) { | 25 if (result == MOJO_RESULT_RESOURCE_EXHAUSTED) { |
| 31 g_client = new HelloWorldClientImpl(pipe); | 26 buf = new char[len]; |
| 32 | 27 result = mojo::ReadMessage(pipe, buf, &len, NULL, NULL, |
| 33 mojo::ScratchBuffer buf; | 28 MOJO_READ_MESSAGE_FLAG_NONE); |
| 34 const std::string kGreeting("hello, world!"); | 29 } |
| 35 mojo::String* greeting = mojo::String::NewCopyOf(&buf, kGreeting); | 30 if (result < MOJO_RESULT_OK) { |
| 36 | 31 // Failure.. |
| 37 g_client->service()->Greeting(greeting); | 32 if (buf) |
| 33 delete[] buf; |
| 34 return NULL; |
| 35 } |
| 36 return buf; |
| 38 } | 37 } |
| 39 | 38 |
| 40 } // examples | 39 class SampleMessageWaiter { |
| 41 } // mojo | 40 public: |
| 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 }; |
| 42 | 70 |
| 43 extern "C" SAMPLE_APP_EXPORT MojoResult CDECL MojoMain( | 71 extern "C" SAMPLE_APP_EXPORT MojoResult CDECL MojoMain( |
| 44 mojo::Handle pipe) { | 72 mojo::Handle pipe) { |
| 45 // Create a message loop on this thread for processing incoming messages. | 73 SampleMessageWaiter(pipe).WaitAndRead(); |
| 46 // This creates a dependency on base that we'll be removing soon. | |
| 47 base::MessageLoop loop; | |
| 48 | |
| 49 // Set the global bindings support. | |
| 50 mojo::common::BindingsSupportImpl bindings_support; | |
| 51 mojo::BindingsSupport::Set(&bindings_support); | |
| 52 | |
| 53 // Send message out. | |
| 54 mojo::examples::SayHello(pipe); | |
| 55 | |
| 56 // Run loop to receieve Ack. | |
| 57 loop.Run(); | |
| 58 | |
| 59 mojo::BindingsSupport::Set(NULL); | |
| 60 return MOJO_RESULT_OK; | 74 return MOJO_RESULT_OK; |
| 61 } | 75 } |
| OLD | NEW |