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