1 // Copyright 2015 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 #ifndef COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_BACKEND_H_
6 #define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_BACKEND_H_
11 #include "base/containers/scoped_ptr_hash_map.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/macros.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/weak_ptr.h"
16 #include "components/password_manager/core/browser/affiliation_fetch_throttler_delegate.h"
17 #include "components/password_manager/core/browser/affiliation_fetcher_delegate.h"
18 #include "components/password_manager/core/browser/affiliation_service.h"
19 #include "components/password_manager/core/browser/affiliation_utils.h"
20 #include "components/password_manager/core/browser/facet_manager_host.h"
25 class SingleThreadTaskRunner
;
32 class URLRequestContextGetter
;
35 namespace password_manager
{
37 class AffiliationDatabase
;
38 class AffiliationFetcher
;
39 class AffiliationFetchThrottler
;
42 // The AffiliationBackend is the part of the AffiliationService that lives on a
43 // background thread suitable for performing blocking I/O. As most tasks require
44 // I/O, the backend ends up doing most of the work for the AffiliationService;
45 // the latter being just a thin layer that delegates most tasks to the backend.
47 // This class is not thread-safe, but it is fine to construct it on one thread
48 // and then transfer it to the background thread for the rest of its life.
49 // Initialize() must be called already on the final (background) thread.
50 class AffiliationBackend
: public FacetManagerHost
,
51 public AffiliationFetcherDelegate
,
52 public AffiliationFetchThrottlerDelegate
{
54 using StrategyOnCacheMiss
= AffiliationService::StrategyOnCacheMiss
;
56 // Constructs an instance that will use |request_context_getter| for all
57 // network requests, use |task_runner| for asynchronous tasks, and will rely
58 // on |time_source| and |time_tick_source| to tell the current time/ticks.
59 // Construction is very cheap, expensive steps are deferred to Initialize().
61 const scoped_refptr
<net::URLRequestContextGetter
>& request_context_getter
,
62 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
63 scoped_ptr
<base::Clock
> time_source
,
64 scoped_ptr
<base::TickClock
> time_tick_source
);
65 ~AffiliationBackend() override
;
67 // Performs the I/O-heavy part of initialization. The database used to cache
68 // affiliation information locally will be opened/created at |db_path|.
69 void Initialize(const base::FilePath
& db_path
);
71 // Implementations for methods of the same name in AffiliationService. They
72 // are not documented here again. See affiliation_service.h for details:
74 const FacetURI
& facet_uri
,
75 StrategyOnCacheMiss cache_miss_strategy
,
76 const AffiliationService::ResultCallback
& callback
,
77 const scoped_refptr
<base::TaskRunner
>& callback_task_runner
);
78 void Prefetch(const FacetURI
& facet_uri
, const base::Time
& keep_fresh_until
);
79 void CancelPrefetch(const FacetURI
& facet_uri
,
80 const base::Time
& keep_fresh_until
);
82 void TrimCacheForFacet(const FacetURI
& facet_uri
);
84 // Deletes the cache database file at |db_path|, and all auxiliary files. The
85 // database must be closed before calling this.
86 static void DeleteCache(const base::FilePath
& db_path
);
89 friend class AffiliationBackendTest
;
90 FRIEND_TEST_ALL_PREFIXES(
91 AffiliationBackendTest
,
92 DiscardCachedDataIfNoLongerNeededWithEmptyAffiliation
);
94 // Retrieves the FacetManager corresponding to |facet_uri|, creating it and
95 // storing it into |facet_managers_| if it did not exist.
96 FacetManager
* GetOrCreateFacetManager(const FacetURI
& facet_uri
);
98 // Discards cached data corresponding to |affiliated_facets| unless there are
99 // FacetManagers that still need the data.
100 void DiscardCachedDataIfNoLongerNeeded(
101 const AffiliatedFacets
& affiliated_facets
);
103 // Scheduled by RequestNotificationAtTime() to be called back at times when a
104 // FacetManager needs to be notified.
105 void OnSendNotification(const FacetURI
& facet_uri
);
108 bool ReadAffiliationsFromDatabase(
109 const FacetURI
& facet_uri
,
110 AffiliatedFacetsWithUpdateTime
* affiliations
) override
;
111 void SignalNeedNetworkRequest() override
;
112 void RequestNotificationAtTime(const FacetURI
& facet_uri
,
113 base::Time time
) override
;
115 // AffiliationFetcherDelegate:
116 void OnFetchSucceeded(
117 scoped_ptr
<AffiliationFetcherDelegate::Result
> result
) override
;
118 void OnFetchFailed() override
;
119 void OnMalformedResponse() override
;
121 // AffiliationFetchThrottlerDelegate:
122 bool OnCanSendNetworkRequest() override
;
124 // Returns the number of in-memory FacetManagers. Used only for testing.
125 size_t facet_manager_count_for_testing() { return facet_managers_
.size(); }
127 // Reports the |requested_facet_uri_count| in a single fetch; and the elapsed
128 // time before the first fetch, and in-between subsequent fetches.
129 void ReportStatistics(size_t requested_facet_uri_count
);
131 // To be called after Initialize() to use |throttler| instead of the default
132 // one. Used only for testing.
133 void SetThrottlerForTesting(scoped_ptr
<AffiliationFetchThrottler
> throttler
);
135 // Created in Initialize(), and ensures that all subsequent methods are called
136 // on the same thread.
137 scoped_ptr
<base::ThreadChecker
> thread_checker_
;
139 scoped_refptr
<net::URLRequestContextGetter
> request_context_getter_
;
140 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
141 scoped_ptr
<base::Clock
> clock_
;
142 scoped_ptr
<base::TickClock
> tick_clock_
;
144 scoped_ptr
<AffiliationDatabase
> cache_
;
145 scoped_ptr
<AffiliationFetcher
> fetcher_
;
146 scoped_ptr
<AffiliationFetchThrottler
> throttler_
;
148 base::Time construction_time_
;
149 base::Time last_request_time_
;
151 // Contains a FacetManager for each facet URI that need ongoing attention. To
152 // save memory, managers are discarded as soon as they become redundant.
153 base::ScopedPtrHashMap
<FacetURI
, scoped_ptr
<FacetManager
>> facet_managers_
;
155 base::WeakPtrFactory
<AffiliationBackend
> weak_ptr_factory_
;
157 DISALLOW_COPY_AND_ASSIGN(AffiliationBackend
);
160 } // namespace password_manager
162 #endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_BACKEND_H_