Give names to all utility processes.
[chromium-blink-merge.git] / components / password_manager / core / browser / affiliation_utils.h
blob1f56c4e8e122aa6dd7f8569aba97fd702f018ce3
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This file contains utilities related to working with "facets".
6 //
7 // A "facet" is defined as the manifestation of a logical application on a given
8 // platform. For example, "My Bank" may have released an Android application
9 // and a Web application accessible from a browser. These are all facets of the
10 // "My Bank" logical application.
12 // Facets that belong to the same logical application are said to be affiliated
13 // with each other. Conceptually, "affiliations" can be seen as an equivalence
14 // relation defined over the set of all facets. Each equivalence class contains
15 // facets that belong to the same logical application, and therefore should be
16 // treated as synonymous for certain purposes, e.g., sharing credentials.
18 // A valid facet identifier will be a URI of the form:
20 // * https://<host>[:<port>]
22 // For web sites. Only HTTPS sites are supported. The syntax corresponds to
23 // that of 'serialized-origin' in RFC 6454. That is, in canonical form, the
24 // URI must not contain components other than the scheme (required, must be
25 // "https"), host (required), and port (optional); with canonicalization
26 // performed the same way as it normally would be for standard URLs.
28 // * android://<certificate_hash>@<package_name>
30 // For Android applications. In canonical form, the URI must not contain
31 // components other than the scheme (must be "android"), username, and host
32 // (all required). The host part must be a valid Android package name, with
33 // no escaping, so it must be composed of characters [a-zA-Z0-9_.].
35 // The username part must be the hash of the certificate used to sign the
36 // APK, base64-encoded using padding and the "URL and filename safe" base64
37 // alphabet, with no further escaping. This is normally calculated as:
39 // echo -n -e "$PEM_KEY" |
40 // openssl x509 -outform DER |
41 // openssl sha -sha512 -binary | base64 | tr '+/' '-_'
44 #ifndef COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_UTILS_H_
45 #define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_UTILS_H_
47 #include <iosfwd>
48 #include <string>
49 #include <vector>
51 #include "base/containers/hash_tables.h"
52 #include "base/logging.h"
53 #include "base/time/time.h"
54 #include "url/url_parse.h"
56 namespace base {
57 class CommandLine;
58 } // namespace base
60 namespace password_manager {
62 // Encapsulates a facet URI in canonical form.
64 // This is a very light-weight wrapper around an std::string containing the text
65 // of the URI, and can be passed around as a value. The main rationale for the
66 // existance of this class is to make it clearer in the code when a certain URI
67 // is known to be a valid facet URI in canonical form, and to allow verifying
68 // and converting URIs to such canonical form.
70 // Note that it would be impractical to use GURL to represent facet URIs, as
71 // GURL has built-in logic to parse the rest of the URI according to its scheme,
72 // and obviously, it does not recognize the "android" scheme. Therefore, after
73 // parsing, everything ends up in the path component, which is not too helpful.
74 class FacetURI {
75 public:
76 FacetURI();
78 // As a light-weight std::string wrapper, allow copy and assign.
79 FacetURI(const FacetURI&) = default;
80 FacetURI& operator=(const FacetURI&) = default;
82 // Constructs an instance to encapsulate the canonical form of |spec|.
83 // If |spec| is not a valid facet URI, then an invalid instance is returned,
84 // which then should be discarded.
85 static FacetURI FromPotentiallyInvalidSpec(const std::string& spec);
87 // Constructs a valid FacetURI instance from a valid |canonical_spec|.
88 // Note: The passed-in URI is not verified at all. Use only when you are sure
89 // the URI is valid and in canonical form.
90 static FacetURI FromCanonicalSpec(const std::string& canonical_spec);
92 // Comparison operators so that FacetURI can be used in std::equal.
93 bool operator==(const FacetURI& other) const;
94 bool operator!=(const FacetURI& other) const;
96 // Relational operators so that FacetURI can be used in sorted containers.
97 bool operator<(const FacetURI& other) const;
98 bool operator>(const FacetURI& other) const;
100 // Returns whether or not this instance represents a valid facet identifier
101 // referring to a Web application.
102 bool IsValidWebFacetURI() const;
104 // Returns whether or not this instance represents a valid facet identifier
105 // referring to an Android application.
106 bool IsValidAndroidFacetURI() const;
108 // Returns whether or not this instance represents a valid facet identifier
109 // referring to either a Web or an Android application. The empty identfier is
110 // not considered valid.
111 bool is_valid() const { return is_valid_; }
113 // Returns whether or not this instance represents the empty facet identifier.
114 bool is_empty() const { return canonical_spec_.empty(); }
116 // Returns the canonical scheme of the encapsulated facet URI, provided it is
117 // valid, or the empty string otherwise.
118 std::string scheme() const;
120 // Returns the canonical package name that the encapsulated facet URI
121 // references, provided it is a valid Android facet URI, or the empty string
122 // otherwise.
123 std::string android_package_name() const;
125 // Returns the text of the encapsulated canonical URI, which must be valid.
126 const std::string& canonical_spec() const {
127 DCHECK(is_valid_);
128 return canonical_spec_;
131 // Returns the text of the encapsulated canonical URI, even if it is invalid.
132 const std::string& potentially_invalid_spec() const {
133 return canonical_spec_;
136 private:
137 // Internal constructor to be used by the static factory methods.
138 FacetURI(const std::string& canonical_spec, bool is_valid);
140 // Whether |canonical_spec_| contains a valid facet URI in canonical form.
141 bool is_valid_;
143 // The text of the encapsulated canonical URI, valid if and only if
144 // |is_valid_| is true.
145 std::string canonical_spec_;
147 // Identified components of the canonical spec.
148 url::Parsed parsed_;
151 // A collection of facets affiliated with each other, i.e. an equivalence class.
152 typedef std::vector<FacetURI> AffiliatedFacets;
154 // A collection of facets affiliated with each other, i.e. an equivalence class,
155 // plus a timestamp that indicates the last time the data was updated from an
156 // authoritative source.
157 struct AffiliatedFacetsWithUpdateTime {
158 AffiliatedFacetsWithUpdateTime();
159 ~AffiliatedFacetsWithUpdateTime();
161 AffiliatedFacets facets;
162 base::Time last_update_time;
165 // Returns whether or not equivalence classes |a| and |b| are equal, that is,
166 // whether or not they consist of the same set of facets.
168 // Note that this will do some sorting, so it can be expensive for large inputs.
169 bool AreEquivalenceClassesEqual(const AffiliatedFacets& a,
170 const AffiliatedFacets& b);
172 // A shorter way to spell FacetURI::IsValidAndroidFacetURI().
173 bool IsValidAndroidFacetURI(const std::string& uri);
175 // Returns whether or not affiliation based matching is enabled, either via
176 // command line flags or field trials. The command line flag, if present, always
177 // takes precedence.
178 bool IsAffiliationBasedMatchingEnabled(const base::CommandLine& command_line);
180 // For logging use only.
181 std::ostream& operator<<(std::ostream& os, const FacetURI& facet_uri);
183 } // namespace password_manager
185 // Provide a hash function so that hash_sets and maps can contain FacetURIs.
186 namespace BASE_HASH_NAMESPACE {
188 template <>
189 struct hash<password_manager::FacetURI> {
190 size_t operator()(const password_manager::FacetURI& facet_uri) const {
191 return hash<std::string>()(facet_uri.potentially_invalid_spec());
195 } // namespace BASE_HASH_NAMESPACE
197 #endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_UTILS_H_