OLD | NEW |
(Empty) | |
| 1 // Copyright 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 "mojo/public/cpp/application/application_delegate.h" |
| 8 #include "mojo/public/cpp/application/application_impl.h" |
| 9 #include "mojo/public/cpp/application/interface_factory_with_context.h" |
| 10 #include "mojo/public/interfaces/service_provider/content_handler.mojom.h" |
| 11 #include "mojo/public/interfaces/service_provider/service_provider.mojom.h" |
| 12 |
| 13 namespace mojo { |
| 14 namespace examples { |
| 15 |
| 16 class ContentHandlerApp; |
| 17 |
| 18 class ContentHandlerImpl : public InterfaceImpl<ContentHandler> { |
| 19 public: |
| 20 explicit ContentHandlerImpl(ContentHandlerApp* content_handler_app) |
| 21 : content_handler_app_(content_handler_app) { |
| 22 } |
| 23 virtual ~ContentHandlerImpl() {} |
| 24 |
| 25 private: |
| 26 virtual void OnConnect(const mojo::String& url, |
| 27 ScopedDataPipeConsumerHandle content, |
| 28 ServiceProviderPtr service_provider) MOJO_OVERRIDE; |
| 29 |
| 30 ContentHandlerApp* content_handler_app_; |
| 31 }; |
| 32 |
| 33 class ContentHandlerApp |
| 34 : public ApplicationDelegate, |
| 35 public InterfaceFactoryWithContext<ContentHandlerImpl, |
| 36 ContentHandlerApp> { |
| 37 public: |
| 38 ContentHandlerApp() : InterfaceFactoryWithContext(this) { |
| 39 } |
| 40 |
| 41 virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE { |
| 42 } |
| 43 |
| 44 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) |
| 45 MOJO_OVERRIDE { |
| 46 connection->AddService(this); |
| 47 return true; |
| 48 } |
| 49 |
| 50 void PrintResponse(ScopedDataPipeConsumerHandle body) const { |
| 51 for (;;) { |
| 52 char buf[512]; |
| 53 uint32_t num_bytes = sizeof(buf); |
| 54 MojoResult result = ReadDataRaw(body.get(), buf, &num_bytes, |
| 55 MOJO_READ_DATA_FLAG_NONE); |
| 56 if (result == MOJO_RESULT_SHOULD_WAIT) { |
| 57 Wait(body.get(), |
| 58 MOJO_HANDLE_SIGNAL_READABLE, |
| 59 MOJO_DEADLINE_INDEFINITE); |
| 60 } else if (result == MOJO_RESULT_OK) { |
| 61 if (fwrite(buf, num_bytes, 1, stdout) != 1) { |
| 62 printf("\nUnexpected error writing to file\n"); |
| 63 break; |
| 64 } |
| 65 } else { |
| 66 break; |
| 67 } |
| 68 |
| 69 printf("\n>>> EOF <<<\n"); |
| 70 } |
| 71 } |
| 72 }; |
| 73 |
| 74 void ContentHandlerImpl::OnConnect(const mojo::String& url, |
| 75 ScopedDataPipeConsumerHandle content, |
| 76 ServiceProviderPtr service_provider) { |
| 77 printf("ContentHandler::OnConnect - url:%s - body follows\n\n", |
| 78 url.To<std::string>().c_str()); |
| 79 content_handler_app_->PrintResponse(content.Pass()); |
| 80 } |
| 81 |
| 82 } // namespace examples |
| 83 |
| 84 // static |
| 85 ApplicationDelegate* ApplicationDelegate::Create() { |
| 86 return new examples::ContentHandlerApp(); |
| 87 } |
| 88 |
| 89 } // namespace mojo |
OLD | NEW |