| 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/callback.h" |
| 10 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 11 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
| 12 #include "content/common/content_export.h" | 13 #include "content/common/content_export.h" |
| 14 #include "content/common/possibly_associated_interface_ptr.h" |
| 13 #include "content/common/url_loader.mojom.h" | 15 #include "content/common/url_loader.mojom.h" |
| 14 #include "content/common/url_loader_factory.mojom.h" | 16 #include "content/common/url_loader_factory.mojom.h" |
| 15 #include "content/public/common/url_loader_throttle.h" | 17 #include "content/public/common/url_loader_throttle.h" |
| 16 #include "mojo/public/cpp/bindings/binding.h" | 18 #include "mojo/public/cpp/bindings/binding.h" |
| 17 | 19 |
| 18 namespace base { | 20 namespace base { |
| 19 class SingleThreadTaskRunner; | 21 class SingleThreadTaskRunner; |
| 20 } | 22 } |
| 21 | 23 |
| 22 namespace content { | 24 namespace content { |
| 23 | 25 |
| 24 namespace mojom { | 26 namespace mojom { |
| 25 class URLLoaderFactory; | 27 class URLLoaderFactory; |
| 26 } | 28 } |
| 27 | 29 |
| 28 // ThrottlingURLLoader is a wrapper around the mojom::URLLoader[Factory] | 30 // ThrottlingURLLoader is a wrapper around the mojom::URLLoader[Factory] |
| 29 // interfaces. It applies a list of URLLoaderThrottle instances which could | 31 // interfaces. It applies a list of URLLoaderThrottle instances which could |
| 30 // defer, resume or cancel the URL loading. | 32 // defer, resume or cancel the URL loading. |
| 31 class CONTENT_EXPORT ThrottlingURLLoader : public mojom::URLLoaderClient, | 33 class CONTENT_EXPORT ThrottlingURLLoader : public mojom::URLLoaderClient, |
| 32 public URLLoaderThrottle::Delegate { | 34 public URLLoaderThrottle::Delegate { |
| 33 public: | 35 public: |
| 34 // |factory| and |client| must stay alive during the lifetime of the returned | 36 // |factory| and |client| must stay alive during the lifetime of the returned |
| 35 // object. | 37 // object. |
| 38 // Please note that the request may not start immediately since it could be |
| 39 // deferred by throttles. |
| 36 static std::unique_ptr<ThrottlingURLLoader> CreateLoaderAndStart( | 40 static std::unique_ptr<ThrottlingURLLoader> CreateLoaderAndStart( |
| 37 mojom::URLLoaderFactory* factory, | 41 mojom::URLLoaderFactory* factory, |
| 38 std::vector<std::unique_ptr<URLLoaderThrottle>> throttles, | 42 std::vector<std::unique_ptr<URLLoaderThrottle>> throttles, |
| 39 int32_t routing_id, | 43 int32_t routing_id, |
| 40 int32_t request_id, | 44 int32_t request_id, |
| 41 uint32_t options, | 45 uint32_t options, |
| 42 std::unique_ptr<ResourceRequest> url_request, | 46 const ResourceRequest& url_request, |
| 43 mojom::URLLoaderClient* client, | 47 mojom::URLLoaderClient* client, |
| 44 scoped_refptr<base::SingleThreadTaskRunner> task_runner = | 48 scoped_refptr<base::SingleThreadTaskRunner> task_runner = |
| 45 base::ThreadTaskRunnerHandle::Get()); | 49 base::ThreadTaskRunnerHandle::Get()); |
| 50 |
| 51 using StartLoaderCallback = |
| 52 base::OnceCallback<void(mojom::URLLoaderRequest request, |
| 53 mojom::URLLoaderClientPtr client)>; |
| 54 |
| 55 // Similar to the method above, but uses a |start_loader_callback| instead of |
| 56 // a mojom::URLLoaderFactory to start the loader. The callback must be safe |
| 57 // to call during the lifetime of the returned object. |
| 58 static std::unique_ptr<ThrottlingURLLoader> CreateLoaderAndStart( |
| 59 StartLoaderCallback start_loader_callback, |
| 60 std::vector<std::unique_ptr<URLLoaderThrottle>> throttles, |
| 61 const ResourceRequest& url_request, |
| 62 mojom::URLLoaderClient* client, |
| 63 scoped_refptr<base::SingleThreadTaskRunner> task_runner = |
| 64 base::ThreadTaskRunnerHandle::Get()); |
| 46 | 65 |
| 47 ~ThrottlingURLLoader() override; | 66 ~ThrottlingURLLoader() override; |
| 48 | 67 |
| 49 void FollowRedirect(); | 68 void FollowRedirect(); |
| 50 void SetPriority(net::RequestPriority priority, int32_t intra_priority_value); | 69 void SetPriority(net::RequestPriority priority, int32_t intra_priority_value); |
| 51 | 70 |
| 52 private: | 71 private: |
| 53 ThrottlingURLLoader(std::vector<std::unique_ptr<URLLoaderThrottle>> throttles, | 72 ThrottlingURLLoader(std::vector<std::unique_ptr<URLLoaderThrottle>> throttles, |
| 54 mojom::URLLoaderClient* client); | 73 mojom::URLLoaderClient* client); |
| 55 | 74 |
| 75 // Either of the two sets of arguments below is valid but not both: |
| 76 // - |factory|, |routing_id|, |request_id| and |options|; |
| 77 // - |start_loader_callback|. |
| 56 void Start(mojom::URLLoaderFactory* factory, | 78 void Start(mojom::URLLoaderFactory* factory, |
| 57 int32_t routing_id, | 79 int32_t routing_id, |
| 58 int32_t request_id, | 80 int32_t request_id, |
| 59 uint32_t options, | 81 uint32_t options, |
| 60 std::unique_ptr<ResourceRequest> url_request, | 82 StartLoaderCallback start_loader_callback, |
| 83 const ResourceRequest& url_request, |
| 61 scoped_refptr<base::SingleThreadTaskRunner> task_runner); | 84 scoped_refptr<base::SingleThreadTaskRunner> task_runner); |
| 62 | 85 |
| 86 void StartNow(mojom::URLLoaderFactory* factory, |
| 87 int32_t routing_id, |
| 88 int32_t request_id, |
| 89 uint32_t options, |
| 90 StartLoaderCallback start_loader_callback, |
| 91 const ResourceRequest& url_request, |
| 92 scoped_refptr<base::SingleThreadTaskRunner> task_runner); |
| 93 |
| 63 // mojom::URLLoaderClient implementation: | 94 // mojom::URLLoaderClient implementation: |
| 64 void OnReceiveResponse(const ResourceResponseHead& response_head, | 95 void OnReceiveResponse(const ResourceResponseHead& response_head, |
| 65 const base::Optional<net::SSLInfo>& ssl_info, | 96 const base::Optional<net::SSLInfo>& ssl_info, |
| 66 mojom::DownloadedTempFilePtr downloaded_file) override; | 97 mojom::DownloadedTempFilePtr downloaded_file) override; |
| 67 void OnReceiveRedirect(const net::RedirectInfo& redirect_info, | 98 void OnReceiveRedirect(const net::RedirectInfo& redirect_info, |
| 68 const ResourceResponseHead& response_head) override; | 99 const ResourceResponseHead& response_head) override; |
| 69 void OnDataDownloaded(int64_t data_len, int64_t encoded_data_len) override; | 100 void OnDataDownloaded(int64_t data_len, int64_t encoded_data_len) override; |
| 70 void OnUploadProgress(int64_t current_position, | 101 void OnUploadProgress(int64_t current_position, |
| 71 int64_t total_size, | 102 int64_t total_size, |
| 72 OnUploadProgressCallback ack_callback) override; | 103 OnUploadProgressCallback ack_callback) override; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 87 DEFERRED_RESPONSE | 118 DEFERRED_RESPONSE |
| 88 }; | 119 }; |
| 89 DeferredStage deferred_stage_ = DEFERRED_NONE; | 120 DeferredStage deferred_stage_ = DEFERRED_NONE; |
| 90 bool cancelled_by_throttle_ = false; | 121 bool cancelled_by_throttle_ = false; |
| 91 | 122 |
| 92 std::unique_ptr<URLLoaderThrottle> throttle_; | 123 std::unique_ptr<URLLoaderThrottle> throttle_; |
| 93 | 124 |
| 94 mojom::URLLoaderClient* forwarding_client_; | 125 mojom::URLLoaderClient* forwarding_client_; |
| 95 mojo::Binding<mojom::URLLoaderClient> client_binding_; | 126 mojo::Binding<mojom::URLLoaderClient> client_binding_; |
| 96 | 127 |
| 97 mojom::URLLoaderAssociatedPtr url_loader_; | 128 PossiblyAssociatedInterfacePtr<mojom::URLLoader> url_loader_; |
| 98 | 129 |
| 99 struct StartInfo { | 130 struct StartInfo { |
| 100 StartInfo(mojom::URLLoaderFactory* in_url_loader_factory, | 131 StartInfo(mojom::URLLoaderFactory* in_url_loader_factory, |
| 101 int32_t in_routing_id, | 132 int32_t in_routing_id, |
| 102 int32_t in_request_id, | 133 int32_t in_request_id, |
| 103 uint32_t in_options, | 134 uint32_t in_options, |
| 104 std::unique_ptr<ResourceRequest> in_url_request, | 135 StartLoaderCallback in_start_loader_callback, |
| 136 const ResourceRequest& in_url_request, |
| 105 scoped_refptr<base::SingleThreadTaskRunner> in_task_runner); | 137 scoped_refptr<base::SingleThreadTaskRunner> in_task_runner); |
| 106 ~StartInfo(); | 138 ~StartInfo(); |
| 107 | 139 |
| 108 mojom::URLLoaderFactory* url_loader_factory; | 140 mojom::URLLoaderFactory* url_loader_factory; |
| 109 int32_t routing_id; | 141 int32_t routing_id; |
| 110 int32_t request_id; | 142 int32_t request_id; |
| 111 uint32_t options; | 143 uint32_t options; |
| 112 std::unique_ptr<ResourceRequest> url_request; | 144 |
| 145 StartLoaderCallback start_loader_callback; |
| 146 |
| 147 ResourceRequest url_request; |
| 113 // |task_runner_| is used to set up |client_binding_|. | 148 // |task_runner_| is used to set up |client_binding_|. |
| 114 scoped_refptr<base::SingleThreadTaskRunner> task_runner; | 149 scoped_refptr<base::SingleThreadTaskRunner> task_runner; |
| 115 }; | 150 }; |
| 116 // Set if start is deferred. | 151 // Set if start is deferred. |
| 117 std::unique_ptr<StartInfo> start_info_; | 152 std::unique_ptr<StartInfo> start_info_; |
| 118 | 153 |
| 119 struct ResponseInfo { | 154 struct ResponseInfo { |
| 120 ResponseInfo(const ResourceResponseHead& in_response_head, | 155 ResponseInfo(const ResourceResponseHead& in_response_head, |
| 121 const base::Optional<net::SSLInfo>& in_ssl_info, | 156 const base::Optional<net::SSLInfo>& in_ssl_info, |
| 122 mojom::DownloadedTempFilePtr in_downloaded_file); | 157 mojom::DownloadedTempFilePtr in_downloaded_file); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 150 }; | 185 }; |
| 151 // Set if request is deferred and SetPriority() is called. | 186 // Set if request is deferred and SetPriority() is called. |
| 152 std::unique_ptr<PriorityInfo> priority_info_; | 187 std::unique_ptr<PriorityInfo> priority_info_; |
| 153 | 188 |
| 154 DISALLOW_COPY_AND_ASSIGN(ThrottlingURLLoader); | 189 DISALLOW_COPY_AND_ASSIGN(ThrottlingURLLoader); |
| 155 }; | 190 }; |
| 156 | 191 |
| 157 } // namespace content | 192 } // namespace content |
| 158 | 193 |
| 159 #endif // CONTENT_COMMON_THROTTLING_URL_LOADER_H_ | 194 #endif // CONTENT_COMMON_THROTTLING_URL_LOADER_H_ |
| OLD | NEW |