| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_COMMON_THROTTLING_URL_LOADER_H_ | 5 #ifndef CONTENT_COMMON_THROTTLING_URL_LOADER_H_ |
| 6 #define CONTENT_COMMON_THROTTLING_URL_LOADER_H_ | 6 #define CONTENT_COMMON_THROTTLING_URL_LOADER_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/threading/thread_task_runner_handle.h" |
| 10 #include "content/common/content_export.h" | 12 #include "content/common/content_export.h" |
| 11 #include "content/common/url_loader.mojom.h" | 13 #include "content/common/url_loader.mojom.h" |
| 12 #include "content/common/url_loader_factory.mojom.h" | 14 #include "content/common/url_loader_factory.mojom.h" |
| 13 #include "content/public/common/url_loader_throttle.h" | 15 #include "content/public/common/url_loader_throttle.h" |
| 14 #include "mojo/public/cpp/bindings/binding.h" | 16 #include "mojo/public/cpp/bindings/binding.h" |
| 15 | 17 |
| 18 namespace base { |
| 19 class SingleThreadTaskRunner; |
| 20 } |
| 21 |
| 16 namespace content { | 22 namespace content { |
| 17 | 23 |
| 18 namespace mojom { | 24 namespace mojom { |
| 19 class URLLoaderFactory; | 25 class URLLoaderFactory; |
| 20 } | 26 } |
| 21 | 27 |
| 22 // ThrottlingURLLoader is a wrapper around the mojom::URLLoader[Factory] | 28 // ThrottlingURLLoader is a wrapper around the mojom::URLLoader[Factory] |
| 23 // interfaces. It applies a list of URLLoaderThrottle instances which could | 29 // interfaces. It applies a list of URLLoaderThrottle instances which could |
| 24 // defer, resume or cancel the URL loading. | 30 // defer, resume or cancel the URL loading. |
| 25 class CONTENT_EXPORT ThrottlingURLLoader : public mojom::URLLoaderClient, | 31 class CONTENT_EXPORT ThrottlingURLLoader : public mojom::URLLoaderClient, |
| 26 public URLLoaderThrottle::Delegate { | 32 public URLLoaderThrottle::Delegate { |
| 27 public: | 33 public: |
| 28 // |factory| and |client| must stay alive during the lifetime of the returned | 34 // |factory| and |client| must stay alive during the lifetime of the returned |
| 29 // object. | 35 // object. |
| 30 static std::unique_ptr<ThrottlingURLLoader> CreateLoaderAndStart( | 36 static std::unique_ptr<ThrottlingURLLoader> CreateLoaderAndStart( |
| 31 mojom::URLLoaderFactory* factory, | 37 mojom::URLLoaderFactory* factory, |
| 32 std::vector<std::unique_ptr<URLLoaderThrottle>> throttles, | 38 std::vector<std::unique_ptr<URLLoaderThrottle>> throttles, |
| 33 int32_t routing_id, | 39 int32_t routing_id, |
| 34 int32_t request_id, | 40 int32_t request_id, |
| 35 uint32_t options, | 41 uint32_t options, |
| 36 std::unique_ptr<ResourceRequest> url_request, | 42 std::unique_ptr<ResourceRequest> url_request, |
| 37 mojom::URLLoaderClient* client); | 43 mojom::URLLoaderClient* client, |
| 44 scoped_refptr<base::SingleThreadTaskRunner> task_runner = |
| 45 base::ThreadTaskRunnerHandle::Get()); |
| 38 | 46 |
| 39 ~ThrottlingURLLoader() override; | 47 ~ThrottlingURLLoader() override; |
| 40 | 48 |
| 41 void FollowRedirect(); | 49 void FollowRedirect(); |
| 42 void SetPriority(net::RequestPriority priority, int32_t intra_priority_value); | 50 void SetPriority(net::RequestPriority priority, int32_t intra_priority_value); |
| 43 | 51 |
| 44 private: | 52 private: |
| 45 ThrottlingURLLoader(std::vector<std::unique_ptr<URLLoaderThrottle>> throttles, | 53 ThrottlingURLLoader(std::vector<std::unique_ptr<URLLoaderThrottle>> throttles, |
| 46 mojom::URLLoaderClient* client); | 54 mojom::URLLoaderClient* client); |
| 47 | 55 |
| 48 void Start(mojom::URLLoaderFactory* factory, | 56 void Start(mojom::URLLoaderFactory* factory, |
| 49 int32_t routing_id, | 57 int32_t routing_id, |
| 50 int32_t request_id, | 58 int32_t request_id, |
| 51 uint32_t options, | 59 uint32_t options, |
| 52 std::unique_ptr<ResourceRequest> url_request); | 60 std::unique_ptr<ResourceRequest> url_request, |
| 61 scoped_refptr<base::SingleThreadTaskRunner> task_runner); |
| 53 | 62 |
| 54 // mojom::URLLoaderClient implementation: | 63 // mojom::URLLoaderClient implementation: |
| 55 void OnReceiveResponse(const ResourceResponseHead& response_head, | 64 void OnReceiveResponse(const ResourceResponseHead& response_head, |
| 56 const base::Optional<net::SSLInfo>& ssl_info, | 65 const base::Optional<net::SSLInfo>& ssl_info, |
| 57 mojom::DownloadedTempFilePtr downloaded_file) override; | 66 mojom::DownloadedTempFilePtr downloaded_file) override; |
| 58 void OnReceiveRedirect(const net::RedirectInfo& redirect_info, | 67 void OnReceiveRedirect(const net::RedirectInfo& redirect_info, |
| 59 const ResourceResponseHead& response_head) override; | 68 const ResourceResponseHead& response_head) override; |
| 60 void OnDataDownloaded(int64_t data_len, int64_t encoded_data_len) override; | 69 void OnDataDownloaded(int64_t data_len, int64_t encoded_data_len) override; |
| 61 void OnUploadProgress(int64_t current_position, | 70 void OnUploadProgress(int64_t current_position, |
| 62 int64_t total_size, | 71 int64_t total_size, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 80 DeferredStage deferred_stage_ = DEFERRED_NONE; | 89 DeferredStage deferred_stage_ = DEFERRED_NONE; |
| 81 bool cancelled_by_throttle_ = false; | 90 bool cancelled_by_throttle_ = false; |
| 82 | 91 |
| 83 std::unique_ptr<URLLoaderThrottle> throttle_; | 92 std::unique_ptr<URLLoaderThrottle> throttle_; |
| 84 | 93 |
| 85 mojom::URLLoaderClient* forwarding_client_; | 94 mojom::URLLoaderClient* forwarding_client_; |
| 86 mojo::Binding<mojom::URLLoaderClient> client_binding_; | 95 mojo::Binding<mojom::URLLoaderClient> client_binding_; |
| 87 | 96 |
| 88 mojom::URLLoaderAssociatedPtr url_loader_; | 97 mojom::URLLoaderAssociatedPtr url_loader_; |
| 89 | 98 |
| 99 struct StartInfo { |
| 100 StartInfo(mojom::URLLoaderFactory* in_url_loader_factory, |
| 101 int32_t in_routing_id, |
| 102 int32_t in_request_id, |
| 103 uint32_t in_options, |
| 104 std::unique_ptr<ResourceRequest> in_url_request, |
| 105 scoped_refptr<base::SingleThreadTaskRunner> in_task_runner); |
| 106 ~StartInfo(); |
| 107 |
| 108 mojom::URLLoaderFactory* url_loader_factory; |
| 109 int32_t routing_id; |
| 110 int32_t request_id; |
| 111 uint32_t options; |
| 112 std::unique_ptr<ResourceRequest> url_request; |
| 113 // |task_runner_| is used to set up |client_binding_|. |
| 114 scoped_refptr<base::SingleThreadTaskRunner> task_runner; |
| 115 }; |
| 90 // Set if start is deferred. | 116 // Set if start is deferred. |
| 91 mojom::URLLoaderFactory* url_loader_factory_ = nullptr; | 117 std::unique_ptr<StartInfo> start_info_; |
| 92 int32_t routing_id_ = -1; | |
| 93 int32_t request_id_ = -1; | |
| 94 uint32_t options_ = mojom::kURLLoadOptionNone; | |
| 95 std::unique_ptr<ResourceRequest> url_request_; | |
| 96 | 118 |
| 97 // Set if either response or redirect is deferred. | 119 struct ResponseInfo { |
| 98 ResourceResponseHead response_head_; | 120 ResponseInfo(const ResourceResponseHead& in_response_head, |
| 121 const base::Optional<net::SSLInfo>& in_ssl_info, |
| 122 mojom::DownloadedTempFilePtr in_downloaded_file); |
| 123 ~ResponseInfo(); |
| 99 | 124 |
| 125 ResourceResponseHead response_head; |
| 126 base::Optional<net::SSLInfo> ssl_info; |
| 127 mojom::DownloadedTempFilePtr downloaded_file; |
| 128 }; |
| 100 // Set if response is deferred. | 129 // Set if response is deferred. |
| 101 base::Optional<net::SSLInfo> ssl_info_; | 130 std::unique_ptr<ResponseInfo> response_info_; |
| 102 mojom::DownloadedTempFilePtr downloaded_file_; | |
| 103 | 131 |
| 132 struct RedirectInfo { |
| 133 RedirectInfo(const net::RedirectInfo& in_redirect_info, |
| 134 const ResourceResponseHead& in_response_head); |
| 135 ~RedirectInfo(); |
| 136 |
| 137 net::RedirectInfo redirect_info; |
| 138 ResourceResponseHead response_head; |
| 139 }; |
| 104 // Set if redirect is deferred. | 140 // Set if redirect is deferred. |
| 105 net::RedirectInfo redirect_info_; | 141 std::unique_ptr<RedirectInfo> redirect_info_; |
| 106 | 142 |
| 143 struct PriorityInfo { |
| 144 PriorityInfo(net::RequestPriority in_priority, |
| 145 int32_t in_intra_priority_value); |
| 146 ~PriorityInfo(); |
| 147 |
| 148 net::RequestPriority priority; |
| 149 int32_t intra_priority_value; |
| 150 }; |
| 107 // Set if request is deferred and SetPriority() is called. | 151 // Set if request is deferred and SetPriority() is called. |
| 108 bool set_priority_cached_ = false; | 152 std::unique_ptr<PriorityInfo> priority_info_; |
| 109 net::RequestPriority priority_ = net::MINIMUM_PRIORITY; | |
| 110 int32_t intra_priority_value_ = 0; | |
| 111 | 153 |
| 112 DISALLOW_COPY_AND_ASSIGN(ThrottlingURLLoader); | 154 DISALLOW_COPY_AND_ASSIGN(ThrottlingURLLoader); |
| 113 }; | 155 }; |
| 114 | 156 |
| 115 } // namespace content | 157 } // namespace content |
| 116 | 158 |
| 117 #endif // CONTENT_COMMON_THROTTLING_URL_LOADER_H_ | 159 #endif // CONTENT_COMMON_THROTTLING_URL_LOADER_H_ |
| OLD | NEW |