Index: examples/content_handler_demo/content_handler_demo.cc |
diff --git a/examples/content_handler_demo/content_handler_demo.cc b/examples/content_handler_demo/content_handler_demo.cc |
index 3abf0f78ff295859fae84d1313636490ffd846d9..81723a87dbd57e0c319c9b5fef7c1145cfc6e826 100644 |
--- a/examples/content_handler_demo/content_handler_demo.cc |
+++ b/examples/content_handler_demo/content_handler_demo.cc |
@@ -4,45 +4,34 @@ |
#include <stdio.h> |
+#include "mojo/public/c/system/main.h" |
#include "mojo/public/cpp/application/application_delegate.h" |
#include "mojo/public/cpp/application/application_impl.h" |
+#include "mojo/public/cpp/application/application_runner.h" |
#include "mojo/public/cpp/application/interface_factory_impl.h" |
#include "mojo/services/public/interfaces/content_handler/content_handler.mojom.h" |
namespace mojo { |
namespace examples { |
-class ContentHandlerApp; |
- |
-class ContentHandlerImpl : public InterfaceImpl<ContentHandler> { |
- public: |
- explicit ContentHandlerImpl(ContentHandlerApp* content_handler_app) |
- : content_handler_app_(content_handler_app) { |
- } |
- virtual ~ContentHandlerImpl() {} |
- |
- private: |
- virtual void OnConnect( |
- const mojo::String& requestor_url, |
- URLResponsePtr response, |
- InterfaceRequest<ServiceProvider> service_provider) override; |
- |
- ContentHandlerApp* content_handler_app_; |
-}; |
- |
-class ContentHandlerApp : public ApplicationDelegate { |
+class PrintBodyApplication : public InterfaceImpl<Application> { |
public: |
- ContentHandlerApp() : content_handler_factory_(this) { |
+ PrintBodyApplication(ShellPtr shell, ScopedDataPipeConsumerHandle body) |
+ : shell_(shell.Pass()), body_(body.Pass()) { |
+ shell_.set_client(this); |
} |
- virtual void Initialize(ApplicationImpl* app) override {} |
+ virtual void Initialize(Array<String> args) override {} |
- virtual bool ConfigureIncomingConnection( |
- ApplicationConnection* connection) override { |
- connection->AddService(&content_handler_factory_); |
- return true; |
+ virtual void AcceptConnection(const String& requestor_url, |
+ ServiceProviderPtr service_provider) override { |
+ printf("ContentHandler::OnConnect - requestor_url:%s - body follows\n\n", |
+ requestor_url.To<std::string>().c_str()); |
+ PrintResponse(body_.Pass()); |
+ delete this; |
} |
+ private: |
void PrintResponse(ScopedDataPipeConsumerHandle body) const { |
for (;;) { |
char buf[512]; |
@@ -66,25 +55,47 @@ class ContentHandlerApp : public ApplicationDelegate { |
} |
} |
+ ShellPtr shell_; |
+ ScopedDataPipeConsumerHandle body_; |
+ |
+ MOJO_DISALLOW_COPY_AND_ASSIGN(PrintBodyApplication); |
+}; |
+ |
+class ContentHandlerImpl : public InterfaceImpl<ContentHandler> { |
+ public: |
+ explicit ContentHandlerImpl() {} |
+ virtual ~ContentHandlerImpl() {} |
+ |
private: |
- InterfaceFactoryImplWithContext<ContentHandlerImpl, |
- ContentHandlerApp> content_handler_factory_; |
+ virtual void StartApplication(ShellPtr shell, |
+ URLResponsePtr response) override { |
+ // The application will delete itself after being connected to. |
+ new PrintBodyApplication(shell.Pass(), response->body.Pass()); |
+ } |
}; |
-void ContentHandlerImpl::OnConnect( |
- const mojo::String& requestor_url, |
- URLResponsePtr response, |
- InterfaceRequest<ServiceProvider> service_provider) { |
- printf("ContentHandler::OnConnect - requestor_url:%s - body follows\n\n", |
- requestor_url.To<std::string>().c_str()); |
- content_handler_app_->PrintResponse(response->body.Pass()); |
-} |
+class ContentHandlerApp : public ApplicationDelegate { |
+ public: |
+ ContentHandlerApp() : content_handler_factory_() {} |
-} // namespace examples |
+ virtual void Initialize(ApplicationImpl* app) override {} |
-// static |
-ApplicationDelegate* ApplicationDelegate::Create() { |
- return new examples::ContentHandlerApp(); |
-} |
+ virtual bool ConfigureIncomingConnection( |
+ ApplicationConnection* connection) override { |
+ connection->AddService(&content_handler_factory_); |
+ return true; |
+ } |
+ |
+ private: |
+ InterfaceFactoryImpl<ContentHandlerImpl> content_handler_factory_; |
+ MOJO_DISALLOW_COPY_AND_ASSIGN(ContentHandlerApp); |
+}; |
+ |
+} // namespace examples |
} // namespace mojo |
+ |
+MojoResult MojoMain(MojoHandle shell_handle) { |
+ mojo::ApplicationRunner runner(new mojo::examples::ContentHandlerApp); |
+ return runner.Run(shell_handle); |
+} |