Index: net/http/http_cache_lookup_manager.cc |
diff --git a/net/http/http_cache_lookup_manager.cc b/net/http/http_cache_lookup_manager.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..b1d822fa56eb6ef2520fa6dafc6dfed33b7c365e |
--- /dev/null |
+++ b/net/http/http_cache_lookup_manager.cc |
@@ -0,0 +1,71 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "net/http/http_cache_lookup_manager.h" |
+ |
+#include "net/base/load_flags.h" |
+ |
+namespace net { |
+ |
+HttpCacheLookupManager::LookupTransaction::LookupTransaction( |
+ std::unique_ptr<ServerPushHelper> server_push_helper) |
+ : push_helper(std::move(server_push_helper)), |
+ request(new HttpRequestInfo()), |
+ transaction(nullptr) {} |
+ |
+HttpCacheLookupManager::LookupTransaction::~LookupTransaction() {} |
+ |
+int HttpCacheLookupManager::LookupTransaction::StartLookup( |
+ HttpCache* cache, |
+ const CompletionCallback& callback, |
+ const NetLogWithSource& net_log) { |
+ request->url = push_helper->GetURL(); |
+ request->method = "GET"; |
+ request->load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; |
+ cache->CreateTransaction(DEFAULT_PRIORITY, &transaction); |
+ return transaction->Start(request.get(), callback, net_log); |
+} |
+ |
+void HttpCacheLookupManager::LookupTransaction::CancelPush() { |
+ DCHECK(push_helper.get()); |
+ push_helper->Cancel(); |
+} |
+ |
+HttpCacheLookupManager::HttpCacheLookupManager(HttpCache* http_cache, |
+ const NetLogWithSource& net_log) |
+ : net_log_(net_log), http_cache_(http_cache), weak_factory_(this) {} |
+ |
+HttpCacheLookupManager::~HttpCacheLookupManager() {} |
+ |
+void HttpCacheLookupManager::OnPush( |
+ std::unique_ptr<ServerPushHelper> push_helper) { |
+ GURL pushed_url = push_helper->GetURL(); |
+ |
+ // There's a pending lookup transaction sent over already. |
+ if (lookup_transactions_.find(pushed_url) != lookup_transactions_.end()) |
Ryan Hamilton
2016/11/18 20:47:02
nit: if (ContainsKey(lookup_transactions_, pushed_
Zhongyi Shi
2016/11/19 05:07:12
Done.
|
+ return; |
+ |
+ auto lookup = base::MakeUnique<LookupTransaction>(std::move(push_helper)); |
+ |
+ int rv = lookup->StartLookup( |
+ http_cache_, base::Bind(&HttpCacheLookupManager::OnLookupComplete, |
+ weak_factory_.GetWeakPtr(), pushed_url), |
+ net_log_); |
+ |
+ if (rv == ERR_IO_PENDING) { |
+ lookup_transactions_[pushed_url] = std::move(lookup); |
+ } |
Ryan Hamilton
2016/11/18 20:47:02
nit: no {}s on 1-line ifs
Zhongyi Shi
2016/11/19 05:07:12
Done.
|
+} |
Ryan Hamilton
2016/11/18 20:47:02
This method looks really clean now!
|
+ |
+void HttpCacheLookupManager::OnLookupComplete(const GURL& url, int rv) { |
+ auto it = lookup_transactions_.find(url); |
+ DCHECK(it != lookup_transactions_.end()); |
+ |
+ if (rv == OK) |
+ it->second->CancelPush(); |
+ |
+ lookup_transactions_.erase(it); |
+} |
+ |
+} // namespace net |