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

Unified 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: more gn 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | mojo/mojo.gyp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/examples/content_handler_demo/content_handler_demo.cc
diff --git a/mojo/examples/content_handler_demo/content_handler_demo.cc b/mojo/examples/content_handler_demo/content_handler_demo.cc
new file mode 100644
index 0000000000000000000000000000000000000000..21864f75f474e90f1a2a301190334f799a4932a5
--- /dev/null
+++ b/mojo/examples/content_handler_demo/content_handler_demo.cc
@@ -0,0 +1,89 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <stdio.h>
+
+#include "mojo/public/cpp/application/application_delegate.h"
+#include "mojo/public/cpp/application/application_impl.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& url,
+ URLResponsePtr content,
+ ServiceProviderPtr service_provider) MOJO_OVERRIDE;
+
+ ContentHandlerApp* content_handler_app_;
+};
+
+class ContentHandlerApp : public ApplicationDelegate {
+ public:
+ ContentHandlerApp() : content_handler_factory_(this) {
+ }
+
+ virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE {
+ }
+
+ virtual bool ConfigureIncomingConnection(ApplicationConnection* connection)
+ MOJO_OVERRIDE {
+ connection->AddService(&content_handler_factory_);
+ return true;
+ }
+
+ void PrintResponse(ScopedDataPipeConsumerHandle body) const {
+ for (;;) {
+ char buf[512];
+ uint32_t num_bytes = sizeof(buf);
+ MojoResult result = ReadDataRaw(body.get(), buf, &num_bytes,
+ MOJO_READ_DATA_FLAG_NONE);
+ if (result == MOJO_RESULT_SHOULD_WAIT) {
+ Wait(body.get(),
+ MOJO_HANDLE_SIGNAL_READABLE,
+ MOJO_DEADLINE_INDEFINITE);
+ } else if (result == MOJO_RESULT_OK) {
+ if (fwrite(buf, num_bytes, 1, stdout) != 1) {
+ printf("\nUnexpected error writing to file\n");
+ break;
+ }
+ } else {
+ break;
+ }
+
+ printf("\n>>> EOF <<<\n");
+ }
+ }
+
+ private:
+ InterfaceFactoryImplWithContext<ContentHandlerImpl,
+ ContentHandlerApp> content_handler_factory_;
+};
+
+void ContentHandlerImpl::OnConnect(const mojo::String& url,
+ URLResponsePtr content,
+ ServiceProviderPtr service_provider) {
+ printf("ContentHandler::OnConnect - url:%s - body follows\n\n",
+ url.To<std::string>().c_str());
+ content_handler_app_->PrintResponse(content->body.Pass());
+}
+
+} // namespace examples
+
+// static
+ApplicationDelegate* ApplicationDelegate::Create() {
+ return new examples::ContentHandlerApp();
+}
+
+} // namespace mojo
« no previous file with comments | « no previous file | mojo/mojo.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698