File: passwords_private_utils.h

package info (click to toggle)
chromium 90.0.4430.212-1~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,450,632 kB
  • sloc: cpp: 19,832,434; javascript: 2,948,838; ansic: 2,312,399; python: 1,464,622; xml: 584,121; java: 514,189; asm: 470,557; objc: 83,463; perl: 77,861; sh: 77,030; cs: 70,789; fortran: 24,137; tcl: 18,916; php: 18,872; makefile: 16,848; ruby: 16,721; pascal: 13,150; sql: 10,199; yacc: 7,507; lex: 1,313; lisp: 840; awk: 329; jsp: 39; sed: 19
file content (77 lines) | stat: -rw-r--r-- 2,977 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
// Copyright 2017 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.

#ifndef CHROME_BROWSER_EXTENSIONS_API_PASSWORDS_PRIVATE_PASSWORDS_PRIVATE_UTILS_H_
#define CHROME_BROWSER_EXTENSIONS_API_PASSWORDS_PRIVATE_PASSWORDS_PRIVATE_UTILS_H_

#include <functional>
#include <map>
#include <string>

#include "base/containers/flat_map.h"
#include "chrome/common/extensions/api/passwords_private.h"

namespace password_manager {
struct PasswordForm;
}  // namespace password_manager

namespace extensions {

// Obtains a collection of URLs from the passed in form. This includes an origin
// URL used for internal logic, a human friendly string shown to the user as
// well as a URL that is linked to.
api::passwords_private::UrlCollection CreateUrlCollectionFromForm(
    const password_manager::PasswordForm& form);

// This class is an id generator for an arbitrary key type. It is used by both
// PasswordManagerPresenter and PasswordCheckDelegate to create ids send to the
// UI. It is similar to base::IDMap, but has the following important
// differences:
// - IdGenerator owns a copy of the key data, so that clients don't need to
//   worry about dangling pointers.
// - Repeated calls to GenerateId with the same |key| are no-ops, and return the
//   same ids.
template <typename KeyT,
          typename IdT = int32_t,
          typename KeyCompare = std::less<>>
class IdGenerator {
 public:
  // This method generates an id corresponding to |key|. Additionally it
  // remembers ids generated in the past, so that this method is idempotent.
  // Furthermore, it is guaranteed that different ids are returned for different
  // |key| arguments. This implies GenerateId(a) == GenerateId(b) if and only if
  // a == b.
  IdT GenerateId(const KeyT& key) {
    auto result = key_cache_.emplace(key, next_id_);
    if (result.second) {
      // In case we haven't seen |key| before, add a pointer to the inserted key
      // and the corresponding id to the |id_cache_|. This insertion should
      // always succeed.
      auto iter = id_cache_.emplace_hint(id_cache_.end(), next_id_,
                                         &result.first->first);
      DCHECK_EQ(&result.first->first, iter->second);
      ++next_id_;
    }

    return result.first->second;
  }

  // This method tries to return the key corresponding to |id|. In case |id| was
  // not generated by GenerateId() before, this method returns nullptr.
  // Otherwise it returns a pointer p to a key, such that |id| ==
  // GenerateId(*p).
  const KeyT* TryGetKey(IdT id) const {
    auto it = id_cache_.find(id);
    return it != id_cache_.end() ? it->second : nullptr;
  }

 private:
  std::map<KeyT, IdT, KeyCompare> key_cache_;
  base::flat_map<IdT, const KeyT*> id_cache_;
  IdT next_id_ = 0;
};

}  // namespace extensions

#endif  // CHROME_BROWSER_EXTENSIONS_API_PASSWORDS_PRIVATE_PASSWORDS_PRIVATE_UTILS_H_