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