Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/password_manager/core/browser/affiliation_backend.h" | 5 #include "components/password_manager/core/browser/affiliation_backend.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | |
| 10 #include "base/location.h" | |
| 11 #include "base/single_thread_task_runner.h" | |
| 9 #include "base/task_runner.h" | 12 #include "base/task_runner.h" |
| 13 #include "base/thread_task_runner_handle.h" | |
| 10 #include "base/threading/thread_checker.h" | 14 #include "base/threading/thread_checker.h" |
| 11 #include "base/time/clock.h" | 15 #include "base/time/clock.h" |
| 12 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 13 #include "components/password_manager/core/browser/affiliation_database.h" | 17 #include "components/password_manager/core/browser/affiliation_database.h" |
| 14 #include "components/password_manager/core/browser/affiliation_fetcher.h" | 18 #include "components/password_manager/core/browser/affiliation_fetcher.h" |
| 15 #include "components/password_manager/core/browser/facet_manager.h" | 19 #include "components/password_manager/core/browser/facet_manager.h" |
| 16 #include "net/url_request/url_request_context_getter.h" | 20 #include "net/url_request/url_request_context_getter.h" |
| 17 | 21 |
| 18 namespace password_manager { | 22 namespace password_manager { |
| 19 | 23 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 37 NOTIMPLEMENTED(); | 41 NOTIMPLEMENTED(); |
| 38 } | 42 } |
| 39 } | 43 } |
| 40 | 44 |
| 41 void AffiliationBackend::GetAffiliations( | 45 void AffiliationBackend::GetAffiliations( |
| 42 const FacetURI& facet_uri, | 46 const FacetURI& facet_uri, |
| 43 bool cached_only, | 47 bool cached_only, |
| 44 const AffiliationService::ResultCallback& callback, | 48 const AffiliationService::ResultCallback& callback, |
| 45 const scoped_refptr<base::TaskRunner>& callback_task_runner) { | 49 const scoped_refptr<base::TaskRunner>& callback_task_runner) { |
| 46 DCHECK(thread_checker_ && thread_checker_->CalledOnValidThread()); | 50 DCHECK(thread_checker_ && thread_checker_->CalledOnValidThread()); |
| 47 if (!facet_managers_.contains(facet_uri)) { | |
| 48 scoped_ptr<FacetManager> new_manager(new FacetManager(this, facet_uri)); | |
| 49 facet_managers_.add(facet_uri, new_manager.Pass()); | |
| 50 } | |
| 51 | 51 |
| 52 FacetManager* facet_manager = facet_managers_.get(facet_uri); | 52 FacetManager* facet_manager = GetOrCreateFacetManager(facet_uri); |
| 53 DCHECK(facet_manager); | 53 DCHECK(facet_manager); |
| 54 facet_manager->GetAffiliations(cached_only, callback, callback_task_runner); | 54 facet_manager->GetAffiliations(cached_only, callback, callback_task_runner); |
| 55 | 55 |
| 56 if (facet_manager->CanBeDiscarded()) | 56 if (facet_manager->CanBeDiscarded()) |
| 57 facet_managers_.erase(facet_uri); | 57 facet_managers_.erase(facet_uri); |
| 58 } | 58 } |
| 59 | 59 |
| 60 void AffiliationBackend::Prefetch(const FacetURI& facet_uri, | 60 void AffiliationBackend::Prefetch(const FacetURI& facet_uri, |
| 61 const base::Time& keep_fresh_until) { | 61 const base::Time& keep_fresh_until) { |
| 62 DCHECK(thread_checker_ && thread_checker_->CalledOnValidThread()); | 62 DCHECK(thread_checker_ && thread_checker_->CalledOnValidThread()); |
| 63 | 63 |
| 64 // TODO(engedy): Implement this. crbug.com/437865. | 64 FacetManager* facet_manager = GetOrCreateFacetManager(facet_uri); |
| 65 NOTIMPLEMENTED(); | 65 DCHECK(facet_manager); |
| 66 facet_manager->Prefetch(keep_fresh_until); | |
| 67 | |
| 68 if (facet_manager->CanBeDiscarded()) | |
| 69 facet_managers_.erase(facet_uri); | |
| 66 } | 70 } |
| 67 | 71 |
| 68 void AffiliationBackend::CancelPrefetch(const FacetURI& facet_uri, | 72 void AffiliationBackend::CancelPrefetch(const FacetURI& facet_uri, |
| 69 const base::Time& keep_fresh_until) { | 73 const base::Time& keep_fresh_until) { |
| 70 DCHECK(thread_checker_ && thread_checker_->CalledOnValidThread()); | 74 DCHECK(thread_checker_ && thread_checker_->CalledOnValidThread()); |
| 71 | 75 |
| 72 // TODO(engedy): Implement this. crbug.com/437865. | 76 FacetManager* facet_manager = facet_managers_.get(facet_uri); |
| 73 NOTIMPLEMENTED(); | 77 if (!facet_manager) |
| 78 return; | |
| 79 facet_manager->CancelPrefetch(keep_fresh_until); | |
| 80 | |
| 81 if (facet_manager->CanBeDiscarded()) | |
| 82 facet_managers_.erase(facet_uri); | |
| 74 } | 83 } |
| 75 | 84 |
| 76 void AffiliationBackend::TrimCache() { | 85 void AffiliationBackend::TrimCache() { |
| 77 DCHECK(thread_checker_ && thread_checker_->CalledOnValidThread()); | 86 DCHECK(thread_checker_ && thread_checker_->CalledOnValidThread()); |
| 78 | 87 |
| 79 // TODO(engedy): Implement this. crbug.com/437865. | 88 // TODO(engedy): Implement this. crbug.com/437865. |
| 80 NOTIMPLEMENTED(); | 89 NOTIMPLEMENTED(); |
| 81 } | 90 } |
| 82 | 91 |
| 92 FacetManager* AffiliationBackend::GetOrCreateFacetManager( | |
| 93 const FacetURI& facet_uri) { | |
| 94 if (!facet_managers_.contains(facet_uri)) { | |
| 95 scoped_ptr<FacetManager> new_manager( | |
| 96 new FacetManager(facet_uri, this, clock_.get())); | |
| 97 facet_managers_.add(facet_uri, new_manager.Pass()); | |
| 98 } | |
| 99 return facet_managers_.get(facet_uri); | |
| 100 } | |
| 101 | |
| 83 void AffiliationBackend::SendNetworkRequest() { | 102 void AffiliationBackend::SendNetworkRequest() { |
| 84 DCHECK(!fetcher_); | 103 DCHECK(!fetcher_); |
| 85 | 104 |
| 86 std::vector<FacetURI> requested_facet_uris; | 105 std::vector<FacetURI> requested_facet_uris; |
| 87 for (const auto& facet_manager_pair : facet_managers_) { | 106 for (const auto& facet_manager_pair : facet_managers_) { |
| 88 if (facet_manager_pair.second->DoesRequireFetch()) | 107 if (facet_manager_pair.second->DoesRequireFetch()) |
| 89 requested_facet_uris.push_back(facet_manager_pair.first); | 108 requested_facet_uris.push_back(facet_manager_pair.first); |
| 90 } | 109 } |
| 91 DCHECK(!requested_facet_uris.empty()); | 110 DCHECK(!requested_facet_uris.empty()); |
| 92 fetcher_.reset(AffiliationFetcher::Create(request_context_getter_.get(), | 111 fetcher_.reset(AffiliationFetcher::Create(request_context_getter_.get(), |
| 93 requested_facet_uris, this)); | 112 requested_facet_uris, this)); |
| 94 fetcher_->StartRequest(); | 113 fetcher_->StartRequest(); |
| 95 } | 114 } |
| 96 | 115 |
| 97 base::Time AffiliationBackend::GetCurrentTime() { | 116 void AffiliationBackend::OnSendNotification(const FacetURI& facet_uri) { |
| 98 return clock_->Now(); | 117 DCHECK(thread_checker_ && thread_checker_->CalledOnValidThread()); |
| 99 } | |
| 100 | 118 |
| 101 base::Time AffiliationBackend::ReadLastUpdateTimeFromDatabase( | 119 FacetManager* facet_manager = facet_managers_.get(facet_uri); |
| 102 const FacetURI& facet_uri) { | 120 if (!facet_manager) |
| 103 AffiliatedFacetsWithUpdateTime affiliation; | 121 return; |
| 104 return ReadAffiliationsFromDatabase(facet_uri, &affiliation) | 122 facet_manager->NotifyAtRequestedTime(); |
| 105 ? affiliation.last_update_time | 123 |
| 106 : base::Time(); | 124 if (facet_manager->CanBeDiscarded()) |
| 125 facet_managers_.erase(facet_uri); | |
| 107 } | 126 } |
| 108 | 127 |
| 109 bool AffiliationBackend::ReadAffiliationsFromDatabase( | 128 bool AffiliationBackend::ReadAffiliationsFromDatabase( |
| 110 const FacetURI& facet_uri, | 129 const FacetURI& facet_uri, |
| 111 AffiliatedFacetsWithUpdateTime* affiliations) { | 130 AffiliatedFacetsWithUpdateTime* affiliations) { |
| 112 return cache_->GetAffiliationsForFacet(facet_uri, affiliations); | 131 return cache_->GetAffiliationsForFacet(facet_uri, affiliations); |
| 113 } | 132 } |
| 114 | 133 |
| 115 void AffiliationBackend::SignalNeedNetworkRequest() { | 134 void AffiliationBackend::SignalNeedNetworkRequest() { |
| 116 // TODO(engedy): Add more sophisticated throttling logic. crbug.com/437865. | 135 // TODO(engedy): Add more sophisticated throttling logic. crbug.com/437865. |
| 117 if (fetcher_) | 136 if (fetcher_) |
| 118 return; | 137 return; |
| 119 SendNetworkRequest(); | 138 SendNetworkRequest(); |
| 120 } | 139 } |
| 121 | 140 |
| 141 void AffiliationBackend::RequestNotificationAtTime(const FacetURI& facet_uri, | |
| 142 base::Time time) { | |
| 143 // TODO(engedy): Avoid spamming the task runner; only ever schedule the first | |
| 144 // callback. crbug.com/437865. | |
| 145 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
| 146 FROM_HERE, base::Bind(&AffiliationBackend::OnSendNotification, | |
| 147 weak_ptr_factory_.GetWeakPtr(), facet_uri), | |
| 148 time - clock_->Now()); | |
|
Mike West
2015/02/25 10:33:05
Are prefetches meant to be stored across restarts?
engedy
2015/03/10 11:01:00
Indeed, they will not survive a restart. This is d
| |
| 149 } | |
| 150 | |
| 122 void AffiliationBackend::OnFetchSucceeded( | 151 void AffiliationBackend::OnFetchSucceeded( |
| 123 scoped_ptr<AffiliationFetcherDelegate::Result> result) { | 152 scoped_ptr<AffiliationFetcherDelegate::Result> result) { |
| 124 DCHECK(thread_checker_ && thread_checker_->CalledOnValidThread()); | 153 DCHECK(thread_checker_ && thread_checker_->CalledOnValidThread()); |
| 125 fetcher_.reset(); | 154 fetcher_.reset(); |
| 126 | 155 |
| 127 for (const AffiliatedFacets& affiliated_facets : *result) { | 156 for (const AffiliatedFacets& affiliated_facets : *result) { |
| 128 AffiliatedFacetsWithUpdateTime affiliation; | 157 AffiliatedFacetsWithUpdateTime affiliation; |
| 129 affiliation.facets = affiliated_facets; | 158 affiliation.facets = affiliated_facets; |
| 130 affiliation.last_update_time = clock_->Now(); | 159 affiliation.last_update_time = clock_->Now(); |
| 131 | 160 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 167 } | 196 } |
| 168 | 197 |
| 169 void AffiliationBackend::OnMalformedResponse() { | 198 void AffiliationBackend::OnMalformedResponse() { |
| 170 DCHECK(thread_checker_ && thread_checker_->CalledOnValidThread()); | 199 DCHECK(thread_checker_ && thread_checker_->CalledOnValidThread()); |
| 171 | 200 |
| 172 // TODO(engedy): Implement this. crbug.com/437865. | 201 // TODO(engedy): Implement this. crbug.com/437865. |
| 173 NOTIMPLEMENTED(); | 202 NOTIMPLEMENTED(); |
| 174 } | 203 } |
| 175 | 204 |
| 176 } // namespace password_manager | 205 } // namespace password_manager |
| OLD | NEW |