summaryrefslogtreecommitdiffstats
path: root/mojo/examples/wget
diff options
context:
space:
mode:
authordarin@chromium.org <darin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-07-11 04:14:54 +0000
committerdarin@chromium.org <darin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-07-11 04:14:54 +0000
commit91025cc6f9b1a97696de8b6d59fd141579eb7b61 (patch)
treec4e93b777fe54cd3b02820116c7a4a16f2f1387b /mojo/examples/wget
parented3cf8a2e596fe4bd5c8e94ef93add01ad8bc0cd (diff)
downloadchromium_src-91025cc6f9b1a97696de8b6d59fd141579eb7b61.zip
chromium_src-91025cc6f9b1a97696de8b6d59fd141579eb7b61.tar.gz
chromium_src-91025cc6f9b1a97696de8b6d59fd141579eb7b61.tar.bz2
Mojo: Refactor URLLoader interface.
This change simplifies the URLLoader interface, eliminating the URLLoaderClient interface. The URLResponse now includes the DataPipeConsumerHandle for the response body stream as well as information about a possible redirect response. One nice thing about this change is that you can now pass around the URLResponsePtr without also having to pass around the DataPipeConsumerHandle for the response body stream. This didn't enable me to eliminate ResponseDetails as I think that structure should include not only the URLResponse but also the URLLoader used to generate the response. (That enables the LaunchInstance to get destroyed immediately after delegating the response to the handler app as it no longer needs to stick around to keep the URLLoader alive.) The recipient of the URLLoader might be interested in calling the new QueryStatus method to find out more information about the URL load (e.g., Blink wants to know the encoded size of a response body, which may not be available at the time when response headers are received). Review URL: https://codereview.chromium.org/373373002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@282535 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'mojo/examples/wget')
-rw-r--r--mojo/examples/wget/wget.cc110
1 files changed, 50 insertions, 60 deletions
diff --git a/mojo/examples/wget/wget.cc b/mojo/examples/wget/wget.cc
index 9ec7e5c..55a8cb8 100644
--- a/mojo/examples/wget/wget.cc
+++ b/mojo/examples/wget/wget.cc
@@ -6,68 +6,29 @@
#include "mojo/public/cpp/application/application_delegate.h"
#include "mojo/public/cpp/application/application_impl.h"
+#include "mojo/public/cpp/utility/run_loop.h"
#include "mojo/services/public/interfaces/network/network_service.mojom.h"
#include "mojo/services/public/interfaces/network/url_loader.mojom.h"
namespace mojo {
namespace examples {
+namespace {
-class WGetApp : public ApplicationDelegate, public URLLoaderClient {
+class ResponsePrinter {
public:
- virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE {
- app->ConnectToService("mojo:mojo_network_service", &network_service_);
- Start();
- }
-
- private:
- virtual void OnReceivedRedirect(URLResponsePtr response,
- const String& new_url,
- const String& new_method) MOJO_OVERRIDE {
- PrintResponse(response);
- }
-
- virtual void OnReceivedResponse(URLResponsePtr response) MOJO_OVERRIDE {
- PrintResponse(response);
- PrintResponseBody();
- Start();
- }
-
- virtual void OnReceivedError(NetworkErrorPtr error) MOJO_OVERRIDE {
- printf("Got error: %d (%s)\n",
- error->code, error->description.get().c_str());
- }
-
- virtual void OnReceivedEndOfResponseBody() MOJO_OVERRIDE {
- // Ignored.
- }
-
- void Start() {
- std::string url = PromptForURL();
- printf("Loading: %s\n", url.c_str());
-
- network_service_->CreateURLLoader(Get(&url_loader_));
- url_loader_.set_client(this);
-
- URLRequestPtr request(URLRequest::New());
- request->url = url;
- request->method = "GET";
- request->auto_follow_redirects = true;
-
- DataPipe data_pipe;
- response_body_stream_ = data_pipe.consumer_handle.Pass();
+ void Run(URLResponsePtr response) const {
+ if (response->error) {
+ printf("Got error: %d (%s)\n",
+ response->error->code, response->error->description.get().c_str());
+ } else {
+ PrintResponse(response);
+ PrintResponseBody(response->body.Pass());
+ }
- url_loader_->Start(request.Pass(), data_pipe.producer_handle.Pass());
+ RunLoop::current()->Quit(); // All done!
}
- std::string PromptForURL() {
- printf("Enter URL> ");
- char buf[1024];
- if (scanf("%1023s", buf) != 1)
- buf[0] = '\0';
- return buf;
- }
-
- void PrintResponse(const URLResponsePtr& response) {
+ void PrintResponse(const URLResponsePtr& response) const {
printf(">>> Headers <<< \n");
printf(" %s\n", response->status_line.get().c_str());
if (response->headers) {
@@ -76,20 +37,17 @@ class WGetApp : public ApplicationDelegate, public URLLoaderClient {
}
}
- void PrintResponseBody() {
+ void PrintResponseBody(ScopedDataPipeConsumerHandle body) const {
// Read response body in blocking fashion.
printf(">>> Body <<<\n");
for (;;) {
char buf[512];
uint32_t num_bytes = sizeof(buf);
- MojoResult result = ReadDataRaw(
- response_body_stream_.get(),
- buf,
- &num_bytes,
- MOJO_READ_DATA_FLAG_NONE);
+ MojoResult result = ReadDataRaw(body.get(), buf, &num_bytes,
+ MOJO_READ_DATA_FLAG_NONE);
if (result == MOJO_RESULT_SHOULD_WAIT) {
- Wait(response_body_stream_.get(),
+ Wait(body.get(),
MOJO_HANDLE_SIGNAL_READABLE,
MOJO_DEADLINE_INDEFINITE);
} else if (result == MOJO_RESULT_OK) {
@@ -104,10 +62,42 @@ class WGetApp : public ApplicationDelegate, public URLLoaderClient {
printf("\n>>> EOF <<<\n");
}
+};
+
+} // namespace
+
+class WGetApp : public ApplicationDelegate {
+ public:
+ virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE {
+ app->ConnectToService("mojo:mojo_network_service", &network_service_);
+ Start();
+ }
+
+ private:
+ void Start() {
+ std::string url = PromptForURL();
+ printf("Loading: %s\n", url.c_str());
+
+ network_service_->CreateURLLoader(Get(&url_loader_));
+
+ URLRequestPtr request(URLRequest::New());
+ request->url = url;
+ request->method = "GET";
+ request->auto_follow_redirects = true;
+
+ url_loader_->Start(request.Pass(), ResponsePrinter());
+ }
+
+ std::string PromptForURL() {
+ printf("Enter URL> ");
+ char buf[1024];
+ if (scanf("%1023s", buf) != 1)
+ buf[0] = '\0';
+ return buf;
+ }
NetworkServicePtr network_service_;
URLLoaderPtr url_loader_;
- ScopedDataPipeConsumerHandle response_body_stream_;
};
} // namespace examples