• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "net/http/http_cache_lookup_manager.h"
6 
7 #include <memory>
8 
9 #include "base/containers/contains.h"
10 #include "base/functional/bind.h"
11 #include "base/values.h"
12 #include "net/base/load_flags.h"
13 #include "net/base/network_anonymization_key.h"
14 #include "net/http/http_request_info.h"
15 namespace net {
16 
17 // Returns parameters associated with the start of a server push lookup
18 // transaction.
NetLogPushLookupTransactionParams(const NetLogSource & net_log,const ServerPushDelegate::ServerPushHelper * push_helper)19 base::Value::Dict NetLogPushLookupTransactionParams(
20     const NetLogSource& net_log,
21     const ServerPushDelegate::ServerPushHelper* push_helper) {
22   base::Value::Dict dict;
23   net_log.AddToEventParameters(dict);
24   dict.Set("push_url", push_helper->GetURL().possibly_invalid_spec());
25   return dict;
26 }
27 
LookupTransaction(std::unique_ptr<ServerPushHelper> server_push_helper,NetLog * net_log)28 HttpCacheLookupManager::LookupTransaction::LookupTransaction(
29     std::unique_ptr<ServerPushHelper> server_push_helper,
30     NetLog* net_log)
31     : push_helper_(std::move(server_push_helper)),
32       request_(std::make_unique<HttpRequestInfo>()),
33       net_log_(NetLogWithSource::Make(
34           net_log,
35           NetLogSourceType::SERVER_PUSH_LOOKUP_TRANSACTION)) {}
36 
37 HttpCacheLookupManager::LookupTransaction::~LookupTransaction() = default;
38 
StartLookup(HttpCache * cache,CompletionOnceCallback callback,const NetLogWithSource & session_net_log)39 int HttpCacheLookupManager::LookupTransaction::StartLookup(
40     HttpCache* cache,
41     CompletionOnceCallback callback,
42     const NetLogWithSource& session_net_log) {
43   net_log_.BeginEvent(NetLogEventType::SERVER_PUSH_LOOKUP_TRANSACTION, [&] {
44     return NetLogPushLookupTransactionParams(session_net_log.source(),
45                                              push_helper_.get());
46   });
47 
48   request_->url = push_helper_->GetURL();
49   // Note: since HTTP/2 Server Push has been disabled and this code will likely
50   // be removed, just use empty NIKs and NAKs here. For more info, see
51   // https://crbug.com/1355929.
52   request_->network_isolation_key = NetworkIsolationKey();
53   request_->network_anonymization_key = NetworkAnonymizationKey();
54   request_->method = "GET";
55   request_->load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION;
56   cache->CreateTransaction(DEFAULT_PRIORITY, &transaction_);
57   return transaction_->Start(request_.get(), std::move(callback), net_log_);
58 }
59 
OnLookupComplete(int result)60 void HttpCacheLookupManager::LookupTransaction::OnLookupComplete(int result) {
61   if (result == OK) {
62     DCHECK(push_helper_.get());
63     push_helper_->Cancel();
64   }
65   net_log_.EndEventWithNetErrorCode(
66       NetLogEventType::SERVER_PUSH_LOOKUP_TRANSACTION, result);
67 }
68 
HttpCacheLookupManager(HttpCache * http_cache)69 HttpCacheLookupManager::HttpCacheLookupManager(HttpCache* http_cache)
70     : http_cache_(http_cache) {}
71 
72 HttpCacheLookupManager::~HttpCacheLookupManager() = default;
73 
OnPush(std::unique_ptr<ServerPushHelper> push_helper,const NetLogWithSource & session_net_log)74 void HttpCacheLookupManager::OnPush(
75     std::unique_ptr<ServerPushHelper> push_helper,
76     const NetLogWithSource& session_net_log) {
77   GURL pushed_url = push_helper->GetURL();
78 
79   // There's a pending lookup transaction sent over already.
80   if (base::Contains(lookup_transactions_, pushed_url))
81     return;
82 
83   auto lookup = std::make_unique<LookupTransaction>(std::move(push_helper),
84                                                     session_net_log.net_log());
85   // TODO(zhongyi): add events in session net log to log the creation of
86   // LookupTransaction.
87 
88   int rv = lookup->StartLookup(
89       http_cache_,
90       base::BindOnce(&HttpCacheLookupManager::OnLookupComplete,
91                      weak_factory_.GetWeakPtr(), pushed_url),
92       session_net_log);
93 
94   if (rv == ERR_IO_PENDING) {
95     lookup_transactions_[pushed_url] = std::move(lookup);
96   } else {
97     lookup->OnLookupComplete(rv);
98   }
99 }
100 
OnLookupComplete(const GURL & url,int rv)101 void HttpCacheLookupManager::OnLookupComplete(const GURL& url, int rv) {
102   auto it = lookup_transactions_.find(url);
103   DCHECK(it != lookup_transactions_.end());
104 
105   it->second->OnLookupComplete(rv);
106 
107   lookup_transactions_.erase(it);
108 }
109 
110 }  // namespace net
111