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