Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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. | |
| 4 #include "components/safe_browsing/password_protection/password_protection_reque st.h" | |
| 5 | |
| 6 #include "base/memory/ptr_util.h" | |
| 7 #include "base/memory/weak_ptr.h" | |
| 8 #include "base/metrics/histogram_macros.h" | |
| 9 #include "components/data_use_measurement/core/data_use_user_data.h" | |
| 10 #include "content/public/browser/browser_thread.h" | |
| 11 #include "net/base/escape.h" | |
| 12 #include "net/base/load_flags.h" | |
| 13 #include "net/base/url_util.h" | |
| 14 #include "net/http/http_status_code.h" | |
| 15 | |
| 16 using content::BrowserThread; | |
| 17 | |
| 18 namespace safe_browsing { | |
| 19 | |
| 20 PasswordProtectionRequest::PasswordProtectionRequest( | |
| 21 const GURL& main_frame_url, | |
| 22 LoginReputationClientRequest::TriggerType type, | |
| 23 bool is_extended_reporting, | |
| 24 bool is_incognito, | |
| 25 base::WeakPtr<PasswordProtectionService> pps, | |
| 26 int request_timeout_in_ms) | |
| 27 : main_frame_url_(main_frame_url), | |
| 28 request_type_(type), | |
| 29 is_extended_reporting_(is_extended_reporting), | |
| 30 is_incognito_(is_incognito), | |
| 31 password_protection_service_(pps), | |
| 32 request_timeout_in_ms_(request_timeout_in_ms), | |
| 33 weakptr_factory_(this) { | |
| 34 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 35 } | |
| 36 | |
| 37 PasswordProtectionRequest::~PasswordProtectionRequest() { | |
| 38 weakptr_factory_.InvalidateWeakPtrs(); | |
| 39 } | |
| 40 | |
| 41 void PasswordProtectionRequest::Start() { | |
| 42 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 43 // Initially we only send ping for Safe Browsing Extended Reporting users when | |
| 44 // they are not in incognito mode. We may loose these conditions later. | |
| 45 if (is_incognito_) { | |
| 46 Finish(RequestOutcome::INCOGNITO, nullptr); | |
| 47 return; | |
| 48 } | |
| 49 if (!is_extended_reporting_) { | |
| 50 Finish(RequestOutcome::NO_EXTENDED_REPORTING, nullptr); | |
| 51 return; | |
| 52 } | |
| 53 | |
| 54 // In case the request take too long, we set a timer to cancel this request. | |
| 55 StartTimeout(); | |
| 56 | |
| 57 CheckWhitelistsOnUIThread(); | |
| 58 } | |
| 59 | |
| 60 void PasswordProtectionRequest::Cancel(bool timed_out) { | |
| 61 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 62 if (fetcher_.get()) { | |
|
Nathan Parker
2017/03/23 20:45:50
nit: You can .reset() unconditionally
Jialiu Lin
2017/03/23 22:42:59
Done.
| |
| 63 // If there is already a URLFetcher started, we should cancel it. | |
| 64 fetcher_.reset(); | |
| 65 } | |
| 66 | |
| 67 Finish(timed_out ? TIMEDOUT : CANCELED, nullptr); | |
|
Nathan Parker
2017/03/23 20:45:50
Is the expectation that Cancel() be followed by de
Jialiu Lin
2017/03/23 22:42:59
Yes, and Finish(..) will destroy this object by as
Nathan Parker
2017/03/24 00:41:50
Can you add a comment on Cancel() in the .h to tha
Jialiu Lin
2017/03/24 01:42:42
Already did. :-)
| |
| 68 } | |
| 69 | |
| 70 void PasswordProtectionRequest::StartTimeout() { | |
| 71 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 72 timeout_start_time_ = base::TimeTicks::Now(); | |
| 73 | |
| 74 // If request is not done withing 10 milliseconds, we cancel this request. | |
|
Nathan Parker
2017/03/23 20:45:49
nit: 10 seconds
Jialiu Lin
2017/03/23 22:42:59
Done.
| |
| 75 // The weak pointer used for the timeout will be invalidated (and | |
| 76 // hence would prevent the timeout) if the check completes on time and | |
| 77 // execution reaches Finish(). | |
| 78 BrowserThread::PostDelayedTask( | |
| 79 BrowserThread::UI, FROM_HERE, | |
| 80 base::Bind(&PasswordProtectionRequest::Cancel, GetWeakPtr(), true), | |
| 81 base::TimeDelta::FromMilliseconds(request_timeout_in_ms_)); | |
| 82 } | |
| 83 | |
| 84 void PasswordProtectionRequest::Finish( | |
| 85 RequestOutcome outcome, | |
| 86 std::unique_ptr<LoginReputationClientResponse> response) { | |
| 87 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 88 | |
| 89 UMA_HISTOGRAM_ENUMERATION("PasswordProtection.RequestOutcome", outcome, | |
| 90 RequestOutcome::MAX_OUTCOME); | |
| 91 if (!timeout_start_time_.is_null()) { | |
| 92 UMA_HISTOGRAM_TIMES("PasswordProtection.RequestDuration", | |
| 93 base::TimeTicks::Now() - timeout_start_time_); | |
|
Nathan Parker
2017/03/23 20:45:49
Is this recorded time any different from PasswordP
Jialiu Lin
2017/03/23 22:42:59
NetworkDuration measures the time from SendRequest
Nathan Parker
2017/03/24 00:41:50
I suspect the only delay > 1 ms is the network cal
Jialiu Lin
2017/03/24 01:42:42
you're right. The differences will be tiny. Let m
| |
| 94 } | |
| 95 | |
| 96 if (response) { | |
| 97 if (request_type_ == LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE) { | |
|
Nathan Parker
2017/03/23 20:45:50
How about a switch statement with no default, to e
Nathan Parker
2017/03/23 20:45:50
I thought this was going to be LOW_REPUTATION -- m
Jialiu Lin
2017/03/23 22:42:59
UNFAMILIAR_LOGIN_PAGE is the request trigger type
Jialiu Lin
2017/03/23 22:43:00
Done.
Nathan Parker
2017/03/24 00:41:50
AH yes, nm.
| |
| 98 UMA_HISTOGRAM_ENUMERATION( | |
| 99 "PasswordProtection.UnfamiliarLoginPageVerdict", | |
| 100 response->verdict_type(), | |
| 101 LoginReputationClientResponse_VerdictType_VerdictType_MAX + 1); | |
| 102 } else if (request_type_ == | |
| 103 LoginReputationClientRequest::PASSWORD_REUSE_EVENT) { | |
| 104 UMA_HISTOGRAM_ENUMERATION( | |
| 105 "PasswordProtection.PasswordReuseEventVerdict", | |
| 106 response->verdict_type(), | |
| 107 LoginReputationClientResponse_VerdictType_VerdictType_MAX + 1); | |
| 108 } | |
| 109 } | |
| 110 | |
| 111 DCHECK(password_protection_service_); | |
| 112 password_protection_service_->RequestFinished(this, std::move(response)); | |
| 113 } | |
| 114 | |
| 115 // TODO(jialiul): there are some duplicated code | |
| 116 void PasswordProtectionRequest::CheckWhitelistsOnUIThread() { | |
| 117 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 118 if (!password_protection_service_) { | |
|
Nathan Parker
2017/03/23 20:45:50
Is this possible? The PPS is shut down on the UI
Jialiu Lin
2017/03/23 22:42:59
Just a safe measure, since PPS is a weakptr. But i
| |
| 119 Finish(RequestOutcome::SERVICE_DESTROYED, nullptr); | |
| 120 return; | |
| 121 } | |
| 122 | |
| 123 BrowserThread::PostTask( | |
| 124 BrowserThread::IO, FROM_HERE, | |
| 125 base::Bind(&PasswordProtectionService::CheckCsdWhitelistOnIOThread, | |
| 126 password_protection_service_, main_frame_url_, | |
| 127 base::Bind(&PasswordProtectionRequest::OnWhitelistCheckDone, | |
| 128 GetWeakPtr()))); | |
| 129 } | |
| 130 | |
| 131 void PasswordProtectionRequest::OnWhitelistCheckDone(bool match_whitelist) { | |
| 132 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 133 if (match_whitelist) | |
| 134 Finish(RequestOutcome::MATCHED_WHITELIST, nullptr); | |
| 135 else | |
| 136 CheckCachedVerdicts(); | |
| 137 } | |
| 138 | |
| 139 void PasswordProtectionRequest::CheckCachedVerdicts() { | |
| 140 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 141 if (!password_protection_service_) { | |
| 142 Finish(RequestOutcome::SERVICE_DESTROYED, nullptr); | |
| 143 return; | |
| 144 } | |
| 145 | |
| 146 std::unique_ptr<LoginReputationClientResponse> cached_response = | |
| 147 base::MakeUnique<LoginReputationClientResponse>(); | |
| 148 auto verdict = password_protection_service_->GetCachedVerdict( | |
| 149 password_protection_service_->GetSettingMapForActiveProfile(), | |
| 150 main_frame_url_, cached_response.get()); | |
| 151 if (verdict != LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED) | |
| 152 Finish(RequestOutcome::RESPONSE_ALREADY_CACHED, std::move(cached_response)); | |
| 153 else | |
| 154 SendRequest(); | |
| 155 } | |
| 156 | |
| 157 void PasswordProtectionRequest::OnURLFetchComplete( | |
| 158 const net::URLFetcher* source) { | |
| 159 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 160 if (!source->GetStatus().is_success() || | |
| 161 net::HTTP_OK != source->GetResponseCode()) { | |
| 162 Finish(RequestOutcome::FETCH_FAILED, nullptr); | |
|
Nathan Parker
2017/03/23 20:45:50
Add todo: log to UMA the httpResponseOrErrorCode l
Jialiu Lin
2017/03/23 22:42:59
Done. UMA metrics added.
| |
| 163 return; | |
| 164 } | |
| 165 std::unique_ptr<LoginReputationClientResponse> response = | |
| 166 base::MakeUnique<LoginReputationClientResponse>(); | |
| 167 std::string response_body; | |
| 168 bool received_data = source->GetResponseAsString(&response_body); | |
| 169 DCHECK(received_data); | |
| 170 fetcher_.reset(); // We don't need it anymore. | |
| 171 UMA_HISTOGRAM_TIMES("PasswordProtection.RequestNetworkDuration", | |
| 172 base::TimeTicks::Now() - request_start_time_); | |
| 173 if (response->ParseFromString(response_body)) { | |
| 174 Finish(RequestOutcome::SUCCEEDED, std::move(response)); | |
| 175 } else { | |
| 176 Finish(RequestOutcome::RESPONSE_MALFORMED, nullptr); | |
| 177 } | |
| 178 } | |
| 179 | |
| 180 void PasswordProtectionRequest::SendRequest() { | |
| 181 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 182 // This is the last chance to check whether password_protection_service_ is | |
| 183 // still available. If not, drop the request. | |
| 184 if (!password_protection_service_) { | |
|
Nathan Parker
2017/03/23 20:45:50
This was already checked in CheckCachedVerdicts, w
Jialiu Lin
2017/03/23 22:42:59
Right. removed this check.
| |
| 185 Finish(RequestOutcome::SERVICE_DESTROYED, nullptr); | |
| 186 return; | |
| 187 } | |
| 188 | |
| 189 LoginReputationClientRequest request; | |
| 190 request.set_page_url(main_frame_url_.spec()); | |
| 191 request.set_trigger_type(request_type_); | |
| 192 request.set_stored_verdict_cnt( | |
| 193 password_protection_service_->GetStoredVerdictCount()); | |
| 194 | |
| 195 std::string serialized_request; | |
| 196 if (!request.SerializeToString(&serialized_request)) { | |
| 197 Finish(RequestOutcome::REQUEST_MALFORMED, nullptr); | |
| 198 return; | |
| 199 } | |
| 200 | |
| 201 fetcher_ = net::URLFetcher::Create( | |
| 202 0, PasswordProtectionService::GetPasswordProtectionRequestUrl(), | |
| 203 net::URLFetcher::POST, this); | |
| 204 data_use_measurement::DataUseUserData::AttachToFetcher( | |
| 205 fetcher_.get(), data_use_measurement::DataUseUserData::SAFE_BROWSING); | |
| 206 fetcher_->SetLoadFlags(net::LOAD_DISABLE_CACHE); | |
| 207 fetcher_->SetAutomaticallyRetryOn5xx(false); | |
| 208 fetcher_->SetRequestContext( | |
| 209 password_protection_service_->request_context_getter().get()); | |
| 210 fetcher_->SetUploadData("application/octet-stream", serialized_request); | |
| 211 request_start_time_ = base::TimeTicks::Now(); | |
| 212 fetcher_->Start(); | |
| 213 } | |
| 214 | |
| 215 } // namespace safe_browsing | |
| OLD | NEW |