OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 | 6 |
| 7 #include "mojo/public/c/system/main.h" |
7 #include "mojo/public/cpp/application/application_delegate.h" | 8 #include "mojo/public/cpp/application/application_delegate.h" |
8 #include "mojo/public/cpp/application/application_impl.h" | 9 #include "mojo/public/cpp/application/application_impl.h" |
| 10 #include "mojo/public/cpp/application/application_runner.h" |
9 #include "mojo/public/cpp/application/interface_factory_impl.h" | 11 #include "mojo/public/cpp/application/interface_factory_impl.h" |
10 #include "mojo/services/public/interfaces/content_handler/content_handler.mojom.
h" | 12 #include "mojo/services/public/interfaces/content_handler/content_handler.mojom.
h" |
11 | 13 |
12 namespace mojo { | 14 namespace mojo { |
13 namespace examples { | 15 namespace examples { |
14 | 16 |
15 class ContentHandlerApp; | 17 class PrintBodyApplication : public InterfaceImpl<Application> { |
| 18 public: |
| 19 PrintBodyApplication(ShellPtr shell, ScopedDataPipeConsumerHandle body) |
| 20 : shell_(shell.Pass()), body_(body.Pass()) { |
| 21 shell_.set_client(this); |
| 22 } |
16 | 23 |
17 class ContentHandlerImpl : public InterfaceImpl<ContentHandler> { | 24 virtual void Initialize(Array<String> args) override {} |
18 public: | 25 |
19 explicit ContentHandlerImpl(ContentHandlerApp* content_handler_app) | 26 virtual void AcceptConnection(const String& requestor_url, |
20 : content_handler_app_(content_handler_app) { | 27 ServiceProviderPtr service_provider) override { |
| 28 printf("ContentHandler::OnConnect - requestor_url:%s - body follows\n\n", |
| 29 requestor_url.To<std::string>().c_str()); |
| 30 PrintResponse(body_.Pass()); |
| 31 delete this; |
21 } | 32 } |
22 virtual ~ContentHandlerImpl() {} | |
23 | 33 |
24 private: | 34 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 { | 35 void PrintResponse(ScopedDataPipeConsumerHandle body) const { |
47 for (;;) { | 36 for (;;) { |
48 char buf[512]; | 37 char buf[512]; |
49 uint32_t num_bytes = sizeof(buf); | 38 uint32_t num_bytes = sizeof(buf); |
50 MojoResult result = ReadDataRaw(body.get(), buf, &num_bytes, | 39 MojoResult result = ReadDataRaw(body.get(), buf, &num_bytes, |
51 MOJO_READ_DATA_FLAG_NONE); | 40 MOJO_READ_DATA_FLAG_NONE); |
52 if (result == MOJO_RESULT_SHOULD_WAIT) { | 41 if (result == MOJO_RESULT_SHOULD_WAIT) { |
53 Wait(body.get(), | 42 Wait(body.get(), |
54 MOJO_HANDLE_SIGNAL_READABLE, | 43 MOJO_HANDLE_SIGNAL_READABLE, |
55 MOJO_DEADLINE_INDEFINITE); | 44 MOJO_DEADLINE_INDEFINITE); |
56 } else if (result == MOJO_RESULT_OK) { | 45 } else if (result == MOJO_RESULT_OK) { |
57 if (fwrite(buf, num_bytes, 1, stdout) != 1) { | 46 if (fwrite(buf, num_bytes, 1, stdout) != 1) { |
58 printf("\nUnexpected error writing to file\n"); | 47 printf("\nUnexpected error writing to file\n"); |
59 break; | 48 break; |
60 } | 49 } |
61 } else { | 50 } else { |
62 break; | 51 break; |
63 } | 52 } |
64 | 53 |
65 printf("\n>>> EOF <<<\n"); | 54 printf("\n>>> EOF <<<\n"); |
66 } | 55 } |
67 } | 56 } |
68 | 57 |
69 private: | 58 ShellPtr shell_; |
70 InterfaceFactoryImplWithContext<ContentHandlerImpl, | 59 ScopedDataPipeConsumerHandle body_; |
71 ContentHandlerApp> content_handler_factory_; | 60 |
| 61 MOJO_DISALLOW_COPY_AND_ASSIGN(PrintBodyApplication); |
72 }; | 62 }; |
73 | 63 |
74 void ContentHandlerImpl::OnConnect( | 64 class ContentHandlerImpl : public InterfaceImpl<ContentHandler> { |
75 const mojo::String& requestor_url, | 65 public: |
76 URLResponsePtr response, | 66 explicit ContentHandlerImpl() {} |
77 InterfaceRequest<ServiceProvider> service_provider) { | 67 virtual ~ContentHandlerImpl() {} |
78 printf("ContentHandler::OnConnect - requestor_url:%s - body follows\n\n", | 68 |
79 requestor_url.To<std::string>().c_str()); | 69 private: |
80 content_handler_app_->PrintResponse(response->body.Pass()); | 70 virtual void StartApplication(ShellPtr shell, |
81 } | 71 URLResponsePtr response) override { |
| 72 // The application will delete itself after being connected to. |
| 73 new PrintBodyApplication(shell.Pass(), response->body.Pass()); |
| 74 } |
| 75 }; |
| 76 |
| 77 class ContentHandlerApp : public ApplicationDelegate { |
| 78 public: |
| 79 ContentHandlerApp() : content_handler_factory_() {} |
| 80 |
| 81 virtual void Initialize(ApplicationImpl* app) override {} |
| 82 |
| 83 virtual bool ConfigureIncomingConnection( |
| 84 ApplicationConnection* connection) override { |
| 85 connection->AddService(&content_handler_factory_); |
| 86 return true; |
| 87 } |
| 88 |
| 89 private: |
| 90 InterfaceFactoryImpl<ContentHandlerImpl> content_handler_factory_; |
| 91 |
| 92 MOJO_DISALLOW_COPY_AND_ASSIGN(ContentHandlerApp); |
| 93 }; |
82 | 94 |
83 } // namespace examples | 95 } // namespace examples |
| 96 } // namespace mojo |
84 | 97 |
85 // static | 98 MojoResult MojoMain(MojoHandle shell_handle) { |
86 ApplicationDelegate* ApplicationDelegate::Create() { | 99 mojo::ApplicationRunner runner(new mojo::examples::ContentHandlerApp); |
87 return new examples::ContentHandlerApp(); | 100 return runner.Run(shell_handle); |
88 } | 101 } |
89 | |
90 } // namespace mojo | |
OLD | NEW |