1 // Copyright (c) 2017 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_APPCACHE_APPCACHE_UPDATE_URL_LOADER_REQUEST_H_ 6 #define CONTENT_BROWSER_APPCACHE_APPCACHE_UPDATE_URL_LOADER_REQUEST_H_ 7 8 #include <stddef.h> 9 #include <stdint.h> 10 #include <memory> 11 #include <string> 12 13 #include "base/macros.h" 14 #include "content/browser/appcache/appcache_update_job.h" 15 #include "mojo/public/cpp/bindings/receiver.h" 16 #include "mojo/public/cpp/bindings/remote.h" 17 #include "mojo/public/cpp/system/simple_watcher.h" 18 #include "net/base/io_buffer.h" 19 #include "services/network/public/cpp/net_adapters.h" 20 #include "services/network/public/cpp/resource_request.h" 21 #include "services/network/public/mojom/url_loader.mojom.h" 22 #include "services/network/public/mojom/url_response_head.mojom-forward.h" 23 24 namespace net { 25 class HttpResponseInfo; 26 } 27 28 namespace content { 29 30 // URLLoaderClient subclass for the UpdateRequestBase class. Provides 31 // functionality to update the AppCache using functionality provided by the 32 // network URL loader. 33 class AppCacheUpdateJob::UpdateURLLoaderRequest 34 : public network::mojom::URLLoaderClient { 35 public: 36 UpdateURLLoaderRequest(base::WeakPtr<StoragePartitionImpl> partition, 37 const GURL& url, 38 int buffer_size, 39 URLFetcher* fetcher); 40 ~UpdateURLLoaderRequest() override; 41 42 // This method is called to start the request. 43 void Start(); 44 45 // Sets all extra request headers. Any extra request headers set by other 46 // methods are overwritten by this method. This method may only be called 47 // before Start() is called. It is an error to call it later. 48 void SetExtraRequestHeaders(const net::HttpRequestHeaders& headers); 49 50 // Returns the request URL. 51 GURL GetURL() const; 52 53 // Sets flags which control the request load. e.g. if it can be loaded 54 // from cache, etc. 55 void SetLoadFlags(int flags); 56 57 // Gets the load flags on the request. 58 int GetLoadFlags() const; 59 60 // Get the mime type. This method may only be called after the response was 61 // started. 62 std::string GetMimeType() const; 63 64 // Cookie policy. 65 void SetSiteForCookies(const GURL& site_for_cookies); 66 67 // Sets the origin of the context which initiated the request. 68 void SetInitiator(const base::Optional<url::Origin>& initiator); 69 70 // Get all response headers, as a HttpResponseHeaders object. See comments 71 // in HttpResponseHeaders class as to the format of the data. 72 net::HttpResponseHeaders* GetResponseHeaders() const; 73 74 // Returns the HTTP response code (e.g., 200, 404, and so on). This method 75 // may only be called once the delegate's OnResponseStarted method has been 76 // called. For non-HTTP requests, this method returns -1. 77 int GetResponseCode() const; 78 79 // Fetch the X-AppCache-Allowed response header and return the scope based 80 // on the header. 81 std::string GetAppCacheAllowedHeader() const; 82 83 // Get the HTTP response info in its entirety. 84 const net::HttpResponseInfo& GetResponseInfo() const; 85 86 // Initiates an asynchronous read. Multiple concurrent reads are not 87 // supported. 88 void Read(); 89 90 // This method may be called at any time after Start() has been called to 91 // cancel the request. 92 // Returns net::ERR_ABORTED or any applicable net error. 93 int Cancel(); 94 95 // Set fetch metadata headers ( only `Sec-Fetch-Dest` for now ) for secure 96 // resources. 97 // TODO(lyf): Remove this function after moving `Sec-Fetch-Dest` to the 98 // network service. SetFetchMetadataHeaders()99 void SetFetchMetadataHeaders() { 100 if (GetURL().SchemeIsCryptographic()) 101 request_.headers.SetHeader("Sec-Fetch-Dest", "empty"); 102 } 103 104 // network::mojom::URLLoaderClient implementation. 105 // These methods are called by the network loader. 106 void OnReceiveResponse( 107 network::mojom::URLResponseHeadPtr response_head) override; 108 void OnReceiveRedirect( 109 const net::RedirectInfo& redirect_info, 110 network::mojom::URLResponseHeadPtr response_head) override; 111 void OnUploadProgress(int64_t current_position, 112 int64_t total_size, 113 OnUploadProgressCallback ack_callback) override; 114 void OnReceiveCachedMetadata(mojo_base::BigBuffer data) override; 115 void OnTransferSizeUpdated(int32_t transfer_size_diff) override; 116 void OnStartLoadingResponseBody( 117 mojo::ScopedDataPipeConsumerHandle body) override; 118 void OnComplete(const network::URLLoaderCompletionStatus& status) override; 119 120 private: 121 // Helper function to initiate an asynchronous read on the data pipe. 122 void StartReading(MojoResult unused); 123 124 // Helper function to setup the data pipe watcher to start reading from 125 // the pipe. We need to do this when the data pipe is available and there is 126 // a pending read. 127 void MaybeStartReading(); 128 129 URLFetcher* fetcher_; 130 // |partition_| is used to get the network URLLoader. 131 base::WeakPtr<StoragePartitionImpl> partition_; 132 133 network::ResourceRequest request_; 134 network::mojom::URLResponseHeadPtr response_; 135 network::URLLoaderCompletionStatus response_status_; 136 // Response details. 137 std::unique_ptr<net::HttpResponseInfo> http_response_info_; 138 // Binds the URLLoaderClient interface to the channel. 139 mojo::Receiver<network::mojom::URLLoaderClient> client_receiver_{this}; 140 // The network URL loader. 141 mojo::Remote<network::mojom::URLLoader> url_loader_; 142 // Caller buffer size. 143 int buffer_size_; 144 // The mojo data pipe. 145 mojo::ScopedDataPipeConsumerHandle handle_; 146 // Used to watch the data pipe to initiate reads. 147 mojo::SimpleWatcher handle_watcher_; 148 // Set to true when the caller issues a read request. We set it to false in 149 // the StartReading() function when the mojo BeginReadData API returns a 150 // value indicating one of the following: 151 // 1. Data is available. 152 // 2. End of data has been reached. 153 // 3. Error. 154 // Please look at the StartReading() function for details. 155 bool read_requested_; 156 // Adapter for transferring data from a mojo data pipe to net. 157 scoped_refptr<network::MojoToNetPendingBuffer> pending_read_; 158 159 DISALLOW_COPY_AND_ASSIGN(UpdateURLLoaderRequest); 160 }; 161 162 } // namespace content 163 164 #endif // CONTENT_BROWSER_APPCACHE_APPCACHE_UPDATE_URL_LOADER_REQUEST_H_ 165