• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_NETWORK_URL_LOADER_IMPL_H_
6 #define MOJO_SERVICES_NETWORK_URL_LOADER_IMPL_H_
7 
8 #include "base/compiler_specific.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/weak_ptr.h"
11 #include "mojo/common/handle_watcher.h"
12 #include "mojo/public/cpp/bindings/interface_impl.h"
13 #include "mojo/services/public/interfaces/network/url_loader.mojom.h"
14 #include "net/base/net_errors.h"
15 #include "net/url_request/url_request.h"
16 
17 namespace mojo {
18 class NetworkContext;
19 
20 class URLLoaderImpl : public InterfaceImpl<URLLoader>,
21                       public net::URLRequest::Delegate {
22  public:
23   explicit URLLoaderImpl(NetworkContext* context);
24   virtual ~URLLoaderImpl();
25 
26  private:
27   class PendingWriteToDataPipe;
28   class DependentIOBuffer;
29 
30   // URLLoader methods:
31   virtual void Start(
32       URLRequestPtr request,
33       const Callback<void(URLResponsePtr)>& callback) OVERRIDE;
34   virtual void FollowRedirect(
35       const Callback<void(URLResponsePtr)>& callback) OVERRIDE;
36   virtual void QueryStatus(
37       const Callback<void(URLLoaderStatusPtr)>& callback) OVERRIDE;
38 
39   // net::URLRequest::Delegate methods:
40   virtual void OnReceivedRedirect(net::URLRequest* url_request,
41                                   const net::RedirectInfo& redirect_info,
42                                   bool* defer_redirect) OVERRIDE;
43   virtual void OnResponseStarted(net::URLRequest* url_request) OVERRIDE;
44   virtual void OnReadCompleted(net::URLRequest* url_request, int bytes_read)
45       OVERRIDE;
46 
47   void SendError(
48       int error,
49       const Callback<void(URLResponsePtr)>& callback);
50   void SendResponse(URLResponsePtr response);
51   void OnResponseBodyStreamReady(MojoResult result);
52   void WaitToReadMore();
53   void ReadMore();
54   void DidRead(uint32_t num_bytes, bool completed_synchronously);
55 
56   NetworkContext* context_;
57   scoped_ptr<net::URLRequest> url_request_;
58   Callback<void(URLResponsePtr)> callback_;
59   ScopedDataPipeProducerHandle response_body_stream_;
60   scoped_refptr<PendingWriteToDataPipe> pending_write_;
61   common::HandleWatcher handle_watcher_;
62   uint32 response_body_buffer_size_;
63   bool auto_follow_redirects_;
64 
65   base::WeakPtrFactory<URLLoaderImpl> weak_ptr_factory_;
66 };
67 
68 }  // namespace mojo
69 
70 #endif  // MOJO_SERVICES_NETWORK_URL_LOADER_IMPL_H_
71