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& net_log, | |
| 17 std::string push_url, | |
|
eroman
2017/02/07 22:33:42
Can you reduce the copying of |push_url| ?
Minima
Zhongyi Shi
2017/02/08 00:40:53
Done.
| |
| 18 NetLogCaptureMode /* capture_mode */) { | |
| 19 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | |
| 20 net_log.AddToEventParameters(dict.get()); | |
| 21 dict->SetString("push_url", push_url); | |
| 22 return std::move(dict); | |
| 23 } | |
| 24 | |
| 25 std::unique_ptr<base::Value> NetLogServerPushLookupCompleteCallback( | |
| 26 bool found_in_cache, | |
| 27 NetLogCaptureMode /* capture_mode */) { | |
| 28 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | |
| 29 dict->SetBoolean("found_in_cache", found_in_cache); | |
| 30 return std::move(dict); | |
| 31 } | |
| 32 | |
| 12 HttpCacheLookupManager::LookupTransaction::LookupTransaction( | 33 HttpCacheLookupManager::LookupTransaction::LookupTransaction( |
| 13 std::unique_ptr<ServerPushHelper> server_push_helper) | 34 std::unique_ptr<ServerPushHelper> server_push_helper, |
| 35 NetLog* net_log) | |
| 14 : push_helper_(std::move(server_push_helper)), | 36 : push_helper_(std::move(server_push_helper)), |
| 15 request_(new HttpRequestInfo()), | 37 request_(new HttpRequestInfo()), |
| 16 transaction_(nullptr) {} | 38 transaction_(nullptr), |
| 39 net_log_(NetLogWithSource::Make( | |
|
eroman
2017/02/07 22:33:42
Instead of creating a new source for this LookupTr
Zhongyi Shi
2017/02/08 00:40:53
SERVER_PUSH_LOOKUP_TRANSACTION is something simila
eroman
2017/02/10 23:57:47
Sure, that makes sense then!
| |
| 40 net_log, | |
| 41 NetLogSourceType::SERVER_PUSH_LOOKUP_TRANSACTION)) {} | |
| 17 | 42 |
| 18 HttpCacheLookupManager::LookupTransaction::~LookupTransaction() {} | 43 HttpCacheLookupManager::LookupTransaction::~LookupTransaction() { |
| 44 net_log_.EndEvent(NetLogEventType::SERVER_PUSH_LOOKUP_TRANSACTION); | |
|
eroman
2017/02/07 22:33:42
What is the meaning for SERVER_PUSH_LOOKUP_TRANSAC
Zhongyi Shi
2017/02/08 00:40:53
Done.
Agreed that it's better to match the Begin/
| |
| 45 } | |
| 19 | 46 |
| 20 int HttpCacheLookupManager::LookupTransaction::StartLookup( | 47 int HttpCacheLookupManager::LookupTransaction::StartLookup( |
| 21 HttpCache* cache, | 48 HttpCache* cache, |
| 22 const CompletionCallback& callback, | 49 const CompletionCallback& callback, |
| 23 const NetLogWithSource& net_log) { | 50 const NetLogWithSource& net_log) { |
| 51 net_log_.BeginEvent( | |
| 52 NetLogEventType::SERVER_PUSH_LOOKUP_TRANSACTION, | |
| 53 base::Bind(&NetLogPushLookupTransactionCallback, net_log.source(), | |
| 54 push_helper_->GetURL().spec())); | |
| 55 | |
| 24 request_->url = push_helper_->GetURL(); | 56 request_->url = push_helper_->GetURL(); |
| 25 request_->method = "GET"; | 57 request_->method = "GET"; |
| 26 request_->load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; | 58 request_->load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; |
| 27 cache->CreateTransaction(DEFAULT_PRIORITY, &transaction_); | 59 cache->CreateTransaction(DEFAULT_PRIORITY, &transaction_); |
| 28 return transaction_->Start(request_.get(), callback, net_log); | 60 return transaction_->Start(request_.get(), callback, net_log_); |
| 29 } | 61 } |
| 30 | 62 |
| 31 void HttpCacheLookupManager::LookupTransaction::CancelPush() { | 63 void HttpCacheLookupManager::LookupTransaction::OnLookupComplete( |
| 64 bool found_in_cache) { | |
| 65 net_log_.AddEvent( | |
| 66 NetLogEventType::SERVER_PUSH_LOOKUP_COMPLETE, | |
| 67 base::Bind(&NetLogServerPushLookupCompleteCallback, found_in_cache)); | |
| 68 if (!found_in_cache) | |
| 69 return; | |
| 70 | |
| 32 DCHECK(push_helper_.get()); | 71 DCHECK(push_helper_.get()); |
| 33 push_helper_->Cancel(); | 72 push_helper_->Cancel(); |
| 34 } | 73 } |
| 35 | 74 |
| 36 HttpCacheLookupManager::HttpCacheLookupManager(HttpCache* http_cache, | 75 HttpCacheLookupManager::HttpCacheLookupManager(HttpCache* http_cache, |
| 37 const NetLogWithSource& net_log) | 76 NetLog* net_log) |
| 38 : net_log_(net_log), http_cache_(http_cache), weak_factory_(this) {} | 77 : net_log_(net_log), http_cache_(http_cache), weak_factory_(this) {} |
| 39 | 78 |
| 40 HttpCacheLookupManager::~HttpCacheLookupManager() {} | 79 HttpCacheLookupManager::~HttpCacheLookupManager() {} |
| 41 | 80 |
| 42 void HttpCacheLookupManager::OnPush( | 81 void HttpCacheLookupManager::OnPush( |
| 43 std::unique_ptr<ServerPushHelper> push_helper) { | 82 std::unique_ptr<ServerPushHelper> push_helper, |
| 83 const NetLogWithSource& net_log) { | |
| 44 GURL pushed_url = push_helper->GetURL(); | 84 GURL pushed_url = push_helper->GetURL(); |
| 45 | 85 |
| 46 // There's a pending lookup transaction sent over already. | 86 // There's a pending lookup transaction sent over already. |
| 47 if (base::ContainsKey(lookup_transactions_, pushed_url)) | 87 if (base::ContainsKey(lookup_transactions_, pushed_url)) |
| 48 return; | 88 return; |
| 49 | 89 |
| 50 auto lookup = base::MakeUnique<LookupTransaction>(std::move(push_helper)); | 90 auto lookup = |
| 91 base::MakeUnique<LookupTransaction>(std::move(push_helper), net_log_); | |
|
eroman
2017/02/07 22:33:42
[optional] I found it a bit confusing that here (a
Zhongyi Shi
2017/02/08 00:40:53
Done.
| |
| 51 | 92 |
| 52 int rv = lookup->StartLookup( | 93 int rv = lookup->StartLookup( |
| 53 http_cache_, base::Bind(&HttpCacheLookupManager::OnLookupComplete, | 94 http_cache_, base::Bind(&HttpCacheLookupManager::OnLookupComplete, |
| 54 weak_factory_.GetWeakPtr(), pushed_url), | 95 weak_factory_.GetWeakPtr(), pushed_url), |
| 55 net_log_); | 96 net_log); |
| 56 | 97 |
| 57 if (rv == ERR_IO_PENDING) | 98 if (rv == ERR_IO_PENDING) { |
| 58 lookup_transactions_[pushed_url] = std::move(lookup); | 99 lookup_transactions_[pushed_url] = std::move(lookup); |
| 100 } else { | |
| 101 lookup->OnLookupComplete(rv == OK); | |
| 102 } | |
| 59 } | 103 } |
| 60 | 104 |
| 61 void HttpCacheLookupManager::OnLookupComplete(const GURL& url, int rv) { | 105 void HttpCacheLookupManager::OnLookupComplete(const GURL& url, int rv) { |
| 62 auto it = lookup_transactions_.find(url); | 106 auto it = lookup_transactions_.find(url); |
| 63 DCHECK(it != lookup_transactions_.end()); | 107 DCHECK(it != lookup_transactions_.end()); |
| 64 | 108 |
| 65 if (rv == OK) | 109 it->second->OnLookupComplete(rv == OK); |
| 66 it->second->CancelPush(); | |
| 67 | 110 |
| 68 lookup_transactions_.erase(it); | 111 lookup_transactions_.erase(it); |
| 69 } | 112 } |
| 70 | 113 |
| 71 } // namespace net | 114 } // namespace net |
| OLD | NEW |