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