Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #include "net/http/http_cache_lookup_manager.h" | 5 #include "net/http/http_cache_lookup_manager.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/values.h" | |
| 8 #include "net/base/load_flags.h" | 9 #include "net/base/load_flags.h" |
| 9 | 10 |
| 10 namespace net { | 11 namespace net { |
| 11 | 12 |
| 13 // Returns parameters associated with the start of a server push lookup | |
| 14 // transaction. | |
| 15 std::unique_ptr<base::Value> NetLogPushLookupTransactionCallback( | |
| 16 const NetLogSource& source, | |
|
Ryan Hamilton
2017/02/07 15:22:58
nit: this should be net_log
Zhongyi Shi
2017/02/07 19:08:22
Done.
| |
| 17 std::string push_url, | |
| 18 NetLogCaptureMode /* capture_mode */) { | |
| 19 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | |
| 20 if (source.IsValid()) | |
|
Ryan Hamilton
2017/02/07 15:22:58
Under what circumstances will this not be valid?
Zhongyi Shi
2017/02/07 19:08:22
Done.
I thought the tests would have the invalid
| |
| 21 source.AddToEventParameters(dict.get()); | |
| 22 dict->SetString("push_url", push_url); | |
| 23 return std::move(dict); | |
| 24 } | |
| 25 | |
| 26 std::unique_ptr<base::Value> NetLogServerPushLookupCompleteCallback( | |
| 27 bool found_in_cache, | |
| 28 NetLogCaptureMode /* capture_mode */) { | |
| 29 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | |
| 30 dict->SetBoolean("found_in_cache", found_in_cache); | |
| 31 return std::move(dict); | |
| 32 } | |
| 33 | |
| 12 HttpCacheLookupManager::LookupTransaction::LookupTransaction( | 34 HttpCacheLookupManager::LookupTransaction::LookupTransaction( |
| 13 std::unique_ptr<ServerPushHelper> server_push_helper) | 35 std::unique_ptr<ServerPushHelper> server_push_helper, |
| 36 NetLog* net_log) | |
| 14 : push_helper_(std::move(server_push_helper)), | 37 : push_helper_(std::move(server_push_helper)), |
| 15 request_(new HttpRequestInfo()), | 38 request_(new HttpRequestInfo()), |
| 16 transaction_(nullptr) {} | 39 transaction_(nullptr), |
| 40 net_log_( | |
| 41 NetLogWithSource::Make(net_log, | |
| 42 NetLogSourceType::PUSH_LOOKUP_TRANSACTION)) {} | |
| 17 | 43 |
| 18 HttpCacheLookupManager::LookupTransaction::~LookupTransaction() {} | 44 HttpCacheLookupManager::LookupTransaction::~LookupTransaction() { |
| 45 net_log_.EndEvent(NetLogEventType::PUSH_LOOKUP_TRANSACTION); | |
| 46 } | |
| 19 | 47 |
| 20 int HttpCacheLookupManager::LookupTransaction::StartLookup( | 48 int HttpCacheLookupManager::LookupTransaction::StartLookup( |
| 21 HttpCache* cache, | 49 HttpCache* cache, |
| 22 const CompletionCallback& callback, | 50 const CompletionCallback& callback, |
| 23 const NetLogWithSource& net_log) { | 51 const NetLogWithSource& source) { |
| 52 net_log_.BeginEvent( | |
| 53 NetLogEventType::PUSH_LOOKUP_TRANSACTION, | |
| 54 base::Bind(&NetLogPushLookupTransactionCallback, source.source(), | |
| 55 push_helper_->GetURL().spec())); | |
| 56 | |
| 24 request_->url = push_helper_->GetURL(); | 57 request_->url = push_helper_->GetURL(); |
| 25 request_->method = "GET"; | 58 request_->method = "GET"; |
| 26 request_->load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; | 59 request_->load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; |
| 27 cache->CreateTransaction(DEFAULT_PRIORITY, &transaction_); | 60 cache->CreateTransaction(DEFAULT_PRIORITY, &transaction_); |
| 28 return transaction_->Start(request_.get(), callback, net_log); | 61 return transaction_->Start(request_.get(), callback, net_log_); |
| 29 } | 62 } |
| 30 | 63 |
| 31 void HttpCacheLookupManager::LookupTransaction::CancelPush() { | 64 void HttpCacheLookupManager::LookupTransaction::OnLookupComplete( |
| 65 bool found_in_cache) { | |
| 66 net_log_.AddEvent( | |
| 67 NetLogEventType::SERVER_PUSH_LOOKUP_COMPLETE, | |
|
Ryan Hamilton
2017/02/07 15:22:58
nit: This is called SERVER_PUSH_... and the other
Zhongyi Shi
2017/02/07 19:08:21
Done.
| |
| 68 base::Bind(&NetLogServerPushLookupCompleteCallback, found_in_cache)); | |
| 69 if (!found_in_cache) | |
| 70 return; | |
| 71 | |
| 32 DCHECK(push_helper_.get()); | 72 DCHECK(push_helper_.get()); |
| 33 push_helper_->Cancel(); | 73 push_helper_->Cancel(); |
| 34 } | 74 } |
| 35 | 75 |
| 36 HttpCacheLookupManager::HttpCacheLookupManager(HttpCache* http_cache, | 76 HttpCacheLookupManager::HttpCacheLookupManager(HttpCache* http_cache, |
| 37 const NetLogWithSource& net_log) | 77 NetLog* net_log) |
| 38 : net_log_(net_log), http_cache_(http_cache), weak_factory_(this) {} | 78 : net_log_(net_log), http_cache_(http_cache), weak_factory_(this) {} |
| 39 | 79 |
| 40 HttpCacheLookupManager::~HttpCacheLookupManager() {} | 80 HttpCacheLookupManager::~HttpCacheLookupManager() {} |
| 41 | 81 |
| 42 void HttpCacheLookupManager::OnPush( | 82 void HttpCacheLookupManager::OnPush( |
| 43 std::unique_ptr<ServerPushHelper> push_helper) { | 83 std::unique_ptr<ServerPushHelper> push_helper, |
| 84 const NetLogWithSource& net_log) { | |
| 44 GURL pushed_url = push_helper->GetURL(); | 85 GURL pushed_url = push_helper->GetURL(); |
| 45 | 86 |
| 46 // There's a pending lookup transaction sent over already. | 87 // There's a pending lookup transaction sent over already. |
| 47 if (base::ContainsKey(lookup_transactions_, pushed_url)) | 88 if (base::ContainsKey(lookup_transactions_, pushed_url)) |
| 48 return; | 89 return; |
| 49 | 90 |
| 50 auto lookup = base::MakeUnique<LookupTransaction>(std::move(push_helper)); | 91 auto lookup = |
| 92 base::MakeUnique<LookupTransaction>(std::move(push_helper), net_log_); | |
| 51 | 93 |
| 52 int rv = lookup->StartLookup( | 94 int rv = lookup->StartLookup( |
| 53 http_cache_, base::Bind(&HttpCacheLookupManager::OnLookupComplete, | 95 http_cache_, base::Bind(&HttpCacheLookupManager::OnLookupComplete, |
| 54 weak_factory_.GetWeakPtr(), pushed_url), | 96 weak_factory_.GetWeakPtr(), pushed_url), |
| 55 net_log_); | 97 net_log); |
| 56 | 98 |
| 57 if (rv == ERR_IO_PENDING) | 99 if (rv == ERR_IO_PENDING) { |
| 58 lookup_transactions_[pushed_url] = std::move(lookup); | 100 lookup_transactions_[pushed_url] = std::move(lookup); |
| 101 } else { | |
| 102 lookup->OnLookupComplete(false); | |
|
Ryan Hamilton
2017/02/07 15:22:58
I would have thought this would be OnLookupComplet
Zhongyi Shi
2017/02/07 19:08:22
Done.
An synchronous return should imply that the
| |
| 103 } | |
| 59 } | 104 } |
| 60 | 105 |
| 61 void HttpCacheLookupManager::OnLookupComplete(const GURL& url, int rv) { | 106 void HttpCacheLookupManager::OnLookupComplete(const GURL& url, int rv) { |
| 62 auto it = lookup_transactions_.find(url); | 107 auto it = lookup_transactions_.find(url); |
| 63 DCHECK(it != lookup_transactions_.end()); | 108 DCHECK(it != lookup_transactions_.end()); |
| 64 | 109 |
| 65 if (rv == OK) | 110 it->second->OnLookupComplete(rv == OK); |
| 66 it->second->CancelPush(); | |
| 67 | 111 |
| 68 lookup_transactions_.erase(it); | 112 lookup_transactions_.erase(it); |
| 69 } | 113 } |
| 70 | 114 |
| 71 } // namespace net | 115 } // namespace net |
| OLD | NEW |