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#ifndef MOJO_SERVICES_HTML_VIEWER_WEBURLLOADER_IMPL_H_
6#define MOJO_SERVICES_HTML_VIEWER_WEBURLLOADER_IMPL_H_
7
8#include "base/memory/weak_ptr.h"
9#include "mojo/common/handle_watcher.h"
10#include "mojo/services/public/interfaces/network/url_loader.mojom.h"
11#include "third_party/WebKit/public/platform/WebURLLoader.h"
12#include "third_party/WebKit/public/platform/WebURLRequest.h"
13
14namespace mojo {
15class NetworkService;
16
17// The concrete type of WebURLRequest::ExtraData.
18class WebURLRequestExtraData : public blink::WebURLRequest::ExtraData {
19 public:
20  WebURLRequestExtraData();
21  virtual ~WebURLRequestExtraData();
22
23  URLResponsePtr synthetic_response;
24};
25
26class WebURLLoaderImpl : public blink::WebURLLoader {
27 public:
28  explicit WebURLLoaderImpl(NetworkService* network_service);
29
30 private:
31  virtual ~WebURLLoaderImpl();
32
33  // blink::WebURLLoader methods:
34  virtual void loadSynchronously(
35      const blink::WebURLRequest& request, blink::WebURLResponse& response,
36      blink::WebURLError& error, blink::WebData& data) OVERRIDE;
37  virtual void loadAsynchronously(
38      const blink::WebURLRequest&, blink::WebURLLoaderClient* client) OVERRIDE;
39  virtual void cancel() OVERRIDE;
40  virtual void setDefersLoading(bool defers_loading) OVERRIDE;
41
42  void OnReceivedResponse(URLResponsePtr response);
43  void OnReceivedError(URLResponsePtr response);
44  void OnReceivedRedirect(URLResponsePtr response);
45  void ReadMore();
46  void WaitToReadMore();
47  void OnResponseBodyStreamReady(MojoResult result);
48
49  blink::WebURLLoaderClient* client_;
50  GURL url_;
51  URLLoaderPtr url_loader_;
52  ScopedDataPipeConsumerHandle response_body_stream_;
53  common::HandleWatcher handle_watcher_;
54
55  base::WeakPtrFactory<WebURLLoaderImpl> weak_factory_;
56
57  DISALLOW_COPY_AND_ASSIGN(WebURLLoaderImpl);
58};
59
60}  // namespace mojo
61
62#endif  // MOJO_SERVICES_HTML_VIEWER_WEBURLLOADER_IMPL_H_
63