File: http_cache_lookup_manager.cc

package info (click to toggle)
chromium 73.0.3683.75-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,792,156 kB
  • sloc: cpp: 13,473,466; ansic: 1,577,080; python: 898,539; javascript: 655,737; xml: 341,883; asm: 306,070; java: 289,969; perl: 80,911; objc: 67,198; sh: 43,184; cs: 27,853; makefile: 12,092; php: 11,064; yacc: 10,373; tcl: 8,875; ruby: 3,941; lex: 1,800; pascal: 1,473; lisp: 812; awk: 41; jsp: 39; sed: 19; sql: 3
file content (102 lines) | stat: -rw-r--r-- 3,584 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// 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 <memory>

#include "base/values.h"
#include "net/base/load_flags.h"

namespace net {

// Returns parameters associated with the start of a server push lookup
// transaction.
std::unique_ptr<base::Value> NetLogPushLookupTransactionCallback(
    const NetLogSource& net_log,
    const ServerPushDelegate::ServerPushHelper* push_helper,
    NetLogCaptureMode /* capture_mode */) {
  std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
  net_log.AddToEventParameters(dict.get());
  dict->SetString("push_url", push_helper->GetURL().possibly_invalid_spec());
  return std::move(dict);
}

HttpCacheLookupManager::LookupTransaction::LookupTransaction(
    std::unique_ptr<ServerPushHelper> server_push_helper,
    NetLog* net_log)
    : push_helper_(std::move(server_push_helper)),
      request_(new HttpRequestInfo()),
      transaction_(nullptr),
      net_log_(NetLogWithSource::Make(
          net_log,
          NetLogSourceType::SERVER_PUSH_LOOKUP_TRANSACTION)) {}

HttpCacheLookupManager::LookupTransaction::~LookupTransaction() = default;

int HttpCacheLookupManager::LookupTransaction::StartLookup(
    HttpCache* cache,
    CompletionOnceCallback callback,
    const NetLogWithSource& session_net_log) {
  net_log_.BeginEvent(NetLogEventType::SERVER_PUSH_LOOKUP_TRANSACTION,
                      base::Bind(&NetLogPushLookupTransactionCallback,
                                 session_net_log.source(), push_helper_.get()));

  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(), std::move(callback), net_log_);
}

void HttpCacheLookupManager::LookupTransaction::OnLookupComplete(int result) {
  if (result == OK) {
    DCHECK(push_helper_.get());
    push_helper_->Cancel();
  }
  net_log_.EndEventWithNetErrorCode(
      NetLogEventType::SERVER_PUSH_LOOKUP_TRANSACTION, result);
}

HttpCacheLookupManager::HttpCacheLookupManager(HttpCache* http_cache)
    : http_cache_(http_cache), weak_factory_(this) {}

HttpCacheLookupManager::~HttpCacheLookupManager() = default;

void HttpCacheLookupManager::OnPush(
    std::unique_ptr<ServerPushHelper> push_helper,
    const NetLogWithSource& session_net_log) {
  GURL pushed_url = push_helper->GetURL();

  // There's a pending lookup transaction sent over already.
  if (base::ContainsKey(lookup_transactions_, pushed_url))
    return;

  auto lookup = std::make_unique<LookupTransaction>(std::move(push_helper),
                                                    session_net_log.net_log());
  // TODO(zhongyi): add events in session net log to log the creation of
  // LookupTransaction.

  int rv = lookup->StartLookup(
      http_cache_, base::Bind(&HttpCacheLookupManager::OnLookupComplete,
                              weak_factory_.GetWeakPtr(), pushed_url),
      session_net_log);

  if (rv == ERR_IO_PENDING) {
    lookup_transactions_[pushed_url] = std::move(lookup);
  } else {
    lookup->OnLookupComplete(rv);
  }
}

void HttpCacheLookupManager::OnLookupComplete(const GURL& url, int rv) {
  auto it = lookup_transactions_.find(url);
  DCHECK(it != lookup_transactions_.end());

  it->second->OnLookupComplete(rv);

  lookup_transactions_.erase(it);
}

}  // namespace net