Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(70)

Side by Side Diff: mojo/examples/content_handler_demo/content_handler_demo.cc

Issue 423963004: First cut at "content handling" support in Mojo. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Use URLResponse with ContentHandler interface instead of data pipe Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | mojo/mojo.gyp » ('j') | mojo/service_manager/service_manager.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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_with_context.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(const mojo::String& url,
26 URLResponsePtr content,
27 ServiceProviderPtr service_provider) MOJO_OVERRIDE;
28
29 ContentHandlerApp* content_handler_app_;
30 };
31
32 class ContentHandlerApp
33 : public ApplicationDelegate,
34 public InterfaceFactoryWithContext<ContentHandlerImpl,
35 ContentHandlerApp> {
36 public:
37 ContentHandlerApp() : InterfaceFactoryWithContext(this) {
38 }
39
40 virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE {
41 }
42
43 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection)
44 MOJO_OVERRIDE {
45 connection->AddService(this);
46 return true;
47 }
48
49 void PrintResponse(ScopedDataPipeConsumerHandle body) const {
50 for (;;) {
51 char buf[512];
52 uint32_t num_bytes = sizeof(buf);
53 MojoResult result = ReadDataRaw(body.get(), buf, &num_bytes,
54 MOJO_READ_DATA_FLAG_NONE);
55 if (result == MOJO_RESULT_SHOULD_WAIT) {
56 Wait(body.get(),
57 MOJO_HANDLE_SIGNAL_READABLE,
58 MOJO_DEADLINE_INDEFINITE);
59 } else if (result == MOJO_RESULT_OK) {
60 if (fwrite(buf, num_bytes, 1, stdout) != 1) {
61 printf("\nUnexpected error writing to file\n");
62 break;
63 }
64 } else {
65 break;
66 }
67
68 printf("\n>>> EOF <<<\n");
69 }
70 }
71 };
72
73 void ContentHandlerImpl::OnConnect(const mojo::String& url,
74 URLResponsePtr content,
75 ServiceProviderPtr service_provider) {
76 printf("ContentHandler::OnConnect - url:%s - body follows\n\n",
77 url.To<std::string>().c_str());
78 content_handler_app_->PrintResponse(content->body.Pass());
79 }
80
81 } // namespace examples
82
83 // static
84 ApplicationDelegate* ApplicationDelegate::Create() {
85 return new examples::ContentHandlerApp();
86 }
87
88 } // namespace mojo
OLDNEW
« no previous file with comments | « no previous file | mojo/mojo.gyp » ('j') | mojo/service_manager/service_manager.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698