1 // Copyright 2016 The Chromium Authors 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 NET_HTTP_HTTP_CACHE_LOOKUP_MANAGER_H_ 6 #define NET_HTTP_HTTP_CACHE_LOOKUP_MANAGER_H_ 7 8 #include "base/memory/raw_ptr.h" 9 #include "net/base/net_export.h" 10 #include "net/http/http_cache.h" 11 #include "net/http/http_cache_transaction.h" 12 #include "net/spdy/server_push_delegate.h" 13 14 namespace net { 15 16 struct HttpRequestInfo; 17 18 // An implementation of ServerPushDelegate that issues an HttpCache::Transaction 19 // to lookup whether the response to the pushed URL is cached and cancel the 20 // push in that case. 21 class NET_EXPORT_PRIVATE HttpCacheLookupManager : public ServerPushDelegate { 22 public: 23 // |http_cache| MUST outlive the HttpCacheLookupManager. 24 explicit HttpCacheLookupManager(HttpCache* http_cache); 25 ~HttpCacheLookupManager() override; 26 27 // ServerPushDelegate implementation. 28 void OnPush(std::unique_ptr<ServerPushHelper> push_helper, 29 const NetLogWithSource& session_net_log) override; 30 31 // Invoked when the HttpCache::Transaction for |url| finishes to cancel the 32 // server push if the response to the server push is found cached. 33 void OnLookupComplete(const GURL& url, int rv); 34 35 private: 36 // A class that takes the ownership of ServerPushHelper, issues and owns an 37 // HttpCache::Transaction which lookups the response in cache for the server 38 // push. 39 class LookupTransaction { 40 public: 41 LookupTransaction(std::unique_ptr<ServerPushHelper> push_helper, 42 NetLog* net_log); 43 ~LookupTransaction(); 44 45 // Issues an HttpCache::Transaction to lookup whether the response is cached 46 // without header validation. 47 int StartLookup(HttpCache* cache, 48 CompletionOnceCallback callback, 49 const NetLogWithSource& session_net_log); 50 51 void OnLookupComplete(int result); 52 53 private: 54 std::unique_ptr<ServerPushHelper> push_helper_; 55 std::unique_ptr<HttpRequestInfo> request_; 56 std::unique_ptr<HttpTransaction> transaction_; 57 const NetLogWithSource net_log_; 58 }; 59 60 // HttpCache must outlive the HttpCacheLookupManager. 61 raw_ptr<HttpCache> http_cache_; 62 std::map<GURL, std::unique_ptr<LookupTransaction>> lookup_transactions_; 63 base::WeakPtrFactory<HttpCacheLookupManager> weak_factory_{this}; 64 }; 65 66 } // namespace net 67 68 #endif // NET_HTTP_HTTP_CACHE_LOOKUP_MANAGER_H_ 69