OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 CONTENT_BROWSER_LOADER_MOJO_ASYNC_RESOURCE_HANDLER_H_ |
| 6 #define CONTENT_BROWSER_LOADER_MOJO_ASYNC_RESOURCE_HANDLER_H_ |
| 7 |
| 8 #include <stdint.h> |
| 9 |
| 10 #include <memory> |
| 11 #include <string> |
| 12 |
| 13 #include "base/macros.h" |
| 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/timer/timer.h" |
| 16 #include "content/browser/loader/resource_handler.h" |
| 17 #include "content/common/content_export.h" |
| 18 #include "content/common/url_loader.mojom.h" |
| 19 #include "mojo/message_pump/handle_watcher.h" |
| 20 #include "mojo/public/cpp/bindings/binding.h" |
| 21 #include "net/base/io_buffer.h" |
| 22 #include "url/gurl.h" |
| 23 |
| 24 namespace net { |
| 25 class URLRequest; |
| 26 } |
| 27 |
| 28 namespace content { |
| 29 class ResourceDispatcherHostImpl; |
| 30 class ResourceRequestInfoImpl; |
| 31 struct ResourceResponse; |
| 32 |
| 33 // Used to complete an asynchronous resource request in response to resource |
| 34 // load events from the resource dispatcher host. This class is used only |
| 35 // when LoadingWithMojo runtime enabled flag is enabled. |
| 36 // |
| 37 // TODO(yhirano): Implement redirects. |
| 38 // TODO(yhirano): Implement downloading to file. |
| 39 // TODO(yhirano): Add histograms. |
| 40 // TODO(yhirano): Set zoom level. |
| 41 // TODO(yhirano): Send cached metadata. |
| 42 // |
| 43 // This class can be inherited only for tests. |
| 44 class CONTENT_EXPORT MojoAsyncResourceHandler |
| 45 : public ResourceHandler, |
| 46 public NON_EXPORTED_BASE(mojom::URLLoader) { |
| 47 public: |
| 48 MojoAsyncResourceHandler( |
| 49 net::URLRequest* request, |
| 50 ResourceDispatcherHostImpl* rdh, |
| 51 mojo::InterfaceRequest<mojom::URLLoader> mojo_request, |
| 52 mojom::URLLoaderClientPtr url_loader_client); |
| 53 ~MojoAsyncResourceHandler() override; |
| 54 |
| 55 // ResourceHandler implementation: |
| 56 bool OnRequestRedirected(const net::RedirectInfo& redirect_info, |
| 57 ResourceResponse* response, |
| 58 bool* defer) override; |
| 59 bool OnResponseStarted(ResourceResponse* response, bool* defer) override; |
| 60 bool OnWillStart(const GURL& url, bool* defer) override; |
| 61 bool OnBeforeNetworkStart(const GURL& url, bool* defer) override; |
| 62 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
| 63 int* buf_size, |
| 64 int min_size) override; |
| 65 bool OnReadCompleted(int bytes_read, bool* defer) override; |
| 66 void OnResponseCompleted(const net::URLRequestStatus& status, |
| 67 const std::string& security_info, |
| 68 bool* defer) override; |
| 69 void OnDataDownloaded(int bytes_downloaded) override; |
| 70 |
| 71 ResourceRequestInfoImpl* GetRequestInfoForTesting() { |
| 72 return GetRequestInfo(); |
| 73 } |
| 74 // mojom::URLLoader implementation |
| 75 void FollowRedirect() override; |
| 76 void Cancel() override; |
| 77 |
| 78 static void SetAllocationSizeForTesting(size_t size); |
| 79 static constexpr size_t kDefaultAllocationSize = 512 * 1024; |
| 80 |
| 81 protected: |
| 82 // These functions can be overriden only for tests. |
| 83 virtual MojoResult BeginWrite(void** data, uint32_t* available); |
| 84 virtual MojoResult EndWrite(uint32_t written); |
| 85 |
| 86 private: |
| 87 class SharedWriter; |
| 88 class WriterIOBuffer; |
| 89 |
| 90 // This funcion copies data stored in |buffer_| to |shared_writer_| and |
| 91 // resets |buffer_| to a WriterIOBuffer when all bytes are copied. Returns |
| 92 // true when done successfully. |
| 93 bool CopyReadData(bool* defer); |
| 94 // Allocates a WriterIOBuffer and set it to |*buf|. Returns true when done |
| 95 // successfully. |
| 96 bool AllocateBuffer(scoped_refptr<net::IOBufferWithSize>* buf, bool* defer); |
| 97 |
| 98 void Resume(); |
| 99 void OnDefer(); |
| 100 bool CheckForSufficientResource(); |
| 101 void OnWritable(MojoResult result); |
| 102 |
| 103 ResourceDispatcherHostImpl* rdh_; |
| 104 mojo::Binding<mojom::URLLoader> binding_; |
| 105 |
| 106 bool has_checked_for_sufficient_resources_ = false; |
| 107 bool sent_received_response_message_ = false; |
| 108 bool is_using_io_buffer_not_from_writer_ = false; |
| 109 base::TimeTicks response_started_ticks_; |
| 110 |
| 111 mojo::common::HandleWatcher handle_watcher_; |
| 112 std::unique_ptr<mojom::URLLoader> url_loader_; |
| 113 mojom::URLLoaderClientPtr url_loader_client_; |
| 114 scoped_refptr<net::IOBufferWithSize> buffer_; |
| 115 size_t buffer_offset_ = 0; |
| 116 size_t buffer_bytes_read_ = 0; |
| 117 scoped_refptr<SharedWriter> shared_writer_; |
| 118 |
| 119 DISALLOW_COPY_AND_ASSIGN(MojoAsyncResourceHandler); |
| 120 }; |
| 121 |
| 122 } // namespace content |
| 123 |
| 124 #endif // CONTENT_BROWSER_LOADER_MOJO_ASYNC_RESOURCE_HANDLER_H_ |
OLD | NEW |