1 // Copyright 2018 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 CHROME_BROWSER_CHROMEOS_WILCO_DTC_SUPPORTD_WILCO_DTC_SUPPORTD_WEB_REQUEST_SERVICE_H_ 6 #define CHROME_BROWSER_CHROMEOS_WILCO_DTC_SUPPORTD_WILCO_DTC_SUPPORTD_WEB_REQUEST_SERVICE_H_ 7 8 #include <memory> 9 #include <string> 10 #include <vector> 11 12 #include "base/containers/queue.h" 13 #include "base/macros.h" 14 #include "base/strings/string_piece.h" 15 #include "chrome/services/wilco_dtc_supportd/public/mojom/wilco_dtc_supportd.mojom.h" 16 #include "url/gurl.h" 17 18 namespace network { 19 20 struct ResourceRequest; 21 class SimpleURLLoader; 22 23 } // namespace network 24 25 namespace chromeos { 26 27 // Max number of supported pending web requests. 28 extern const int kWilcoDtcSupportdWebRequestQueueMaxSize; 29 30 // Max size of web response body in bytes. 31 extern const int kWilcoDtcSupportdWebResponseMaxSizeInBytes; 32 33 class WilcoDtcSupportdNetworkContext; 34 35 // This class manages and performs web requests initiated by 36 // wilco_dtc_supportd_processor. This service performs only one request at a 37 // time and queues additional incoming requests. It can handle the limited 38 // number of queued web requests. If the web request queue overflows, new web 39 // requests fail with kNetworkError. 40 class WilcoDtcSupportdWebRequestService final { 41 public: 42 using PerformWebRequestCallback = base::OnceCallback<void( 43 wilco_dtc_supportd::mojom::WilcoDtcSupportdWebRequestStatus, 44 int, 45 mojo::ScopedHandle)>; 46 47 explicit WilcoDtcSupportdWebRequestService( 48 std::unique_ptr<WilcoDtcSupportdNetworkContext> network_context); 49 ~WilcoDtcSupportdWebRequestService(); 50 51 // Performs web request. The response is returned by |callback| which is 52 // guaranteed to be called. The requests, that were not complete in lifetime 53 // of the service, will be canceled and the |callback| will be executed in 54 // the destructor and fail with kNetworkError. 55 void PerformRequest( 56 wilco_dtc_supportd::mojom::WilcoDtcSupportdWebRequestHttpMethod 57 http_method, 58 GURL url, 59 std::vector<base::StringPiece> headers, 60 std::string request_body, 61 PerformWebRequestCallback callback); 62 request_queue_size_for_testing()63 int request_queue_size_for_testing() { return request_queue_.size(); } 64 set_allow_local_requests_for_testing(bool allow)65 void set_allow_local_requests_for_testing(bool allow) { 66 allow_local_requests_ = allow; 67 } 68 69 private: 70 struct WebRequest { 71 WebRequest(); 72 ~WebRequest(); 73 74 std::unique_ptr<network::ResourceRequest> request; 75 std::string request_body; 76 PerformWebRequestCallback callback; 77 }; 78 // Starts the next web request from the web request queue if there is no 79 // |active_request_| and |request_queue_| is not empty. 80 void MaybeStartNextRequest(); 81 82 // On the |active_request_| completed. Runs the |active_request_.callback|. 83 // Starts the next web request if there is any in the |request_queue_|. 84 void OnRequestComplete(std::unique_ptr<std::string> response_body); 85 86 // Configures whether it's allowed to perform web requests to the local host 87 // URL. Change only for tests. 88 bool allow_local_requests_ = false; 89 90 std::unique_ptr<WilcoDtcSupportdNetworkContext> network_context_; 91 // Should be reset for every web request. 92 std::unique_ptr<network::SimpleURLLoader> url_loader_; 93 94 base::queue<std::unique_ptr<WebRequest>> request_queue_; 95 std::unique_ptr<WebRequest> active_request_; 96 97 DISALLOW_COPY_AND_ASSIGN(WilcoDtcSupportdWebRequestService); 98 }; 99 100 } // namespace chromeos 101 102 #endif // CHROME_BROWSER_CHROMEOS_WILCO_DTC_SUPPORTD_WILCO_DTC_SUPPORTD_WEB_REQUEST_SERVICE_H_ 103