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.
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"
15 class ContentHandlerApp;
17 class ContentHandlerImpl : public InterfaceImpl<ContentHandler> {
19 explicit ContentHandlerImpl(ContentHandlerApp* content_handler_app)
20 : content_handler_app_(content_handler_app) {
22 virtual ~ContentHandlerImpl() {}
25 virtual void OnConnect(const mojo::String& url,
26 URLResponsePtr response,
27 InterfaceRequest<ServiceProvider> service_provider)
30 ContentHandlerApp* content_handler_app_;
33 class ContentHandlerApp : public ApplicationDelegate {
35 ContentHandlerApp() : content_handler_factory_(this) {
38 virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE {
41 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection)
43 connection->AddService(&content_handler_factory_);
47 void PrintResponse(ScopedDataPipeConsumerHandle body) const {
50 uint32_t num_bytes = sizeof(buf);
51 MojoResult result = ReadDataRaw(body.get(), buf, &num_bytes,
52 MOJO_READ_DATA_FLAG_NONE);
53 if (result == MOJO_RESULT_SHOULD_WAIT) {
55 MOJO_HANDLE_SIGNAL_READABLE,
56 MOJO_DEADLINE_INDEFINITE);
57 } else if (result == MOJO_RESULT_OK) {
58 if (fwrite(buf, num_bytes, 1, stdout) != 1) {
59 printf("\nUnexpected error writing to file\n");
66 printf("\n>>> EOF <<<\n");
71 InterfaceFactoryImplWithContext<ContentHandlerImpl,
72 ContentHandlerApp> content_handler_factory_;
75 void ContentHandlerImpl::OnConnect(
76 const mojo::String& url,
77 URLResponsePtr response,
78 InterfaceRequest<ServiceProvider> service_provider) {
79 printf("ContentHandler::OnConnect - url:%s - body follows\n\n",
80 url.To<std::string>().c_str());
81 content_handler_app_->PrintResponse(response->body.Pass());
84 } // namespace examples
87 ApplicationDelegate* ApplicationDelegate::Create() {
88 return new examples::ContentHandlerApp();