File: facet_manager.h

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (137 lines) | stat: -rw-r--r-- 5,631 bytes parent folder | download
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2015 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 COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_FACET_MANAGER_H_
#define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_FACET_MANAGER_H_

#include <set>
#include <vector>

#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/time/time.h"
#include "components/password_manager/core/browser/affiliation_service.h"
#include "components/password_manager/core/browser/affiliation_utils.h"

namespace base {
class Clock;
class TaskRunner;
}  // namespace base

namespace password_manager {

class FacetManagerHost;

// Encapsulates the state and logic required for handling affiliation requests
// concerning a single facet. The AffiliationBackend owns one instance for each
// facet that requires attention, and it itself implements the FacetManagerHost
// interface to provide shared functionality needed by all FacetManagers.
class FacetManager {
 public:
  using StrategyOnCacheMiss = AffiliationService::StrategyOnCacheMiss;

  // Both the |backend| and |clock| must outlive this object.
  FacetManager(const FacetURI& facet_uri,
               FacetManagerHost* backend,
               base::Clock* clock);
  ~FacetManager();

  // Facet-specific implementations for methods in AffiliationService of the
  // same name. See documentation in affiliation_service.h for details:
  void GetAffiliations(
      StrategyOnCacheMiss cache_miss_strategy,
      const AffiliationService::ResultCallback& callback,
      const scoped_refptr<base::TaskRunner>& callback_task_runner);
  void Prefetch(const base::Time& keep_fresh_until);
  void CancelPrefetch(const base::Time& keep_fresh_until);

  // Called when |affiliation| information regarding this facet has just been
  // fetched from the Affiliation API.
  void OnFetchSucceeded(const AffiliatedFacetsWithUpdateTime& affiliation);

  // Called by the backend when the time specified in RequestNotificationAtTime
  // has come to pass, so that |this| can perform delayed administrative tasks.
  void NotifyAtRequestedTime();

  // Returns whether this instance has becomes redundant, that is, it has no
  // more meaningful state than a newly created instance would have.
  bool CanBeDiscarded() const;

  // Returns whether or not cached data for this facet can be discarded without
  // harm when trimming the database.
  bool CanCachedDataBeDiscarded() const;

  // Returns whether or not affiliation information relating to this facet needs
  // to be fetched right now.
  bool DoesRequireFetch() const;

  // The members below are made public for the sake of tests.

  // Returns whether or not cached data for this facet is fresh (not stale).
  bool IsCachedDataFresh() const;

  // Returns whether or not cached data for this facet is near-stale or stale.
  bool IsCachedDataNearStale() const;

  // The duration after which cached affiliation data is considered near-stale.
  static const int kCacheSoftExpiryInHours;

  // The duration after which cached affiliation data is considered stale.
  static const int kCacheHardExpiryInHours;

 private:
  struct RequestInfo;

  // Returns the time when the cached data for this facet will become stale.
  // The data is considered stale with the returned time value inclusive.
  base::Time GetCacheHardExpiryTime() const;

  // Returns the time when cached data for this facet becomes near-stale.
  // The data is considered near-stale with the returned time value inclusive.
  base::Time GetCacheSoftExpiryTime() const;

  // Returns the maximum of |keep_fresh_thresholds_|, or the NULL time if the
  // set is empty.
  base::Time GetMaximumKeepFreshUntilThreshold() const;

  // Returns the next time affiliation data for this facet needs to be fetched
  // due to active prefetch requests, or base::Time::Max() if not at all.
  base::Time GetNextRequiredFetchTimeDueToPrefetch() const;

  // Posts the callback of the request described by |request_info| with success.
  static void ServeRequestWithSuccess(const RequestInfo& request_info,
                                      const AffiliatedFacets& affiliation);

  // Posts the callback of the request described by |request_info| with failure.
  static void ServeRequestWithFailure(const RequestInfo& request_info);

  FacetURI facet_uri_;
  FacetManagerHost* backend_;
  base::Clock* clock_;

  // The last time affiliation information was fetched for this facet, i.e. the
  // freshness of the data in the cache. If there is no corresponding data in
  // the database, this will contain the NULL time. Otherwise, the update time
  // in the database should match this value; it is stored to reduce disk I/O.
  base::Time last_update_time_;

  // Contains information about the GetAffiliations() requests that are waiting
  // for the result of looking up this facet.
  std::vector<RequestInfo> pending_requests_;

  // Keeps track of |keep_fresh_until| thresholds corresponding to Prefetch()
  // requests for this facet. Affiliation information for this facet must be
  // kept fresh by periodic refetches until at least the maximum time in this
  // set (exclusive).
  //
  // This is not a single timestamp but rather a multiset so that cancellation
  // of individual prefetches can be supported even if there are two requests
  // with the same |keep_fresh_until| threshold.
  std::multiset<base::Time> keep_fresh_until_thresholds_;

  DISALLOW_COPY_AND_ASSIGN(FacetManager);
};

}  // namespace password_manager
#endif  // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_FACET_MANAGER_H_