Chromium Code Reviews| Index: examples/http_handler/http_handler.cc |
| diff --git a/examples/http_handler/http_handler.cc b/examples/http_handler/http_handler.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c7c80edf1c223f6aca1ac4be6f2b0fa6754f452d |
| --- /dev/null |
| +++ b/examples/http_handler/http_handler.cc |
| @@ -0,0 +1,59 @@ |
| +// 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 "base/bind.h" |
| +#include "mojo/application/application_runner_chromium.h" |
| +#include "mojo/public/c/system/main.h" |
| +#include "mojo/public/cpp/application/application_connection.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/public/cpp/application/service_provider_impl.h" |
| +#include "services/http_server/public/http_server.mojom.h" |
| + |
| +namespace mojo { |
| +namespace examples { |
| + |
| +// This is an example of a self-contained HTTP handler. It uses the HTTP Server |
| +// service to handle the HTTP protocol details, and just contains the logic for |
| +// handling its registered urls. |
| +class HttpHandler : public ApplicationDelegate, |
| + public HttpServerClient { |
| + public: |
| + HttpHandler() {} |
| + virtual ~HttpHandler() {} |
|
yzshen1
2014/11/12 17:38:33
nit: I think the style guide now prefers "~HttpHan
jam
2014/11/12 20:48:40
Done.
|
| + |
| + private: |
| + // ApplicationDelegate: |
| + void Initialize(ApplicationImpl* app) override { |
| + app->ConnectToService("mojo:http_server", &http_server_service_); |
| + |
| + http_server_service_.set_client(this); |
| + http_server_service_->AddHandler( |
| + "/test", |
| + base::Bind(&HttpHandler::AddHandlerCallback, base::Unretained(this))); |
| + } |
| + |
| + // HttpServerClient: |
| + void OnHandleRequest(HttpRequestPtr request, |
| + const Callback<void(HttpResponsePtr)>& callback) { |
|
yzshen1
2014/11/12 17:38:33
nit: override
jam
2014/11/12 20:48:40
Done.
|
| + http_server_service_->CreateResponse(200, "Hello World", callback); |
| + } |
| + |
| + void AddHandlerCallback(bool result) { |
| + CHECK(result); |
| + } |
| + |
| + HttpServerServicePtr http_server_service_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(HttpHandler); |
| +}; |
| + |
| +} // namespace examples |
| +} // namespace mojo |
| + |
| +MojoResult MojoMain(MojoHandle shell_handle) { |
| + mojo::ApplicationRunnerChromium runner(new mojo::examples::HttpHandler()); |
| + return runner.Run(shell_handle); |
| +} |